Annotation of chimera/NSBrowserView.mm, revision 1.27

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));
                    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));
1.25      hyatt     762:         if (url)
                    763:             url->GetFileName(defaultFileName); // (2) For file URLs, use the file name.
1.2       hyatt     764:     }
                    765:     
                    766:     if (defaultFileName.IsEmpty() && mDocument && isHTML) {
                    767:         nsCOMPtr<nsIDOMHTMLDocument> htmlDoc(do_QueryInterface(mDocument));
                    768:         nsAutoString title;
                    769:         if (htmlDoc)
                    770:             htmlDoc->GetTitle(title); // (3) Use the title of the document.
                    771:         defaultFileName.AssignWithConversion(title);
                    772:     }
                    773:     
                    774:     if (defaultFileName.IsEmpty()) {
                    775:         // (4) Use the caller provided name. XXXdwh
                    776:     }
                    777: 
1.25      hyatt     778:     if (defaultFileName.IsEmpty())
1.2       hyatt     779:         // (5) Use the host.
1.25      hyatt     780:         mURL->GetHost(defaultFileName);
1.2       hyatt     781:     
                    782:     // One last case to handle about:blank and other fruity untitled pages.
                    783:     if (defaultFileName.IsEmpty())
                    784:         defaultFileName = "untitled";
                    785:         
                    786:     // Validate the file name to ensure legality.
                    787:     for (PRUint32 i = 0; i < defaultFileName.Length(); i++)
                    788:         if (defaultFileName[i] == ':' || defaultFileName[i] == '/')
                    789:             defaultFileName.SetCharAt(i, ' ');
                    790:             
                    791:     // Make sure the appropriate extension is appended to the suggested file name.
                    792:     nsCOMPtr<nsIURI> fileURI(do_CreateInstance("@mozilla.org/network/standard-url;1"));
                    793:     nsCOMPtr<nsIURL> fileURL(do_QueryInterface(fileURI));
                    794:     if (!fileURL)
                    795:         return;
1.25      hyatt     796:     fileURL->SetFilePath(defaultFileName);
1.2       hyatt     797:     
1.25      hyatt     798:     nsCAutoString fileExtension;
                    799:     fileURL->GetFileExtension(fileExtension);
1.2       hyatt     800:     
                    801:     PRBool setExtension = PR_FALSE;
                    802:     if (mContentType.Equals("text/html")) {
                    803:         if (fileExtension.IsEmpty() || (!fileExtension.Equals("htm") && !fileExtension.Equals("html"))) {
                    804:             defaultFileName += ".html";
                    805:             setExtension = PR_TRUE;
                    806:         }
                    807:     }
                    808:     
                    809:     if (!setExtension && fileExtension.IsEmpty()) {
                    810:         nsCOMPtr<nsIMIMEService> mimeService(do_GetService("@mozilla.org/mime;1"));
                    811:         if (!mimeService)
                    812:             return;
                    813:         nsCOMPtr<nsIMIMEInfo> mimeInfo;
                    814:         mimeService->GetFromMIMEType(mContentType.get(), getter_AddRefs(mimeInfo));
                    815:         
                    816:         PRUint32 extCount;
                    817:         char** extList;
                    818:         mimeInfo->GetFileExtensions(&extCount, &extList);
                    819:         
                    820:         if (extCount > 0) {
                    821:             defaultFileName += ".";
                    822:             defaultFileName += extList[0];
                    823:         }
                    824:     }
                    825:     
                    826:     // Now it's time to pose the save dialog.
                    827:     NSSavePanel* savePanel = [NSSavePanel savePanel];
                    828:     NSString* file = nil;
                    829:     if (!defaultFileName.IsEmpty())
                    830:         file = [[NSString alloc] initWithCString: defaultFileName.get()];
                    831:         
                    832:     if (isHTML)
                    833:         [savePanel setAccessoryView: mFilterView];
                    834:         
                    835:     if ([savePanel runModalForDirectory: nil file: file] == NSFileHandlingPanelCancelButton)
                    836:         return;
                    837:        
                    838:     // Release the file string.
                    839:     [file release];
                    840:     
                    841:     // Update the filter index.
                    842:     if (isHTML && mFilterList) {
                    843:         filterIndex = [mFilterList indexOfSelectedItem];
                    844:         dirBranch->SetIntPref("save_converter_index", filterIndex);
                    845:     }
                    846:     
                    847:     // Convert the content type to text/plain if it was selected in the filter.
                    848:     if (isHTML && filterIndex == 2)
                    849:         mContentType = "text/plain";
                    850:     
                    851:     nsCOMPtr<nsISupports> sourceData;
                    852:     if (isHTML && filterIndex != 1)
                    853:         sourceData = do_QueryInterface(mDocument);
                    854:     else
                    855:         sourceData = do_QueryInterface(mURL);
                    856:         
