Annotation of chimera/NSBrowserView.mm, revision 1.28

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:   
1.26      hyatt     182:   printf("made a chrome window.\n");
                    183:   
1.22      hyatt     184:   *_retval = listener;
                    185:   NS_IF_ADDREF(*_retval);
                    186:   return NS_OK;
                    187: }
                    188: 
1.1       hyatt     189: // Implementation of nsIWebBrowserChrome
                    190: /* void setStatus (in unsigned long statusType, in wstring status); */
                    191: NS_IMETHODIMP 
                    192: nsCocoaBrowserListener::SetStatus(PRUint32 statusType, const PRUnichar *status)
                    193: {
                    194:   if (!mContainer) {
                    195:     return NS_ERROR_FAILURE;
                    196:   }
                    197: 
                    198:   NSString* str = nsnull;
                    199:   if (status && (*status != PRUnichar(0))) {
                    200:     str = [NSString stringWithCharacters:status length:nsCRT::strlen(status)];
                    201:   }
                    202: 
                    203:   [mContainer setStatus:str ofType:(NSStatusType)statusType];
                    204: 
                    205:   return NS_OK;
                    206: }
                    207: 
                    208: /* attribute nsIWebBrowser webBrowser; */
                    209: NS_IMETHODIMP 
                    210: nsCocoaBrowserListener::GetWebBrowser(nsIWebBrowser * *aWebBrowser)
                    211: {
                    212:   NS_ENSURE_ARG_POINTER(aWebBrowser);
                    213:   if (!mView) {
                    214:     return NS_ERROR_FAILURE;
                    215:   }
                    216:   *aWebBrowser = [mView getWebBrowser];
                    217: 
                    218:   return NS_OK;
                    219: }
                    220: NS_IMETHODIMP 
                    221: nsCocoaBrowserListener::SetWebBrowser(nsIWebBrowser * aWebBrowser)
                    222: {
                    223:   if (!mView) {
                    224:     return NS_ERROR_FAILURE;
                    225:   }
                    226: 
                    227:   [mView setWebBrowser:aWebBrowser];
                    228: 
                    229:   return NS_OK;
                    230: }
                    231: 
                    232: /* attribute unsigned long chromeFlags; */
                    233: NS_IMETHODIMP 
                    234: nsCocoaBrowserListener::GetChromeFlags(PRUint32 *aChromeFlags)
                    235: {
                    236:   NS_ENSURE_ARG_POINTER(aChromeFlags);
                    237:   *aChromeFlags = mChromeFlags;
                    238:   return NS_OK;
                    239: }
                    240: NS_IMETHODIMP 
                    241: nsCocoaBrowserListener::SetChromeFlags(PRUint32 aChromeFlags)
                    242: {
                    243:   // XXX Do nothing with them for now
                    244:   mChromeFlags = aChromeFlags;
                    245:   return NS_OK;
                    246: }
                    247: 
                    248: /* void destroyBrowserWindow (); */
                    249: NS_IMETHODIMP 
                    250: nsCocoaBrowserListener::DestroyBrowserWindow()
                    251: {
                    252:   // XXX Could send this up to the container, but for now,
                    253:   // we just destroy the enclosing window.
                    254:   NSWindow* window = [mView window];
                    255: 
                    256:   if (window) {
                    257:     [window close];
                    258:   }
                    259: 
                    260:   return NS_OK;
                    261: }
                    262: 
                    263: /* void sizeBrowserTo (in long aCX, in long aCY); */
                    264: NS_IMETHODIMP 
                    265: nsCocoaBrowserListener::SizeBrowserTo(PRInt32 aCX, PRInt32 aCY)
                    266: {
                    267:   if (mContainer) {
                    268:     NSSize size;
                    269:     
                    270:     size.width = (float)aCX;
                    271:     size.height = (float)aCY;
                    272: 
                    273:     [mContainer sizeBrowserTo:size];
                    274:   }
                    275:   
                    276:   return NS_OK;
                    277: }
                    278: 
                    279: /* void showAsModal (); */
                    280: NS_IMETHODIMP 
                    281: nsCocoaBrowserListener::ShowAsModal()
                    282: {
                    283:   if (!mView) {
                    284:     return NS_ERROR_FAILURE;
                    285:   }
                    286: 
                    287:   NSWindow* window = [mView window];
                    288: 
                    289:   if (!window) {
                    290:     return NS_ERROR_FAILURE;
                    291:   }
                    292: 
                    293:   mIsModal = PR_TRUE;
1.22      hyatt     294:   //int result = [NSApp runModalForWindow:window];
1.1       hyatt     295:   mIsModal = PR_FALSE;
                    296: 
1.22      hyatt     297:   return NS_OK;
1.1       hyatt     298: }
                    299: 
                    300: /* boolean isWindowModal (); */
                    301: NS_IMETHODIMP 
                    302: nsCocoaBrowserListener::IsWindowModal(PRBool *_retval)
                    303: {
                    304:   NS_ENSURE_ARG_POINTER(_retval);
                    305: 
                    306:   *_retval = mIsModal;
                    307: 
                    308:   return NS_OK;
                    309: }
                    310: 
                    311: /* void exitModalEventLoop (in nsresult aStatus); */
                    312: NS_IMETHODIMP 
                    313: nsCocoaBrowserListener::ExitModalEventLoop(nsresult aStatus)
                    314: {
1.22      hyatt     315: //  [NSApp stopModalWithCode:(int)aStatus];
1.1       hyatt     316: 
                    317:   return NS_OK;
                    318: }
                    319: 
                    320: // Implementation of nsIEmbeddingSiteWindow
                    321: /* void setDimensions (in unsigned long flags, in long x, in long y, in long cx, in long cy); */
                    322: NS_IMETHODIMP 
                    323: nsCocoaBrowserListener::SetDimensions(PRUint32 flags, 
                    324:              PRInt32 x, 
                    325:              PRInt32 y, 
                    326:              PRInt32 cx, 
                    327:              PRInt32 cy)
                    328: {
                    329:   if (!mView) {
                    330:     return NS_ERROR_FAILURE;
                    331:   }
                    332: 
                    333:   NSWindow* window = [mView window];
                    334:   if (!window) {
                    335:     return NS_ERROR_FAILURE;
                    336:   }
                    337: 
                    338:   if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION) {
                    339:     NSPoint origin;
                    340:     origin.x = (float)x;
                    341:     origin.y = (float)y;
                    342:     [window setFrameOrigin:origin];
                    343:   }
                    344: 
                    345:   if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER) {
                    346:     NSRect frame = [window frame];
                    347:     frame.size.width = (float)cx;
                    348:     frame.size.height = (float)cy;
                    349:     [window setFrame:frame display:YES];
                    350:   }
                    351:   else if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER) {
                    352:     NSSize size;
                    353:     size.width = (float)cx;
                    354:     size.height = (float)cy;
                    355:     [window setContentSize:size];
                    356:   }
                    357: 
                    358:   return NS_OK;
                    359: }
                    360: 
                    361: /* void getDimensions (in unsigned long flags, out long x, out long y, out long cx, out long cy); */
                    362: NS_IMETHODIMP 
                    363: nsCocoaBrowserListener::GetDimensions(PRUint32 flags, 
                    364:              PRInt32 *x, 
                    365:              PRInt32 *y, 
                    366:              PRInt32 *cx, 
                    367:              PRInt32 *cy)
                    368: {
                    369:   NS_ENSURE_ARG_POINTER(x);
                    370:   NS_ENSURE_ARG_POINTER(y);
                    371:   NS_ENSURE_ARG_POINTER(cx);
                    372:   NS_ENSURE_ARG_POINTER(cy);
                    373: 
                    374:   if (!mView) {
                    375:     return NS_ERROR_FAILURE;
                    376:   }
                    377: 
                    378:   NSWindow* window = [mView window];
                    379:   if (!window) {
                    380:     return NS_ERROR_FAILURE;
                    381:   }
                    382: 
                    383:   NSRect frame = [window frame];
                    384:   if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION) {
                    385:     *x = (PRInt32)frame.origin.x;
                    386:     *y = (PRInt32)frame.origin.y;
                    387:   }
                    388:   if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER) {
                    389:     *cx = (PRInt32)frame.size.width;
                    390:     *cy = (PRInt32)frame.size.height;
                    391:   }
                    392:   else if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER) {
                    393:     NSView* contentView = [window contentView];
                    394:     NSRect contentFrame = [contentView frame];
                    395:     *cx = (PRInt32)contentFrame.size.width;
                    396:     *cy = (PRInt32)contentFrame.size.height;    
                    397:   }
                    398: 
                    399:   return NS_OK;
                    400: }
                    401: 
                    402: /* void setFocus (); */
                    403: NS_IMETHODIMP 
                    404: nsCocoaBrowserListener::SetFocus()
                    405: {
                    406:   if (!mView) {
                    407:     return NS_ERROR_FAILURE;
                    408:   }
                    409: 
                    410:   NSWindow* window = [mView window];
                    411:   if (!window) {
                    412:     return NS_ERROR_FAILURE;
                    413:   }
                    414: 
                    415:   [window makeKeyAndOrderFront:window];
                    416: 
                    417:   return NS_OK;
                    418: }
                    419: 
                    420: /* attribute boolean visibility; */
                    421: NS_IMETHODIMP 
                    422: nsCocoaBrowserListener::GetVisibility(PRBool *aVisibility)
                    423: {
                    424:   NS_ENSURE_ARG_POINTER(aVisibility);
                    425: 
                    426:   if (!mView) {
                    427:     return NS_ERROR_FAILURE;
                    428:   }
                    429: 
                    430:   NSWindow* window = [mView window];
                    431:   if (!window) {
                    432:     return NS_ERROR_FAILURE;
                    433:   }
                    434: 
                    435:   *aVisibility = [window isMiniaturized];
                    436: 
                    437:   return NS_OK;
                    438: }
                    439: NS_IMETHODIMP 
                    440: nsCocoaBrowserListener::SetVisibility(PRBool aVisibility)
                    441: {
                    442:   if (!mView) {
                    443:     return NS_ERROR_FAILURE;
                    444:   }
                    445: 
                    446:   NSWindow* window = [mView window];
                    447:   if (!window) {
                    448:     return NS_ERROR_FAILURE;
                    449:   }
                    450: 
                    451:   if (aVisibility) {
                    452:     [window deminiaturize:window];
                    453:   }
                    454:   else {
                    455:     [window miniaturize:window];
                    456:   }
                    457: 
                    458:   return NS_OK;
                    459: }
                    460: 
                    461: /* attribute wstring title; */
                    462: NS_IMETHODIMP 
                    463: nsCocoaBrowserListener::GetTitle(PRUnichar * *aTitle)
                    464: {
                    465:   NS_ENSURE_ARG_POINTER(aTitle);
                    466: 
                    467:   if (!mContainer) {
                    468:     return NS_ERROR_FAILURE;
                    469:   }
                    470: 
                    471:   NSString* title = [mContainer title];
                    472:   unsigned int length = [title length];
                    473:   if (length) {
                    474:     *aTitle = (PRUnichar*)nsMemory::Alloc((length+1)*sizeof(PRUnichar));
                    475:     if (!*aTitle) {
                    476:       return NS_ERROR_OUT_OF_MEMORY;
                    477:     }
                    478:     [title getCharacters:*aTitle];
                    479:   }
                    480:   else {
                    481:     *aTitle = nsnull;
                    482:   }
                    483:   
                    484:   return NS_OK;
                    485: }
                    486: NS_IMETHODIMP 
                    487: nsCocoaBrowserListener::SetTitle(const PRUnichar * aTitle)
                    488: {
                    489:   NS_ENSURE_ARG(aTitle);
                    490: 
                    491:   if (!mContainer) {
                    492:     return NS_ERROR_FAILURE;
                    493:   }
                    494: 
                    495:   NSString* str = [NSString stringWithCharacters:aTitle length:nsCRT::strlen(aTitle)];
                    496:   [mContainer setTitle:str];
                    497: 
                    498:   return NS_OK;
                    499: }
                    500: 
                    501: /* [noscript] readonly attribute voidPtr siteWindow; */
                    502: NS_IMETHODIMP 
                    503: nsCocoaBrowserListener::GetSiteWindow(void * *aSiteWindow)
                    504: {
                    505:   NS_ENSURE_ARG_POINTER(aSiteWindow);
                    506: 
                    507:   if (!mView) {
                    508:     return NS_ERROR_FAILURE;
                    509:   }
                    510: 
                    511:   NSWindow* window = [mView window];
                    512:   if (!window) {
                    513:     return NS_ERROR_FAILURE;
                    514:   }
                    515: 
                    516:   *aSiteWindow = (void*)window;
                    517: 
                    518:   return NS_OK;
                    519: }
                    520: 
                    521: // Implementation of nsIWebProgressListener
                    522: /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aStateFlags, in unsigned long aStatus); */
                    523: NS_IMETHODIMP 
                    524: nsCocoaBrowserListener::OnStateChange(nsIWebProgress *aWebProgress, 
                    525:              nsIRequest *aRequest, 
                    526:              PRInt32 aStateFlags, 
                    527:              PRUint32 aStatus)
                    528: {
                    529:   NSEnumerator* enumerator = [mListeners objectEnumerator];
                    530:   id obj;
                    531:   
                    532:   if (aStateFlags & nsIWebProgressListener::STATE_IS_NETWORK) {
                    533:     if (aStateFlags & nsIWebProgressListener::STATE_START) {
                    534:       while ((obj = [enumerator nextObject])) {
                    535:  [obj onLoadingStarted];
                    536:       }
                    537:     }
                    538:     else if (aStateFlags & nsIWebProgressListener::STATE_STOP) {
                    539:       while ((obj = [enumerator nextObject])) {
                    540:  [obj onLoadingCompleted:(NS_SUCCEEDED(aStatus))];
                    541:       }
                    542:     }
                    543:   }
                    544: 
                    545: 
                    546:   return NS_OK;
                    547: }
                    548: 
                    549: /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
                    550: NS_IMETHODIMP 
                    551: nsCocoaBrowserListener::OnProgressChange(nsIWebProgress *aWebProgress, 
                    552:           nsIRequest *aRequest, 
                    553:           PRInt32 aCurSelfProgress, 
                    554:           PRInt32 aMaxSelfProgress, 
                    555:           PRInt32 aCurTotalProgress, 
                    556:           PRInt32 aMaxTotalProgress)
                    557: {
                    558:   NSEnumerator* enumerator = [mListeners objectEnumerator];
                    559:   id obj;
                    560:  
                    561:   while ((obj = [enumerator nextObject])) {
                    562:     [obj onProgressChange:aCurTotalProgress outOf:aMaxTotalProgress];
                    563:   }
                    564:   
                    565:   return NS_OK;
                    566: }
                    567: 
                    568: /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
                    569: NS_IMETHODIMP 
                    570: nsCocoaBrowserListener::OnLocationChange(nsIWebProgress *aWebProgress, 
                    571:           nsIRequest *aRequest, 
                    572:           nsIURI *location)
                    573: {
1.25      hyatt     574:   if (!location)
1.1       hyatt     575:     return NS_ERROR_FAILURE;
1.25      hyatt     576:     
                    577:   nsCAutoString spec;
                    578:   location->GetSpec(spec);
1.1       hyatt     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));
1.28    ! hyatt     699:         channel->GetContentType(mContentType);
        !           700:        
