|
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- / |
|
2 /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 'use strict'; |
|
8 |
|
9 /** |
|
10 * This code exists to be a "grab bag" of global code that needs to be |
|
11 * loaded per B2G process, but doesn't need to directly interact with |
|
12 * web content. |
|
13 * |
|
14 * (It's written as an XPCOM service because it needs to watch |
|
15 * app-startup.) |
|
16 */ |
|
17 |
|
18 const Cc = Components.classes; |
|
19 const Ci = Components.interfaces; |
|
20 const Cu = Components.utils; |
|
21 |
|
22 Cu.import('resource://gre/modules/Services.jsm'); |
|
23 Cu.import('resource://gre/modules/XPCOMUtils.jsm'); |
|
24 |
|
25 // Preloading the CSP jsm in this process early on. |
|
26 Cu.import("resource://gre/modules/CSPUtils.jsm"); |
|
27 |
|
28 function debug(msg) { |
|
29 log(msg); |
|
30 } |
|
31 function log(msg) { |
|
32 // This file implements console.log(), so use dump(). |
|
33 //dump('ProcessGlobal: ' + msg + '\n'); |
|
34 } |
|
35 |
|
36 function ProcessGlobal() {} |
|
37 ProcessGlobal.prototype = { |
|
38 classID: Components.ID('{1a94c87a-5ece-4d11-91e1-d29c29f21b28}'), |
|
39 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, |
|
40 Ci.nsISupportsWeakReference]), |
|
41 |
|
42 observe: function pg_observe(subject, topic, data) { |
|
43 switch (topic) { |
|
44 case 'app-startup': { |
|
45 Services.obs.addObserver(this, 'console-api-log-event', false); |
|
46 break; |
|
47 } |
|
48 case 'console-api-log-event': { |
|
49 // Pipe `console` log messages to the nsIConsoleService which |
|
50 // writes them to logcat on Gonk. |
|
51 let message = subject.wrappedJSObject; |
|
52 let prefix = ('Content JS ' + message.level.toUpperCase() + |
|
53 ' at ' + message.filename + ':' + message.lineNumber + |
|
54 ' in ' + (message.functionName || 'anonymous') + ': '); |
|
55 Services.console.logStringMessage(prefix + Array.join(message.arguments, |
|
56 ' ')); |
|
57 break; |
|
58 } |
|
59 } |
|
60 }, |
|
61 }; |
|
62 |
|
63 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ProcessGlobal]); |