2019-03-09 01:21:32 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
2020-07-07 19:17:11 +03:00
|
|
|
this.UrlClassifierExceptionListService = function() {};
|
2019-03-09 01:21:32 +03:00
|
|
|
|
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"RemoteSettings",
|
|
|
|
"resource://services-settings/remote-settings.js"
|
|
|
|
);
|
|
|
|
|
|
|
|
const COLLECTION_NAME = "url-classifier-skip-urls";
|
|
|
|
|
|
|
|
class Feature {
|
|
|
|
constructor(name, prefName) {
|
|
|
|
this.name = name;
|
|
|
|
this.prefName = prefName;
|
|
|
|
this.observers = new Set();
|
|
|
|
this.prefValue = null;
|
|
|
|
this.remoteEntries = null;
|
|
|
|
|
|
|
|
if (prefName) {
|
|
|
|
this.prefValue = Services.prefs.getStringPref(this.prefName, null);
|
|
|
|
Services.prefs.addObserver(prefName, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 09:16:49 +03:00
|
|
|
async addAndRunObserver(observer) {
|
2019-03-09 01:21:32 +03:00
|
|
|
this.observers.add(observer);
|
|
|
|
this.notifyObservers(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeObserver(observer) {
|
|
|
|
this.observers.delete(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
observe(subject, topic, data) {
|
|
|
|
if (topic != "nsPref:changed" || data != this.prefName) {
|
|
|
|
Cu.reportError(`Unexpected event ${topic} with ${data}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.prefValue = Services.prefs.getStringPref(this.prefName, null);
|
|
|
|
this.notifyObservers();
|
|
|
|
}
|
|
|
|
|
2019-03-09 01:21:48 +03:00
|
|
|
onRemoteSettingsUpdate(entries) {
|
|
|
|
this.remoteEntries = [];
|
2020-04-06 01:29:16 +03:00
|
|
|
|
2019-03-09 01:21:48 +03:00
|
|
|
for (let entry of entries) {
|
|
|
|
if (entry.feature == this.name) {
|
|
|
|
this.remoteEntries.push(entry.pattern.toLowerCase());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 01:21:32 +03:00
|
|
|
notifyObservers(observer = null) {
|
|
|
|
let entries = [];
|
|
|
|
if (this.prefValue) {
|
|
|
|
entries = this.prefValue.split(",");
|
|
|
|
}
|
|
|
|
|
2020-05-08 09:16:49 +03:00
|
|
|
if (this.remoteEntries) {
|
|
|
|
for (let entry of this.remoteEntries) {
|
|
|
|
entries.push(entry);
|
|
|
|
}
|
2019-03-09 01:21:32 +03:00
|
|
|
}
|
|
|
|
|
2019-04-04 20:38:09 +03:00
|
|
|
let entriesAsString = entries.join(",").toLowerCase();
|
2019-03-09 01:21:32 +03:00
|
|
|
if (observer) {
|
2020-07-07 19:17:11 +03:00
|
|
|
observer.onExceptionListUpdate(entriesAsString);
|
2019-03-09 01:21:32 +03:00
|
|
|
} else {
|
|
|
|
for (let obs of this.observers) {
|
2020-07-07 19:17:11 +03:00
|
|
|
obs.onExceptionListUpdate(entriesAsString);
|
2019-03-09 01:21:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 19:17:11 +03:00
|
|
|
UrlClassifierExceptionListService.prototype = {
|
2019-03-09 01:21:32 +03:00
|
|
|
classID: Components.ID("{b9f4fd03-9d87-4bfd-9958-85a821750ddc}"),
|
2020-07-07 19:17:11 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI([
|
Bug 1649221: Update ChromeUtils.generateQI callers to pass strings. r=mccr8,remote-protocol-reviewers,marionette-reviewers,perftest-reviewers,webcompat-reviewers,geckoview-reviewers,preferences-reviewers,agi,whimboo,Bebe,twisniewski
Differential Revision: https://phabricator.services.mozilla.com/D81594
2020-07-11 02:58:28 +03:00
|
|
|
"nsIUrlClassifierExceptionListService",
|
2020-07-07 19:17:11 +03:00
|
|
|
]),
|
2019-03-09 01:21:32 +03:00
|
|
|
|
|
|
|
features: {},
|
2019-03-09 01:21:48 +03:00
|
|
|
_initialized: false,
|
|
|
|
|
2020-04-06 01:29:16 +03:00
|
|
|
async lazyInit() {
|
2019-03-09 01:21:48 +03:00
|
|
|
if (this._initialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-06 01:29:16 +03:00
|
|
|
let rs = RemoteSettings(COLLECTION_NAME);
|
|
|
|
rs.on("sync", event => {
|
2019-03-09 01:21:48 +03:00
|
|
|
let {
|
|
|
|
data: { current },
|
|
|
|
} = event;
|
2020-04-06 01:29:16 +03:00
|
|
|
this.entries = current || [];
|
|
|
|
this.onUpdateEntries(current);
|
2019-03-09 01:21:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
this._initialized = true;
|
2020-04-06 01:29:16 +03:00
|
|
|
|
|
|
|
// 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).
|
|
|
|
this.entries = await rs.get();
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
// RemoteSettings.get() could return null, ensure passing a list to
|
|
|
|
// onUpdateEntries.
|
|
|
|
if (!this.entries) {
|
|
|
|
this.entries = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.onUpdateEntries(this.entries);
|
|
|
|
},
|
|
|
|
|
|
|
|
onUpdateEntries(entries) {
|
|
|
|
for (let key of Object.keys(this.features)) {
|
|
|
|
let feature = this.features[key];
|
|
|
|
feature.onRemoteSettingsUpdate(entries);
|
|
|
|
feature.notifyObservers();
|
|
|
|
}
|
2019-03-09 01:21:48 +03:00
|
|
|
},
|
2019-03-09 01:21:32 +03:00
|
|
|
|
2020-07-07 19:17:11 +03:00
|
|
|
registerAndRunExceptionListObserver(feature, prefName, observer) {
|
2020-04-06 01:29:16 +03:00
|
|
|
// 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.
|
2019-03-09 01:21:48 +03:00
|
|
|
this.lazyInit();
|
|
|
|
|
2019-03-09 01:21:32 +03:00
|
|
|
if (!this.features[feature]) {
|
2020-04-06 01:29:16 +03:00
|
|
|
let featureObj = new Feature(feature, prefName);
|
|
|
|
this.features[feature] = featureObj;
|
|
|
|
// If we've previously initialized, we need to pass the entries
|
|
|
|
// we already have to the new feature.
|
|
|
|
if (this.entries) {
|
|
|
|
featureObj.onRemoteSettingsUpdate(this.entries);
|
|
|
|
}
|
2019-03-09 01:21:32 +03:00
|
|
|
}
|
2020-05-08 09:16:49 +03:00
|
|
|
this.features[feature].addAndRunObserver(observer);
|
2019-03-09 01:21:32 +03:00
|
|
|
},
|
|
|
|
|
2020-07-07 19:17:11 +03:00
|
|
|
unregisterExceptionListObserver(feature, observer) {
|
2019-03-09 01:21:32 +03:00
|
|
|
if (!this.features[feature]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.features[feature].removeObserver(observer);
|
|
|
|
},
|
2020-04-10 00:28:43 +03:00
|
|
|
|
|
|
|
clear() {
|
|
|
|
this.features = {};
|
|
|
|
this._initialized = false;
|
|
|
|
this.entries = null;
|
|
|
|
},
|
2019-03-09 01:21:48 +03:00
|
|
|
};
|
2019-03-09 01:21:32 +03:00
|
|
|
|
2020-07-07 19:17:11 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["UrlClassifierExceptionListService"];
|