webextension.js (3369B)
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 "use strict"; 5 6 const { 7 webExtensionDescriptorSpec, 8 } = require("resource://devtools/shared/specs/descriptors/webextension.js"); 9 const { 10 FrontClassWithSpec, 11 registerFront, 12 } = require("resource://devtools/shared/protocol.js"); 13 const { 14 DescriptorMixin, 15 } = require("resource://devtools/client/fronts/descriptors/descriptor-mixin.js"); 16 const DESCRIPTOR_TYPES = require("resource://devtools/client/fronts/descriptors/descriptor-types.js"); 17 18 class WebExtensionDescriptorFront extends DescriptorMixin( 19 FrontClassWithSpec(webExtensionDescriptorSpec) 20 ) { 21 constructor(client, targetFront, parentFront) { 22 super(client, targetFront, parentFront); 23 this.traits = {}; 24 } 25 26 descriptorType = DESCRIPTOR_TYPES.EXTENSION; 27 28 form(json) { 29 this.actorID = json.actor; 30 31 // Do not use `form` name to avoid colliding with protocol.js's `form` method 32 this._form = json; 33 this.traits = json.traits || {}; 34 } 35 36 setTarget(targetFront) { 37 this._targetFront = targetFront; 38 } 39 40 get backgroundScriptStatus() { 41 return this._form.backgroundScriptStatus; 42 } 43 44 get debuggable() { 45 return this._form.debuggable; 46 } 47 48 get hidden() { 49 return this._form.hidden; 50 } 51 52 get iconDataURL() { 53 return this._form.iconDataURL; 54 } 55 56 get iconURL() { 57 return this._form.iconURL; 58 } 59 60 get id() { 61 return this._form.id; 62 } 63 64 get isSystem() { 65 return this._form.isSystem; 66 } 67 68 get isWebExtensionDescriptor() { 69 return true; 70 } 71 72 get isWebExtension() { 73 return this._form.isWebExtension; 74 } 75 76 get manifestURL() { 77 return this._form.manifestURL; 78 } 79 80 get name() { 81 return this._form.name; 82 } 83 84 get persistentBackgroundScript() { 85 return this._form.persistentBackgroundScript; 86 } 87 88 get temporarilyInstalled() { 89 return this._form.temporarilyInstalled; 90 } 91 92 get url() { 93 return this._form.url; 94 } 95 96 get warnings() { 97 return this._form.warnings; 98 } 99 100 isServerTargetSwitchingEnabled() { 101 // Since Firefox 133, this is always true for webextension toolboxes. 102 return true; 103 } 104 105 getWatcher() { 106 return super.getWatcher({ 107 isServerTargetSwitchingEnabled: this.isServerTargetSwitchingEnabled(), 108 }); 109 } 110 111 /** 112 * Retrieve the WindowGlobalTargetFront for the top level WindowGlobal 113 * currently active related to the Web Extension. 114 * 115 * WebExtensionDescriptors will be created for any type of addon type 116 * (webextension, search plugin, themes). Only webextensions can be targets. 117 * This method will throw for other addon types. 118 * 119 * TODO: We should filter out non-webextension & non-debuggable addons on the 120 * server to avoid the isWebExtension check here. See Bug 1644355. 121 */ 122 async getTarget() { 123 if (!this.isWebExtension) { 124 throw new Error( 125 "Tried to create a target for an addon which is not a webextension: " + 126 this.actorID 127 ); 128 } 129 130 if (this._targetFront && !this._targetFront.isDestroyed()) { 131 return this._targetFront; 132 } 133 134 throw new Error( 135 "Missing webextension target actor front. TargetCommand did not notify it (yet?) to the descriptor" 136 ); 137 } 138 } 139 140 registerFront(WebExtensionDescriptorFront);