1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
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 : * travis@netscape.com
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either of the GNU General Public License Version 2 or later (the "GPL"),
27 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : // Local Includes
40 : #include "nsDOMWindowList.h"
41 :
42 : // Helper classes
43 : #include "nsCOMPtr.h"
44 :
45 : // Interfaces needed
46 : #include "nsIDocument.h"
47 : #include "nsIDOMDocument.h"
48 : #include "nsIDOMWindow.h"
49 : #include "nsIDocShell.h"
50 : #include "nsIDocShellTreeItem.h"
51 : #include "nsIDocShellTreeNode.h"
52 : #include "nsIInterfaceRequestor.h"
53 : #include "nsIInterfaceRequestorUtils.h"
54 : #include "nsIScriptGlobalObject.h"
55 : #include "nsIWebNavigation.h"
56 :
57 0 : nsDOMWindowList::nsDOMWindowList(nsIDocShell *aDocShell)
58 : {
59 0 : SetDocShell(aDocShell);
60 0 : }
61 :
62 0 : nsDOMWindowList::~nsDOMWindowList()
63 : {
64 0 : }
65 :
66 0 : NS_IMPL_ADDREF(nsDOMWindowList)
67 0 : NS_IMPL_RELEASE(nsDOMWindowList)
68 :
69 0 : NS_INTERFACE_MAP_BEGIN(nsDOMWindowList)
70 0 : NS_INTERFACE_MAP_ENTRY(nsIDOMWindowCollection)
71 0 : NS_INTERFACE_MAP_ENTRY(nsISupports)
72 0 : NS_INTERFACE_MAP_END
73 :
74 : NS_IMETHODIMP
75 0 : nsDOMWindowList::SetDocShell(nsIDocShell* aDocShell)
76 : {
77 0 : nsCOMPtr<nsIDocShellTreeNode> docShellAsNode(do_QueryInterface(aDocShell));
78 0 : mDocShellNode = docShellAsNode; // Weak Reference
79 :
80 0 : return NS_OK;
81 : }
82 :
83 : NS_IMETHODIMP
84 0 : nsDOMWindowList::GetLength(PRUint32* aLength)
85 : {
86 0 : nsresult rv = NS_OK;
87 :
88 0 : *aLength = 0;
89 :
90 0 : nsCOMPtr<nsIWebNavigation> shellAsNav(do_QueryInterface(mDocShellNode));
91 :
92 0 : if (shellAsNav) {
93 0 : nsCOMPtr<nsIDOMDocument> domdoc;
94 0 : shellAsNav->GetDocument(getter_AddRefs(domdoc));
95 :
96 0 : nsCOMPtr<nsIDocument> doc(do_QueryInterface(domdoc));
97 :
98 0 : if (doc) {
99 0 : doc->FlushPendingNotifications(Flush_ContentAndNotify);
100 : }
101 : }
102 :
103 : // The above flush might cause mDocShellNode to be cleared, so we
104 : // need to check that it's still non-null here.
105 :
106 0 : if (mDocShellNode) {
107 : PRInt32 length;
108 0 : rv = mDocShellNode->GetChildCount(&length);
109 :
110 0 : *aLength = length;
111 : }
112 :
113 0 : return rv;
114 : }
115 :
116 : NS_IMETHODIMP
117 0 : nsDOMWindowList::Item(PRUint32 aIndex, nsIDOMWindow** aReturn)
118 : {
119 0 : nsCOMPtr<nsIDocShellTreeItem> item;
120 :
121 0 : *aReturn = nsnull;
122 :
123 0 : nsCOMPtr<nsIWebNavigation> shellAsNav = do_QueryInterface(mDocShellNode);
124 :
125 0 : if (shellAsNav) {
126 0 : nsCOMPtr<nsIDOMDocument> domdoc;
127 0 : shellAsNav->GetDocument(getter_AddRefs(domdoc));
128 :
129 0 : nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc);
130 :
131 0 : if (doc) {
132 0 : doc->FlushPendingNotifications(Flush_ContentAndNotify);
133 : }
134 : }
135 :
136 : // The above flush might cause mDocShellNode to be cleared, so we
137 : // need to check that it's still non-null here.
138 :
139 0 : if (mDocShellNode) {
140 0 : mDocShellNode->GetChildAt(aIndex, getter_AddRefs(item));
141 :
142 0 : nsCOMPtr<nsIScriptGlobalObject> globalObject(do_GetInterface(item));
143 0 : NS_ASSERTION(!item || (item && globalObject),
144 : "Couldn't get to the globalObject");
145 :
146 0 : if (globalObject) {
147 0 : CallQueryInterface(globalObject, aReturn);
148 : }
149 : }
150 0 : return NS_OK;
151 : }
152 :
153 : NS_IMETHODIMP
154 0 : nsDOMWindowList::NamedItem(const nsAString& aName, nsIDOMWindow** aReturn)
155 : {
156 0 : nsCOMPtr<nsIDocShellTreeItem> item;
157 :
158 0 : *aReturn = nsnull;
159 :
160 0 : nsCOMPtr<nsIWebNavigation> shellAsNav(do_QueryInterface(mDocShellNode));
161 :
162 0 : if (shellAsNav) {
163 0 : nsCOMPtr<nsIDOMDocument> domdoc;
164 0 : shellAsNav->GetDocument(getter_AddRefs(domdoc));
165 :
166 0 : nsCOMPtr<nsIDocument> doc(do_QueryInterface(domdoc));
167 :
168 0 : if (doc) {
169 0 : doc->FlushPendingNotifications(Flush_ContentAndNotify);
170 : }
171 : }
172 :
173 : // The above flush might cause mDocShellNode to be cleared, so we
174 : // need to check that it's still non-null here.
175 :
176 0 : if (mDocShellNode) {
177 0 : mDocShellNode->FindChildWithName(PromiseFlatString(aName).get(),
178 : false, false, nsnull,
179 0 : nsnull, getter_AddRefs(item));
180 :
181 0 : nsCOMPtr<nsIScriptGlobalObject> globalObject(do_GetInterface(item));
182 0 : if (globalObject) {
183 0 : CallQueryInterface(globalObject.get(), aReturn);
184 : }
185 : }
186 :
187 0 : return NS_OK;
188 : }
|