Back to index

lightning-sunbird  0.9+nobinonly
nsDOMWindowList.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   travis@netscape.com
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 // Local Includes
00040 #include "nsDOMWindowList.h"
00041 
00042 // Helper classes
00043 #include "nsCOMPtr.h"
00044 
00045 // Interfaces needed
00046 #include "nsIDocument.h"
00047 #include "nsIDOMDocument.h"
00048 #include "nsIDOMWindow.h"
00049 #include "nsIDocShell.h"
00050 #include "nsIDocShellTreeItem.h"
00051 #include "nsIDocShellTreeNode.h"
00052 #include "nsIInterfaceRequestor.h"
00053 #include "nsIInterfaceRequestorUtils.h"
00054 #include "nsIScriptGlobalObject.h"
00055 #include "nsIWebNavigation.h"
00056 
00057 nsDOMWindowList::nsDOMWindowList(nsIDocShell *aDocShell)
00058 {
00059   SetDocShell(aDocShell);
00060 }
00061 
00062 nsDOMWindowList::~nsDOMWindowList()
00063 {
00064 }
00065 
00066 NS_IMPL_ADDREF(nsDOMWindowList)
00067 NS_IMPL_RELEASE(nsDOMWindowList)
00068 
00069 NS_INTERFACE_MAP_BEGIN(nsDOMWindowList)
00070    NS_INTERFACE_MAP_ENTRY(nsIDOMWindowCollection)
00071    NS_INTERFACE_MAP_ENTRY(nsISupports)
00072 NS_INTERFACE_MAP_END
00073 
00074 NS_IMETHODIMP
00075 nsDOMWindowList::SetDocShell(nsIDocShell* aDocShell)
00076 {
00077   nsCOMPtr<nsIDocShellTreeNode> docShellAsNode(do_QueryInterface(aDocShell));
00078   mDocShellNode = docShellAsNode; // Weak Reference
00079 
00080   return NS_OK;
00081 }
00082 
00083 NS_IMETHODIMP 
00084 nsDOMWindowList::GetLength(PRUint32* aLength)
00085 {
00086   nsresult rv = NS_OK;
00087 
00088   *aLength = 0;
00089 
00090   nsCOMPtr<nsIWebNavigation> shellAsNav(do_QueryInterface(mDocShellNode));
00091 
00092   if (shellAsNav) {
00093     nsCOMPtr<nsIDOMDocument> domdoc;
00094     shellAsNav->GetDocument(getter_AddRefs(domdoc));
00095 
00096     nsCOMPtr<nsIDocument> doc(do_QueryInterface(domdoc));
00097 
00098     if (doc) {
00099       doc->FlushPendingNotifications(Flush_ContentAndNotify);
00100     }
00101   }
00102 
00103   // The above flush might cause mDocShellNode to be cleared, so we
00104   // need to check that it's still non-null here.
00105 
00106   if (mDocShellNode) {
00107     PRInt32 length;
00108     rv = mDocShellNode->GetChildCount(&length);
00109 
00110     *aLength = length;
00111   }
00112 
00113   return rv;
00114 }
00115 
00116 NS_IMETHODIMP 
00117 nsDOMWindowList::Item(PRUint32 aIndex, nsIDOMWindow** aReturn)
00118 {
00119   nsCOMPtr<nsIDocShellTreeItem> item;
00120 
00121   *aReturn = nsnull;
00122 
00123   nsCOMPtr<nsIWebNavigation> shellAsNav = do_QueryInterface(mDocShellNode);
00124 
00125   if (shellAsNav) {
00126     nsCOMPtr<nsIDOMDocument> domdoc;
00127     shellAsNav->GetDocument(getter_AddRefs(domdoc));
00128 
00129     nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc);
00130 
00131     if (doc) {
00132       doc->FlushPendingNotifications(Flush_ContentAndNotify);
00133     }
00134   }
00135 
00136   // The above flush might cause mDocShellNode to be cleared, so we
00137   // need to check that it's still non-null here.
00138 
00139   if (mDocShellNode) {
00140     mDocShellNode->GetChildAt(aIndex, getter_AddRefs(item));
00141 
00142     nsCOMPtr<nsIScriptGlobalObject> globalObject(do_GetInterface(item));
00143     NS_WARN_IF_FALSE(!item || (item && globalObject),
00144                      "Couldn't get to the globalObject");
00145 
00146     if (globalObject) {
00147       CallQueryInterface(globalObject, aReturn);
00148     }
00149   }
00150   return NS_OK;
00151 }
00152 
00153 NS_IMETHODIMP 
00154 nsDOMWindowList::NamedItem(const nsAString& aName, nsIDOMWindow** aReturn)
00155 {
00156   nsCOMPtr<nsIDocShellTreeItem> item;
00157 
00158   *aReturn = nsnull;
00159 
00160   nsCOMPtr<nsIWebNavigation> shellAsNav(do_QueryInterface(mDocShellNode));
00161 
00162   if (shellAsNav) {
00163     nsCOMPtr<nsIDOMDocument> domdoc;
00164     shellAsNav->GetDocument(getter_AddRefs(domdoc));
00165 
00166     nsCOMPtr<nsIDocument> doc(do_QueryInterface(domdoc));
00167 
00168     if (doc) {
00169       doc->FlushPendingNotifications(Flush_ContentAndNotify);
00170     }
00171   }
00172 
00173   // The above flush might cause mDocShellNode to be cleared, so we
00174   // need to check that it's still non-null here.
00175 
00176   if (mDocShellNode) {
00177     mDocShellNode->FindChildWithName(PromiseFlatString(aName).get(),
00178                                      PR_FALSE, PR_FALSE, nsnull,
00179                                      nsnull, getter_AddRefs(item));
00180 
00181     nsCOMPtr<nsIScriptGlobalObject> globalObject(do_GetInterface(item));
00182     NS_ASSERTION(globalObject, "Couldn't get to the globalObject");
00183     if (globalObject) {
00184       CallQueryInterface(globalObject.get(), aReturn);
00185     }
00186   }
00187 
00188   return NS_OK;
00189 }