summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/URLDecorationAnnotationsService.sys.mjs
blob: ca285e972d6bf7949b1138f918c0c00c13187cc1 (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
69
70
/* 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/. */

export function URLDecorationAnnotationsService() {}

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  RemoteSettings: "resource://services-settings/remote-settings.sys.mjs",
});

const COLLECTION_NAME = "anti-tracking-url-decoration";
const PREF_NAME = "privacy.restrict3rdpartystorage.url_decorations";

URLDecorationAnnotationsService.prototype = {
  classID: Components.ID("{5874af6d-5719-4e1b-b155-ef4eae7fcb32}"),
  QueryInterface: ChromeUtils.generateQI([
    "nsIObserver",
    "nsIURLDecorationAnnotationsService",
  ]),

  _initialized: false,
  _prefBranch: null,

  onDataAvailable(entries) {
    // Use this technique in order to ensure the pref cannot be changed by the
    // user e.g. through about:config.  This preferences is only intended as a
    // mechanism for reflecting this data to content processes.
    if (this._prefBranch === null) {
      this._prefBranch = Services.prefs.getDefaultBranch("");
    }

    const branch = this._prefBranch;
    branch.unlockPref(PREF_NAME);
    branch.setStringPref(
      PREF_NAME,
      entries.map(x => x.token.replace(/ /, "%20")).join(" ")
    );
    branch.lockPref(PREF_NAME);
  },

  observe(aSubject, aTopic, aData) {
    if (aTopic == "profile-after-change") {
      this.ensureUpdated();
    }
  },

  ensureUpdated() {
    if (this._initialized) {
      return Promise.resolve();
    }
    this._initialized = true;

    const client = lazy.RemoteSettings(COLLECTION_NAME);
    client.on("sync", event => {
      let {
        data: { current },
      } = event;
      this.onDataAvailable(current);
    });

    // Now trigger an update from the server if necessary to get a fresh copy
    // of the data
    return client.get({}).then(entries => {
      this.onDataAvailable(entries);
      return undefined;
    });
  },
};