Bug 1620657 - Add `CanonicalBrowsingContext::GetParentProcessWidgetContaining()` r=nika,tnikkel
authorJames Willcox <snorp@snorp.net>
Wed, 25 Mar 2020 23:47:40 +0000
changeset 520647 75da647bf315b6bf2c2f33b4d7a02c8ed84bee93
parent 520646 3e4ba5c063d6aacae2a098f35d1df0f6baff8b54
child 520648 5afc157c37555d70287824ea0b490006df28da16
push id37254
push usernerli@mozilla.com
push dateFri, 27 Mar 2020 04:48:07 +0000
treeherdermozilla-central@2d758b42bd73 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnika, tnikkel
bugs1620657
milestone76.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1620657 - Add `CanonicalBrowsingContext::GetParentProcessWidgetContaining()` r=nika,tnikkel Differential Revision: https://phabricator.services.mozilla.com/D67683
docshell/base/CanonicalBrowsingContext.cpp
docshell/base/CanonicalBrowsingContext.h
--- a/docshell/base/CanonicalBrowsingContext.cpp
+++ b/docshell/base/CanonicalBrowsingContext.cpp
@@ -11,16 +11,18 @@
 #include "mozilla/dom/WindowGlobalParent.h"
 #include "mozilla/dom/ContentProcessManager.h"
 #include "mozilla/dom/MediaController.h"
 #include "mozilla/dom/MediaControlService.h"
 #include "mozilla/dom/PlaybackController.h"
 #include "mozilla/ipc/ProtocolUtils.h"
 #include "mozilla/NullPrincipal.h"
 
+#include "nsGlobalWindowOuter.h"
+
 using namespace mozilla::ipc;
 
 extern mozilla::LazyLogModule gAutoplayPermissionLog;
 
 #define AUTOPLAY_LOG(msg, ...) \
   MOZ_LOG(gAutoplayPermissionLog, LogLevel::Debug, (msg, ##__VA_ARGS__))
 
 namespace mozilla {
@@ -117,16 +119,38 @@ void CanonicalBrowsingContext::GetWindow
     aWindows.AppendElement(static_cast<WindowGlobalParent*>(window.get()));
   }
 }
 
 WindowGlobalParent* CanonicalBrowsingContext::GetCurrentWindowGlobal() const {
   return static_cast<WindowGlobalParent*>(GetCurrentWindowContext());
 }
 
+already_AddRefed<nsIWidget>
+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<nsIWidget> 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<WindowGlobalParent>
 CanonicalBrowsingContext::GetEmbedderWindowGlobal() const {
   uint64_t windowId = GetEmbedderInnerWindowId();
   if (windowId == 0) {
     return nullptr;
   }
 
   return WindowGlobalParent::GetByInnerWindowId(windowId);
--- a/docshell/base/CanonicalBrowsingContext.h
+++ b/docshell/base/CanonicalBrowsingContext.h
@@ -56,16 +56,18 @@ class CanonicalBrowsingContext final : p
   void SetInFlightProcessId(uint64_t aProcessId);
   uint64_t GetInFlightProcessId() const { return mInFlightProcessId; }
 
   void GetWindowGlobals(nsTArray<RefPtr<WindowGlobalParent>>& aWindows);
 
   // The current active WindowGlobal.
   WindowGlobalParent* GetCurrentWindowGlobal() const;
 
+  already_AddRefed<nsIWidget> GetParentProcessWidgetContaining();
+
   already_AddRefed<WindowGlobalParent> GetEmbedderWindowGlobal() const;
 
   nsISHistory* GetSessionHistory();
   void SetSessionHistory(nsISHistory* aSHistory) {
     mSessionHistory = aSHistory;
   }
   JSObject* WrapObject(JSContext* aCx,
                        JS::Handle<JSObject*> aGivenProto) override;