Annotation of chimera/NSBrowserView.mm, revision 1.23

1.1       hyatt       1: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
                      2: /* ***** BEGIN LICENSE BLOCK *****
                      3:  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
                      4:  *
                      5:  * The contents of this file are subject to the Netscape Public License
                      6:  * Version 1.1 (the "License"); you may not use this file except in
                      7:  * compliance with the License. You may obtain a copy of the License at
                      8:  * http://www.mozilla.org/NPL/
                      9:  *
                     10:  * Software distributed under the License is distributed on an "AS IS" basis,
                     11:  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
                     12:  * for the specific language governing rights and limitations under the
                     13:  * License.
                     14:  *
                     15:  * The Original Code is mozilla.org code.
                     16:  *
                     17:  * The Initial Developer of the Original Code is 
                     18:  * Netscape Communications Corporation.
                     19:  * Portions created by the Initial Developer are Copyright (C) 1998
                     20:  * the Initial Developer. All Rights Reserved.
                     21:  *
                     22:  * Contributor(s):
                     23:  *
                     24:  * Alternatively, the contents of this file may be used under the terms of
                     25:  * either the GNU General Public License Version 2 or later (the "GPL"), or 
                     26:  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
                     27:  * in which case the provisions of the GPL or the LGPL are applicable instead
                     28:  * of those above. If you wish to allow use of your version of this file only
                     29:  * under the terms of either the GPL or the LGPL, and not to allow others to
                     30:  * use your version of this file under the terms of the NPL, indicate your
                     31:  * decision by deleting the provisions above and replace them with the notice
                     32:  * and other provisions required by the GPL or the LGPL. If you do not delete
                     33:  * the provisions above, a recipient may use your version of this file under
                     34:  * the terms of any one of the NPL, the GPL or the LGPL.
                     35:  *
                     36:  * ***** END LICENSE BLOCK ***** */
                     37: 
                     38: #import "NSBrowserView.h"
1.2       hyatt      39: #import "ProgressDlgController.h"
1.15      hyatt      40: #import "FindDlgController.h"
1.1       hyatt      41: #import "nsCocoaBrowserService.h"
                     42: 
                     43: // Embedding includes
                     44: #include "nsCWebBrowser.h"
                     45: #include "nsIInterfaceRequestor.h"
                     46: #include "nsIWebBrowserChrome.h"
                     47: #include "nsIEmbeddingSiteWindow.h"
                     48: #include "nsIWebProgressListener.h"
                     49: #include "nsIWebBrowser.h"
                     50: #include "nsIWebNavigation.h"
                     51: #include "nsIURI.h"
                     52: #include "nsIDOMWindow.h"
                     53: #include "nsWeakReference.h"
                     54: 
                     55: // XPCOM and String includes
                     56: #include "nsCRT.h"
                     57: #include "nsXPIDLString.h"
                     58: #include "nsCOMPtr.h"
                     59: 
1.8       hyatt      60: // Printing
                     61: #include "nsIWebBrowserPrint.h"
                     62: #include "nsIPrintSettings.h"
                     63: 
                     64: // Saving of links/images/docs
1.2       hyatt      65: #include "nsIWebBrowserFocus.h"
                     66: #include "nsIDOMHTMLDocument.h"
1.17      hyatt      67: #include "nsIDOMNSDocument.h"
                     68: #include "nsIDOMLocation.h"
1.2       hyatt      69: #include "nsIURL.h"
                     70: #include "nsIWebBrowserPersist.h"
                     71: #include "nsIProperties.h"
                     72: #include "nsIRequest.h"
                     73: #include "nsIChannel.h"
                     74: #include "nsIHttpChannel.h"
                     75: #include "nsIPref.h"
                     76: #include "nsIMIMEService.h"
                     77: #include "nsIMIMEInfo.h"
                     78: #include "nsISHistory.h"
1.3       hyatt      79: #include "nsIHistoryEntry.h"
                     80: #include "nsISHEntry.h"
1.17      hyatt      81: #include "nsNetUtil.h"
1.1       hyatt      82: 
1.11      hyatt      83: // Cut/copy/paste
                     84: #include "nsIClipboardCommands.h"
                     85: #include "nsIInterfaceRequestorUtils.h"
                     86: 
1.4       hyatt      87: const char* persistContractID = "@mozilla.org/embedding/browser/nsWebBrowserPersist;1";
                     88: const char* dirServiceContractID = "@mozilla.org/file/directory_service;1";
                     89: 
1.1       hyatt      90: class nsCocoaBrowserListener : public nsSupportsWeakReference,
                     91:                                public nsIInterfaceRequestor,
1.22      hyatt      92:                         public nsIWebBrowserChrome,
                     93:                                public nsIWindowCreator,
                     94:                         public nsIEmbeddingSiteWindow,
1.1       hyatt      95:                                public nsIWebProgressListener
                     96: {
                     97: public:
                     98:   nsCocoaBrowserListener(NSBrowserView* aView);
                     99:   virtual ~nsCocoaBrowserListener();
                    100: 
                    101:   NS_DECL_ISUPPORTS
                    102:   NS_DECL_NSIINTERFACEREQUESTOR
                    103:   NS_DECL_NSIWEBBROWSERCHROME
1.22      hyatt     104:   NS_DECL_NSIWINDOWCREATOR
1.1       hyatt     105:   NS_DECL_NSIEMBEDDINGSITEWINDOW
                    106:   NS_DECL_NSIWEBPROGRESSLISTENER
                    107: 
                    108:   void AddListener(id <NSBrowserListener> aListener);
                    109:   void RemoveListener(id <NSBrowserListener> aListener);
                    110:   void SetContainer(id <NSBrowserContainer> aContainer);
                    111: 
                    112: private:
                    113:   NSBrowserView* mView;     // WEAK - it owns us
                    114:   NSMutableArray* mListeners;
                    115:   id <NSBrowserContainer> mContainer;
                    116:   PRBool mIsModal;
                    117:   PRUint32 mChromeFlags;
                    118: };
                    119: 
                    120: nsCocoaBrowserListener::nsCocoaBrowserListener(NSBrowserView* aView)
                    121:   : mView(aView), mContainer(nsnull), mIsModal(PR_FALSE), mChromeFlags(0)
                    122: {
                    123:   NS_INIT_ISUPPORTS();
                    124:   mListeners = [[NSMutableArray alloc] init];
                    125: }
                    126: 
                    127: nsCocoaBrowserListener::~nsCocoaBrowserListener()
                    128: {
                    129:   [mListeners release];
                    130:   mView = nsnull;
                    131:   if (mContainer) {
                    132:     [mContainer release];
                    133:   }
                    134: }
                    135: 
1.22      hyatt     136: NS_IMPL_ISUPPORTS6(nsCocoaBrowserListener,
1.1       hyatt     137:       nsIInterfaceRequestor,
                    138:       nsIWebBrowserChrome,
1.22      hyatt     139:            nsIWindowCreator,
1.1       hyatt     140:       nsIEmbeddingSiteWindow,
                    141:       nsIWebProgressListener,
1.22      hyatt     142:            nsISupportsWeakReference)
1.1       hyatt     143: 
                    144: // Implementation of nsIInterfaceRequestor
                    145: NS_IMETHODIMP 
                    146: nsCocoaBrowserListener::GetInterface(const nsIID &aIID, void** aInstancePtr)
                    147: {
                    148:   if (aIID.Equals(NS_GET_IID(nsIDOMWindow))) {
                    149:     nsCOMPtr<nsIWebBrowser> browser = dont_AddRef([mView getWebBrowser]);
                    150:     if (browser)
                    151:       return browser->GetContentDOMWindow((nsIDOMWindow **) aInstancePtr);
                    152:   }
                    153:   
                    154:   return QueryInterface(aIID, aInstancePtr);
                    155: }
                    156: 
