Bug 882552 - Change Binder Thread's default priority to 0. r=mwu, r=jlebar
[gecko.git] / toolkit / modules / TelemetryTimestamps.jsm
blob7a17b877078a93e5b2323ea860f5d66f69cf6045
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 this.EXPORTED_SYMBOLS = ["TelemetryTimestamps"];
7 /**
8  * This module's purpose is to collect timestamps for important
9  * application-specific events.
10  *
11  * The TelemetryPing component attaches the timestamps stored by this module to
12  * the telemetry submission, substracting the process lifetime so that the times
13  * are relative to process startup. The overall goal is to produce a basic
14  * timeline of the startup process.
15  */
16 let timeStamps = {};
18 this.TelemetryTimestamps = {
19   /**
20    * Adds a timestamp to the list. The addition of TimeStamps that already have
21    * a value stored is ignored.
22    *
23    * @param name must be a unique, generally "camelCase" descriptor of what the
24    *             timestamp represents. e.g.: "delayedStartupStarted"
25    * @param value is a timeStamp in milliseconds since the epoch. If omitted,
26    *              defaults to Date.now().
27    */
28   add: function TT_add(name, value) {
29     // Default to "now" if not specified
30     if (value == null)
31       value = Date.now();
33     if (isNaN(value))
34       throw new Error("Value must be a timestamp");
36     // If there's an existing value, just ignore the new value.
37     if (timeStamps.hasOwnProperty(name))
38       return;
40     timeStamps[name] = value;
41   },
43   /**
44    * Returns a JS object containing all of the timeStamps as properties (can be
45    * easily serialized to JSON). Used by TelemetryPing to retrieve the data
46    * to attach to the telemetry submission.
47    */
48   get: function TT_get() {
49     // Return a copy of the object by passing it through JSON.
50     return JSON.parse(JSON.stringify(timeStamps));
51   }