From 1f15cc4c1ecc730994f7ecda9bb54040f677f457 Mon Sep 17 00:00:00 2001 From: James Willcox Date: Wed, 25 Mar 2020 23:47:40 +0000 Subject: [PATCH] Bug 1620657 - Add `CanonicalBrowsingContext::GetParentProcessWidgetContaining()` r=nika,tnikkel Differential Revision: https://phabricator.services.mozilla.com/D67683 --HG-- extra : moz-landing-system : lando --- docshell/base/CanonicalBrowsingContext.cpp | 24 ++++++++++++++++++++++ docshell/base/CanonicalBrowsingContext.h | 2 ++ 2 files changed, 26 insertions(+) diff --git a/docshell/base/CanonicalBrowsingContext.cpp b/docshell/base/CanonicalBrowsingContext.cpp index 08f9c6cb11eb..9daeaf6c20d2 100644 --- a/docshell/base/CanonicalBrowsingContext.cpp +++ b/docshell/base/CanonicalBrowsingContext.cpp @@ -16,6 +16,8 @@ #include "mozilla/ipc/ProtocolUtils.h" #include "mozilla/NullPrincipal.h" +#include "nsGlobalWindowOuter.h" + using namespace mozilla::ipc; extern mozilla::LazyLogModule gAutoplayPermissionLog; @@ -122,6 +124,28 @@ WindowGlobalParent* CanonicalBrowsingContext::GetCurrentWindowGlobal() const { return static_cast(GetCurrentWindowContext()); } +already_AddRefed +CanonicalBrowsingContext::GetParentProcessWidgetContaining() { + // If our document is loaded in-process, such as chrome documents, get the + // widget directly from our outer window. Otherwise, try to get the widget + // from the toplevel content's browser's element. + nsCOMPtr widget; + if (nsGlobalWindowOuter* window = nsGlobalWindowOuter::Cast(GetDOMWindow())) { + widget = window->GetNearestWidget(); + } else if (Element* topEmbedder = Top()->GetEmbedderElement()) { + widget = nsContentUtils::WidgetForContent(topEmbedder); + if (!widget) { + widget = nsContentUtils::WidgetForDocument(topEmbedder->OwnerDoc()); + } + } + + if (widget) { + widget = widget->GetTopLevelWidget(); + } + + return widget.forget(); +} + already_AddRefed CanonicalBrowsingContext::GetEmbedderWindowGlobal() const { uint64_t windowId = GetEmbedderInnerWindowId(); diff --git a/docshell/base/CanonicalBrowsingContext.h b/docshell/base/CanonicalBrowsingContext.h index add350ed7653..cd1230655052 100644 --- a/docshell/base/CanonicalBrowsingContext.h +++ b/docshell/base/CanonicalBrowsingContext.h @@ -61,6 +61,8 @@ class CanonicalBrowsingContext final : public BrowsingContext { // The current active WindowGlobal. WindowGlobalParent* GetCurrentWindowGlobal() const; + already_AddRefed GetParentProcessWidgetContaining(); + already_AddRefed GetEmbedderWindowGlobal() const; nsISHistory* GetSessionHistory();