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/. */
4"use strict";
5
6const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
7
8// List of sites we match against Topsites in order to identify sites
9// that should be converted to search Topsites
10const SEARCH_SHORTCUTS = [
11  { keyword: "@amazon", shortURL: "amazon", url: "https://amazon.com" },
12  { keyword: "@\u767E\u5EA6", shortURL: "baidu", url: "https://baidu.com" },
13  { keyword: "@google", shortURL: "google", url: "https://google.com" },
14  {
15    keyword: "@\u044F\u043D\u0434\u0435\u043A\u0441",
16    shortURL: "yandex",
17    url: "https://yandex.com",
18  },
19];
20this.SEARCH_SHORTCUTS = SEARCH_SHORTCUTS;
21
22// These can be added via the editor but will not be added organically
23this.CUSTOM_SEARCH_SHORTCUTS = [
24  ...SEARCH_SHORTCUTS,
25  { keyword: "@bing", shortURL: "bing", url: "https://bing.com" },
26  {
27    keyword: "@duckduckgo",
28    shortURL: "duckduckgo",
29    url: "https://duckduckgo.com",
30  },
31  { keyword: "@ebay", shortURL: "ebay", url: "https://ebay.com" },
32  { keyword: "@twitter", shortURL: "twitter", url: "https://twitter.com" },
33  {
34    keyword: "@wikipedia",
35    shortURL: "wikipedia",
36    url: "https://wikipedia.org",
37  },
38];
39
40// Note: you must add the activity stream branch to the beginning of this if using outside activity stream
41this.SEARCH_SHORTCUTS_EXPERIMENT = "improvesearch.topSiteSearchShortcuts";
42this.SEARCH_SHORTCUTS_SEARCH_ENGINES_PREF =
43  "improvesearch.topSiteSearchShortcuts.searchEngines";
44this.SEARCH_SHORTCUTS_HAVE_PINNED_PREF =
45  "improvesearch.topSiteSearchShortcuts.havePinned";
46
47function getSearchProvider(candidateShortURL) {
48  return (
49    SEARCH_SHORTCUTS.filter(match => candidateShortURL === match.shortURL)[0] ||
50    null
51  );
52}
53this.getSearchProvider = getSearchProvider;
54
55// Get the search form URL for a given search keyword. This allows us to pick
56// different tippytop icons for the different variants. Sush as yandex.com vs. yandex.ru.
57// See more details in bug 1643523.
58async function getSearchFormURL(keyword) {
59  const engine = await Services.search.getEngineByAlias(keyword);
60  return engine?.wrappedJSObject._searchForm;
61}
62this.getSearchFormURL = getSearchFormURL;
63
64// Check topsite against predefined list of valid search engines
65// https://searchfox.org/mozilla-central/rev/ca869724246f4230b272ed1c8b9944596e80d920/toolkit/components/search/nsSearchService.js#939
66async function checkHasSearchEngine(keyword) {
67  return (await Services.search.getAppProvidedEngines()).find(
68    e => e.aliases.includes(keyword) && !e.hidden
69  );
70}
71this.checkHasSearchEngine = checkHasSearchEngine;
72
73const EXPORTED_SYMBOLS = [
74  "checkHasSearchEngine",
75  "getSearchProvider",
76  "getSearchFormURL",
77  "SEARCH_SHORTCUTS",
78  "CUSTOM_SEARCH_SHORTCUTS",
79  "SEARCH_SHORTCUTS_EXPERIMENT",
80  "SEARCH_SHORTCUTS_SEARCH_ENGINES_PREF",
81  "SEARCH_SHORTCUTS_HAVE_PINNED_PREF",
82];
83