Annotation of chimera/NSBrowserView.mm, revision 1.35

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

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