Bug 1588420 - Add init() to SharePicker.idl, which provides a means to get to browser parent r=droeh
authorMarcos Cáceres <marcos@marcosc.com>
Tue, 15 Oct 2019 00:52:09 +0000
changeset 497770 7b19086f802cc163da1478fcfe9cf35133d95ca7
parent 497769 faa955e8bbeff366c7e9cc72624d716bbca51c6a
child 497771 3eb63c112f5ad197126daa785caf4e1770d02b35
child 497969 4e1f8ca63a68bd178bc4041ce55dcc18e8a887f0
push id98060
push usermcaceres@mozilla.com
push dateWed, 16 Oct 2019 00:55:30 +0000
treeherderautoland@7b19086f802c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdroeh
bugs1588420
milestone71.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 1588420 - Add init() to SharePicker.idl, which provides a means to get to browser parent r=droeh Differential Revision: https://phabricator.services.mozilla.com/D49215
browser/components/webshare/SharePicker.js
dom/ipc/WindowGlobalParent.cpp
widget/nsISharePicker.idl
--- a/browser/components/webshare/SharePicker.js
+++ b/browser/components/webshare/SharePicker.js
@@ -2,17 +2,19 @@
  * 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/. */
 
 const { XPCOMUtils } = ChromeUtils.import(
   "resource://gre/modules/XPCOMUtils.jsm"
 );
 
 class SharePicker {
-  constructor() {}
+  constructor() {
+    this._initialized = false;
+  }
 
   get classDescription() {
     return "Web Share Picker";
   }
 
   get classID() {
     return Components.ID("{1201d357-8417-4926-a694-e6408fbedcf8}");
   }
@@ -21,16 +23,35 @@ class SharePicker {
     return "@mozilla.org/sharepicker;1";
   }
 
   get QueryInterface() {
     return ChromeUtils.generateQI([Ci.nsISharePicker]);
   }
 
   /**
+   * Initializer.
+   *
+   * @param {nsIDOMWindow} openerWindow
+   */
+  init(openerWindow) {
+    if (this._initialized) {
+      throw new Error("Unexpected re-initialization. This is not allowed.");
+    }
+
+    this._initialized = true;
+
+    if (openerWindow instanceof Ci.nsIDOMWindow === false) {
+      throw new TypeError("Expected nsIDOMWindow");
+    }
+
+    this._openerWindow = openerWindow;
+  }
+
+  /**
    * The data being shared by the Document.
    *
    * @param {String?} title - title of the share
    * @param {String?} text - text shared
    * @param {nsIURI?} url - a URI shared
    */
   async share(title, text, url) {
     // If anything goes wrong, always throw a real DOMException.
@@ -44,12 +65,18 @@ class SharePicker {
     //
     // Returning without throwing is success.
     //
     // This mock implementation just rejects - it's just here
     // as a guide to do actual platform integration.
     throw new DOMException("Not supported.", "AbortError");
   }
 
+  /**
+   * @returns mozIDOMWindowProxy
+   */
+  get openerWindow() {
+    return this._openerWindow;
+  }
   __init() {}
 }
 
 const NSGetFactory = XPCOMUtils.generateNSGetFactory([SharePicker]);
--- a/dom/ipc/WindowGlobalParent.cpp
+++ b/dom/ipc/WindowGlobalParent.cpp
@@ -352,22 +352,34 @@ NS_IMPL_ISUPPORTS0(ShareHandler)
 
 }  // namespace
 
 mozilla::ipc::IPCResult WindowGlobalParent::RecvShare(
     IPCWebShareData&& aData, WindowGlobalParent::ShareResolver&& aResolver) {
   // Widget Layer handoff...
   nsCOMPtr<nsISharePicker> sharePicker =
       do_GetService("@mozilla.org/sharepicker;1");
-
   if (!sharePicker) {
     aResolver(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
     return IPC_OK();
   }
 
+  // Initialize the ShareWidget
+  RefPtr<BrowserParent> parent = GetBrowserParent();
+  if (NS_WARN_IF(!parent)) {
+    aResolver(NS_ERROR_FAILURE);
+    return IPC_OK();
+  }
+  nsCOMPtr<mozIDOMWindowProxy> openerWindow = parent->GetParentWindowOuter();
+  if (!openerWindow) {
+    aResolver(NS_ERROR_FAILURE);
+    return IPC_OK();
+  }
+  sharePicker->Init(openerWindow);
+
   // And finally share the data...
   RefPtr<Promise> promise;
   nsresult rv = sharePicker->Share(aData.title(), aData.text(), aData.url(),
                                    getter_AddRefs(promise));
   if (NS_FAILED(rv)) {
     aResolver(rv);
     return IPC_OK();
   }
--- a/widget/nsISharePicker.idl
+++ b/widget/nsISharePicker.idl
@@ -3,17 +3,30 @@
  * 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 "nsISupports.idl"
 
 interface nsIURI;
 
+interface mozIDOMWindowProxy;
+
 [scriptable, uuid(1201d357-8417-4926-a694-e6408fbedcf8)]
 interface nsISharePicker : nsISupports
 {
   /**
+   * Initialize the share picker widget.
+   * @param nsIDOMWindow openerWindow.
+   */
+  void init(in mozIDOMWindowProxy openerWindow);
+
+  /**
+   * Returns the parent window this was initialized with.
+   */
+  readonly attribute mozIDOMWindowProxy openerWindow;
+
+  /**
    * XPCOM Analog of navigator.share() as per:
    * https://w3c.github.io/web-share/#share-method
    */
   Promise share(in AUTF8String title, in AUTF8String text, in nsIURI url);
 };