summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/link.js
blob: 2e85caad0c24a3c57e902efa9decaa3c8d4c16af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* 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";

const {
  gDevTools,
} = require("resource://devtools/client/framework/devtools.js");

/**
 * Retrieve the most recent chrome window.
 */
function _getTopWindow() {
  // Try the main application window, such as a browser window.
  let win = Services.wm.getMostRecentWindow(gDevTools.chromeWindowType);
  if (win?.openWebLinkIn && win?.openTrustedLinkIn) {
    return win;
  }
  // For non-browser cases like Browser Toolbox, try any chrome window.
  win = Services.wm.getMostRecentWindow(null);
  if (win?.openWebLinkIn && win?.openTrustedLinkIn) {
    return win;
  }
  return null;
}

/**
 * Opens a |url| that does not require trusted access, such as a documentation page, in a
 * new tab.
 *
 * @param {String} url
 *        The url to open.
 * @param {Object} options
 *        Optional parameters, see documentation for openUILinkIn in utilityOverlay.js
 */
exports.openDocLink = async function (url, options) {
  const top = _getTopWindow();
  if (!top) {
    return;
  }
  top.openWebLinkIn(url, "tab", options);
};

/**
 * Opens a |url| controlled by web content in a new tab.
 *
 * If the current tab has an open toolbox, this will attempt to refine the
 * `triggeringPrincipal` of the link using the tab's `contentPrincipal`.  This is only an
 * approximation, so bug 1467945 hopes to improve this.
 *
 * @param {String} url
 *        The url to open.
 * @param {Object} options
 *        Optional parameters, see documentation for openUILinkIn in utilityOverlay.js
 */
exports.openContentLink = async function (url, options = {}) {
  const top = _getTopWindow();
  if (!top) {
    return;
  }
  if (!options.triggeringPrincipal && top.gBrowser) {
    const tab = top.gBrowser.selectedTab;
    if (gDevTools.hasToolboxForTab(tab)) {
      options.triggeringPrincipal = tab.linkedBrowser.contentPrincipal;
      options.csp = tab.linkedBrowser.csp;
    }
  }
  top.openWebLinkIn(url, "tab", options);
};

/**
 * Open a trusted |url| in a new tab using the SystemPrincipal.
 *
 * @param {String} url
 *        The url to open.
 * @param {Object} options
 *        Optional parameters, see documentation for openUILinkIn in utilityOverlay.js
 */
exports.openTrustedLink = async function (url, options) {
  const top = _getTopWindow();
  if (!top) {
    return;
  }
  top.openTrustedLinkIn(url, "tab", options);
};