summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/PartitioningExceptionListService.sys.mjs
blob: 8df2bc651d016f412bd7abb9b9ed95cd960b4353 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* 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/. */

const lazy = {};

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

const COLLECTION_NAME = "partitioning-exempt-urls";
const PREF_NAME = "privacy.restrict3rdpartystorage.skip_list";

class Feature {
  constructor() {
    this.prefName = PREF_NAME;
    this.observers = new Set();
    this.prefValue = [];
    this.remoteEntries = [];

    if (this.prefName) {
      let prefValue = Services.prefs.getStringPref(this.prefName, null);
      this.prefValue = prefValue ? prefValue.split(";") : [];
      Services.prefs.addObserver(this.prefName, this);
    }
  }

  async addAndRunObserver(observer) {
    this.observers.add(observer);
    this.notifyObservers(observer);
  }

  removeObserver(observer) {
    this.observers.delete(observer);
  }

  observe(subject, topic, data) {
    if (topic != "nsPref:changed" || data != this.prefName) {
      console.error(`Unexpected event ${topic} with ${data}`);
      return;
    }

    let prefValue = Services.prefs.getStringPref(this.prefName, null);
    this.prefValue = prefValue ? prefValue.split(";") : [];
    this.notifyObservers();
  }

  onRemoteSettingsUpdate(entries) {
    this.remoteEntries = [];

    for (let entry of entries) {
      this.remoteEntries.push(
        `${entry.firstPartyOrigin},${entry.thirdPartyOrigin}`
      );
    }
  }

  notifyObservers(observer = null) {
    let entries = this.prefValue.concat(this.remoteEntries);
    let entriesAsString = entries.join(";").toLowerCase();
    if (observer) {
      observer.onExceptionListUpdate(entriesAsString);
    } else {
      for (let obs of this.observers) {
        obs.onExceptionListUpdate(entriesAsString);
      }
    }
  }
}

export function PartitioningExceptionListService() {}

PartitioningExceptionListService.prototype = {
  classID: Components.ID("{ab94809d-33f0-4f28-af38-01efbd3baf22}"),
  QueryInterface: ChromeUtils.generateQI([
    "nsIPartitioningExceptionListService",
  ]),

  _initialized: false,

  async lazyInit() {
    if (this._initialized) {
      return;
    }

    this.feature = new Feature();

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

    this._initialized = true;

    let entries;
    // If the remote settings list hasn't been populated yet we have to make sure
    // to do it before firing the first notification.
    // This has to be run after _initialized is set because we'll be
    // blocked while getting entries from RemoteSetting, and we don't want
    // LazyInit is executed again.
    try {
      // The data will be initially available from the local DB (via a
      // resource:// URI).
      entries = await rs.get();
    } catch (e) {}

    // RemoteSettings.get() could return null, ensure passing a list to
    // onUpdateEntries.
    this.onUpdateEntries(entries || []);
  },

  onUpdateEntries(entries) {
    if (!this.feature) {
      return;
    }
    this.feature.onRemoteSettingsUpdate(entries);
    this.feature.notifyObservers();
  },

  registerAndRunExceptionListObserver(observer) {
    // We don't await this; the caller is C++ and won't await this function,
    // and because we prevent re-entering into this method, once it's been
    // called once any subsequent calls will early-return anyway - so
    // awaiting that would be meaningless. Instead, `Feature` implementations
    // make sure not to call into observers until they have data, and we
    // make sure to let feature instances know whether we have data
    // immediately.
    this.lazyInit();

    this.feature.addAndRunObserver(observer);
  },

  unregisterExceptionListObserver(observer) {
    if (!this.feature) {
      return;
    }
    this.feature.removeObserver(observer);
  },
};