Bug 1359538 - Add API to retrieve engines by extension ID. r=florian
authorMichael Kaply <mozilla@kaply.com>
Wed, 26 Apr 2017 10:58:08 -0500
changeset 359964 fa0107fba5371fe1c7cab0ef09d3609eac5cc6a7
parent 359963 5dc81141d3e4fe6b29c642d0785d678de3c2b8e3
child 359965 4d13294ef068adfab6e36d969113f74b8aa7dbc3
push id43172
push usermozilla@kaply.com
push dateMon, 22 May 2017 19:49:17 +0000
treeherderautoland@fa0107fba537 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersflorian
bugs1359538
milestone55.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 1359538 - Add API to retrieve engines by extension ID. r=florian MozReview-Commit-ID: 2Qz6B1u8Or
netwerk/base/nsIBrowserSearchService.idl
toolkit/components/search/nsSearchService.js
toolkit/components/search/tests/xpcshell/test_addEngineWithExtensionID.js
toolkit/components/search/tests/xpcshell/xpcshell.ini
--- a/netwerk/base/nsIBrowserSearchService.idl
+++ b/netwerk/base/nsIBrowserSearchService.idl
@@ -410,16 +410,26 @@ interface nsIBrowserSearchService : nsIS
    *
    * @returns an array of nsISearchEngine objects.
    */
   void getDefaultEngines(
             [optional] out unsigned long engineCount,
             [retval, array, size_is(engineCount)] out nsISearchEngine engines);
 
   /**
+   * Returns an array of search engines installed by a given extension.
+   *
+   * @returns an array of nsISearchEngine objects.
+   */
+  void getEnginesByExtensionID(in AString extensionID,
+            [optional] out unsigned long engineCount,
+            [retval, array, size_is(engineCount)] out nsISearchEngine engines);
+
+
+  /**
    * Moves a visible search engine.
    *
    * @param  engine
    *         The engine to move.
    * @param  newIndex
    *         The engine's new index in the set of visible engines.
    *
    * @throws NS_ERROR_FAILURE if newIndex is out of bounds, or if engine is
--- a/toolkit/components/search/nsSearchService.js
+++ b/toolkit/components/search/nsSearchService.js
@@ -3987,16 +3987,27 @@ SearchService.prototype = {
       return a.name.localeCompare(b.name);
     }
     engines.sort(compareEngines);
 
     aCount.value = engines.length;
     return engines;
   },
 
+  getEnginesByExtensionID: function SRCH_SVC_getEngines(aExtensionID, aCount) {
+    this._ensureInitialized();
+    LOG("getEngines: getting all engines for " + aExtensionID);
+    var engines = this._getSortedEngines(true).filter(function(engine) {
+      return engine._extensionID == aExtensionID;
+    });
+    aCount.value = engines.length;
+    return engines;
+  },
+
+
   getEngineByName: function SRCH_SVC_getEngineByName(aEngineName) {
     this._ensureInitialized();
     return this._engines[aEngineName] || null;
   },
 
   getEngineByAlias: function SRCH_SVC_getEngineByAlias(aAlias) {
     this._ensureInitialized();
     for (var engineName in this._engines) {
new file mode 100644
--- /dev/null
+++ b/toolkit/components/search/tests/xpcshell/test_addEngineWithExtensionID.js
@@ -0,0 +1,25 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const kSearchEngineID = "addEngineWithDetails_test_engine";
+const kSearchEngineURL = "http://example.com/?search={searchTerms}";
+const kSearchTerm = "foo";
+const kExtensionID = "extension@mozilla.com";
+
+add_task(async function test_addEngineWithExtensionID() {
+  do_check_false(Services.search.isInitialized);
+
+  await asyncInit();
+
+  Services.search.addEngineWithDetails(kSearchEngineID, "", "", "", "get",
+                                       kSearchEngineURL, kExtensionID);
+
+  let engine = Services.search.getEngineByName(kSearchEngineID);
+  do_check_neq(engine, null);
+
+  let engines = Services.search.getEnginesByExtensionID(kExtensionID);
+  do_check_eq(engines.length, 1);
+  do_check_eq(engines[0], engine);
+});
--- a/toolkit/components/search/tests/xpcshell/xpcshell.ini
+++ b/toolkit/components/search/tests/xpcshell/xpcshell.ini
@@ -91,12 +91,13 @@ tags = addons
 [test_selectedEngine.js]
 [test_geodefaults.js]
 [test_hidden.js]
 [test_currentEngine_fallback.js]
 [test_require_engines_in_cache.js]
 [test_svg_icon.js]
 [test_searchReset.js]
 [test_addEngineWithDetails.js]
+[test_addEngineWithExtensionID.js]
 [test_chromeresource_icon1.js]
 [test_chromeresource_icon2.js]
 [test_engineUpdate.js]
 [test_paramSubstitution.js]