Skip to content

Commit

Permalink
Bug 993520: Add innerID option to ConsoleAPI to allow creating consol…
Browse files Browse the repository at this point in the history
…es for specific DOM windows. r=msucan
  • Loading branch information
Mossop committed Apr 15, 2014
1 parent 8cda837 commit 169120e
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 8 deletions.
19 changes: 12 additions & 7 deletions toolkit/devtools/Console.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ function createDumper(aLevel) {
}
let args = Array.prototype.slice.call(arguments, 0);
let frame = getStack(Components.stack.caller, 1)[0];
sendConsoleAPIMessage(aLevel, frame, args);
sendConsoleAPIMessage(this, aLevel, frame, args);
let data = args.map(function(arg) {
return stringify(arg, true);
});
Expand All @@ -493,7 +493,7 @@ function createMultiLineDumper(aLevel) {
dumpMessage(this, aLevel, "");
let args = Array.prototype.slice.call(arguments, 0);
let frame = getStack(Components.stack.caller, 1)[0];
sendConsoleAPIMessage(aLevel, frame, args);
sendConsoleAPIMessage(this, aLevel, frame, args);
args.forEach(function(arg) {
this.dump(log(arg));
}, this);
Expand All @@ -504,6 +504,8 @@ function createMultiLineDumper(aLevel) {
* Send a Console API message. This function will send a console-api-log-event
* notification through the nsIObserverService.
*
* @param {object} aConsole
* The instance of ConsoleAPI performing the logging.
* @param {string} aLevel
* Message severity level. This is usually the name of the console method
* that was called.
Expand All @@ -519,11 +521,11 @@ function createMultiLineDumper(aLevel) {
* - stacktrace: for trace(). Holds the array of stack frames as given by
* getStack().
*/
function sendConsoleAPIMessage(aLevel, aFrame, aArgs, aOptions = {})
function sendConsoleAPIMessage(aConsole, aLevel, aFrame, aArgs, aOptions = {})
{
let consoleEvent = {
ID: "jsm",
innerID: aFrame.filename,
innerID: aConsole.innerID || aFrame.filename,
level: aLevel,
filename: aFrame.filename,
lineNumber: aFrame.lineNumber,
Expand Down Expand Up @@ -578,6 +580,8 @@ function sendConsoleAPIMessage(aLevel, aFrame, aArgs, aOptions = {})
* LOG_LEVELS, no message will be logged
* - dump {function} : An optional function to intercept all strings
* written to stdout
* - innerID {string}: An ID representing the source of the message.
* Normally the inner ID of a DOM window.
* @return {object}
* A console API instance object
*/
Expand All @@ -587,6 +591,7 @@ function ConsoleAPI(aConsoleOptions = {}) {
this.dump = aConsoleOptions.dump || dump;
this.prefix = aConsoleOptions.prefix || "";
this.maxLogLevel = aConsoleOptions.maxLogLevel || "all";
this.innerID = aConsoleOptions.innerID || null;

// Bind all the functions to this object.
for (let prop in this) {
Expand All @@ -610,7 +615,7 @@ ConsoleAPI.prototype = {
}
let args = Array.prototype.slice.call(arguments, 0);
let trace = getStack(Components.stack.caller);
sendConsoleAPIMessage("trace", trace[0], args,
sendConsoleAPIMessage(this, "trace", trace[0], args,
{ stacktrace: trace });
dumpMessage(this, "trace", "\n" + formatTrace(trace));
},
Expand All @@ -628,7 +633,7 @@ ConsoleAPI.prototype = {
let args = Array.prototype.slice.call(arguments, 0);
let frame = getStack(Components.stack.caller, 1)[0];
let timer = startTimer(args[0]);
sendConsoleAPIMessage("time", frame, args, { timer: timer });
sendConsoleAPIMessage(this, "time", frame, args, { timer: timer });
dumpMessage(this, "time",
"'" + timer.name + "' @ " + (new Date()));
},
Expand All @@ -640,7 +645,7 @@ ConsoleAPI.prototype = {
let args = Array.prototype.slice.call(arguments, 0);
let frame = getStack(Components.stack.caller, 1)[0];
let timer = stopTimer(args[0]);
sendConsoleAPIMessage("timeEnd", frame, args, { timer: timer });
sendConsoleAPIMessage(this, "timeEnd", frame, args, { timer: timer });
dumpMessage(this, "timeEnd",
"'" + timer.name + "' " + timer.duration + "ms");
},
Expand Down
1 change: 1 addition & 0 deletions toolkit/devtools/webconsole/test/chrome.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ support-files =
[test_bug819670_getter_throws.html]
[test_cached_messages.html]
[test_consoleapi.html]
[test_consoleapi_innerID.html]
[test_file_uri.html]
[test_reflow.html]
[test_jsterm.html]
Expand Down
164 changes: 164 additions & 0 deletions toolkit/devtools/webconsole/test/test_consoleapi_innerID.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf8">
<title>Test for the innerID property of the Console API</title>
<script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript;version=1.8" src="common.js"></script>
<!-- Any copyright is dedicated to the Public Domain.
- http://creativecommons.org/publicdomain/zero/1.0/ -->
</head>
<body>
<p>Test for the Console API</p>

<script class="testbody" type="text/javascript;version=1.8">
SimpleTest.waitForExplicitFinish();

let expectedConsoleCalls = [];

function doConsoleCalls(aState)
{
let { ConsoleAPI } = Cu.import("resource://gre/modules/devtools/Console.jsm", {});
let console = new ConsoleAPI({
innerID: window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils)
.currentInnerWindowID
});

let longString = (new Array(DebuggerServer.LONG_STRING_LENGTH + 2)).join("a");

console.log("foobarBaz-log", undefined);
console.info("foobarBaz-info", null);
console.warn("foobarBaz-warn", top.document.documentElement);
console.debug(null);
console.trace();
console.dir(top.document, top.location);
console.log("foo", longString);

expectedConsoleCalls = [
{
level: "log",
filename: /test_consoleapi/,
functionName: "doConsoleCalls",
timeStamp: /^\d+$/,
arguments: ["foobarBaz-log", { type: "undefined" }],
},
{
level: "info",
filename: /test_consoleapi/,
functionName: "doConsoleCalls",
timeStamp: /^\d+$/,
arguments: ["foobarBaz-info", { type: "null" }],
},
{
level: "warn",
filename: /test_consoleapi/,
functionName: "doConsoleCalls",
timeStamp: /^\d+$/,
arguments: ["foobarBaz-warn", { type: "object", actor: /[a-z]/ }],
},
{
level: "debug",
filename: /test_consoleapi/,
functionName: "doConsoleCalls",
timeStamp: /^\d+$/,
arguments: [{ type: "null" }],
},
{
level: "trace",
filename: /test_consoleapi/,
functionName: "doConsoleCalls",
timeStamp: /^\d+$/,
stacktrace: [
{
filename: /test_consoleapi/,
functionName: "doConsoleCalls",
},
{
filename: /test_consoleapi/,
functionName: "onAttach",
},
],
},
{
level: "dir",
filename: /test_consoleapi/,
functionName: "doConsoleCalls",
timeStamp: /^\d+$/,
arguments: [
{
type: "object",
actor: /[a-z]/,
class: "XULDocument",
},
{
type: "object",
actor: /[a-z]/,
class: "Location",
}
],
},
{
level: "log",
filename: /test_consoleapi/,
functionName: "doConsoleCalls",
timeStamp: /^\d+$/,
arguments: [
"foo",
{
type: "longString",
initial: longString.substring(0,
DebuggerServer.LONG_STRING_INITIAL_LENGTH),
length: longString.length,
actor: /[a-z]/,
},
],
},
];
}

function startTest()
{
removeEventListener("load", startTest);

attachConsole(["ConsoleAPI"], onAttach, true);
}

function onAttach(aState, aResponse)
{
onConsoleAPICall = onConsoleAPICall.bind(null, aState);
aState.dbgClient.addListener("consoleAPICall", onConsoleAPICall);
doConsoleCalls(aState.actor);
}

let consoleCalls = [];

function onConsoleAPICall(aState, aType, aPacket)
{
info("received message level: " + aPacket.message.level);
is(aPacket.from, aState.actor, "console API call actor");

consoleCalls.push(aPacket.message);
if (consoleCalls.length != expectedConsoleCalls.length) {
return;
}

aState.dbgClient.removeListener("consoleAPICall", onConsoleAPICall);

expectedConsoleCalls.forEach(function(aMessage, aIndex) {
info("checking received console call #" + aIndex);
checkConsoleAPICall(consoleCalls[aIndex], expectedConsoleCalls[aIndex]);
});


consoleCalls = [];

closeDebugger(aState, function() {
SimpleTest.finish();
});
}

addEventListener("load", startTest);
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion toolkit/devtools/webconsole/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,7 @@ ConsoleAPIListener.prototype =

let apiMessage = aMessage.wrappedJSObject;
if (this.window) {
let msgWindow = Services.wm.getOuterWindowWithId(apiMessage.ID);
let msgWindow = Services.wm.getCurrentInnerWindowWithId(apiMessage.innerID);
if (!msgWindow || !this.layoutHelpers.isIncludedInTopLevelWindow(msgWindow)) {
// Not the same window!
return;
Expand Down

0 comments on commit 169120e

Please sign in to comment.