Bug 1265834 - Part 1: Move normalizeTime from downloads into a shared util, r?aswan
MozReview-Commit-ID: GKWBbvTPbGy
--- 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"
}