Bug 1265834 - Part 1: Move normalizeTime from downloads into a shared util, r?aswan draft
authorBob Silverberg <bsilverberg@mozilla.com>
Fri, 13 May 2016 10:18:04 -0400
changeset 367525 b2429883de8e6e0384c307b4af0630ff40f899c2
parent 367424 26656bbb6d39afcac4fbe4d7763ed2c9f05f6f89
child 367526 05fa42ce4e70bea3a143c3b8e63e93399ee8a15c
push id18256
push userbmo:bob.silverberg@gmail.com
push dateMon, 16 May 2016 19:41:40 +0000
reviewersaswan
bugs1265834
milestone49.0a1
Bug 1265834 - Part 1: Move normalizeTime from downloads into a shared util, r?aswan MozReview-Commit-ID: GKWBbvTPbGy
toolkit/components/extensions/ExtensionUtils.jsm
toolkit/components/extensions/ext-downloads.js
toolkit/components/extensions/schemas/downloads.json
--- a/toolkit/components/extensions/ExtensionUtils.jsm
+++ b/toolkit/components/extensions/ExtensionUtils.jsm
@@ -1182,23 +1182,43 @@ class ChildAPIManager {
 
   hasListener(path, name, listener) {
     let ref = path.concat(name).join(".");
     let set = this.listeners.get(ref) || new Set();
     return set.has(listener);
   }
 }
 
+/**
+ * Returns a number which represents the number of milliseconds since the epoch
+ * for either a startDate or an endDate. Accepts several formats:
+ * a Date object, an ISO8601 string, or a number of milliseconds since the epoch as
+ * either a number or a string.
+ *
+ * @param date: (Date) or (String) or (Number)
+ *      The date to convert.
+ * @returns (Number)
+ *      The number of milliseconds since the epoch for the date
+ */
+function normalizeTime(date) {
+  // Of all the formats we accept the "number of milliseconds since the epoch as a string"
+  // is an outlier, everything else can just be passed directly to the Date constructor.
+  const result = new Date((typeof date == "string" && /^\d+$/.test(date))
+                        ? parseInt(date, 10) : date);
+  return result.valueOf();
+}
+
 this.ExtensionUtils = {
   detectLanguage,
   extend,
   flushJarCache,
   ignoreEvent,
   injectAPI,
   instanceOf,
+  normalizeTime,
   promiseDocumentReady,
   runSafe,
   runSafeSync,
   runSafeSyncWithoutClone,
   runSafeWithoutClone,
   BaseContext,
   DefaultWeakMap,
   EventManager,
--- a/toolkit/components/extensions/ext-downloads.js
+++ b/toolkit/components/extensions/ext-downloads.js
@@ -15,16 +15,17 @@ XPCOMUtils.defineLazyModuleGetter(this, 
 XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
                                   "resource://gre/modules/NetUtil.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "EventEmitter",
                                   "resource://devtools/shared/event-emitter.js");
 
 Cu.import("resource://gre/modules/ExtensionUtils.jsm");
 const {
   ignoreEvent,
+  normalizeTime,
   runSafeSync,
   SingletonEventManager,
 } = ExtensionUtils;
 
 const DOWNLOAD_ITEM_FIELDS = ["id", "url", "referrer", "filename", "incognito",
                               "danger", "mime", "startTime", "endTime",
                               "estimatedEndTime", "state",
                               "paused", "canResume", "error",
@@ -236,34 +237,28 @@ function downloadQuery(query) {
       if (term[0] == "-") {
         queryNegativeTerms.push(term.slice(1).toLowerCase());
       } else {
         queryTerms.push(term.toLowerCase());
       }
     }
   }
 
-  function normalizeTime(arg, before) {
+  function normalizeDownloadTime(arg, before) {
     if (arg == null) {
       return before ? Number.MAX_VALUE : 0;
+    } else {
+      return normalizeTime(arg);
     }
-
-    // We accept several formats: a Date object, an ISO8601 string, or a
-    // number of milliseconds since the epoch as either a number or a string.
-    // The "number of milliseconds since the epoch as a string" is an outlier,
-    // everything else can just be passed directly to the Date constructor.
-    const date = new Date((typeof arg == "string" && /^\d+$/.test(arg))
-                          ? parseInt(arg, 10) : arg);
-    return date.valueOf();
   }
 
-  const startedBefore = normalizeTime(query.startedBefore, true);
-  const startedAfter = normalizeTime(query.startedAfter, false);
-  // const endedBefore = normalizeTime(query.endedBefore, true);
-  // const endedAfter = normalizeTime(query.endedAfter, false);
+  const startedBefore = normalizeDownloadTime(query.startedBefore, true);
+  const startedAfter = normalizeDownloadTime(query.startedAfter, false);
+  // const endedBefore = normalizeDownloadTime(query.endedBefore, true);
+  // const endedAfter = normalizeDownloadTime(query.endedAfter, false);
 
   const totalBytesGreater = query.totalBytesGreater || 0;
   const totalBytesLess = (query.totalBytesLess != null)
         ? query.totalBytesLess : Number.MAX_VALUE;
 
   // Handle options for which we can have a regular expression and/or
   // an explicit value to match.
   function makeMatch(regex, value, field) {
--- a/toolkit/components/extensions/schemas/downloads.json
+++ b/toolkit/components/extensions/schemas/downloads.json
@@ -206,17 +206,17 @@
           "previous": {
             "optional": true,
             "type": "boolean"
           }
         }
       },
       {
         "id": "DownloadTime",
-        "description": "A time specified as a Date object, a number of string representing milliseconds since the epoch, or an ISO 8601 string",
+        "description": "A time specified as a Date object, a number or string representing milliseconds since the epoch, or an ISO 8601 string",
         "choices": [
           {
             "type": "string",
             "pattern": "^[1-9]\\d*$"
           },
           {
             "$ref": "extensionTypes.Date"
           }