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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/* 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",
});
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
const COLLECTION_NAME = "query-stripping";
const SHARED_DATA_KEY = "URLQueryStripping";
const PREF_STRIP_LIST_NAME = "privacy.query_stripping.strip_list";
const PREF_ALLOW_LIST_NAME = "privacy.query_stripping.allow_list";
const PREF_TESTING_ENABLED = "privacy.query_stripping.testing";
const PREF_STRIP_IS_TEST =
"privacy.query_stripping.strip_on_share.enableTestMode";
ChromeUtils.defineLazyGetter(lazy, "logger", () => {
return console.createInstance({
prefix: "URLQueryStrippingListService",
maxLogLevelPref: "privacy.query_stripping.listService.logLevel",
});
});
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
"testStripOnShare",
PREF_STRIP_IS_TEST
);
// Lazy getter for the strip-on-share strip list.
ChromeUtils.defineLazyGetter(lazy, "StripOnShareList", async () => {
let response = await fetch(
"chrome://global/content/antitracking/StripOnShare.json"
);
if (!response.ok) {
lazy.logger.error(
"Error fetching strip-on-share strip list" + response.status
);
throw new Error(
"Error fetching strip-on-share strip list" + response.status
);
}
return response.json();
});
export class URLQueryStrippingListService {
classId = Components.ID("{afff16f0-3fd2-4153-9ccd-c6d9abd879e4}");
QueryInterface = ChromeUtils.generateQI(["nsIURLQueryStrippingListService"]);
#isInitialized = false;
#pendingInit = null;
#initResolver;
#stripOnShareTestList = null;
#rs;
#onSyncCallback;
constructor() {
lazy.logger.debug("constructor");
this.observers = new Set();
this.stripOnShareObservers = new Set();
this.stripOnShareParams = null;
this.prefStripList = new Set();
this.prefAllowList = new Set();
this.remoteStripList = new Set();
this.remoteAllowList = new Set();
this.isParentProcess =
Services.appinfo.processType === Services.appinfo.PROCESS_TYPE_DEFAULT;
}
#onSync(event) {
lazy.logger.debug("onSync", event);
let {
data: { current },
} = event;
this._onRemoteSettingsUpdate(current);
}
async testSetList(testList) {
this.#stripOnShareTestList = testList;
await this._notifyStripOnShareObservers();
}
testHasStripOnShareObservers() {
return !!this.stripOnShareObservers.size;
}
testHasQPSObservers() {
return !!this.observers.size;
}
async #init() {
// If there is already an init pending wait for it to complete.
if (this.#pendingInit) {
lazy.logger.debug("#init: Waiting for pending init");
await this.#pendingInit;
return;
}
if (this.#isInitialized) {
lazy.logger.debug("#init: Skip, already initialized");
return;
}
// Create a promise that resolves when init is complete. This allows us to
// handle incoming init calls while we're still initializing.
this.#pendingInit = new Promise(initResolve => {
this.#initResolver = initResolve;
});
this.#isInitialized = true;
lazy.logger.debug("#init: Run");
// We can only access the remote settings in the parent process. For content
// processes, we will use sharedData to sync the list to content processes.
if (this.isParentProcess) {
this.#rs = lazy.RemoteSettings(COLLECTION_NAME);
if (!this.#onSyncCallback) {
this.#onSyncCallback = this.#onSync.bind(this);
this.#rs.on("sync", this.#onSyncCallback);
}
// Get the initially available entries for remote settings.
let entries;
try {
entries = await this.#rs.get();
} catch (e) {}
this._onRemoteSettingsUpdate(entries || []);
} else {
// Register the message listener for the remote settings update from the
// sharedData.
Services.cpmm.sharedData.addEventListener("change", this);
// Get the remote settings data from the shared data.
let data = this._getListFromSharedData();
this._onRemoteSettingsUpdate(data);
}
// Get the list from pref.
this._onPrefUpdate(
PREF_STRIP_LIST_NAME,
Services.prefs.getStringPref(PREF_STRIP_LIST_NAME, "")
);
this._onPrefUpdate(
PREF_ALLOW_LIST_NAME,
Services.prefs.getStringPref(PREF_ALLOW_LIST_NAME, "")
);
Services.prefs.addObserver(PREF_STRIP_LIST_NAME, this);
Services.prefs.addObserver(PREF_ALLOW_LIST_NAME, this);
Services.obs.addObserver(this, "xpcom-shutdown");
this.#initResolver();
this.#pendingInit = null;
}
async #shutdown() {
// Ensure any pending init is done before shutdown.
if (this.#pendingInit) {
await this.#pendingInit;
}
// Already shut down.
if (!this.#isInitialized) {
return;
}
this.#isInitialized = false;
lazy.logger.debug("#shutdown");
// Unregister RemoteSettings listener (if it was registered).
if (this.#onSyncCallback) {
this.#rs.off("sync", this.#onSyncCallback);
this.#onSyncCallback = null;
}
Services.obs.removeObserver(this, "xpcom-shutdown");
Services.prefs.removeObserver(PREF_STRIP_LIST_NAME, this);
Services.prefs.removeObserver(PREF_ALLOW_LIST_NAME, this);
}
get hasObservers() {
return !this.observers.size && !this.stripOnShareObservers.size;
}
_onRemoteSettingsUpdate(entries) {
this.remoteStripList.clear();
this.remoteAllowList.clear();
for (let entry of entries) {
for (let item of entry.stripList) {
this.remoteStripList.add(item);
}
for (let item of entry.allowList) {
this.remoteAllowList.add(item);
}
}
// Because only the parent process will get the remote settings update, so
// we will sync the list to the shared data so that content processes can
// get the list.
if (this.isParentProcess) {
Services.ppmm.sharedData.set(SHARED_DATA_KEY, {
stripList: this.remoteStripList,
allowList: this.remoteAllowList,
});
if (Services.prefs.getBoolPref(PREF_TESTING_ENABLED, false)) {
Services.ppmm.sharedData.flush();
}
}
this._notifyObservers();
}
_onPrefUpdate(pref, value) {
switch (pref) {
case PREF_STRIP_LIST_NAME:
this.prefStripList = new Set(value ? value.split(" ") : []);
break;
case PREF_ALLOW_LIST_NAME:
this.prefAllowList = new Set(value ? value.split(",") : []);
break;
default:
console.error(`Unexpected pref name ${pref}`);
return;
}
this._notifyObservers();
this._notifyStripOnShareObservers();
}
_getListFromSharedData() {
let data = Services.cpmm.sharedData.get(SHARED_DATA_KEY);
return data ? [data] : [];
}
_notifyObservers(observer) {
let stripEntries = new Set([
...this.prefStripList,
...this.remoteStripList,
]);
let allowEntries = new Set([
...this.prefAllowList,
...this.remoteAllowList,
]);
let stripEntriesAsString = Array.from(stripEntries).join(" ").toLowerCase();
let allowEntriesAsString = Array.from(allowEntries).join(",").toLowerCase();
let observers = observer ? [observer] : this.observers;
if (observer || this.observers.size) {
lazy.logger.debug("_notifyObservers", {
observerCount: observers.length,
runObserverAfterRegister: observer != null,
stripEntriesAsString,
allowEntriesAsString,
});
}
for (let obs of observers) {
obs.onQueryStrippingListUpdate(
stripEntriesAsString,
allowEntriesAsString
);
}
}
async _notifyStripOnShareObservers(observer) {
this.stripOnShareParams = await lazy.StripOnShareList;
// Changing to different test list allows us to test
// site specific params as the websites that current have
// site specific params cannot be opened in a test env
if (lazy.testStripOnShare) {
this.stripOnShareParams = this.#stripOnShareTestList;
}
if (!this.stripOnShareParams) {
lazy.logger.error("StripOnShare list is undefined");
return;
}
// Add the qps params to the global rules of the strip-on-share list.
let qpsParams = [...this.prefStripList, ...this.remoteStripList].map(
param => param.toLowerCase()
);
this.stripOnShareParams.global.queryParams.push(...qpsParams);
// Getting rid of duplicates.
this.stripOnShareParams.global.queryParams = [
...new Set(this.stripOnShareParams.global.queryParams),
];
// Build an array of StripOnShareRules.
let rules = Object.values(this.stripOnShareParams);
let stringifiedRules = [];
// We need to stringify the rules so later we can initialise WebIDL dictionaries from them.
// The dictionaries init call needs stringified json.
rules.forEach(rule => {
stringifiedRules.push(JSON.stringify(rule));
});
let observers = observer ? new Set([observer]) : this.stripOnShareObservers;
if (observers.size) {
lazy.logger.debug("_notifyStripOnShareObservers", {
observerCount: observers.size,
runObserverAfterRegister: observer != null,
stringifiedRules,
});
}
for (let obs of observers) {
obs.onStripOnShareUpdate(stringifiedRules);
}
}
async registerAndRunObserver(observer) {
lazy.logger.debug("registerAndRunObserver", {
isInitialized: this.#isInitialized,
pendingInit: this.#pendingInit,
});
await this.#init();
this.observers.add(observer);
this._notifyObservers(observer);
}
async registerAndRunObserverStripOnShare(observer) {
lazy.logger.debug("registerAndRunObserverStripOnShare", {
isInitialized: this.#isInitialized,
pendingInit: this.#pendingInit,
});
await this.#init();
this.stripOnShareObservers.add(observer);
await this._notifyStripOnShareObservers(observer);
}
async unregisterObserver(observer) {
this.observers.delete(observer);
if (this.hasObservers) {
lazy.logger.debug("Last observer unregistered, shutting down...");
await this.#shutdown();
}
}
async unregisterStripOnShareObserver(observer) {
this.stripOnShareObservers.delete(observer);
if (this.hasObservers) {
lazy.logger.debug("Last observer unregistered, shutting down...");
await this.#shutdown();
}
}
async clearLists() {
if (!this.isParentProcess) {
return;
}
// Ensure init.
await this.#init();
// Clear the lists of remote settings.
this._onRemoteSettingsUpdate([]);
// Clear the user pref for the strip list. The pref change observer will
// handle the rest of the work.
Services.prefs.clearUserPref(PREF_STRIP_LIST_NAME);
Services.prefs.clearUserPref(PREF_ALLOW_LIST_NAME);
}
observe(subject, topic, data) {
lazy.logger.debug("observe", { topic, data });
switch (topic) {
case "xpcom-shutdown":
this.#shutdown();
break;
case "nsPref:changed":
let prefValue = Services.prefs.getStringPref(data, "");
this._onPrefUpdate(data, prefValue);
break;
default:
console.error(`Unexpected event ${topic}`);
}
}
handleEvent(event) {
if (event.type != "change") {
return;
}
if (!event.changedKeys.includes(SHARED_DATA_KEY)) {
return;
}
let data = this._getListFromSharedData();
this._onRemoteSettingsUpdate(data);
this._notifyObservers();
}
async testWaitForInit() {
if (this.#pendingInit) {
await this.#pendingInit;
}
return this.#isInitialized;
}
}
|