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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* 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 { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import('resource://gre/modules/Services.jsm');
this.EXPORTED_SYMBOLS = ['SystemAppProxy'];
const kMainSystemAppId = 'main';
var SystemAppProxy = {
_frameInfoMap: new Map(),
_pendingLoadedEvents: [],
_pendingReadyEvents: [],
_pendingListeners: [],
// To call when a main system app iframe is created
// Only used for main system app.
registerFrame: function systemApp_registerFrame(frame) {
this.registerFrameWithId(kMainSystemAppId, frame);
},
// To call when a new system(-remote) app iframe is created with ID
registerFrameWithId: function systemApp_registerFrameWithId(frameId,
frame) {
// - Frame ID of main system app is predefined as 'main'.
// - Frame ID of system-remote app is defined themselves.
//
// frameInfo = {
// isReady: ...,
// isLoaded: ...,
// frame: ...
// }
let frameInfo = { frameId: frameId,
isReady: false,
isLoaded: false,
frame: frame };
this._frameInfoMap.set(frameId, frameInfo);
// Register all DOM event listeners added before we got a ref to
// this system app iframe.
this._pendingListeners
.forEach(args => {
if (args[0] === frameInfo.frameId) {
this.addEventListenerWithId.apply(this, args);
}
});
// Removed registered event listeners.
this._pendingListeners =
this._pendingListeners
.filter(args => { return args[0] != frameInfo.frameId; });
},
unregisterFrameWithId: function systemApp_unregisterFrameWithId(frameId) {
this._frameInfoMap.delete(frameId);
// remove all pending event listener to the deleted system(-remote) app
this._pendingListeners = this._pendingListeners.filter(
args => { return args[0] != frameId; });
this._pendingReadyEvents = this._pendingReadyEvents.filter(
([evtFrameId]) => { return evtFrameId != frameId });
this._pendingLoadedEvents = this._pendingLoadedEvents.filter(
([evtFrameId]) => { return evtFrameId != frameId });
},
// Get the main system app frame
_getMainSystemAppInfo: function systemApp_getMainSystemAppInfo() {
return this._frameInfoMap.get(kMainSystemAppId);
},
// Get the main system app frame
// Only used for the main system app.
getFrame: function systemApp_getFrame() {
return this.getFrameWithId(kMainSystemAppId);
},
// Get the frame of the specific system app
getFrameWithId: function systemApp_getFrameWithId(frameId) {
let frameInfo = this._frameInfoMap.get(frameId);
if (!frameInfo) {
throw new Error('no frame ID is ' + frameId);
}
if (!frameInfo.frame) {
throw new Error('no content window');
}
return frameInfo.frame;
},
// To call when the load event of the main system app document is triggered.
// i.e. everything that is not lazily loaded are run and done.
// Only used for the main system app.
setIsLoaded: function systemApp_setIsLoaded() {
this.setIsLoadedWithId(kMainSystemAppId);
},
// To call when the load event of the specific system app document is
// triggered. i.e. everything that is not lazily loaded are run and done.
setIsLoadedWithId: function systemApp_setIsLoadedWithId(frameId) {
let frameInfo = this._frameInfoMap.get(frameId);
if (!frameInfo) {
throw new Error('no frame ID is ' + frameId);
}
if (frameInfo.isLoaded) {
if (frameInfo.frameId === kMainSystemAppId) {
Cu.reportError('SystemApp has already been declared as being loaded.');
}
else {
Cu.reportError('SystemRemoteApp (ID: ' + frameInfo.frameId + ') ' +
'has already been declared as being loaded.');
}
}
frameInfo.isLoaded = true;
// Dispatch all events being queued while the system app was still loading
this._pendingLoadedEvents
.forEach(([evtFrameId, evtType, evtDetails]) => {
if (evtFrameId === frameInfo.frameId) {
this.sendCustomEventWithId(evtFrameId, evtType, evtDetails, true);
}
});
// Remove sent events.
this._pendingLoadedEvents =
this._pendingLoadedEvents
.filter(([evtFrameId]) => { return evtFrameId != frameInfo.frameId });
},
// To call when the main system app is ready to receive events
// i.e. when system-message-listener-ready mozContentEvent is sent.
// Only used for the main system app.
setIsReady: function systemApp_setIsReady() {
this.setIsReadyWithId(kMainSystemAppId);
},
// To call when the specific system(-remote) app is ready to receive events
// i.e. when system-message-listener-ready mozContentEvent is sent.
setIsReadyWithId: function systemApp_setIsReadyWithId(frameId) {
let frameInfo = this._frameInfoMap.get(frameId);
if (!frameInfo) {
throw new Error('no frame ID is ' + frameId);
}
if (!frameInfo.isLoaded) {
Cu.reportError('SystemApp.setIsLoaded() should be called before setIsReady().');
}
if (frameInfo.isReady) {
Cu.reportError('SystemApp has already been declared as being ready.');
}
frameInfo.isReady = true;
// Dispatch all events being queued while the system app was still not ready
this._pendingReadyEvents
.forEach(([evtFrameId, evtType, evtDetails]) => {
if (evtFrameId === frameInfo.frameId) {
this.sendCustomEventWithId(evtFrameId, evtType, evtDetails);
}
});
// Remove sent events.
this._pendingReadyEvents =
this._pendingReadyEvents
.filter(([evtFrameId]) => { return evtFrameId != frameInfo.frameId });
},
/*
* Common way to send an event to the main system app.
* Only used for the main system app.
*
* // In gecko code:
* SystemAppProxy.sendCustomEvent('foo', { data: 'bar' });
* // In system app:
* window.addEventListener('foo', function (event) {
* event.details == 'bar'
* });
*
* @param type The custom event type.
* @param details The event details.
* @param noPending Set to true to emit this event even before the system
* app is ready.
* Event is always pending if the app is not loaded yet.
* @param target The element who dispatch this event.
*
* @returns event? Dispatched event, or null if the event is pending.
*/
_sendCustomEvent: function systemApp_sendCustomEvent(type,
details,
noPending,
target) {
let args = Array.prototype.slice.call(arguments);
return this.sendCustomEventWithId
.apply(this, [kMainSystemAppId].concat(args));
},
/*
* Common way to send an event to the specific system app.
*
* // In gecko code (send custom event from main system app):
* SystemAppProxy.sendCustomEventWithId('main', 'foo', { data: 'bar' });
* // In system app:
* window.addEventListener('foo', function (event) {
* event.details == 'bar'
* });
*
* @param frameId Specify the system(-remote) app who dispatch this event.
* @param type The custom event type.
* @param details The event details.
* @param noPending Set to true to emit this event even before the system
* app is ready.
* Event is always pending if the app is not loaded yet.
* @param target The element who dispatch this event.
*
* @returns event? Dispatched event, or null if the event is pending.
*/
sendCustomEventWithId: function systemApp_sendCustomEventWithId(frameId,
type,
details,
noPending,
target) {
let frameInfo = this._frameInfoMap.get(frameId);
let content = (frameInfo && frameInfo.frame) ?
frameInfo.frame.contentWindow : null;
// If the system app isn't loaded yet,
// queue events until someone calls setIsLoaded
if (!content || !(frameInfo && frameInfo.isLoaded)) {
if (noPending) {
this._pendingLoadedEvents.push([frameId, type, details]);
} else {
this._pendingReadyEvents.push([frameId, type, details]);
}
return null;
}
// If the system app isn't ready yet,
// queue events until someone calls setIsReady
if (!(frameInfo && frameInfo.isReady) && !noPending) {
this._pendingReadyEvents.push([frameId, type, details]);
return null;
}
let event = content.document.createEvent('CustomEvent');
let payload;
// If the root object already has __exposedProps__,
// we consider the caller already wrapped (correctly) the object.
if ('__exposedProps__' in details) {
payload = details;
} else {
payload = details ? Cu.cloneInto(details, content) : {};
}
if ((target || content) === frameInfo.frame.contentWindow) {
dump('XXX FIXME : Dispatch a ' + type + ': ' + details.type + '\n');
}
event.initCustomEvent(type, true, false, payload);
(target || content).dispatchEvent(event);
return event;
},
// Now deprecated, use sendCustomEvent with a custom event name
dispatchEvent: function systemApp_dispatchEvent(details, target) {
return this._sendCustomEvent('mozChromeEvent', details, false, target);
},
dispatchKeyboardEvent: function systemApp_dispatchKeyboardEvent(type, details) {
try {
let frameInfo = this._getMainSystemAppInfo();
let content = (frameInfo && frameInfo.frame) ? frameInfo.frame.contentWindow
: null;
if (!content) {
throw new Error('no content window');
}
// If we don't already have a TextInputProcessor, create one now
if (!this.TIP) {
this.TIP = Cc['@mozilla.org/text-input-processor;1']
.createInstance(Ci.nsITextInputProcessor);
if (!this.TIP) {
throw new Error('failed to create textInputProcessor');
}
}
if (!this.TIP.beginInputTransactionForTests(content)) {
this.TIP = null;
throw new Error('beginInputTransaction failed');
}
let e = new content.KeyboardEvent('', { key: details.key, });
if (type === 'keydown') {
this.TIP.keydown(e);
}
else if (type === 'keyup') {
this.TIP.keyup(e);
}
else {
throw new Error('unexpected event type: ' + type);
}
}
catch(e) {
dump('dispatchKeyboardEvent: ' + e + '\n');
}
},
// Listen for dom events on the main system app
addEventListener: function systemApp_addEventListener() {
let args = Array.prototype.slice.call(arguments);
this.addEventListenerWithId.apply(this, [kMainSystemAppId].concat(args));
},
// Listen for dom events on the specific system app
addEventListenerWithId: function systemApp_addEventListenerWithId(frameId,
...args) {
let frameInfo = this._frameInfoMap.get(frameId);
if (!frameInfo) {
this._pendingListeners.push(arguments);
return false;
}
let content = frameInfo.frame.contentWindow;
content.addEventListener.apply(content, args);
return true;
},
// remove the event listener from the main system app
removeEventListener: function systemApp_removeEventListener(name, listener) {
this.removeEventListenerWithId.apply(this, [kMainSystemAppId, name, listener]);
},
// remove the event listener from the specific system app
removeEventListenerWithId: function systemApp_removeEventListenerWithId(frameId,
name,
listener) {
let frameInfo = this._frameInfoMap.get(frameId);
if (frameInfo) {
let content = frameInfo.frame.contentWindow;
content.removeEventListener.apply(content, [name, listener]);
}
else {
this._pendingListeners = this._pendingListeners.filter(
args => {
return args[0] != frameId || args[1] != name || args[2] != listener;
});
}
},
// Get all frame in system app
getFrames: function systemApp_getFrames(frameId) {
let frameList = [];
for (let frameId of this._frameInfoMap.keys()) {
let frameInfo = this._frameInfoMap.get(frameId);
let systemAppFrame = frameInfo.frame;
let subFrames = systemAppFrame.contentDocument.querySelectorAll('iframe');
frameList.push(systemAppFrame);
for (let i = 0; i < subFrames.length; ++i) {
frameList.push(subFrames[i]);
}
}
return frameList;
}
};
this.SystemAppProxy = SystemAppProxy;
|