1.2       hyatt     701:         // Get the content-disposition if we're an HTTP channel.
                    702:         nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
1.28    ! hyatt     703:         if (httpChannel)
        !           704:             httpChannel->GetResponseHeader(nsCAutoString("content-disposition"), mContentDisposition);
1.2       hyatt     705:         
                    706:         mPersist->CancelSave();
                    707:         PRBool exists;
                    708:         mTmpFile->Exists(&exists);
                    709:         if (exists)
                    710:             mTmpFile->Remove(PR_FALSE);
                    711:         PerformSave();
                    712:     }
                    713:     return NS_OK;
                    714: }
                    715: 
                    716: void nsHeaderSniffer::PerformSave()
                    717: {
                    718:     // Are we an HTML document? If so, we will want to append an accessory view to
                    719:     // the save dialog to provide the user with the option of doing a complete
                    720:     // save vs. a single file save.
                    721:     PRBool isHTML = (mDocument && mContentType.Equals("text/html") ||
                    722:                      mContentType.Equals("text/xml") ||
                    723:                      mContentType.Equals("application/xhtml+xml"));
                    724:     
                    725:     // Next find out the directory that we should start in.
                    726:     nsCOMPtr<nsIPrefService> prefs(do_GetService("@mozilla.org/preferences-service;1"));
                    727:     if (!prefs)
                    728:         return;
                    729:     nsCOMPtr<nsIPrefBranch> dirBranch;
                    730:     prefs->GetBranch("browser.download.", getter_AddRefs(dirBranch));
                    731:     PRInt32 filterIndex = 0;
                    732:     if (dirBranch) {
                    733:         nsresult rv = dirBranch->GetIntPref("save_converter_index", &filterIndex);
                    734:         if (NS_FAILED(rv))
                    735:             filterIndex = 0;
                    736:     }
                    737:     if (mFilterList)
                    738:         [mFilterList selectItemAtIndex: filterIndex];
                    739:         
                    740:     // We need to figure out what file name to use.
                    741:     nsCAutoString defaultFileName;
                    742:     
                    743:     if (!mContentDisposition.IsEmpty()) {
                    744:         // (1) Use the HTTP header suggestion.
                    745:         PRInt32 index = mContentDisposition.Find("filename=");
                    746:         if (index >= 0) {
                    747:             // Take the substring following the prefix.
                    748:             index += 9;
                    749:             nsCAutoString filename;
                    750:             mContentDisposition.Right(filename, mContentDisposition.Length() - index);
                    751:             defaultFileName = filename;
                    752:         }
                    753:     }
                    754:     
                    755:     if (defaultFileName.IsEmpty()) {
                    756:         nsCOMPtr<nsIURL> url(do_QueryInterface(mURL));
1.25      hyatt     757:         if (url)
                    758:             url->GetFileName(defaultFileName); // (2) For file URLs, use the file name.
1.2       hyatt     759:     }
                    760:     
                    761:     if (defaultFileName.IsEmpty() && mDocument && isHTML) {
                    762:         nsCOMPtr<nsIDOMHTMLDocument> htmlDoc(do_QueryInterface(mDocument));
                    763:         nsAutoString title;
                    764:         if (htmlDoc)
                    765:             htmlDoc->GetTitle(title); // (3) Use the title of the document.
                    766:         defaultFileName.AssignWithConversion(title);
                    767:     }
                    768:     
                    769:     if (defaultFileName.IsEmpty()) {
                    770:         // (4) Use the caller provided name. XXXdwh
                    771:     }
                    772: 
1.25      hyatt     773:     if (defaultFileName.IsEmpty())
1.2       hyatt     774:         // (5) Use the host.
1.25      hyatt     775:         mURL->GetHost(defaultFileName);
1.2       hyatt     776:     
                    777:     // One last case to handle about:blank and other fruity untitled pages.
                    778:     if (defaultFileName.IsEmpty())
                    779:         defaultFileName = "untitled";
                    780:         
                    781:     // Validate the file name to ensure legality.
                    782:     for (PRUint32 i = 0; i < defaultFileName.Length(); i++)
                    783:         if (defaultFileName[i] == ':' || defaultFileName[i] == '/')
                    784:             defaultFileName.SetCharAt(i, ' ');
                    785:             
                    786:     // Make sure the appropriate extension is appended to the suggested file name.
                    787:     nsCOMPtr<nsIURI> fileURI(do_CreateInstance("@mozilla.org/network/standard-url;1"));
                    788:     nsCOMPtr<nsIURL> fileURL(do_QueryInterface(fileURI));
                    789:     if (!fileURL)
                    790:         return;
1.25      hyatt     791:     fileURL->SetFilePath(defaultFileName);
1.2       hyatt     792:     
1.25      hyatt     793:     nsCAutoString fileExtension;
                    794:     fileURL->GetFileExtension(fileExtension);
1.2       hyatt     795:     
                    796:     PRBool setExtension = PR_FALSE;
                    797:     if (mContentType.Equals("text/html")) {
                    798:         if (fileExtension.IsEmpty() || (!fileExtension.Equals("htm") && !fileExtension.Equals("html"))) {
                    799:             defaultFileName += ".html";
                    800:             setExtension = PR_TRUE;
                    801:         }
                    802:     }
                    803:     
                    804:     if (!setExtension && fileExtension.IsEmpty()) {
                    805:         nsCOMPtr<nsIMIMEService> mimeService(do_GetService("@mozilla.org/mime;1"));
                    806:         if (!mimeService)
                    807:             return;
                    808:         nsCOMPtr<nsIMIMEInfo> mimeInfo;
                    809:         mimeService->GetFromMIMEType(mContentType.get(), getter_AddRefs(mimeInfo));
                    810:         
                    811:         PRUint32 extCount;
                    812:         char** extList;
                    813:         mimeInfo->GetFileExtensions(&extCount, &extList);
                    814:         
                    815:         if (extCount > 0) {
                    816:             defaultFileName += ".";
                    817:             defaultFileName += extList[0];
                    818:         }
                    819:     }
                    820:     
                    821:     // Now it's time to pose the save dialog.
                    822:     NSSavePanel* savePanel = [NSSavePanel savePanel];
                    823:     NSString* file = nil;
                    824:     if (!defaultFileName.IsEmpty())
                    825:         file = [[NSString alloc] initWithCString: defaultFileName.get()];
                    826:         
                    827:     if (isHTML)
                    828:         [savePanel setAccessoryView: mFilterView];
                    829:         
                    830:     if ([savePanel runModalForDirectory: nil file: file] == NSFileHandlingPanelCancelButton)
                    831:         return;
                    832:        
                    833:     // Release the file string.
                    834:     [file release];
                    835:     
                    836:     // Update the filter index.
                    837:     if (isHTML && mFilterList) {
                    838:         filterIndex = [mFilterList indexOfSelectedItem];
                    839:         dirBranch->SetIntPref("save_converter_index", filterIndex);
                    840:     }
                    841:     
                    842:     // Convert the content type to text/plain if it was selected in the filter.
                    843:     if (isHTML && filterIndex == 2)
                    844:         mContentType = "text/plain";
                    845:     
                    846:     nsCOMPtr<nsISupports> sourceData;
                    847:     if (isHTML && filterIndex != 1)
                    848:         sourceData = do_QueryInterface(mDocument);
                    849:     else
                    850:         sourceData = do_QueryInterface(mURL);
                    851:         
1.4       hyatt     852:     nsCOMPtr<nsIWebBrowserPersist> webPersist(do_CreateInstance(persistContractID));
1.2       hyatt     853:     ProgressDlgController* progressDialog = [[ProgressDlgController alloc] initWithWindowNibName: @"ProgressDialog"];
1.4       hyatt     854:     [progressDialog setWebPersist: webPersist 
1.2       hyatt     855:                     source: sourceData.get()
                    856:                     destination: [savePanel filename]
                    857:                     contentType: mContentType.get()
1.3       hyatt     858:                     postData: mPostData
                    859:                     bypassCache: mBypassCache];
                    860:                     
1.2       hyatt     861:     [progressDialog showWindow: progressDialog];
                    862: }
                    863: 
                    864: /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
                    865: NS_IMETHODIMP 
                    866: nsHeaderSniffer::OnProgressChange(nsIWebProgress *aWebProgress, 
                    867:           nsIRequest *aRequest, 
                    868:           PRInt32 aCurSelfProgress, 
                    869:           PRInt32 aMaxSelfProgress, 
                    870:           PRInt32 aCurTotalProgress, 
                    871:           PRInt32 aMaxTotalProgress)
                    872: {
                    873:   return NS_OK;
                    874: }
                    875: 
                    876: /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
                    877: NS_IMETHODIMP 
                    878: nsHeaderSniffer::OnLocationChange(nsIWebProgress *aWebProgress, 
                    879:           nsIRequest *aRequest, 
                    880:           nsIURI *location)
                    881: {
                    882:   return NS_OK;
                    883: }
                    884: 
                    885: /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
                    886: NS_IMETHODIMP 
                    887: nsHeaderSniffer::OnStatusChange(nsIWebProgress *aWebProgress, 
                    888:               nsIRequest *aRequest, 
                    889:               nsresult aStatus, 
                    890:               const PRUnichar *aMessage)
                    891: {
                    892:   return NS_OK;
                    893: }
                    894: 
                    895: /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long state); */
                    896: NS_IMETHODIMP 
                    897: nsHeaderSniffer::OnSecurityChange(nsIWebProgress *aWebProgress, 
                    898:                        nsIRequest *aRequest, 
                    899:                                   PRInt32 state)
                    900: {
                    901:   return NS_OK;
                    902: }
                    903: 
