Bug 1855385 [wpt PR 42195] - [text-spacing-trim] Remove redundant copying of variants...
[gecko.git] / browser / components / search / SearchUIUtils.sys.mjs
blobbb3e1e3c820667f50925172b95fdc593b1a435d8
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/. */
5 /**
6  * Various utilities for search related UI.
7  */
9 const lazy = {};
11 ChromeUtils.defineLazyGetter(lazy, "SearchUIUtilsL10n", () => {
12   return new Localization(["browser/search.ftl", "branding/brand.ftl"]);
13 });
15 export var SearchUIUtils = {
16   initialized: false,
18   init() {
19     if (!this.initialized) {
20       Services.obs.addObserver(this, "browser-search-engine-modified");
22       this.initialized = true;
23     }
24   },
26   observe(engine, topic, data) {
27     switch (data) {
28       case "engine-default":
29         this.updatePlaceholderNamePreference(engine, false);
30         break;
31       case "engine-default-private":
32         this.updatePlaceholderNamePreference(engine, true);
33         break;
34     }
35   },
37   /**
38    * Adds an open search engine and handles error UI.
39    *
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.
48    */
49   async addOpenSearchEngine(locationURL, image, browsingContext) {
50     try {
51       await Services.search.addOpenSearchEngine(locationURL, image);
52     } catch (ex) {
53       let titleMsgName;
54       let descMsgName;
55       switch (ex.result) {
56         case Ci.nsISearchService.ERROR_DUPLICATE_ENGINE:
57           titleMsgName = "opensearch-error-duplicate-title";
58           descMsgName = "opensearch-error-duplicate-desc";
59           break;
60         case Ci.nsISearchService.ERROR_ENGINE_CORRUPTED:
61           titleMsgName = "opensearch-error-format-title";
62           descMsgName = "opensearch-error-format-desc";
63           break;
64         default:
65           // i.e. ERROR_DOWNLOAD_FAILURE
66           titleMsgName = "opensearch-error-download-title";
67           descMsgName = "opensearch-error-download-desc";
68           break;
69       }
71       let [title, text] = await lazy.SearchUIUtilsL10n.formatValues([
72         {
73           id: titleMsgName,
74         },
75         {
76           id: descMsgName,
77           args: {
78             "location-url": locationURL,
79           },
80         },
81       ]);
83       Services.prompt.alertBC(
84         browsingContext,
85         Ci.nsIPrompt.MODAL_TYPE_CONTENT,
86         title,
87         text
88       );
89       return false;
90     }
91     return true;
92   },
94   /**
95    * Returns the URL to use for where to get more search engines.
96    *
97    * @returns {string}
98    */
99   get searchEnginesURL() {
100     return Services.urlFormatter.formatURLPref(
101       "browser.search.searchEnginesURL"
102     );
103   },
105   /**
106    * Update the placeholderName preference for the default search engine.
107    *
108    * @param {SearchEngine} engine The new default search engine.
109    * @param {boolean} isPrivate Whether this change applies to private windows.
110    */
111   updatePlaceholderNamePreference(engine, isPrivate) {
112     const prefName =
113       "browser.urlbar.placeholderName" + (isPrivate ? ".private" : "");
114     if (engine.isAppProvided) {
115       Services.prefs.setStringPref(prefName, engine.name);
116     } else {
117       Services.prefs.clearUserPref(prefName);
118     }
119   },