Bug 891986 - Keep the source ArrayBuffer to a decodeAudioData call alive until the...
[gecko.git] / dom / base / nsDOMWindowList.cpp
blob50ad132dc93cf0577706d29feeaeaee828b1f586
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 "nsIInterfaceRequestor.h"
18 #include "nsIInterfaceRequestorUtils.h"
19 #include "nsIScriptGlobalObject.h"
20 #include "nsIWebNavigation.h"
22 nsDOMWindowList::nsDOMWindowList(nsIDocShell *aDocShell)
24 SetDocShell(aDocShell);
27 nsDOMWindowList::~nsDOMWindowList()
31 NS_IMPL_ADDREF(nsDOMWindowList)
32 NS_IMPL_RELEASE(nsDOMWindowList)
34 NS_INTERFACE_MAP_BEGIN(nsDOMWindowList)
35 NS_INTERFACE_MAP_ENTRY(nsIDOMWindowCollection)
36 NS_INTERFACE_MAP_ENTRY(nsISupports)
37 NS_INTERFACE_MAP_END
39 NS_IMETHODIMP
40 nsDOMWindowList::SetDocShell(nsIDocShell* aDocShell)
42 nsCOMPtr<nsIDocShellTreeNode> docShellAsNode(do_QueryInterface(aDocShell));
43 mDocShellNode = docShellAsNode; // Weak Reference
45 return NS_OK;
48 void
49 nsDOMWindowList::EnsureFresh()
51 nsCOMPtr<nsIWebNavigation> shellAsNav = do_QueryInterface(mDocShellNode);
53 if (shellAsNav) {
54 nsCOMPtr<nsIDOMDocument> domdoc;
55 shellAsNav->GetDocument(getter_AddRefs(domdoc));
57 nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc);
59 if (doc) {
60 doc->FlushPendingNotifications(Flush_ContentAndNotify);
65 uint32_t
66 nsDOMWindowList::GetLength()
68 EnsureFresh();
70 NS_ENSURE_TRUE(mDocShellNode, 0);
72 int32_t length;
73 nsresult rv = mDocShellNode->GetChildCount(&length);
74 NS_ENSURE_SUCCESS(rv, 0);
76 return uint32_t(length);
79 NS_IMETHODIMP
80 nsDOMWindowList::GetLength(uint32_t* aLength)
82 *aLength = GetLength();
83 return NS_OK;
86 already_AddRefed<nsIDOMWindow>
87 nsDOMWindowList::IndexedGetter(uint32_t aIndex, bool& aFound)
89 EnsureFresh();
91 aFound = false;
92 NS_ENSURE_TRUE(mDocShellNode, nullptr);
94 nsCOMPtr<nsIDocShellTreeItem> item;
95 mDocShellNode->GetChildAt(aIndex, getter_AddRefs(item));
97 if (!item) {
98 return nullptr;
101 nsCOMPtr<nsIDOMWindow> window = do_GetInterface(item);
102 MOZ_ASSERT(window);
104 aFound = true;
105 return window.forget();
108 NS_IMETHODIMP
109 nsDOMWindowList::Item(uint32_t aIndex, nsIDOMWindow** aReturn)
111 bool found;
112 nsCOMPtr<nsIDOMWindow> window = IndexedGetter(aIndex, found);
113 window.forget(aReturn);
114 return NS_OK;
117 NS_IMETHODIMP
118 nsDOMWindowList::NamedItem(const nsAString& aName, nsIDOMWindow** aReturn)
120 nsCOMPtr<nsIDocShellTreeItem> item;
122 *aReturn = nullptr;
124 EnsureFresh();
126 if (mDocShellNode) {
127 mDocShellNode->FindChildWithName(PromiseFlatString(aName).get(),
128 false, false, nullptr,
129 nullptr, getter_AddRefs(item));
131 nsCOMPtr<nsIScriptGlobalObject> globalObject(do_GetInterface(item));
132 if (globalObject) {
133 CallQueryInterface(globalObject.get(), aReturn);
137 return NS_OK;