open-request-in-tab.js (2185B)
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/. */ 4 5 // This file is a chrome-API-dependent version of the module 6 // devtools/client/netmonitor/src/utils/open-request-in-tab.js, so that it can 7 // take advantage of utilizing chrome APIs. But because of this, it isn't 8 // intended to be used in Chrome-API-free applications, such as the Launchpad. 9 // 10 // Please keep in mind that if the feature in this file has changed, don't 11 // forget to also change that accordingly in 12 // devtools/client/netmonitor/src/utils/open-request-in-tab.js. 13 14 "use strict"; 15 16 const { 17 gDevTools, 18 } = require("resource://devtools/client/framework/devtools.js"); 19 20 /** 21 * Opens given request in a new tab. 22 */ 23 function openRequestInTab(url, requestHeaders, requestPostData) { 24 const win = Services.wm.getMostRecentWindow(gDevTools.chromeWindowType); 25 const rawData = requestPostData ? requestPostData.postData : null; 26 let postData; 27 28 if (rawData?.text) { 29 const stringStream = getInputStreamFromString(rawData.text); 30 postData = Cc["@mozilla.org/network/mime-input-stream;1"].createInstance( 31 Ci.nsIMIMEInputStream 32 ); 33 34 const contentTypeHeader = requestHeaders.headers.find(e => { 35 return e.name.toLowerCase() === "content-type"; 36 }); 37 38 postData.addHeader( 39 "Content-Type", 40 contentTypeHeader 41 ? contentTypeHeader.value 42 : "application/x-www-form-urlencoded" 43 ); 44 postData.setData(stringStream); 45 } 46 const { userContextId } = win.gBrowser.contentPrincipal; 47 win.gBrowser.selectedTab = win.gBrowser.addWebTab(url, { 48 // TODO this should be using the original request principal 49 triggeringPrincipal: Services.scriptSecurityManager.createNullPrincipal({ 50 userContextId, 51 }), 52 userContextId, 53 postData, 54 }); 55 } 56 57 function getInputStreamFromString(data) { 58 const stringStream = Cc[ 59 "@mozilla.org/io/string-input-stream;1" 60 ].createInstance(Ci.nsIStringInputStream); 61 stringStream.setByteStringData(data); 62 return stringStream; 63 } 64 65 module.exports = { 66 openRequestInTab, 67 };