1.1       hyatt     904: 
                    905: @implementation NSBrowserView
                    906: 
                    907: - (id)initWithFrame:(NSRect)frame
                    908: {
                    909:   [super initWithFrame:frame];
                    910: 
                    911:   nsresult rv = nsCocoaBrowserService::InitEmbedding();
                    912:   if (NS_FAILED(rv)) {
                    913:     // XXX need to throw
                    914:   }
                    915:   
                    916:   _listener = new nsCocoaBrowserListener(self);
                    917:   NS_ADDREF(_listener);
                    918: 
                    919:   // Create the web browser instance
                    920:   nsCOMPtr<nsIWebBrowser> browser = do_CreateInstance(NS_WEBBROWSER_CONTRACTID, &rv);
                    921:   if (NS_FAILED(rv)) {
                    922:     // XXX need to throw
                    923:   }
                    924: 
                    925:   _webBrowser = browser;
                    926:   NS_ADDREF(_webBrowser);
                    927: 
                    928:   // Set the container nsIWebBrowserChrome
                    929:   _webBrowser->SetContainerWindow(NS_STATIC_CAST(nsIWebBrowserChrome *, 
                    930:             _listener));
                    931:   
                    932:   // Register as a listener for web progress
                    933:   nsCOMPtr<nsIWeakReference> weak = do_GetWeakReference(NS_STATIC_CAST(nsIWebProgressListener*, _listener));
                    934:   _webBrowser->AddWebBrowserListener(weak, NS_GET_IID(nsIWebProgressListener));
                    935: 
                    936:   // Hook up the widget hierarchy with us as the parent
                    937:   nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(_webBrowser);
                    938:   baseWin->InitWindow((NSView*)self, nsnull, 0, 0, 
                    939:          frame.size.width, frame.size.height);
                    940:   baseWin->Create();
                    941: 
                    942:   return self;
                    943: }
                    944: 
