2019-04-26 23:29:16 +03:00
|
|
|
/* -*- 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 "mozilla/dom/ResizeObserverController.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/Document.h"
|
|
|
|
#include "mozilla/dom/ErrorEvent.h"
|
Bug 1656114 - Part 4: Accumulate page use counters in the parent process. r=emilio,nika
This changes the way we deal with page use counters so that we can
handle out of process iframes.
Currently, when a parent document is being destroyed, we poke into all
of the sub-documents to merge their use counters into the parent's page
use counters, which we then report via Telemetry. With Fission enabled,
the sub-documents may be out of process. We can't simply turn these
into async IPC calls, since the parent document will be destroyed
shortly, as might the content processes holding the sub-documents.
So instead, each document during its initialization identifies which
ancestor document it will contribute its page use counters to, and
stores its WindowContext id to identify that ancestor. A message is
sent to the parent process to notify it that page use counter data will
be sent at some later point. That later point is when the document
loses its window. It doesn't matter if the ancestor document has
already been destroyed at this point, since all we need is its
WindowContext id to uniquely identify it. Once the parent process has
received all of the use counters it expects to accumulate to a given
WindowContext Id, it reports them via Telemetry.
Reporting of document use counters remains unchanged and is done by each
document in their content process.
While we're here, we also:
* Limit use counters to be reported for a pre-defined set of document
URL schemes, rather than be based on the document principal.
* Add proper MOZ_LOG logging for use counters instead of printfs.
Differential Revision: https://phabricator.services.mozilla.com/D87188
2020-10-12 01:03:43 +03:00
|
|
|
#include "mozilla/dom/RootedDictionary.h"
|
2019-04-26 23:29:16 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2019-05-03 08:38:00 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2019-04-26 23:29:16 +03:00
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsRefreshDriver.h"
|
|
|
|
#include <limits>
|
|
|
|
|
2020-11-04 11:54:36 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2019-04-26 23:29:16 +03:00
|
|
|
|
|
|
|
void ResizeObserverNotificationHelper::WillRefresh(TimeStamp aTime) {
|
2019-04-30 22:44:55 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mOwner, "Should've de-registered on-time!");
|
2019-04-26 23:29:16 +03:00
|
|
|
mOwner->Notify();
|
2019-04-30 22:44:55 +03:00
|
|
|
// Note that mOwner may be null / dead here.
|
2019-04-26 23:29:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefreshDriver* ResizeObserverNotificationHelper::GetRefreshDriver() const {
|
|
|
|
PresShell* presShell = mOwner->GetPresShell();
|
|
|
|
if (MOZ_UNLIKELY(!presShell)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPresContext* presContext = presShell->GetPresContext();
|
|
|
|
if (MOZ_UNLIKELY(!presContext)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return presContext->RefreshDriver();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResizeObserverNotificationHelper::Register() {
|
|
|
|
if (mRegistered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefreshDriver* refreshDriver = GetRefreshDriver();
|
|
|
|
if (!refreshDriver) {
|
|
|
|
// We maybe navigating away from this page or currently in an iframe with
|
|
|
|
// display: none. Just abort the Register(), no need to do notification.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-25 05:36:29 +03:00
|
|
|
refreshDriver->AddRefreshObserver(this, FlushType::Display, "ResizeObserver");
|
2019-04-26 23:29:16 +03:00
|
|
|
mRegistered = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResizeObserverNotificationHelper::Unregister() {
|
|
|
|
if (!mRegistered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefreshDriver* refreshDriver = GetRefreshDriver();
|
2019-05-24 14:26:01 +03:00
|
|
|
MOZ_RELEASE_ASSERT(
|
|
|
|
refreshDriver,
|
|
|
|
"We should not leave a dangling reference to the observer around");
|
2019-04-26 23:29:16 +03:00
|
|
|
|
2019-05-01 18:00:42 +03:00
|
|
|
bool rv = refreshDriver->RemoveRefreshObserver(this, FlushType::Display);
|
2019-04-30 22:44:55 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(rv, "Should remove the observer successfully");
|
2019-05-03 08:38:00 +03:00
|
|
|
Unused << rv;
|
2019-04-26 23:29:16 +03:00
|
|
|
|
|
|
|
mRegistered = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResizeObserverNotificationHelper::~ResizeObserverNotificationHelper() {
|
2019-04-30 22:44:55 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!mRegistered, "How can we die when registered?");
|
|
|
|
MOZ_RELEASE_ASSERT(!mOwner, "Forgot to clear weak pointer?");
|
2019-04-26 23:29:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResizeObserverController::Traverse(
|
|
|
|
nsCycleCollectionTraversalCallback& aCb) {
|
|
|
|
ImplCycleCollectionTraverse(aCb, mResizeObservers, "mResizeObservers");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResizeObserverController::Unlink() { mResizeObservers.Clear(); }
|
|
|
|
|
2019-04-30 22:44:55 +03:00
|
|
|
void ResizeObserverController::ShellDetachedFromDocument() {
|
|
|
|
mResizeObserverNotificationHelper->Unregister();
|
|
|
|
}
|
|
|
|
|
2019-04-26 23:29:16 +03:00
|
|
|
void ResizeObserverController::Notify() {
|
|
|
|
if (mResizeObservers.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We may call BroadcastAllActiveObservations(), which might cause mDocument
|
|
|
|
// to be destroyed (and the ResizeObserverController with it).
|
|
|
|
// e.g. if mDocument is in an iframe, and the observer JS removes it from the
|
|
|
|
// parent document and we trigger an unlucky GC/CC (or maybe if the observer
|
|
|
|
// JS navigates to a different URL). Or the author uses elem.offsetTop API,
|
|
|
|
// which could flush style + layout and make the document destroyed if we're
|
|
|
|
// inside an iframe that has suddenly become |display:none| via the author
|
|
|
|
// doing something in their ResizeObserver callback.
|
|
|
|
//
|
|
|
|
// Therefore, we ref-count mDocument here to make sure it and its members
|
|
|
|
// (e.g. mResizeObserverController, which is `this` pointer) are still alive
|
|
|
|
// in the rest of this function because after it goes up, `this` is possible
|
|
|
|
// deleted.
|
|
|
|
RefPtr<Document> doc(mDocument);
|
|
|
|
|
|
|
|
uint32_t shallowestTargetDepth = 0;
|
|
|
|
|
|
|
|
GatherAllActiveObservations(shallowestTargetDepth);
|
|
|
|
|
|
|
|
while (HasAnyActiveObservations()) {
|
|
|
|
DebugOnly<uint32_t> oldShallowestTargetDepth = shallowestTargetDepth;
|
|
|
|
shallowestTargetDepth = BroadcastAllActiveObservations();
|
|
|
|
NS_ASSERTION(oldShallowestTargetDepth < shallowestTargetDepth,
|
|
|
|
"shallowestTargetDepth should be getting strictly deeper");
|
|
|
|
|
|
|
|
// Flush layout, so that any callback functions' style changes / resizes
|
|
|
|
// get a chance to take effect.
|
|
|
|
doc->FlushPendingNotifications(FlushType::Layout);
|
|
|
|
|
|
|
|
// To avoid infinite resize loop, we only gather all active observations
|
|
|
|
// that have the depth of observed target element more than current
|
|
|
|
// shallowestTargetDepth.
|
|
|
|
GatherAllActiveObservations(shallowestTargetDepth);
|
|
|
|
}
|
|
|
|
|
|
|
|
mResizeObserverNotificationHelper->Unregister();
|
|
|
|
|
|
|
|
if (HasAnySkippedObservations()) {
|
|
|
|
// Per spec, we deliver an error if the document has any skipped
|
|
|
|
// observations. Also, we re-register via ScheduleNotification().
|
|
|
|
RootedDictionary<ErrorEventInit> init(RootingCx());
|
|
|
|
|
|
|
|
init.mMessage.AssignLiteral(
|
|
|
|
"ResizeObserver loop completed with undelivered notifications.");
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
|
2019-06-25 23:07:03 +03:00
|
|
|
if (nsCOMPtr<nsPIDOMWindowInner> window = doc->GetInnerWindow()) {
|
2019-04-26 23:29:16 +03:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(window);
|
|
|
|
MOZ_ASSERT(sgo);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(sgo->HandleScriptError(init, &status))) {
|
|
|
|
status = nsEventStatus_eIgnore;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We don't fire error events at any global for non-window JS on the main
|
|
|
|
// thread.
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to deliver pending notifications in next cycle.
|
|
|
|
ScheduleNotification();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResizeObserverController::GatherAllActiveObservations(uint32_t aDepth) {
|
2019-11-19 19:45:15 +03:00
|
|
|
for (ResizeObserver* observer : mResizeObservers) {
|
|
|
|
observer->GatherActiveObservations(aDepth);
|
2019-04-26 23:29:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ResizeObserverController::BroadcastAllActiveObservations() {
|
|
|
|
uint32_t shallowestTargetDepth = std::numeric_limits<uint32_t>::max();
|
|
|
|
|
2019-11-19 19:45:15 +03:00
|
|
|
// Copy the observers as this invokes the callbacks and could register and
|
|
|
|
// unregister observers at will.
|
2020-05-05 13:08:02 +03:00
|
|
|
for (auto& observer : mResizeObservers.Clone()) {
|
2020-03-06 12:57:45 +03:00
|
|
|
// MOZ_KnownLive because 'observers' is guaranteed to keep it
|
|
|
|
// alive.
|
|
|
|
//
|
|
|
|
// This can go away once
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=1620312 is fixed.
|
|
|
|
uint32_t targetDepth =
|
|
|
|
MOZ_KnownLive(observer)->BroadcastActiveObservations();
|
2019-04-26 23:29:16 +03:00
|
|
|
if (targetDepth < shallowestTargetDepth) {
|
|
|
|
shallowestTargetDepth = targetDepth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return shallowestTargetDepth;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResizeObserverController::HasAnyActiveObservations() const {
|
2019-11-19 19:45:15 +03:00
|
|
|
for (auto& observer : mResizeObservers) {
|
|
|
|
if (observer->HasActiveObservations()) {
|
2019-04-26 23:29:16 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResizeObserverController::HasAnySkippedObservations() const {
|
2019-11-19 19:45:15 +03:00
|
|
|
for (auto& observer : mResizeObservers) {
|
|
|
|
if (observer->HasSkippedObservations()) {
|
2019-04-26 23:29:16 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResizeObserverController::ScheduleNotification() {
|
|
|
|
mResizeObserverNotificationHelper->Register();
|
|
|
|
}
|
|
|
|
|
|
|
|
ResizeObserverController::~ResizeObserverController() {
|
2019-04-30 22:44:55 +03:00
|
|
|
MOZ_RELEASE_ASSERT(
|
|
|
|
!mResizeObserverNotificationHelper->IsRegistered(),
|
|
|
|
"Nothing else should keep a reference to our helper when we go away");
|
|
|
|
mResizeObserverNotificationHelper->DetachFromOwner();
|
2019-04-26 23:29:16 +03:00
|
|
|
}
|
|
|
|
|
2019-11-20 17:04:47 +03:00
|
|
|
void ResizeObserverController::AddSizeOfIncludingThis(
|
|
|
|
nsWindowSizes& aSizes) const {
|
|
|
|
MallocSizeOf mallocSizeOf = aSizes.mState.mMallocSizeOf;
|
|
|
|
size_t size = mallocSizeOf(this);
|
|
|
|
size += mResizeObservers.ShallowSizeOfExcludingThis(mallocSizeOf);
|
|
|
|
// TODO(emilio): Measure the observers individually or something? They aren't
|
|
|
|
// really owned by us.
|
|
|
|
aSizes.mDOMResizeObserverControllerSize += size;
|
|
|
|
}
|
|
|
|
|
2020-11-04 11:54:36 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|