1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/b2g/components/ProcessGlobal.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,63 @@ 1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- / 1.5 +/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +'use strict'; 1.11 + 1.12 +/** 1.13 + * This code exists to be a "grab bag" of global code that needs to be 1.14 + * loaded per B2G process, but doesn't need to directly interact with 1.15 + * web content. 1.16 + * 1.17 + * (It's written as an XPCOM service because it needs to watch 1.18 + * app-startup.) 1.19 + */ 1.20 + 1.21 +const Cc = Components.classes; 1.22 +const Ci = Components.interfaces; 1.23 +const Cu = Components.utils; 1.24 + 1.25 +Cu.import('resource://gre/modules/Services.jsm'); 1.26 +Cu.import('resource://gre/modules/XPCOMUtils.jsm'); 1.27 + 1.28 +// Preloading the CSP jsm in this process early on. 1.29 +Cu.import("resource://gre/modules/CSPUtils.jsm"); 1.30 + 1.31 +function debug(msg) { 1.32 + log(msg); 1.33 +} 1.34 +function log(msg) { 1.35 + // This file implements console.log(), so use dump(). 1.36 + //dump('ProcessGlobal: ' + msg + '\n'); 1.37 +} 1.38 + 1.39 +function ProcessGlobal() {} 1.40 +ProcessGlobal.prototype = { 1.41 + classID: Components.ID('{1a94c87a-5ece-4d11-91e1-d29c29f21b28}'), 1.42 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, 1.43 + Ci.nsISupportsWeakReference]), 1.44 + 1.45 + observe: function pg_observe(subject, topic, data) { 1.46 + switch (topic) { 1.47 + case 'app-startup': { 1.48 + Services.obs.addObserver(this, 'console-api-log-event', false); 1.49 + break; 1.50 + } 1.51 + case 'console-api-log-event': { 1.52 + // Pipe `console` log messages to the nsIConsoleService which 1.53 + // writes them to logcat on Gonk. 1.54 + let message = subject.wrappedJSObject; 1.55 + let prefix = ('Content JS ' + message.level.toUpperCase() + 1.56 + ' at ' + message.filename + ':' + message.lineNumber + 1.57 + ' in ' + (message.functionName || 'anonymous') + ': '); 1.58 + Services.console.logStringMessage(prefix + Array.join(message.arguments, 1.59 + ' ')); 1.60 + break; 1.61 + } 1.62 + } 1.63 + }, 1.64 +}; 1.65 + 1.66 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ProcessGlobal]);