1.22      hyatt     157: // Implementation of nsIWindowCreator.  The CocoaBrowserService forwards requests
                    158: // for a new window that have a parent to us, and we take over from there.  
                    159: /* nsIWebBrowserChrome createChromeWindow (in nsIWebBrowserChrome parent, in PRUint32 chromeFlags); */
                    160: NS_IMETHODIMP 
                    161: nsCocoaBrowserListener::CreateChromeWindow(nsIWebBrowserChrome *parent, 
                    162:                                            PRUint32 chromeFlags, 
                    163:                                            nsIWebBrowserChrome **_retval)
                    164: {
                    165:   if (parent != this) {
                    166:     printf("Mismatch in nsCocoaBrowserListener::CreateChromeWindow.  We should be the owning parent.\n");
                    167:     return NS_ERROR_FAILURE;
                    168:   }
                    169:   
                    170:   NSBrowserView* childView = [mContainer createBrowserWindow: chromeFlags];
                    171:   if (!childView) {
                    172:     printf("No NSBrowserView hooked up for a newly created window yet.\n");
                    173:     return NS_ERROR_FAILURE;
                    174:   }
                    175:   
                    176:   nsCocoaBrowserListener* listener = [childView getCocoaBrowserListener];
                    177:   if (!listener) {
                    178:     printf("Uh-oh! No listener yet for a newly created window (nsCocoaBrowserlistener)\n");
                    179:     return NS_ERROR_FAILURE;
                    180:   }
                    181:   
                    182:   *_retval = listener;
                    183:   NS_IF_ADDREF(*_retval);
                    184:   return NS_OK;
                    185: }
                    186: 
