Bug 1544505 - Add helpful getter methods to JSWindowActorChild, r=mconley,jdai
authorNika Layzell <nika@thelayzells.com>
Wed, 17 Apr 2019 18:18:33 +0000
changeset 469903 c0aae5562089156f1288f4fbec8bd53de468a1b0
parent 469902 43d9bbc5d92cd6511481700068984476c6fdc238
child 469904 f0b845496dadb7c26522614a1a692d363a931dc0
push id35884
push userapavel@mozilla.com
push dateThu, 18 Apr 2019 21:35:00 +0000
treeherdermozilla-central@74dc09642c22 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmconley, jdai
bugs1544505
milestone68.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 1544505 - Add helpful getter methods to JSWindowActorChild, r=mconley,jdai Differential Revision: https://phabricator.services.mozilla.com/D27545
dom/chrome-webidl/JSWindowActor.webidl
dom/ipc/JSWindowActorChild.cpp
dom/ipc/JSWindowActorChild.h
dom/ipc/JSWindowActorParent.cpp
--- a/dom/chrome-webidl/JSWindowActor.webidl
+++ b/dom/chrome-webidl/JSWindowActor.webidl
@@ -18,16 +18,28 @@ interface JSWindowActor {
 interface JSWindowActorParent {
   readonly attribute WindowGlobalParent manager;
 };
 JSWindowActorParent implements JSWindowActor;
 
 [ChromeOnly, ChromeConstructor]
 interface JSWindowActorChild {
   readonly attribute WindowGlobalChild manager;
+
+  [Throws]
+  readonly attribute Document? document;
+
+  [Throws]
+  readonly attribute BrowsingContext? browsingContext;
+
+  // NOTE: As this returns a window proxy, it may not be currently referencing
+  // the document associated with this JSWindowActor. Generally prefer using
+  // `document`.
+  [Throws]
+  readonly attribute WindowProxy? contentWindow;
 };
 JSWindowActorChild implements JSWindowActor;
 
 // WebIDL callback interface version of the nsIObserver interface for use when
 // calling the observe method on JSWindowActors.
 //
 // NOTE: This isn't marked as ChromeOnly, as it has no interface object, and
 // thus cannot be conditionally exposed.
--- a/dom/ipc/JSWindowActorChild.cpp
+++ b/dom/ipc/JSWindowActorChild.cpp
@@ -4,16 +4,18 @@
  * 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/JSWindowActorBinding.h"
 #include "mozilla/dom/JSWindowActorChild.h"
 #include "mozilla/dom/WindowGlobalChild.h"
 #include "mozilla/dom/WindowGlobalParent.h"
 #include "mozilla/dom/MessageManagerBinding.h"
+#include "mozilla/dom/BrowsingContext.h"
+#include "nsGlobalWindowInner.h"
 
 namespace mozilla {
 namespace dom {
 
 JSObject* JSWindowActorChild::WrapObject(JSContext* aCx,
                                          JS::Handle<JSObject*> aGivenProto) {
   return JSWindowActorChild_Binding::Wrap(aCx, this, aGivenProto);
 }
@@ -61,17 +63,17 @@ class AsyncMessageToParent : public Runn
 }  // anonymous namespace
 
 void JSWindowActorChild::SendAsyncMessage(JSContext* aCx,
                                           const nsAString& aMessageName,
                                           JS::Handle<JS::Value> aObj,
                                           JS::Handle<JS::Value> aTransfers,
                                           ErrorResult& aRv) {
   // If we've closed our channel already, just raise an exception.
-  if (NS_WARN_IF(mManager->IsClosed())) {
+  if (NS_WARN_IF(!mManager || mManager->IsClosed())) {
     aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
     return;
   }
 
   // Serialize our object out to a StructuredCloneData.
   ipc::StructuredCloneData data;
   if (!aObj.isUndefined() && !nsFrameMessageManager::GetParamsForMessage(
                                  aCx, aObj, aTransfers, data)) {
@@ -103,16 +105,43 @@ void JSWindowActorChild::SendAsyncMessag
 
   if (!mManager->SendAsyncMessage(mName, PromiseFlatString(aMessageName),
                                   msgData)) {
     aRv.Throw(NS_ERROR_UNEXPECTED);
     return;
   }
 }
 
+Document* JSWindowActorChild::GetDocument(ErrorResult& aRv) {
+  if (!mManager) {
+    aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
+    return nullptr;
+  }
+
+  nsGlobalWindowInner* window = mManager->WindowGlobal();
+  return window ? window->GetDocument() : nullptr;
+}
+
+BrowsingContext* JSWindowActorChild::GetBrowsingContext(ErrorResult& aRv) {
+  if (!mManager) {
+    aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
+    return nullptr;
+  }
+
+  return mManager->BrowsingContext();
+}
+
+Nullable<WindowProxyHolder> JSWindowActorChild::GetContentWindow(
+    ErrorResult& aRv) {
+  if (BrowsingContext* bc = GetBrowsingContext(aRv)) {
+    return WindowProxyHolder(bc);
+  }
+  return nullptr;
+}
+
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(JSWindowActorChild)
   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
   NS_INTERFACE_MAP_ENTRY(nsISupports)
 NS_INTERFACE_MAP_END
 
 NS_IMPL_CYCLE_COLLECTING_ADDREF(JSWindowActorChild)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(JSWindowActorChild)
 
--- a/dom/ipc/JSWindowActorChild.h
+++ b/dom/ipc/JSWindowActorChild.h
@@ -12,17 +12,22 @@
 #include "mozilla/ErrorResult.h"
 #include "mozilla/dom/BindingDeclarations.h"
 #include "nsCycleCollectionParticipant.h"
 #include "nsWrapperCache.h"
 
 namespace mozilla {
 namespace dom {
 
+template <typename>
+struct Nullable;
+
+class Document;
 class WindowGlobalChild;
+class WindowProxyHolder;
 
 }  // namespace dom
 }  // namespace mozilla
 
 namespace mozilla {
 namespace dom {
 
 class JSWindowActorChild final : public nsISupports, public nsWrapperCache {
@@ -45,16 +50,20 @@ class JSWindowActorChild final : public 
   }
 
   WindowGlobalChild* Manager() const;
   void Init(const nsAString& aName, WindowGlobalChild* aManager);
   void SendAsyncMessage(JSContext* aCx, const nsAString& aMessageName,
                         JS::Handle<JS::Value> aObj,
                         JS::Handle<JS::Value> aTransfers, ErrorResult& aRv);
 
+  Document* GetDocument(ErrorResult& aRv);
+  BrowsingContext* GetBrowsingContext(ErrorResult& aRv);
+  Nullable<WindowProxyHolder> GetContentWindow(ErrorResult& aRv);
+
  private:
   nsString mName;
   RefPtr<WindowGlobalChild> mManager;
 };
 
 }  // namespace dom
 }  // namespace mozilla
 
--- a/dom/ipc/JSWindowActorParent.cpp
+++ b/dom/ipc/JSWindowActorParent.cpp
@@ -60,17 +60,17 @@ class AsyncMessageToChild : public Runna
 }  // anonymous namespace
 
 void JSWindowActorParent::SendAsyncMessage(JSContext* aCx,
                                            const nsAString& aMessageName,
                                            JS::Handle<JS::Value> aObj,
                                            JS::Handle<JS::Value> aTransfers,
                                            ErrorResult& aRv) {
   // If we've closed our channel already, just raise a warning.
-  if (NS_WARN_IF(mManager->IsClosed())) {
+  if (NS_WARN_IF(!mManager || mManager->IsClosed())) {
     aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
     return;
   }
 
   // Serialize our object out to a StructuredCloneData.
   ipc::StructuredCloneData data;
   if (!aObj.isUndefined() && !nsFrameMessageManager::GetParamsForMessage(
                                  aCx, aObj, aTransfers, data)) {