Merge inbound to m-c on a CLOSED TREE.
[gecko.git] / dom / base / nsDOMWindowList.cpp
blob9b342687e90b06dabfabf6ad15f65b9bd79f0230
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/. */
6 // Local Includes
7 #include "nsDOMWindowList.h"
9 // Helper classes
10 #include "nsCOMPtr.h"
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"
21 nsDOMWindowList::nsDOMWindowList(nsIDocShell *aDocShell)
23 SetDocShell(aDocShell);
26 nsDOMWindowList::~nsDOMWindowList()
30 NS_IMPL_ADDREF(nsDOMWindowList)
31 NS_IMPL_RELEASE(nsDOMWindowList)
33 NS_INTERFACE_MAP_BEGIN(nsDOMWindowList)
34 NS_INTERFACE_MAP_ENTRY(nsIDOMWindowCollection)
35 NS_INTERFACE_MAP_ENTRY(nsISupports)
36 NS_INTERFACE_MAP_END
38 NS_IMETHODIMP
39 nsDOMWindowList::SetDocShell(nsIDocShell* aDocShell)
41 nsCOMPtr<nsIDocShellTreeNode> docShellAsNode(do_QueryInterface(aDocShell));
42 mDocShellNode = docShellAsNode; // Weak Reference
44 return NS_OK;
47 void
48 nsDOMWindowList::EnsureFresh()
50 nsCOMPtr<nsIWebNavigation> shellAsNav = do_QueryInterface(mDocShellNode);
52 if (shellAsNav) {
53 nsCOMPtr<nsIDOMDocument> domdoc;
54 shellAsNav->GetDocument(getter_AddRefs(domdoc));
56 nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc);
58 if (doc) {
59 doc->FlushPendingNotifications(Flush_ContentAndNotify);
64 uint32_t
65 nsDOMWindowList::GetLength()
67 EnsureFresh();
69 NS_ENSURE_TRUE(mDocShellNode, 0);
71 int32_t length;
72 nsresult rv = mDocShellNode->GetChildCount(&length);
73 NS_ENSURE_SUCCESS(rv, 0);
75 return uint32_t(length);
78 NS_IMETHODIMP
79 nsDOMWindowList::GetLength(uint32_t* aLength)
81 *aLength = GetLength();
82 return NS_OK;
85 already_AddRefed<nsIDOMWindow>
86 nsDOMWindowList::IndexedGetter(uint32_t aIndex, bool& aFound)
88 aFound = false;
90 nsCOMPtr<nsIDocShellTreeItem> item = GetDocShellTreeItemAt(aIndex);
91 if (!item) {
92 return nullptr;
95 nsCOMPtr<nsIDOMWindow> window = do_GetInterface(item);
96 MOZ_ASSERT(window);
98 aFound = true;
99 return window.forget();
102 NS_IMETHODIMP
103 nsDOMWindowList::Item(uint32_t aIndex, nsIDOMWindow** aReturn)
105 bool found;
106 nsCOMPtr<nsIDOMWindow> window = IndexedGetter(aIndex, found);
107 window.forget(aReturn);
108 return NS_OK;
111 NS_IMETHODIMP
112 nsDOMWindowList::NamedItem(const nsAString& aName, nsIDOMWindow** aReturn)
114 nsCOMPtr<nsIDocShellTreeItem> item;
116 *aReturn = nullptr;
118 EnsureFresh();
120 if (mDocShellNode) {
121 mDocShellNode->FindChildWithName(PromiseFlatString(aName).get(),
122 false, false, nullptr,
123 nullptr, getter_AddRefs(item));
125 nsCOMPtr<nsIScriptGlobalObject> globalObject(do_GetInterface(item));
126 if (globalObject) {
127 CallQueryInterface(globalObject.get(), aReturn);
131 return NS_OK;