1.1       hyatt     187: // Implementation of nsIWebBrowserChrome
                    188: /* void setStatus (in unsigned long statusType, in wstring status); */
                    189: NS_IMETHODIMP 
                    190: nsCocoaBrowserListener::SetStatus(PRUint32 statusType, const PRUnichar *status)
                    191: {
                    192:   if (!mContainer) {
                    193:     return NS_ERROR_FAILURE;
                    194:   }
                    195: 
                    196:   NSString* str = nsnull;
                    197:   if (status && (*status != PRUnichar(0))) {
                    198:     str = [NSString stringWithCharacters:status length:nsCRT::strlen(status)];
                    199:   }
                    200: 
                    201:   [mContainer setStatus:str ofType:(NSStatusType)statusType];
                    202: 
                    203:   return NS_OK;
                    204: }
                    205: 
                    206: /* attribute nsIWebBrowser webBrowser; */
                    207: NS_IMETHODIMP 
                    208: nsCocoaBrowserListener::GetWebBrowser(nsIWebBrowser * *aWebBrowser)
                    209: {
                    210:   NS_ENSURE_ARG_POINTER(aWebBrowser);
                    211:   if (!mView) {
                    212:     return NS_ERROR_FAILURE;
                    213:   }
                    214:   *aWebBrowser = [mView getWebBrowser];
                    215: 
                    216:   return NS_OK;
                    217: }
                    218: NS_IMETHODIMP 
                    219: nsCocoaBrowserListener::SetWebBrowser(nsIWebBrowser * aWebBrowser)
                    220: {
                    221:   if (!mView) {
                    222:     return NS_ERROR_FAILURE;
                    223:   }
                    224: 
                    225:   [mView setWebBrowser:aWebBrowser];
                    226: 
                    227:   return NS_OK;
                    228: }
                    229: 
                    230: /* attribute unsigned long chromeFlags; */
                    231: NS_IMETHODIMP 
                    232: nsCocoaBrowserListener::GetChromeFlags(PRUint32 *aChromeFlags)
                    233: {
                    234:   NS_ENSURE_ARG_POINTER(aChromeFlags);
                    235:   *aChromeFlags = mChromeFlags;
                    236:   return NS_OK;
                    237: }
                    238: NS_IMETHODIMP 
                    239: nsCocoaBrowserListener::SetChromeFlags(PRUint32 aChromeFlags)
                    240: {
                    241:   // XXX Do nothing with them for now
                    242:   mChromeFlags = aChromeFlags;
                    243:   return NS_OK;
                    244: }
                    245: 
                    246: /* void destroyBrowserWindow (); */
                    247: NS_IMETHODIMP 
                    248: nsCocoaBrowserListener::DestroyBrowserWindow()
                    249: {
                    250:   // XXX Could send this up to the container, but for now,
                    251:   // we just destroy the enclosing window.
                    252:   NSWindow* window = [mView window];
                    253: 
                    254:   if (window) {
                    255:     [window close];
                    256:   }
                    257: 
                    258:   return NS_OK;
                    259: }
                    260: 
                    261: /* void sizeBrowserTo (in long aCX, in long aCY); */
                    262: NS_IMETHODIMP 
                    263: nsCocoaBrowserListener::SizeBrowserTo(PRInt32 aCX, PRInt32 aCY)
                    264: {
                    265:   if (mContainer) {
                    266:     NSSize size;
                    267:     
                    268:     size.width = (float)aCX;
                    269:     size.height = (float)aCY;
                    270: 
                    271:     [mContainer sizeBrowserTo:size];
                    272:   }
                    273:   
                    274:   return NS_OK;
                    275: }
                    276: 
                    277: /* void showAsModal (); */
                    278: NS_IMETHODIMP 
                    279: nsCocoaBrowserListener::ShowAsModal()
                    280: {
                    281:   if (!mView) {
                    282:     return NS_ERROR_FAILURE;
                    283:   }
                    284: 
                    285:   NSWindow* window = [mView window];
                    286: 
                    287:   if (!window) {
                    288:     return NS_ERROR_FAILURE;
                    289:   }
                    290: 
                    291:   mIsModal = PR_TRUE;
1.22      hyatt     292:   //int result = [NSApp runModalForWindow:window];
1.1       hyatt     293:   mIsModal = PR_FALSE;
                    294: 
1.22      hyatt     295:   return NS_OK;
1.1       hyatt     296: }
                    297: 
                    298: /* boolean isWindowModal (); */
                    299: NS_IMETHODIMP 
                    300: nsCocoaBrowserListener::IsWindowModal(PRBool *_retval)
                    301: {
                    302:   NS_ENSURE_ARG_POINTER(_retval);
                    303: 
                    304:   *_retval = mIsModal;
                    305: 
                    306:   return NS_OK;
                    307: }
                    308: 
                    309: /* void exitModalEventLoop (in nsresult aStatus); */
                    310: NS_IMETHODIMP 
                    311: nsCocoaBrowserListener::ExitModalEventLoop(nsresult aStatus)
                    312: {
1.22      hyatt     313: //  [NSApp stopModalWithCode:(int)aStatus];
1.1       hyatt     314: 
                    315:   return NS_OK;
                    316: }
                    317: 
                    318: // Implementation of nsIEmbeddingSiteWindow
                    319: /* void setDimensions (in unsigned long flags, in long x, in long y, in long cx, in long cy); */
                    320: NS_IMETHODIMP 
                    321: nsCocoaBrowserListener::SetDimensions(PRUint32 flags, 
                    322:              PRInt32 x, 
                    323:              PRInt32 y, 
                    324:              PRInt32 cx, 
                    325:              PRInt32 cy)
                    326: {
                    327:   if (!mView) {
                    328:     return NS_ERROR_FAILURE;
                    329:   }
                    330: 
                    331:   NSWindow* window = [mView window];
                    332:   if (!window) {
                    333:     return NS_ERROR_FAILURE;
                    334:   }
                    335: 
                    336:   if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION) {
                    337:     NSPoint origin;
                    338:     origin.x = (float)x;
                    339:     origin.y = (float)y;
                    340:     [window setFrameOrigin:origin];
                    341:   }
                    342: 
                    343:   if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER) {
                    344:     NSRect frame = [window frame];
                    345:     frame.size.width = (float)cx;
                    346:     frame.size.height = (float)cy;
                    347:     [window setFrame:frame display:YES];
                    348:   }
                    349:   else if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER) {
                    350:     NSSize size;
                    351:     size.width = (float)cx;
                    352:     size.height = (float)cy;
                    353:     [window setContentSize:size];
                    354:   }
                    355: 
                    356:   return NS_OK;
                    357: }
                    358: 
                    359: /* void getDimensions (in unsigned long flags, out long x, out long y, out long cx, out long cy); */
                    360: NS_IMETHODIMP 
                    361: nsCocoaBrowserListener::GetDimensions(PRUint32 flags, 
                    362:              PRInt32 *x, 
                    363:              PRInt32 *y, 
                    364:              PRInt32 *cx, 
                    365:              PRInt32 *cy)
                    366: {
                    367:   NS_ENSURE_ARG_POINTER(x);
                    368:   NS_ENSURE_ARG_POINTER(y);
                    369:   NS_ENSURE_ARG_POINTER(cx);
                    370:   NS_ENSURE_ARG_POINTER(cy);
                    371: 
                    372:   if (!mView) {
                    373:     return NS_ERROR_FAILURE;
                    374:   }
                    375: 
                    376:   NSWindow* window = [mView window];
                    377:   if (!window) {
                    378:     return NS_ERROR_FAILURE;
                    379:   }
                    380: 
                    381:   NSRect frame = [window frame];
                    382:   if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION) {
                    383:     *x = (PRInt32)frame.origin.x;
                    384:     *y = (PRInt32)frame.origin.y;
                    385:   }
                    386:   if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER) {
                    387:     *cx = (PRInt32)frame.size.width;
                    388:     *cy = (PRInt32)frame.size.height;
                    389:   }
                    390:   else if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER) {
                    391:     NSView* contentView = [window contentView];
                    392:     NSRect contentFrame = [contentView frame];
                    393:     *cx = (PRInt32)contentFrame.size.width;
                    394:     *cy = (PRInt32)contentFrame.size.height;    
                    395:   }
                    396: 
                    397:   return NS_OK;
                    398: }
                    399: 
                    400: /* void setFocus (); */
                    401: NS_IMETHODIMP 
                    402: nsCocoaBrowserListener::SetFocus()
                    403: {
                    404:   if (!mView) {
                    405:     return NS_ERROR_FAILURE;
                    406:   }
                    407: 
                    408:   NSWindow* window = [mView window];
                    409:   if (!window) {
                    410:     return NS_ERROR_FAILURE;
                    411:   }
                    412: 
                    413:   [window makeKeyAndOrderFront:window];
                    414: 
                    415:   return NS_OK;
                    416: }
                    417: 
                    418: /* attribute boolean visibility; */
                    419: NS_IMETHODIMP 
                    420: nsCocoaBrowserListener::GetVisibility(PRBool *aVisibility)
                    421: {
                    422:   NS_ENSURE_ARG_POINTER(aVisibility);
                    423: 
                    424:   if (!mView) {
                    425:     return NS_ERROR_FAILURE;
                    426:   }
                    427: 
                    428:   NSWindow* window = [mView window];
                    429:   if (!window) {
                    430:     return NS_ERROR_FAILURE;
                    431:   }
                    432: 
                    433:   *aVisibility = [window isMiniaturized];
                    434: 
                    435:   return NS_OK;
                    436: }
                    437: NS_IMETHODIMP 
                    438: nsCocoaBrowserListener::SetVisibility(PRBool aVisibility)
                    439: {
                    440:   if (!mView) {
                    441:     return NS_ERROR_FAILURE;
                    442:   }
                    443: 
                    444:   NSWindow* window = [mView window];
                    445:   if (!window) {
                    446:     return NS_ERROR_FAILURE;
                    447:   }
                    448: 
                    449:   if (aVisibility) {
                    450:     [window deminiaturize:window];
                    451:   }
                    452:   else {
                    453:     [window miniaturize:window];
                    454:   }
                    455: 
                    456:   return NS_OK;
                    457: }
                    458: 
                    459: /* attribute wstring title; */
                    460: NS_IMETHODIMP 
                    461: nsCocoaBrowserListener::GetTitle(PRUnichar * *aTitle)
                    462: {
                    463:   NS_ENSURE_ARG_POINTER(aTitle);
                    464: 
                    465:   if (!mContainer) {
                    466:     return NS_ERROR_FAILURE;
                    467:   }
                    468: 
                    469:   NSString* title = [mContainer title];
                    470:   unsigned int length = [title length];
                    471:   if (length) {
                    472:     *aTitle = (PRUnichar*)nsMemory::Alloc((length+1)*sizeof(PRUnichar));
                    473:     if (!*aTitle) {
                    474:       return NS_ERROR_OUT_OF_MEMORY;
                    475:     }
                    476:     [title getCharacters:*aTitle];
                    477:   }
                    478:   else {
                    479:     *aTitle = nsnull;
                    480:   }
                    481:   
                    482:   return NS_OK;
                    483: }
                    484: NS_IMETHODIMP 
                    485: nsCocoaBrowserListener::SetTitle(const PRUnichar * aTitle)
                    486: {
                    487:   NS_ENSURE_ARG(aTitle);
                    488: 
                    489:   if (!mContainer) {
                    490:     return NS_ERROR_FAILURE;
                    491:   }
                    492: 
                    493:   NSString* str = [NSString stringWithCharacters:aTitle length:nsCRT::strlen(aTitle)];
                    494:   [mContainer setTitle:str];
                    495: 
                    496:   return NS_OK;
                    497: }
                    498: 
                    499: /* [noscript] readonly attribute voidPtr siteWindow; */
                    500: NS_IMETHODIMP 
                    501: nsCocoaBrowserListener::GetSiteWindow(void * *aSiteWindow)
                    502: {
                    503:   NS_ENSURE_ARG_POINTER(aSiteWindow);
                    504: 
                    505:   if (!mView) {
                    506:     return NS_ERROR_FAILURE;
                    507:   }
                    508: 
                    509:   NSWindow* window = [mView window];
                    510:   if (!window) {
                    511:     return NS_ERROR_FAILURE;
                    512:   }
                    513: 
                    514:   *aSiteWindow = (void*)window;
                    515: 
                    516:   return NS_OK;
                    517: }
                    518: 
                    519: // Implementation of nsIWebProgressListener
                    520: /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aStateFlags, in unsigned long aStatus); */
                    521: NS_IMETHODIMP 
                    522: nsCocoaBrowserListener::OnStateChange(nsIWebProgress *aWebProgress, 
                    523:              nsIRequest *aRequest, 
                    524:              PRInt32 aStateFlags, 
                    525:              PRUint32 aStatus)
                    526: {
                    527:   NSEnumerator* enumerator = [mListeners objectEnumerator];
                    528:   id obj;
                    529:   
                    530:   if (aStateFlags & nsIWebProgressListener::STATE_IS_NETWORK) {
                    531:     if (aStateFlags & nsIWebProgressListener::STATE_START) {
                    532:       while ((obj = [enumerator nextObject])) {
                    533:  [obj onLoadingStarted];
                    534:       }
                    535:     }
                    536:     else if (aStateFlags & nsIWebProgressListener::STATE_STOP) {
                    537:       while ((obj = [enumerator nextObject])) {
                    538:  [obj onLoadingCompleted:(NS_SUCCEEDED(aStatus))];
                    539:       }
                    540:     }
                    541:   }
                    542: 
                    543: 
                    544:   return NS_OK;
                    545: }
                    546: 
                    547: /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
                    548: NS_IMETHODIMP 
                    549: nsCocoaBrowserListener::OnProgressChange(nsIWebProgress *aWebProgress, 
                    550:           nsIRequest *aRequest, 
                    551:           PRInt32 aCurSelfProgress, 
                    552:           PRInt32 aMaxSelfProgress, 
                    553:           PRInt32 aCurTotalProgress, 
                    554:           PRInt32 aMaxTotalProgress)
                    555: {
                    556:   NSEnumerator* enumerator = [mListeners objectEnumerator];
                    557:   id obj;
                    558:  
                    559:   while ((obj = [enumerator nextObject])) {
                    560:     [obj onProgressChange:aCurTotalProgress outOf:aMaxTotalProgress];
                    561:   }
                    562:   
                    563:   return NS_OK;
                    564: }
                    565: 
                    566: /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
                    567: NS_IMETHODIMP 
                    568: nsCocoaBrowserListener::OnLocationChange(nsIWebProgress *aWebProgress, 
                    569:           nsIRequest *aRequest, 
                    570:           nsIURI *location)
                    571: {
                    572:   nsXPIDLCString spec;
                    573:   
                    574:   location->GetSpec(getter_Copies(spec));
                    575:   if (!spec) {
                    576:     return NS_ERROR_FAILURE;
                    577:   }
                    578: 
                    579:   const char* cstr = spec.get();
                    580:   NSString* str = [NSString stringWithCString:cstr];
                    581:   NSURL* url = [NSURL URLWithString:str];
                    582: 
                    583:   NSEnumerator* enumerator = [mListeners objectEnumerator];
                    584:   id obj;
                    585:  
                    586:   while ((obj = [enumerator nextObject])) {
                    587:     [obj onLocationChange:url];
                    588:   }
                    589: 
                    590:   return NS_OK;
                    591: }
                    592: 
                    593: /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
                    594: NS_IMETHODIMP 
                    595: nsCocoaBrowserListener::OnStatusChange(nsIWebProgress *aWebProgress, 
                    596:               nsIRequest *aRequest, 
                    597:               nsresult aStatus, 
                    598:               const PRUnichar *aMessage)
                    599: {
1.18      hyatt     600:   nsCAutoString msg; msg.AssignWithConversion(aMessage);
                    601:   
                    602:   NSString* str = [NSString stringWithCString:msg.get()];
                    603:   
                    604:   NSEnumerator* enumerator = [mListeners objectEnumerator];
                    605:   id obj;
                    606:  
                    607:   while ((obj = [enumerator nextObject])) {
                    608:     [obj onStatusChange: str];
                    609:   }
                    610: 
1.1       hyatt     611:   return NS_OK;
                    612: }
                    613: 
                    614: /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long state); */
                    615: NS_IMETHODIMP 
                    616: nsCocoaBrowserListener::OnSecurityChange(nsIWebProgress *aWebProgress, 
                    617:           nsIRequest *aRequest, 
                    618:           PRInt32 state)
                    619: {
                    620:   return NS_OK;
                    621: }
                    622: 
                    623: void 
                    624: nsCocoaBrowserListener::AddListener(id <NSBrowserListener> aListener)
                    625: {
                    626:   [mListeners addObject:aListener];
                    627: }
                    628: 
                    629: void 
                    630: nsCocoaBrowserListener::RemoveListener(id <NSBrowserListener> aListener)
                    631: {
                    632:   [mListeners removeObject:aListener];
                    633: }
                    634: 
                    635: void 
                    636: nsCocoaBrowserListener::SetContainer(id <NSBrowserContainer> aContainer)
                    637: {
                    638:   [mContainer autorelease];
                    639: 
                    640:   mContainer = aContainer;
                    641: 
                    642:   [mContainer retain];
                    643: }
                    644: 
