2019-01-30 19:07:21 +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/BrowsingContextGroup.h"
|
2020-05-04 17:53:34 +03:00
|
|
|
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2020-12-15 04:33:24 +03:00
|
|
|
#include "mozilla/InputTaskManager.h"
|
2019-01-30 19:07:21 +03:00
|
|
|
#include "mozilla/dom/BrowsingContextBinding.h"
|
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2020-05-04 17:53:34 +03:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2019-12-06 00:46:45 +03:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2020-04-07 18:17:47 +03:00
|
|
|
#include "mozilla/dom/DocGroup.h"
|
2019-12-01 00:13:54 +03:00
|
|
|
#include "mozilla/StaticPrefs_dom.h"
|
|
|
|
#include "mozilla/ThrottledEventQueue.h"
|
2020-02-18 16:30:04 +03:00
|
|
|
#include "nsFocusManager.h"
|
2021-03-09 14:39:36 +03:00
|
|
|
#include "nsTHashMap.h"
|
2019-01-30 19:07:21 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2020-04-24 21:33:09 +03:00
|
|
|
static StaticRefPtr<BrowsingContextGroup> sChromeGroup;
|
|
|
|
|
2021-03-09 14:39:36 +03:00
|
|
|
static StaticAutoPtr<nsTHashMap<uint64_t, RefPtr<BrowsingContextGroup>>>
|
2020-04-24 21:33:09 +03:00
|
|
|
sBrowsingContextGroups;
|
|
|
|
|
|
|
|
already_AddRefed<BrowsingContextGroup> BrowsingContextGroup::GetOrCreate(
|
|
|
|
uint64_t aId) {
|
|
|
|
if (!sBrowsingContextGroups) {
|
|
|
|
sBrowsingContextGroups =
|
2021-03-09 14:39:36 +03:00
|
|
|
new nsTHashMap<nsUint64HashKey, RefPtr<BrowsingContextGroup>>();
|
2020-04-24 21:33:09 +03:00
|
|
|
ClearOnShutdown(&sBrowsingContextGroups);
|
2019-05-03 02:56:41 +03:00
|
|
|
}
|
2020-04-07 18:17:47 +03:00
|
|
|
|
2021-03-09 14:39:36 +03:00
|
|
|
return do_AddRef(sBrowsingContextGroups->LookupOrInsertWith(
|
|
|
|
aId, [&aId] { return do_AddRef(new BrowsingContextGroup(aId)); }));
|
2020-04-24 21:33:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<BrowsingContextGroup> BrowsingContextGroup::Create() {
|
|
|
|
return GetOrCreate(nsContentUtils::GenerateBrowsingContextId());
|
|
|
|
}
|
|
|
|
|
2020-10-23 00:31:18 +03:00
|
|
|
BrowsingContextGroup::BrowsingContextGroup(uint64_t aId) : mId(aId) {
|
2020-04-07 18:17:47 +03:00
|
|
|
mTimerEventQueue = ThrottledEventQueue::Create(
|
|
|
|
GetMainThreadSerialEventTarget(), "BrowsingContextGroup timer queue");
|
|
|
|
|
|
|
|
mWorkerEventQueue = ThrottledEventQueue::Create(
|
|
|
|
GetMainThreadSerialEventTarget(), "BrowsingContextGroup worker queue");
|
2019-05-03 02:56:41 +03:00
|
|
|
}
|
|
|
|
|
2020-04-29 21:49:10 +03:00
|
|
|
void BrowsingContextGroup::Register(nsISupports* aContext) {
|
2021-03-18 22:24:48 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mDestroyed);
|
2020-04-29 21:49:10 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aContext);
|
|
|
|
mContexts.PutEntry(aContext);
|
2019-01-30 19:07:21 +03:00
|
|
|
}
|
|
|
|
|
2020-04-29 21:49:10 +03:00
|
|
|
void BrowsingContextGroup::Unregister(nsISupports* aContext) {
|
2021-03-18 22:24:48 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mDestroyed);
|
2020-04-29 21:49:10 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aContext);
|
|
|
|
mContexts.RemoveEntry(aContext);
|
2019-07-17 03:31:38 +03:00
|
|
|
|
2020-07-30 23:27:41 +03:00
|
|
|
MaybeDestroy();
|
2020-07-26 11:12:52 +03:00
|
|
|
}
|
2020-07-24 23:24:36 +03:00
|
|
|
|
2020-07-30 23:27:37 +03:00
|
|
|
void BrowsingContextGroup::EnsureHostProcess(ContentParent* aProcess) {
|
2021-03-18 22:24:48 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mDestroyed);
|
2020-07-30 23:27:37 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(this != sChromeGroup,
|
|
|
|
"cannot have content host for chrome group");
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aProcess->GetRemoteType() != PREALLOC_REMOTE_TYPE,
|
|
|
|
"cannot use preallocated process as host");
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!aProcess->GetRemoteType().IsEmpty(),
|
|
|
|
"host process must have remote type");
|
|
|
|
|
2021-02-09 21:19:38 +03:00
|
|
|
if (aProcess->IsDead() ||
|
|
|
|
mHosts.WithEntryHandle(aProcess->GetRemoteType(), [&](auto&& entry) {
|
|
|
|
if (entry) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(
|
|
|
|
entry.Data() == aProcess,
|
|
|
|
"There's already another host process for this remote type");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This process wasn't already marked as our host, so insert it, and
|
|
|
|
// begin subscribing, unless the process is still launching.
|
|
|
|
entry.Insert(do_AddRef(aProcess));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})) {
|
|
|
|
aProcess->AddBrowsingContextGroup(this);
|
2020-07-26 11:12:52 +03:00
|
|
|
}
|
2020-07-30 23:27:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BrowsingContextGroup::RemoveHostProcess(ContentParent* aProcess) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aProcess);
|
2020-09-16 07:43:54 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aProcess->GetRemoteType() != PREALLOC_REMOTE_TYPE);
|
2020-07-30 23:27:37 +03:00
|
|
|
auto entry = mHosts.Lookup(aProcess->GetRemoteType());
|
|
|
|
if (entry && entry.Data() == aProcess) {
|
|
|
|
entry.Remove();
|
2020-04-24 21:33:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CollectContextInitializers(
|
|
|
|
Span<RefPtr<BrowsingContext>> aContexts,
|
|
|
|
nsTArray<SyncedContextInitializer>& aInits) {
|
|
|
|
// The order that we record these initializers is important, as it will keep
|
|
|
|
// the order that children are attached to their parent in the newly connected
|
|
|
|
// content process consistent.
|
|
|
|
for (auto& context : aContexts) {
|
|
|
|
aInits.AppendElement(context->GetIPCInitializer());
|
|
|
|
for (auto& window : context->GetWindowContexts()) {
|
|
|
|
aInits.AppendElement(window->GetIPCInitializer());
|
|
|
|
CollectContextInitializers(window->Children(), aInits);
|
|
|
|
}
|
|
|
|
}
|
2019-01-30 19:07:21 +03:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:27:37 +03:00
|
|
|
void BrowsingContextGroup::Subscribe(ContentParent* aProcess) {
|
2021-03-18 22:24:48 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mDestroyed);
|
2020-07-30 23:27:37 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aProcess && !aProcess->IsLaunching());
|
2020-09-16 07:43:54 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aProcess->GetRemoteType() != PREALLOC_REMOTE_TYPE);
|
2020-07-30 23:27:37 +03:00
|
|
|
|
|
|
|
// Check if we're already subscribed to this process.
|
|
|
|
if (!mSubscribers.EnsureInserted(aProcess)) {
|
2019-03-14 21:50:45 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-30 23:27:37 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
2020-08-26 22:18:44 +03:00
|
|
|
// If the process is already marked as dead, we won't be the host, but may
|
|
|
|
// still need to subscribe to the process due to creating a popup while
|
|
|
|
// shutting down.
|
|
|
|
if (!aProcess->IsDead()) {
|
|
|
|
auto hostEntry = mHosts.Lookup(aProcess->GetRemoteType());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(hostEntry && hostEntry.Data() == aProcess,
|
|
|
|
"Cannot subscribe a non-host process");
|
|
|
|
}
|
2020-07-30 23:27:37 +03:00
|
|
|
#endif
|
2019-03-14 21:50:45 +03:00
|
|
|
|
2020-04-24 21:33:04 +03:00
|
|
|
// FIXME: This won't send non-discarded children of discarded BCs, but those
|
|
|
|
// BCs will be in the process of being destroyed anyway.
|
|
|
|
// FIXME: Prevent that situation from occuring.
|
2020-04-29 21:49:10 +03:00
|
|
|
nsTArray<SyncedContextInitializer> inits(mContexts.Count());
|
2020-04-24 21:33:04 +03:00
|
|
|
CollectContextInitializers(mToplevels, inits);
|
2020-04-24 05:31:55 +03:00
|
|
|
|
2020-04-24 21:33:04 +03:00
|
|
|
// Send all of our contexts to the target content process.
|
2020-04-24 21:33:09 +03:00
|
|
|
Unused << aProcess->SendRegisterBrowsingContextGroup(Id(), inits);
|
2020-04-24 21:33:04 +03:00
|
|
|
|
|
|
|
// If the focused or active BrowsingContexts belong in this group, tell the
|
|
|
|
// newly subscribed process.
|
|
|
|
if (nsFocusManager* fm = nsFocusManager::GetFocusManager()) {
|
|
|
|
BrowsingContext* focused = fm->GetFocusedBrowsingContextInChrome();
|
|
|
|
if (focused && focused->Group() != this) {
|
|
|
|
focused = nullptr;
|
2020-04-22 18:48:17 +03:00
|
|
|
}
|
2020-04-24 21:33:04 +03:00
|
|
|
BrowsingContext* active = fm->GetActiveBrowsingContextInChrome();
|
|
|
|
if (active && active->Group() != this) {
|
|
|
|
active = nullptr;
|
2020-04-24 00:52:47 +03:00
|
|
|
}
|
2020-04-24 05:31:55 +03:00
|
|
|
|
2020-04-24 21:33:04 +03:00
|
|
|
if (focused || active) {
|
2021-03-10 08:36:48 +03:00
|
|
|
Unused << aProcess->SendSetupFocusedAndActive(
|
2021-03-12 13:00:52 +03:00
|
|
|
focused, active, fm->GetActionIdForActiveBrowsingContextInChrome());
|
2020-04-24 21:33:04 +03:00
|
|
|
}
|
2020-04-24 05:31:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 23:27:37 +03:00
|
|
|
void BrowsingContextGroup::Unsubscribe(ContentParent* aProcess) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aProcess);
|
2020-09-16 07:43:54 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aProcess->GetRemoteType() != PREALLOC_REMOTE_TYPE);
|
2020-07-30 23:27:37 +03:00
|
|
|
mSubscribers.RemoveEntry(aProcess);
|
|
|
|
aProcess->RemoveBrowsingContextGroup(this);
|
|
|
|
|
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
auto hostEntry = mHosts.Lookup(aProcess->GetRemoteType());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!hostEntry || hostEntry.Data() != aProcess,
|
|
|
|
"Unsubscribing existing host entry");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentParent* BrowsingContextGroup::GetHostProcess(
|
|
|
|
const nsACString& aRemoteType) {
|
|
|
|
return mHosts.GetWeak(aRemoteType);
|
|
|
|
}
|
|
|
|
|
2020-10-23 00:31:18 +03:00
|
|
|
void BrowsingContextGroup::UpdateToplevelsSuspendedIfNeeded() {
|
|
|
|
if (!StaticPrefs::dom_suspend_inactive_enabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mToplevelsSuspended = ShouldSuspendAllTopLevelContexts();
|
2020-07-22 00:04:53 +03:00
|
|
|
for (const auto& context : mToplevels) {
|
|
|
|
nsPIDOMWindowOuter* outer = context->GetDOMWindow();
|
2020-10-23 00:31:18 +03:00
|
|
|
if (!outer) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> inner = outer->GetCurrentInnerWindow();
|
|
|
|
if (!inner) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (mToplevelsSuspended && !inner->GetWasSuspendedByGroup()) {
|
|
|
|
inner->Suspend();
|
|
|
|
inner->SetWasSuspendedByGroup(true);
|
|
|
|
} else if (!mToplevelsSuspended && inner->GetWasSuspendedByGroup()) {
|
|
|
|
inner->Resume();
|
|
|
|
inner->SetWasSuspendedByGroup(false);
|
2020-07-22 00:04:53 +03:00
|
|
|
}
|
|
|
|
}
|
2020-10-23 00:31:18 +03:00
|
|
|
}
|
2020-07-22 00:04:53 +03:00
|
|
|
|
2020-10-23 00:31:18 +03:00
|
|
|
bool BrowsingContextGroup::ShouldSuspendAllTopLevelContexts() const {
|
|
|
|
for (const auto& context : mToplevels) {
|
|
|
|
if (!context->InactiveForSuspend()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2020-07-22 00:04:53 +03:00
|
|
|
}
|
|
|
|
|
2020-07-30 23:27:41 +03:00
|
|
|
BrowsingContextGroup::~BrowsingContextGroup() { Destroy(); }
|
|
|
|
|
|
|
|
void BrowsingContextGroup::Destroy() {
|
2021-03-18 22:24:48 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
2020-07-30 23:27:41 +03:00
|
|
|
if (mDestroyed) {
|
2021-03-18 22:24:48 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mHosts.Count() == 0);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mSubscribers.Count() == 0);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT_IF(sBrowsingContextGroups,
|
|
|
|
!sBrowsingContextGroups->Contains(Id()) ||
|
|
|
|
*sBrowsingContextGroups->Lookup(Id()) != this);
|
2020-07-30 23:27:41 +03:00
|
|
|
}
|
|
|
|
mDestroyed = true;
|
|
|
|
#endif
|
2020-04-24 21:33:09 +03:00
|
|
|
|
2020-11-26 09:58:29 +03:00
|
|
|
// Make sure to call `RemoveBrowsingContextGroup` for every entry in both
|
|
|
|
// `mHosts` and `mSubscribers`. This will visit most entries twice, but
|
|
|
|
// `RemoveBrowsingContextGroup` is safe to call multiple times.
|
|
|
|
for (auto& entry : mHosts) {
|
|
|
|
entry.GetData()->RemoveBrowsingContextGroup(this);
|
|
|
|
}
|
2020-07-30 23:27:37 +03:00
|
|
|
for (auto& entry : mSubscribers) {
|
|
|
|
entry.GetKey()->RemoveBrowsingContextGroup(this);
|
2019-01-30 19:07:21 +03:00
|
|
|
}
|
2020-11-26 09:58:29 +03:00
|
|
|
mHosts.Clear();
|
2020-07-30 23:27:37 +03:00
|
|
|
mSubscribers.Clear();
|
2019-01-30 19:07:21 +03:00
|
|
|
|
2020-07-30 23:27:37 +03:00
|
|
|
if (sBrowsingContextGroups) {
|
|
|
|
sBrowsingContextGroups->Remove(Id());
|
2020-07-24 23:24:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 23:27:41 +03:00
|
|
|
void BrowsingContextGroup::AddKeepAlive() {
|
2021-03-18 22:24:48 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mDestroyed);
|
2020-07-30 23:27:41 +03:00
|
|
|
mKeepAliveCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowsingContextGroup::RemoveKeepAlive() {
|
2021-03-18 22:24:48 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mDestroyed);
|
2020-07-30 23:27:41 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mKeepAliveCount > 0);
|
|
|
|
mKeepAliveCount--;
|
|
|
|
|
|
|
|
MaybeDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowsingContextGroup::MaybeDestroy() {
|
|
|
|
if (mContexts.IsEmpty() && mKeepAliveCount == 0 && this != sChromeGroup) {
|
|
|
|
// There are no synced contexts still referencing this group. We can clear
|
|
|
|
// all subscribers, and destroy ourselves.
|
|
|
|
Destroy();
|
|
|
|
|
|
|
|
// We may have been deleted here as the ContentChild/Parent may
|
|
|
|
// have held the last references to `this`.
|
|
|
|
// Do not access any members at this point.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 19:07:21 +03:00
|
|
|
nsISupports* BrowsingContextGroup::GetParentObject() const {
|
|
|
|
return xpc::NativeGlobal(xpc::PrivilegedJunkScope());
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* BrowsingContextGroup::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
|
|
|
return BrowsingContextGroup_Binding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
2019-12-01 00:13:54 +03:00
|
|
|
nsresult BrowsingContextGroup::QueuePostMessageEvent(
|
|
|
|
already_AddRefed<nsIRunnable>&& aRunnable) {
|
|
|
|
if (StaticPrefs::dom_separate_event_queue_for_post_message_enabled()) {
|
|
|
|
if (!mPostMessageEventQueue) {
|
|
|
|
nsCOMPtr<nsISerialEventTarget> target = GetMainThreadSerialEventTarget();
|
|
|
|
mPostMessageEventQueue = ThrottledEventQueue::Create(
|
|
|
|
target, "PostMessage Queue",
|
|
|
|
nsIRunnablePriority::PRIORITY_DEFERRED_TIMERS);
|
|
|
|
nsresult rv = mPostMessageEventQueue->SetIsPaused(false);
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the queue is enabled
|
|
|
|
if (mPostMessageEventQueue->IsPaused()) {
|
|
|
|
nsresult rv = mPostMessageEventQueue->SetIsPaused(false);
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPostMessageEventQueue) {
|
|
|
|
mPostMessageEventQueue->Dispatch(std::move(aRunnable),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowsingContextGroup::FlushPostMessageEvents() {
|
|
|
|
if (StaticPrefs::dom_separate_event_queue_for_post_message_enabled()) {
|
|
|
|
if (mPostMessageEventQueue) {
|
|
|
|
nsresult rv = mPostMessageEventQueue->SetIsPaused(true);
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(rv);
|
|
|
|
nsCOMPtr<nsIRunnable> event;
|
|
|
|
while ((event = mPostMessageEventQueue->GetEvent())) {
|
|
|
|
NS_DispatchToMainThread(event.forget());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 04:33:24 +03:00
|
|
|
bool BrowsingContextGroup::HasActiveBC() {
|
|
|
|
for (auto& topLevelBC : Toplevels()) {
|
|
|
|
if (topLevelBC->IsActive()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowsingContextGroup::IncInputEventSuspensionLevel() {
|
2021-01-08 15:58:24 +03:00
|
|
|
MOZ_ASSERT(StaticPrefs::dom_input_events_canSuspendInBCG_enabled());
|
2020-12-15 04:33:24 +03:00
|
|
|
if (!mHasIncreasedInputTaskManagerSuspensionLevel && HasActiveBC()) {
|
|
|
|
IncInputTaskManagerSuspensionLevel();
|
|
|
|
}
|
|
|
|
++mInputEventSuspensionLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowsingContextGroup::DecInputEventSuspensionLevel() {
|
2021-01-08 15:58:24 +03:00
|
|
|
MOZ_ASSERT(StaticPrefs::dom_input_events_canSuspendInBCG_enabled());
|
2020-12-15 04:33:24 +03:00
|
|
|
--mInputEventSuspensionLevel;
|
|
|
|
if (!mInputEventSuspensionLevel &&
|
|
|
|
mHasIncreasedInputTaskManagerSuspensionLevel) {
|
|
|
|
DecInputTaskManagerSuspensionLevel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowsingContextGroup::DecInputTaskManagerSuspensionLevel() {
|
2021-01-08 15:58:24 +03:00
|
|
|
MOZ_ASSERT(StaticPrefs::dom_input_events_canSuspendInBCG_enabled());
|
2020-12-15 04:33:24 +03:00
|
|
|
MOZ_ASSERT(mHasIncreasedInputTaskManagerSuspensionLevel);
|
|
|
|
|
|
|
|
InputTaskManager::Get()->DecSuspensionLevel();
|
|
|
|
mHasIncreasedInputTaskManagerSuspensionLevel = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowsingContextGroup::IncInputTaskManagerSuspensionLevel() {
|
2021-01-08 15:58:24 +03:00
|
|
|
MOZ_ASSERT(StaticPrefs::dom_input_events_canSuspendInBCG_enabled());
|
2020-12-15 04:33:24 +03:00
|
|
|
MOZ_ASSERT(!mHasIncreasedInputTaskManagerSuspensionLevel);
|
|
|
|
MOZ_ASSERT(HasActiveBC());
|
|
|
|
|
|
|
|
InputTaskManager::Get()->IncSuspensionLevel();
|
|
|
|
mHasIncreasedInputTaskManagerSuspensionLevel = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowsingContextGroup::UpdateInputTaskManagerIfNeeded(bool aIsActive) {
|
2021-01-08 15:58:24 +03:00
|
|
|
MOZ_ASSERT(StaticPrefs::dom_input_events_canSuspendInBCG_enabled());
|
2020-12-15 04:33:24 +03:00
|
|
|
if (!aIsActive) {
|
|
|
|
if (mHasIncreasedInputTaskManagerSuspensionLevel) {
|
|
|
|
MOZ_ASSERT(mInputEventSuspensionLevel > 0);
|
|
|
|
if (!HasActiveBC()) {
|
|
|
|
DecInputTaskManagerSuspensionLevel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (mInputEventSuspensionLevel &&
|
|
|
|
!mHasIncreasedInputTaskManagerSuspensionLevel) {
|
|
|
|
IncInputTaskManagerSuspensionLevel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 17:53:07 +03:00
|
|
|
/* static */
|
|
|
|
BrowsingContextGroup* BrowsingContextGroup::GetChromeGroup() {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
|
|
|
|
if (!sChromeGroup && XRE_IsParentProcess()) {
|
2020-04-24 21:33:09 +03:00
|
|
|
sChromeGroup = BrowsingContextGroup::Create();
|
2019-10-24 17:53:07 +03:00
|
|
|
ClearOnShutdown(&sChromeGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sChromeGroup;
|
|
|
|
}
|
|
|
|
|
2020-04-07 18:16:13 +03:00
|
|
|
void BrowsingContextGroup::GetDocGroups(nsTArray<DocGroup*>& aDocGroups) {
|
2020-04-07 18:17:47 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2021-03-17 18:49:46 +03:00
|
|
|
for (const auto& entry : mDocGroups) {
|
|
|
|
aDocGroups.AppendElement(entry.GetData());
|
2020-04-07 18:17:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DocGroup> BrowsingContextGroup::AddDocument(
|
|
|
|
const nsACString& aKey, Document* aDocument) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2021-02-26 12:11:45 +03:00
|
|
|
RefPtr<DocGroup>& docGroup = mDocGroups.LookupOrInsertWith(
|
2021-02-15 19:37:52 +03:00
|
|
|
aKey, [&] { return DocGroup::Create(this, aKey); });
|
2020-04-07 18:16:13 +03:00
|
|
|
|
2020-04-07 18:17:47 +03:00
|
|
|
docGroup->AddDocument(aDocument);
|
|
|
|
return do_AddRef(docGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowsingContextGroup::RemoveDocument(const nsACString& aKey,
|
|
|
|
Document* aDocument) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
RefPtr<DocGroup> docGroup = aDocument->GetDocGroup();
|
|
|
|
// Removing the last document in DocGroup might decrement the
|
|
|
|
// DocGroup BrowsingContextGroup's refcount to 0.
|
|
|
|
RefPtr<BrowsingContextGroup> kungFuDeathGrip(this);
|
|
|
|
docGroup->RemoveDocument(aDocument);
|
|
|
|
|
|
|
|
if (docGroup->IsEmpty()) {
|
|
|
|
mDocGroups.Remove(aKey);
|
2020-04-07 18:16:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 21:33:04 +03:00
|
|
|
already_AddRefed<BrowsingContextGroup> BrowsingContextGroup::Select(
|
|
|
|
WindowContext* aParent, BrowsingContext* aOpener) {
|
|
|
|
if (aParent) {
|
|
|
|
return do_AddRef(aParent->Group());
|
|
|
|
}
|
|
|
|
if (aOpener) {
|
|
|
|
return do_AddRef(aOpener->Group());
|
|
|
|
}
|
2020-04-24 21:33:09 +03:00
|
|
|
return Create();
|
2020-04-24 21:33:04 +03:00
|
|
|
}
|
|
|
|
|
2020-04-24 21:33:09 +03:00
|
|
|
void BrowsingContextGroup::GetAllGroups(
|
|
|
|
nsTArray<RefPtr<BrowsingContextGroup>>& aGroups) {
|
|
|
|
aGroups.Clear();
|
|
|
|
if (!sBrowsingContextGroups) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-24 21:33:04 +03:00
|
|
|
|
2020-04-24 21:33:09 +03:00
|
|
|
aGroups.SetCapacity(sBrowsingContextGroups->Count());
|
|
|
|
for (auto& group : *sBrowsingContextGroups) {
|
|
|
|
aGroups.AppendElement(group.GetData());
|
|
|
|
}
|
2020-04-24 21:33:04 +03:00
|
|
|
}
|
|
|
|
|
2019-02-15 14:35:48 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(BrowsingContextGroup, mContexts,
|
2020-07-30 23:27:37 +03:00
|
|
|
mToplevels, mHosts, mSubscribers,
|
2020-04-07 18:17:47 +03:00
|
|
|
mTimerEventQueue, mWorkerEventQueue)
|
2019-01-30 19:07:21 +03:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(BrowsingContextGroup, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(BrowsingContextGroup, Release)
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|