From 5aced17f4592ace81b32fcf8884e27cd1744bb5d Mon Sep 17 00:00:00 2001 From: David Dahl Date: Tue, 9 Nov 2010 15:19:19 -0500 Subject: [PATCH] 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 --- browser/installer/package-manifest.in | 2 + dom/base/ConsoleAPI.js | 106 ++++++++++++ dom/base/ConsoleAPI.manifest | 4 + dom/base/Makefile.in | 5 + dom/tests/browser/Makefile.in | 2 + dom/tests/browser/browser_ConsoleAPITests.js | 159 ++++++++++++++++++ dom/tests/browser/test-console-api.html | 18 ++ .../components/console/hudservice/Makefile.in | 2 - .../hudservice/tests/browser/Makefile.in | 1 - 9 files changed, 296 insertions(+), 3 deletions(-) create mode 100644 dom/base/ConsoleAPI.js create mode 100644 dom/base/ConsoleAPI.manifest create mode 100644 dom/tests/browser/browser_ConsoleAPITests.js create mode 100644 dom/tests/browser/test-console-api.html diff --git a/browser/installer/package-manifest.in b/browser/installer/package-manifest.in index e8a9afdd65d..24ff97177c6 100644 --- a/browser/installer/package-manifest.in +++ b/browser/installer/package-manifest.in @@ -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 diff --git a/dom/base/ConsoleAPI.js b/dom/base/ConsoleAPI.js new file mode 100644 index 00000000000..026ebb165c6 --- /dev/null +++ b/dom/base/ConsoleAPI.js @@ -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 (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]); diff --git a/dom/base/ConsoleAPI.manifest b/dom/base/ConsoleAPI.manifest new file mode 100644 index 00000000000..9fcc9d5538c --- /dev/null +++ b/dom/base/ConsoleAPI.manifest @@ -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 diff --git a/dom/base/Makefile.in b/dom/base/Makefile.in index 8a8a12bf765..b772c4b9fc1 100644 --- a/dom/base/Makefile.in +++ b/dom/base/Makefile.in @@ -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 \ diff --git a/dom/tests/browser/Makefile.in b/dom/tests/browser/Makefile.in index 925da2d440c..3ab3220ee8c 100644 --- a/dom/tests/browser/Makefile.in +++ b/dom/tests/browser/Makefile.in @@ -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) diff --git a/dom/tests/browser/browser_ConsoleAPITests.js b/dom/tests/browser/browser_ConsoleAPITests.js new file mode 100644 index 00000000000..a5c87eae071 --- /dev/null +++ b/dom/tests/browser/browser_ConsoleAPITests.js @@ -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 + * + * 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; +} diff --git a/dom/tests/browser/test-console-api.html b/dom/tests/browser/test-console-api.html new file mode 100644 index 00000000000..29552e47b3b --- /dev/null +++ b/dom/tests/browser/test-console-api.html @@ -0,0 +1,18 @@ + + + Console API test page + + + +

Console API Test Page

+ + + diff --git a/toolkit/components/console/hudservice/Makefile.in b/toolkit/components/console/hudservice/Makefile.in index ca11fc4776c..5f523513b3d 100644 --- a/toolkit/components/console/hudservice/Makefile.in +++ b/toolkit/components/console/hudservice/Makefile.in @@ -43,8 +43,6 @@ VPATH = @srcdir@ include $(DEPTH)/config/autoconf.mk -MODULE = hudservice - EXTRA_JS_MODULES = HUDService.jsm \ PropertyPanel.jsm \ $(NULL) diff --git a/toolkit/components/console/hudservice/tests/browser/Makefile.in b/toolkit/components/console/hudservice/tests/browser/Makefile.in index 113f1901085..b36684de894 100644 --- a/toolkit/components/console/hudservice/tests/browser/Makefile.in +++ b/toolkit/components/console/hudservice/tests/browser/Makefile.in @@ -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 \