|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 // Local Includes |
|
7 #include "nsDOMWindowList.h" |
|
8 |
|
9 // Helper classes |
|
10 #include "nsCOMPtr.h" |
|
11 |
|
12 // Interfaces needed |
|
13 #include "nsIDocument.h" |
|
14 #include "nsIDOMDocument.h" |
|
15 #include "nsIDOMWindow.h" |
|
16 #include "nsIDocShell.h" |
|
17 #include "nsIInterfaceRequestorUtils.h" |
|
18 #include "nsIScriptGlobalObject.h" |
|
19 #include "nsIWebNavigation.h" |
|
20 |
|
21 nsDOMWindowList::nsDOMWindowList(nsIDocShell *aDocShell) |
|
22 { |
|
23 SetDocShell(aDocShell); |
|
24 } |
|
25 |
|
26 nsDOMWindowList::~nsDOMWindowList() |
|
27 { |
|
28 } |
|
29 |
|
30 NS_IMPL_ADDREF(nsDOMWindowList) |
|
31 NS_IMPL_RELEASE(nsDOMWindowList) |
|
32 |
|
33 NS_INTERFACE_MAP_BEGIN(nsDOMWindowList) |
|
34 NS_INTERFACE_MAP_ENTRY(nsIDOMWindowCollection) |
|
35 NS_INTERFACE_MAP_ENTRY(nsISupports) |
|
36 NS_INTERFACE_MAP_END |
|
37 |
|
38 NS_IMETHODIMP |
|
39 nsDOMWindowList::SetDocShell(nsIDocShell* aDocShell) |
|
40 { |
|
41 mDocShellNode = aDocShell; // Weak Reference |
|
42 |
|
43 return NS_OK; |
|
44 } |
|
45 |
|
46 void |
|
47 nsDOMWindowList::EnsureFresh() |
|
48 { |
|
49 nsCOMPtr<nsIWebNavigation> shellAsNav = do_QueryInterface(mDocShellNode); |
|
50 |
|
51 if (shellAsNav) { |
|
52 nsCOMPtr<nsIDOMDocument> domdoc; |
|
53 shellAsNav->GetDocument(getter_AddRefs(domdoc)); |
|
54 |
|
55 nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc); |
|
56 |
|
57 if (doc) { |
|
58 doc->FlushPendingNotifications(Flush_ContentAndNotify); |
|
59 } |
|
60 } |
|
61 } |
|
62 |
|
63 uint32_t |
|
64 nsDOMWindowList::GetLength() |
|
65 { |
|
66 EnsureFresh(); |
|
67 |
|
68 NS_ENSURE_TRUE(mDocShellNode, 0); |
|
69 |
|
70 int32_t length; |
|
71 nsresult rv = mDocShellNode->GetChildCount(&length); |
|
72 NS_ENSURE_SUCCESS(rv, 0); |
|
73 |
|
74 return uint32_t(length); |
|
75 } |
|
76 |
|
77 NS_IMETHODIMP |
|
78 nsDOMWindowList::GetLength(uint32_t* aLength) |
|
79 { |
|
80 *aLength = GetLength(); |
|
81 return NS_OK; |
|
82 } |
|
83 |
|
84 already_AddRefed<nsIDOMWindow> |
|
85 nsDOMWindowList::IndexedGetter(uint32_t aIndex, bool& aFound) |
|
86 { |
|
87 aFound = false; |
|
88 |
|
89 nsCOMPtr<nsIDocShellTreeItem> item = GetDocShellTreeItemAt(aIndex); |
|
90 if (!item) { |
|
91 return nullptr; |
|
92 } |
|
93 |
|
94 nsCOMPtr<nsIDOMWindow> window = do_GetInterface(item); |
|
95 MOZ_ASSERT(window); |
|
96 |
|
97 aFound = true; |
|
98 return window.forget(); |
|
99 } |
|
100 |
|
101 NS_IMETHODIMP |
|
102 nsDOMWindowList::Item(uint32_t aIndex, nsIDOMWindow** aReturn) |
|
103 { |
|
104 bool found; |
|
105 nsCOMPtr<nsIDOMWindow> window = IndexedGetter(aIndex, found); |
|
106 window.forget(aReturn); |
|
107 return NS_OK; |
|
108 } |
|
109 |
|
110 NS_IMETHODIMP |
|
111 nsDOMWindowList::NamedItem(const nsAString& aName, nsIDOMWindow** aReturn) |
|
112 { |
|
113 nsCOMPtr<nsIDocShellTreeItem> item; |
|
114 |
|
115 *aReturn = nullptr; |
|
116 |
|
117 EnsureFresh(); |
|
118 |
|
119 if (mDocShellNode) { |
|
120 mDocShellNode->FindChildWithName(PromiseFlatString(aName).get(), |
|
121 false, false, nullptr, |
|
122 nullptr, getter_AddRefs(item)); |
|
123 |
|
124 nsCOMPtr<nsIScriptGlobalObject> globalObject(do_GetInterface(item)); |
|
125 if (globalObject) { |
|
126 CallQueryInterface(globalObject.get(), aReturn); |
|
127 } |
|
128 } |
|
129 |
|
130 return NS_OK; |
|
131 } |