worker.js (5298B)
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 { 8 workerTargetSpec, 9 } = require("resource://devtools/shared/specs/targets/worker.js"); 10 11 const { 12 WebConsoleActor, 13 } = require("resource://devtools/server/actors/webconsole.js"); 14 const { ThreadActor } = require("resource://devtools/server/actors/thread.js"); 15 const { TracerActor } = require("resource://devtools/server/actors/tracer.js"); 16 const { 17 ObjectsManagerActor, 18 } = require("resource://devtools/server/actors/objects-manager.js"); 19 20 const Targets = require("resource://devtools/server/actors/targets/index.js"); 21 22 const makeDebuggerUtil = require("resource://devtools/server/actors/utils/make-debugger.js"); 23 const { 24 SourcesManager, 25 } = require("resource://devtools/server/actors/utils/sources-manager.js"); 26 27 const { 28 BaseTargetActor, 29 } = require("resource://devtools/server/actors/targets/base-target-actor.js"); 30 31 class WorkerTargetActor extends BaseTargetActor { 32 /** 33 * Target actor for a worker in the content process. 34 * 35 * @param {DevToolsServerConnection} conn: The connection to the client. 36 * @param {WorkerGlobalScope} workerGlobal: The worker global. 37 * @param {object} workerDebuggerData: The worker debugger information 38 * @param {string} workerDebuggerData.id: The worker debugger id 39 * @param {string} workerDebuggerData.url: The worker debugger url 40 * @param {string} workerDebuggerData.type: The worker debugger type 41 * @param {number?} workerDebuggerData.relatedDocumentInnerWindowId: (optional) 42 * If the worker is spawned from a document, the innerWindowId of it. 43 * @param {boolean} workerDebuggerData.workerConsoleApiMessagesDispatchedToMainThread: 44 * Value of the dom.worker.console.dispatch_events_to_main_thread pref 45 * @param {object} sessionContext: The Session Context to help know what is debugged. 46 * See devtools/server/actors/watcher/session-context.js 47 */ 48 constructor(conn, workerGlobal, workerDebuggerData, sessionContext) { 49 super(conn, Targets.TYPES.WORKER, workerTargetSpec); 50 51 // workerGlobal is needed by the console actor for evaluations. 52 this.#workerGlobal = workerGlobal; 53 this.sessionContext = sessionContext; 54 55 // We don't have access to Ci from worker thread 56 // 2 == nsIWorkerDebugger.TYPE_SERVICE 57 // 1 == nsIWorkerDebugger.TYPE_SHARED 58 if (workerDebuggerData.type == 2) { 59 this.targetType = Targets.TYPES.SERVICE_WORKER; 60 } else if (workerDebuggerData.type == 1) { 61 this.targetType = Targets.TYPES.SHARED_WORKER; 62 } 63 this._relatedDocumentInnerWindowId = 64 workerDebuggerData.relatedDocumentInnerWindowId; 65 66 this._workerDebuggerData = workerDebuggerData; 67 this._sourcesManager = null; 68 this.workerConsoleApiMessagesDispatchedToMainThread = 69 workerDebuggerData.workerConsoleApiMessagesDispatchedToMainThread; 70 71 this.makeDebugger = makeDebuggerUtil.bind(null, { 72 findDebuggees: () => { 73 return [workerGlobal]; 74 }, 75 shouldAddNewGlobalAsDebuggee: () => true, 76 }); 77 78 // needed by the console actor 79 this.threadActor = new ThreadActor(this); 80 81 // needed by the thread actor to communicate with the console when evaluating logpoints. 82 this._consoleActor = new WebConsoleActor(this.conn, this); 83 84 this.tracerActor = new TracerActor(this.conn, this); 85 this.objectsManagerActor = new ObjectsManagerActor(this.conn, this); 86 87 this.manage(this.threadActor); 88 this.manage(this._consoleActor); 89 this.manage(this.tracerActor); 90 this.manage(this.objectsManagerActor); 91 } 92 93 #workerGlobal = null; 94 95 get targetGlobal() { 96 return this.#workerGlobal; 97 } 98 99 // Expose the worker URL to the thread actor. 100 // so that it can easily know what is the base URL of all worker scripts. 101 get workerUrl() { 102 return this._workerDebuggerData.url; 103 } 104 105 form() { 106 return { 107 actor: this.actorID, 108 targetType: this.targetType, 109 110 consoleActor: this._consoleActor?.actorID, 111 threadActor: this.threadActor?.actorID, 112 tracerActor: this.tracerActor?.actorID, 113 objectsManagerActor: this.objectsManagerActor?.actorID, 114 115 id: this._workerDebuggerData.id, 116 name: this._workerDebuggerData.name, 117 type: this._workerDebuggerData.type, 118 url: this._workerDebuggerData.url, 119 relatedDocumentInnerWindowId: this._relatedDocumentInnerWindowId, 120 traits: { 121 // See trait description in browsing-context.js 122 supportsTopLevelTargetFlag: false, 123 }, 124 }; 125 } 126 127 get dbg() { 128 if (!this._dbg) { 129 this._dbg = this.makeDebugger(); 130 } 131 return this._dbg; 132 } 133 134 get sourcesManager() { 135 if (this._sourcesManager === null) { 136 this._sourcesManager = new SourcesManager(this.threadActor); 137 } 138 139 return this._sourcesManager; 140 } 141 142 destroy() { 143 super.destroy(); 144 145 if (this._sourcesManager) { 146 this._sourcesManager.destroy(); 147 this._sourcesManager = null; 148 } 149 150 this.#workerGlobal = null; 151 this._dbg = null; 152 this._consoleActor = null; 153 this.threadActor = null; 154 } 155 } 156 exports.WorkerTargetActor = WorkerTargetActor;