summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/resources/extensions-backgroundscript-status.js
blob: f6c7b3067d1ae4eb738cc9a2845de0f17d7da162 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const {
  TYPES: { EXTENSIONS_BGSCRIPT_STATUS },
} = require("resource://devtools/server/actors/resources/index.js");

class ExtensionsBackgroundScriptStatusWatcher {
  /**
   * Start watching for the status updates related to a background
   * scripts extension context (either an event page or a background
   * service worker).
   *
   * This is used in about:debugging to update the background script
   * row updated visible in Extensions details cards (only for extensions
   * with a non persistent background script defined in the manifest)
   * when the background contex is terminated on idle or started back
   * to handle a persistent WebExtensions API event.
   *
   * @param RootActor rootActor
   *        The root actor in the parent process from which we should
   *        observe root resources.
   * @param Object options
   *        Dictionary object with following attributes:
   *        - onAvailable: mandatory function
   *          This will be called for each resource.
   */
  async watch(rootActor, { onAvailable }) {
    this.rootActor = rootActor;
    this.onAvailable = onAvailable;

    Services.obs.addObserver(this, "extension:background-script-status");
  }

  observe(subject, topic) {
    switch (topic) {
      case "extension:background-script-status": {
        const { addonId, isRunning } = subject.wrappedJSObject;
        this.onBackgroundScriptStatus(addonId, isRunning);
        break;
      }
    }
  }

  onBackgroundScriptStatus(addonId, isRunning) {
    this.onAvailable([
      {
        resourceType: EXTENSIONS_BGSCRIPT_STATUS,
        payload: {
          addonId,
          isRunning,
        },
      },
    ]);
  }

  destroy() {
    if (this.onAvailable) {
      this.onAvailable = null;
      Services.obs.removeObserver(this, "extension:background-script-status");
    }
  }
}

module.exports = ExtensionsBackgroundScriptStatusWatcher;