2011-09-15 11:20:22 -07:00
|
|
|
/* -*- Mode: js2; js2-basic-offset: 2; indent-tabs-mode: nil; -*- */
|
|
|
|
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-11-09 12:19:19 -08:00
|
|
|
|
|
|
|
let Cu = Components.utils;
|
|
|
|
let Ci = Components.interfaces;
|
|
|
|
let Cc = Components.classes;
|
2012-06-02 03:45:32 -07:00
|
|
|
|
2011-05-31 23:33:22 -07:00
|
|
|
// The maximum allowed number of concurrent timers per page.
|
|
|
|
const MAX_PAGE_TIMERS = 10000;
|
2010-11-09 12:19:19 -08:00
|
|
|
|
2012-06-02 03:45:32 -07:00
|
|
|
// The regular expression used to parse %s/%d and other placeholders for
|
|
|
|
// variables in strings that need to be interpolated.
|
|
|
|
const ARGUMENT_PATTERN = /%\d*\.?\d*([osdif])\b/g;
|
|
|
|
|
|
|
|
// The maximum stacktrace depth when populating the stacktrace array used for
|
|
|
|
// console.trace().
|
|
|
|
const DEFAULT_MAX_STACKTRACE_DEPTH = 200;
|
|
|
|
|
|
|
|
// The console API methods are async and their action is executed later. This
|
|
|
|
// delay tells how much later.
|
2012-06-12 04:25:07 -07:00
|
|
|
const CALL_DELAY = 15; // milliseconds
|
|
|
|
|
|
|
|
// This constant tells how many messages to process in a single timer execution.
|
|
|
|
const MESSAGES_IN_INTERVAL = 1500;
|
2012-06-02 03:45:32 -07:00
|
|
|
|
2010-11-09 12:19:19 -08:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2011-08-24 13:34:16 -07:00
|
|
|
Cu.import("resource://gre/modules/ConsoleAPIStorage.jsm");
|
2012-04-24 08:52:11 -07:00
|
|
|
Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
|
2010-11-09 12:19:19 -08:00
|
|
|
|
2012-06-12 04:25:07 -07:00
|
|
|
let gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
2012-06-02 03:45:32 -07:00
|
|
|
|
2010-11-09 12:19:19 -08:00
|
|
|
function ConsoleAPI() {}
|
|
|
|
ConsoleAPI.prototype = {
|
|
|
|
|
|
|
|
classID: Components.ID("{b49c18f8-3379-4fc0-8c90-d7772c1a9ff3}"),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer]),
|
|
|
|
|
2012-06-02 03:45:32 -07:00
|
|
|
_timerInitialized: false,
|
|
|
|
_queuedCalls: null,
|
|
|
|
_timerCallback: null,
|
|
|
|
_destroyedWindows: null,
|
|
|
|
|
2010-11-09 12:19:19 -08:00
|
|
|
// nsIDOMGlobalPropertyInitializer
|
|
|
|
init: function CA_init(aWindow) {
|
2011-05-31 23:33:22 -07:00
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown", false);
|
|
|
|
Services.obs.addObserver(this, "inner-window-destroyed", false);
|
|
|
|
|
2010-11-17 12:20:20 -08:00
|
|
|
let self = this;
|
2011-02-07 23:18:18 -08:00
|
|
|
let chromeObject = {
|
2010-11-17 12:20:20 -08:00
|
|
|
// window.console API
|
|
|
|
log: function CA_log() {
|
2012-04-24 08:52:11 -07:00
|
|
|
self.queueCall("log", arguments, aWindow);
|
2010-11-17 12:20:20 -08:00
|
|
|
},
|
|
|
|
info: function CA_info() {
|
2012-04-24 08:52:11 -07:00
|
|
|
self.queueCall("info", arguments, aWindow);
|
2010-11-17 12:20:20 -08:00
|
|
|
},
|
|
|
|
warn: function CA_warn() {
|
2012-04-24 08:52:11 -07:00
|
|
|
self.queueCall("warn", arguments, aWindow);
|
2010-11-17 12:20:20 -08:00
|
|
|
},
|
|
|
|
error: function CA_error() {
|
2012-04-24 08:52:11 -07:00
|
|
|
self.queueCall("error", arguments, aWindow);
|
2010-12-10 09:28:17 -08:00
|
|
|
},
|
2011-04-08 14:20:41 -07:00
|
|
|
debug: function CA_debug() {
|
2012-04-24 08:52:11 -07:00
|
|
|
self.queueCall("debug", arguments, aWindow);
|
2011-04-08 14:20:41 -07:00
|
|
|
},
|
2011-04-11 10:48:15 -07:00
|
|
|
trace: function CA_trace() {
|
2012-04-24 08:52:11 -07:00
|
|
|
self.queueCall("trace", arguments, aWindow);
|
2011-04-11 10:48:15 -07:00
|
|
|
},
|
2011-06-09 06:27:30 -07:00
|
|
|
// Displays an interactive listing of all the properties of an object.
|
|
|
|
dir: function CA_dir() {
|
2012-04-24 08:52:11 -07:00
|
|
|
self.queueCall("dir", arguments, aWindow);
|
2011-06-09 06:27:30 -07:00
|
|
|
},
|
2011-06-14 04:38:11 -07:00
|
|
|
group: function CA_group() {
|
2012-04-24 08:52:11 -07:00
|
|
|
self.queueCall("group", arguments, aWindow);
|
2011-06-14 04:38:11 -07:00
|
|
|
},
|
|
|
|
groupCollapsed: function CA_groupCollapsed() {
|
2012-04-24 08:52:11 -07:00
|
|
|
self.queueCall("groupCollapsed", arguments, aWindow);
|
2011-06-14 04:38:11 -07:00
|
|
|
},
|
|
|
|
groupEnd: function CA_groupEnd() {
|
2012-04-24 08:52:11 -07:00
|
|
|
self.queueCall("groupEnd", arguments, aWindow);
|
2011-06-14 04:38:11 -07:00
|
|
|
},
|
2011-05-31 23:33:22 -07:00
|
|
|
time: function CA_time() {
|
2012-04-24 08:52:11 -07:00
|
|
|
self.queueCall("time", arguments, aWindow);
|
2011-05-31 23:33:22 -07:00
|
|
|
},
|
|
|
|
timeEnd: function CA_timeEnd() {
|
2012-04-24 08:52:11 -07:00
|
|
|
self.queueCall("timeEnd", arguments, aWindow);
|
2011-05-31 23:33:22 -07:00
|
|
|
},
|
2011-02-07 23:18:18 -08:00
|
|
|
__exposedProps__: {
|
|
|
|
log: "r",
|
|
|
|
info: "r",
|
|
|
|
warn: "r",
|
2011-04-08 14:20:41 -07:00
|
|
|
error: "r",
|
|
|
|
debug: "r",
|
2011-04-11 10:48:15 -07:00
|
|
|
trace: "r",
|
2011-06-14 04:38:11 -07:00
|
|
|
dir: "r",
|
|
|
|
group: "r",
|
|
|
|
groupCollapsed: "r",
|
2011-05-31 23:33:22 -07:00
|
|
|
groupEnd: "r",
|
|
|
|
time: "r",
|
|
|
|
timeEnd: "r"
|
2011-02-07 23:18:18 -08:00
|
|
|
}
|
2010-11-17 12:20:20 -08:00
|
|
|
};
|
2011-02-07 23:18:18 -08:00
|
|
|
|
|
|
|
// We need to return an actual content object here, instead of a wrapped
|
|
|
|
// chrome object. This allows things like console.log.bind() to work.
|
2011-05-03 13:43:08 -07:00
|
|
|
let contentObj = Cu.createObjectIn(aWindow);
|
|
|
|
function genPropDesc(fun) {
|
|
|
|
return { enumerable: true, configurable: true, writable: true,
|
|
|
|
value: chromeObject[fun].bind(chromeObject) };
|
|
|
|
}
|
|
|
|
const properties = {
|
|
|
|
log: genPropDesc('log'),
|
|
|
|
info: genPropDesc('info'),
|
|
|
|
warn: genPropDesc('warn'),
|
|
|
|
error: genPropDesc('error'),
|
|
|
|
debug: genPropDesc('debug'),
|
|
|
|
trace: genPropDesc('trace'),
|
2011-06-09 06:27:30 -07:00
|
|
|
dir: genPropDesc('dir'),
|
2011-06-14 04:38:11 -07:00
|
|
|
group: genPropDesc('group'),
|
|
|
|
groupCollapsed: genPropDesc('groupCollapsed'),
|
|
|
|
groupEnd: genPropDesc('groupEnd'),
|
2011-05-31 23:33:22 -07:00
|
|
|
time: genPropDesc('time'),
|
|
|
|
timeEnd: genPropDesc('timeEnd'),
|
2011-05-03 13:43:08 -07:00
|
|
|
__noSuchMethod__: { enumerable: true, configurable: true, writable: true,
|
|
|
|
value: function() {} },
|
|
|
|
__mozillaConsole__: { value: true }
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.defineProperties(contentObj, properties);
|
|
|
|
Cu.makeObjectPropsNormal(contentObj);
|
2011-02-07 23:18:18 -08:00
|
|
|
|
2012-06-02 03:45:32 -07:00
|
|
|
this._queuedCalls = [];
|
|
|
|
this._destroyedWindows = [];
|
|
|
|
|
2011-05-03 13:43:08 -07:00
|
|
|
return contentObj;
|
2010-11-09 12:19:19 -08:00
|
|
|
},
|
|
|
|
|
2011-05-31 23:33:22 -07:00
|
|
|
observe: function CA_observe(aSubject, aTopic, aData)
|
|
|
|
{
|
|
|
|
if (aTopic == "xpcom-shutdown") {
|
|
|
|
Services.obs.removeObserver(this, "xpcom-shutdown");
|
|
|
|
Services.obs.removeObserver(this, "inner-window-destroyed");
|
2012-06-02 03:45:32 -07:00
|
|
|
this._destroyedWindows = [];
|
|
|
|
this._queuedCalls = [];
|
2012-06-12 04:25:07 -07:00
|
|
|
gTimer = null;
|
2011-05-31 23:33:22 -07:00
|
|
|
}
|
|
|
|
else if (aTopic == "inner-window-destroyed") {
|
|
|
|
let innerWindowID = aSubject.QueryInterface(Ci.nsISupportsPRUint64).data;
|
|
|
|
delete this.timerRegistry[innerWindowID + ""];
|
2012-06-02 03:45:32 -07:00
|
|
|
this._destroyedWindows.push(innerWindowID);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queue a call to a console method. See the CALL_DELAY constant.
|
|
|
|
*
|
|
|
|
* @param string aMethod
|
|
|
|
* The console method the code has invoked.
|
|
|
|
* @param object aArguments
|
|
|
|
* The arguments passed to the console method.
|
2012-04-24 08:52:11 -07:00
|
|
|
* @param object aWindow
|
|
|
|
* The window from where the console method was called.
|
2012-06-02 03:45:32 -07:00
|
|
|
*/
|
2012-04-24 08:52:11 -07:00
|
|
|
queueCall: function CA_queueCall(aMethod, aArguments, aWindow)
|
2012-06-02 03:45:32 -07:00
|
|
|
{
|
2012-04-24 08:52:11 -07:00
|
|
|
let outerID;
|
|
|
|
let innerID;
|
|
|
|
try {
|
|
|
|
let windowUtils = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
|
|
|
|
outerID = windowUtils.outerWindowID;
|
|
|
|
innerID = windowUtils.currentInnerWindowID;
|
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
Cu.reportError(ex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-02 03:45:32 -07:00
|
|
|
let metaForCall = {
|
2012-04-24 08:52:11 -07:00
|
|
|
outerID: outerID,
|
|
|
|
innerID: innerID,
|
|
|
|
isPrivate: PrivateBrowsingUtils.isWindowPrivate(aWindow),
|
2012-06-02 03:45:32 -07:00
|
|
|
timeStamp: Date.now(),
|
|
|
|
stack: this.getStackTrace(aMethod != "trace" ? 1 : null),
|
|
|
|
};
|
|
|
|
|
|
|
|
this._queuedCalls.push([aMethod, aArguments, metaForCall]);
|
|
|
|
|
|
|
|
if (!this._timerInitialized) {
|
2012-06-12 04:25:07 -07:00
|
|
|
gTimer.initWithCallback(this._timerCallback.bind(this), CALL_DELAY,
|
|
|
|
Ci.nsITimer.TYPE_REPEATING_SLACK);
|
2012-06-02 03:45:32 -07:00
|
|
|
this._timerInitialized = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Timer callback used to process each of the queued calls.
|
|
|
|
* @private
|
|
|
|
*/
|
2012-06-12 04:25:07 -07:00
|
|
|
_timerCallback: function CA__timerCallback()
|
2012-06-02 03:45:32 -07:00
|
|
|
{
|
2012-06-12 04:25:07 -07:00
|
|
|
this._queuedCalls.splice(0, MESSAGES_IN_INTERVAL)
|
|
|
|
.forEach(this._processQueuedCall, this);
|
|
|
|
|
|
|
|
if (!this._queuedCalls.length) {
|
|
|
|
this._timerInitialized = false;
|
|
|
|
this._destroyedWindows = [];
|
|
|
|
gTimer.cancel();
|
|
|
|
}
|
2012-06-02 03:45:32 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a queued call to a console method.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param array aCall
|
|
|
|
* Array that holds information about the queued call.
|
|
|
|
*/
|
|
|
|
_processQueuedCall: function CA__processQueuedItem(aCall)
|
|
|
|
{
|
|
|
|
let [method, args, meta] = aCall;
|
|
|
|
|
|
|
|
let notifyMeta = {
|
|
|
|
outerID: meta.outerID,
|
|
|
|
innerID: meta.innerID,
|
2012-04-24 08:52:11 -07:00
|
|
|
isPrivate: meta.isPrivate,
|
2012-06-02 03:45:32 -07:00
|
|
|
timeStamp: meta.timeStamp,
|
|
|
|
frame: meta.stack[0],
|
|
|
|
};
|
|
|
|
|
|
|
|
let notifyArguments = null;
|
|
|
|
|
|
|
|
switch (method) {
|
|
|
|
case "log":
|
|
|
|
case "info":
|
|
|
|
case "warn":
|
|
|
|
case "error":
|
|
|
|
case "debug":
|
|
|
|
notifyArguments = this.processArguments(args);
|
|
|
|
break;
|
|
|
|
case "trace":
|
|
|
|
notifyArguments = meta.stack;
|
|
|
|
break;
|
|
|
|
case "group":
|
|
|
|
case "groupCollapsed":
|
|
|
|
notifyArguments = this.beginGroup(args);
|
|
|
|
break;
|
|
|
|
case "groupEnd":
|
|
|
|
case "dir":
|
|
|
|
notifyArguments = args;
|
|
|
|
break;
|
|
|
|
case "time":
|
|
|
|
notifyArguments = this.startTimer(meta.innerID, args[0], meta.timeStamp);
|
|
|
|
break;
|
|
|
|
case "timeEnd":
|
|
|
|
notifyArguments = this.stopTimer(meta.innerID, args[0], meta.timeStamp);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// unknown console API method!
|
|
|
|
return;
|
2011-05-31 23:33:22 -07:00
|
|
|
}
|
2012-06-02 03:45:32 -07:00
|
|
|
|
|
|
|
this.notifyObservers(method, notifyArguments, notifyMeta);
|
2011-05-31 23:33:22 -07:00
|
|
|
},
|
|
|
|
|
2010-11-09 12:19:19 -08:00
|
|
|
/**
|
2011-08-24 13:34:16 -07:00
|
|
|
* Notify all observers of any console API call.
|
|
|
|
*
|
|
|
|
* @param string aLevel
|
|
|
|
* The message level.
|
|
|
|
* @param mixed aArguments
|
|
|
|
* The arguments given to the console API call.
|
2012-06-02 03:45:32 -07:00
|
|
|
* @param object aMeta
|
|
|
|
* Object that holds metadata about the console API call:
|
|
|
|
* - outerID - the outer ID of the window where the message came from.
|
|
|
|
* - innerID - the inner ID of the window where the message came from.
|
2012-04-24 08:52:11 -07:00
|
|
|
* - isPrivate - Whether the window is in private browsing mode.
|
2012-06-02 03:45:32 -07:00
|
|
|
* - frame - the youngest content frame in the call stack.
|
|
|
|
* - timeStamp - when the console API call occurred.
|
|
|
|
*/
|
|
|
|
notifyObservers: function CA_notifyObservers(aLevel, aArguments, aMeta) {
|
2010-11-09 12:19:19 -08:00
|
|
|
let consoleEvent = {
|
2012-06-02 03:45:32 -07:00
|
|
|
ID: aMeta.outerID,
|
|
|
|
innerID: aMeta.innerID,
|
2010-11-09 12:19:19 -08:00
|
|
|
level: aLevel,
|
2012-06-02 03:45:32 -07:00
|
|
|
filename: aMeta.frame.filename,
|
|
|
|
lineNumber: aMeta.frame.lineNumber,
|
|
|
|
functionName: aMeta.frame.functionName,
|
2012-01-11 02:51:49 -08:00
|
|
|
arguments: aArguments,
|
2012-06-02 03:45:32 -07:00
|
|
|
timeStamp: aMeta.timeStamp,
|
2010-11-09 12:19:19 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
consoleEvent.wrappedJSObject = consoleEvent;
|
|
|
|
|
2012-04-24 08:52:11 -07:00
|
|
|
// Store non-private messages for which the inner window was not destroyed.
|
|
|
|
if (!aMeta.isPrivate && this._destroyedWindows.indexOf(aMeta.innerID) == -1) {
|
2012-06-02 03:45:32 -07:00
|
|
|
ConsoleAPIStorage.recordEvent(aMeta.innerID, consoleEvent);
|
|
|
|
}
|
2011-08-24 13:34:16 -07:00
|
|
|
|
2012-06-02 03:45:32 -07:00
|
|
|
Services.obs.notifyObservers(consoleEvent, "console-api-log-event",
|
|
|
|
aMeta.outerID);
|
2011-04-11 10:48:15 -07:00
|
|
|
},
|
|
|
|
|
2011-09-15 11:20:22 -07:00
|
|
|
/**
|
|
|
|
* Process the console API call arguments in order to perform printf-like
|
|
|
|
* string substitution.
|
|
|
|
* TODO: object substitution should display an interactive property list (bug
|
|
|
|
* 685815) and width and precision qualifiers should be taken into account
|
|
|
|
* (bug 685813).
|
|
|
|
*
|
|
|
|
* @param mixed aArguments
|
|
|
|
* The arguments given to the console API call.
|
|
|
|
**/
|
|
|
|
processArguments: function CA_processArguments(aArguments) {
|
2012-06-02 03:45:32 -07:00
|
|
|
if (aArguments.length < 2 || typeof aArguments[0] != "string") {
|
2011-09-15 11:20:22 -07:00
|
|
|
return aArguments;
|
|
|
|
}
|
|
|
|
let args = Array.prototype.slice.call(aArguments);
|
|
|
|
let format = args.shift();
|
|
|
|
// Format specification regular expression.
|
2012-06-02 03:45:32 -07:00
|
|
|
let processed = format.replace(ARGUMENT_PATTERN, function CA_PA_substitute(match, submatch) {
|
|
|
|
switch (submatch) {
|
2011-09-15 11:20:22 -07:00
|
|
|
case "o":
|
|
|
|
case "s":
|
2011-10-07 05:29:33 -07:00
|
|
|
return String(args.shift());
|
2011-09-15 11:20:22 -07:00
|
|
|
case "d":
|
|
|
|
case "i":
|
|
|
|
return parseInt(args.shift());
|
|
|
|
case "f":
|
|
|
|
return parseFloat(args.shift());
|
|
|
|
default:
|
2012-06-02 03:45:32 -07:00
|
|
|
return submatch;
|
2011-09-15 11:20:22 -07:00
|
|
|
};
|
|
|
|
});
|
|
|
|
args.unshift(processed);
|
|
|
|
return args;
|
|
|
|
},
|
|
|
|
|
2011-04-11 10:48:15 -07:00
|
|
|
/**
|
|
|
|
* Build the stacktrace array for the console.trace() call.
|
|
|
|
*
|
2012-06-02 03:45:32 -07:00
|
|
|
* @param number [aMaxDepth=DEFAULT_MAX_STACKTRACE_DEPTH]
|
|
|
|
* Optional maximum stacktrace depth.
|
2011-04-11 10:48:15 -07:00
|
|
|
* @return array
|
|
|
|
* Each element is a stack frame that holds the following properties:
|
|
|
|
* filename, lineNumber, functionName and language.
|
2012-06-02 03:45:32 -07:00
|
|
|
*/
|
|
|
|
getStackTrace: function CA_getStackTrace(aMaxDepth) {
|
|
|
|
if (!aMaxDepth) {
|
|
|
|
aMaxDepth = DEFAULT_MAX_STACKTRACE_DEPTH;
|
|
|
|
}
|
|
|
|
|
2011-04-11 10:48:15 -07:00
|
|
|
let stack = [];
|
2012-06-02 03:45:32 -07:00
|
|
|
let frame = Components.stack.caller.caller;
|
2011-04-11 10:48:15 -07:00
|
|
|
while (frame = frame.caller) {
|
|
|
|
if (frame.language == Ci.nsIProgrammingLanguage.JAVASCRIPT ||
|
|
|
|
frame.language == Ci.nsIProgrammingLanguage.JAVASCRIPT2) {
|
|
|
|
stack.push({
|
|
|
|
filename: frame.filename,
|
|
|
|
lineNumber: frame.lineNumber,
|
|
|
|
functionName: frame.name,
|
|
|
|
language: frame.language,
|
|
|
|
});
|
2012-06-02 03:45:32 -07:00
|
|
|
if (stack.length == aMaxDepth) {
|
|
|
|
break;
|
|
|
|
}
|
2011-04-11 10:48:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stack;
|
2011-06-14 04:38:11 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Begin a new group for logging output together.
|
|
|
|
**/
|
|
|
|
beginGroup: function CA_beginGroup() {
|
|
|
|
return Array.prototype.join.call(arguments[0], " ");
|
2011-05-31 23:33:22 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A registry of started timers. It contains a map of pages (defined by their
|
|
|
|
* inner window IDs) to timer maps. Timer maps are key-value pairs of timer
|
|
|
|
* names to timer start times, for all timers defined in that page. Timer
|
|
|
|
* names are prepended with the inner window ID in order to avoid conflicts
|
|
|
|
* with Object.prototype functions.
|
|
|
|
*/
|
|
|
|
timerRegistry: {},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new timer by recording the current time under the specified name.
|
|
|
|
*
|
|
|
|
* @param number aWindowId
|
|
|
|
* The inner ID of the window.
|
|
|
|
* @param string aName
|
|
|
|
* The name of the timer.
|
2012-06-02 03:45:32 -07:00
|
|
|
* @param number [aTimestamp=Date.now()]
|
|
|
|
* Optional timestamp that tells when the timer was originally started.
|
2011-05-31 23:33:22 -07:00
|
|
|
* @return object
|
|
|
|
* The name property holds the timer name and the started property
|
|
|
|
* holds the time the timer was started. In case of error, it returns
|
|
|
|
* an object with the single property "error" that contains the key
|
|
|
|
* for retrieving the localized error message.
|
|
|
|
**/
|
2012-06-02 03:45:32 -07:00
|
|
|
startTimer: function CA_startTimer(aWindowId, aName, aTimestamp) {
|
2011-05-31 23:33:22 -07:00
|
|
|
if (!aName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let innerID = aWindowId + "";
|
|
|
|
if (!this.timerRegistry[innerID]) {
|
|
|
|
this.timerRegistry[innerID] = {};
|
|
|
|
}
|
|
|
|
let pageTimers = this.timerRegistry[innerID];
|
|
|
|
if (Object.keys(pageTimers).length > MAX_PAGE_TIMERS - 1) {
|
|
|
|
return { error: "maxTimersExceeded" };
|
|
|
|
}
|
|
|
|
let key = aWindowId + "-" + aName.toString();
|
|
|
|
if (!pageTimers[key]) {
|
2012-06-02 03:45:32 -07:00
|
|
|
pageTimers[key] = aTimestamp || Date.now();
|
2011-05-31 23:33:22 -07:00
|
|
|
}
|
|
|
|
return { name: aName, started: pageTimers[key] };
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop the timer with the specified name and retrieve the elapsed time.
|
|
|
|
*
|
|
|
|
* @param number aWindowId
|
|
|
|
* The inner ID of the window.
|
|
|
|
* @param string aName
|
|
|
|
* The name of the timer.
|
2012-06-02 03:45:32 -07:00
|
|
|
* @param number [aTimestamp=Date.now()]
|
|
|
|
* Optional timestamp that tells when the timer was originally stopped.
|
2011-05-31 23:33:22 -07:00
|
|
|
* @return object
|
|
|
|
* The name property holds the timer name and the duration property
|
|
|
|
* holds the number of milliseconds since the timer was started.
|
|
|
|
**/
|
2012-06-02 03:45:32 -07:00
|
|
|
stopTimer: function CA_stopTimer(aWindowId, aName, aTimestamp) {
|
2011-05-31 23:33:22 -07:00
|
|
|
if (!aName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let innerID = aWindowId + "";
|
|
|
|
let pageTimers = this.timerRegistry[innerID];
|
|
|
|
if (!pageTimers) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let key = aWindowId + "-" + aName.toString();
|
|
|
|
if (!pageTimers[key]) {
|
|
|
|
return;
|
|
|
|
}
|
2012-06-02 03:45:32 -07:00
|
|
|
let duration = (aTimestamp || Date.now()) - pageTimers[key];
|
2011-05-31 23:33:22 -07:00
|
|
|
delete pageTimers[key];
|
|
|
|
return { name: aName, duration: duration };
|
2011-05-12 05:29:17 -07:00
|
|
|
}
|
2010-11-09 12:19:19 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
let NSGetFactory = XPCOMUtils.generateNSGetFactory([ConsoleAPI]);
|