Bug 1480504 - Add internal aliases for common engines. r=Mardak
authorMichael Kaply <mozilla@kaply.com>
Tue, 07 Aug 2018 20:52:12 +0000
changeset 430435 2ab02d99fe7e1733f20429d2228f261b97b34d85
parent 430434 6f39c71a8f6ce42ca894c7d609c8016e78b3e6ed
child 430436 b118b720b2c7c94b634c86350b83014c2840e7bf
push id34405
push userncsoregi@mozilla.com
push dateWed, 08 Aug 2018 09:57:26 +0000
treeherdermozilla-central@c65991f3fa10 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMardak
bugs1480504
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 1480504 - Add internal aliases for common engines. r=Mardak Differential Revision: https://phabricator.services.mozilla.com/D2681
toolkit/components/places/PlacesSearchAutocompleteProvider.jsm
toolkit/components/places/tests/unifiedcomplete/test_PlacesSearchAutocompleteProvider.js
--- a/toolkit/components/places/PlacesSearchAutocompleteProvider.jsm
+++ b/toolkit/components/places/PlacesSearchAutocompleteProvider.jsm
@@ -12,16 +12,21 @@ 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.
@@ -78,41 +83,56 @@ const SearchAutocompleteProviderInternal
       this.defaultMatch = {
         engineName: currentEngine.name,
         iconUrl: currentEngine.iconURI ? currentEngine.iconURI.spec : null,
       };
     }
 
     // The search engines will always be processed in the order returned by the
     // search service, which can be defined by the user.
-    Services.search.getVisibleEngines().forEach(e => this._addEngine(e));
+    Services.search.getEngines().forEach(e => this._addEngine(e));
   },
 
   _addEngine(engine) {
     let domain = engine.getResultDomain();
 
-    if (engine.alias) {
-      this.aliasMatches.push({
-        alias: engine.alias,
-        engineName: engine.name,
-        iconUrl: engine.iconURI ? engine.iconURI.spec : null,
-        resultDomain: domain,
-      });
-    }
-
-    if (domain) {
+    if (domain && !engine.hidden) {
       this.priorityMatches.push({
         token: domain,
         // The searchForm property returns a simple URL for the search engine, but
         // we may need an URL which includes an affiliate code (bug 990799).
         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);
+    }
+
+    if (aliases.length) {
+      this.aliasMatches.push({
+        aliases,
+        engineName: engine.name,
+        iconUrl: engine.iconURI ? engine.iconURI.spec : null,
+        resultDomain: domain,
+      });
+    }
   },
 
   getSuggestionController(searchToken, inPrivateContext, maxLocalResults,
                           maxRemoteResults, userContextId) {
     let engine = Services.search.currentEngine;
     if (!engine) {
       return null;
     }
@@ -255,17 +275,17 @@ var PlacesSearchAutocompleteProvider = O
    *           resultDomain: The domain name for the search engine's results;
    *                         see nsISearchEngine::getResultDomain.
    *         }
    */
   async findMatchByAlias(searchToken) {
     await this.ensureInitialized();
 
     return SearchAutocompleteProviderInternal.aliasMatches
-             .find(m => m.alias.toLocaleLowerCase() == searchToken.toLocaleLowerCase());
+             .find(m => m.aliases.some(a => a.toLocaleLowerCase() == searchToken.toLocaleLowerCase()));
   },
 
   async getDefaultMatch() {
     await this.ensureInitialized();
 
     return SearchAutocompleteProviderInternal.defaultMatch;
   },
 
--- a/toolkit/components/places/tests/unifiedcomplete/test_PlacesSearchAutocompleteProvider.js
+++ b/toolkit/components/places/tests/unifiedcomplete/test_PlacesSearchAutocompleteProvider.js
@@ -50,50 +50,50 @@ add_task(async function add_search_engin
   Assert.equal(match.iconUrl, null);
 });
 
 add_task(async function test_aliased_search_engine_match() {
   Assert.equal(null, await PlacesSearchAutocompleteProvider.findMatchByAlias("sober"));
   // Lower case
   let match = await PlacesSearchAutocompleteProvider.findMatchByAlias("pork");
   Assert.equal(match.engineName, "bacon");
-  Assert.equal(match.alias, "pork");
+  Assert.equal(match.aliases[0], "pork");
   Assert.equal(match.iconUrl, null);
   // Upper case
   let match1 = await PlacesSearchAutocompleteProvider.findMatchByAlias("PORK");
   Assert.equal(match1.engineName, "bacon");
-  Assert.equal(match1.alias, "pork");
+  Assert.equal(match1.aliases[0], "pork");
   Assert.equal(match1.iconUrl, null);
   // Cap case
   let match2 = await PlacesSearchAutocompleteProvider.findMatchByAlias("Pork");
   Assert.equal(match2.engineName, "bacon");
-  Assert.equal(match2.alias, "pork");
+  Assert.equal(match2.aliases[0], "pork");
   Assert.equal(match2.iconUrl, null);
 });
 
 add_task(async function test_aliased_search_engine_match_upper_case_alias() {
   let promiseTopic = promiseSearchTopic("engine-added");
   Assert.equal(null, await PlacesSearchAutocompleteProvider.findMatchByToken("patch"));
   Services.search.addEngineWithDetails("patch", "", "PR", "Search Patch",
                                        "GET", "http://www.patch.moz/?search={searchTerms}");
   await promiseTopic;
   // lower case
   let match = await PlacesSearchAutocompleteProvider.findMatchByAlias("pr");
   Assert.equal(match.engineName, "patch");
-  Assert.equal(match.alias, "PR");
+  Assert.equal(match.aliases[0], "PR");
   Assert.equal(match.iconUrl, null);
   // Upper case
   let match1 = await PlacesSearchAutocompleteProvider.findMatchByAlias("PR");
   Assert.equal(match1.engineName, "patch");
-  Assert.equal(match1.alias, "PR");
+  Assert.equal(match1.aliases[0], "PR");
   Assert.equal(match1.iconUrl, null);
   // Cap case
   let match2 = await PlacesSearchAutocompleteProvider.findMatchByAlias("Pr");
   Assert.equal(match2.engineName, "patch");
-  Assert.equal(match2.alias, "PR");
+  Assert.equal(match2.aliases[0], "PR");
   Assert.equal(match2.iconUrl, null);
 });
 
 add_task(async function remove_search_engine_nomatch() {
   let engine = Services.search.getEngineByName("bacon");
   let promiseTopic = promiseSearchTopic("engine-removed");
   Services.search.removeEngine(engine);
   await promiseTopic;
@@ -109,16 +109,25 @@ add_task(async function test_parseSubmis
   let result = PlacesSearchAutocompleteProvider.parseSubmissionURL(submissionURL);
   Assert.equal(result.engineName, engine.name);
   Assert.equal(result.terms, "terms");
 
   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);
     });
   });
 }