Annotation of chimera/NSBrowserView.mm, revision 1.19

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

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