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 "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
)
39 nsDOMWindowList::SetDocShell(nsIDocShell
* aDocShell
)
41 mDocShellNode
= aDocShell
; // Weak Reference
47 nsDOMWindowList::EnsureFresh()
49 nsCOMPtr
<nsIWebNavigation
> shellAsNav
= do_QueryInterface(mDocShellNode
);
52 nsCOMPtr
<nsIDOMDocument
> domdoc
;
53 shellAsNav
->GetDocument(getter_AddRefs(domdoc
));
55 nsCOMPtr
<nsIDocument
> doc
= do_QueryInterface(domdoc
);
58 doc
->FlushPendingNotifications(Flush_ContentAndNotify
);
64 nsDOMWindowList::GetLength()
68 NS_ENSURE_TRUE(mDocShellNode
, 0);
71 nsresult rv
= mDocShellNode
->GetChildCount(&length
);
72 NS_ENSURE_SUCCESS(rv
, 0);
74 return uint32_t(length
);
78 nsDOMWindowList::GetLength(uint32_t* aLength
)
80 *aLength
= GetLength();
84 already_AddRefed
<nsIDOMWindow
>
85 nsDOMWindowList::IndexedGetter(uint32_t aIndex
, bool& aFound
)
89 nsCOMPtr
<nsIDocShellTreeItem
> item
= GetDocShellTreeItemAt(aIndex
);
94 nsCOMPtr
<nsIDOMWindow
> window
= item
->GetWindow();
98 return window
.forget();
102 nsDOMWindowList::Item(uint32_t aIndex
, nsIDOMWindow
** aReturn
)
105 nsCOMPtr
<nsIDOMWindow
> window
= IndexedGetter(aIndex
, found
);
106 window
.forget(aReturn
);
111 nsDOMWindowList::NamedItem(const nsAString
& aName
, nsIDOMWindow
** aReturn
)
113 nsCOMPtr
<nsIDocShellTreeItem
> item
;
120 mDocShellNode
->FindChildWithName(PromiseFlatString(aName
).get(),
121 false, false, nullptr,
122 nullptr, getter_AddRefs(item
));
124 nsCOMPtr
<nsIScriptGlobalObject
> globalObject(do_GetInterface(item
));
126 CallQueryInterface(globalObject
.get(), aReturn
);