Backed out changeset 68ed52f7e45d (bug 1899241) for causing sccache misses (bug 19048...
[gecko.git] / toolkit / modules / IgnoreLists.sys.mjs
blob1b7282d77f946adaeacc3564feb0139cbee33e20
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 file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const lazy = {};
7 ChromeUtils.defineESModuleGetters(lazy, {
8   RemoteSettings: "resource://services-settings/remote-settings.sys.mjs",
9   RemoteSettingsClient:
10     "resource://services-settings/RemoteSettingsClient.sys.mjs",
11 });
13 const SETTINGS_IGNORELIST_KEY = "hijack-blocklists";
15 class IgnoreListsManager {
16   async init() {
17     if (!this._ignoreListSettings) {
18       this._ignoreListSettings = lazy.RemoteSettings(SETTINGS_IGNORELIST_KEY);
19     }
20   }
22   async getAndSubscribe(listener) {
23     await this.init();
25     // Trigger a get of the initial value.
26     const settings = await this._getIgnoreList();
28     // Listen for future updates after we first get the values.
29     this._ignoreListSettings.on("sync", listener);
31     return settings;
32   }
34   unsubscribe(listener) {
35     if (!this._ignoreListSettings) {
36       return;
37     }
39     this._ignoreListSettings.off("sync", listener);
40   }
42   async _getIgnoreList() {
43     if (this._getSettingsPromise) {
44       return this._getSettingsPromise;
45     }
47     const settings = await (this._getSettingsPromise =
48       this._getIgnoreListSettings());
49     delete this._getSettingsPromise;
50     return settings;
51   }
53   /**
54    * Obtains the current ignore list from remote settings. This includes
55    * verifying the signature of the ignore list within the database.
56    *
57    * If the signature in the database is invalid, the database will be wiped
58    * and the stored dump will be used, until the settings next update.
59    *
60    * Note that this may cause a network check of the certificate, but that
61    * should generally be quick.
62    *
63    * @param {boolean} [firstTime]
64    *   Internal boolean to indicate if this is the first time check or not.
65    * @returns {array}
66    *   An array of objects in the database, or an empty array if none
67    *   could be obtained.
68    */
69   async _getIgnoreListSettings(firstTime = true) {
70     let result = [];
71     try {
72       result = await this._ignoreListSettings.get({
73         verifySignature: true,
74       });
75     } catch (ex) {
76       if (
77         ex instanceof lazy.RemoteSettingsClient.InvalidSignatureError &&
78         firstTime
79       ) {
80         // The local database is invalid, try and reset it.
81         await this._ignoreListSettings.db.clear();
82         // Now call this again.
83         return this._getIgnoreListSettings(false);
84       }
85       // Don't throw an error just log it, just continue with no data, and hopefully
86       // a sync will fix things later on.
87       console.error(ex);
88     }
89     return result;
90   }
93 export const IgnoreLists = new IgnoreListsManager();