1.2       hyatt     645: // Implementation of a header sniffer class that is used when saving Web pages and images.
                    646: class nsHeaderSniffer :  public nsIWebProgressListener
                    647: {
                    648: public:
                    649:     nsHeaderSniffer(nsIWebBrowserPersist* aPersist, nsIFile* aFile, nsIURI* aURL,
1.3       hyatt     650:                     nsIDOMDocument* aDocument, nsIInputStream* aPostData, PRBool aBypassCache,
1.2       hyatt     651:                     NSView* aFilterView, NSPopUpButton* aFilterList)
                    652:     {
                    653:         NS_INIT_REFCNT();
                    654:         mPersist = aPersist;
                    655:         mTmpFile = aFile;
                    656:         mURL = aURL;
                    657:         mDocument = aDocument;
1.3       hyatt     658:         mPostData = aPostData;
1.2       hyatt     659:         mBypassCache = aBypassCache;
                    660:         mFilterView = aFilterView;
                    661:         mFilterList = aFilterList;
                    662:     }
                    663:                   
1.4       hyatt     664:     virtual ~nsHeaderSniffer() 
                    665:     {
                    666:     };
1.2       hyatt     667: 
                    668:     NS_DECL_ISUPPORTS
                    669:     NS_DECL_NSIWEBPROGRESSLISTENER
                    670:   
                    671: protected:
                    672:     void PerformSave();
                    673:     
                    674: private:
                    675:     nsIWebBrowserPersist* mPersist; // Weak. It owns us as a listener.
                    676:     nsCOMPtr<nsIFile> mTmpFile;
                    677:     nsCOMPtr<nsIURI> mURL;
                    678:     nsCOMPtr<nsIDOMDocument> mDocument;
1.3       hyatt     679:     nsCOMPtr<nsIInputStream> mPostData;
1.2       hyatt     680:     PRBool mBypassCache;
                    681:     nsCString mContentType;
                    682:     nsCString mContentDisposition;
                    683:     NSView* mFilterView;
                    684:     NSPopUpButton* mFilterList;
                    685: };
                    686: 
                    687: NS_IMPL_ISUPPORTS1(nsHeaderSniffer, nsIWebProgressListener)
                    688: 
                    689: // Implementation of nsIWebProgressListener
                    690: /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aStateFlags, in unsigned long aStatus); */
                    691: NS_IMETHODIMP 
                    692: nsHeaderSniffer::OnStateChange(nsIWebProgress *aWebProgress, 
                    693:              nsIRequest *aRequest, 
                    694:              PRInt32 aStateFlags, 
                    695:              PRUint32 aStatus)
                    696: {
                    697:     if (aStateFlags & nsIWebProgressListener::STATE_START) {
                    698:         nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
                    699:         nsXPIDLCString contentType;
                    700:         channel->GetContentType(getter_Copies(contentType));
                    701:         mContentType = contentType;
                    702:         
                    703:         // Get the content-disposition if we're an HTTP channel.
                    704:         nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
                    705:         if (httpChannel) {
                    706:             nsXPIDLCString disp;
                    707:             httpChannel->GetResponseHeader("content-disposition", getter_Copies(disp));
                    708:             mContentDisposition = disp;
                    709:         }
                    710:         
                    711:         mPersist->CancelSave();
                    712:         PRBool exists;
                    713:         mTmpFile->Exists(&exists);
                    714:         if (exists)
                    715:             mTmpFile->Remove(PR_FALSE);
                    716:         PerformSave();
                    717:     }
                    718:     return NS_OK;
                    719: }
                    720: 
                    721: void nsHeaderSniffer::PerformSave()
                    722: {
                    723:     // Are we an HTML document? If so, we will want to append an accessory view to
                    724:     // the save dialog to provide the user with the option of doing a complete
                    725:     // save vs. a single file save.
                    726:     PRBool isHTML = (mDocument && mContentType.Equals("text/html") ||
                    727:                      mContentType.Equals("text/xml") ||
                    728:                      mContentType.Equals("application/xhtml+xml"));
                    729:     
                    730:     // Next find out the directory that we should start in.
                    731:     nsCOMPtr<nsIPrefService> prefs(do_GetService("@mozilla.org/preferences-service;1"));
                    732:     if (!prefs)
                    733:         return;
                    734:     nsCOMPtr<nsIPrefBranch> dirBranch;
                    735:     prefs->GetBranch("browser.download.", getter_AddRefs(dirBranch));
                    736:     PRInt32 filterIndex = 0;
                    737:     if (dirBranch) {
                    738:         nsresult rv = dirBranch->GetIntPref("save_converter_index", &filterIndex);
                    739:         if (NS_FAILED(rv))
                    740:             filterIndex = 0;
                    741:     }
                    742:     if (mFilterList)
                    743:         [mFilterList selectItemAtIndex: filterIndex];
                    744:         
                    745:     // We need to figure out what file name to use.
                    746:     nsCAutoString defaultFileName;
                    747:     
                    748:     if (!mContentDisposition.IsEmpty()) {
                    749:         // (1) Use the HTTP header suggestion.
                    750:         PRInt32 index = mContentDisposition.Find("filename=");
                    751:         if (index >= 0) {
                    752:             // Take the substring following the prefix.
                    753:             index += 9;
                    754:             nsCAutoString filename;
                    755:             mContentDisposition.Right(filename, mContentDisposition.Length() - index);
                    756:             defaultFileName = filename;
                    757:         }
                    758:     }
                    759:     
                    760:     if (defaultFileName.IsEmpty()) {
                    761:         nsCOMPtr<nsIURL> url(do_QueryInterface(mURL));
                    762:         if (url) {
                    763:             nsXPIDLCString fileName;
                    764:             url->GetFileName(getter_Copies(fileName));
                    765:             defaultFileName = fileName; // (2) For file URLs, use the file name.
                    766:         }
                    767:     }
                    768:     
                    769:     if (defaultFileName.IsEmpty() && mDocument && isHTML) {
                    770:         nsCOMPtr<nsIDOMHTMLDocument> htmlDoc(do_QueryInterface(mDocument));
                    771:         nsAutoString title;
                    772:         if (htmlDoc)
                    773:             htmlDoc->GetTitle(title); // (3) Use the title of the document.
                    774:         defaultFileName.AssignWithConversion(title);
                    775:     }
                    776:     
                    777:     if (defaultFileName.IsEmpty()) {
                    778:         // (4) Use the caller provided name. XXXdwh
                    779:     }
                    780: 
                    781:     if (defaultFileName.IsEmpty()) {
                    782:         // (5) Use the host.
                    783:         nsXPIDLCString host;
                    784:         mURL->GetHost(getter_Copies(host));
                    785:         defaultFileName = host;
                    786:     }
                    787:     
                    788:     // One last case to handle about:blank and other fruity untitled pages.
                    789:     if (defaultFileName.IsEmpty())
                    790:         defaultFileName = "untitled";
                    791:         
                    792:     // Validate the file name to ensure legality.
                    793:     for (PRUint32 i = 0; i < defaultFileName.Length(); i++)
                    794:         if (defaultFileName[i] == ':' || defaultFileName[i] == '/')
                    795:             defaultFileName.SetCharAt(i, ' ');
                    796:             
                    797:     // Make sure the appropriate extension is appended to the suggested file name.
                    798:     nsCOMPtr<nsIURI> fileURI(do_CreateInstance("@mozilla.org/network/standard-url;1"));
                    799:     nsCOMPtr<nsIURL> fileURL(do_QueryInterface(fileURI));
                    800:     if (!fileURL)
                    801:         return;
                    802:     fileURL->SetFilePath(defaultFileName.get());
                    803:     
                    804:     nsXPIDLCString fileEx;
                    805:     fileURL->GetFileExtension(getter_Copies(fileEx));
                    806:     nsDependentCString fileExtension(fileEx.get());
                    807:     
                    808:     PRBool setExtension = PR_FALSE;
                    809:     if (mContentType.Equals("text/html")) {
                    810:         if (fileExtension.IsEmpty() || (!fileExtension.Equals("htm") && !fileExtension.Equals("html"))) {
                    811:             defaultFileName += ".html";
                    812:             setExtension = PR_TRUE;
                    813:         }
                    814:     }
                    815:     
                    816:     if (!setExtension && fileExtension.IsEmpty()) {
                    817:         nsCOMPtr<nsIMIMEService> mimeService(do_GetService("@mozilla.org/mime;1"));
                    818:         if (!mimeService)
                    819:             return;
                    820:         nsCOMPtr<nsIMIMEInfo> mimeInfo;
                    821:         mimeService->GetFromMIMEType(mContentType.get(), getter_AddRefs(mimeInfo));
                    822:         
                    823:         PRUint32 extCount;
                    824:         char** extList;
                    825:         mimeInfo->GetFileExtensions(&extCount, &extList);
                    826:         
                    827:         if (extCount > 0) {
                    828:             defaultFileName += ".";
                    829:             defaultFileName += extList[0];
                    830:         }
                    831:     }
                    832:     
                    833:     // Now it's time to pose the save dialog.
                    834:     NSSavePanel* savePanel = [NSSavePanel savePanel];
                    835:     NSString* file = nil;
                    836:     if (!defaultFileName.IsEmpty())
                    837:         file = [[NSString alloc] initWithCString: defaultFileName.get()];
                    838:         
                    839:     if (isHTML)
                    840:         [savePanel setAccessoryView: mFilterView];
                    841:         
                    842:     if ([savePanel runModalForDirectory: nil file: file] == NSFileHandlingPanelCancelButton)
                    843:         return;
                    844:        
                    845:     // Release the file string.
                    846:     [file release];
                    847:     
                    848:     // Update the filter index.
                    849:     if (isHTML && mFilterList) {
                    850:         filterIndex = [mFilterList indexOfSelectedItem];
                    851:         dirBranch->SetIntPref("save_converter_index", filterIndex);
                    852:     }
                    853:     
                    854:     // Convert the content type to text/plain if it was selected in the filter.
                    855:     if (isHTML && filterIndex == 2)
                    856:         mContentType = "text/plain";
                    857:     
                    858:     nsCOMPtr<nsISupports> sourceData;
                    859:     if (isHTML && filterIndex != 1)
                    860:         sourceData = do_QueryInterface(mDocument);
                    861:     else
                    862:         sourceData = do_QueryInterface(mURL);
                    863:         
1.4       hyatt     864:     nsCOMPtr<nsIWebBrowserPersist> webPersist(do_CreateInstance(persistContractID));
1.2       hyatt     865:     ProgressDlgController* progressDialog = [[ProgressDlgController alloc] initWithWindowNibName: @"ProgressDialog"];
1.4       hyatt     866:     [progressDialog setWebPersist: webPersist 
1.2       hyatt     867:                     source: sourceData.get()
                    868:                     destination: [savePanel filename]
                    869:                     contentType: mContentType.get()
1.3       hyatt     870:                     postData: mPostData
                    871:                     bypassCache: mBypassCache];
                    872:                     
1.2       hyatt     873:     [progressDialog showWindow: progressDialog];
                    874: }
                    875: 
                    876: /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
                    877: NS_IMETHODIMP 
                    878: nsHeaderSniffer::OnProgressChange(nsIWebProgress *aWebProgress, 
                    879:           nsIRequest *aRequest, 
                    880:           PRInt32 aCurSelfProgress, 
                    881:           PRInt32 aMaxSelfProgress, 
                    882:           PRInt32 aCurTotalProgress, 
                    883:           PRInt32 aMaxTotalProgress)
                    884: {
                    885:   return NS_OK;
                    886: }
                    887: 
                    888: /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
                    889: NS_IMETHODIMP 
                    890: nsHeaderSniffer::OnLocationChange(nsIWebProgress *aWebProgress, 
                    891:           nsIRequest *aRequest, 
                    892:           nsIURI *location)
                    893: {
                    894:   return NS_OK;
                    895: }
                    896: 
                    897: /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
                    898: NS_IMETHODIMP 
                    899: nsHeaderSniffer::OnStatusChange(nsIWebProgress *aWebProgress, 
                    900:               nsIRequest *aRequest, 
                    901:               nsresult aStatus, 
                    902:               const PRUnichar *aMessage)
                    903: {
                    904:   return NS_OK;
                    905: }
                    906: 
                    907: /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long state); */
                    908: NS_IMETHODIMP 
                    909: nsHeaderSniffer::OnSecurityChange(nsIWebProgress *aWebProgress, 
                    910:                        nsIRequest *aRequest, 
                    911:                                   PRInt32 state)
                    912: {
                    913:   return NS_OK;
                    914: }
                    915: 
