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"); |
anton
2016/09/30 06:51:03
Please make sure it does not interfere with https:
diegocarloslima
2016/10/26 13:48:04
Yes, they don't conflict to each other, altough th
|
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"; |
+ |
+function initListeners() |
+{ |
+ 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() |
+{ |
+ setBoolPref(shouldSaveFiltersPref, false); |
+} |
+ |
+function getBoolPref(name) |
+{ |
+ 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() |
+ { |
+ 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}; |
} |