1.6       hyatt     945: - (void)destroyWebBrowser
                    946: {
                    947:   nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(_webBrowser);
                    948:   baseWin->Destroy();
                    949: }
                    950: 
1.1       hyatt     951: - (void)dealloc 
                    952: {
1.23      hyatt     953:   [super dealloc];
                    954:   
1.1       hyatt     955:   NS_RELEASE(_listener);
                    956:   NS_IF_RELEASE(_webBrowser);
1.10      hyatt     957:   
                    958:   nsCocoaBrowserService::BrowserClosed();
1.18      hyatt     959:   
                    960:   printf("NSBrowserView died.\n");
1.1       hyatt     961: }
                    962: 
                    963: - (void)setFrame:(NSRect)frameRect 
                    964: {
                    965:   [super setFrame:frameRect];
                    966:   if (_webBrowser) {
                    967:     nsCOMPtr<nsIBaseWindow> window = do_QueryInterface(_webBrowser);
                    968:     window->SetSize((PRInt32)frameRect.size.width, 
                    969:        (PRInt32)frameRect.size.height,
                    970:        PR_TRUE);
                    971:   }
                    972: }
                    973: 
                    974: - (void)addListener:(id <NSBrowserListener>)listener
                    975: {
                    976:   _listener->AddListener(listener);
                    977: }
                    978: 
                    979: - (void)removeListener:(id <NSBrowserListener>)listener
                    980: {
                    981:   _listener->RemoveListener(listener);
                    982: }
                    983: 
                    984: - (void)setContainer:(id <NSBrowserContainer>)container
                    985: {
                    986:   _listener->SetContainer(container);
                    987: }
                    988: 
                    989: - (nsIDOMWindow*)getContentWindow
                    990: {
                    991:   nsIDOMWindow* window;
                    992: 
                    993:   _webBrowser->GetContentDOMWindow(&window);
                    994: 
                    995:   return window;
                    996: }
                    997: 
                    998: - (void)loadURI:(NSURL *)url flags:(unsigned int)flags
                    999: {
                   1000:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1001:   
                   1002:   NSString* spec = [url absoluteString];
                   1003:   int length = [spec length];
                   1004:   PRUnichar* specStr = nsMemory::Alloc((length+1) * sizeof(PRUnichar));
                   1005:   [spec getCharacters:specStr];
                   1006:   specStr[length] = PRUnichar(0);
                   1007:   
                   1008: 
                   1009:   PRUint32 navFlags = nsIWebNavigation::LOAD_FLAGS_NONE;
                   1010:   if (flags & NSLoadFlagsDontPutInHistory) {
                   1011:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_HISTORY;
                   1012:   }
                   1013:   if (flags & NSLoadFlagsReplaceHistoryEntry) {
                   1014:     navFlags |= nsIWebNavigation::LOAD_FLAGS_REPLACE_HISTORY;
                   1015:   }
                   1016:   if (flags & NSLoadFlagsBypassCacheAndProxy) {
                   1017:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE | 
                   1018:                 nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY;
                   1019:   }
                   1020: 
                   1021:   nsresult rv = nav->LoadURI(specStr, navFlags, nsnull, nsnull, nsnull);
                   1022:   if (NS_FAILED(rv)) {
                   1023:     // XXX need to throw
                   1024:   }
                   1025: 
                   1026:   nsMemory::Free(specStr);
                   1027: }
                   1028: 
                   1029: - (void)reload:(unsigned int)flags
                   1030: {
                   1031:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1032: 
                   1033:   PRUint32 navFlags = nsIWebNavigation::LOAD_FLAGS_NONE;
                   1034:   if (flags & NSLoadFlagsBypassCacheAndProxy) {
                   1035:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE | 
                   1036:                 nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY;
                   1037:   }
                   1038: 
                   1039:   nsresult rv = nav->Reload(navFlags);
                   1040:   if (NS_FAILED(rv)) {
                   1041:     // XXX need to throw
                   1042:   }  
                   1043: }
                   1044: 
                   1045: - (BOOL)canGoBack
                   1046: {
                   1047:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1048: 
                   1049:   PRBool can;
                   1050:   nav->GetCanGoBack(&can);
                   1051: 
                   1052:   return can ? YES : NO;
                   1053: }
                   1054: 
                   1055: - (BOOL)canGoForward
                   1056: {
                   1057:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1058: 
                   1059:   PRBool can;
                   1060:   nav->GetCanGoForward(&can);
                   1061: 
                   1062:   return can ? YES : NO;
                   1063: }
                   1064: 
                   1065: - (void)goBack
                   1066: {
                   1067:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1068: 
                   1069:   nsresult rv = nav->GoBack();
                   1070:   if (NS_FAILED(rv)) {
                   1071:     // XXX need to throw
                   1072:   }  
                   1073: }
                   1074: 
                   1075: - (void)goForward
                   1076: {
                   1077:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1078: 
                   1079:   nsresult rv = nav->GoForward();
                   1080:   if (NS_FAILED(rv)) {
                   1081:     // XXX need to throw
                   1082:   }  
                   1083: }
                   1084: 
                   1085: - (void)gotoIndex:(int)index
                   1086: {
                   1087:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1088: 
                   1089:   nsresult rv = nav->GotoIndex(index);
                   1090:   if (NS_FAILED(rv)) {
                   1091:     // XXX need to throw
                   1092:   }    
                   1093: }
                   1094: 
                   1095: - (void)stop:(unsigned int)flags
                   1096: {
                   1097:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1098: 
                   1099:   nsresult rv = nav->Stop(flags);
                   1100:   if (NS_FAILED(rv)) {
                   1101:     // XXX need to throw
                   1102:   }    
                   1103: }
                   1104: 
                   1105: - (NSURL*)getCurrentURI
                   1106: {
                   1107:   nsCOMPtr<nsIURI> uri;
                   1108:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1109: 
                   1110:   nav->GetCurrentURI(getter_AddRefs(uri));
                   1111:   if (!uri) {
                   1112:     return nsnull;
                   1113:   }
                   1114: 
1.25      hyatt    1115:   nsCAutoString spec;
                   1116:   uri->GetSpec(spec);
1.1       hyatt    1117:   
                   1118:   const char* cstr = spec.get();
                   1119:   NSString* str = [NSString stringWithCString:cstr];
                   1120:   NSURL* url = [NSURL URLWithString:str];
                   1121:   
                   1122:   return url;
1.22      hyatt    1123: }
                   1124: 
                   1125: - (nsCocoaBrowserListener*)getCocoaBrowserListener
                   1126: {
                   1127:   return _listener;
1.1       hyatt    1128: }
                   1129: 
                   1130: - (nsIWebBrowser*)getWebBrowser
                   1131: {
                   1132:   NS_IF_ADDREF(_webBrowser);
                   1133:   return _webBrowser;
                   1134: }
                   1135: 
                   1136: - (void)setWebBrowser:(nsIWebBrowser*)browser
                   1137: {
                   1138:   _webBrowser = browser;
                   1139: 
                   1140:   if (_webBrowser) {
                   1141:     // Set the container nsIWebBrowserChrome
                   1142:     _webBrowser->SetContainerWindow(NS_STATIC_CAST(nsIWebBrowserChrome *, 
                   1143:               _listener));
                   1144: 
                   1145:     NSRect frame = [self frame];
                   1146:  
                   1147:     // Hook up the widget hierarchy with us as the parent
                   1148:     nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(_webBrowser);
                   1149:     baseWin->InitWindow((NSView*)self, nsnull, 0, 0, 
                   1150:      frame.size.width, frame.size.height);
                   1151:     baseWin->Create();
                   1152:   }
                   1153: 
1.2       hyatt    1154: }
                   1155: 
                   1156: -(void) saveInternal: (nsIURI*)aURI
                   1157:         withDocument: (nsIDOMDocument*)aDocument
                   1158:         bypassCache: (BOOL)aBypassCache
                   1159:         filterView: (NSView*)aFilterView
                   1160:         filterList: (NSPopUpButton*)aFilterList
                   1161: {
                   1162:     // Create our web browser persist object.  This is the object that knows
                   1163:     // how to actually perform the saving of the page (and of the images
                   1164:     // on the page).
                   1165:     nsCOMPtr<nsIWebBrowserPersist> webPersist(do_CreateInstance(persistContractID));
                   1166:     if (!webPersist)
                   1167:         return;
                   1168:     
                   1169:     // Make a temporary file object that we can save to.
                   1170:     nsCOMPtr<nsIProperties> dirService(do_GetService(dirServiceContractID));
                   1171:     if (!dirService)
                   1172:         return;
                   1173:     nsCOMPtr<nsIFile> tmpFile;
                   1174:     dirService->Get("TmpD", NS_GET_IID(nsIFile), getter_AddRefs(tmpFile));
                   1175:     static short unsigned int tmpRandom = 0;
1.24      hyatt    1176:     nsCAutoString tmpNo; tmpNo.AppendInt(tmpRandom++);
1.2       hyatt    1177:     nsCAutoString saveFile("-sav");
                   1178:     saveFile += tmpNo;
                   1179:     saveFile += "tmp";
                   1180:     tmpFile->Append(saveFile.get()); 
                   1181:     
                   1182:     // Get the post data if we're an HTML doc.
                   1183:     nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(_webBrowser));
                   1184:     nsCOMPtr<nsISHistory> sessionHistory;
                   1185:     webNav->GetSessionHistory(getter_AddRefs(sessionHistory));
