Bug 1018240 - Part 1: reinitialize nsSearchService when the browser locale changes. r=adw

This patch makes nsSearchService watch for changes in the
general.useragent.locale pref, asynchronously reiniting itself when the pref
changes.

reinit-complete and reinit-failed events are broadcast on the usual search
service topic.
This commit is contained in:
Richard Newman 2014-07-21 10:54:55 -07:00
parent 5551861c89
commit 6b1cd2581c

View File

@ -143,6 +143,7 @@ const EMPTY_DOC = "<?xml version=\"1.0\"?>\n" +
"/>";
const BROWSER_SEARCH_PREF = "browser.search.";
const LOCALE_PREF = "general.useragent.locale";
const USER_DEFINED = "{searchTerms}";
@ -590,13 +591,12 @@ function sherlockBytesToLines(aBytes, aCharsetCode) {
* exists in nsHttpHandler.cpp when building the UA string.
*/
function getLocale() {
const localePref = "general.useragent.locale";
var locale = getLocalizedPref(localePref);
let locale = getLocalizedPref(LOCALE_PREF);
if (locale)
return locale;
// Not localized
return Services.prefs.getCharPref(localePref);
// Not localized.
return Services.prefs.getCharPref(LOCALE_PREF);
}
/**
@ -3190,6 +3190,27 @@ SearchService.prototype = {
}.bind(this));
},
_asyncReInit: function () {
// Start by clearing the initialized state, so we don't abort early.
gInitialized = false;
// Clear the engines, too, so we don't stick with the stale ones.
this._engines = {};
this.__sortedEngines = null;
// Typically we'll re-init as a result of a pref observer,
// so signal to 'callers' that we're done.
return this._asyncLoadEngines()
.then(() => {
Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, "reinit-complete");
gInitialized = true;
},
(err) => {
LOG("Reinit failed: " + err);
Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, "reinit-failed");
});
},
_readCacheFile: function SRCH_SVC__readCacheFile(aFile) {
let stream = Cc["@mozilla.org/network/file-input-stream;1"].
createInstance(Ci.nsIFileInputStream);
@ -4150,11 +4171,21 @@ SearchService.prototype = {
break;
case "nsPref:changed":
if (aVerb == LOCALE_PREF) {
// Locale changed. Re-init. We rely on observers, because we can't
// return this promise to anyone.
this._asyncReInit();
break;
}
let currPref = BROWSER_SEARCH_PREF + "selectedEngine";
let defPref = BROWSER_SEARCH_PREF + "defaultenginename";
if (aVerb == currPref && !this._changingCurrentEngine) {
this._setEngineByPref("currentEngine", currPref);
} else if (aVerb == defPref && !this._changingDefaultEngine) {
break;
}
let defPref = BROWSER_SEARCH_PREF + "defaultenginename";
if (aVerb == defPref && !this._changingDefaultEngine) {
this._setEngineByPref("defaultEngine", defPref);
}
break;
@ -4205,6 +4236,7 @@ SearchService.prototype = {
Services.obs.addObserver(this, QUIT_APPLICATION_TOPIC, false);
Services.prefs.addObserver(BROWSER_SEARCH_PREF + "defaultenginename", this, false);
Services.prefs.addObserver(BROWSER_SEARCH_PREF + "selectedEngine", this, false);
Services.prefs.addObserver(LOCALE_PREF, this, false);
// The current stage of shutdown. Used to help analyze crash
// signatures in case of shutdown timeout.
@ -4252,6 +4284,7 @@ SearchService.prototype = {
Services.obs.removeObserver(this, QUIT_APPLICATION_TOPIC);
Services.prefs.removeObserver(BROWSER_SEARCH_PREF + "defaultenginename", this);
Services.prefs.removeObserver(BROWSER_SEARCH_PREF + "selectedEngine", this);
Services.prefs.removeObserver(LOCALE_PREF, this);
},
QueryInterface: function SRCH_SVC_QI(aIID) {