extensions-backgroundscript-status.js (1950B)
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 class ExtensionsBackgroundScriptStatusWatcher { 8 /** 9 * Start watching for the status updates related to a background 10 * scripts extension context (either an event page or a background 11 * service worker). 12 * 13 * This is used in about:debugging to update the background script 14 * row updated visible in Extensions details cards (only for extensions 15 * with a non persistent background script defined in the manifest) 16 * when the background contex is terminated on idle or started back 17 * to handle a persistent WebExtensions API event. 18 * 19 * @param RootActor rootActor 20 * The root actor in the parent process from which we should 21 * observe root resources. 22 * @param Object options 23 * Dictionary object with following attributes: 24 * - onAvailable: mandatory function 25 * This will be called for each resource. 26 */ 27 async watch(rootActor, { onAvailable }) { 28 this.rootActor = rootActor; 29 this.onAvailable = onAvailable; 30 31 Services.obs.addObserver(this, "extension:background-script-status"); 32 } 33 34 observe(subject, topic) { 35 switch (topic) { 36 case "extension:background-script-status": { 37 const { addonId, isRunning } = subject.wrappedJSObject; 38 this.onBackgroundScriptStatus(addonId, isRunning); 39 break; 40 } 41 } 42 } 43 44 onBackgroundScriptStatus(addonId, isRunning) { 45 this.onAvailable([ 46 { 47 payload: { 48 addonId, 49 isRunning, 50 }, 51 }, 52 ]); 53 } 54 55 destroy() { 56 if (this.onAvailable) { 57 this.onAvailable = null; 58 Services.obs.removeObserver(this, "extension:background-script-status"); 59 } 60 } 61 } 62 63 module.exports = ExtensionsBackgroundScriptStatusWatcher;