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/. */
7 #include "nsDOMWindowList.h"
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
)
40 nsDOMWindowList::SetDocShell(nsIDocShell
* aDocShell
)
42 nsCOMPtr
<nsIDocShellTreeNode
> docShellAsNode(do_QueryInterface(aDocShell
));
43 mDocShellNode
= docShellAsNode
; // Weak Reference
49 nsDOMWindowList::EnsureFresh()
51 nsCOMPtr
<nsIWebNavigation
> shellAsNav
= do_QueryInterface(mDocShellNode
);
54 nsCOMPtr
<nsIDOMDocument
> domdoc
;
55 shellAsNav
->GetDocument(getter_AddRefs(domdoc
));
57 nsCOMPtr
<nsIDocument
> doc
= do_QueryInterface(domdoc
);
60 doc
->FlushPendingNotifications(Flush_ContentAndNotify
);
66 nsDOMWindowList::GetLength()
70 NS_ENSURE_TRUE(mDocShellNode
, 0);
73 nsresult rv
= mDocShellNode
->GetChildCount(&length
);
74 NS_ENSURE_SUCCESS(rv
, 0);
76 return uint32_t(length
);
80 nsDOMWindowList::GetLength(uint32_t* aLength
)
82 *aLength
= GetLength();
86 already_AddRefed
<nsIDOMWindow
>
87 nsDOMWindowList::IndexedGetter(uint32_t aIndex
, bool& aFound
)
92 NS_ENSURE_TRUE(mDocShellNode
, nullptr);
94 nsCOMPtr
<nsIDocShellTreeItem
> item
;
95 mDocShellNode
->GetChildAt(aIndex
, getter_AddRefs(item
));
101 nsCOMPtr
<nsIDOMWindow
> window
= do_GetInterface(item
);
105 return window
.forget();
109 nsDOMWindowList::Item(uint32_t aIndex
, nsIDOMWindow
** aReturn
)
112 nsCOMPtr
<nsIDOMWindow
> window
= IndexedGetter(aIndex
, found
);
113 window
.forget(aReturn
);
118 nsDOMWindowList::NamedItem(const nsAString
& aName
, nsIDOMWindow
** aReturn
)
120 nsCOMPtr
<nsIDocShellTreeItem
> item
;
127 mDocShellNode
->FindChildWithName(PromiseFlatString(aName
).get(),
128 false, false, nullptr,
129 nullptr, getter_AddRefs(item
));
131 nsCOMPtr
<nsIScriptGlobalObject
> globalObject(do_GetInterface(item
));
133 CallQueryInterface(globalObject
.get(), aReturn
);