1.1       hyatt     916: 
                    917: @implementation NSBrowserView
                    918: 
                    919: - (id)initWithFrame:(NSRect)frame
                    920: {
                    921:   [super initWithFrame:frame];
                    922: 
                    923:   nsresult rv = nsCocoaBrowserService::InitEmbedding();
                    924:   if (NS_FAILED(rv)) {
                    925:     // XXX need to throw
                    926:   }
                    927:   
                    928:   _listener = new nsCocoaBrowserListener(self);
                    929:   NS_ADDREF(_listener);
                    930: 
                    931:   // Create the web browser instance
                    932:   nsCOMPtr<nsIWebBrowser> browser = do_CreateInstance(NS_WEBBROWSER_CONTRACTID, &rv);
                    933:   if (NS_FAILED(rv)) {
                    934:     // XXX need to throw
                    935:   }
                    936: 
                    937:   _webBrowser = browser;
                    938:   NS_ADDREF(_webBrowser);
                    939: 
                    940:   // Set the container nsIWebBrowserChrome
                    941:   _webBrowser->SetContainerWindow(NS_STATIC_CAST(nsIWebBrowserChrome *, 
                    942:             _listener));
                    943:   
                    944:   // Register as a listener for web progress
                    945:   nsCOMPtr<nsIWeakReference> weak = do_GetWeakReference(NS_STATIC_CAST(nsIWebProgressListener*, _listener));
                    946:   _webBrowser->AddWebBrowserListener(weak, NS_GET_IID(nsIWebProgressListener));
                    947: 
                    948:   // Hook up the widget hierarchy with us as the parent
                    949:   nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(_webBrowser);
                    950:   baseWin->InitWindow((NSView*)self, nsnull, 0, 0, 
                    951:          frame.size.width, frame.size.height);
                    952:   baseWin->Create();
                    953: 
                    954:   return self;
                    955: }
                    956: 
