jstracer-state.js (2872B)
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 { JSTracer } = ChromeUtils.importESModule( 8 "resource://devtools/server/tracer/tracer.sys.mjs", 9 { global: "contextual" } 10 ); 11 12 const Targets = require("resource://devtools/server/actors/targets/index.js"); 13 loader.lazyRequireGetter( 14 this, 15 "TRACER_LOG_METHODS", 16 "resource://devtools/shared/specs/tracer.js", 17 true 18 ); 19 20 class TracingStateWatcher { 21 /** 22 * Start watching for tracing state changes for a given target actor. 23 * 24 * @param TargetActor targetActor 25 * The target actor from which we should observe 26 * @param Object options 27 * Dictionary object with following attributes: 28 * - onAvailable: mandatory function 29 * This will be called for each resource. 30 */ 31 async watch(targetActor, { onAvailable }) { 32 // Bug 1874204: tracer doesn't support tracing content process from the browser toolbox just yet 33 if (targetActor.targetType == Targets.TYPES.PROCESS) { 34 return; 35 } 36 37 this.targetActor = targetActor; 38 this.onAvailable = onAvailable; 39 40 this.tracingListener = { 41 onTracingToggled: this.onTracingToggled.bind(this), 42 }; 43 JSTracer.addTracingListener(this.tracingListener); 44 } 45 46 /** 47 * Stop watching for tracing state 48 */ 49 destroy() { 50 if (!this.tracingListener) { 51 return; 52 } 53 JSTracer.removeTracingListener(this.tracingListener); 54 } 55 56 /** 57 * Be notified by the underlying JavaScriptTracer class 58 * in case it stops by itself, instead of being stopped when the Actor's stopTracing 59 * method is called by the user. 60 * 61 * @param {boolean} enabled 62 * True if the tracer starts tracing, false it it stops. 63 * @param {string} reason 64 * Optional string to justify why the tracer stopped. 65 */ 66 async onTracingToggled(enabled, reason) { 67 const tracerActor = this.targetActor.getTargetScopedActor("tracer"); 68 const logMethod = tracerActor?.getLogMethod(); 69 70 // JavascriptTracer only supports recording once in the same process/thread. 71 // If we open another DevTools, on the same process, we would receive notification 72 // about a JavascriptTracer controlled by another toolbox's tracer actor. 73 // Ignore them as our current tracer actor didn't start tracing. 74 if (!logMethod) { 75 return; 76 } 77 78 this.onAvailable([ 79 { 80 enabled, 81 logMethod, 82 profile: 83 logMethod == TRACER_LOG_METHODS.PROFILER && !enabled 84 ? await tracerActor.getProfile() 85 : undefined, 86 timeStamp: ChromeUtils.dateNow(), 87 reason, 88 traceValues: tracerActor.traceValues, 89 }, 90 ]); 91 } 92 } 93 94 module.exports = TracingStateWatcher;