Back to index

lightning-sunbird  0.9+nobinonly
nsContentTreeOwner.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is the Mozilla browser.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications, Inc.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Travis Bogard <travis@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 // Local Includes
00041 #include "nsContentTreeOwner.h"
00042 #include "nsXULWindow.h"
00043 
00044 // Helper Classes
00045 #include "nsIGenericFactory.h"
00046 #include "nsIServiceManager.h"
00047 #include "nsAutoPtr.h"
00048 
00049 // Interfaces needed to be included
00050 #include "nsIDOMNode.h"
00051 #include "nsIDOMElement.h"
00052 #include "nsIDOMNodeList.h"
00053 #include "nsIDOMWindowInternal.h"
00054 #include "nsIDOMChromeWindow.h"
00055 #include "nsIBrowserDOMWindow.h"
00056 #include "nsIDOMXULElement.h"
00057 #include "nsIEmbeddingSiteWindow.h"
00058 #include "nsIEmbeddingSiteWindow2.h"
00059 #include "nsIPrompt.h"
00060 #include "nsIAuthPrompt.h"
00061 #include "nsIWindowMediator.h"
00062 #include "nsIXULBrowserWindow.h"
00063 #include "nsIPrincipal.h"
00064 #include "nsIURIFixup.h"
00065 #include "nsCDefaultURIFixup.h"
00066 #include "nsIPrefService.h"
00067 #include "nsIPrefBranch.h"
00068 #include "nsIWebNavigation.h"
00069 
00070 // Needed for nsIDocument::FlushPendingNotifications(...)
00071 #include "nsIDOMDocument.h"
00072 #include "nsIDocument.h"
00073 
00074 // CIDs
00075 static NS_DEFINE_CID(kWindowMediatorCID, NS_WINDOWMEDIATOR_CID);
00076 
00077 //*****************************************************************************
00078 //*** nsSiteWindow2 declaration
00079 //*****************************************************************************
00080 
00081 class nsSiteWindow2 : public nsIEmbeddingSiteWindow2
00082 {
00083 public:
00084   nsSiteWindow2(nsContentTreeOwner *aAggregator);
00085   virtual ~nsSiteWindow2();
00086 
00087   NS_DECL_ISUPPORTS
00088   NS_DECL_NSIEMBEDDINGSITEWINDOW
00089   NS_DECL_NSIEMBEDDINGSITEWINDOW2
00090 
00091 private:
00092   nsContentTreeOwner *mAggregator;
00093 };
00094 
00095 //*****************************************************************************
00096 //***    nsContentTreeOwner: Object Management
00097 //*****************************************************************************
00098 
00099 nsContentTreeOwner::nsContentTreeOwner(PRBool fPrimary) : mXULWindow(nsnull), 
00100    mPrimary(fPrimary), mContentTitleSetting(PR_FALSE)
00101 {
00102   // note if this fails, QI on nsIEmbeddingSiteWindow(2) will simply fail
00103   mSiteWindow2 = new nsSiteWindow2(this);
00104 }
00105 
00106 nsContentTreeOwner::~nsContentTreeOwner()
00107 {
00108   delete mSiteWindow2;
00109 }
00110 
00111 //*****************************************************************************
00112 // nsContentTreeOwner::nsISupports
00113 //*****************************************************************************   
00114 
00115 NS_IMPL_ADDREF(nsContentTreeOwner)
00116 NS_IMPL_RELEASE(nsContentTreeOwner)
00117 
00118 NS_INTERFACE_MAP_BEGIN(nsContentTreeOwner)
00119    NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDocShellTreeOwner)
00120    NS_INTERFACE_MAP_ENTRY(nsIDocShellTreeOwner)
00121    NS_INTERFACE_MAP_ENTRY(nsIDocShellTreeOwner_MOZILLA_1_8_BRANCH)
00122    NS_INTERFACE_MAP_ENTRY(nsIBaseWindow)
00123    NS_INTERFACE_MAP_ENTRY(nsIWebBrowserChrome)
00124    NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
00125    NS_INTERFACE_MAP_ENTRY(nsIWindowProvider)
00126    // XXXbz why not just implement those interfaces directly on this object?
00127    // Should file a followup and fix.
00128    NS_INTERFACE_MAP_ENTRY_AGGREGATED(nsIEmbeddingSiteWindow, mSiteWindow2)
00129    NS_INTERFACE_MAP_ENTRY_AGGREGATED(nsIEmbeddingSiteWindow2, mSiteWindow2)
00130 NS_INTERFACE_MAP_END
00131 
00132 //*****************************************************************************
00133 // nsContentTreeOwner::nsIInterfaceRequestor
00134 //*****************************************************************************   
00135 
00136 NS_IMETHODIMP nsContentTreeOwner::GetInterface(const nsIID& aIID, void** aSink)
00137 {
00138   NS_ENSURE_ARG_POINTER(aSink);
00139   *aSink = 0;
00140 
00141   if(aIID.Equals(NS_GET_IID(nsIPrompt)))
00142     return mXULWindow->GetInterface(aIID, aSink);
00143   if(aIID.Equals(NS_GET_IID(nsIAuthPrompt)))
00144     return mXULWindow->GetInterface(aIID, aSink);
00145   if (aIID.Equals(NS_GET_IID(nsIDocShellTreeItem))) {
00146     nsCOMPtr<nsIDocShell> shell;
00147     mXULWindow->GetDocShell(getter_AddRefs(shell));
00148     if (shell)
00149       return shell->QueryInterface(aIID, aSink);
00150     return NS_ERROR_FAILURE;
00151   }
00152 
00153   if (aIID.Equals(NS_GET_IID(nsIDOMWindow))) {
00154     nsCOMPtr<nsIDocShellTreeItem> shell;
00155     mXULWindow->GetPrimaryContentShell(getter_AddRefs(shell));
00156     if (shell) {
00157       nsCOMPtr<nsIInterfaceRequestor> thing(do_QueryInterface(shell));
00158       if (thing)
00159         return thing->GetInterface(aIID, aSink);
00160     }
00161     return NS_ERROR_FAILURE;
00162   }
00163 
00164   if (aIID.Equals(NS_GET_IID(nsIXULWindow)))
00165     return mXULWindow->QueryInterface(aIID, aSink);
00166 
00167   return QueryInterface(aIID, aSink);
00168 }
00169 
00170 //*****************************************************************************
00171 // nsContentTreeOwner::nsIDocShellTreeOwner
00172 //*****************************************************************************   
00173 
00174 NS_IMETHODIMP nsContentTreeOwner::FindItemWithName(const PRUnichar* aName,
00175    nsIDocShellTreeItem* aRequestor, nsIDocShellTreeItem* aOriginalRequestor,
00176    nsIDocShellTreeItem** aFoundItem)
00177 {
00178    NS_ENSURE_ARG_POINTER(aFoundItem);
00179 
00180    *aFoundItem = nsnull;
00181 
00182    PRBool fIs_Content = PR_FALSE;
00183 
00184    /* Special Cases */
00185    if (!aName || !*aName)
00186       return NS_OK;
00187 
00188    nsDependentString name(aName);
00189 
00190    if (name.LowerCaseEqualsLiteral("_blank"))
00191       return NS_OK;
00192    // _main is an IE target which should be case-insensitive but isn't
00193    // see bug 217886 for details
00194    if (name.LowerCaseEqualsLiteral("_content") ||
00195        name.EqualsLiteral("_main")) {
00196      // If we're being called with an aRequestor and it's targetable, just
00197      // return it -- _main and _content from inside targetable content shells
00198      // should just be that content shell.  Note that we don't have to worry
00199      // about the case when it's not targetable because it's primary -- that
00200      // will Just Work when we call GetPrimaryContentShell.
00201      if (aRequestor) {
00202        // This better be the root item!
00203 #ifdef DEBUG
00204        nsCOMPtr<nsIDocShellTreeItem> debugRoot;
00205        aRequestor->GetSameTypeRootTreeItem(getter_AddRefs(debugRoot));
00206        NS_ASSERTION(SameCOMIdentity(debugRoot, aRequestor),
00207                     "Bogus aRequestor");
00208 #endif
00209        PRInt32 count = mXULWindow->mTargetableShells.Count();
00210        for (PRInt32 i = 0; i < count; ++i) {
00211          nsCOMPtr<nsIDocShellTreeItem> item =
00212            do_QueryReferent(mXULWindow->mTargetableShells[i]);
00213          if (SameCOMIdentity(item, aRequestor)) {
00214            NS_ADDREF(*aFoundItem = aRequestor);
00215            return NS_OK;
00216          }
00217        }
00218      }
00219      mXULWindow->GetPrimaryContentShell(aFoundItem);
00220      if(*aFoundItem)
00221        return NS_OK;
00222      // Fall through and keep looking...
00223      fIs_Content = PR_TRUE;
00224    }
00225 
00226    nsCOMPtr<nsIWindowMediator> windowMediator(do_GetService(kWindowMediatorCID));
00227    NS_ENSURE_TRUE(windowMediator, NS_ERROR_FAILURE);
00228 
00229    nsCOMPtr<nsISimpleEnumerator> windowEnumerator;
00230    NS_ENSURE_SUCCESS(windowMediator->GetXULWindowEnumerator(nsnull, 
00231       getter_AddRefs(windowEnumerator)), NS_ERROR_FAILURE);
00232    
00233    PRBool more;
00234    
00235    windowEnumerator->HasMoreElements(&more);
00236    while(more) {
00237      nsCOMPtr<nsISupports> nextWindow = nsnull;
00238      windowEnumerator->GetNext(getter_AddRefs(nextWindow));
00239      nsCOMPtr<nsIXULWindow> xulWindow(do_QueryInterface(nextWindow));
00240      NS_ENSURE_TRUE(xulWindow, NS_ERROR_FAILURE);
00241 
00242      if (fIs_Content) {
00243        xulWindow->GetPrimaryContentShell(aFoundItem);
00244      } else {
00245        // Get all the targetable windows from xulWindow and search them
00246        nsRefPtr<nsXULWindow> win;
00247        xulWindow->QueryInterface(NS_GET_IID(nsXULWindow), getter_AddRefs(win));
00248        if (win) {
00249          PRInt32 count = win->mTargetableShells.Count();
00250          PRInt32 i;
00251          for (i = 0; i < count && !*aFoundItem; ++i) {
00252            nsCOMPtr<nsIDocShellTreeItem> shellAsTreeItem =
00253              do_QueryReferent(win->mTargetableShells[i]);
00254            if (shellAsTreeItem) {
00255              // Get the root tree item of same type, since roots are the only
00256              // things that call into the treeowner to look for named items.
00257              // XXXbz ideally we could guarantee that mTargetableShells only
00258              // contains roots, but the current treeowner apis don't allow
00259              // that... yet.
00260              nsCOMPtr<nsIDocShellTreeItem> root;
00261              shellAsTreeItem->GetSameTypeRootTreeItem(getter_AddRefs(root));
00262              NS_ASSERTION(root, "Must have root tree item of same type");
00263              shellAsTreeItem.swap(root);
00264              if (aRequestor != shellAsTreeItem) {
00265                // Do this so we can pass in the tree owner as the
00266                // requestor so the child knows not to call back up.
00267                nsCOMPtr<nsIDocShellTreeOwner> shellOwner;
00268                shellAsTreeItem->GetTreeOwner(getter_AddRefs(shellOwner));
00269                nsCOMPtr<nsISupports> shellOwnerSupports =
00270                  do_QueryInterface(shellOwner);
00271 
00272                shellAsTreeItem->FindItemWithName(aName, shellOwnerSupports,
00273                                                  aOriginalRequestor,
00274                                                  aFoundItem);
00275              }
00276            }
00277          }
00278        }
00279      }
00280      
00281      if (*aFoundItem)
00282        return NS_OK;
00283 
00284      windowEnumerator->HasMoreElements(&more);
00285    }
00286    return NS_OK;      
00287 }
00288 
00289 NS_IMETHODIMP nsContentTreeOwner::ContentShellAdded(nsIDocShellTreeItem* aContentShell,
00290    PRBool aPrimary, const PRUnichar* aID)
00291 {
00292    NS_ENSURE_STATE(mXULWindow);
00293    if (aID) {
00294      return mXULWindow->ContentShellAdded(aContentShell, aPrimary, PR_FALSE,
00295                                           nsDependentString(aID));
00296    }
00297 
00298    return mXULWindow->ContentShellAdded(aContentShell, aPrimary, PR_FALSE,
00299                                         EmptyString());
00300 }
00301 
00302 NS_IMETHODIMP nsContentTreeOwner::GetPrimaryContentShell(nsIDocShellTreeItem** aShell)
00303 {
00304    return mXULWindow->GetPrimaryContentShell(aShell);
00305 }
00306 
00307 NS_IMETHODIMP nsContentTreeOwner::SizeShellTo(nsIDocShellTreeItem* aShellItem,
00308    PRInt32 aCX, PRInt32 aCY)
00309 {
00310    return mXULWindow->SizeShellTo(aShellItem, aCX, aCY);
00311 }
00312 
00313 NS_IMETHODIMP
00314 nsContentTreeOwner::SetPersistence(PRBool aPersistPosition,
00315                                    PRBool aPersistSize,
00316                                    PRBool aPersistSizeMode)
00317 {
00318   nsCOMPtr<nsIDOMElement> docShellElement;
00319   mXULWindow->GetWindowDOMElement(getter_AddRefs(docShellElement));
00320   if(!docShellElement)
00321     return NS_ERROR_FAILURE;
00322 
00323   nsAutoString persistString;
00324   docShellElement->GetAttribute(NS_LITERAL_STRING("persist"), persistString);
00325 
00326   PRBool saveString = PR_FALSE;
00327   PRInt32 index;
00328 
00329   // Set X
00330   index = persistString.Find("screenX");
00331   if (!aPersistPosition && index >= 0) {
00332     persistString.Cut(index, 7);
00333     saveString = PR_TRUE;
00334   } else if (aPersistPosition && index < 0) {
00335     persistString.AppendLiteral(" screenX");
00336     saveString = PR_TRUE;
00337   }
00338   // Set Y
00339   index = persistString.Find("screenY");
00340   if (!aPersistPosition && index >= 0) {
00341     persistString.Cut(index, 7);
00342     saveString = PR_TRUE;
00343   } else if (aPersistPosition && index < 0) {
00344     persistString.AppendLiteral(" screenY");
00345     saveString = PR_TRUE;
00346   }
00347   // Set CX
00348   index = persistString.Find("width");
00349   if (!aPersistSize && index >= 0) {
00350     persistString.Cut(index, 5);
00351     saveString = PR_TRUE;
00352   } else if (aPersistSize && index < 0) {
00353     persistString.AppendLiteral(" width");
00354     saveString = PR_TRUE;
00355   }
00356   // Set CY
00357   index = persistString.Find("height");
00358   if (!aPersistSize && index >= 0) {
00359     persistString.Cut(index, 6);
00360     saveString = PR_TRUE;
00361   } else if (aPersistSize && index < 0) {
00362     persistString.AppendLiteral(" height");
00363     saveString = PR_TRUE;
00364   }
00365   // Set SizeMode
00366   index = persistString.Find("sizemode");
00367   if (!aPersistSizeMode && (index >= 0)) {
00368     persistString.Cut(index, 8);
00369     saveString = PR_TRUE;
00370   } else if (aPersistSizeMode && (index < 0)) {
00371     persistString.AppendLiteral(" sizemode");
00372     saveString = PR_TRUE;
00373   }
00374 
00375   if(saveString) 
00376     docShellElement->SetAttribute(NS_LITERAL_STRING("persist"), persistString);
00377 
00378   return NS_OK;
00379 }
00380 
00381 NS_IMETHODIMP
00382 nsContentTreeOwner::GetPersistence(PRBool* aPersistPosition,
00383                                    PRBool* aPersistSize,
00384                                    PRBool* aPersistSizeMode)
00385 {
00386   nsCOMPtr<nsIDOMElement> docShellElement;
00387   mXULWindow->GetWindowDOMElement(getter_AddRefs(docShellElement));
00388   if(!docShellElement) 
00389     return NS_ERROR_FAILURE;
00390 
00391   nsAutoString persistString;
00392   docShellElement->GetAttribute(NS_LITERAL_STRING("persist"), persistString);
00393 
00394   // data structure doesn't quite match the question, but it's close enough
00395   // for what we want (since this method is never actually called...)
00396   if (aPersistPosition)
00397     *aPersistPosition = persistString.Find("screenX") >= 0 || persistString.Find("screenY") >= 0 ? PR_TRUE : PR_FALSE;
00398   if (aPersistSize)
00399     *aPersistSize = persistString.Find("width") >= 0 || persistString.Find("height") >= 0 ? PR_TRUE : PR_FALSE;
00400   if (aPersistSizeMode)
00401     *aPersistSizeMode = persistString.Find("sizemode") >= 0 ? PR_TRUE : PR_FALSE;
00402 
00403   return NS_OK;
00404 }
00405 
00406 //*****************************************************************************
00407 // nsContentTreeOwner::nsIWebBrowserChrome
00408 //*****************************************************************************   
00409 
00410 NS_IMETHODIMP nsContentTreeOwner::SetStatus(PRUint32 aStatusType, const PRUnichar* aStatus)
00411 {
00412   // We only allow the status to be set from the primary content shell
00413   if (!mPrimary && aStatusType != STATUS_LINK)
00414     return NS_OK;
00415   
00416   nsCOMPtr<nsIXULBrowserWindow> xulBrowserWindow;
00417   mXULWindow->GetXULBrowserWindow(getter_AddRefs(xulBrowserWindow));
00418 
00419    if (xulBrowserWindow)
00420    {
00421      switch(aStatusType)
00422      {
00423      case STATUS_SCRIPT:
00424        xulBrowserWindow->SetJSStatus(aStatus);
00425        break;
00426      case STATUS_SCRIPT_DEFAULT:
00427        xulBrowserWindow->SetJSDefaultStatus(aStatus);
00428        break;
00429      case STATUS_LINK:
00430        xulBrowserWindow->SetOverLink(aStatus);
00431        break;
00432      }
00433    }
00434 
00435   return NS_OK;
00436 }
00437 
00438 NS_IMETHODIMP nsContentTreeOwner::SetWebBrowser(nsIWebBrowser* aWebBrowser)
00439 {
00440    NS_ERROR("Haven't Implemented this yet");
00441    return NS_ERROR_FAILURE;
00442 }
00443 
00444 NS_IMETHODIMP nsContentTreeOwner::GetWebBrowser(nsIWebBrowser** aWebBrowser)
00445 {
00446   // Unimplemented, and probably will remain so; xpfe windows have docshells,
00447   // not webbrowsers.
00448   NS_ENSURE_ARG_POINTER(aWebBrowser);
00449   *aWebBrowser = 0;
00450   return NS_ERROR_FAILURE;
00451 }
00452 
00453 NS_IMETHODIMP nsContentTreeOwner::SetChromeFlags(PRUint32 aChromeFlags)
00454 {
00455    return mXULWindow->SetChromeFlags(aChromeFlags);
00456 }
00457 
00458 NS_IMETHODIMP nsContentTreeOwner::GetChromeFlags(PRUint32* aChromeFlags)
00459 {
00460   return mXULWindow->GetChromeFlags(aChromeFlags);
00461 }
00462 
00463 NS_IMETHODIMP nsContentTreeOwner::DestroyBrowserWindow()
00464 {
00465    NS_ERROR("Haven't Implemented this yet");
00466    return NS_ERROR_FAILURE;
00467 }
00468 
00469 NS_IMETHODIMP nsContentTreeOwner::SizeBrowserTo(PRInt32 aCX, PRInt32 aCY)
00470 {
00471    NS_ERROR("Haven't Implemented this yet");
00472    return NS_ERROR_FAILURE;
00473 }
00474 
00475 NS_IMETHODIMP nsContentTreeOwner::ShowAsModal()
00476 {
00477    return mXULWindow->ShowModal();
00478 }
00479 
00480 NS_IMETHODIMP nsContentTreeOwner::IsWindowModal(PRBool *_retval)
00481 {
00482   *_retval = mXULWindow->mContinueModalLoop;
00483   return NS_OK;
00484 }
00485 
00486 NS_IMETHODIMP nsContentTreeOwner::ExitModalEventLoop(nsresult aStatus)
00487 {
00488    return mXULWindow->ExitModalLoop(aStatus);   
00489 }
00490 
00491 //*****************************************************************************
00492 // nsContentTreeOwner::nsIBaseWindow
00493 //*****************************************************************************   
00494 
00495 NS_IMETHODIMP nsContentTreeOwner::InitWindow(nativeWindow aParentNativeWindow,
00496    nsIWidget* parentWidget, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy)   
00497 {
00498    // Ignore wigdet parents for now.  Don't think those are a vaild thing to call.
00499    NS_ENSURE_SUCCESS(SetPositionAndSize(x, y, cx, cy, PR_FALSE), NS_ERROR_FAILURE);
00500 
00501    return NS_OK;
00502 }
00503 
00504 NS_IMETHODIMP nsContentTreeOwner::Create()
00505 {
00506    NS_ASSERTION(PR_FALSE, "You can't call this");
00507    return NS_ERROR_UNEXPECTED;
00508 }
00509 
00510 NS_IMETHODIMP nsContentTreeOwner::Destroy()
00511 {
00512    return mXULWindow->Destroy();
00513 }
00514 
00515 NS_IMETHODIMP nsContentTreeOwner::SetPosition(PRInt32 aX, PRInt32 aY)
00516 {
00517    return mXULWindow->SetPosition(aX, aY);
00518 }
00519 
00520 NS_IMETHODIMP nsContentTreeOwner::GetPosition(PRInt32* aX, PRInt32* aY)
00521 {
00522    return mXULWindow->GetPosition(aX, aY);
00523 }
00524 
00525 NS_IMETHODIMP nsContentTreeOwner::SetSize(PRInt32 aCX, PRInt32 aCY, PRBool aRepaint)
00526 {
00527    return mXULWindow->SetSize(aCX, aCY, aRepaint);
00528 }
00529 
00530 NS_IMETHODIMP nsContentTreeOwner::GetSize(PRInt32* aCX, PRInt32* aCY)
00531 {
00532    return mXULWindow->GetSize(aCX, aCY);
00533 }
00534 
00535 NS_IMETHODIMP nsContentTreeOwner::SetPositionAndSize(PRInt32 aX, PRInt32 aY,
00536    PRInt32 aCX, PRInt32 aCY, PRBool aRepaint)
00537 {
00538    return mXULWindow->SetPositionAndSize(aX, aY, aCX, aCY, aRepaint);
00539 }
00540 
00541 NS_IMETHODIMP nsContentTreeOwner::GetPositionAndSize(PRInt32* aX, PRInt32* aY,
00542    PRInt32* aCX, PRInt32* aCY)
00543 {
00544    return mXULWindow->GetPositionAndSize(aX, aY, aCX, aCY); 
00545 }
00546 
00547 NS_IMETHODIMP nsContentTreeOwner::Repaint(PRBool aForce)
00548 {
00549    return mXULWindow->Repaint(aForce);
00550 }
00551 
00552 NS_IMETHODIMP nsContentTreeOwner::GetParentWidget(nsIWidget** aParentWidget)
00553 {
00554    return mXULWindow->GetParentWidget(aParentWidget);
00555 }
00556 
00557 NS_IMETHODIMP nsContentTreeOwner::SetParentWidget(nsIWidget* aParentWidget)
00558 {
00559    NS_ASSERTION(PR_FALSE, "You can't call this");
00560    return NS_ERROR_NOT_IMPLEMENTED;
00561 }
00562 
00563 NS_IMETHODIMP nsContentTreeOwner::GetParentNativeWindow(nativeWindow* aParentNativeWindow)
00564 {
00565    return mXULWindow->GetParentNativeWindow(aParentNativeWindow);
00566 }
00567 
00568 NS_IMETHODIMP nsContentTreeOwner::SetParentNativeWindow(nativeWindow aParentNativeWindow)
00569 {
00570    NS_ASSERTION(PR_FALSE, "You can't call this");
00571    return NS_ERROR_NOT_IMPLEMENTED;
00572 }
00573 
00574 NS_IMETHODIMP nsContentTreeOwner::GetVisibility(PRBool* aVisibility)
00575 {
00576    return mXULWindow->GetVisibility(aVisibility);
00577 }
00578 
00579 NS_IMETHODIMP nsContentTreeOwner::SetVisibility(PRBool aVisibility)
00580 {
00581    return mXULWindow->SetVisibility(aVisibility);
00582 }
00583 
00584 NS_IMETHODIMP nsContentTreeOwner::GetEnabled(PRBool *aEnabled)
00585 {
00586    return mXULWindow->GetEnabled(aEnabled);
00587 }
00588 
00589 NS_IMETHODIMP nsContentTreeOwner::SetEnabled(PRBool aEnable)
00590 {
00591    return mXULWindow->SetEnabled(aEnable);
00592 }
00593 
00594 NS_IMETHODIMP nsContentTreeOwner::GetBlurSuppression(PRBool *aBlurSuppression)
00595 {
00596   return mXULWindow->GetBlurSuppression(aBlurSuppression);
00597 }
00598 
00599 NS_IMETHODIMP nsContentTreeOwner::SetBlurSuppression(PRBool aBlurSuppression)
00600 {
00601   return mXULWindow->SetBlurSuppression(aBlurSuppression);
00602 }
00603 
00604 NS_IMETHODIMP nsContentTreeOwner::GetMainWidget(nsIWidget** aMainWidget)
00605 {
00606    NS_ENSURE_ARG_POINTER(aMainWidget);
00607 
00608    *aMainWidget = mXULWindow->mWindow;
00609    NS_IF_ADDREF(*aMainWidget);
00610 
00611    return NS_OK;
00612 }
00613 
00614 NS_IMETHODIMP nsContentTreeOwner::SetFocus()
00615 {
00616    return mXULWindow->SetFocus();
00617 }
00618 
00619 NS_IMETHODIMP nsContentTreeOwner::GetTitle(PRUnichar** aTitle)
00620 {
00621    NS_ENSURE_ARG_POINTER(aTitle);
00622 
00623    return mXULWindow->GetTitle(aTitle);
00624 }
00625 
00626 NS_IMETHODIMP nsContentTreeOwner::SetTitle(const PRUnichar* aTitle)
00627 {
00628    // We only allow the title to be set from the primary content shell
00629   if(!mPrimary || !mContentTitleSetting)
00630     return NS_OK;
00631   
00632   nsAutoString   title;
00633   nsAutoString   docTitle(aTitle);
00634 
00635   if (docTitle.IsEmpty())
00636     docTitle.Assign(mTitleDefault);
00637   
00638   if (!docTitle.IsEmpty()) {
00639     if (!mTitlePreface.IsEmpty()) {
00640       // Title will be: "Preface: Doc Title - Mozilla"
00641       title.Assign(mTitlePreface);
00642       title.Append(docTitle);
00643     }
00644     else {
00645       // Title will be: "Doc Title - Mozilla"
00646       title = docTitle;
00647     }
00648   
00649     if (!mWindowTitleModifier.IsEmpty())
00650       title += mTitleSeparator + mWindowTitleModifier;
00651   }
00652   else
00653     title.Assign(mWindowTitleModifier); // Title will just be plain "Mozilla"
00654 
00655   //
00656   // if there is no location bar we modify the title to display at least
00657   // the scheme and host (if any) as an anti-spoofing measure.
00658   //
00659   nsCOMPtr<nsIDOMElement> docShellElement;
00660   mXULWindow->GetWindowDOMElement(getter_AddRefs(docShellElement));
00661 
00662   if (docShellElement) {
00663     nsAutoString chromeString;
00664     docShellElement->GetAttribute(NS_LITERAL_STRING("chromehidden"), chromeString);
00665     if (chromeString.Find(NS_LITERAL_STRING("location")) != kNotFound) {
00666       //
00667       // location bar is turned off, find the browser location
00668       //
00669       // use the document's nsPrincipal to find the true owner
00670       // in case of javascript: or data: documents
00671       //
00672       nsCOMPtr<nsIDocShellTreeItem> dsitem;
00673       GetPrimaryContentShell(getter_AddRefs(dsitem));
00674       nsCOMPtr<nsIDOMDocument> domdoc(do_GetInterface(dsitem));
00675       nsCOMPtr<nsIDocument> doc(do_QueryInterface(domdoc));
00676       if (doc) {
00677         nsCOMPtr<nsIURI> uri;
00678         nsIPrincipal* principal = doc->GetPrincipal();
00679         if (principal) {
00680           principal->GetURI(getter_AddRefs(uri));
00681           if (uri) {
00682             //
00683             // remove any user:pass information
00684             //
00685             nsCOMPtr<nsIURIFixup> fixup(do_GetService(NS_URIFIXUP_CONTRACTID));
00686             if (fixup) {
00687               nsCOMPtr<nsIURI> tmpuri;
00688               nsresult rv = fixup->CreateExposableURI(uri,getter_AddRefs(tmpuri));
00689               if (NS_SUCCEEDED(rv) && tmpuri) {
00690                 // (don't bother if there's no host)
00691                 nsCAutoString host;
00692                 nsCAutoString prepath;
00693                 tmpuri->GetHost(host);
00694                 tmpuri->GetPrePath(prepath);
00695                 if (!host.IsEmpty()) {
00696                   //
00697                   // We have a scheme/host, update the title
00698                   //
00699                   title.Insert(NS_ConvertUTF8toUTF16(prepath) +
00700                                mTitleSeparator, 0);
00701                 }
00702               }
00703             }
00704           }
00705         }
00706       }
00707     }
00708   }
00709 
00710   return mXULWindow->SetTitle(title.get());
00711 }
00712 
00713 //*****************************************************************************
00714 // nsContentTreeOwner: nsIWindowProvider
00715 //*****************************************************************************   
00716 NS_IMETHODIMP
00717 nsContentTreeOwner::ProvideWindow(nsIDOMWindow* aParent,
00718                                   PRUint32 aChromeFlags,
00719                                   PRBool aPositionSpecified,
00720                                   PRBool aSizeSpecified,
00721                                   nsIURI* aURI,
00722                                   const nsAString& aName,
00723                                   const nsACString& aFeatures,
00724                                   PRBool* aWindowIsNew,
00725                                   nsIDOMWindow** aReturn)
00726 {
00727   NS_ENSURE_ARG_POINTER(aParent);
00728   
00729   *aReturn = nsnull;
00730 
00731   if (!mXULWindow) {
00732     // Nothing to do here
00733     return NS_OK;
00734   }
00735 
00736 #ifdef DEBUG
00737   nsCOMPtr<nsIWebNavigation> parentNav = do_GetInterface(aParent);
00738   nsCOMPtr<nsIDocShellTreeOwner> parentOwner = do_GetInterface(parentNav);
00739   NS_ASSERTION(SameCOMIdentity(parentOwner,
00740                                NS_STATIC_CAST(nsIDocShellTreeOwner*, this)),
00741                "Parent from wrong docshell tree?");
00742 #endif
00743 
00744   // First check what our prefs say
00745   nsCOMPtr<nsIPrefService> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
00746   if (!prefs) {
00747     return NS_OK;
00748   }
00749 
00750   nsCOMPtr<nsIPrefBranch> branch;
00751   prefs->GetBranch("browser.link.", getter_AddRefs(branch));
00752   if (!branch) {
00753     return NS_OK;
00754   }
00755 
00756   // Where should we open this?
00757   PRInt32 containerPref;
00758   if (NS_FAILED(branch->GetIntPref("open_newwindow", &containerPref))) {
00759     return NS_OK;
00760   }
00761 
00762   if (containerPref != nsIBrowserDOMWindow::OPEN_NEWTAB &&
00763       containerPref != nsIBrowserDOMWindow::OPEN_CURRENTWINDOW) {
00764     // Just open a window normally
00765     return NS_OK;
00766   }
00767 
00768   /* Now check our restriction pref.  The restriction pref is a power-user's
00769      fine-tuning pref. values:     
00770      0: no restrictions - divert everything
00771      1: don't divert window.open at all
00772      2: don't divert window.open with features
00773   */
00774   PRInt32 restrictionPref;
00775   if (NS_FAILED(branch->GetIntPref("open_newwindow.restriction",
00776                                    &restrictionPref)) ||
00777       restrictionPref < 0 ||
00778       restrictionPref > 2) {
00779     restrictionPref = 2; // Sane default behavior
00780   }
00781 
00782   if (restrictionPref == 1) {
00783     return NS_OK;
00784   }
00785 
00786   if (restrictionPref == 2 &&
00787       // Only continue if there are no size/position features and no special
00788       // chrome flags.
00789       (aChromeFlags != nsIWebBrowserChrome::CHROME_ALL ||
00790        aPositionSpecified || aSizeSpecified)) {
00791     return NS_OK;
00792   }
00793 
00794   nsCOMPtr<nsIDOMWindowInternal> domWin;
00795   mXULWindow->GetWindowDOMWindow(getter_AddRefs(domWin));
00796   nsCOMPtr<nsIDOMChromeWindow> chromeWin = do_QueryInterface(domWin);
00797   if (!chromeWin) {
00798     // Really odd... but whatever
00799     NS_WARNING("nsXULWindow's DOMWindow is not a chrome window");
00800     return NS_OK;
00801   }
00802   
00803   nsCOMPtr<nsIBrowserDOMWindow> browserDOMWin;
00804   chromeWin->GetBrowserDOMWindow(getter_AddRefs(browserDOMWin));
00805   if (!browserDOMWin) {
00806     return NS_OK;
00807   }
00808 
00809   *aWindowIsNew = (containerPref != nsIBrowserDOMWindow::OPEN_CURRENTWINDOW);
00810   
00811   // Get a new rendering area from the browserDOMWin.  To make this
00812   // safe for cases when it'll try to return an existing window or
00813   // something, get it with a null URI.
00814   return browserDOMWin->OpenURI(nsnull, aParent, containerPref,
00815                                 nsIBrowserDOMWindow::OPEN_NEW, aReturn);
00816 }
00817 
00818 //*****************************************************************************
00819 // nsContentTreeOwner::nsIDocShellTreeOwner_MOZILLA_1_8_BRANCH
00820 //*****************************************************************************   
00821 NS_IMETHODIMP
00822 nsContentTreeOwner::ContentShellAdded2(nsIDocShellTreeItem* aContentShell,
00823                                        PRBool aPrimary, PRBool aTargetable,
00824                                        const nsAString& aID)
00825 {
00826   NS_ENSURE_STATE(mXULWindow);
00827   return mXULWindow->ContentShellAdded(aContentShell, aPrimary, aTargetable,
00828                                        aID);
00829 }
00830 
00831 NS_IMETHODIMP
00832 nsContentTreeOwner::ContentShellRemoved(nsIDocShellTreeItem* aContentShell)
00833 {
00834   NS_ENSURE_STATE(mXULWindow);
00835   return mXULWindow->ContentShellRemoved(aContentShell);
00836 }
00837 
00838 //*****************************************************************************
00839 // nsContentTreeOwner: Accessors
00840 //*****************************************************************************   
00841 
00842 void nsContentTreeOwner::XULWindow(nsXULWindow* aXULWindow)
00843 {
00844    mXULWindow = aXULWindow;
00845    if(mXULWindow && mPrimary)
00846       {
00847       // Get the window title modifiers
00848       nsCOMPtr<nsIDOMElement> docShellElement;
00849       mXULWindow->GetWindowDOMElement(getter_AddRefs(docShellElement));
00850 
00851       nsAutoString   contentTitleSetting;
00852 
00853       if(docShellElement)  
00854          {
00855          docShellElement->GetAttribute(NS_LITERAL_STRING("contenttitlesetting"), contentTitleSetting);
00856          if(contentTitleSetting.EqualsLiteral("true"))
00857             {
00858             mContentTitleSetting = PR_TRUE;
00859             docShellElement->GetAttribute(NS_LITERAL_STRING("titledefault"), mTitleDefault);
00860             docShellElement->GetAttribute(NS_LITERAL_STRING("titlemodifier"), mWindowTitleModifier);
00861             docShellElement->GetAttribute(NS_LITERAL_STRING("titlepreface"), mTitlePreface);
00862             
00863 #if defined(XP_MACOSX) && defined(MOZ_XUL_APP)
00864             // On OS X, treat the titlemodifier like it's the titledefault, and don't ever append
00865             // the separator + appname.
00866             if (mTitleDefault.IsEmpty()) {
00867                 docShellElement->SetAttribute(NS_LITERAL_STRING("titledefault"),
00868                                               mWindowTitleModifier);
00869                 docShellElement->RemoveAttribute(NS_LITERAL_STRING("titlemodifier"));
00870                 mTitleDefault = mWindowTitleModifier;
00871                 mWindowTitleModifier.Truncate();
00872             }
00873 #endif
00874             docShellElement->GetAttribute(NS_LITERAL_STRING("titlemenuseparator"), mTitleSeparator);
00875             }
00876          }
00877       else
00878          {
00879          NS_ERROR("This condition should never happen.  If it does, "
00880             "we just won't get a modifier, but it still shouldn't happen.");
00881          }
00882       }
00883 }
00884 
00885 nsXULWindow* nsContentTreeOwner::XULWindow()
00886 {
00887    return mXULWindow;
00888 }
00889 
00890 //*****************************************************************************
00891 //*** nsSiteWindow2 implementation
00892 //*****************************************************************************
00893 
00894 nsSiteWindow2::nsSiteWindow2(nsContentTreeOwner *aAggregator)
00895 {
00896   mAggregator = aAggregator;
00897 }
00898 
00899 nsSiteWindow2::~nsSiteWindow2()
00900 {
00901 }
00902 
00903 NS_IMPL_ADDREF_USING_AGGREGATOR(nsSiteWindow2, mAggregator)
00904 NS_IMPL_RELEASE_USING_AGGREGATOR(nsSiteWindow2, mAggregator)
00905 
00906 NS_INTERFACE_MAP_BEGIN(nsSiteWindow2)
00907   NS_INTERFACE_MAP_ENTRY(nsISupports)
00908   NS_INTERFACE_MAP_ENTRY(nsIEmbeddingSiteWindow)
00909   NS_INTERFACE_MAP_ENTRY(nsIEmbeddingSiteWindow2)
00910 NS_INTERFACE_MAP_END_AGGREGATED(mAggregator)
00911 
00912 NS_IMETHODIMP
00913 nsSiteWindow2::SetDimensions(PRUint32 aFlags,
00914                     PRInt32 aX, PRInt32 aY, PRInt32 aCX, PRInt32 aCY)
00915 {
00916   // XXX we're ignoring aFlags
00917   return mAggregator->SetPositionAndSize(aX, aY, aCX, aCY, PR_TRUE);
00918 }
00919 
00920 NS_IMETHODIMP
00921 nsSiteWindow2::GetDimensions(PRUint32 aFlags,
00922                     PRInt32 *aX, PRInt32 *aY, PRInt32 *aCX, PRInt32 *aCY)
00923 {
00924   // XXX we're ignoring aFlags
00925   return mAggregator->GetPositionAndSize(aX, aY, aCX, aCY);
00926 }
00927 
00928 NS_IMETHODIMP
00929 nsSiteWindow2::SetFocus(void)
00930 {
00931 #if 0
00932   /* This implementation focuses the main document and could make sense.
00933      However this method is actually being used from within
00934      nsGlobalWindow::Focus (providing a hook for MDI embedding apps)
00935      and it's better for our purposes to not pick a document and
00936      focus it, but allow nsGlobalWindow to carry on unhindered.
00937   */
00938   nsXULWindow *window = mAggregator->XULWindow();
00939   if (window) {
00940     nsCOMPtr<nsIDocShell> docshell;
00941     window->GetDocShell(getter_AddRefs(docshell));
00942     nsCOMPtr<nsIDOMWindowInternal> domWindow(do_GetInterface(docshell));
00943     if (domWindow)
00944       domWindow->Focus();
00945   }
00946 #endif
00947   return NS_OK;
00948 }
00949 
00950 /* this implementation focuses another window. if there isn't another
00951    window to focus, we do nothing. */
00952 NS_IMETHODIMP
00953 nsSiteWindow2::Blur(void)
00954 {
00955   nsCOMPtr<nsISimpleEnumerator> windowEnumerator;
00956   nsCOMPtr<nsIXULWindow>        xulWindow;
00957   PRBool                        more, foundUs;
00958   nsXULWindow                  *ourWindow = mAggregator->XULWindow();
00959 
00960   {
00961     nsCOMPtr<nsIWindowMediator> windowMediator(do_GetService(kWindowMediatorCID));
00962     if (windowMediator)
00963       windowMediator->GetZOrderXULWindowEnumerator(0, PR_TRUE,
00964                         getter_AddRefs(windowEnumerator));
00965   }
00966 
00967   if (!windowEnumerator)
00968     return NS_ERROR_FAILURE;
00969 
00970   // step through the top-level windows
00971   foundUs = PR_FALSE;
00972   windowEnumerator->HasMoreElements(&more);
00973   while (more) {
00974 
00975     nsCOMPtr<nsISupports>  nextWindow;
00976     nsCOMPtr<nsIXULWindow> nextXULWindow;
00977 
00978     windowEnumerator->GetNext(getter_AddRefs(nextWindow));
00979     nextXULWindow = do_QueryInterface(nextWindow);
00980 
00981     // got it!(?)
00982     if (foundUs) {
00983       xulWindow = nextXULWindow;
00984       break;
00985     }
00986 
00987     // remember the very first one, in case we have to wrap
00988     if (!xulWindow)
00989       xulWindow = nextXULWindow;
00990 
00991     // look for us
00992     if (nextXULWindow == ourWindow)
00993       foundUs = PR_TRUE;
00994 
00995     windowEnumerator->HasMoreElements(&more);
00996   }
00997 
00998   // change focus to the window we just found
00999   if (xulWindow) {
01000     nsCOMPtr<nsIDocShell> docshell;
01001     xulWindow->GetDocShell(getter_AddRefs(docshell));
01002     nsCOMPtr<nsIDOMWindowInternal> domWindow(do_GetInterface(docshell));
01003     if (domWindow)
01004       domWindow->Focus();
01005   }
01006   return NS_OK;
01007 }
01008 
01009 NS_IMETHODIMP
01010 nsSiteWindow2::GetVisibility(PRBool *aVisibility)
01011 {
01012   return mAggregator->GetVisibility(aVisibility);
01013 }
01014 
01015 NS_IMETHODIMP
01016 nsSiteWindow2::SetVisibility(PRBool aVisibility)
01017 {
01018   return mAggregator->SetVisibility(aVisibility);
01019 }
01020 
01021 NS_IMETHODIMP
01022 nsSiteWindow2::GetTitle(PRUnichar * *aTitle)
01023 {
01024   return mAggregator->GetTitle(aTitle);
01025 }
01026 
01027 NS_IMETHODIMP
01028 nsSiteWindow2::SetTitle(const PRUnichar * aTitle)
01029 {
01030   return mAggregator->SetTitle(aTitle);
01031 }
01032 
01033 NS_IMETHODIMP
01034 nsSiteWindow2::GetSiteWindow(void **aSiteWindow)
01035 {
01036   return mAggregator->GetParentNativeWindow(aSiteWindow);
01037 }
01038