1.3       hyatt    1186:     nsCOMPtr<nsIHistoryEntry> entry;
                   1187:     PRInt32 sindex;
                   1188:     sessionHistory->GetIndex(&sindex);
                   1189:     sessionHistory->GetEntryAtIndex(sindex, PR_FALSE, getter_AddRefs(entry));
                   1190:     nsCOMPtr<nsISHEntry> shEntry(do_QueryInterface(entry));
                   1191:     nsCOMPtr<nsIInputStream> postData;
                   1192:     if (shEntry)
                   1193:         shEntry->GetPostData(getter_AddRefs(postData));
                   1194:     
1.2       hyatt    1195:     nsHeaderSniffer* sniffer = new nsHeaderSniffer(webPersist, tmpFile, aURI, 
1.3       hyatt    1196:                                                    aDocument, postData, aBypassCache,
1.2       hyatt    1197:                                                    aFilterView, aFilterList);
                   1198:     if (!sniffer)
                   1199:         return;
                   1200:     webPersist->SetProgressListener(sniffer);
                   1201:     webPersist->SaveURI(aURI, nsnull, tmpFile);
1.8       hyatt    1202: }
                   1203: 
                   1204: -(void)printDocument
                   1205: {
                   1206:     nsCOMPtr<nsIDOMWindow> domWindow;
                   1207:     _webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
                   1208:     nsCOMPtr<nsIInterfaceRequestor> ir(do_QueryInterface(domWindow));
                   1209:     nsCOMPtr<nsIWebBrowserPrint> print;
                   1210:     ir->GetInterface(NS_GET_IID(nsIWebBrowserPrint), getter_AddRefs(print));
                   1211:     print->Print(nsnull, nsnull);
1.2       hyatt    1212: }
                   1213: 
