Bug 1432263 - Make matchBuckets string parsing the same everywhere it's done. r?mak draft
authorDrew Willcoxon <adw@mozilla.com>
Mon, 22 Jan 2018 11:21:55 -0800
changeset 723221 87d66b3b47991cf76ff35cf6b704ec33eb3fda08
parent 723064 3d23e6d98a09a3395bf2b0d9cb03dd4be358c715
child 746809 7f279c6635a85be65e9164195f18beddeab70e72
push id96372
push userbmo:adw@mozilla.com
push dateMon, 22 Jan 2018 19:22:41 +0000
reviewersmak
bugs1432263
milestone60.0a1
Bug 1432263 - Make matchBuckets string parsing the same everywhere it's done. r?mak MozReview-Commit-ID: GmGCHl4RtWB
browser/components/preferences/in-content/search.js
toolkit/components/places/PlacesUtils.jsm
toolkit/components/places/UnifiedComplete.js
--- a/browser/components/preferences/in-content/search.js
+++ b/browser/components/preferences/in-content/search.js
@@ -83,19 +83,18 @@ var gSearchPane = {
   _syncFromShowSearchSuggestionsFirstPref(checkbox, pref) {
     if (!pref.value) {
       // The pref is cleared, meaning search suggestions are shown first.
       checkbox.checked = true;
       return;
     }
     // The pref has a value.  If the first bucket in the pref is search
     // suggestions, then check the checkbox.
-    let bucketPair = pref.value.split(",")[0];
-    let bucketName = bucketPair.split(":")[0];
-    checkbox.checked = bucketName == "suggestion";
+    let buckets = PlacesUtils.convertMatchBucketsStringToArray(pref.value);
+    checkbox.checked = buckets[0] && buckets[0][0] == "suggestion";
   },
 
   _syncToShowSearchSuggestionsFirstPref(checked, pref) {
     if (checked) {
       // Show search suggestions first, so clear the pref since that's the
       // default.
       pref.reset();
       return;
--- a/toolkit/components/places/PlacesUtils.jsm
+++ b/toolkit/components/places/PlacesUtils.jsm
@@ -444,16 +444,37 @@ this.PlacesUtils = {
         continue;
       }
       encodedParams[key] = encodeURIComponent(params[key]);
     }
     return "moz-action:" + type + "," + JSON.stringify(encodedParams);
   },
 
   /**
+   * Parses matchBuckets strings (for example, "suggestion:4,general:Infinity")
+   * like those used in the browser.urlbar.matchBuckets preference.
+   *
+   * @param   str
+   *          A matchBuckets string.
+   * @returns An array of the form: [
+   *            [bucketName_0, bucketPriority_0],
+   *            [bucketName_1, bucketPriority_1],
+   *            ...
+   *            [bucketName_n, bucketPriority_n]
+   *          ]
+   */
+  convertMatchBucketsStringToArray(str) {
+    return str.split(",")
+              .map(v => {
+                let bucket = v.split(":");
+                return [ bucket[0].trim().toLowerCase(), Number(bucket[1]) ];
+              });
+  },
+
+  /**
    * Determines whether or not a ResultNode is a Bookmark folder.
    * @param   aNode
    *          A result node
    * @returns true if the node is a Bookmark folder, false otherwise
    */
   nodeIsFolder: function PU_nodeIsFolder(aNode) {
     return (aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_FOLDER ||
             aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_FOLDER_SHORTCUT);
--- a/toolkit/components/places/UnifiedComplete.js
+++ b/toolkit/components/places/UnifiedComplete.js
@@ -334,24 +334,16 @@ XPCOMUtils.defineLazyPreferenceGetter(th
                                       "services.sync.username");
 
 function setTimeout(callback, ms) {
   let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
   timer.initWithCallback(callback, ms, timer.TYPE_ONE_SHOT);
   return timer;
 }
 
-function convertBucketsCharPrefToArray(str) {
-  return str.split(",")
-            .map(v => {
-              let bucket = v.split(":");
-              return [ bucket[0].trim().toLowerCase(), Number(bucket[1]) ];
-            });
-}
-
 /**
  * Storage object for switch-to-tab entries.
  * This takes care of caching and registering open pages, that will be reused
  * by switch-to-tab queries.  It has an internal cache, so that the Sqlite
  * store is lazy initialized only on first use.
  * It has a simple API:
  *   initDatabase(conn): initializes the temporary Sqlite entities to store data
  *   add(uri): adds a given nsIURI to the store
@@ -483,31 +475,31 @@ XPCOMUtils.defineLazyGetter(this, "Prefs
   }
 
   function getPrefValue(pref) {
     switch (pref) {
       case "matchBuckets": {
         // Convert from pref char format to an array and add the default buckets.
         let val = readPref(pref);
         try {
-          val = convertBucketsCharPrefToArray(val);
+          val = PlacesUtils.convertMatchBucketsStringToArray(val);
         } catch (ex) {
-          val = convertBucketsCharPrefToArray(PREF_URLBAR_DEFAULTS.get(pref));
+          val = PlacesUtils.convertMatchBucketsStringToArray(PREF_URLBAR_DEFAULTS.get(pref));
         }
         return [ ...DEFAULT_BUCKETS_BEFORE,
                 ...val,
                 ...DEFAULT_BUCKETS_AFTER ];
       }
       case "matchBucketsSearch": {
         // Convert from pref char format to an array and add the default buckets.
         let val = readPref(pref);
         if (val) {
           // Convert from pref char format to an array and add the default buckets.
           try {
-            val = convertBucketsCharPrefToArray(val);
+            val = PlacesUtils.convertMatchBucketsStringToArray(val);
             return [ ...DEFAULT_BUCKETS_BEFORE,
                     ...val,
                     ...DEFAULT_BUCKETS_AFTER ];
           } catch (ex) { /* invalid format, will just return matchBuckets */ }
         }
         return store.get("matchBuckets");
       }
       case "suggest.history.onlyTyped": {