summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/link.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /devtools/client/shared/link.js
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/client/shared/link.js')
-rw-r--r--devtools/client/shared/link.js86
1 files changed, 86 insertions, 0 deletions
diff --git a/devtools/client/shared/link.js b/devtools/client/shared/link.js
new file mode 100644
index 0000000000..431a9a7ea3
--- /dev/null
+++ b/devtools/client/shared/link.js
@@ -0,0 +1,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 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?.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 (TargetFactory.isKnownTab(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);
+};