gecko-dev/devtools/client/shared/link.js
Gurzau Raul ad28ad0169 Backed out 9 changesets (bug 1485676) for failing at damp inspector/cold-open.js on a CLOSED TREE
Backed out changeset 4de2e71debfe (bug 1485676)
Backed out changeset e63025150c7a (bug 1485676)
Backed out changeset f9ef30ae3f7f (bug 1485676)
Backed out changeset a83636fab16a (bug 1485676)
Backed out changeset b1fd24929e09 (bug 1485676)
Backed out changeset acb27b915742 (bug 1485676)
Backed out changeset ba2157632772 (bug 1485676)
Backed out changeset d1d6b9bc2372 (bug 1485676)
Backed out changeset d7646ea8640b (bug 1485676)
2018-09-22 19:42:04 +03:00

87 lines
2.6 KiB
JavaScript

/* 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 Services = require("Services");
const { gDevTools } = require("devtools/client/framework/devtools");
const { TargetFactory } = require("devtools/client/framework/target");
/**
* 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 && win.openWebLinkIn && win.openTrustedLinkIn) {
return win;
}
// For non-browser cases like Browser Toolbox, try any chrome window.
win = Services.wm.getMostRecentWindow(null);
if (win && 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 (TargetFactory.isKnownTab(tab)) {
const target = TargetFactory.forTab(tab);
options.triggeringPrincipal = target.contentPrincipal;
}
}
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);
};