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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
|
/* 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';
this.EXPORTED_SYMBOLS = ['Keyboard'];
const Cu = Components.utils;
const Cc = Components.classes;
const Ci = Components.interfaces;
Cu.import('resource://gre/modules/Services.jsm');
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
"@mozilla.org/parentprocessmessagemanager;1", "nsIMessageBroadcaster");
XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy",
"resource://gre/modules/SystemAppProxy.jsm");
XPCOMUtils.defineLazyGetter(this, "appsService", function() {
return Cc["@mozilla.org/AppsService;1"].getService(Ci.nsIAppsService);
});
XPCOMUtils.defineLazyGetter(this, "hardwareKeyHandler", function() {
#ifdef MOZ_B2G
return Cc["@mozilla.org/HardwareKeyHandler;1"]
.getService(Ci.nsIHardwareKeyHandler);
#else
return null;
#endif
});
var Utils = {
getMMFromMessage: function u_getMMFromMessage(msg) {
let mm;
try {
mm = msg.target.QueryInterface(Ci.nsIFrameLoaderOwner)
.frameLoader.messageManager;
} catch(e) {
mm = msg.target;
}
return mm;
},
checkPermissionForMM: function u_checkPermissionForMM(mm, permName) {
return mm.assertPermission(permName);
}
};
this.Keyboard = {
#ifdef MOZ_B2G
// For receving keyboard event fired from hardware before it's dispatched,
// |this| object is used to be the listener to get the forwarded event.
// As the listener, |this| object must implement nsIHardwareKeyEventListener
// and nsSupportsWeakReference.
// Please see nsIHardwareKeyHandler.idl to get more information.
QueryInterface: XPCOMUtils.generateQI([
Ci.nsIHardwareKeyEventListener,
Ci.nsISupportsWeakReference
]),
#endif
_isConnectedToHardwareKeyHandler: false,
_formMM: null, // The current web page message manager.
_keyboardMM: null, // The keyboard app message manager.
_keyboardID: -1, // The keyboard app's ID number. -1 = invalid
_nextKeyboardID: 0, // The ID number counter.
_systemMMs: [], // The message managers registered to handle system async
// messages.
_supportsSwitchingTypes: [],
_systemMessageNames: [
'SetValue', 'RemoveFocus', 'SetSelectedOption', 'SetSelectedOptions',
'SetSupportsSwitchingTypes', 'RegisterSync', 'Unregister'
],
_messageNames: [
'RemoveFocus',
'SetSelectionRange', 'ReplaceSurroundingText', 'ShowInputMethodPicker',
'SwitchToNextInputMethod', 'HideInputMethod',
'SendKey', 'GetContext',
'SetComposition', 'EndComposition',
'RegisterSync', 'Unregister',
'ReplyHardwareKeyEvent'
],
get formMM() {
if (this._formMM && !Cu.isDeadWrapper(this._formMM))
return this._formMM;
return null;
},
set formMM(mm) {
this._formMM = mm;
},
sendToForm: function(name, data) {
if (!this.formMM) {
dump("Keyboard.jsm: Attempt to send message " + name +
" to form but no message manager exists.\n");
return;
}
try {
this.formMM.sendAsyncMessage(name, data);
} catch(e) { }
},
sendToKeyboard: function(name, data) {
try {
this._keyboardMM.sendAsyncMessage(name, data);
} catch(e) {
return false;
}
return true;
},
sendToSystem: function(name, data) {
if (!this._systemMMs.length) {
dump("Keyboard.jsm: Attempt to send message " + name +
" to system but no message manager registered.\n");
return;
}
this._systemMMs.forEach((mm, i) => {
data.inputManageId = i;
mm.sendAsyncMessage(name, data);
});
},
init: function keyboardInit() {
Services.obs.addObserver(this, 'inprocess-browser-shown', false);
Services.obs.addObserver(this, 'remote-browser-shown', false);
Services.obs.addObserver(this, 'oop-frameloader-crashed', false);
Services.obs.addObserver(this, 'message-manager-close', false);
// For receiving the native hardware keyboard event
if (hardwareKeyHandler) {
hardwareKeyHandler.registerListener(this);
}
for (let name of this._messageNames) {
ppmm.addMessageListener('Keyboard:' + name, this);
}
for (let name of this._systemMessageNames) {
ppmm.addMessageListener('System:' + name, this);
}
this.inputRegistryGlue = new InputRegistryGlue();
},
// This method will be registered into nsIHardwareKeyHandler:
// Send the initialized dictionary retrieved from the native keyboard event
// to input-method-app for generating a new event.
onHardwareKey: function onHardwareKeyReceived(evt) {
return this.sendToKeyboard('Keyboard:ReceiveHardwareKeyEvent', {
type: evt.type,
keyDict: evt.initDict
});
},
observe: function keyboardObserve(subject, topic, data) {
let frameLoader = null;
let mm = null;
if (topic == 'message-manager-close') {
mm = subject;
} else {
frameLoader = subject.QueryInterface(Ci.nsIFrameLoader);
mm = frameLoader.messageManager;
}
if (topic == 'oop-frameloader-crashed' ||
topic == 'message-manager-close') {
if (this.formMM == mm) {
// The application has been closed unexpectingly. Let's tell the
// keyboard app that the focus has been lost.
this.sendToKeyboard('Keyboard:Blur', {});
// Notify system app to hide keyboard.
this.sendToSystem('System:Blur', {});
// XXX: To be removed when content migrate away from mozChromeEvents.
SystemAppProxy.dispatchEvent({
type: 'inputmethod-contextchange',
inputType: 'blur'
});
this.formMM = null;
}
} else {
// Ignore notifications that aren't from a BrowserOrApp
if (!frameLoader.ownerIsMozBrowserOrAppFrame) {
return;
}
this.initFormsFrameScript(mm);
}
},
initFormsFrameScript: function(mm) {
mm.addMessageListener('Forms:Focus', this);
mm.addMessageListener('Forms:Blur', this);
mm.addMessageListener('Forms:SelectionChange', this);
mm.addMessageListener('Forms:SetSelectionRange:Result:OK', this);
mm.addMessageListener('Forms:SetSelectionRange:Result:Error', this);
mm.addMessageListener('Forms:ReplaceSurroundingText:Result:OK', this);
mm.addMessageListener('Forms:ReplaceSurroundingText:Result:Error', this);
mm.addMessageListener('Forms:SendKey:Result:OK', this);
mm.addMessageListener('Forms:SendKey:Result:Error', this);
mm.addMessageListener('Forms:SequenceError', this);
mm.addMessageListener('Forms:GetContext:Result:OK', this);
mm.addMessageListener('Forms:SetComposition:Result:OK', this);
mm.addMessageListener('Forms:EndComposition:Result:OK', this);
},
receiveMessage: function keyboardReceiveMessage(msg) {
// If we get a 'Keyboard:XXX'/'System:XXX' message, check that the sender
// has the required permission.
let mm;
// Assert the permission based on the prefix of the message.
let permName;
if (msg.name.startsWith("Keyboard:")) {
permName = "input";
} else if (msg.name.startsWith("System:")) {
permName = "input-manage";
}
// There is no permission to check (nor we need to get the mm)
// for Form: messages.
if (permName) {
mm = Utils.getMMFromMessage(msg);
if (!mm) {
dump("Keyboard.jsm: Message " + msg.name + " has no message manager.");
return;
}
if (!Utils.checkPermissionForMM(mm, permName)) {
dump("Keyboard.jsm: Message " + msg.name +
" from a content process with no '" + permName + "' privileges.\n");
return;
}
}
// we don't process kb messages (other than register)
// if they come from a kb that we're currently not regsitered for.
// this decision is made with the kbID kept by us and kb app
let kbID = null;
if ('kbID' in msg.data) {
kbID = msg.data.kbID;
}
if (0 === msg.name.indexOf('Keyboard:') &&
('Keyboard:RegisterSync' !== msg.name && this._keyboardID !== kbID)
) {
return;
}
switch (msg.name) {
case 'Forms:Focus':
this.handleFocus(msg);
break;
case 'Forms:Blur':
this.handleBlur(msg);
break;
case 'Forms:SelectionChange':
case 'Forms:SetSelectionRange:Result:OK':
case 'Forms:ReplaceSurroundingText:Result:OK':
case 'Forms:SendKey:Result:OK':
case 'Forms:SendKey:Result:Error':
case 'Forms:SequenceError':
case 'Forms:GetContext:Result:OK':
case 'Forms:SetComposition:Result:OK':
case 'Forms:EndComposition:Result:OK':
case 'Forms:SetSelectionRange:Result:Error':
case 'Forms:ReplaceSurroundingText:Result:Error':
let name = msg.name.replace(/^Forms/, 'Keyboard');
this.forwardEvent(name, msg);
break;
case 'System:SetValue':
this.setValue(msg);
break;
case 'Keyboard:RemoveFocus':
case 'System:RemoveFocus':
this.removeFocus();
break;
case 'System:RegisterSync': {
if (this._systemMMs.length !== 0) {
dump('Keyboard.jsm Warning: There are more than one content page ' +
'with input-manage permission. There will be undeterministic ' +
'responses to addInput()/removeInput() if both content pages are ' +
'trying to respond to the same request event.\n');
}
let id = this._systemMMs.length;
this._systemMMs.push(mm);
return id;
}
case 'System:Unregister':
this._systemMMs.splice(msg.data.id, 1);
break;
case 'System:SetSelectedOption':
this.setSelectedOption(msg);
break;
case 'System:SetSelectedOptions':
this.setSelectedOption(msg);
break;
case 'System:SetSupportsSwitchingTypes':
this.setSupportsSwitchingTypes(msg);
break;
case 'Keyboard:SetSelectionRange':
this.setSelectionRange(msg);
break;
case 'Keyboard:ReplaceSurroundingText':
this.replaceSurroundingText(msg);
break;
case 'Keyboard:SwitchToNextInputMethod':
this.switchToNextInputMethod();
break;
case 'Keyboard:ShowInputMethodPicker':
this.showInputMethodPicker();
break;
case 'Keyboard:SendKey':
this.sendKey(msg);
break;
case 'Keyboard:GetContext':
this.getContext(msg);
break;
case 'Keyboard:SetComposition':
this.setComposition(msg);
break;
case 'Keyboard:EndComposition':
this.endComposition(msg);
break;
case 'Keyboard:RegisterSync':
this._keyboardMM = mm;
if (kbID) {
// keyboard identifies itself, use its kbID
// this msg would be async, so no need to return
this._keyboardID = kbID;
}else{
// generate the id for the keyboard
this._keyboardID = this._nextKeyboardID;
this._nextKeyboardID++;
// this msg is sync,
// and we want to return the id back to inputmethod
return this._keyboardID;
}
break;
case 'Keyboard:Unregister':
this._keyboardMM = null;
this._keyboardID = -1;
break;
case 'Keyboard:ReplyHardwareKeyEvent':
if (hardwareKeyHandler) {
let reply = msg.data;
hardwareKeyHandler.onHandledByInputMethodApp(reply.type,
reply.defaultPrevented);
}
break;
}
},
handleFocus: function keyboardHandleFocus(msg) {
// Set the formMM to the new message manager received.
let mm = msg.target.QueryInterface(Ci.nsIFrameLoaderOwner)
.frameLoader.messageManager;
this.formMM = mm;
// Notify the nsIHardwareKeyHandler that the input-method-app is active now.
if (hardwareKeyHandler && !this._isConnectedToHardwareKeyHandler) {
this._isConnectedToHardwareKeyHandler = true;
hardwareKeyHandler.onInputMethodAppConnected();
}
// Notify the current active input app to gain focus.
this.forwardEvent('Keyboard:Focus', msg);
// Notify System app, used also to render value selectors for now;
// that's why we need the info about choices / min / max here as well...
this.sendToSystem('System:Focus', msg.data);
// XXX: To be removed when content migrate away from mozChromeEvents.
SystemAppProxy.dispatchEvent({
type: 'inputmethod-contextchange',
inputType: msg.data.inputType,
value: msg.data.value,
choices: JSON.stringify(msg.data.choices),
min: msg.data.min,
max: msg.data.max
});
},
handleBlur: function keyboardHandleBlur(msg) {
let mm = msg.target.QueryInterface(Ci.nsIFrameLoaderOwner)
.frameLoader.messageManager;
// A blur message can't be sent to the keyboard if the focus has
// already been taken away at first place.
// This check is here to prevent problem caused by out-of-order
// ipc messages from two processes.
if (mm !== this.formMM) {
return;
}
// unset formMM
this.formMM = null;
// Notify the nsIHardwareKeyHandler that
// the input-method-app is disabled now.
if (hardwareKeyHandler && this._isConnectedToHardwareKeyHandler) {
this._isConnectedToHardwareKeyHandler = false;
hardwareKeyHandler.onInputMethodAppDisconnected();
}
this.forwardEvent('Keyboard:Blur', msg);
this.sendToSystem('System:Blur', {});
// XXX: To be removed when content migrate away from mozChromeEvents.
SystemAppProxy.dispatchEvent({
type: 'inputmethod-contextchange',
inputType: 'blur'
});
},
forwardEvent: function keyboardForwardEvent(newEventName, msg) {
this.sendToKeyboard(newEventName, msg.data);
},
setSelectedOption: function keyboardSetSelectedOption(msg) {
this.sendToForm('Forms:Select:Choice', msg.data);
},
setSelectedOptions: function keyboardSetSelectedOptions(msg) {
this.sendToForm('Forms:Select:Choice', msg.data);
},
setSelectionRange: function keyboardSetSelectionRange(msg) {
this.sendToForm('Forms:SetSelectionRange', msg.data);
},
setValue: function keyboardSetValue(msg) {
this.sendToForm('Forms:Input:Value', msg.data);
},
removeFocus: function keyboardRemoveFocus() {
if (!this.formMM) {
return;
}
this.sendToForm('Forms:Select:Blur', {});
},
replaceSurroundingText: function keyboardReplaceSurroundingText(msg) {
this.sendToForm('Forms:ReplaceSurroundingText', msg.data);
},
showInputMethodPicker: function keyboardShowInputMethodPicker() {
this.sendToSystem('System:ShowAll', {});
// XXX: To be removed with mozContentEvent support from shell.js
SystemAppProxy.dispatchEvent({
type: "inputmethod-showall"
});
},
switchToNextInputMethod: function keyboardSwitchToNextInputMethod() {
this.sendToSystem('System:Next', {});
// XXX: To be removed with mozContentEvent support from shell.js
SystemAppProxy.dispatchEvent({
type: "inputmethod-next"
});
},
sendKey: function keyboardSendKey(msg) {
this.sendToForm('Forms:Input:SendKey', msg.data);
},
getContext: function keyboardGetContext(msg) {
if (!this.formMM) {
return;
}
this.sendToKeyboard('Keyboard:SupportsSwitchingTypesChange', {
types: this._supportsSwitchingTypes
});
this.sendToForm('Forms:GetContext', msg.data);
},
setComposition: function keyboardSetComposition(msg) {
this.sendToForm('Forms:SetComposition', msg.data);
},
endComposition: function keyboardEndComposition(msg) {
this.sendToForm('Forms:EndComposition', msg.data);
},
setSupportsSwitchingTypes: function setSupportsSwitchingTypes(msg) {
this._supportsSwitchingTypes = msg.data.types;
this.sendToKeyboard('Keyboard:SupportsSwitchingTypesChange', msg.data);
},
// XXX: To be removed with mozContentEvent support from shell.js
setLayouts: function keyboardSetLayouts(layouts) {
// The input method plugins may not have loaded yet,
// cache the layouts so on init we can respond immediately instead
// of going back and forth between keyboard_manager
var types = [];
Object.keys(layouts).forEach((type) => {
if (layouts[type] > 1) {
types.push(type);
}
});
this._supportsSwitchingTypes = types;
this.sendToKeyboard('Keyboard:SupportsSwitchingTypesChange', {
types: types
});
}
};
function InputRegistryGlue() {
this._messageId = 0;
this._msgMap = new Map();
ppmm.addMessageListener('InputRegistry:Add', this);
ppmm.addMessageListener('InputRegistry:Remove', this);
ppmm.addMessageListener('System:InputRegistry:Add:Done', this);
ppmm.addMessageListener('System:InputRegistry:Remove:Done', this);
};
InputRegistryGlue.prototype.receiveMessage = function(msg) {
let mm = Utils.getMMFromMessage(msg);
let permName = msg.name.startsWith("System:") ? "input-mgmt" : "input";
if (!Utils.checkPermissionForMM(mm, permName)) {
dump("InputRegistryGlue message " + msg.name +
" from a content process with no " + permName + " privileges.");
return;
}
switch (msg.name) {
case 'InputRegistry:Add':
this.addInput(msg, mm);
break;
case 'InputRegistry:Remove':
this.removeInput(msg, mm);
break;
case 'System:InputRegistry:Add:Done':
case 'System:InputRegistry:Remove:Done':
this.returnMessage(msg.data);
break;
}
};
InputRegistryGlue.prototype.addInput = function(msg, mm) {
let msgId = this._messageId++;
this._msgMap.set(msgId, {
mm: mm,
requestId: msg.data.requestId
});
let manifestURL = appsService.getManifestURLByLocalId(msg.data.appId);
Keyboard.sendToSystem('System:InputRegistry:Add', {
id: msgId,
manifestURL: manifestURL,
inputId: msg.data.inputId,
inputManifest: msg.data.inputManifest
});
// XXX: To be removed when content migrate away from mozChromeEvents.
SystemAppProxy.dispatchEvent({
type: 'inputregistry-add',
id: msgId,
manifestURL: manifestURL,
inputId: msg.data.inputId,
inputManifest: msg.data.inputManifest
});
};
InputRegistryGlue.prototype.removeInput = function(msg, mm) {
let msgId = this._messageId++;
this._msgMap.set(msgId, {
mm: mm,
requestId: msg.data.requestId
});
let manifestURL = appsService.getManifestURLByLocalId(msg.data.appId);
Keyboard.sendToSystem('System:InputRegistry:Remove', {
id: msgId,
manifestURL: manifestURL,
inputId: msg.data.inputId
});
// XXX: To be removed when content migrate away from mozChromeEvents.
SystemAppProxy.dispatchEvent({
type: 'inputregistry-remove',
id: msgId,
manifestURL: manifestURL,
inputId: msg.data.inputId
});
};
InputRegistryGlue.prototype.returnMessage = function(detail) {
if (!this._msgMap.has(detail.id)) {
dump('InputRegistryGlue: Ignoring already handled message response. ' +
'id=' + detail.id + '\n');
return;
}
let { mm, requestId } = this._msgMap.get(detail.id);
this._msgMap.delete(detail.id);
if (Cu.isDeadWrapper(mm)) {
dump('InputRegistryGlue: Message manager has already died.\n');
return;
}
if (!('error' in detail)) {
mm.sendAsyncMessage('InputRegistry:Result:OK', {
requestId: requestId
});
} else {
mm.sendAsyncMessage('InputRegistry:Result:Error', {
error: detail.error,
requestId: requestId
});
}
};
this.Keyboard.init();
|