Bug 785487: have AboutHomeUtils use the asynchronous search API, r=mikedeboer

--HG--
extra : transplant_source : o%00%28%00%0A%F1%B4%97L%EA%82%D9%0E%1An%B9m%8B%2C%40
This commit is contained in:
Gavin Sharp 2012-11-29 21:51:17 -08:00
parent 10e409c49f
commit 511250c5ce
2 changed files with 42 additions and 17 deletions

View File

@ -2305,21 +2305,30 @@ function BrowserOnAboutPageLoad(doc) {
}
docElt.setAttribute("snippetsVersion", AboutHomeUtils.snippetsVersion);
// Retrieve the engine to use for about:home initially
function updateSearchEngine() {
let engine = AboutHomeUtils.defaultSearchEngine;
docElt.setAttribute("searchEngineName", engine.name);
docElt.setAttribute("searchEngineURL", engine.searchURL);
AboutHomeUtils.getSearchEngineInfo(function (info) {
if (!info)
return;
docElt.setAttribute("searchEngineName", info.name);
docElt.setAttribute("searchEngineURL", info.searchURL);
});
}
updateSearchEngine();
// Listen for the event that's triggered when the user changes search engine.
// At this point we simply reload about:home to reflect the change.
Services.obs.addObserver(updateSearchEngine, "browser-search-engine-modified", false);
// Listen for the event that's triggered when the user changes search engine,
// and update about:home to reflect the change.
function engineObserver(subject, topic, data) {
if (data != "engine-default")
return;
updateSearchEngine();
}
Services.obs.addObserver(engineObserver, "browser-search-engine-modified", false);
// Remove the observer when the page is reloaded or closed.
doc.defaultView.addEventListener("pagehide", function removeObserver() {
doc.defaultView.removeEventListener("pagehide", removeObserver);
Services.obs.removeObserver(updateSearchEngine, "browser-search-engine-modified");
Services.obs.removeObserver(engineObserver, "browser-search-engine-modified");
}, false);
#ifdef MOZ_SERVICES_HEALTHREPORT

View File

@ -21,17 +21,33 @@ this.AboutHomeUtils = {
/**
* Returns an object containing the name and searchURL of the original default
* search engine.
*
* @param callback
* Invoked once information is available, gets an object with "name"
* and "searchURL" properties, or null in case of failure.
*/
get defaultSearchEngine() {
let defaultEngine = Services.search.defaultEngine;
let submission = defaultEngine.getSubmission("_searchTerms_", null, "homepage");
if (submission.postData) {
throw new Error("Home page does not support POST search engines.");
}
return Object.freeze({
name: defaultEngine.name,
searchURL: submission.uri.spec
getSearchEngineInfo: function (callback) {
if (!callback)
throw new Error("Must pass a callback to getSearchEngineInfo");
Services.search.init(function (status) {
if (!Components.isSuccessCode(status)) {
callback(null);
return;
}
let defaultEngine = Services.search.defaultEngine;
let submission = defaultEngine.getSubmission("_searchTerms_", null, "homepage");
if (submission.postData) {
Components.utils.reportError("Home page does not support POST search engines.");
callback(null);
return;
}
callback({
name: defaultEngine.name,
searchURL: submission.uri.spec
});
});
},