Bug 1888033 - [Menu Redesign] Add a secret setting and feature flag for the menu...
[gecko.git] / devtools / client / shared / link.js
blob2e85caad0c24a3c57e902efa9decaa3c8d4c16af
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 const {
8   gDevTools,
9 } = require("resource://devtools/client/framework/devtools.js");
11 /**
12  * Retrieve the most recent chrome window.
13  */
14 function _getTopWindow() {
15   // Try the main application window, such as a browser window.
16   let win = Services.wm.getMostRecentWindow(gDevTools.chromeWindowType);
17   if (win?.openWebLinkIn && win?.openTrustedLinkIn) {
18     return win;
19   }
20   // For non-browser cases like Browser Toolbox, try any chrome window.
21   win = Services.wm.getMostRecentWindow(null);
22   if (win?.openWebLinkIn && win?.openTrustedLinkIn) {
23     return win;
24   }
25   return null;
28 /**
29  * Opens a |url| that does not require trusted access, such as a documentation page, in a
30  * new tab.
31  *
32  * @param {String} url
33  *        The url to open.
34  * @param {Object} options
35  *        Optional parameters, see documentation for openUILinkIn in utilityOverlay.js
36  */
37 exports.openDocLink = async function (url, options) {
38   const top = _getTopWindow();
39   if (!top) {
40     return;
41   }
42   top.openWebLinkIn(url, "tab", options);
45 /**
46  * Opens a |url| controlled by web content in a new tab.
47  *
48  * If the current tab has an open toolbox, this will attempt to refine the
49  * `triggeringPrincipal` of the link using the tab's `contentPrincipal`.  This is only an
50  * approximation, so bug 1467945 hopes to improve this.
51  *
52  * @param {String} url
53  *        The url to open.
54  * @param {Object} options
55  *        Optional parameters, see documentation for openUILinkIn in utilityOverlay.js
56  */
57 exports.openContentLink = async function (url, options = {}) {
58   const top = _getTopWindow();
59   if (!top) {
60     return;
61   }
62   if (!options.triggeringPrincipal && top.gBrowser) {
63     const tab = top.gBrowser.selectedTab;
64     if (gDevTools.hasToolboxForTab(tab)) {
65       options.triggeringPrincipal = tab.linkedBrowser.contentPrincipal;
66       options.csp = tab.linkedBrowser.csp;
67     }
68   }
69   top.openWebLinkIn(url, "tab", options);
72 /**
73  * Open a trusted |url| in a new tab using the SystemPrincipal.
74  *
75  * @param {String} url
76  *        The url to open.
77  * @param {Object} options
78  *        Optional parameters, see documentation for openUILinkIn in utilityOverlay.js
79  */
80 exports.openTrustedLink = async function (url, options) {
81   const top = _getTopWindow();
82   if (!top) {
83     return;
84   }
85   top.openTrustedLinkIn(url, "tab", options);