1.14      hyatt    1214: -(void)findInPage
                   1215: {
1.15      hyatt    1216:     FindDlgController* findDialog = [[FindDlgController alloc] initWithWindowNibName: @"FindDialog"];
1.16      hyatt    1217:     nsCOMPtr<nsIWebBrowserFocus> wbf(do_QueryInterface(_webBrowser));
                   1218:     nsCOMPtr<nsIDOMWindow> rootWindow;
                   1219:     nsCOMPtr<nsIDOMWindow> focusedWindow;
                   1220:     _webBrowser->GetContentDOMWindow(getter_AddRefs(rootWindow));
                   1221:     wbf->GetFocusedWindow(getter_AddRefs(focusedWindow));
                   1222:     if (!focusedWindow)
                   1223:         focusedWindow = rootWindow;
                   1224:     nsCOMPtr<nsIWebBrowserFind> webFind(do_GetInterface(_webBrowser));
                   1225:     nsCOMPtr<nsIWebBrowserFindInFrames> framesFind(do_QueryInterface(webFind));
                   1226:     framesFind->SetRootSearchFrame(rootWindow);
                   1227:     framesFind->SetCurrentSearchFrame(focusedWindow);
                   1228:     
                   1229:     [findDialog setFind: webFind];
1.15      hyatt    1230:     [findDialog showWindow: findDialog];
1.14      hyatt    1231: }
                   1232: 
                   1233: -(void)findAgain
                   1234: {
                   1235: }
                   1236: 
