1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 * Various utilities for search related UI.
11 ChromeUtils.defineLazyGetter(lazy, "SearchUIUtilsL10n", () => {
12 return new Localization(["browser/search.ftl", "branding/brand.ftl"]);
15 export var SearchUIUtils = {
19 if (!this.initialized) {
20 Services.obs.addObserver(this, "browser-search-engine-modified");
22 this.initialized = true;
26 observe(engine, topic, data) {
28 case "engine-default":
29 this.updatePlaceholderNamePreference(engine, false);
31 case "engine-default-private":
32 this.updatePlaceholderNamePreference(engine, true);
38 * Adds an open search engine and handles error UI.
40 * @param {string} locationURL
41 * The URL where the OpenSearch definition is located.
42 * @param {string} image
43 * A URL string to an icon file to be used as the search engine's
44 * icon. This value may be overridden by an icon specified in the
45 * engine description file.
46 * @param {object} browsingContext
47 * The browsing context any error prompt should be opened for.
49 async addOpenSearchEngine(locationURL, image, browsingContext) {
51 await Services.search.addOpenSearchEngine(locationURL, image);
56 case Ci.nsISearchService.ERROR_DUPLICATE_ENGINE:
57 titleMsgName = "opensearch-error-duplicate-title";
58 descMsgName = "opensearch-error-duplicate-desc";
60 case Ci.nsISearchService.ERROR_ENGINE_CORRUPTED:
61 titleMsgName = "opensearch-error-format-title";
62 descMsgName = "opensearch-error-format-desc";
65 // i.e. ERROR_DOWNLOAD_FAILURE
66 titleMsgName = "opensearch-error-download-title";
67 descMsgName = "opensearch-error-download-desc";
71 let [title, text] = await lazy.SearchUIUtilsL10n.formatValues([
78 "location-url": locationURL,
83 Services.prompt.alertBC(
85 Ci.nsIPrompt.MODAL_TYPE_CONTENT,
95 * Returns the URL to use for where to get more search engines.
99 get searchEnginesURL() {
100 return Services.urlFormatter.formatURLPref(
101 "browser.search.searchEnginesURL"
106 * Update the placeholderName preference for the default search engine.
108 * @param {SearchEngine} engine The new default search engine.
109 * @param {boolean} isPrivate Whether this change applies to private windows.
111 updatePlaceholderNamePreference(engine, isPrivate) {
113 "browser.urlbar.placeholderName" + (isPrivate ? ".private" : "");
114 if (engine.isAppProvided) {
115 Services.prefs.setStringPref(prefName, engine.name);
117 Services.prefs.clearUserPref(prefName);