Bug 764270 - Generalize use of Services.jsm in nsSearchSuggestions. r=gavin, yoric

This commit is contained in:
Ekanan Ketunuti 2012-06-16 10:07:40 +07:00
parent 642ec0956f
commit db9cdfb8c7

View File

@ -7,7 +7,6 @@ const SEARCH_RESPONSE_SUGGESTION_JSON = "application/x-suggestions+json";
const BROWSER_SUGGEST_PREF = "browser.search.suggest.enabled";
const XPCOM_SHUTDOWN_TOPIC = "xpcom-shutdown";
const NS_PREFBRANCH_PREFCHANGE_TOPIC_ID = "nsPref:changed";
const SEARCH_BUNDLE = "chrome://global/locale/search/search.properties";
const Cc = Components.classes;
const Ci = Components.interfaces;
@ -21,6 +20,7 @@ const HTTP_SERVICE_UNAVAILABLE = 503;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/nsFormAutoCompleteResult.jsm");
Cu.import("resource://gre/modules/Services.jsm");
/**
* SuggestAutoComplete is a base class that implements nsIAutoCompleteSearch
@ -37,31 +37,18 @@ SuggestAutoComplete.prototype = {
_init: function() {
this._addObservers();
this._loadSuggestPref();
this._suggestEnabled = Services.prefs.getBoolPref(BROWSER_SUGGEST_PREF);
},
/**
* this._strings is the string bundle for message internationalization.
*/
get _strings() {
if (!this.__strings) {
var sbs = Cc["@mozilla.org/intl/stringbundle;1"].
getService(Ci.nsIStringBundleService);
this.__strings = sbs.createBundle(SEARCH_BUNDLE);
}
return this.__strings;
get _suggestionLabel() {
delete this._suggestionLabel;
let bundle = Services.strings.createBundle("chrome://global/locale/search/search.properties");
return this._suggestionLabel = bundle.GetStringFromName("suggestion_label");
},
__strings: null,
/**
* Search suggestions will be shown if this._suggestEnabled is true.
*/
_loadSuggestPref: function SAC_loadSuggestPref() {
var prefService = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
this._suggestEnabled = prefService.getBoolPref(BROWSER_SUGGEST_PREF);
},
_suggestEnabled: null,
/*************************************************************************
@ -351,7 +338,7 @@ SuggestAutoComplete.prototype = {
// if we have any suggestions, put a label at the top
if (comments.length > 0)
comments[0] = this._strings.GetStringFromName("suggestion_label");
comments[0] = this._suggestionLabel;
// now put the history results above the suggestions
var finalResults = historyResults.concat(results);
@ -412,9 +399,6 @@ SuggestAutoComplete.prototype = {
if (!previousResult)
this._formHistoryResult = null;
var searchService = Cc["@mozilla.org/browser/search-service;1"].
getService(Ci.nsIBrowserSearchService);
// If there's an existing request, stop it. There is no smart filtering
// here as there is when looking through history/form data because the
// result set returned by the server is different for every typed value -
@ -424,7 +408,7 @@ SuggestAutoComplete.prototype = {
this._listener = listener;
var engine = searchService.currentEngine;
var engine = Services.search.currentEngine;
this._checkForEngineSwitch(engine);
@ -481,7 +465,7 @@ SuggestAutoComplete.prototype = {
observe: function SAC_observe(aSubject, aTopic, aData) {
switch (aTopic) {
case NS_PREFBRANCH_PREFCHANGE_TOPIC_ID:
this._loadSuggestPref();
this._suggestEnabled = Services.prefs.getBoolPref(BROWSER_SUGGEST_PREF);
break;
case XPCOM_SHUTDOWN_TOPIC:
this._removeObservers();
@ -490,23 +474,15 @@ SuggestAutoComplete.prototype = {
},
_addObservers: function SAC_addObservers() {
var prefService2 = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
prefService2.addObserver(BROWSER_SUGGEST_PREF, this, false);
Services.prefs.addObserver(BROWSER_SUGGEST_PREF, this, false);
var os = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
os.addObserver(this, XPCOM_SHUTDOWN_TOPIC, false);
Services.obs.addObserver(this, XPCOM_SHUTDOWN_TOPIC, false);
},
_removeObservers: function SAC_removeObservers() {
var prefService2 = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
prefService2.removeObserver(BROWSER_SUGGEST_PREF, this);
Services.prefs.removeObserver(BROWSER_SUGGEST_PREF, this);
var os = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
os.removeObserver(this, XPCOM_SHUTDOWN_TOPIC);
Services.obs.removeObserver(this, XPCOM_SHUTDOWN_TOPIC);
},
// nsISupports