2012-11-07 15:53:24 +04:00
|
|
|
/* 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");
|
|
|
|
|
2014-04-07 17:59:48 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy",
|
|
|
|
"resource://gre/modules/SystemAppProxy.jsm");
|
|
|
|
|
2013-09-17 19:11:57 +04:00
|
|
|
this.Keyboard = {
|
2014-08-07 10:44:46 +04:00
|
|
|
_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.
|
2014-03-31 06:38:45 +04:00
|
|
|
_systemMessageName: [
|
|
|
|
'SetValue', 'RemoveFocus', 'SetSelectedOption', 'SetSelectedOptions'
|
|
|
|
],
|
|
|
|
|
2012-11-07 15:53:24 +04:00
|
|
|
_messageNames: [
|
2014-04-25 03:16:00 +04:00
|
|
|
'RemoveFocus',
|
2013-08-01 21:16:43 +04:00
|
|
|
'SetSelectionRange', 'ReplaceSurroundingText', 'ShowInputMethodPicker',
|
2013-08-08 04:07:41 +04:00
|
|
|
'SwitchToNextInputMethod', 'HideInputMethod',
|
2013-08-30 16:07:16 +04:00
|
|
|
'GetText', 'SendKey', 'GetContext',
|
2014-02-13 10:29:50 +04:00
|
|
|
'SetComposition', 'EndComposition',
|
|
|
|
'Register', 'Unregister'
|
2012-11-07 15:53:24 +04:00
|
|
|
],
|
|
|
|
|
2014-02-13 10:29:50 +04:00
|
|
|
get formMM() {
|
|
|
|
if (this._formMM && !Cu.isDeadWrapper(this._formMM))
|
|
|
|
return this._formMM;
|
2012-11-07 15:53:24 +04:00
|
|
|
|
2013-09-23 17:40:59 +04:00
|
|
|
return null;
|
2012-11-07 15:53:24 +04:00
|
|
|
},
|
|
|
|
|
2014-02-13 10:29:50 +04:00
|
|
|
set formMM(mm) {
|
|
|
|
this._formMM = mm;
|
2012-11-07 15:53:24 +04:00
|
|
|
},
|
|
|
|
|
2014-02-13 10:29:50 +04:00
|
|
|
sendToForm: function(name, data) {
|
2013-09-06 22:18:48 +04:00
|
|
|
try {
|
2014-02-13 10:29:50 +04:00
|
|
|
this.formMM.sendAsyncMessage(name, data);
|
|
|
|
} catch(e) { }
|
|
|
|
},
|
|
|
|
|
|
|
|
sendToKeyboard: function(name, data) {
|
|
|
|
try {
|
|
|
|
this._keyboardMM.sendAsyncMessage(name, data);
|
2013-09-06 22:18:48 +04:00
|
|
|
} catch(e) { }
|
|
|
|
},
|
|
|
|
|
2012-11-07 15:53:24 +04:00
|
|
|
init: function keyboardInit() {
|
2014-02-21 02:19:18 +04:00
|
|
|
Services.obs.addObserver(this, 'inprocess-browser-shown', false);
|
|
|
|
Services.obs.addObserver(this, 'remote-browser-shown', false);
|
2013-05-24 13:58:24 +04:00
|
|
|
Services.obs.addObserver(this, 'oop-frameloader-crashed', false);
|
2012-11-07 15:53:24 +04:00
|
|
|
|
2014-03-31 06:38:45 +04:00
|
|
|
for (let name of this._messageNames) {
|
2012-11-07 15:53:24 +04:00
|
|
|
ppmm.addMessageListener('Keyboard:' + name, this);
|
2014-03-31 06:38:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let name of this._systemMessageName) {
|
|
|
|
ppmm.addMessageListener('System:' + name, this);
|
|
|
|
}
|
2012-11-07 15:53:24 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
observe: function keyboardObserve(subject, topic, data) {
|
|
|
|
let frameLoader = subject.QueryInterface(Ci.nsIFrameLoader);
|
|
|
|
let mm = frameLoader.messageManager;
|
2013-05-24 13:58:24 +04:00
|
|
|
|
|
|
|
if (topic == 'oop-frameloader-crashed') {
|
2014-02-13 10:29:50 +04:00
|
|
|
if (this.formMM == mm) {
|
2013-05-24 13:58:24 +04:00
|
|
|
// The application has been closed unexpectingly. Let's tell the
|
|
|
|
// keyboard app that the focus has been lost.
|
2014-02-13 10:29:50 +04:00
|
|
|
this.sendToKeyboard('Keyboard:FocusChange', { 'type': 'blur' });
|
2013-05-24 13:58:24 +04:00
|
|
|
}
|
|
|
|
} else {
|
2014-02-21 02:19:18 +04:00
|
|
|
// Ignore notifications that aren't from a BrowserOrApp
|
|
|
|
if (!frameLoader.ownerIsBrowserOrAppFrame) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-11 18:11:43 +04:00
|
|
|
this.initFormsFrameScript(mm);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
initFormsFrameScript: function(mm) {
|
|
|
|
mm.addMessageListener('Forms:Input', this);
|
|
|
|
mm.addMessageListener('Forms:SelectionChange', this);
|
|
|
|
mm.addMessageListener('Forms:GetText:Result:OK', this);
|
|
|
|
mm.addMessageListener('Forms:GetText:Result:Error', this);
|
|
|
|
mm.addMessageListener('Forms:SetSelectionRange:Result:OK', this);
|
2014-03-20 14:37:00 +04:00
|
|
|
mm.addMessageListener('Forms:SetSelectionRange:Result:Error', this);
|
2013-11-11 18:11:43 +04:00
|
|
|
mm.addMessageListener('Forms:ReplaceSurroundingText:Result:OK', this);
|
2014-03-20 14:37:00 +04:00
|
|
|
mm.addMessageListener('Forms:ReplaceSurroundingText:Result:Error', this);
|
2013-11-11 18:11:43 +04:00
|
|
|
mm.addMessageListener('Forms:SendKey:Result:OK', this);
|
2014-01-02 19:39:26 +04:00
|
|
|
mm.addMessageListener('Forms:SendKey:Result:Error', this);
|
2013-11-11 18:11:43 +04:00
|
|
|
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);
|
2012-11-07 15:53:24 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function keyboardReceiveMessage(msg) {
|
2014-03-31 06:38:45 +04:00
|
|
|
// If we get a 'Keyboard:XXX'/'System:XXX' message, check that the sender
|
|
|
|
// has the required permission.
|
2014-02-13 10:29:50 +04:00
|
|
|
let mm;
|
|
|
|
let isKeyboardRegistration = msg.name == "Keyboard:Register" ||
|
|
|
|
msg.name == "Keyboard:Unregister";
|
2014-03-31 06:38:45 +04:00
|
|
|
if (msg.name.indexOf("Keyboard:") === 0 ||
|
|
|
|
msg.name.indexOf("System:") === 0) {
|
2014-02-13 10:29:50 +04:00
|
|
|
if (!this.formMM && !isKeyboardRegistration) {
|
2013-09-23 17:40:59 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-16 00:35:18 +04:00
|
|
|
try {
|
|
|
|
mm = msg.target.QueryInterface(Ci.nsIFrameLoaderOwner)
|
|
|
|
.frameLoader.messageManager;
|
|
|
|
} catch(e) {
|
|
|
|
mm = msg.target;
|
|
|
|
}
|
|
|
|
|
|
|
|
// That should never happen.
|
|
|
|
if (!mm) {
|
|
|
|
dump("!! No message manager found for " + msg.name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-20 07:05:52 +04:00
|
|
|
let testing = false;
|
|
|
|
try {
|
|
|
|
testing = Services.prefs.getBoolPref("dom.mozInputMethod.testing");
|
|
|
|
} catch (e) {
|
|
|
|
}
|
2014-03-31 06:38:45 +04:00
|
|
|
|
|
|
|
let perm = (msg.name.indexOf("Keyboard:") === 0) ? "input"
|
|
|
|
: "input-manage";
|
2014-02-13 10:29:50 +04:00
|
|
|
if (!isKeyboardRegistration && !testing &&
|
2014-03-31 06:38:45 +04:00
|
|
|
!mm.assertPermission(perm)) {
|
2013-02-16 00:35:18 +04:00
|
|
|
dump("Keyboard message " + msg.name +
|
2014-03-31 06:38:45 +04:00
|
|
|
" from a content process with no '" + perm + "' privileges.");
|
2013-02-16 00:35:18 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-07 10:44:46 +04:00
|
|
|
// 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:Register' !== msg.name && this._keyboardID !== kbID)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-07 15:53:24 +04:00
|
|
|
switch (msg.name) {
|
|
|
|
case 'Forms:Input':
|
2013-08-09 22:01:28 +04:00
|
|
|
this.handleFocusChange(msg);
|
2012-11-07 15:53:24 +04:00
|
|
|
break;
|
2013-03-15 16:28:51 +04:00
|
|
|
case 'Forms:SelectionChange':
|
2013-08-08 04:07:41 +04:00
|
|
|
case 'Forms:GetText:Result:OK':
|
|
|
|
case 'Forms:GetText:Result:Error':
|
|
|
|
case 'Forms:SetSelectionRange:Result:OK':
|
|
|
|
case 'Forms:ReplaceSurroundingText:Result:OK':
|
|
|
|
case 'Forms:SendKey:Result:OK':
|
2014-01-02 19:39:26 +04:00
|
|
|
case 'Forms:SendKey:Result:Error':
|
2013-08-08 04:07:41 +04:00
|
|
|
case 'Forms:SequenceError':
|
|
|
|
case 'Forms:GetContext:Result:OK':
|
2013-08-30 16:07:16 +04:00
|
|
|
case 'Forms:SetComposition:Result:OK':
|
|
|
|
case 'Forms:EndComposition:Result:OK':
|
2014-03-20 14:37:00 +04:00
|
|
|
case 'Forms:SetSelectionRange:Result:Error':
|
|
|
|
case 'Forms:ReplaceSurroundingText:Result:Error':
|
2013-08-08 04:07:41 +04:00
|
|
|
let name = msg.name.replace(/^Forms/, 'Keyboard');
|
|
|
|
this.forwardEvent(name, msg);
|
2013-03-15 16:28:51 +04:00
|
|
|
break;
|
2013-08-08 04:07:41 +04:00
|
|
|
|
2014-03-31 06:38:45 +04:00
|
|
|
case 'System:SetValue':
|
2012-11-07 15:53:24 +04:00
|
|
|
this.setValue(msg);
|
|
|
|
break;
|
|
|
|
case 'Keyboard:RemoveFocus':
|
2014-03-31 06:38:45 +04:00
|
|
|
case 'System:RemoveFocus':
|
2012-11-07 15:53:24 +04:00
|
|
|
this.removeFocus();
|
|
|
|
break;
|
2014-03-31 06:38:45 +04:00
|
|
|
case 'System:SetSelectedOption':
|
2012-11-07 15:53:24 +04:00
|
|
|
this.setSelectedOption(msg);
|
|
|
|
break;
|
2014-03-31 06:38:45 +04:00
|
|
|
case 'System:SetSelectedOptions':
|
2012-11-07 15:53:24 +04:00
|
|
|
this.setSelectedOption(msg);
|
|
|
|
break;
|
2013-03-15 16:28:51 +04:00
|
|
|
case 'Keyboard:SetSelectionRange':
|
|
|
|
this.setSelectionRange(msg);
|
|
|
|
break;
|
2013-04-13 18:53:41 +04:00
|
|
|
case 'Keyboard:ReplaceSurroundingText':
|
|
|
|
this.replaceSurroundingText(msg);
|
|
|
|
break;
|
2013-08-01 21:16:43 +04:00
|
|
|
case 'Keyboard:SwitchToNextInputMethod':
|
|
|
|
this.switchToNextInputMethod();
|
|
|
|
break;
|
|
|
|
case 'Keyboard:ShowInputMethodPicker':
|
|
|
|
this.showInputMethodPicker();
|
|
|
|
break;
|
2013-08-08 04:07:41 +04:00
|
|
|
case 'Keyboard:GetText':
|
|
|
|
this.getText(msg);
|
|
|
|
break;
|
|
|
|
case 'Keyboard:SendKey':
|
|
|
|
this.sendKey(msg);
|
|
|
|
break;
|
|
|
|
case 'Keyboard:GetContext':
|
|
|
|
this.getContext(msg);
|
|
|
|
break;
|
2013-08-30 16:07:16 +04:00
|
|
|
case 'Keyboard:SetComposition':
|
|
|
|
this.setComposition(msg);
|
|
|
|
break;
|
|
|
|
case 'Keyboard:EndComposition':
|
|
|
|
this.endComposition(msg);
|
|
|
|
break;
|
2014-02-13 10:29:50 +04:00
|
|
|
case 'Keyboard:Register':
|
|
|
|
this._keyboardMM = mm;
|
2014-08-07 10:44:46 +04:00
|
|
|
if (kbID !== null) {
|
|
|
|
// 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;
|
|
|
|
}
|
2014-02-13 10:29:50 +04:00
|
|
|
break;
|
|
|
|
case 'Keyboard:Unregister':
|
|
|
|
this._keyboardMM = null;
|
2014-08-07 10:44:46 +04:00
|
|
|
this._keyboardID = -1;
|
2014-02-13 10:29:50 +04:00
|
|
|
break;
|
2012-11-07 15:53:24 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-08-08 04:07:41 +04:00
|
|
|
forwardEvent: function keyboardForwardEvent(newEventName, msg) {
|
2014-02-13 10:29:50 +04:00
|
|
|
this.formMM = msg.target.QueryInterface(Ci.nsIFrameLoaderOwner)
|
|
|
|
.frameLoader.messageManager;
|
2012-11-07 15:53:24 +04:00
|
|
|
|
2014-02-13 10:29:50 +04:00
|
|
|
this.sendToKeyboard(newEventName, msg.data);
|
2013-03-15 16:28:51 +04:00
|
|
|
},
|
|
|
|
|
2013-08-09 22:01:28 +04:00
|
|
|
handleFocusChange: function keyboardHandleFocusChange(msg) {
|
|
|
|
this.forwardEvent('Keyboard:FocusChange', msg);
|
|
|
|
|
2013-10-17 19:08:55 +04:00
|
|
|
// Chrome event, used also to render value selectors; that's why we need
|
|
|
|
// the info about choices / min / max here as well...
|
2014-04-07 17:59:48 +04:00
|
|
|
SystemAppProxy.dispatchEvent({
|
2013-08-09 22:01:28 +04:00
|
|
|
type: 'inputmethod-contextchange',
|
2013-10-17 19:08:55 +04:00
|
|
|
inputType: msg.data.type,
|
|
|
|
value: msg.data.value,
|
|
|
|
choices: JSON.stringify(msg.data.choices),
|
|
|
|
min: msg.data.min,
|
|
|
|
max: msg.data.max
|
2013-08-09 22:01:28 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-11-07 15:53:24 +04:00
|
|
|
setSelectedOption: function keyboardSetSelectedOption(msg) {
|
2014-02-13 10:29:50 +04:00
|
|
|
this.sendToForm('Forms:Select:Choice', msg.data);
|
2012-11-07 15:53:24 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
setSelectedOptions: function keyboardSetSelectedOptions(msg) {
|
2014-02-13 10:29:50 +04:00
|
|
|
this.sendToForm('Forms:Select:Choice', msg.data);
|
2012-11-07 15:53:24 +04:00
|
|
|
},
|
|
|
|
|
2013-03-15 16:28:51 +04:00
|
|
|
setSelectionRange: function keyboardSetSelectionRange(msg) {
|
2014-02-13 10:29:50 +04:00
|
|
|
this.sendToForm('Forms:SetSelectionRange', msg.data);
|
2013-03-15 16:28:51 +04:00
|
|
|
},
|
|
|
|
|
2012-11-07 15:53:24 +04:00
|
|
|
setValue: function keyboardSetValue(msg) {
|
2014-02-13 10:29:50 +04:00
|
|
|
this.sendToForm('Forms:Input:Value', msg.data);
|
2012-11-07 15:53:24 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
removeFocus: function keyboardRemoveFocus() {
|
2014-02-13 10:29:50 +04:00
|
|
|
this.sendToForm('Forms:Select:Blur', {});
|
2013-04-13 18:53:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
replaceSurroundingText: function keyboardReplaceSurroundingText(msg) {
|
2014-02-13 10:29:50 +04:00
|
|
|
this.sendToForm('Forms:ReplaceSurroundingText', msg.data);
|
2013-08-01 21:16:43 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
showInputMethodPicker: function keyboardShowInputMethodPicker() {
|
2014-04-07 17:59:48 +04:00
|
|
|
SystemAppProxy.dispatchEvent({
|
2013-08-09 22:01:28 +04:00
|
|
|
type: "inputmethod-showall"
|
2013-08-01 21:16:43 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
switchToNextInputMethod: function keyboardSwitchToNextInputMethod() {
|
2014-04-07 17:59:48 +04:00
|
|
|
SystemAppProxy.dispatchEvent({
|
2013-08-09 22:01:28 +04:00
|
|
|
type: "inputmethod-next"
|
2013-08-01 21:16:43 +04:00
|
|
|
});
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
getText: function keyboardGetText(msg) {
|
2014-02-13 10:29:50 +04:00
|
|
|
this.sendToForm('Forms:GetText', msg.data);
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
sendKey: function keyboardSendKey(msg) {
|
2014-02-13 10:29:50 +04:00
|
|
|
this.sendToForm('Forms:Input:SendKey', msg.data);
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
getContext: function keyboardGetContext(msg) {
|
2013-09-12 21:02:36 +04:00
|
|
|
if (this._layouts) {
|
2014-02-13 10:29:50 +04:00
|
|
|
this.sendToKeyboard('Keyboard:LayoutsChange', this._layouts);
|
2013-09-12 21:02:36 +04:00
|
|
|
}
|
|
|
|
|
2014-02-13 10:29:50 +04:00
|
|
|
this.sendToForm('Forms:GetContext', msg.data);
|
2013-08-30 16:07:16 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
setComposition: function keyboardSetComposition(msg) {
|
2014-02-13 10:29:50 +04:00
|
|
|
this.sendToForm('Forms:SetComposition', msg.data);
|
2013-08-30 16:07:16 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
endComposition: function keyboardEndComposition(msg) {
|
2014-02-13 10:29:50 +04:00
|
|
|
this.sendToForm('Forms:EndComposition', msg.data);
|
2013-09-12 21:02:36 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of keyboard layouts active from keyboard_manager
|
|
|
|
*/
|
|
|
|
_layouts: null,
|
|
|
|
setLayouts: function keyboardSetLayoutCount(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
|
|
|
|
this._layouts = layouts;
|
|
|
|
|
2014-02-13 10:29:50 +04:00
|
|
|
this.sendToKeyboard('Keyboard:LayoutsChange', layouts);
|
2012-11-07 15:53:24 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-17 19:11:57 +04:00
|
|
|
this.Keyboard.init();
|