dom/base/nsDOMWindowList.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/base/nsDOMWindowList.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,131 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +// Local Includes
    1.10 +#include "nsDOMWindowList.h"
    1.11 +
    1.12 +// Helper classes
    1.13 +#include "nsCOMPtr.h"
    1.14 +
    1.15 +// Interfaces needed
    1.16 +#include "nsIDocument.h"
    1.17 +#include "nsIDOMDocument.h"
    1.18 +#include "nsIDOMWindow.h"
    1.19 +#include "nsIDocShell.h"
    1.20 +#include "nsIInterfaceRequestorUtils.h"
    1.21 +#include "nsIScriptGlobalObject.h"
    1.22 +#include "nsIWebNavigation.h"
    1.23 +
    1.24 +nsDOMWindowList::nsDOMWindowList(nsIDocShell *aDocShell)
    1.25 +{
    1.26 +  SetDocShell(aDocShell);
    1.27 +}
    1.28 +
    1.29 +nsDOMWindowList::~nsDOMWindowList()
    1.30 +{
    1.31 +}
    1.32 +
    1.33 +NS_IMPL_ADDREF(nsDOMWindowList)
    1.34 +NS_IMPL_RELEASE(nsDOMWindowList)
    1.35 +
    1.36 +NS_INTERFACE_MAP_BEGIN(nsDOMWindowList)
    1.37 +   NS_INTERFACE_MAP_ENTRY(nsIDOMWindowCollection)
    1.38 +   NS_INTERFACE_MAP_ENTRY(nsISupports)
    1.39 +NS_INTERFACE_MAP_END
    1.40 +
    1.41 +NS_IMETHODIMP
    1.42 +nsDOMWindowList::SetDocShell(nsIDocShell* aDocShell)
    1.43 +{
    1.44 +  mDocShellNode = aDocShell; // Weak Reference
    1.45 +
    1.46 +  return NS_OK;
    1.47 +}
    1.48 +
    1.49 +void
    1.50 +nsDOMWindowList::EnsureFresh()
    1.51 +{
    1.52 +  nsCOMPtr<nsIWebNavigation> shellAsNav = do_QueryInterface(mDocShellNode);
    1.53 +
    1.54 +  if (shellAsNav) {
    1.55 +    nsCOMPtr<nsIDOMDocument> domdoc;
    1.56 +    shellAsNav->GetDocument(getter_AddRefs(domdoc));
    1.57 +
    1.58 +    nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc);
    1.59 +
    1.60 +    if (doc) {
    1.61 +      doc->FlushPendingNotifications(Flush_ContentAndNotify);
    1.62 +    }
    1.63 +  }
    1.64 +}
    1.65 +
    1.66 +uint32_t
    1.67 +nsDOMWindowList::GetLength()
    1.68 +{
    1.69 +  EnsureFresh();
    1.70 +
    1.71 +  NS_ENSURE_TRUE(mDocShellNode, 0);
    1.72 +
    1.73 +  int32_t length;
    1.74 +  nsresult rv = mDocShellNode->GetChildCount(&length);
    1.75 +  NS_ENSURE_SUCCESS(rv, 0);
    1.76 +
    1.77 +  return uint32_t(length);
    1.78 +}
    1.79 +
    1.80 +NS_IMETHODIMP 
    1.81 +nsDOMWindowList::GetLength(uint32_t* aLength)
    1.82 +{
    1.83 +  *aLength = GetLength();
    1.84 +  return NS_OK;
    1.85 +}
    1.86 +
    1.87 +already_AddRefed<nsIDOMWindow>
    1.88 +nsDOMWindowList::IndexedGetter(uint32_t aIndex, bool& aFound)
    1.89 +{
    1.90 +  aFound = false;
    1.91 +
    1.92 +  nsCOMPtr<nsIDocShellTreeItem> item = GetDocShellTreeItemAt(aIndex);
    1.93 +  if (!item) {
    1.94 +    return nullptr;
    1.95 +  }
    1.96 +
    1.97 +  nsCOMPtr<nsIDOMWindow> window = do_GetInterface(item);
    1.98 +  MOZ_ASSERT(window);
    1.99 +
   1.100 +  aFound = true;
   1.101 +  return window.forget();
   1.102 +}
   1.103 +
   1.104 +NS_IMETHODIMP 
   1.105 +nsDOMWindowList::Item(uint32_t aIndex, nsIDOMWindow** aReturn)
   1.106 +{
   1.107 +  bool found;
   1.108 +  nsCOMPtr<nsIDOMWindow> window = IndexedGetter(aIndex, found);
   1.109 +  window.forget(aReturn);
   1.110 +  return NS_OK;
   1.111 +}
   1.112 +
   1.113 +NS_IMETHODIMP 
   1.114 +nsDOMWindowList::NamedItem(const nsAString& aName, nsIDOMWindow** aReturn)
   1.115 +{
   1.116 +  nsCOMPtr<nsIDocShellTreeItem> item;
   1.117 +
   1.118 +  *aReturn = nullptr;
   1.119 +
   1.120 +  EnsureFresh();
   1.121 +
   1.122 +  if (mDocShellNode) {
   1.123 +    mDocShellNode->FindChildWithName(PromiseFlatString(aName).get(),
   1.124 +                                     false, false, nullptr,
   1.125 +                                     nullptr, getter_AddRefs(item));
   1.126 +
   1.127 +    nsCOMPtr<nsIScriptGlobalObject> globalObject(do_GetInterface(item));
   1.128 +    if (globalObject) {
   1.129 +      CallQueryInterface(globalObject.get(), aReturn);
   1.130 +    }
   1.131 +  }
   1.132 +
   1.133 +  return NS_OK;
   1.134 +}

mercurial