blackboxing.js (2647B)
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 "use strict"; 6 7 const { Actor } = require("resource://devtools/shared/protocol.js"); 8 const { 9 blackboxingSpec, 10 } = require("resource://devtools/shared/specs/blackboxing.js"); 11 12 const { SessionDataHelpers } = ChromeUtils.importESModule( 13 "resource://devtools/server/actors/watcher/SessionDataHelpers.sys.mjs", 14 { global: "contextual" } 15 ); 16 const { SUPPORTED_DATA } = SessionDataHelpers; 17 const { BLACKBOXING } = SUPPORTED_DATA; 18 19 /** 20 * This actor manages the blackboxing of sources. 21 * 22 * Blackboxing data should be available as early as possible to new targets and 23 * will be forwarded to the WatcherActor to populate the shared session data available to 24 * all DevTools targets. 25 * 26 * @class 27 */ 28 class BlackboxingActor extends Actor { 29 constructor(watcherActor) { 30 super(watcherActor.conn, blackboxingSpec); 31 this.watcherActor = watcherActor; 32 } 33 34 /** 35 * Request to blackbox a new JS file either completely if no range is passed. 36 * Or only a precise subset of lines described by range attribute. 37 * 38 * @param {string} url 39 * Mandatory argument to mention what URL of JS file should be blackboxed. 40 * @param {Array<Objects>} ranges 41 * The whole file will be blackboxed if this array is empty. 42 * Each range is made of an object like this: 43 * { 44 * start: { line: 1, column: 1 }, 45 * end: { line: 10, column: 10 }, 46 * } 47 */ 48 blackbox(url, ranges) { 49 if (!ranges.length) { 50 return this.watcherActor.addOrSetDataEntry( 51 BLACKBOXING, 52 [{ url, range: null }], 53 "add" 54 ); 55 } 56 return this.watcherActor.addOrSetDataEntry( 57 BLACKBOXING, 58 ranges.map(range => { 59 return { 60 url, 61 range, 62 }; 63 }), 64 "add" 65 ); 66 } 67 68 /** 69 * Request to unblackbox some JS sources. 70 * 71 * See `blackbox` for more info. 72 */ 73 unblackbox(url, ranges) { 74 if (!ranges.length) { 75 const existingRanges = ( 76 this.watcherActor.getSessionDataForType(BLACKBOXING) || [] 77 ).filter(entry => entry.url == url); 78 79 return this.watcherActor.removeDataEntry(BLACKBOXING, existingRanges); 80 } 81 return this.watcherActor.removeDataEntry( 82 BLACKBOXING, 83 ranges.map(range => { 84 return { 85 url, 86 range, 87 }; 88 }) 89 ); 90 } 91 } 92 93 exports.BlackboxingActor = BlackboxingActor;