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"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2019-05-03 02:56:41 +03:00
|
|
|
BrowsingContextGroup::BrowsingContextGroup() {
|
|
|
|
if (XRE_IsContentProcess()) {
|
|
|
|
ContentChild::GetSingleton()->HoldBrowsingContextGroup(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
if (XRE_IsContentProcess()) {
|
|
|
|
ContentChild::GetSingleton()->ReleaseBrowsingContextGroup(this);
|
|
|
|
// We may have been deleted here as the ContentChild may have held the
|
|
|
|
// last references to `this`.
|
|
|
|
// Do not access any members at this point.
|
|
|
|
}
|
|
|
|
}
|
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);
|
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);
|
|
|
|
|
2019-03-14 21:51:09 +03:00
|
|
|
nsTArray<BrowsingContext::IPCInitializer> inits(mContexts.Count());
|
2019-05-08 10:24:31 +03:00
|
|
|
|
|
|
|
// First, perform a pre-order walk of our BrowsingContext objects from our
|
|
|
|
// toplevels. This should visit every active BrowsingContext.
|
|
|
|
for (auto& context : mToplevels) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!IsContextCached(context),
|
|
|
|
"cached contexts must have a parent");
|
|
|
|
|
2019-03-14 21:51:09 +03:00
|
|
|
context->PreOrderWalk([&](BrowsingContext* aContext) {
|
|
|
|
inits.AppendElement(aContext->GetIPCInitializer());
|
|
|
|
});
|
2019-03-14 21:50:45 +03:00
|
|
|
}
|
2019-03-14 21:51:09 +03:00
|
|
|
|
2019-05-08 10:24:31 +03:00
|
|
|
// Ensure that cached BrowsingContext objects are also visited, by visiting
|
|
|
|
// them after mToplevels.
|
|
|
|
for (auto iter = mCachedContexts.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
iter.Get()->GetKey()->PreOrderWalk([&](BrowsingContext* aContext) {
|
|
|
|
inits.AppendElement(aContext->GetIPCInitializer());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should have visited every browsing context.
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(inits.Length() == mContexts.Count(),
|
|
|
|
"Visited the wrong number of contexts!");
|
|
|
|
|
2019-03-14 21:51:09 +03:00
|
|
|
// Send all of our contexts to the target content process.
|
|
|
|
Unused << aProcess->SendRegisterBrowsingContextGroup(inits);
|
2019-03-14 21:50:45 +03:00
|
|
|
}
|
|
|
|
|
2019-04-29 10:01:50 +03:00
|
|
|
bool BrowsingContextGroup::IsContextCached(BrowsingContext* aContext) const {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aContext);
|
|
|
|
return mCachedContexts.Contains(aContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowsingContextGroup::CacheContext(BrowsingContext* aContext) {
|
|
|
|
mCachedContexts.PutEntry(aContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrowsingContextGroup::CacheContexts(
|
|
|
|
const BrowsingContext::Children& aContexts) {
|
|
|
|
for (BrowsingContext* child : aContexts) {
|
|
|
|
mCachedContexts.PutEntry(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BrowsingContextGroup::EvictCachedContext(BrowsingContext* aContext) {
|
|
|
|
return mCachedContexts.EnsureRemoved(aContext);
|
|
|
|
}
|
|
|
|
|
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-10-24 17:53:07 +03:00
|
|
|
static StaticRefPtr<BrowsingContextGroup> sChromeGroup;
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
BrowsingContextGroup* BrowsingContextGroup::GetChromeGroup() {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
|
|
|
|
if (!sChromeGroup && XRE_IsParentProcess()) {
|
|
|
|
sChromeGroup = new BrowsingContextGroup();
|
|
|
|
ClearOnShutdown(&sChromeGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sChromeGroup;
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:35:48 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(BrowsingContextGroup, mContexts,
|
2019-04-29 10:01:50 +03:00
|
|
|
mToplevels, mSubscribers, mCachedContexts)
|
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
|