GeckoViewChildModule.sys.mjs (2090B)
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 import { GeckoViewUtils } from "resource://gre/modules/GeckoViewUtils.sys.mjs"; 6 7 const { debug, warn } = GeckoViewUtils.initLogging("Module[C]"); 8 9 export class GeckoViewChildModule { 10 static initLogging(aModuleName) { 11 this._moduleName = aModuleName; 12 const tag = aModuleName.replace("GeckoView", "") + "[C]"; 13 return GeckoViewUtils.initLogging(tag); 14 } 15 16 static create(aGlobal, aModuleName) { 17 return new this(aModuleName || this._moduleName, aGlobal); 18 } 19 20 constructor(aModuleName, aGlobal) { 21 this.moduleName = aModuleName; 22 this.messageManager = aGlobal; 23 this.enabled = false; 24 25 if (!aGlobal._gvEventDispatcher) { 26 aGlobal._gvEventDispatcher = GeckoViewUtils.getDispatcherForWindow( 27 aGlobal.content 28 ); 29 aGlobal.addEventListener( 30 "unload", 31 event => { 32 if (event.target === this.messageManager) { 33 aGlobal._gvEventDispatcher.finalize(); 34 } 35 }, 36 { 37 mozSystemGroup: true, 38 } 39 ); 40 } 41 this.eventDispatcher = aGlobal._gvEventDispatcher; 42 43 this.messageManager.addMessageListener( 44 "GeckoView:UpdateModuleState", 45 aMsg => { 46 if (aMsg.data.module !== this.moduleName) { 47 return; 48 } 49 50 const { enabled } = aMsg.data; 51 52 if (enabled !== this.enabled) { 53 if (!enabled) { 54 this.onDisable(); 55 } 56 57 this.enabled = enabled; 58 59 if (enabled) { 60 this.onEnable(); 61 } 62 } 63 } 64 ); 65 66 this.onInit(); 67 68 this.messageManager.sendAsyncMessage("GeckoView:ContentModuleLoaded", { 69 module: this.moduleName, 70 }); 71 } 72 73 // Override to initialize module. 74 onInit() {} 75 76 // Override to enable module after setting a Java delegate. 77 onEnable() {} 78 79 // Override to disable module after clearing the Java delegate. 80 onDisable() {} 81 }