1.4       hyatt     857:     nsCOMPtr<nsIWebBrowserPersist> webPersist(do_CreateInstance(persistContractID));
1.2       hyatt     858:     ProgressDlgController* progressDialog = [[ProgressDlgController alloc] initWithWindowNibName: @"ProgressDialog"];
1.4       hyatt     859:     [progressDialog setWebPersist: webPersist 
1.2       hyatt     860:                     source: sourceData.get()
                    861:                     destination: [savePanel filename]
                    862:                     contentType: mContentType.get()
1.3       hyatt     863:                     postData: mPostData
                    864:                     bypassCache: mBypassCache];
                    865:                     
1.2       hyatt     866:     [progressDialog showWindow: progressDialog];
                    867: }
                    868: 
                    869: /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
                    870: NS_IMETHODIMP 
                    871: nsHeaderSniffer::OnProgressChange(nsIWebProgress *aWebProgress, 
                    872:           nsIRequest *aRequest, 
                    873:           PRInt32 aCurSelfProgress, 
                    874:           PRInt32 aMaxSelfProgress, 
                    875:           PRInt32 aCurTotalProgress, 
                    876:           PRInt32 aMaxTotalProgress)
                    877: {
                    878:   return NS_OK;
                    879: }
                    880: 
                    881: /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
                    882: NS_IMETHODIMP 
                    883: nsHeaderSniffer::OnLocationChange(nsIWebProgress *aWebProgress, 
                    884:           nsIRequest *aRequest, 
                    885:           nsIURI *location)
                    886: {
                    887:   return NS_OK;
                    888: }
                    889: 
                    890: /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
                    891: NS_IMETHODIMP 
                    892: nsHeaderSniffer::OnStatusChange(nsIWebProgress *aWebProgress, 
                    893:               nsIRequest *aRequest, 
                    894:               nsresult aStatus, 
                    895:               const PRUnichar *aMessage)
                    896: {
                    897:   return NS_OK;
                    898: }
                    899: 
                    900: /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long state); */
                    901: NS_IMETHODIMP 
                    902: nsHeaderSniffer::OnSecurityChange(nsIWebProgress *aWebProgress, 
                    903:                        nsIRequest *aRequest, 
                    904:                                   PRInt32 state)
                    905: {
                    906:   return NS_OK;
                    907: }
                    908: 
