Bug 568629: add a ConsoleAPI component used to implement window.console object exposed to web content (disabled for the moment), r=gavin/dietrich, sr=jst, a=blocking

--HG--
extra : rebase_source : 9bfe8478039ee16310f9f87da87c66d778739a73
This commit is contained in:
David Dahl 2010-11-09 15:19:19 -05:00
parent 7307c2f4d5
commit 5aced17f45
9 changed files with 296 additions and 3 deletions

View File

@ -271,6 +271,8 @@
@BINPATH@/components/zipwriter.xpt
; JavaScript components
@BINPATH@/components/ConsoleAPI.manifest
@BINPATH@/components/ConsoleAPI.js
@BINPATH@/components/FeedProcessor.manifest
@BINPATH@/components/FeedProcessor.js
@BINPATH@/components/BrowserFeeds.manifest

106
dom/base/ConsoleAPI.js Normal file
View File

@ -0,0 +1,106 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Console API code.
*
* The Initial Developer of the Original Code is Mozilla Foundation
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* David Dahl <ddahl@mozilla.com> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
let Cu = Components.utils;
let Ci = Components.interfaces;
let Cc = Components.classes;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
function ConsoleAPI() {}
ConsoleAPI.prototype = {
classID: Components.ID("{b49c18f8-3379-4fc0-8c90-d7772c1a9ff3}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer]),
// The ID of our attached window
id: null,
// nsIDOMGlobalPropertyInitializer
// Associates us with the window we're attached to.
init: function CA_init(aWindow) {
try {
this.id = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils)
.outerWindowID;
} catch (ex) {
Cu.reportError(ex);
}
return this;
},
// window.console API
log: function CA_log() {
this.notifyObservers("log", arguments);
},
info: function CA_info() {
this.notifyObservers("info", arguments);
},
warn: function CA_warn() {
this.notifyObservers("warn", arguments);
},
error: function CA_error() {
this.notifyObservers("error", arguments);
},
__exposedProps__: {
log: "r",
info: "r",
warn: "r",
error: "r"
},
/**
* Notify all observers of any console API call
**/
notifyObservers: function CA_notifyObservers(aLevel, aArguments) {
let consoleEvent = {
ID: this.id,
level: aLevel,
arguments: aArguments
};
consoleEvent.wrappedJSObject = consoleEvent;
Services.obs.notifyObservers(consoleEvent,
"console-api-log-event", this.id);
}
};
let NSGetFactory = XPCOMUtils.generateNSGetFactory([ConsoleAPI]);

View File

@ -0,0 +1,4 @@
component {b49c18f8-3379-4fc0-8c90-d7772c1a9ff3} ConsoleAPI.js
contract @mozilla.org/console-api;1 {b49c18f8-3379-4fc0-8c90-d7772c1a9ff3}
# disabled for now
# category JavaScript-global-property console @mozilla.org/console-api;1

View File

@ -47,6 +47,11 @@ LIBRARY_NAME = jsdombase_s
LIBXUL_LIBRARY = 1
FORCE_STATIC_LIB = 1
EXTRA_PP_COMPONENTS = \
ConsoleAPI.js \
ConsoleAPI.manifest \
$(NULL)
XPIDLSRCS = \
nsIEntropyCollector.idl \
nsIScriptChannel.idl \

View File

@ -48,6 +48,8 @@ _BROWSER_FILES = \
browser_focus_steal_from_chrome.js \
browser_focus_steal_from_chrome_during_mousedown.js \
browser_autofocus_background.js \
browser_ConsoleAPITests.js \
test-console-api.html \
$(NULL)
libs:: $(_BROWSER_FILES)

View File