1.6       hyatt     957: - (void)destroyWebBrowser
                    958: {
                    959:   nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(_webBrowser);
                    960:   baseWin->Destroy();
                    961: }
                    962: 
1.1       hyatt     963: - (void)dealloc 
                    964: {
1.23    ! hyatt     965:   [super dealloc];
        !           966:   
1.1       hyatt     967:   NS_RELEASE(_listener);
                    968:   NS_IF_RELEASE(_webBrowser);
1.10      hyatt     969:   
                    970:   nsCocoaBrowserService::BrowserClosed();
1.18      hyatt     971:   
                    972:   printf("NSBrowserView died.\n");
1.1       hyatt     973: }
                    974: 
                    975: - (void)setFrame:(NSRect)frameRect 
                    976: {
                    977:   [super setFrame:frameRect];
                    978:   if (_webBrowser) {
                    979:     nsCOMPtr<nsIBaseWindow> window = do_QueryInterface(_webBrowser);
                    980:     window->SetSize((PRInt32)frameRect.size.width, 
                    981:        (PRInt32)frameRect.size.height,
                    982:        PR_TRUE);
                    983:   }
                    984: }
                    985: 
                    986: - (void)addListener:(id <NSBrowserListener>)listener
                    987: {
                    988:   _listener->AddListener(listener);
                    989: }
                    990: 
                    991: - (void)removeListener:(id <NSBrowserListener>)listener
                    992: {
                    993:   _listener->RemoveListener(listener);
                    994: }
                    995: 
                    996: - (void)setContainer:(id <NSBrowserContainer>)container
                    997: {
                    998:   _listener->SetContainer(container);
                    999: }
                   1000: 
                   1001: - (nsIDOMWindow*)getContentWindow
                   1002: {
                   1003:   nsIDOMWindow* window;
                   1004: 
                   1005:   _webBrowser->GetContentDOMWindow(&window);
                   1006: 
                   1007:   return window;
                   1008: }
                   1009: 
                   1010: - (void)loadURI:(NSURL *)url flags:(unsigned int)flags
                   1011: {
                   1012:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1013:   
                   1014:   NSString* spec = [url absoluteString];
                   1015:   int length = [spec length];
                   1016:   PRUnichar* specStr = nsMemory::Alloc((length+1) * sizeof(PRUnichar));
                   1017:   [spec getCharacters:specStr];
                   1018:   specStr[length] = PRUnichar(0);
                   1019:   
                   1020: 
                   1021:   PRUint32 navFlags = nsIWebNavigation::LOAD_FLAGS_NONE;
                   1022:   if (flags & NSLoadFlagsDontPutInHistory) {
                   1023:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_HISTORY;
                   1024:   }
                   1025:   if (flags & NSLoadFlagsReplaceHistoryEntry) {
                   1026:     navFlags |= nsIWebNavigation::LOAD_FLAGS_REPLACE_HISTORY;
                   1027:   }
                   1028:   if (flags & NSLoadFlagsBypassCacheAndProxy) {
                   1029:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE | 
                   1030:                 nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY;
                   1031:   }
                   1032: 
                   1033:   nsresult rv = nav->LoadURI(specStr, navFlags, nsnull, nsnull, nsnull);
                   1034:   if (NS_FAILED(rv)) {
                   1035:     // XXX need to throw
                   1036:   }
                   1037: 
                   1038:   nsMemory::Free(specStr);
                   1039: }
                   1040: 
                   1041: - (void)reload:(unsigned int)flags
                   1042: {
                   1043:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1044: 
                   1045:   PRUint32 navFlags = nsIWebNavigation::LOAD_FLAGS_NONE;
                   1046:   if (flags & NSLoadFlagsBypassCacheAndProxy) {
                   1047:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE | 
                   1048:                 nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY;
                   1049:   }
                   1050: 
                   1051:   nsresult rv = nav->Reload(navFlags);
                   1052:   if (NS_FAILED(rv)) {
                   1053:     // XXX need to throw
                   1054:   }  
                   1055: }
                   1056: 
                   1057: - (BOOL)canGoBack
                   1058: {
                   1059:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1060: 
                   1061:   PRBool can;
                   1062:   nav->GetCanGoBack(&can);
                   1063: 
                   1064:   return can ? YES : NO;
                   1065: }
                   1066: 
                   1067: - (BOOL)canGoForward
                   1068: {
                   1069:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1070: 
                   1071:   PRBool can;
                   1072:   nav->GetCanGoForward(&can);
                   1073: 
                   1074:   return can ? YES : NO;
                   1075: }
                   1076: 
                   1077: - (void)goBack
                   1078: {
                   1079:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1080: 
                   1081:   nsresult rv = nav->GoBack();
                   1082:   if (NS_FAILED(rv)) {
                   1083:     // XXX need to throw
                   1084:   }  
                   1085: }
                   1086: 
                   1087: - (void)goForward
                   1088: {
                   1089:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1090: 
                   1091:   nsresult rv = nav->GoForward();
                   1092:   if (NS_FAILED(rv)) {
                   1093:     // XXX need to throw
                   1094:   }  
                   1095: }
                   1096: 
                   1097: - (void)gotoIndex:(int)index
                   1098: {
                   1099:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1100: 
                   1101:   nsresult rv = nav->GotoIndex(index);
                   1102:   if (NS_FAILED(rv)) {
                   1103:     // XXX need to throw
                   1104:   }    
                   1105: }
                   1106: 
                   1107: - (void)stop:(unsigned int)flags
                   1108: {
                   1109:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1110: 
                   1111:   nsresult rv = nav->Stop(flags);
                   1112:   if (NS_FAILED(rv)) {
                   1113:     // XXX need to throw
                   1114:   }    
                   1115: }
                   1116: 
                   1117: - (NSURL*)getCurrentURI
                   1118: {
                   1119:   nsCOMPtr<nsIURI> uri;
                   1120:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1121: 
                   1122:   nav->GetCurrentURI(getter_AddRefs(uri));
                   1123:   if (!uri) {
                   1124:     return nsnull;
                   1125:   }
                   1126: 
                   1127:   nsXPIDLCString spec;
                   1128:   uri->GetSpec(getter_Copies(spec));
                   1129:   
                   1130:   const char* cstr = spec.get();
                   1131:   NSString* str = [NSString stringWithCString:cstr];
                   1132:   NSURL* url = [NSURL URLWithString:str];
                   1133:   
                   1134:   return url;
1.22      hyatt    1135: }
                   1136: 
                   1137: - (nsCocoaBrowserListener*)getCocoaBrowserListener
                   1138: {
                   1139:   return _listener;
1.1       hyatt    1140: }
                   1141: 
                   1142: - (nsIWebBrowser*)getWebBrowser
                   1143: {
                   1144:   NS_IF_ADDREF(_webBrowser);
                   1145:   return _webBrowser;
                   1146: }
                   1147: 
                   1148: - (void)setWebBrowser:(nsIWebBrowser*)browser
                   1149: {
                   1150:   _webBrowser = browser;
                   1151: 
                   1152:   if (_webBrowser) {
                   1153:     // Set the container nsIWebBrowserChrome
                   1154:     _webBrowser->SetContainerWindow(NS_STATIC_CAST(nsIWebBrowserChrome *, 
                   1155:               _listener));
                   1156: 
                   1157:     NSRect frame = [self frame];
                   1158:  
                   1159:     // Hook up the widget hierarchy with us as the parent
                   1160:     nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(_webBrowser);
                   1161:     baseWin->InitWindow((NSView*)self, nsnull, 0, 0, 
                   1162:      frame.size.width, frame.size.height);
                   1163:     baseWin->Create();
                   1164:   }
                   1165: 
1.2       hyatt    1166: }
                   1167: 
                   1168: -(void) saveInternal: (nsIURI*)aURI
                   1169:         withDocument: (nsIDOMDocument*)aDocument
                   1170:         bypassCache: (BOOL)aBypassCache
                   1171:         filterView: (NSView*)aFilterView
                   1172:         filterList: (NSPopUpButton*)aFilterList
                   1173: {
                   1174:     // Create our web browser persist object.  This is the object that knows
                   1175:     // how to actually perform the saving of the page (and of the images
                   1176:     // on the page).
                   1177:     nsCOMPtr<nsIWebBrowserPersist> webPersist(do_CreateInstance(persistContractID));
                   1178:     if (!webPersist)
                   1179:         return;
                   1180:     
                   1181:     // Make a temporary file object that we can save to.
                   1182:     nsCOMPtr<nsIProperties> dirService(do_GetService(dirServiceContractID));
                   1183:     if (!dirService)
                   1184:         return;
                   1185:     nsCOMPtr<nsIFile> tmpFile;
                   1186:     dirService->Get("TmpD", NS_GET_IID(nsIFile), getter_AddRefs(tmpFile));
                   1187:     static short unsigned int tmpRandom = 0;
                   1188:     nsCAutoString tmpNo; tmpNo.AssignWithConversion(tmpRandom++);
                   1189:     nsCAutoString saveFile("-sav");
                   1190:     saveFile += tmpNo;
                   1191:     saveFile += "tmp";
                   1192:     tmpFile->Append(saveFile.get()); 
                   1193:     
                   1194:     // Get the post data if we're an HTML doc.
                   1195:     nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(_webBrowser));
                   1196:     nsCOMPtr<nsISHistory> sessionHistory;
                   1197:     webNav->GetSessionHistory(getter_AddRefs(sessionHistory));