1.1       hyatt     909: 
                    910: @implementation NSBrowserView
                    911: 
                    912: - (id)initWithFrame:(NSRect)frame
                    913: {
                    914:   [super initWithFrame:frame];
                    915: 
                    916:   nsresult rv = nsCocoaBrowserService::InitEmbedding();
                    917:   if (NS_FAILED(rv)) {
                    918:     // XXX need to throw
                    919:   }
                    920:   
                    921:   _listener = new nsCocoaBrowserListener(self);
                    922:   NS_ADDREF(_listener);
                    923: 
                    924:   // Create the web browser instance
                    925:   nsCOMPtr<nsIWebBrowser> browser = do_CreateInstance(NS_WEBBROWSER_CONTRACTID, &rv);
                    926:   if (NS_FAILED(rv)) {
                    927:     // XXX need to throw
                    928:   }
                    929: 
                    930:   _webBrowser = browser;
                    931:   NS_ADDREF(_webBrowser);
                    932: 
                    933:   // Set the container nsIWebBrowserChrome
                    934:   _webBrowser->SetContainerWindow(NS_STATIC_CAST(nsIWebBrowserChrome *, 
                    935:             _listener));
                    936:   
                    937:   // Register as a listener for web progress
                    938:   nsCOMPtr<nsIWeakReference> weak = do_GetWeakReference(NS_STATIC_CAST(nsIWebProgressListener*, _listener));
                    939:   _webBrowser->AddWebBrowserListener(weak, NS_GET_IID(nsIWebProgressListener));
                    940: 
                    941:   // Hook up the widget hierarchy with us as the parent
                    942:   nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(_webBrowser);
                    943:   baseWin->InitWindow((NSView*)self, nsnull, 0, 0, 
                    944:          frame.size.width, frame.size.height);
                    945:   baseWin->Create();
                    946: 
                    947:   return self;
                    948: }
                    949: 
1.6       hyatt     950: - (void)destroyWebBrowser
                    951: {
                    952:   nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(_webBrowser);
                    953:   baseWin->Destroy();
                    954: }
                    955: 
