application-services.js (2073B)
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 // keyword to use in telemetry, as `reason` parameter 8 const REASON = "application"; 9 10 class ManifestDevToolsError extends Error { 11 constructor(...params) { 12 super(...params); 13 14 this.name = "ManifestDevToolsError"; 15 } 16 } 17 18 class ApplicationServices { 19 init(toolbox) { 20 this._toolbox = toolbox; 21 22 this.features = { 23 doesDebuggerSupportWorkers: Services.prefs.getBoolPref( 24 "devtools.debugger.features.windowless-service-workers", 25 false 26 ), 27 }; 28 } 29 30 selectTool(toolId) { 31 this._assertInit(); 32 return this._toolbox.selectTool(toolId, REASON); 33 } 34 35 async openWorkerInDebugger(workerDescriptorFront) { 36 const debuggerPanel = await this.selectTool("jsdebugger"); 37 debuggerPanel.selectServiceWorker(workerDescriptorFront); 38 } 39 40 async viewWorkerSource(workerDescriptorFront) { 41 // NOTE: this falls back to view-source: if the source can't be inspected 42 // within the debugger. 43 this._toolbox.viewSourceInDebugger( 44 workerDescriptorFront.url, 45 1, 46 1, 47 null, 48 REASON 49 ); 50 } 51 52 async fetchManifest() { 53 let response; 54 55 try { 56 this._assertInit(); 57 const manifestFront = await this._toolbox.target.getFront("manifest"); 58 response = await manifestFront.fetchCanonicalManifest(); 59 } catch (error) { 60 throw new ManifestDevToolsError( 61 error.message, 62 error.fileName, 63 error.lineNumber 64 ); 65 } 66 67 if (response.errorMessage) { 68 throw new Error(response.errorMessage); 69 } 70 71 return response.manifest; 72 } 73 74 _assertInit() { 75 if (!this._toolbox) { 76 throw new Error("Services singleton has not been initialized"); 77 } 78 } 79 } 80 81 module.exports = { 82 ManifestDevToolsError, 83 // exports a singleton, which will be used across all application panel modules 84 services: new ApplicationServices(), 85 };