Bug 726279 - Generalize use of Services.jsm in nsSearchService. r=dteller, dao

This commit is contained in:
Raymond Lee 2012-04-28 11:09:55 -04:00
parent e6abaf3f94
commit 41d8e1c25e

View File

@ -220,18 +220,6 @@ function isUsefulLine(aLine) {
return !(/^\s*($|#)/i.test(aLine));
}
__defineGetter__("gObsSvc", function() {
delete this.gObsSvc;
return this.gObsSvc = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
});
__defineGetter__("gPrefSvc", function() {
delete this.gPrefSvc;
return this.gPrefSvc = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
});
__defineGetter__("FileUtils", function() {
delete this.FileUtils;
Components.utils.import("resource://gre/modules/FileUtils.jsm");
@ -260,9 +248,7 @@ const SEARCH_LOG_PREFIX = "*** Search: ";
*/
function DO_LOG(aText) {
dump(SEARCH_LOG_PREFIX + aText + "\n");
var consoleService = Cc["@mozilla.org/consoleservice;1"].
getService(Ci.nsIConsoleService);
consoleService.logStringMessage(aText);
Services.console.logStringMessage(aText);
}
#ifdef DEBUG
@ -539,15 +525,11 @@ function makeURI(aURLSpec, aCharset) {
* @param aKey
* The directory service key indicating the directory to get.
*/
let _dirSvc = null;
function getDir(aKey, aIFace) {
if (!aKey)
FAIL("getDir requires a directory key!");
if (!_dirSvc)
_dirSvc = Cc["@mozilla.org/file/directory_service;1"].
getService(Ci.nsIProperties);
return _dirSvc.get(aKey, aIFace || Ci.nsIFile);
return Services.dirsvc.get(aKey, aIFace || Ci.nsIFile);
}
/**
@ -685,7 +667,7 @@ function getLocale() {
return locale;
// Not localized
return gPrefSvc.getCharPref(localePref);
return Services.prefs.getCharPref(localePref);
}
/**
@ -697,7 +679,7 @@ function getLocale() {
function getLocalizedPref(aPrefName, aDefault) {
const nsIPLS = Ci.nsIPrefLocalizedString;
try {
return gPrefSvc.getComplexValue(aPrefName, nsIPLS).data;
return Services.prefs.getComplexValue(aPrefName, nsIPLS).data;
} catch (ex) {}
return aDefault;
@ -714,7 +696,7 @@ function setLocalizedPref(aPrefName, aValue) {
var pls = Components.classes["@mozilla.org/pref-localizedstring;1"]
.createInstance(Ci.nsIPrefLocalizedString);
pls.data = aValue;
gPrefSvc.setComplexValue(aPrefName, nsIPLS, pls);
Services.prefs.setComplexValue(aPrefName, nsIPLS, pls);
} catch (ex) {}
}
@ -726,7 +708,7 @@ function setLocalizedPref(aPrefName, aValue) {
*/
function getBoolPref(aName, aDefault) {
try {
return gPrefSvc.getBoolPref(aName);
return Services.prefs.getBoolPref(aName);
} catch (ex) {
return aDefault;
}
@ -775,7 +757,7 @@ function sanitizeName(aName) {
* The name of the pref.
**/
function getMozParamPref(prefName)
gPrefSvc.getCharPref(BROWSER_SEARCH_PREF + "param." + prefName);
Services.prefs.getCharPref(BROWSER_SEARCH_PREF + "param." + prefName);
/**
* Notifies watchers of SEARCH_ENGINE_TOPIC about changes to an engine or to
@ -792,7 +774,7 @@ let gEnginesLoaded = false;
function notifyAction(aEngine, aVerb) {
if (gEnginesLoaded) {
LOG("NOTIFY: Engine: \"" + aEngine.name + "\"; Verb: \"" + aVerb + "\"");
gObsSvc.notifyObservers(aEngine, SEARCH_ENGINE_TOPIC, aVerb);
Services.obs.notifyObservers(aEngine, SEARCH_ENGINE_TOPIC, aVerb);
}
}
@ -833,7 +815,7 @@ function ParamSubstitution(aParamValue, aSearchTerms, aEngine) {
var distributionID = MOZ_DISTRIBUTION_ID;
try {
distributionID = gPrefSvc.getCharPref(BROWSER_SEARCH_PREF + "distributionID");
distributionID = Services.prefs.getCharPref(BROWSER_SEARCH_PREF + "distributionID");
}
catch (ex) { }
@ -1291,9 +1273,7 @@ Engine.prototype = {
},
_confirmAddEngine: function SRCH_SVC_confirmAddEngine() {
var sbs = Cc["@mozilla.org/intl/stringbundle;1"].
getService(Ci.nsIStringBundleService);
var stringBundle = sbs.createBundle(SEARCH_BUNDLE);
var stringBundle = Services.strings.createBundle(SEARCH_BUNDLE);
var titleMessage = stringBundle.GetStringFromName("addEngineConfirmTitle");
// Display only the hostname portion of the URL.
@ -1307,8 +1287,7 @@ Engine.prototype = {
var addButtonLabel =
stringBundle.GetStringFromName("addEngineAddButtonLabel");
var ps = Cc["@mozilla.org/embedcomp/prompt-service;1"].
getService(Ci.nsIPromptService);
var ps = Services.prompt;
var buttonFlags = (ps.BUTTON_TITLE_IS_STRING * ps.BUTTON_POS_0) +
(ps.BUTTON_TITLE_CANCEL * ps.BUTTON_POS_1) +
ps.BUTTON_POS_0_DEFAULT;
@ -1344,13 +1323,10 @@ Engine.prototype = {
LOG("updating " + aEngine._engineToUpdate.name + " failed");
return;
}
var sbs = Cc["@mozilla.org/intl/stringbundle;1"].
getService(Ci.nsIStringBundleService);
var brandBundle = sbs.createBundle(BRAND_BUNDLE);
var brandBundle = Services.strings.createBundle(BRAND_BUNDLE);
var brandName = brandBundle.GetStringFromName("brandShortName");
var searchBundle = sbs.createBundle(SEARCH_BUNDLE);
var searchBundle = Services.strings.createBundle(SEARCH_BUNDLE);
var msgStringName = aErrorString || "error_loading_engine_msg2";
var titleStringName = aTitleString || "error_loading_engine_title";
var title = searchBundle.GetStringFromName(titleStringName);
@ -1358,9 +1334,7 @@ Engine.prototype = {
[brandName, aEngine._location],
2);
var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
getService(Ci.nsIWindowWatcher);
ww.getNewPrompter(null).alert(title, text);
Services.ww.getNewPrompter(null).alert(title, text);
}
if (!aBytes) {
@ -1406,9 +1380,7 @@ Engine.prototype = {
// engine load, then we display a "this is a duplicate engine" prompt,
// otherwise we fail silently.
if (!engineToUpdate) {
var ss = Cc["@mozilla.org/browser/search-service;1"].
getService(Ci.nsIBrowserSearchService);
if (ss.getEngineByName(aEngine.name)) {
if (Services.search.getEngineByName(aEngine.name)) {
if (aEngine._confirm)
onError("error_duplicate_engine_msg", "error_invalid_engine_title");
@ -1698,8 +1670,7 @@ Engine.prototype = {
},
_isDefaultEngine: function SRCH_ENG__isDefaultEngine() {
let defaultPrefB = gPrefSvc.QueryInterface(Ci.nsIPrefService)
.getDefaultBranch(BROWSER_SEARCH_PREF);
let defaultPrefB = Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF);
let nsIPLS = Ci.nsIPrefLocalizedString;
let defaultEngine;
try {
@ -2577,8 +2548,7 @@ SearchService.prototype = {
let cache = {};
let locale = getLocale();
let buildID = Cc["@mozilla.org/xre/app-info;1"].
getService(Ci.nsIXULAppInfo).platformBuildID;
let buildID = Services.appinfo.platformBuildID;
// Allows us to force a cache refresh should the cache format change.
cache.version = CACHE_VERSION;
@ -2695,8 +2665,7 @@ SearchService.prototype = {
function notInToLoad(aCachePath, aIndex)
aCachePath != toLoad[aIndex].path;
let buildID = Cc["@mozilla.org/xre/app-info;1"].
getService(Ci.nsIXULAppInfo).platformBuildID;
let buildID = Services.appinfo.platformBuildID;
let cachePaths = [path for (path in cache.directories)];
let rebuildCache = !cache.directories ||
@ -2937,7 +2906,7 @@ SearchService.prototype = {
let rootURIPref = ""
try {
rootURIPref = gPrefSvc.getCharPref(BROWSER_SEARCH_PREF + "jarURIs");
rootURIPref = Services.prefs.getCharPref(BROWSER_SEARCH_PREF + "jarURIs");
} catch (ex) {}
if (!rootURIPref) {
@ -3003,7 +2972,7 @@ SearchService.prototype = {
// Set the useDB pref to indicate that from now on we should use the order
// information stored in the database.
gPrefSvc.setBoolPref(BROWSER_SEARCH_PREF + "useDBForOrder", true);
Services.prefs.setBoolPref(BROWSER_SEARCH_PREF + "useDBForOrder", true);
var engines = this._getSortedEngines(true);
@ -3062,10 +3031,10 @@ SearchService.prototype = {
try {
var extras =
gPrefSvc.getChildList(BROWSER_SEARCH_PREF + "order.extra.");
Services.prefs.getChildList(BROWSER_SEARCH_PREF + "order.extra.");
for each (prefName in extras) {
engineName = gPrefSvc.getCharPref(prefName);
engineName = Services.prefs.getCharPref(prefName);
engine = this._engines[engineName];
if (!engine || engine.name in addedEngines)
@ -3260,10 +3229,10 @@ SearchService.prototype = {
// First, look at the "browser.search.order.extra" branch.
try {
var extras = gPrefSvc.getChildList(BROWSER_SEARCH_PREF + "order.extra.");
var extras = Services.prefs.getChildList(BROWSER_SEARCH_PREF + "order.extra.");
for each (var prefName in extras) {
engineName = gPrefSvc.getCharPref(prefName);
engineName = Services.prefs.getCharPref(prefName);
if (!(engineName in engineOrder))
engineOrder[engineName] = i++;
@ -3489,7 +3458,7 @@ SearchService.prototype = {
var currentEnginePref = BROWSER_SEARCH_PREF + "selectedEngine";
if (this._currentEngine == this.defaultEngine) {
gPrefSvc.clearUserPref(currentEnginePref);
Services.prefs.clearUserPref(currentEnginePref);
}
else {
setLocalizedPref(currentEnginePref, this._currentEngine.name);
@ -3574,13 +3543,13 @@ SearchService.prototype = {
},
_addObservers: function SRCH_SVC_addObservers() {
gObsSvc.addObserver(this, SEARCH_ENGINE_TOPIC, false);
gObsSvc.addObserver(this, QUIT_APPLICATION_TOPIC, false);
Services.obs.addObserver(this, SEARCH_ENGINE_TOPIC, false);
Services.obs.addObserver(this, QUIT_APPLICATION_TOPIC, false);
},
_removeObservers: function SRCH_SVC_removeObservers() {
gObsSvc.removeObserver(this, SEARCH_ENGINE_TOPIC);
gObsSvc.removeObserver(this, QUIT_APPLICATION_TOPIC);
Services.obs.removeObserver(this, SEARCH_ENGINE_TOPIC);
Services.obs.removeObserver(this, QUIT_APPLICATION_TOPIC);
},
QueryInterface: function SRCH_SVC_QI(aIID) {
@ -3787,9 +3756,9 @@ var engineMetadataService = {
let callback = function(result) {
if (Components.isSuccessCode(result)) {
gObsSvc.notifyObservers(null,
SEARCH_SERVICE_TOPIC,
SEARCH_SERVICE_METADATA_WRITTEN);
Services.obs.notifyObservers(null,
SEARCH_SERVICE_TOPIC,
SEARCH_SERVICE_METADATA_WRITTEN);
}
LOG("epsWriteCommit: done " + result);
};
@ -3814,9 +3783,7 @@ const SEARCH_UPDATE_LOG_PREFIX = "*** Search update: ";
function ULOG(aText) {
if (getBoolPref(BROWSER_SEARCH_PREF + "update.log", false)) {
dump(SEARCH_UPDATE_LOG_PREFIX + aText + "\n");
var consoleService = Cc["@mozilla.org/consoleservice;1"].
getService(Ci.nsIConsoleService);
consoleService.logStringMessage(aText);
Services.console.logStringMessage(aText);
}
}