1.1       hyatt     956: - (void)dealloc 
                    957: {
1.23      hyatt     958:   [super dealloc];
                    959:   
1.1       hyatt     960:   NS_RELEASE(_listener);
                    961:   NS_IF_RELEASE(_webBrowser);
1.10      hyatt     962:   
                    963:   nsCocoaBrowserService::BrowserClosed();
1.18      hyatt     964:   
                    965:   printf("NSBrowserView died.\n");
1.1       hyatt     966: }
                    967: 
                    968: - (void)setFrame:(NSRect)frameRect 
                    969: {
                    970:   [super setFrame:frameRect];
                    971:   if (_webBrowser) {
                    972:     nsCOMPtr<nsIBaseWindow> window = do_QueryInterface(_webBrowser);
                    973:     window->SetSize((PRInt32)frameRect.size.width, 
                    974:        (PRInt32)frameRect.size.height,
                    975:        PR_TRUE);
                    976:   }
                    977: }
                    978: 
                    979: - (void)addListener:(id <NSBrowserListener>)listener
                    980: {
                    981:   _listener->AddListener(listener);
                    982: }
                    983: 
                    984: - (void)removeListener:(id <NSBrowserListener>)listener
                    985: {
                    986:   _listener->RemoveListener(listener);
                    987: }
                    988: 
                    989: - (void)setContainer:(id <NSBrowserContainer>)container
                    990: {
                    991:   _listener->SetContainer(container);
                    992: }
                    993: 
                    994: - (nsIDOMWindow*)getContentWindow
                    995: {
                    996:   nsIDOMWindow* window;
                    997: 
                    998:   _webBrowser->GetContentDOMWindow(&window);
                    999: 
                   1000:   return window;
                   1001: }
                   1002: 
                   1003: - (void)loadURI:(NSURL *)url flags:(unsigned int)flags
                   1004: {
                   1005:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1006:   
                   1007:   NSString* spec = [url absoluteString];
                   1008:   int length = [spec length];
                   1009:   PRUnichar* specStr = nsMemory::Alloc((length+1) * sizeof(PRUnichar));
                   1010:   [spec getCharacters:specStr];
                   1011:   specStr[length] = PRUnichar(0);
                   1012:   
                   1013: 
                   1014:   PRUint32 navFlags = nsIWebNavigation::LOAD_FLAGS_NONE;
                   1015:   if (flags & NSLoadFlagsDontPutInHistory) {
                   1016:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_HISTORY;
                   1017:   }
                   1018:   if (flags & NSLoadFlagsReplaceHistoryEntry) {
                   1019:     navFlags |= nsIWebNavigation::LOAD_FLAGS_REPLACE_HISTORY;
                   1020:   }
                   1021:   if (flags & NSLoadFlagsBypassCacheAndProxy) {
                   1022:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE | 
                   1023:                 nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY;
                   1024:   }
                   1025: 
                   1026:   nsresult rv = nav->LoadURI(specStr, navFlags, nsnull, nsnull, nsnull);
                   1027:   if (NS_FAILED(rv)) {
                   1028:     // XXX need to throw
                   1029:   }
                   1030: 
                   1031:   nsMemory::Free(specStr);
                   1032: }
                   1033: 
                   1034: - (void)reload:(unsigned int)flags
                   1035: {
                   1036:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1037: 
                   1038:   PRUint32 navFlags = nsIWebNavigation::LOAD_FLAGS_NONE;
                   1039:   if (flags & NSLoadFlagsBypassCacheAndProxy) {
                   1040:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE | 
                   1041:                 nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY;
                   1042:   }
                   1043: 
                   1044:   nsresult rv = nav->Reload(navFlags);
                   1045:   if (NS_FAILED(rv)) {
                   1046:     // XXX need to throw
                   1047:   }  
                   1048: }
                   1049: 
                   1050: - (BOOL)canGoBack
                   1051: {
                   1052:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1053: 
                   1054:   PRBool can;
                   1055:   nav->GetCanGoBack(&can);
                   1056: 
                   1057:   return can ? YES : NO;
                   1058: }
                   1059: 
                   1060: - (BOOL)canGoForward
                   1061: {
                   1062:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1063: 
                   1064:   PRBool can;
                   1065:   nav->GetCanGoForward(&can);
                   1066: 
                   1067:   return can ? YES : NO;
                   1068: }
                   1069: 
                   1070: - (void)goBack
                   1071: {
                   1072:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1073: 
                   1074:   nsresult rv = nav->GoBack();
                   1075:   if (NS_FAILED(rv)) {
                   1076:     // XXX need to throw
                   1077:   }  
                   1078: }
                   1079: 
                   1080: - (void)goForward
                   1081: {
                   1082:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1083: 
                   1084:   nsresult rv = nav->GoForward();
                   1085:   if (NS_FAILED(rv)) {
                   1086:     // XXX need to throw
                   1087:   }  
                   1088: }
                   1089: 
                   1090: - (void)gotoIndex:(int)index
                   1091: {
                   1092:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1093: 
                   1094:   nsresult rv = nav->GotoIndex(index);
                   1095:   if (NS_FAILED(rv)) {
                   1096:     // XXX need to throw
                   1097:   }    
                   1098: }
                   1099: 
                   1100: - (void)stop:(unsigned int)flags
                   1101: {
                   1102:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1103: 
                   1104:   nsresult rv = nav->Stop(flags);
                   1105:   if (NS_FAILED(rv)) {
                   1106:     // XXX need to throw
                   1107:   }    
                   1108: }
                   1109: 
                   1110: - (NSURL*)getCurrentURI
                   1111: {
                   1112:   nsCOMPtr<nsIURI> uri;
                   1113:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
                   1114: 
                   1115:   nav->GetCurrentURI(getter_AddRefs(uri));
                   1116:   if (!uri) {
                   1117:     return nsnull;
                   1118:   }
                   1119: 
1.25      hyatt    1120:   nsCAutoString spec;
                   1121:   uri->GetSpec(spec);
1.1       hyatt    1122:   
                   1123:   const char* cstr = spec.get();
                   1124:   NSString* str = [NSString stringWithCString:cstr];
                   1125:   NSURL* url = [NSURL URLWithString:str];
                   1126:   
                   1127:   return url;
1.22      hyatt    1128: }
                   1129: 
                   1130: - (nsCocoaBrowserListener*)getCocoaBrowserListener
                   1131: {
                   1132:   return _listener;
1.1       hyatt    1133: }
                   1134: 
                   1135: - (nsIWebBrowser*)getWebBrowser
                   1136: {
                   1137:   NS_IF_ADDREF(_webBrowser);
                   1138:   return _webBrowser;
                   1139: }
                   1140: 
                   1141: - (void)setWebBrowser:(nsIWebBrowser*)browser
                   1142: {
                   1143:   _webBrowser = browser;
                   1144: 
                   1145:   if (_webBrowser) {
                   1146:     // Set the container nsIWebBrowserChrome
                   1147:     _webBrowser->SetContainerWindow(NS_STATIC_CAST(nsIWebBrowserChrome *, 
                   1148:               _listener));
                   1149: 
                   1150:     NSRect frame = [self frame];
                   1151:  
                   1152:     // Hook up the widget hierarchy with us as the parent
                   1153:     nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(_webBrowser);
                   1154:     baseWin->InitWindow((NSView*)self, nsnull, 0, 0, 
                   1155:      frame.size.width, frame.size.height);
                   1156:     baseWin->Create();
                   1157:   }
                   1158: 
1.2       hyatt    1159: }
                   1160: 
                   1161: -(void) saveInternal: (nsIURI*)aURI
                   1162:         withDocument: (nsIDOMDocument*)aDocument
                   1163:         bypassCache: (BOOL)aBypassCache
                   1164:         filterView: (NSView*)aFilterView
                   1165:         filterList: (NSPopUpButton*)aFilterList
                   1166: {
                   1167:     // Create our web browser persist object.  This is the object that knows
                   1168:     // how to actually perform the saving of the page (and of the images
                   1169:     // on the page).
                   1170:     nsCOMPtr<nsIWebBrowserPersist> webPersist(do_CreateInstance(persistContractID));
                   1171:     if (!webPersist)
                   1172:         return;
                   1173:     
                   1174:     // Make a temporary file object that we can save to.
                   1175:     nsCOMPtr<nsIProperties> dirService(do_GetService(dirServiceContractID));
                   1176:     if (!dirService)
                   1177:         return;
                   1178:     nsCOMPtr<nsIFile> tmpFile;
                   1179:     dirService->Get("TmpD", NS_GET_IID(nsIFile), getter_AddRefs(tmpFile));
                   1180:     static short unsigned int tmpRandom = 0;
1.24      hyatt    1181:     nsCAutoString tmpNo; tmpNo.AppendInt(tmpRandom++);
1.2       hyatt    1182:     nsCAutoString saveFile("-sav");
                   1183:     saveFile += tmpNo;
                   1184:     saveFile += "tmp";
                   1185:     tmpFile->Append(saveFile.get()); 
                   1186:     
                   1187:     // Get the post data if we're an HTML doc.
                   1188:     nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(_webBrowser));
                   1189:     nsCOMPtr<nsISHistory> sessionHistory;
                   1190:     webNav->GetSessionHistory(getter_AddRefs(sessionHistory));
