tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

service-worker-registration-list.js (2958B)


      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 const { XPCOMUtils } = ChromeUtils.importESModule(
      8  "resource://gre/modules/XPCOMUtils.sys.mjs",
      9  { global: "contextual" }
     10 );
     11 loader.lazyRequireGetter(
     12  this,
     13  "ServiceWorkerRegistrationActor",
     14  "resource://devtools/server/actors/worker/service-worker-registration.js",
     15  true
     16 );
     17 
     18 XPCOMUtils.defineLazyServiceGetter(
     19  this,
     20  "swm",
     21  "@mozilla.org/serviceworkers/manager;1",
     22  Ci.nsIServiceWorkerManager
     23 );
     24 
     25 class ServiceWorkerRegistrationActorList {
     26  constructor(conn) {
     27    this._conn = conn;
     28    this._actors = new Map();
     29    this._onListChanged = null;
     30    this._mustNotify = false;
     31    this.onRegister = this.onRegister.bind(this);
     32    this.onUnregister = this.onUnregister.bind(this);
     33  }
     34 
     35  getList() {
     36    // Create a set of registrations.
     37    const registrations = new Set();
     38    const array = swm.getAllRegistrations();
     39    for (let index = 0; index < array.length; ++index) {
     40      registrations.add(
     41        array.queryElementAt(index, Ci.nsIServiceWorkerRegistrationInfo)
     42      );
     43    }
     44 
     45    // Delete each actor for which we don't have a registration.
     46    for (const [registration] of this._actors) {
     47      if (!registrations.has(registration)) {
     48        this._actors.delete(registration);
     49      }
     50    }
     51 
     52    // Create an actor for each registration for which we don't have one.
     53    for (const registration of registrations) {
     54      if (!this._actors.has(registration)) {
     55        this._actors.set(
     56          registration,
     57          new ServiceWorkerRegistrationActor(this._conn, registration)
     58        );
     59      }
     60    }
     61 
     62    if (!this._mustNotify) {
     63      if (this._onListChanged !== null) {
     64        swm.addListener(this);
     65      }
     66      this._mustNotify = true;
     67    }
     68 
     69    const actors = [];
     70    for (const [, actor] of this._actors) {
     71      actors.push(actor);
     72    }
     73 
     74    return Promise.resolve(actors);
     75  }
     76 
     77  get onListchanged() {
     78    return this._onListchanged;
     79  }
     80 
     81  set onListChanged(onListChanged) {
     82    if (typeof onListChanged !== "function" && onListChanged !== null) {
     83      throw new Error("onListChanged must be either a function or null.");
     84    }
     85 
     86    if (this._mustNotify) {
     87      if (this._onListChanged === null && onListChanged !== null) {
     88        swm.addListener(this);
     89      }
     90      if (this._onListChanged !== null && onListChanged === null) {
     91        swm.removeListener(this);
     92      }
     93    }
     94    this._onListChanged = onListChanged;
     95  }
     96 
     97  _notifyListChanged() {
     98    this._onListChanged();
     99 
    100    if (this._onListChanged !== null) {
    101      swm.removeListener(this);
    102    }
    103    this._mustNotify = false;
    104  }
    105 
    106  onRegister() {
    107    this._notifyListChanged();
    108  }
    109 
    110  onUnregister() {
    111    this._notifyListChanged();
    112  }
    113 }
    114 
    115 exports.ServiceWorkerRegistrationActorList = ServiceWorkerRegistrationActorList;