From f94c252fa9cb0e65cdf02f68f98a5d7a03ae540e Mon Sep 17 00:00:00 2001 From: Bill McCloskey Date: Tue, 7 Oct 2014 11:46:25 -0700 Subject: [PATCH] Bug 1067576 - Make console.log work in frame scripts (r=Mossop) --- browser/installer/package-manifest.in | 3 + toolkit/components/moz.build | 1 + .../ContentProcessSingleton.js | 64 +++++++++++++++++++ .../processsingleton/MainProcessSingleton.js | 53 +++++++++++++++ .../ProcessSingleton.manifest | 7 ++ toolkit/components/processsingleton/moz.build | 11 ++++ toolkit/content/browser-content.js | 6 +- toolkit/content/widgets/browser.xml | 1 - 8 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 toolkit/components/processsingleton/ContentProcessSingleton.js create mode 100644 toolkit/components/processsingleton/MainProcessSingleton.js create mode 100644 toolkit/components/processsingleton/ProcessSingleton.manifest create mode 100644 toolkit/components/processsingleton/moz.build diff --git a/browser/installer/package-manifest.in b/browser/installer/package-manifest.in index 5cefadc1c54..79948493b5c 100644 --- a/browser/installer/package-manifest.in +++ b/browser/installer/package-manifest.in @@ -444,6 +444,9 @@ @BINPATH@/components/addoncompat.manifest @BINPATH@/components/multiprocessShims.js @BINPATH@/components/pluginGlue.manifest +@BINPATH@/components/ProcessSingleton.manifest +@BINPATH@/components/MainProcessSingleton.js +@BINPATH@/components/ContentProcessSingleton.js @BINPATH@/browser/components/nsSessionStore.manifest @BINPATH@/browser/components/nsSessionStartup.js @BINPATH@/browser/components/nsSessionStore.js diff --git a/toolkit/components/moz.build b/toolkit/components/moz.build index 926adc5e69c..f7e608b22a3 100644 --- a/toolkit/components/moz.build +++ b/toolkit/components/moz.build @@ -36,6 +36,7 @@ DIRS += [ 'passwordmgr', 'perf', 'places', + 'processsingleton', 'promiseworker', 'prompts', 'protobuf', diff --git a/toolkit/components/processsingleton/ContentProcessSingleton.js b/toolkit/components/processsingleton/ContentProcessSingleton.js new file mode 100644 index 00000000000..f9697e7dd09 --- /dev/null +++ b/toolkit/components/processsingleton/ContentProcessSingleton.js @@ -0,0 +1,64 @@ +/* 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/. */ + +"use strict"; + +const Cu = Components.utils; +const Ci = Components.interfaces; + +Cu.import("resource://gre/modules/Services.jsm"); +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); + +XPCOMUtils.defineLazyServiceGetter(this, "cpmm", + "@mozilla.org/childprocessmessagemanager;1", + "nsIMessageSender"); + +function ContentProcessSingleton() {} +ContentProcessSingleton.prototype = { + classID: Components.ID("{ca2a8470-45c7-11e4-916c-0800200c9a66}"), + QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, + Ci.nsISupportsWeakReference]), + + observe: function(subject, topic, data) { + switch (topic) { + case "app-startup": { + Services.obs.addObserver(this, "console-api-log-event", false); + Services.obs.addObserver(this, "xpcom-shutdown", false); + break; + } + case "console-api-log-event": { + let consoleMsg = subject.wrappedJSObject; + + let msgData = { + level: consoleMsg.level, + filename: consoleMsg.filename, + lineNumber: consoleMsg.lineNumber, + functionName: consoleMsg.functionName, + timeStamp: consoleMsg.timeStamp, + arguments: [], + }; + + // We can't send objects over the message manager, so we sanitize + // them out. + for (let arg of consoleMsg.arguments) { + if ((typeof arg == "object" || typeof arg == "function") && arg !== null) { + msgData.arguments.push(""); + } else { + msgData.arguments.push(arg); + } + } + + cpmm.sendAsyncMessage("Console:Log", msgData); + break; + } + + case "xpcom-shutdown": + Services.obs.removeObserver(this, "console-api-log-event"); + Services.obs.removeObserver(this, "xpcom-shutdown"); + break; + } + }, +}; + +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentProcessSingleton]); diff --git a/toolkit/components/processsingleton/MainProcessSingleton.js b/toolkit/components/processsingleton/MainProcessSingleton.js new file mode 100644 index 00000000000..689edd55972 --- /dev/null +++ b/toolkit/components/processsingleton/MainProcessSingleton.js @@ -0,0 +1,53 @@ +/* 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/. */ + +"use strict"; + +const Cu = Components.utils; +const Ci = Components.interfaces; +const Cc = Components.classes; + +Cu.import("resource://gre/modules/Services.jsm"); +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); + +XPCOMUtils.defineLazyServiceGetter(this, "ppmm", + "@mozilla.org/parentprocessmessagemanager;1", + "nsIMessageListenerManager"); + +XPCOMUtils.defineLazyServiceGetter(this, "globalmm", + "@mozilla.org/globalmessagemanager;1", + "nsIMessageBroadcaster"); + +function MainProcessSingleton() {} +MainProcessSingleton.prototype = { + classID: Components.ID("{0636a680-45cb-11e4-916c-0800200c9a66}"), + QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, + Ci.nsISupportsWeakReference]), + + receiveMessage: function(message) { + let logMsg = message.data; + logMsg.wrappedJSObject = logMsg; + Services.obs.notifyObservers(logMsg, "console-api-log-event", null); + }, + + observe: function(subject, topic, data) { + switch (topic) { + case "app-startup": { + Services.obs.addObserver(this, "xpcom-shutdown", false); + + // Load this script early so that console.* is initialized + // before other frame scripts. + globalmm.loadFrameScript("chrome://global/content/browser-content.js", true); + ppmm.addMessageListener("Console:Log", this); + break; + } + + case "xpcom-shutdown": + ppmm.removeMessageListener("Console:Log", this); + break; + } + }, +}; + +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MainProcessSingleton]); diff --git a/toolkit/components/processsingleton/ProcessSingleton.manifest b/toolkit/components/processsingleton/ProcessSingleton.manifest new file mode 100644 index 00000000000..7a882ed7b6f --- /dev/null +++ b/toolkit/components/processsingleton/ProcessSingleton.manifest @@ -0,0 +1,7 @@ +component {0636a680-45cb-11e4-916c-0800200c9a66} MainProcessSingleton.js process=main +contract @mozilla.org/main-process-singleton;1 {0636a680-45cb-11e4-916c-0800200c9a66} process=main +category app-startup MainProcessSingleton service,@mozilla.org/main-process-singleton;1 process=main + +component {ca2a8470-45c7-11e4-916c-0800200c9a66} ContentProcessSingleton.js process=content +contract @mozilla.org/content-process-singleton;1 {ca2a8470-45c7-11e4-916c-0800200c9a66} process=content +category app-startup ContentProcessSingleton service,@mozilla.org/content-process-singleton;1 process=content diff --git a/toolkit/components/processsingleton/moz.build b/toolkit/components/processsingleton/moz.build new file mode 100644 index 00000000000..6ffaac0f872 --- /dev/null +++ b/toolkit/components/processsingleton/moz.build @@ -0,0 +1,11 @@ +# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +EXTRA_COMPONENTS += [ + 'ContentProcessSingleton.js', + 'MainProcessSingleton.js', + 'ProcessSingleton.manifest', +] diff --git a/toolkit/content/browser-content.js b/toolkit/content/browser-content.js index 1244f468651..7a3399738a2 100644 --- a/toolkit/content/browser-content.js +++ b/toolkit/content/browser-content.js @@ -348,4 +348,8 @@ let PopupBlocking = { {blockedPopups: this.popupData, freshPopup: freshPopup}); }, }; -PopupBlocking.init(); \ No newline at end of file +PopupBlocking.init(); + +// Set up console.* for frame scripts. +let Console = Components.utils.import("resource://gre/modules/devtools/Console.jsm", {}); +this.console = new Console.ConsoleAPI(); diff --git a/toolkit/content/widgets/browser.xml b/toolkit/content/widgets/browser.xml index d2488738032..5488adc45c9 100644 --- a/toolkit/content/widgets/browser.xml +++ b/toolkit/content/widgets/browser.xml @@ -790,7 +790,6 @@ this.messageManager.addMessageListener("PopupBlocking:UpdateBlockedPopups", this); this.messageManager.addMessageListener("Autoscroll:Start", this); this.messageManager.addMessageListener("Autoscroll:Cancel", this); - this.messageManager.loadFrameScript("chrome://global/content/browser-content.js", true); } ]]>