1.3       hyatt    1191:     nsCOMPtr<nsIHistoryEntry> entry;
                   1192:     PRInt32 sindex;
                   1193:     sessionHistory->GetIndex(&sindex);
                   1194:     sessionHistory->GetEntryAtIndex(sindex, PR_FALSE, getter_AddRefs(entry));
                   1195:     nsCOMPtr<nsISHEntry> shEntry(do_QueryInterface(entry));
                   1196:     nsCOMPtr<nsIInputStream> postData;
                   1197:     if (shEntry)
                   1198:         shEntry->GetPostData(getter_AddRefs(postData));
                   1199:     
1.2       hyatt    1200:     nsHeaderSniffer* sniffer = new nsHeaderSniffer(webPersist, tmpFile, aURI, 
1.3       hyatt    1201:                                                    aDocument, postData, aBypassCache,
1.2       hyatt    1202:                                                    aFilterView, aFilterList);
                   1203:     if (!sniffer)
                   1204:         return;
                   1205:     webPersist->SetProgressListener(sniffer);
                   1206:     webPersist->SaveURI(aURI, nsnull, tmpFile);
1.8       hyatt    1207: }
                   1208: 
                   1209: -(void)printDocument
                   1210: {
                   1211:     nsCOMPtr<nsIDOMWindow> domWindow;
                   1212:     _webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
                   1213:     nsCOMPtr<nsIInterfaceRequestor> ir(do_QueryInterface(domWindow));
                   1214:     nsCOMPtr<nsIWebBrowserPrint> print;
                   1215:     ir->GetInterface(NS_GET_IID(nsIWebBrowserPrint), getter_AddRefs(print));
                   1216:     print->Print(nsnull, nsnull);
1.2       hyatt    1217: }
                   1218: 
