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