Bug 1453423 - use openWeb/TrustedLink in devtools/shared/client/link;r=jryans
authorJulian Descottes <jdescottes@mozilla.com>
Wed, 11 Apr 2018 20:35:36 +0200
changeset 781267 4966b5fef257fda94abb735c79634bd8fdd27817
parent 781266 84fc374da5e3e01050664b3e3c34301a5eaea4c6
child 781268 68d9ee4629e45587abfadec3172fcc33c2c3579f
push id106259
push userkgupta@mozilla.com
push dateThu, 12 Apr 2018 19:54:47 +0000
reviewersjryans
bugs1453423
milestone61.0a1
Bug 1453423 - use openWeb/TrustedLink in devtools/shared/client/link;r=jryans MozReview-Commit-ID: EMUqVCIxzUt
devtools/client/framework/components/toolbox-toolbar.js
devtools/client/shared/link.js
--- a/devtools/client/framework/components/toolbox-toolbar.js
+++ b/devtools/client/framework/components/toolbox-toolbar.js
@@ -2,17 +2,17 @@
  * 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/. */
 "use strict";
 
 const { Component, createFactory } = require("devtools/client/shared/vendor/react");
 const dom = require("devtools/client/shared/vendor/react-dom-factories");
 const PropTypes = require("devtools/client/shared/vendor/react-prop-types");
 const {div, button} = dom;
-const {openLink} = require("devtools/client/shared/link");
+const {openTrustedLink} = require("devtools/client/shared/link");
 
 const Menu = require("devtools/client/framework/menu");
 const MenuItem = require("devtools/client/framework/menu-item");
 const ToolboxTab = createFactory(require("devtools/client/framework/components/toolbox-tab"));
 const ToolboxTabs = createFactory(require("devtools/client/framework/components/toolbox-tabs"));
 
 /**
  * This is the overall component for the toolbox toolbar. It is designed to not know how
@@ -392,26 +392,26 @@ function showMeatballMenu(
     menu.append(new MenuItem({ type: "separator" }));
   }
 
   // Getting started
   menu.append(new MenuItem({
     id: "toolbox-meatball-menu-gettingstarted",
     label: L10N.getStr("toolbox.meatballMenu.gettingStarted.label"),
     click: () => {
-      openLink("https://developer.mozilla.org/docs/Tools", toolbox);
+      openTrustedLink("https://developer.mozilla.org/docs/Tools", toolbox);
     },
   }));
 
   // Give feedback
   menu.append(new MenuItem({
     id: "toolbox-meatball-menu-feedback",
     label: L10N.getStr("toolbox.meatballMenu.giveFeedback.label"),
     click: () => {
-      openLink("https://discourse.mozilla.org/c/devtools", toolbox);
+      openTrustedLink("https://discourse.mozilla.org/c/devtools", toolbox);
     },
   }));
 
   const rect = menuButton.getBoundingClientRect();
   const screenX = menuButton.ownerDocument.defaultView.mozInnerScreenX;
   const screenY = menuButton.ownerDocument.defaultView.mozInnerScreenY;
 
   // Display the popup below the button.
--- a/devtools/client/shared/link.js
+++ b/devtools/client/shared/link.js
@@ -1,27 +1,57 @@
 /* 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/. */
 
 "use strict";
 
 /**
- * Opens |url| in a new tab.
+ * Retrieve the top window for the provided toolbox that should have the expected
+ * open*LinkIn methods.
  */
-exports.openLink = async function(url, toolbox) {
+function _getTopWindow(toolbox) {
   const parentDoc = toolbox.doc;
   if (!parentDoc) {
-    return;
+    return null;
   }
 
   const win = parentDoc.querySelector("window");
   if (!win) {
-    return;
+    return null;
   }
 
-  const top = win.ownerDocument.defaultView.top;
-  if (!top || typeof top.openUILinkIn !== "function") {
-    return;
+  return win.ownerDocument.defaultView.top;
+}
+
+/**
+ * Opens a |url| controlled by webcontent in a new tab.
+ *
+ * @param {String} url
+ *        The url to open.
+ * @param {Toolbox} toolbox
+ *        Toolbox reference to find the parent window.
+ * @param {Object} options
+ *        Optional parameters, see documentation for openUILinkIn in utilityOverlay.js
+ */
+exports.openWebLink = async function(url, toolbox, options) {
+  const top = _getTopWindow(toolbox);
+  if (top && typeof top.openWebLinkIn === "function") {
+    top.openWebLinkIn(url, "tab", options);
   }
+};
 
-  top.openUILinkIn(url, "tab");
+/**
+ * Open a trusted |url| in a new tab using the SystemPrincipal.
+ *
+ * @param {String} url
+ *        The url to open.
+ * @param {Toolbox} toolbox
+ *        Toolbox reference to find the parent window.
+ * @param {Object} options
+ *        Optional parameters, see documentation for openUILinkIn in utilityOverlay.js
+ */
+exports.openTrustedLink = async function(url, toolbox, options) {
+  const top = _getTopWindow(toolbox);
+  if (top && typeof top.openWebLinkIn === "function") {
+    top.openTrustedLinkIn(url, "tab", options);
+  }
 };