1.14      hyatt    1219: -(void)findInPage
                   1220: {
1.15      hyatt    1221:     FindDlgController* findDialog = [[FindDlgController alloc] initWithWindowNibName: @"FindDialog"];
1.16      hyatt    1222:     nsCOMPtr<nsIWebBrowserFocus> wbf(do_QueryInterface(_webBrowser));
                   1223:     nsCOMPtr<nsIDOMWindow> rootWindow;
                   1224:     nsCOMPtr<nsIDOMWindow> focusedWindow;
                   1225:     _webBrowser->GetContentDOMWindow(getter_AddRefs(rootWindow));
                   1226:     wbf->GetFocusedWindow(getter_AddRefs(focusedWindow));
                   1227:     if (!focusedWindow)
                   1228:         focusedWindow = rootWindow;
                   1229:     nsCOMPtr<nsIWebBrowserFind> webFind(do_GetInterface(_webBrowser));
                   1230:     nsCOMPtr<nsIWebBrowserFindInFrames> framesFind(do_QueryInterface(webFind));
                   1231:     framesFind->SetRootSearchFrame(rootWindow);
                   1232:     framesFind->SetCurrentSearchFrame(focusedWindow);
                   1233:     
                   1234:     [findDialog setFind: webFind];
1.15      hyatt    1235:     [findDialog showWindow: findDialog];
1.14      hyatt    1236: }
                   1237: 
                   1238: -(void)findAgain
                   1239: {
                   1240: }
                   1241: 