@ -0,0 +1,159 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is DevTools test code.
*
* The Initial Developer of the Original Code is Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* David Dahl <ddahl@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
const TEST_URI = "http://example.com/browser/dom/tests/browser/test-console-api.html";
var gWindow;
function test() {
// FIXME
todo(false, "This test is disabled until bug 587734 lands");
return;
waitForExplicitFinish();
var tab = gBrowser.addTab(TEST_URI);
gBrowser.selectedTab = tab;
var browser = gBrowser.selectedBrowser;
registerCleanupFunction(function () {
gBrowser.removeTab(tab);
});
ConsoleObserver.init();
browser.addEventListener("DOMContentLoaded", function onLoad(event) {
browser.removeEventListener("DOMContentLoaded", onLoad, false);
executeSoon(function test_executeSoon() {
gWindow = browser.contentWindow;
consoleAPISanityTest();
observeConsoleTest();
});
}, false);
}
var gWindow;
function testConsoleData(aMessageObject) {
let messageWindow = getWindowByWindowId(aMessageObject.ID);
is(messageWindow, gWindow, "found correct window by window ID");
is(aMessageObject.level, gLevel, "expected level received");
ok(aMessageObject.arguments, "we have arguments");
is(aMessageObject.arguments.length, gArgs.length, "arguments.length matches");
gArgs.forEach(function (a, i) {
is(aMessageObject.arguments[i], a, "correct arg " + i);
});
if (aMessageObject.level == "error") {
// Test finished
ConsoleObserver.destroy();
finish();
}
}
var gLevel, gArgs;
function expect(level) {
gLevel = level;
gArgs = Array.slice(arguments, 1);
}
function observeConsoleTest() {
let win = XPCNativeWrapper.unwrap(gWindow);
expect("log", "arg");
win.console.log("arg");
expect("info", "arg", "extra arg");
win.console.info("arg", "extra arg");
expect("warn", "arg", "extra arg", 1);
win.console.warn("arg", "extra arg", 1);
expect("error", "arg");
win.console.error("arg");
}
function consoleAPISanityTest() {
let win = XPCNativeWrapper.unwrap(gWindow);
ok(win.console, "we have a console attached");
ok(win.console, "we have a console attached, 2nd attempt");
ok(win.console.log, "console.log is here");
ok(win.console.info, "console.info is here");
ok(win.console.warn, "console.warn is here");
ok(win.console.error, "console.error is here");
}
var ConsoleObserver = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
init: function CO_init() {
Services.obs.addObserver(this, "console-api-log-event", false);
},
destroy: function CO_destroy() {
Services.obs.removeObserver(this, "console-api-log-event");
},
observe: function CO_observe(aSubject, aTopic, aData) {
try {
testConsoleData(aSubject.wrappedJSObject);
} catch (ex) {
// XXX Exceptions in this function currently aren't reported, because of
// some XPConnect weirdness, so report them manually
ok(false, "Exception thrown in CO_observe: " + ex);
}
}
};
function getWindowId(aWindow)
{
return aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils)
.outerWindowID;
}
function getWindowByWindowId(aId) {
let someWindow = Services.wm.getMostRecentWindow("navigator:browser");
if (someWindow) {
let windowUtils = someWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
return windowUtils.getOuterWindowWithId(aId);
}
return null;
}

View File

@ -0,0 +1,18 @@
<!DOCTYPE HTML>
<html dir="ltr" xml:lang="en-US" lang="en-US"><head>
<title>Console API test page</title>
<script type="text/javascript">
function test() {
var str = "Test Message."
console.log(str);
console.info(str);
console.warn(str);
console.error(str);
}
</script>
</head>
<body>
<h1>Console API Test Page</h1>
<button onclick="test();">Log stuff</button>
</body>
</html>

View File

@ -43,8 +43,6 @@ VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = hudservice
EXTRA_JS_MODULES = HUDService.jsm \
PropertyPanel.jsm \
$(NULL)

View File

@ -45,7 +45,6 @@ relativesrcdir = toolkit/components/console/hudservice/tests/browser
include $(DEPTH)/config/autoconf.mk
include $(topsrcdir)/config/rules.mk
_BROWSER_TEST_FILES = \
browser_webconsole_bug_580030_errors_after_page_reload.js \
browser_webconsole_basic_net_logging.js \