Bug 1579923 - WebExt API: Add suggestedIndex property to Result type. r=adw,mixedpuppy
authorMarco Bonardo <mbonardo@mozilla.com>
Mon, 30 Sep 2019 17:14:20 +0000
changeset 495634 e067cb3a0e710c9365ddf236e38bbe2f1bc59019
parent 495633 4301b72d5e3e7efae9abfe9c4f937df61ebc7d77
child 495635 4be2a981c307898ac2858db37872044a962d922c
push id96744
push usermak77@bonardo.net
push dateMon, 30 Sep 2019 17:15:59 +0000
treeherderautoland@e067cb3a0e71 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw, mixedpuppy
bugs1579923
milestone71.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 1579923 - WebExt API: Add suggestedIndex property to Result type. r=adw,mixedpuppy Differential Revision: https://phabricator.services.mozilla.com/D46941
browser/components/extensions/schemas/urlbar.json
browser/components/extensions/test/xpcshell/test_ext_urlbar.js
browser/components/urlbar/UrlbarMuxerUnifiedComplete.jsm
browser/components/urlbar/UrlbarProviderExtension.jsm
--- a/browser/components/extensions/schemas/urlbar.json
+++ b/browser/components/extensions/schemas/urlbar.json
@@ -63,16 +63,22 @@
           },
           "source": {
             "$ref": "SourceType",
             "description": "The result's source."
           },
           "type": {
             "$ref": "ResultType",
             "description": "The result's type."
+          },
+          "suggestedIndex": {
+            "type": "integer",
+            "description": "Suggest a preferred position for this result within the result set.",
+            "optional": true,
+            "default": -1
           }
         }
       },
       {
         "id": "ResultType",
         "type": "string",
         "enum": ["remote_tab", "search", "tab", "tip", "url"],
         "description": "Possible types of results. <code>remote_tab</code>: A synced tab from another device. <code>search</code>: A search suggestion from a search engine. <code>tab</code>: An open tab in the browser. <code>tip</code>: An actionable message to help the user with their query. <code>url</code>: A URL that's not one of the other types."
--- a/browser/components/extensions/test/xpcshell/test_ext_urlbar.js
+++ b/browser/components/extensions/test/xpcshell/test_ext_urlbar.js
@@ -331,16 +331,17 @@ add_task(async function test_onProviderR
       payload: {
         title: "Test url-history result",
         url: "http://example.com/url-history",
         displayUrl: "example.com/url-history",
       },
     },
   ];
 
+  Assert.ok(context.results.every(r => r.suggestedIndex == -1));
   let actualResults = context.results.map(r => ({
     type: r.type,
     source: r.source,
     title: r.title,
     heuristic: r.heuristic,
     payload: r.payload,
   }));
 
@@ -1336,16 +1337,17 @@ add_task(async function test_nonPrivateB
         return [
           {
             type: "url",
             source: "history",
             payload: {
               title: "Test result",
               url: "http://example.com/",
             },
+            suggestedIndex: 1,
           },
         ];
       }, "test");
     },
   });
   await ext.startup();
 
   // Check the provider.
@@ -1371,16 +1373,17 @@ add_task(async function test_nonPrivateB
   // Check isActive and isRestricting.
   Assert.ok(provider.isActive(context));
   Assert.ok(!provider.isRestricting(context));
 
   // Check the results.
   Assert.equal(context.results.length, 2);
   Assert.ok(context.results[0].heuristic);
   Assert.equal(context.results[1].title, "Test result");
+  Assert.equal(context.results[1].suggestedIndex, 1);
 
   await ext.unload();
 });
 
 // Tests the openViewOnFocus property.
 add_task(async function test_setOpenViewOnFocus() {
   let getPrefValue = () => UrlbarPrefs.get("openViewOnFocus");
 
--- a/browser/components/urlbar/UrlbarMuxerUnifiedComplete.jsm
+++ b/browser/components/urlbar/UrlbarMuxerUnifiedComplete.jsm
@@ -64,17 +64,17 @@ class MuxerUnifiedComplete extends Urlba
       heuristicResult.type == UrlbarUtils.RESULT_TYPE.SEARCH
         ? UrlbarPrefs.get("matchBucketsSearch")
         : UrlbarPrefs.get("matchBuckets");
     logger.debug(`Buckets: ${buckets}`);
     // These results have a suggested index and should be moved if possible.
     // The sorting is important, to avoid messing up indices later when we'll
     // insert these results.
     let reshuffleResults = context.results
-      .filter(r => r.suggestedIndex != -1)
+      .filter(r => r.suggestedIndex >= 0)
       .sort((a, b) => a.suggestedIndex - b.suggestedIndex);
     let sortedResults = [];
     // Track which results have been inserted already.
     let handled = new Set();
     for (let [group, slots] of buckets) {
       // Search all the available results to fill this bucket.
       for (let result of context.results) {
         if (slots == 0) {
@@ -94,17 +94,17 @@ class MuxerUnifiedComplete extends Urlba
           // Handle the heuristic result.
           sortedResults.unshift(result);
           handled.add(result);
           context.maxResults -= UrlbarUtils.getSpanForResult(result) - 1;
           slots--;
         } else if (group == RESULT_TYPE_TO_GROUP.get(result.type)) {
           // If there's no suggestedIndex, insert the result now, otherwise
           // we'll handle it later.
-          if (result.suggestedIndex == -1) {
+          if (result.suggestedIndex < 0) {
             sortedResults.push(result);
           }
           handled.add(result);
           context.maxResults -= UrlbarUtils.getSpanForResult(result) - 1;
           slots--;
         } else if (!RESULT_TYPE_TO_GROUP.has(result.type)) {
           let errorMsg = `Result type ${
             result.type
--- a/browser/components/urlbar/UrlbarProviderExtension.jsm
+++ b/browser/components/urlbar/UrlbarProviderExtension.jsm
@@ -307,24 +307,28 @@ class UrlbarProviderExtension extends Ur
       }
       if (!engine) {
         // No engine found.
         throw new Error("Invalid or missing engine specified by extension");
       }
       extResult.payload.engine = engine.name;
     }
 
-    return new UrlbarResult(
+    let result = new UrlbarResult(
       UrlbarProviderExtension.RESULT_TYPES[extResult.type],
       UrlbarProviderExtension.SOURCE_TYPES[extResult.source],
       ...UrlbarResult.payloadAndSimpleHighlights(
         context.tokens,
         extResult.payload || {}
       )
     );
+    if (extResult.suggestedIndex !== undefined) {
+      result.suggestedIndex = extResult.suggestedIndex;
+    }
+    return result;
   }
 }
 
 // Maps extension result type enums to internal result types.
 UrlbarProviderExtension.RESULT_TYPES = {
   keyword: UrlbarUtils.RESULT_TYPE.KEYWORD,
   omnibox: UrlbarUtils.RESULT_TYPE.OMNIBOX,
   remote_tab: UrlbarUtils.RESULT_TYPE.REMOTE_TAB,