Index: adblockplus/Api.jsm |
=================================================================== |
--- a/adblockplus/Api.jsm |
+++ b/adblockplus/Api.jsm |
@@ -32,23 +32,72 @@ function require(module) |
{ |
let result = {}; |
result.wrappedJSObject = result; |
Services.obs.notifyObservers(result, "adblockplus-require", module); |
return result.exports; |
} |
let {Filter} = require("filterClasses"); |
+let {FilterNotifier} = require("filterNotifier"); |
let {FilterStorage} = require("filterStorage"); |
let {defaultMatcher} = require("matcher"); |
let {Prefs} = require("prefs"); |
let {Subscription, SpecialSubscription, RegularSubscription, DownloadableSubscription, ExternalSubscription} = require("subscriptionClasses"); |
let {Synchronizer} = require("synchronizer"); |
let {UI} = require("ui"); |
+let shouldSaveFiltersPref = "should_save_filters"; |
Felix Dahlke
2016/12/13 11:25:09
I think this name is so generic that it could conf
|
+ |
+function initListeners() |
Felix Dahlke
2016/12/13 11:25:09
Nit: Maybe call this `initFilterListeners` or some
|
+{ |
+ FilterNotifier.on("filter.added", onFiltersChanged); |
+ FilterNotifier.on("filter.removed", onFiltersChanged); |
+ FilterNotifier.on("subscription.added", onFiltersChanged); |
+ FilterNotifier.on("subscription.removed", onFiltersChanged); |
+ FilterNotifier.on("save", onSave); |
+} |
+ |
+function onFiltersChanged() |
+{ |
+ setBoolPref(shouldSaveFiltersPref, true); |
+} |
+ |
+function onSave() |
Felix Dahlke
2016/12/13 11:25:09
Nit: Shouldn't this be called `onFiltersSave` for
|
+{ |
+ setBoolPref(shouldSaveFiltersPref, false); |
+} |
+ |
+function getBoolPref(name) |
Felix Dahlke
2016/12/13 11:25:09
It's a bit of a shame that we can apparently not u
|
+{ |
+ let branch = getPrefsBranch(); |
+ try |
+ { |
+ return branch.getBoolPref(name); |
+ } |
+ catch (e) |
+ { |
+ return null; |
+ } |
+} |
+ |
+function setBoolPref(name, value) |
+{ |
+ let branch = getPrefsBranch(); |
+ branch.setBoolPref(name, value); |
+ Services.prefs.savePrefFile(null); |
+} |
+ |
+function getPrefsBranch() |
+{ |
+ let {addonRoot, addonName} = require("info"); |
+ let branchName = "extensions." + addonName + "."; |
+ return Services.prefs.getBranch(branchName); |
+} |
+ |
function getWhitelistingFilter(url) |
{ |
let uriObject = Services.io.newURI(url, null, null); |
try |
{ |
return defaultMatcher.whitelist.matchesAny( |
uriObject.spec, "DOCUMENT", uriObject.host, false, null); |
} |
@@ -59,16 +108,20 @@ function getWhitelistingFilter(url) |
} |
var AdblockPlusApi = |
{ |
get filtersLoaded() |
{ |
return !FilterStorage._loading; |
}, |
+ get requestsSaved() |
Felix Dahlke
2016/12/13 11:25:09
Naming: Do we actually "save requests" here? Seems
|
+ { |
+ return !getBoolPref(shouldSaveFiltersPref) && !FilterStorage._saving && !FilterStorage._needsSave; |
+ }, |
get acceptableAdsEnabled() |
{ |
return FilterStorage.subscriptions.some( |
(subscription) => subscription.url == Prefs.subscriptions_exceptionsurl); |
}, |
set acceptableAdsEnabled(acceptableAdsEnabled) |
{ |
if (acceptableAdsEnabled != AdblockPlusApi.acceptableAdsEnabled) |
@@ -157,29 +210,33 @@ var AdblockPlusApi = |
if (filter.subscriptions.length) |
filter.disabled = true; |
filter = getWhitelistingFilter(url); |
} |
} |
}, |
initCommunication: function() |
{ |
+ initListeners(); |
+ |
Messaging.addListener((function(data) |
{ |
if (!data) |
return {"success": false, "error": "malformed request"}; |
if (data["action"] == "getFiltersLoaded") |
return {"success": true, "value": this.filtersLoaded}; |
if (!this.filtersLoaded) |
return {"success": false, "error": "filters not loaded"}; |
switch (data["action"]) |
{ |
+ case "getRequestsSaved": |
+ return {"success": true, "value": this.requestsSaved}; |
case "getAcceptableAdsEnabled": |
return {"success": true, "value": this.acceptableAdsEnabled}; |
case "setAcceptableAdsEnabled": |
if ("enable" in data) |
{ |
this.acceptableAdsEnabled = !!data["enable"]; |
return {"success": true}; |
} |