Bug 1648583: Add GetCurrentTopByBrowserId() to get the BrowsingContext for a tab r=nika

Differential Revision: https://phabricator.services.mozilla.com/D81181
This commit is contained in:
Randell Jesup 2020-07-09 20:34:06 +00:00
Родитель 262fa6f330
Коммит 4238e3d8e8
2 изменённых файлов: 37 добавлений и 1 удалений

Просмотреть файл

@ -93,10 +93,30 @@ static LazyLogModule gBrowsingContextLog("BrowsingContext");
typedef nsDataHashtable<nsUint64HashKey, BrowsingContext*> BrowsingContextMap;
// All BrowsingContexts indexed by Id
static StaticAutoPtr<BrowsingContextMap> sBrowsingContexts;
// Top-level Content BrowsingContexts only, indexed by BrowserId instead of Id
static StaticAutoPtr<BrowsingContextMap> sCurrentTopByBrowserId;
static void UnregisterBrowserId(BrowsingContext* aBrowsingContext) {
if (!aBrowsingContext->IsTopContent() || !sCurrentTopByBrowserId) {
return;
}
// Avoids an extra lookup
auto browserIdEntry =
sCurrentTopByBrowserId->Lookup(aBrowsingContext->BrowserId());
if (browserIdEntry && browserIdEntry.Data() == aBrowsingContext) {
browserIdEntry.Remove();
}
}
static void Register(BrowsingContext* aBrowsingContext) {
sBrowsingContexts->Put(aBrowsingContext->Id(), aBrowsingContext);
if (aBrowsingContext->IsTopContent()) {
sCurrentTopByBrowserId->Put(aBrowsingContext->BrowserId(),
aBrowsingContext);
}
aBrowsingContext->Group()->Register(aBrowsingContext);
}
@ -134,7 +154,9 @@ WindowContext* BrowsingContext::GetTopWindowContext() {
void BrowsingContext::Init() {
if (!sBrowsingContexts) {
sBrowsingContexts = new BrowsingContextMap();
sCurrentTopByBrowserId = new BrowsingContextMap();
ClearOnShutdown(&sBrowsingContexts);
ClearOnShutdown(&sCurrentTopByBrowserId);
}
}
@ -146,6 +168,12 @@ already_AddRefed<BrowsingContext> BrowsingContext::Get(uint64_t aId) {
return do_AddRef(sBrowsingContexts->Get(aId));
}
/* static */
already_AddRefed<BrowsingContext> BrowsingContext::GetCurrentTopByBrowserId(
uint64_t aBrowserId) {
return do_AddRef(sCurrentTopByBrowserId->Get(aBrowserId));
}
/* static */
already_AddRefed<BrowsingContext> BrowsingContext::GetFromWindow(
WindowProxyHolder& aProxy) {
@ -657,6 +685,7 @@ void BrowsingContext::Detach(bool aFromIPC) {
}
mGroup->Unregister(this);
UnregisterBrowserId(this);
mIsDiscarded = true;
if (XRE_IsParentProcess()) {
@ -1061,6 +1090,7 @@ BrowsingContext::~BrowsingContext() {
if (sBrowsingContexts) {
sBrowsingContexts->Remove(Id());
}
UnregisterBrowserId(this);
}
nsISupports* BrowsingContext::GetParentObject() const {
@ -1483,6 +1513,7 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(BrowsingContext)
if (sBrowsingContexts) {
sBrowsingContexts->Remove(tmp->Id());
}
UnregisterBrowserId(tmp);
if (tmp->GetIsPopupSpam()) {
PopupBlocker::UnregisterOpenPopupSpam();

Просмотреть файл

@ -107,7 +107,8 @@ class WindowProxyHolder;
FIELD(FeaturePolicy, RefPtr<mozilla::dom::FeaturePolicy>) \
/* See nsSandboxFlags.h for the possible flags. */ \
FIELD(SandboxFlags, uint32_t) \
/* A unique identifier for the browser element that is hosting this \
/* A non-zero unique identifier for the browser element that is hosting \
* this \
* BrowsingContext tree. Every BrowsingContext in the element's tree will \
* return the same ID in all processes and it will remain stable \
* regardless of process changes. When a browser element's frameloader is \
@ -182,6 +183,9 @@ class BrowsingContext : public nsILoadContext, public nsWrapperCache {
static already_AddRefed<BrowsingContext> Get(GlobalObject&, uint64_t aId) {
return Get(aId);
}
// Look up the top-level BrowsingContext by BrowserID.
static already_AddRefed<BrowsingContext> GetCurrentTopByBrowserId(
uint64_t aBrowserId);
static already_AddRefed<BrowsingContext> GetFromWindow(
WindowProxyHolder& aProxy);
@ -316,6 +320,7 @@ class BrowsingContext : public nsILoadContext, public nsWrapperCache {
bool IsInSubtreeOf(BrowsingContext* aContext);
bool IsContentSubframe() const { return IsContent() && IsFrame(); }
// non-zero
uint64_t Id() const { return mBrowsingContextId; }
BrowsingContext* GetParent() const;