forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SystemMessageManager.js
233 lines (190 loc) · 8.31 KB
/
SystemMessageManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* 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 Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/ObjectWrapper.jsm");
const kSystemMessageInternalReady = "system-message-internal-ready";
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
"@mozilla.org/childprocessmessagemanager;1",
"nsISyncMessageSender");
function debug(aMsg) {
//dump("-- SystemMessageManager " + Date.now() + " : " + aMsg + "\n");
}
// Implementation of the DOM API for system messages
function SystemMessageManager() {
// Message handlers for this page.
// We can have only one handler per message type.
this._handlers = {};
// Pending messages for this page, keyed by message type.
this._pendings = {};
// Flag to specify if this process has already registered manifest.
this._registerManifestReady = false;
// Flag to determine this process is a parent or child process.
let appInfo = Cc["@mozilla.org/xre/app-info;1"];
this._isParentProcess =
!appInfo || appInfo.getService(Ci.nsIXULRuntime)
.processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
// An oberver to listen to whether the |SystemMessageInternal| is ready.
if (this._isParentProcess) {
Services.obs.addObserver(this, kSystemMessageInternalReady, false);
}
}
SystemMessageManager.prototype = {
__proto__: DOMRequestIpcHelper.prototype,
_dispatchMessage: function sysMessMgr_dispatchMessage(aType, aHandler, aMessage) {
// We get a json blob, but in some cases we want another kind of object
// to be dispatched.
// To do so, we check if we have a with a contract ID of
// "@mozilla.org/dom/system-messages/wrapper/TYPE;1" component implementing
// nsISystemMessageWrapper.
debug("Dispatching " + JSON.stringify(aMessage) + "\n");
let contractID = "@mozilla.org/dom/system-messages/wrapper/" + aType + ";1";
let wrapped = false;
if (contractID in Cc) {
debug(contractID + " is registered, creating an instance");
let wrapper = Cc[contractID].createInstance(Ci.nsISystemMessagesWrapper);
if (wrapper) {
aMessage = wrapper.wrapMessage(aMessage, this._window);
wrapped = true;
debug("wrapped = " + aMessage);
}
}
aHandler.handleMessage(wrapped ? aMessage
: ObjectWrapper.wrap(aMessage, this._window));
// Notify the parent process the message is handled.
cpmm.sendAsyncMessage("SystemMessageManager:HandleMessageDone",
{ type: aType,
message: aMessage });
},
mozSetMessageHandler: function sysMessMgr_setMessageHandler(aType, aHandler) {
debug("setMessage handler for [" + aType + "] " + aHandler);
if (!aType) {
// Just bail out if we have no type.
return;
}
let handlers = this._handlers;
if (!aHandler) {
// Setting the handler to null means we don't want to receive messages
// of this type anymore.
delete handlers[aType];
return;
}
// Last registered handler wins.
handlers[aType] = aHandler;
// Ask for the list of currently pending messages.
cpmm.sendAsyncMessage("SystemMessageManager:GetPendingMessages",
{ type: aType,
uri: this._uri,
manifest: this._manifest });
},
mozHasPendingMessage: function sysMessMgr_hasPendingMessage(aType) {
// If we have a handler for this type, we can't have any pending message.
if (aType in this._handlers) {
return false;
}
return cpmm.sendSyncMessage("SystemMessageManager:HasPendingMessages",
{ type: aType,
uri: this._uri,
manifest: this._manifest })[0];
},
uninit: function sysMessMgr_uninit() {
this._handlers = null;
this._pendings = null;
if (this._isParentProcess) {
Services.obs.removeObserver(this, kSystemMessageInternalReady);
}
cpmm.sendAsyncMessage("SystemMessageManager:Unregister",
{ manifest: this._manifest,
innerWindowID: this.innerWindowID
});
},
receiveMessage: function sysMessMgr_receiveMessage(aMessage) {
debug("receiveMessage " + aMessage.name + " - " +
aMessage.data.type + " for " + aMessage.data.manifest +
" (" + this._manifest + ")");
let msg = aMessage.data;
if (msg.manifest != this._manifest || msg.uri != this._uri) {
return;
}
if (aMessage.name == "SystemMessageManager:Message") {
// Send an acknowledgement to parent to clean up the pending message,
// so a re-launched app won't handle it again, which is redundant.
cpmm.sendAsyncMessage(
"SystemMessageManager:Message:Return:OK",
{ type: msg.type,
manifest: msg.manifest,
uri: msg.uri,
msgID: msg.msgID });
}
// Bail out if we have no handlers registered for this type.
if (!(msg.type in this._handlers)) {
debug("No handler for this type");
return;
}
let messages = (aMessage.name == "SystemMessageManager:Message")
? [msg.msg]
: msg.msgQueue;
messages.forEach(function(aMsg) {
this._dispatchMessage(msg.type, this._handlers[msg.type], aMsg);
}, this);
},
// nsIDOMGlobalPropertyInitializer implementation.
init: function sysMessMgr_init(aWindow) {
debug("init");
this.initHelper(aWindow, ["SystemMessageManager:Message",
"SystemMessageManager:GetPendingMessages:Return"]);
let principal = aWindow.document.nodePrincipal;
this._uri = principal.URI.spec;
let appsService = Cc["@mozilla.org/AppsService;1"]
.getService(Ci.nsIAppsService);
this._manifest = appsService.getManifestURLByLocalId(principal.appId);
// Two cases are valid to register the manifest for the current process:
// 1. This is asked by a child process (parent process must be ready).
// 2. Parent process has already constructed the |SystemMessageInternal|.
// Otherwise, delay to do it when the |SystemMessageInternal| is ready.
let readyToRegister = true;
if (this._isParentProcess) {
let ready = cpmm.sendSyncMessage(
"SystemMessageManager:AskReadyToRegister", null);
if (ready.length == 0 || !ready[0]) {
readyToRegister = false;
}
}
if (readyToRegister) {
this._registerManifest();
}
debug("done");
},
observe: function sysMessMgr_observe(aSubject, aTopic, aData) {
if (aTopic === kSystemMessageInternalReady) {
this._registerManifest();
}
// Call the DOMRequestIpcHelper.observe method.
this.__proto__.__proto__.observe.call(this, aSubject, aTopic, aData);
},
_registerManifest: function sysMessMgr_registerManifest() {
if (!this._registerManifestReady) {
cpmm.sendAsyncMessage("SystemMessageManager:Register",
{ manifest: this._manifest,
innerWindowID: this.innerWindowID
});
this._registerManifestReady = true;
}
},
classID: Components.ID("{bc076ea0-609b-4d8f-83d7-5af7cbdc3bb2}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMNavigatorSystemMessages,
Ci.nsIDOMGlobalPropertyInitializer,
Ci.nsIObserver]),
classInfo: XPCOMUtils.generateCI({classID: Components.ID("{bc076ea0-609b-4d8f-83d7-5af7cbdc3bb2}"),
contractID: "@mozilla.org/system-message-manager;1",
interfaces: [Ci.nsIDOMNavigatorSystemMessages],
flags: Ci.nsIClassInfo.DOM_OBJECT,
classDescription: "System Messages"})
}
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SystemMessageManager]);