Bug 1955948 - Update url classifier exception list service for new exception list logic. r=timhuang
Also switches it over to the new RemoteSettings url-classifier-exceptions collection. Differential Revision: https://phabricator.services.mozilla.com/D243198
This commit is contained in:
@@ -10,7 +10,7 @@ ChromeUtils.defineESModuleGetters(lazy, {
|
||||
RemoteSettings: "resource://services-settings/remote-settings.sys.mjs",
|
||||
});
|
||||
|
||||
const COLLECTION_NAME = "url-classifier-skip-urls";
|
||||
const COLLECTION_NAME = "url-classifier-exceptions";
|
||||
|
||||
class Feature {
|
||||
constructor(name, prefName) {
|
||||
@@ -48,17 +48,67 @@ class Feature {
|
||||
onRemoteSettingsUpdate(entries) {
|
||||
this.remoteEntries = [];
|
||||
|
||||
for (let entry of entries) {
|
||||
if (entry.feature == this.name) {
|
||||
this.remoteEntries.push(entry.pattern.toLowerCase());
|
||||
for (let jsEntry of entries) {
|
||||
let { classifierFeatures } = jsEntry;
|
||||
if (classifierFeatures.includes(this.name)) {
|
||||
let entry = Feature.rsObjectToEntry(jsEntry);
|
||||
if (entry) {
|
||||
this.remoteEntries.push(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a JS object from RemoteSettings to an nsIUrlClassifierExceptionListEntry.
|
||||
* @param {Object} rsObject - The JS object from RemoteSettings to convert.
|
||||
* @returns {nsIUrlClassifierExceptionListEntry} The converted nsIUrlClassifierExceptionListEntry.
|
||||
*/
|
||||
static rsObjectToEntry(rsObject) {
|
||||
let entry = Cc[
|
||||
"@mozilla.org/url-classifier/exception-list-entry;1"
|
||||
].createInstance(Ci.nsIUrlClassifierExceptionListEntry);
|
||||
|
||||
let {
|
||||
urlPattern,
|
||||
topLevelUrlPattern = "",
|
||||
isPrivateBrowsingOnly = false,
|
||||
filterContentBlockingCategories = [],
|
||||
classifierFeatures = [],
|
||||
} = rsObject;
|
||||
|
||||
try {
|
||||
entry.init(
|
||||
urlPattern,
|
||||
topLevelUrlPattern,
|
||||
isPrivateBrowsingOnly,
|
||||
filterContentBlockingCategories,
|
||||
classifierFeatures
|
||||
);
|
||||
} catch (e) {
|
||||
console.error(
|
||||
"Error initializing url classifier exception list entry " + e.message,
|
||||
e,
|
||||
rsObject
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
notifyObservers(observer = null) {
|
||||
let entries = [];
|
||||
if (this.prefValue) {
|
||||
entries = this.prefValue.split(",");
|
||||
for (let prefEntry of this.prefValue.split(",")) {
|
||||
let entry = Feature.rsObjectToEntry({
|
||||
urlPattern: prefEntry,
|
||||
classifierFeatures: [this.name],
|
||||
});
|
||||
if (entry) {
|
||||
entries.push(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.remoteEntries) {
|
||||
@@ -67,12 +117,28 @@ class Feature {
|
||||
}
|
||||
}
|
||||
|
||||
let entriesAsString = entries.join(",").toLowerCase();
|
||||
// Construct nsIUrlClassifierExceptionList with all entries that belong to
|
||||
// this feature.
|
||||
let list = Cc[
|
||||
"@mozilla.org/url-classifier/exception-list;1"
|
||||
].createInstance(Ci.nsIUrlClassifierExceptionList);
|
||||
for (let entry of entries) {
|
||||
try {
|
||||
list.addEntry(entry);
|
||||
} catch (e) {
|
||||
console.error(
|
||||
"Error adding url classifier exception list entry " + e.message,
|
||||
e,
|
||||
entry
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (observer) {
|
||||
observer.onExceptionListUpdate(entriesAsString);
|
||||
observer.onExceptionListUpdate(list);
|
||||
} else {
|
||||
for (let obs of this.observers) {
|
||||
obs.onExceptionListUpdate(entriesAsString);
|
||||
obs.onExceptionListUpdate(list);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user