1.3       hyatt    1198:     nsCOMPtr<nsIHistoryEntry> entry;
                   1199:     PRInt32 sindex;
                   1200:     sessionHistory->GetIndex(&sindex);
                   1201:     sessionHistory->GetEntryAtIndex(sindex, PR_FALSE, getter_AddRefs(entry));
                   1202:     nsCOMPtr<nsISHEntry> shEntry(do_QueryInterface(entry));
                   1203:     nsCOMPtr<nsIInputStream> postData;
                   1204:     if (shEntry)
                   1205:         shEntry->GetPostData(getter_AddRefs(postData));
                   1206:     
1.2       hyatt    1207:     nsHeaderSniffer* sniffer = new nsHeaderSniffer(webPersist, tmpFile, aURI, 
1.3       hyatt    1208:                                                    aDocument, postData, aBypassCache,
1.2       hyatt    1209:                                                    aFilterView, aFilterList);
                   1210:     if (!sniffer)
                   1211:         return;
                   1212:     webPersist->SetProgressListener(sniffer);
                   1213:     webPersist->SaveURI(aURI, nsnull, tmpFile);
1.8       hyatt    1214: }
                   1215: 
                   1216: -(void)printDocument
                   1217: {
                   1218:     nsCOMPtr<nsIDOMWindow> domWindow;
                   1219:     _webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
                   1220:     nsCOMPtr<nsIInterfaceRequestor> ir(do_QueryInterface(domWindow));
                   1221:     nsCOMPtr<nsIWebBrowserPrint> print;
                   1222:     ir->GetInterface(NS_GET_IID(nsIWebBrowserPrint), getter_AddRefs(print));
                   1223:     print->Print(nsnull, nsnull);
1.2       hyatt    1224: }
                   1225: 
