From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- .../src/utils/firefox/open-request-in-tab.js | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 devtools/client/netmonitor/src/utils/firefox/open-request-in-tab.js (limited to 'devtools/client/netmonitor/src/utils/firefox/open-request-in-tab.js') diff --git a/devtools/client/netmonitor/src/utils/firefox/open-request-in-tab.js b/devtools/client/netmonitor/src/utils/firefox/open-request-in-tab.js new file mode 100644 index 0000000000..20ea3dcba2 --- /dev/null +++ b/devtools/client/netmonitor/src/utils/firefox/open-request-in-tab.js @@ -0,0 +1,67 @@ +/* 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/. */ + +// This file is a chrome-API-dependent version of the module +// devtools/client/netmonitor/src/utils/open-request-in-tab.js, so that it can +// take advantage of utilizing chrome APIs. But because of this, it isn't +// intended to be used in Chrome-API-free applications, such as the Launchpad. +// +// Please keep in mind that if the feature in this file has changed, don't +// forget to also change that accordingly in +// devtools/client/netmonitor/src/utils/open-request-in-tab.js. + +"use strict"; + +const { + gDevTools, +} = require("resource://devtools/client/framework/devtools.js"); + +/** + * Opens given request in a new tab. + */ +function openRequestInTab(url, requestHeaders, requestPostData) { + const win = Services.wm.getMostRecentWindow(gDevTools.chromeWindowType); + const rawData = requestPostData ? requestPostData.postData : null; + let postData; + + if (rawData?.text) { + const stringStream = getInputStreamFromString(rawData.text); + postData = Cc["@mozilla.org/network/mime-input-stream;1"].createInstance( + Ci.nsIMIMEInputStream + ); + + const contentTypeHeader = requestHeaders.headers.find(e => { + return e.name.toLowerCase() === "content-type"; + }); + + postData.addHeader( + "Content-Type", + contentTypeHeader + ? contentTypeHeader.value + : "application/x-www-form-urlencoded" + ); + postData.setData(stringStream); + } + const { userContextId } = win.gBrowser.contentPrincipal; + win.gBrowser.selectedTab = win.gBrowser.addWebTab(url, { + // TODO this should be using the original request principal + triggeringPrincipal: Services.scriptSecurityManager.createNullPrincipal({ + userContextId, + }), + userContextId, + postData, + }); +} + +function getInputStreamFromString(data) { + const stringStream = Cc[ + "@mozilla.org/io/string-input-stream;1" + ].createInstance(Ci.nsIStringInputStream); + stringStream.data = data; + return stringStream; +} + +module.exports = { + openRequestInTab, +}; -- cgit v1.2.3