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"
|
|
|
|
#include "mozilla/dom/BrowsingContextBinding.h"
|
|
|
|
#include "mozilla/dom/BindingUtils.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"
|
2019-01-30 19:07:21 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2020-04-24 05:31:55 +03:00
|
|
|
BrowsingContextGroup::BrowsingContextGroup() {
|
|
|
|
if (XRE_IsContentProcess()) {
|
|
|
|
ContentChild::GetSingleton()->HoldBrowsingContextGroup(this);
|
|
|
|
} else {
|
|
|
|
ContentParent::HoldBrowsingContextGroup(this);
|
2019-05-03 02:56:41 +03:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-01-30 19:07:21 +03:00
|
|
|
bool BrowsingContextGroup::Contains(BrowsingContext* aBrowsingContext) {
|
|
|
|
return aBrowsingContext->Group() == this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowsingContextGroup::Register(BrowsingContext* aBrowsingContext) {
|
2019-02-15 14:35:48 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aBrowsingContext);
|
2019-01-30 19:07:21 +03:00
|
|
|
mContexts.PutEntry(aBrowsingContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowsingContextGroup::Unregister(BrowsingContext* aBrowsingContext) {
|
2019-02-15 14:35:48 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aBrowsingContext);
|
2019-01-30 19:07:21 +03:00
|
|
|
mContexts.RemoveEntry(aBrowsingContext);
|
2019-07-17 03:31:38 +03:00
|
|
|
|
|
|
|
if (mContexts.IsEmpty()) {
|
|
|
|
// There are no browsing context still referencing this group. We can clear
|
|
|
|
// all subscribers.
|
|
|
|
UnsubscribeAllContentParents();
|
2020-04-07 18:17:47 +03:00
|
|
|
|
2020-04-24 05:31:55 +03:00
|
|
|
if (XRE_IsContentProcess()) {
|
|
|
|
ContentChild::GetSingleton()->ReleaseBrowsingContextGroup(this);
|
|
|
|
} else {
|
|
|
|
ContentParent::ReleaseBrowsingContextGroup(this);
|
|
|
|
}
|
2020-04-07 18:16:13 +03:00
|
|
|
// 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-07-17 03:31:38 +03:00
|
|
|
}
|
2019-01-30 19:07:21 +03:00
|
|
|
}
|
|
|
|
|
2019-02-15 14:35:48 +03:00
|
|
|
void BrowsingContextGroup::Subscribe(ContentParent* aOriginProcess) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aOriginProcess);
|
|
|
|
mSubscribers.PutEntry(aOriginProcess);
|
|
|
|
aOriginProcess->OnBrowsingContextGroupSubscribe(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowsingContextGroup::Unsubscribe(ContentParent* aOriginProcess) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aOriginProcess);
|
|
|
|
mSubscribers.RemoveEntry(aOriginProcess);
|
|
|
|
aOriginProcess->OnBrowsingContextGroupUnsubscribe(this);
|
2020-04-24 21:33:04 +03:00
|
|
|
|
|
|
|
// If this origin process still embeds any non-discarded BrowsingContexts in
|
|
|
|
// this BrowsingContextGroup, make sure to discard them, as this process is
|
|
|
|
// going away.
|
|
|
|
nsTArray<RefPtr<BrowsingContext>> toDiscard;
|
|
|
|
for (auto& context : mContexts) {
|
|
|
|
if (context.GetKey()->Canonical()->IsEmbeddedInProcess(
|
|
|
|
aOriginProcess->ChildID())) {
|
|
|
|
toDiscard.AppendElement(context.GetKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto& context : toDiscard) {
|
|
|
|
context->Detach(/* aFromIPC */ true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-03-14 21:50:45 +03:00
|
|
|
void BrowsingContextGroup::EnsureSubscribed(ContentParent* aProcess) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aProcess);
|
|
|
|
if (mSubscribers.Contains(aProcess)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Subscribe(aProcess);
|
|
|
|
|
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.
|
|
|
|
nsTArray<SyncedContextInitializer> inits(mContexts.Count() * 2);
|
|
|
|
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.
|
|
|
|
Unused << aProcess->SendRegisterBrowsingContextGroup(inits);
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
Unused << aProcess->SendSetupFocusedAndActive(focused, active);
|
|
|
|
}
|
2020-04-24 05:31:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 19:07:21 +03:00
|
|
|
BrowsingContextGroup::~BrowsingContextGroup() {
|
2019-07-17 03:31:38 +03:00
|
|
|
UnsubscribeAllContentParents();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowsingContextGroup::UnsubscribeAllContentParents() {
|
2019-02-15 14:35:48 +03:00
|
|
|
for (auto iter = mSubscribers.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsRefPtrHashKey<ContentParent>* entry = iter.Get();
|
|
|
|
entry->GetKey()->OnBrowsingContextGroupUnsubscribe(this);
|
2019-01-30 19:07:21 +03:00
|
|
|
}
|
2019-07-17 03:31:38 +03:00
|
|
|
mSubscribers.Clear();
|
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-04-24 05:31:55 +03:00
|
|
|
static StaticRefPtr<BrowsingContextGroup> sChromeGroup;
|
|
|
|
|
2019-10-24 17:53:07 +03:00
|
|
|
/* static */
|
|
|
|
BrowsingContextGroup* BrowsingContextGroup::GetChromeGroup() {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
|
|
|
|
if (!sChromeGroup && XRE_IsParentProcess()) {
|
2020-04-24 05:31:55 +03:00
|
|
|
sChromeGroup = new BrowsingContextGroup();
|
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());
|
|
|
|
for (auto iter = mDocGroups.ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
aDocGroups.AppendElement(iter.Data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DocGroup> BrowsingContextGroup::AddDocument(
|
|
|
|
const nsACString& aKey, Document* aDocument) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
RefPtr<DocGroup>& docGroup = mDocGroups.GetOrInsert(aKey);
|
|
|
|
if (!docGroup) {
|
|
|
|
docGroup = 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());
|
|
|
|
}
|
|
|
|
return MakeAndAddRef<BrowsingContextGroup>();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<BrowsingContextGroup> BrowsingContextGroup::Select(
|
|
|
|
uint64_t aParentId, uint64_t aOpenerId) {
|
|
|
|
RefPtr<WindowContext> parent = WindowContext::GetById(aParentId);
|
|
|
|
MOZ_RELEASE_ASSERT(parent || aParentId == 0);
|
|
|
|
|
|
|
|
RefPtr<BrowsingContext> opener = BrowsingContext::Get(aOpenerId);
|
|
|
|
MOZ_RELEASE_ASSERT(opener || aOpenerId == 0);
|
|
|
|
|
|
|
|
return Select(parent, opener);
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:35:48 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(BrowsingContextGroup, mContexts,
|
2020-04-24 21:33:04 +03:00
|
|
|
mToplevels, 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
|