michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- / michael@0: /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: 'use strict'; michael@0: michael@0: /** michael@0: * This code exists to be a "grab bag" of global code that needs to be michael@0: * loaded per B2G process, but doesn't need to directly interact with michael@0: * web content. michael@0: * michael@0: * (It's written as an XPCOM service because it needs to watch michael@0: * app-startup.) michael@0: */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import('resource://gre/modules/Services.jsm'); michael@0: Cu.import('resource://gre/modules/XPCOMUtils.jsm'); michael@0: michael@0: // Preloading the CSP jsm in this process early on. michael@0: Cu.import("resource://gre/modules/CSPUtils.jsm"); michael@0: michael@0: function debug(msg) { michael@0: log(msg); michael@0: } michael@0: function log(msg) { michael@0: // This file implements console.log(), so use dump(). michael@0: //dump('ProcessGlobal: ' + msg + '\n'); michael@0: } michael@0: michael@0: function ProcessGlobal() {} michael@0: ProcessGlobal.prototype = { michael@0: classID: Components.ID('{1a94c87a-5ece-4d11-91e1-d29c29f21b28}'), michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, michael@0: Ci.nsISupportsWeakReference]), michael@0: michael@0: observe: function pg_observe(subject, topic, data) { michael@0: switch (topic) { michael@0: case 'app-startup': { michael@0: Services.obs.addObserver(this, 'console-api-log-event', false); michael@0: break; michael@0: } michael@0: case 'console-api-log-event': { michael@0: // Pipe `console` log messages to the nsIConsoleService which michael@0: // writes them to logcat on Gonk. michael@0: let message = subject.wrappedJSObject; michael@0: let prefix = ('Content JS ' + message.level.toUpperCase() + michael@0: ' at ' + message.filename + ':' + message.lineNumber + michael@0: ' in ' + (message.functionName || 'anonymous') + ': '); michael@0: Services.console.logStringMessage(prefix + Array.join(message.arguments, michael@0: ' ')); michael@0: break; michael@0: } michael@0: } michael@0: }, michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ProcessGlobal]);