1.14      hyatt    1226: -(void)findInPage
                   1227: {
1.15      hyatt    1228:     FindDlgController* findDialog = [[FindDlgController alloc] initWithWindowNibName: @"FindDialog"];
1.16      hyatt    1229:     nsCOMPtr<nsIWebBrowserFocus> wbf(do_QueryInterface(_webBrowser));
                   1230:     nsCOMPtr<nsIDOMWindow> rootWindow;
                   1231:     nsCOMPtr<nsIDOMWindow> focusedWindow;
                   1232:     _webBrowser->GetContentDOMWindow(getter_AddRefs(rootWindow));
                   1233:     wbf->GetFocusedWindow(getter_AddRefs(focusedWindow));
                   1234:     if (!focusedWindow)
                   1235:         focusedWindow = rootWindow;
                   1236:     nsCOMPtr<nsIWebBrowserFind> webFind(do_GetInterface(_webBrowser));
                   1237:     nsCOMPtr<nsIWebBrowserFindInFrames> framesFind(do_QueryInterface(webFind));
                   1238:     framesFind->SetRootSearchFrame(rootWindow);
                   1239:     framesFind->SetCurrentSearchFrame(focusedWindow);
                   1240:     
                   1241:     [findDialog setFind: webFind];
1.15      hyatt    1242:     [findDialog showWindow: findDialog];
1.14      hyatt    1243: }
                   1244: 
                   1245: -(void)findAgain
                   1246: {
                   1247: }
                   1248: 
1.2       hyatt    1249: -(void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
                   1250: {
                   1251:     nsCOMPtr<nsIWebBrowserFocus> wbf(do_QueryInterface(_webBrowser));
                   1252:     nsCOMPtr<nsIDOMWindow> domWindow;
                   1253:     wbf->GetFocusedWindow(getter_AddRefs(domWindow));
                   1254:     if (!domWindow)
                   1255:         _webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
                   1256:     if (!domWindow)
                   1257:         return;
                   1258:     
                   1259:     nsCOMPtr<nsIDOMDocument> domDocument;
                   1260:     domWindow->GetDocument(getter_AddRefs(domDocument));
                   1261:     if (!domDocument)
                   1262:         return;
1.17      hyatt    1263:     nsCOMPtr<nsIDOMNSDocument> nsDoc(do_QueryInterface(domDocument));
                   1264:     if (!nsDoc)
                   1265:         return;
                   1266:     nsCOMPtr<nsIDOMLocation> location;
                   1267:     nsDoc->GetLocation(getter_AddRefs(location));
                   1268:     if (!location)
                   1269:         return;
                   1270:     nsAutoString urlStr;
                   1271:     location->GetHref(urlStr);
                   1272:     nsCAutoString urlCStr; urlCStr.AssignWithConversion(urlStr);
                   1273:     nsCOMPtr<nsIURI> url;
                   1274:     nsresult rv = NS_NewURI(getter_AddRefs(url), urlCStr.get());
                   1275:     if (NS_FAILED(rv))
                   1276:         return;
                   1277:         
                   1278: #if 0
1.2       hyatt    1279:     nsCOMPtr<nsIDocument> doc(do_QueryInterface(domDocument));
                   1280:     if (!doc)
                   1281:         return;
                   1282:     nsCOMPtr<nsIURI> url;
                   1283:     doc->GetDocumentURL(getter_AddRefs(url));
1.17      hyatt    1284: #endif
1.2       hyatt    1285:      
                   1286:     [self saveInternal: url.get()
                   1287:           withDocument: domDocument
                   1288:           bypassCache: NO
                   1289:           filterView: aFilterView
                   1290:           filterList: aFilterList];
1.11      hyatt    1291: }
                   1292: 
                   1293: 
1.12      hyatt    1294: -(IBAction)cut:(id)aSender
1.11      hyatt    1295: {
                   1296:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
                   1297:     clipboard->CutSelection();
                   1298: }
                   1299: 
1.12      hyatt    1300: -(IBAction)copy:(id)aSender
1.11      hyatt    1301: {
                   1302:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
                   1303:     clipboard->CopySelection();
                   1304: }
                   1305: 
1.12      hyatt    1306: -(IBAction)paste:(id)aSender
1.11      hyatt    1307: {
                   1308:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
                   1309:     clipboard->Paste();
                   1310: }
                   1311: 
1.12      hyatt    1312: -(IBAction)clear:(id)aSender
1.11      hyatt    1313: {
                   1314:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
                   1315:     clipboard->SelectNone();
                   1316: }
                   1317: 
1.12      hyatt    1318: -(IBAction)selectAll:(id)aSender
1.11      hyatt    1319: {
                   1320:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
                   1321:     clipboard->SelectAll();
1.1       hyatt    1322: }
                   1323: 
1.19      hyatt    1324: -(NSString*)getCurrentURLSpec
                   1325: {
                   1326:     NSString* empty = @"";
                   1327:     nsCOMPtr<nsIDOMWindow> domWindow;
                   1328:     _webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
                   1329:     if (!domWindow)
                   1330:         return empty;
                   1331:     
                   1332:     nsCOMPtr<nsIDOMDocument> domDocument;
                   1333:     domWindow->GetDocument(getter_AddRefs(domDocument));
                   1334:     if (!domDocument)
                   1335:         return empty;
                   1336:     nsCOMPtr<nsIDOMNSDocument> nsDoc(do_QueryInterface(domDocument));
                   1337:     if (!nsDoc)
                   1338:         return empty;
                   1339:     nsCOMPtr<nsIDOMLocation> location;
                   1340:     nsDoc->GetLocation(getter_AddRefs(location));
                   1341:     if (!location)
                   1342:         return empty;
                   1343:     nsAutoString urlStr;
                   1344:     location->GetHref(urlStr);
                   1345:     nsCAutoString urlCStr; urlCStr.AssignWithConversion(urlStr);
                   1346:     
                   1347:     return [NSString stringWithCString: urlCStr.get()];
                   1348: }
1.21      hyatt    1349: 
                   1350: - (void)setActive: (BOOL)aIsActive
                   1351: {
                   1352:     nsCOMPtr<nsIWebBrowserFocus> wbf(do_QueryInterface(_webBrowser));
                   1353:     if (aIsActive)
                   1354:         wbf->Activate();
                   1355:     else
                   1356:         wbf->Deactivate();
                   1357: }
1.1       hyatt    1358: @end
                   1359: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>