1.2       hyatt    1242: -(void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
                   1243: {
                   1244:     nsCOMPtr<nsIWebBrowserFocus> wbf(do_QueryInterface(_webBrowser));
                   1245:     nsCOMPtr<nsIDOMWindow> domWindow;
                   1246:     wbf->GetFocusedWindow(getter_AddRefs(domWindow));
                   1247:     if (!domWindow)
                   1248:         _webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
                   1249:     if (!domWindow)
                   1250:         return;
                   1251:     
                   1252:     nsCOMPtr<nsIDOMDocument> domDocument;
                   1253:     domWindow->GetDocument(getter_AddRefs(domDocument));
                   1254:     if (!domDocument)
                   1255:         return;
1.17      hyatt    1256:     nsCOMPtr<nsIDOMNSDocument> nsDoc(do_QueryInterface(domDocument));
                   1257:     if (!nsDoc)
                   1258:         return;
                   1259:     nsCOMPtr<nsIDOMLocation> location;
                   1260:     nsDoc->GetLocation(getter_AddRefs(location));
                   1261:     if (!location)
                   1262:         return;
                   1263:     nsAutoString urlStr;
                   1264:     location->GetHref(urlStr);
                   1265:     nsCAutoString urlCStr; urlCStr.AssignWithConversion(urlStr);
                   1266:     nsCOMPtr<nsIURI> url;
                   1267:     nsresult rv = NS_NewURI(getter_AddRefs(url), urlCStr.get());
                   1268:     if (NS_FAILED(rv))
                   1269:         return;
                   1270:         
                   1271: #if 0
1.2       hyatt    1272:     nsCOMPtr<nsIDocument> doc(do_QueryInterface(domDocument));
                   1273:     if (!doc)
                   1274:         return;
                   1275:     nsCOMPtr<nsIURI> url;
                   1276:     doc->GetDocumentURL(getter_AddRefs(url));
1.17      hyatt    1277: #endif
1.2       hyatt    1278:      
                   1279:     [self saveInternal: url.get()
                   1280:           withDocument: domDocument
                   1281:           bypassCache: NO
                   1282:           filterView: aFilterView
                   1283:           filterList: aFilterList];
1.11      hyatt    1284: }
                   1285: 
                   1286: 
1.12      hyatt    1287: -(IBAction)cut:(id)aSender
1.11      hyatt    1288: {
                   1289:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
                   1290:     clipboard->CutSelection();
                   1291: }
                   1292: 
1.12      hyatt    1293: -(IBAction)copy:(id)aSender
1.11      hyatt    1294: {
                   1295:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
                   1296:     clipboard->CopySelection();
                   1297: }
                   1298: 
1.12      hyatt    1299: -(IBAction)paste:(id)aSender
1.11      hyatt    1300: {
                   1301:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
                   1302:     clipboard->Paste();
                   1303: }
                   1304: 
1.12      hyatt    1305: -(IBAction)clear:(id)aSender
1.11      hyatt    1306: {
                   1307:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
                   1308:     clipboard->SelectNone();
                   1309: }
                   1310: 
1.12      hyatt    1311: -(IBAction)selectAll:(id)aSender
1.11      hyatt    1312: {
                   1313:     nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(_webBrowser));
                   1314:     clipboard->SelectAll();
1.1       hyatt    1315: }
                   1316: 
1.27    ! pinkerto 1317: -(IBAction)undo:(id)aSender
        !          1318: {
        !          1319:   NSLog(@"Undo not yet implemented");
        !          1320: }
        !          1321: 
1.19      hyatt    1322: -(NSString*)getCurrentURLSpec
                   1323: {
                   1324:     NSString* empty = @"";
                   1325:     nsCOMPtr<nsIDOMWindow> domWindow;
                   1326:     _webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
                   1327:     if (!domWindow)
                   1328:         return empty;
                   1329:     
                   1330:     nsCOMPtr<nsIDOMDocument> domDocument;
                   1331:     domWindow->GetDocument(getter_AddRefs(domDocument));
                   1332:     if (!domDocument)
                   1333:         return empty;
                   1334:     nsCOMPtr<nsIDOMNSDocument> nsDoc(do_QueryInterface(domDocument));
                   1335:     if (!nsDoc)
                   1336:         return empty;
                   1337:     nsCOMPtr<nsIDOMLocation> location;
                   1338:     nsDoc->GetLocation(getter_AddRefs(location));
                   1339:     if (!location)
                   1340:         return empty;
                   1341:     nsAutoString urlStr;
                   1342:     location->GetHref(urlStr);
                   1343:     nsCAutoString urlCStr; urlCStr.AssignWithConversion(urlStr);
                   1344:     
                   1345:     return [NSString stringWithCString: urlCStr.get()];
                   1346: }
1.21      hyatt    1347: 
                   1348: - (void)setActive: (BOOL)aIsActive
                   1349: {
                   1350:     nsCOMPtr<nsIWebBrowserFocus> wbf(do_QueryInterface(_webBrowser));
                   1351:     if (aIsActive)
                   1352:         wbf->Activate();
                   1353:     else
                   1354:         wbf->Deactivate();
                   1355: }
1.1       hyatt    1356: @end
                   1357: 

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