1.2       hyatt    1237: -(void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
                   1238: {
                   1239:     nsCOMPtr<nsIWebBrowserFocus> wbf(do_QueryInterface(_webBrowser));
                   1240:     nsCOMPtr<nsIDOMWindow> domWindow;
                   1241:     wbf->GetFocusedWindow(getter_AddRefs(domWindow));
                   1242:     if (!domWindow)
                   1243:         _webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
                   1244:     if (!domWindow)
                   1245:         return;
                   1246:     
                   1247:     nsCOMPtr<nsIDOMDocument> domDocument;
                   1248:     domWindow->GetDocument(getter_AddRefs(domDocument));
                   1249:     if (!domDocument)
                   1250:         return;
1.17      hyatt    1251:     nsCOMPtr<nsIDOMNSDocument> nsDoc(do_QueryInterface(domDocument));
                   1252:     if (!nsDoc)
                   1253:         return;
                   1254:     nsCOMPtr<nsIDOMLocation> location;
                   1255:     nsDoc->GetLocation(getter_AddRefs(location));
                   1256:     if (!location)
                   1257:         return;
                   1258:     nsAutoString urlStr;
                   1259:     location->GetHref(urlStr);
                   1260:     nsCAutoString urlCStr; urlCStr.AssignWithConversion(urlStr);
                   1261:     nsCOMPtr<nsIURI> url;
                   1262:     nsresult rv = NS_NewURI(getter_AddRefs(url), urlCStr.get());
                   1263:     if (NS_FAILED(rv))
                   1264:         return;
                   1265:         
                   1266: #if 0
1.2       hyatt    1267:     nsCOMPtr<nsIDocument> doc(do_QueryInterface(domDocument));
                   1268:     if (!doc)
                   1269:         return;
                   1270:     nsCOMPtr<nsIURI> url;
                   1271:     doc->GetDocumentURL(getter_AddRefs(url));
1.17      hyatt    1272: #endif
1.2       hyatt    1273:      
                   1274:     [self saveInternal: url.get()
                   1275:           withDocument: domDocument
                   1276:           bypassCache: NO
                   1277:           filterView: aFilterView
                   1278:           filterList: aFilterList];
1.11      hyatt    1279: }
                   1280: 
                   1281: 
1.12      hyatt    1282: -(IBAction)cut:(id)aSender
1.11      hyatt    1283: {
                   1284:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
                   1285:     clipboard->CutSelection();
                   1286: }
                   1287: 
1.12      hyatt    1288: -(IBAction)copy:(id)aSender
1.11      hyatt    1289: {
                   1290:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
                   1291:     clipboard->CopySelection();
                   1292: }
                   1293: 
1.12      hyatt    1294: -(IBAction)paste:(id)aSender
1.11      hyatt    1295: {
                   1296:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
                   1297:     clipboard->Paste();
                   1298: }
                   1299: 
1.12      hyatt    1300: -(IBAction)clear:(id)aSender
1.11      hyatt    1301: {
                   1302:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
                   1303:     clipboard->SelectNone();
                   1304: }
                   1305: 
1.12      hyatt    1306: -(IBAction)selectAll:(id)aSender
1.11      hyatt    1307: {
                   1308:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
                   1309:     clipboard->SelectAll();
1.1       hyatt    1310: }
                   1311: 
1.27      pinkerto 1312: -(IBAction)undo:(id)aSender
                   1313: {
                   1314:   NSLog(@"Undo not yet implemented");
                   1315: }
                   1316: 
1.19      hyatt    1317: -(NSString*)getCurrentURLSpec
                   1318: {
                   1319:     NSString* empty = @"";
                   1320:     nsCOMPtr<nsIDOMWindow> domWindow;
                   1321:     _webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
                   1322:     if (!domWindow)
                   1323:         return empty;
                   1324:     
                   1325:     nsCOMPtr<nsIDOMDocument> domDocument;
                   1326:     domWindow->GetDocument(getter_AddRefs(domDocument));
                   1327:     if (!domDocument)
                   1328:         return empty;
                   1329:     nsCOMPtr<nsIDOMNSDocument> nsDoc(do_QueryInterface(domDocument));
                   1330:     if (!nsDoc)
                   1331:         return empty;
                   1332:     nsCOMPtr<nsIDOMLocation> location;
                   1333:     nsDoc->GetLocation(getter_AddRefs(location));
                   1334:     if (!location)
                   1335:         return empty;
                   1336:     nsAutoString urlStr;
                   1337:     location->GetHref(urlStr);
                   1338:     nsCAutoString urlCStr; urlCStr.AssignWithConversion(urlStr);
                   1339:     
                   1340:     return [NSString stringWithCString: urlCStr.get()];
                   1341: }
1.21      hyatt    1342: 
                   1343: - (void)setActive: (BOOL)aIsActive
                   1344: {
                   1345:     nsCOMPtr<nsIWebBrowserFocus> wbf(do_QueryInterface(_webBrowser));
                   1346:     if (aIsActive)
                   1347:         wbf->Activate();
                   1348:     else
                   1349:         wbf->Deactivate();
                   1350: }
1.1       hyatt    1351: @end
                   1352: 

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