hal/WindowIdentifier.cpp
author Andrew McCreight <continuation@gmail.com>
Mon, 22 Apr 2019 16:34:51 +0000
changeset 470376 3073770e06f157040f4c64951b7e8425e1ad7bbe
parent 448947 6f3709b3878117466168c40affa7bca0b60cf75b
child 482216 9d2191d704555dd3e535ad9b5c451919af3c8bb1
permissions -rw-r--r--
Bug 1535403 - Take indirection into account for the CC optimizations for the outer window wrapper. r=peterv Most wrapper cached C++ objects are held alive by their wrapper. The cycle collector takes advantage of this in many classes and ignores the C++ object if the wrapper is marked black. However, this is not true for the outer window's wrapper. Instead, the outer window's wrapper keeps the inner window alive. The inner window usually keeps its outer window alive, but not after it has been unlinked. For reasons I do not yet understand, the outer window's wrapper can be kept alive after the inner window it is a proxy for is unlinked. This patch fixes the cycle collector optimization for the outer window by only applying it if the outer window still has a weak reference to the inner window, which it will until the inner no longer holds the outer alive. This in turn fixes, or at least helps fix, window leaks seen intermittently when the lifetime of outer windows and docshells are tied together. The code comment is based on a review comment by peterv. Differential Revision: https://phabricator.services.mozilla.com/D27981

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et ft=cpp : */
/* 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 "WindowIdentifier.h"

#include "mozilla/dom/ContentChild.h"
#include "nsPIDOMWindow.h"

namespace mozilla {
namespace hal {

WindowIdentifier::WindowIdentifier() : mWindow(nullptr), mIsEmpty(true) {}

WindowIdentifier::WindowIdentifier(nsPIDOMWindowInner* window)
    : mWindow(window), mIsEmpty(false) {
  mID.AppendElement(GetWindowID());
}

WindowIdentifier::WindowIdentifier(const InfallibleTArray<uint64_t>& id,
                                   nsPIDOMWindowInner* window)
    : mID(id), mWindow(window), mIsEmpty(false) {
  mID.AppendElement(GetWindowID());
}

WindowIdentifier::WindowIdentifier(const WindowIdentifier& other)
    : mID(other.mID), mWindow(other.mWindow), mIsEmpty(other.mIsEmpty) {}

const InfallibleTArray<uint64_t>& WindowIdentifier::AsArray() const {
  MOZ_ASSERT(!mIsEmpty);
  return mID;
}

bool WindowIdentifier::HasTraveledThroughIPC() const {
  MOZ_ASSERT(!mIsEmpty);
  return mID.Length() >= 2;
}

void WindowIdentifier::AppendProcessID() {
  MOZ_ASSERT(!mIsEmpty);
  mID.AppendElement(dom::ContentChild::GetSingleton()->GetID());
}

uint64_t WindowIdentifier::GetWindowID() const {
  MOZ_ASSERT(!mIsEmpty);
  return mWindow ? mWindow->WindowID() : UINT64_MAX;
}

nsPIDOMWindowInner* WindowIdentifier::GetWindow() const {
  MOZ_ASSERT(!mIsEmpty);
  return mWindow;
}

}  // namespace hal
}  // namespace mozilla