dom/base/nsDOMWindowList.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 // Local Includes
michael@0 7 #include "nsDOMWindowList.h"
michael@0 8
michael@0 9 // Helper classes
michael@0 10 #include "nsCOMPtr.h"
michael@0 11
michael@0 12 // Interfaces needed
michael@0 13 #include "nsIDocument.h"
michael@0 14 #include "nsIDOMDocument.h"
michael@0 15 #include "nsIDOMWindow.h"
michael@0 16 #include "nsIDocShell.h"
michael@0 17 #include "nsIInterfaceRequestorUtils.h"
michael@0 18 #include "nsIScriptGlobalObject.h"
michael@0 19 #include "nsIWebNavigation.h"
michael@0 20
michael@0 21 nsDOMWindowList::nsDOMWindowList(nsIDocShell *aDocShell)
michael@0 22 {
michael@0 23 SetDocShell(aDocShell);
michael@0 24 }
michael@0 25
michael@0 26 nsDOMWindowList::~nsDOMWindowList()
michael@0 27 {
michael@0 28 }
michael@0 29
michael@0 30 NS_IMPL_ADDREF(nsDOMWindowList)
michael@0 31 NS_IMPL_RELEASE(nsDOMWindowList)
michael@0 32
michael@0 33 NS_INTERFACE_MAP_BEGIN(nsDOMWindowList)
michael@0 34 NS_INTERFACE_MAP_ENTRY(nsIDOMWindowCollection)
michael@0 35 NS_INTERFACE_MAP_ENTRY(nsISupports)
michael@0 36 NS_INTERFACE_MAP_END
michael@0 37
michael@0 38 NS_IMETHODIMP
michael@0 39 nsDOMWindowList::SetDocShell(nsIDocShell* aDocShell)
michael@0 40 {
michael@0 41 mDocShellNode = aDocShell; // Weak Reference
michael@0 42
michael@0 43 return NS_OK;
michael@0 44 }
michael@0 45
michael@0 46 void
michael@0 47 nsDOMWindowList::EnsureFresh()
michael@0 48 {
michael@0 49 nsCOMPtr<nsIWebNavigation> shellAsNav = do_QueryInterface(mDocShellNode);
michael@0 50
michael@0 51 if (shellAsNav) {
michael@0 52 nsCOMPtr<nsIDOMDocument> domdoc;
michael@0 53 shellAsNav->GetDocument(getter_AddRefs(domdoc));
michael@0 54
michael@0 55 nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc);
michael@0 56
michael@0 57 if (doc) {
michael@0 58 doc->FlushPendingNotifications(Flush_ContentAndNotify);
michael@0 59 }
michael@0 60 }
michael@0 61 }
michael@0 62
michael@0 63 uint32_t
michael@0 64 nsDOMWindowList::GetLength()
michael@0 65 {
michael@0 66 EnsureFresh();
michael@0 67
michael@0 68 NS_ENSURE_TRUE(mDocShellNode, 0);
michael@0 69
michael@0 70 int32_t length;
michael@0 71 nsresult rv = mDocShellNode->GetChildCount(&length);
michael@0 72 NS_ENSURE_SUCCESS(rv, 0);
michael@0 73
michael@0 74 return uint32_t(length);
michael@0 75 }
michael@0 76
michael@0 77 NS_IMETHODIMP
michael@0 78 nsDOMWindowList::GetLength(uint32_t* aLength)
michael@0 79 {
michael@0 80 *aLength = GetLength();
michael@0 81 return NS_OK;
michael@0 82 }
michael@0 83
michael@0 84 already_AddRefed<nsIDOMWindow>
michael@0 85 nsDOMWindowList::IndexedGetter(uint32_t aIndex, bool& aFound)
michael@0 86 {
michael@0 87 aFound = false;
michael@0 88
michael@0 89 nsCOMPtr<nsIDocShellTreeItem> item = GetDocShellTreeItemAt(aIndex);
michael@0 90 if (!item) {
michael@0 91 return nullptr;
michael@0 92 }
michael@0 93
michael@0 94 nsCOMPtr<nsIDOMWindow> window = do_GetInterface(item);
michael@0 95 MOZ_ASSERT(window);
michael@0 96
michael@0 97 aFound = true;
michael@0 98 return window.forget();
michael@0 99 }
michael@0 100
michael@0 101 NS_IMETHODIMP
michael@0 102 nsDOMWindowList::Item(uint32_t aIndex, nsIDOMWindow** aReturn)
michael@0 103 {
michael@0 104 bool found;
michael@0 105 nsCOMPtr<nsIDOMWindow> window = IndexedGetter(aIndex, found);
michael@0 106 window.forget(aReturn);
michael@0 107 return NS_OK;
michael@0 108 }
michael@0 109
michael@0 110 NS_IMETHODIMP
michael@0 111 nsDOMWindowList::NamedItem(const nsAString& aName, nsIDOMWindow** aReturn)
michael@0 112 {
michael@0 113 nsCOMPtr<nsIDocShellTreeItem> item;
michael@0 114
michael@0 115 *aReturn = nullptr;
michael@0 116
michael@0 117 EnsureFresh();
michael@0 118
michael@0 119 if (mDocShellNode) {
michael@0 120 mDocShellNode->FindChildWithName(PromiseFlatString(aName).get(),
michael@0 121 false, false, nullptr,
michael@0 122 nullptr, getter_AddRefs(item));
michael@0 123
michael@0 124 nsCOMPtr<nsIScriptGlobalObject> globalObject(do_GetInterface(item));
michael@0 125 if (globalObject) {
michael@0 126 CallQueryInterface(globalObject.get(), aReturn);
michael@0 127 }
michael@0 128 }
michael@0 129
michael@0 130 return NS_OK;
michael@0 131 }

mercurial