Bug 1482125 - Add internal aliases for common search plugins. r=Mardak,adw
authorMichael Kaply <mozilla@kaply.com>
Fri, 10 Aug 2018 19:18:30 +0000
changeset 486144 53bdfc04ee13bf0fcff34734187fdb6598e3f6f2
parent 486143 f5748d54bf4be5312f5a5463bc7d1ce8f8e1a777
child 486145 90d8a466a355840b4045d6caec0d7494aced4adc
push id9719
push userffxbld-merge
push dateFri, 24 Aug 2018 17:49:46 +0000
treeherdermozilla-beta@719ec98fba77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMardak, adw
bugs1482125
milestone63.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1482125 - Add internal aliases for common search plugins. r=Mardak,adw Differential Revision: https://phabricator.services.mozilla.com/D3033
toolkit/components/places/PlacesSearchAutocompleteProvider.jsm
toolkit/components/places/tests/unifiedcomplete/test_PlacesSearchAutocompleteProvider.js
toolkit/components/search/nsSearchService.js
--- a/toolkit/components/places/PlacesSearchAutocompleteProvider.jsm
+++ b/toolkit/components/places/PlacesSearchAutocompleteProvider.jsm
@@ -12,21 +12,16 @@ var EXPORTED_SYMBOLS = [ "PlacesSearchAu
 
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 
 ChromeUtils.defineModuleGetter(this, "SearchSuggestionController",
   "resource://gre/modules/SearchSuggestionController.jsm");
 
 const SEARCH_ENGINE_TOPIC = "browser-search-engine-modified";
 
-const ENGINE_ALIASES = new Map([
-  ["Google", "@google"],
-  ["Amazon", "@amazon"],
-]);
-
 const SearchAutocompleteProviderInternal = {
   /**
    * Array of objects in the format returned by findMatchByToken.
    */
   priorityMatches: null,
 
   /**
    * Array of objects in the format returned by findMatchByAlias.
@@ -102,28 +97,20 @@ const SearchAutocompleteProviderInternal
         url: engine.searchForm,
         engineName: engine.name,
         iconUrl: engine.iconURI ? engine.iconURI.spec : null,
       });
     }
 
     let aliases = [];
 
-    for (let [name, alias] of ENGINE_ALIASES) {
-      if (!engine.wrappedJSObject._isDefault ||
-          !engine.name.startsWith(name)) {
-        continue;
-      }
-      aliases.push(alias);
-      break;
-    }
-
     if (engine.alias) {
       aliases.push(engine.alias);
     }
+    aliases.push(...engine.wrappedJSObject._internalAliases);
 
     if (aliases.length) {
       this.aliasMatches.push({
         aliases,
         engineName: engine.name,
         iconUrl: engine.iconURI ? engine.iconURI.spec : null,
         resultDomain: domain,
       });
--- a/toolkit/components/places/tests/unifiedcomplete/test_PlacesSearchAutocompleteProvider.js
+++ b/toolkit/components/places/tests/unifiedcomplete/test_PlacesSearchAutocompleteProvider.js
@@ -114,22 +114,18 @@ add_task(async function test_parseSubmis
 
   result = PlacesSearchAutocompleteProvider.parseSubmissionURL("http://example.org/");
   Assert.equal(result, null);
 });
 
 add_task(async function test_builtin_aliased_search_engine_match() {
   let match = await PlacesSearchAutocompleteProvider.findMatchByAlias("@google");
   Assert.equal(match.engineName, "Google");
-
-  let match1 = await PlacesSearchAutocompleteProvider.findMatchByAlias("@amazon");
-  Assert.ok(match1.engineName.startsWith("Amazon"));
 });
 
-
 function promiseDefaultSearchEngine() {
   return new Promise(resolve => {
     Services.search.init( () => {
       resolve(Services.search.defaultEngine);
     });
   });
 }
 
--- a/toolkit/components/search/nsSearchService.js
+++ b/toolkit/components/search/nsSearchService.js
@@ -931,16 +931,45 @@ function ParamSubstitution(aParamValue, 
         return param[1];
     }
 
     // Don't replace unknown non-optional parameters.
     return match;
   });
 }
 
+const ENGINE_ALIASES = new Map([
+  ["google", ["@google"]],
+  ["amazondotcom", ["@amazon"]],
+  ["amazondotcom-de", ["@amazon"]],
+  ["amazon-en-GB", ["@amazon"]],
+  ["amazon-france", ["@amazon"]],
+  ["amazon-jp", ["@amazon"]],
+  ["amazon-it", ["@amazon"]],
+  ["twitter", ["@twitter"]],
+  ["wikipedia", ["@wikipedia"]],
+  ["ebay", ["@ebay"]],
+  ["bing", ["@bing"]],
+  ["ddg", ["@duckduckgo", "@ddg"]],
+  ["yandex", ["@yandex"]],
+  ["baidu", ["@baidu"]],
+]);
+
+function getInternalAliases(engine) {
+  if (!engine._isDefault) {
+    return [];
+  }
+  for (let [name, aliases] of ENGINE_ALIASES) {
+    if (engine._shortName.startsWith(name)) {
+      return aliases;
+    }
+  }
+  return [];
+}
+
 /**
  * Creates an engineURL object, which holds the query URL and all parameters.
  *
  * @param aType
  *        A string containing the name of the MIME type of the search results
  *        returned by this URL.
  * @param aMethod
  *        The HTTP request method. Must be a case insensitive value of either
@@ -2188,16 +2217,25 @@ Engine.prototype = {
   get name() {
     return this._name;
   },
 
   get searchForm() {
     return this._getSearchFormWithPurpose();
   },
 
+  /* Internal aliases for default engines only. */
+  __internalAliases: null,
+  get _internalAliases() {
+    if (!this.__internalAliases) {
+      this.__internalAliases = getInternalAliases(this);
+    }
+    return this.__internalAliases;
+  },
+
   _getSearchFormWithPurpose(aPurpose = "") {
     // First look for a <Url rel="searchform">
     var searchFormURL = this._getURLOfType(URLTYPE_SEARCH_HTML, "searchform");
     if (searchFormURL) {
       let submission = searchFormURL.getSubmission("", this, aPurpose);
 
       // If the rel=searchform URL is not type="get" (i.e. has postData),
       // ignore it, since we can only return a URL.
@@ -3781,18 +3819,19 @@ SearchService.prototype = {
     this._ensureInitialized();
     return this._engines[aEngineName] || null;
   },
 
   getEngineByAlias: function SRCH_SVC_getEngineByAlias(aAlias) {
     this._ensureInitialized();
     for (var engineName in this._engines) {
       var engine = this._engines[engineName];
-      if (engine && engine.alias == aAlias)
+      if (engine && (engine.alias == aAlias || engine._internalAliases.includes(aAlias))) {
         return engine;
+      }
     }
     return null;
   },
 
   addEngineWithDetails: function SRCH_SVC_addEWD(aName, iconURL, alias,
                                                  description, method,
                                                  template, extensionID) {
     let isCurrent = false;