michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // Local Includes michael@0: #include "nsDOMWindowList.h" michael@0: michael@0: // Helper classes michael@0: #include "nsCOMPtr.h" michael@0: michael@0: // Interfaces needed michael@0: #include "nsIDocument.h" michael@0: #include "nsIDOMDocument.h" michael@0: #include "nsIDOMWindow.h" michael@0: #include "nsIDocShell.h" michael@0: #include "nsIInterfaceRequestorUtils.h" michael@0: #include "nsIScriptGlobalObject.h" michael@0: #include "nsIWebNavigation.h" michael@0: michael@0: nsDOMWindowList::nsDOMWindowList(nsIDocShell *aDocShell) michael@0: { michael@0: SetDocShell(aDocShell); michael@0: } michael@0: michael@0: nsDOMWindowList::~nsDOMWindowList() michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_ADDREF(nsDOMWindowList) michael@0: NS_IMPL_RELEASE(nsDOMWindowList) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN(nsDOMWindowList) michael@0: NS_INTERFACE_MAP_ENTRY(nsIDOMWindowCollection) michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: NS_IMETHODIMP michael@0: nsDOMWindowList::SetDocShell(nsIDocShell* aDocShell) michael@0: { michael@0: mDocShellNode = aDocShell; // Weak Reference michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsDOMWindowList::EnsureFresh() michael@0: { michael@0: nsCOMPtr shellAsNav = do_QueryInterface(mDocShellNode); michael@0: michael@0: if (shellAsNav) { michael@0: nsCOMPtr domdoc; michael@0: shellAsNav->GetDocument(getter_AddRefs(domdoc)); michael@0: michael@0: nsCOMPtr doc = do_QueryInterface(domdoc); michael@0: michael@0: if (doc) { michael@0: doc->FlushPendingNotifications(Flush_ContentAndNotify); michael@0: } michael@0: } michael@0: } michael@0: michael@0: uint32_t michael@0: nsDOMWindowList::GetLength() michael@0: { michael@0: EnsureFresh(); michael@0: michael@0: NS_ENSURE_TRUE(mDocShellNode, 0); michael@0: michael@0: int32_t length; michael@0: nsresult rv = mDocShellNode->GetChildCount(&length); michael@0: NS_ENSURE_SUCCESS(rv, 0); michael@0: michael@0: return uint32_t(length); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsDOMWindowList::GetLength(uint32_t* aLength) michael@0: { michael@0: *aLength = GetLength(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsDOMWindowList::IndexedGetter(uint32_t aIndex, bool& aFound) michael@0: { michael@0: aFound = false; michael@0: michael@0: nsCOMPtr item = GetDocShellTreeItemAt(aIndex); michael@0: if (!item) { michael@0: return nullptr; michael@0: } michael@0: michael@0: nsCOMPtr window = do_GetInterface(item); michael@0: MOZ_ASSERT(window); michael@0: michael@0: aFound = true; michael@0: return window.forget(); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsDOMWindowList::Item(uint32_t aIndex, nsIDOMWindow** aReturn) michael@0: { michael@0: bool found; michael@0: nsCOMPtr window = IndexedGetter(aIndex, found); michael@0: window.forget(aReturn); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsDOMWindowList::NamedItem(const nsAString& aName, nsIDOMWindow** aReturn) michael@0: { michael@0: nsCOMPtr item; michael@0: michael@0: *aReturn = nullptr; michael@0: michael@0: EnsureFresh(); michael@0: michael@0: if (mDocShellNode) { michael@0: mDocShellNode->FindChildWithName(PromiseFlatString(aName).get(), michael@0: false, false, nullptr, michael@0: nullptr, getter_AddRefs(item)); michael@0: michael@0: nsCOMPtr globalObject(do_GetInterface(item)); michael@0: if (globalObject) { michael@0: CallQueryInterface(globalObject.get(), aReturn); michael@0: } michael@0: } michael@0: michael@0: return NS_OK; michael@0: }