File:  [mozdev] / chimera / NSBrowserView.mm
Revision 1.25: download - view: text, annotated - select for diffs - revision graph
Thu Mar 7 03:18:55 2002 UTC (17 years, 8 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Recover from darin's URL landing.

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

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