dom/base/nsDOMWindowList.cpp
author Mike Hommey <mh+mozilla@glandium.org>
Thu, 17 Jan 2019 13:18:37 +0000
changeset 454358 c28522aaf3918c5f556c65432f8bf14a65bff65a
parent 452447 120fccb288c0839bde15c42987c247c39e0b6b50
child 480902 a15e5a44b7ed0a285c92839055efd83671b49552
permissions -rw-r--r--
Bug 1520149 - Also disable the machine outliner on Android when LTO is enabled. r=dmajor Differential Revision: https://phabricator.services.mozilla.com/D16790

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsDOMWindowList.h"

#include "FlushType.h"
#include "nsCOMPtr.h"
#include "mozilla/dom/Document.h"
#include "nsIDOMWindow.h"
#include "nsIDocShell.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIScriptGlobalObject.h"
#include "nsIWebNavigation.h"

using namespace mozilla;

nsDOMWindowList::nsDOMWindowList(nsIDocShell* aDocShell) {
  SetDocShell(aDocShell);
}

nsDOMWindowList::~nsDOMWindowList() {}

void nsDOMWindowList::SetDocShell(nsIDocShell* aDocShell) {
  mDocShellNode = aDocShell;  // Weak Reference
}

void nsDOMWindowList::EnsureFresh() {
  nsCOMPtr<nsIWebNavigation> shellAsNav = do_QueryInterface(mDocShellNode);

  if (shellAsNav) {
    nsCOMPtr<dom::Document> doc;
    shellAsNav->GetDocument(getter_AddRefs(doc));

    if (doc) {
      doc->FlushPendingNotifications(FlushType::ContentAndNotify);
    }
  }
}

uint32_t nsDOMWindowList::GetLength() {
  EnsureFresh();

  NS_ENSURE_TRUE(mDocShellNode, 0);

  int32_t length;
  nsresult rv = mDocShellNode->GetChildCount(&length);
  NS_ENSURE_SUCCESS(rv, 0);

  return uint32_t(length);
}

already_AddRefed<nsPIDOMWindowOuter> nsDOMWindowList::IndexedGetter(
    uint32_t aIndex) {
  nsCOMPtr<nsIDocShellTreeItem> item = GetDocShellTreeItemAt(aIndex);
  if (!item) {
    return nullptr;
  }

  nsCOMPtr<nsPIDOMWindowOuter> window = item->GetWindow();
  MOZ_ASSERT(window);

  return window.forget();
}

already_AddRefed<nsPIDOMWindowOuter> nsDOMWindowList::NamedItem(
    const nsAString& aName) {
  EnsureFresh();

  if (!mDocShellNode) {
    return nullptr;
  }

  nsCOMPtr<nsIDocShellTreeItem> item;
  mDocShellNode->FindChildWithName(aName, false, false, nullptr, nullptr,
                                   getter_AddRefs(item));

  nsCOMPtr<nsPIDOMWindowOuter> childWindow(do_GetInterface(item));
  return childWindow.forget();
}