Bug 1377559 - Should store value of preference browser.safebrowsing.debug to reuse
MozReview-Commit-ID: 1yWe7wB0ARl
--- a/toolkit/components/url-classifier/SafeBrowsing.jsm
+++ b/toolkit/components/url-classifier/SafeBrowsing.jsm
@@ -5,20 +5,22 @@
this.EXPORTED_SYMBOLS = ["SafeBrowsing"];
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm");
+const PREF_DEBUG_ENABLED = "browser.safebrowsing.debug";
+let loggingEnabled = false;
+
// Log only if browser.safebrowsing.debug is true
function log(...stuff) {
- let logging = Services.prefs.getBoolPref("browser.safebrowsing.debug", false);
- if (!logging) {
+ if (!loggingEnabled) {
return;
}
var d = new Date();
let msg = "SafeBrowsing: " + d.toTimeString() + ": " + stuff.join(" ");
dump(Services.urlFormatter.trimSensitiveURLs(msg) + "\n");
}
@@ -185,23 +187,29 @@ this.SafeBrowsing = {
return reportUrl;
},
observe: function(aSubject, aTopic, aData) {
// skip nextupdatetime and lastupdatetime
if (aData.indexOf("lastupdatetime") >= 0 || aData.indexOf("nextupdatetime") >= 0) {
return;
}
+
+ if (aData == PREF_DEBUG_ENABLED) {
+ loggingEnabled = Services.prefs.getBoolPref(PREF_DEBUG_ENABLED);
+ return;
+ }
+
this.readPrefs();
},
readPrefs: function() {
+ loggingEnabled = Services.prefs.getBoolPref(PREF_DEBUG_ENABLED);
log("reading prefs");
- this.debug = Services.prefs.getBoolPref("browser.safebrowsing.debug");
this.phishingEnabled = Services.prefs.getBoolPref("browser.safebrowsing.phishing.enabled");
this.malwareEnabled = Services.prefs.getBoolPref("browser.safebrowsing.malware.enabled");
this.trackingEnabled = Services.prefs.getBoolPref("privacy.trackingprotection.enabled") || Services.prefs.getBoolPref("privacy.trackingprotection.pbmode.enabled");
this.blockedEnabled = Services.prefs.getBoolPref("browser.safebrowsing.blockedURIs.enabled");
this.trackingAnnotations = Services.prefs.getBoolPref("privacy.trackingprotection.annotate_channels");
this.flashBlockEnabled = Services.prefs.getBoolPref("plugins.flashBlock.enabled");
let flashAllowTable, flashAllowExceptTable, flashTable,
@@ -258,17 +266,17 @@ this.SafeBrowsing = {
for (let child of children) {
log("Child: " + child);
let prefComponents = child.split(".");
let providerName = prefComponents[0];
this.providers[providerName] = {};
}
- if (this.debug) {
+ if (loggingEnabled) {
let providerStr = "";
Object.keys(this.providers).forEach(function(provider) {
if (providerStr === "") {
providerStr = provider;
} else {
providerStr += ", " + provider;
}
});
--- a/toolkit/components/url-classifier/content/listmanager.js
+++ b/toolkit/components/url-classifier/content/listmanager.js
@@ -14,21 +14,23 @@ Cu.import("resource://gre/modules/Servic
//
// TODO more comprehensive update tests, for example add unittest check
// that the listmanagers tables are properly written on updates
// Lower and upper limits on the server-provided polling frequency
const minDelayMs = 5 * 60 * 1000;
const maxDelayMs = 24 * 60 * 60 * 1000;
const defaultUpdateIntervalMs = 30 * 60 * 1000;
+const PREF_DEBUG_ENABLED = "browser.safebrowsing.debug";
+
+let loggingEnabled = false;
// Log only if browser.safebrowsing.debug is true
this.log = function log(...stuff) {
- var debug = Services.prefs.getBoolPref("browser.safebrowsing.debug");
- if (!debug) {
+ if (!loggingEnabled) {
return;
}
var d = new Date();
let msg = "listmanager: " + d.toTimeString() + ": " + stuff.join(" ");
msg = Services.urlFormatter.trimSensitiveURLs(msg);
Services.console.logStringMessage(msg);
dump(msg + "\n");
@@ -36,16 +38,18 @@ this.log = function log(...stuff) {
/**
* A ListManager keeps track of black and white lists and knows
* how to update them.
*
* @constructor
*/
this.PROT_ListManager = function PROT_ListManager() {
+ loggingEnabled = Services.prefs.getBoolPref(PREF_DEBUG_ENABLED);
+
log("Initializing list manager");
this.updateInterval = defaultUpdateIntervalMs;
// A map of tableNames to objects of type
// { updateUrl: <updateUrl>, gethashUrl: <gethashUrl> }
this.tablesData = {};
// A map of updateUrls to maps of tables requiring updates, e.g.
// { safebrowsing-update-url: { goog-phish-shavar: true,
@@ -57,16 +61,17 @@ this.PROT_ListManager = function PROT_Li
// are reset when enabling/disabling updates or on update callbacks (update
// success, update failure, download error).
this.updateCheckers_ = {};
this.requestBackoffs_ = {};
this.dbService_ = Cc["@mozilla.org/url-classifier/dbservice;1"]
.getService(Ci.nsIUrlClassifierDBService);
Services.obs.addObserver(this, "quit-application");
+ Services.prefs.addObserver(PREF_DEBUG_ENABLED, this);
}
/**
* Register a new table table
* @param tableName - the name of the table
* @param updateUrl - the url for updating the table
* @param gethashUrl - the url for fetching hash completions
* @returns true if the table could be created; false otherwise
@@ -95,27 +100,41 @@ PROT_ListManager.prototype.registerTable
60*60*1000 /* request time, 60 min */);
}
this.needsUpdate_[updateUrl][tableName] = false;
return true;
}
/**
- * quit-application callback
* Delete all of our data tables which seem to leak otherwise.
+ * Remove observers
*/
+PROT_ListManager.prototype.shutdown_ = function() {
+ this.stopUpdateCheckers();
+ for (var name in this.tablesData) {
+ delete this.tablesData[name];
+ }
+ Services.obs.removeObserver(this, "quit-application");
+ Services.prefs.removeObserver(PREF_DEBUG_ENABLED, this);
+}
+/**
+ * xpcom-shutdown callback
+ */
PROT_ListManager.prototype.observe = function(aSubject, aTopic, aData) {
- if (aTopic == "quit-application") {
- this.stopUpdateCheckers();
- for (var name in this.tablesData) {
- delete this.tablesData[name];
+ switch (aTopic) {
+ case "quit-application":
+ this.shutdown_();
+ break;
+ case "nsPref:changed":
+ if (aData == PREF_DEBUG_ENABLED) {
+ loggingEnabled = Services.prefs.getBoolPref(PREF_DEBUG_ENABLED);
}
- Services.obs.removeObserver(this, "quit-application");
+ break;
}
}
PROT_ListManager.prototype.getGethashUrl = function(tableName) {
if (this.tablesData[tableName] && this.tablesData[tableName].gethashUrl) {
return this.tablesData[tableName].gethashUrl;
}
--- a/toolkit/components/url-classifier/nsUrlClassifierHashCompleter.js
+++ b/toolkit/components/url-classifier/nsUrlClassifierHashCompleter.js
@@ -10,33 +10,35 @@ const Cu = Components.utils;
// COMPLETE_LENGTH and PARTIAL_LENGTH copied from nsUrlClassifierDBService.h,
// they correspond to the length, in bytes, of a hash prefix and the total
// hash.
const COMPLETE_LENGTH = 32;
const PARTIAL_LENGTH = 4;
// Upper limit on the server response minimumWaitDuration
const MIN_WAIT_DURATION_MAX_VALUE = 24 * 60 * 60 * 1000;
+const PREF_DEBUG_ENABLED = "browser.safebrowsing.debug";
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
XPCOMUtils.defineLazyServiceGetter(this, 'gDbService',
'@mozilla.org/url-classifier/dbservice;1',
'nsIUrlClassifierDBService');
XPCOMUtils.defineLazyServiceGetter(this, 'gUrlUtil',
'@mozilla.org/url-classifier/utils;1',
'nsIUrlClassifierUtils');
+let loggingEnabled = false;
+
// Log only if browser.safebrowsing.debug is true
function log(...stuff) {
- let logging = Services.prefs.getBoolPref("browser.safebrowsing.debug", false);
- if (!logging) {
+ if (!loggingEnabled) {
return;
}
var d = new Date();
let msg = "hashcompleter: " + d.toTimeString() + ": " + stuff.join(" ");
dump(Services.urlFormatter.trimSensitiveURLs(msg) + "\n");
}
@@ -176,17 +178,17 @@ function HashCompleter() {
// Whether we have been informed of a shutdown by the shutdown event.
this._shuttingDown = false;
// A map of gethash URLs to next gethash time in miliseconds
this._nextGethashTimeMs = {};
Services.obs.addObserver(this, "quit-application");
-
+ Services.prefs.addObserver(PREF_DEBUG_ENABLED, this);
}
HashCompleter.prototype = {
classID: Components.ID("{9111de73-9322-4bfc-8b65-2b727f3e6ec8}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIUrlClassifierHashCompleter,
Ci.nsIRunnable,
Ci.nsIObserver,
Ci.nsISupportsWeakReference,
@@ -285,19 +287,26 @@ HashCompleter.prototype = {
// Notifies the RequestBackoff of a new request so we can throttle based on
// max requests/time period. This must be called before a channel is opened,
// and finishRequest must be called once the response is received.
noteRequest: function(aGethashUrl) {
return this._backoffs[aGethashUrl].noteRequest();
},
observe: function HC_observe(aSubject, aTopic, aData) {
- if (aTopic == "quit-application") {
+ switch (aTopic) {
+ case "quit-application":
this._shuttingDown = true;
Services.obs.removeObserver(this, "quit-application");
+ break;
+ case "nsPref:changed":
+ if (aData == PREF_DEBUG_ENABLED) {
+ loggingEnabled = Services.prefs.getBoolPref(PREF_DEBUG_ENABLED);
+ }
+ break;
}
},
};
function HashCompleterRequest(aCompleter, aGethashUrl) {
// HashCompleter object that created this HashCompleterRequest.
this._completer = aCompleter;
// The internal set of hashes and callbacks that this request corresponds to.