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;
|
|
|
|
const kFormsFrameScript = 'chrome://browser/content/forms.js';
|
|
|
|
|
|
|
|
Cu.import('resource://gre/modules/Services.jsm');
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
|
|
|
|
"@mozilla.org/parentprocessmessagemanager;1", "nsIMessageBroadcaster");
|
|
|
|
|
|
|
|
let Keyboard = {
|
|
|
|
_messageManager: null,
|
|
|
|
_messageNames: [
|
2013-03-15 05:28:51 -07:00
|
|
|
'SetValue', 'RemoveFocus', 'SetSelectedOption', 'SetSelectedOptions',
|
|
|
|
'SetSelectionRange'
|
2012-11-07 03:53:24 -08:00
|
|
|
],
|
|
|
|
|
|
|
|
get messageManager() {
|
|
|
|
if (this._messageManager && !Cu.isDeadWrapper(this._messageManager))
|
|
|
|
return this._messageManager;
|
|
|
|
|
|
|
|
throw Error('no message manager set');
|
|
|
|
},
|
|
|
|
|
|
|
|
set messageManager(mm) {
|
|
|
|
this._messageManager = mm;
|
|
|
|
},
|
|
|
|
|
|
|
|
init: function keyboardInit() {
|
2012-11-12 06:10:32 -08:00
|
|
|
Services.obs.addObserver(this, 'in-process-browser-or-app-frame-shown', false);
|
2012-11-07 03:53:24 -08:00
|
|
|
Services.obs.addObserver(this, 'remote-browser-frame-shown', false);
|
|
|
|
|
|
|
|
for (let name of this._messageNames)
|
|
|
|
ppmm.addMessageListener('Keyboard:' + name, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function keyboardObserve(subject, topic, data) {
|
|
|
|
let frameLoader = subject.QueryInterface(Ci.nsIFrameLoader);
|
|
|
|
let mm = frameLoader.messageManager;
|
|
|
|
mm.addMessageListener('Forms:Input', this);
|
2013-03-15 05:28:51 -07:00
|
|
|
mm.addMessageListener('Forms:SelectionChange', this);
|
2013-02-16 18:58:00 -08:00
|
|
|
|
|
|
|
// When not running apps OOP, we need to load forms.js here since this
|
|
|
|
// won't happen from dom/ipc/preload.js
|
|
|
|
try {
|
|
|
|
if (Services.prefs.getBoolPref("dom.ipc.tabs.disabled") === true) {
|
|
|
|
mm.loadFrameScript(kFormsFrameScript, true);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
dump('Error loading ' + kFormsFrameScript + ' as frame script: ' + e + '\n');
|
|
|
|
}
|
2012-11-07 03:53:24 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function keyboardReceiveMessage(msg) {
|
2013-02-15 12:35:18 -08:00
|
|
|
// If we get a 'Keyboard:XXX' message, check that the sender has the
|
|
|
|
// keyboard permission.
|
2013-03-15 05:28:51 -07:00
|
|
|
if (msg.name.indexOf("Keyboard:") != -1) {
|
2013-02-15 12:35:18 -08:00
|
|
|
let mm;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mm.assertPermission("keyboard")) {
|
|
|
|
dump("Keyboard message " + msg.name +
|
|
|
|
" from a content process with no 'keyboard' privileges.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-07 03:53:24 -08:00
|
|
|
switch (msg.name) {
|
|
|
|
case 'Forms:Input':
|
|
|
|
this.handleFormsInput(msg);
|
|
|
|
break;
|
2013-03-15 05:28:51 -07:00
|
|
|
case 'Forms:SelectionChange':
|
|
|
|
this.handleFormsSelectionChange(msg);
|
|
|
|
break;
|
2012-11-07 03:53:24 -08:00
|
|
|
case 'Keyboard:SetValue':
|
|
|
|
this.setValue(msg);
|
|
|
|
break;
|
|
|
|
case 'Keyboard:RemoveFocus':
|
|
|
|
this.removeFocus();
|
|
|
|
break;
|
|
|
|
case 'Keyboard:SetSelectedOption':
|
|
|
|
this.setSelectedOption(msg);
|
|
|
|
break;
|
|
|
|
case 'Keyboard:SetSelectedOptions':
|
|
|
|
this.setSelectedOption(msg);
|
|
|
|
break;
|
2013-03-15 05:28:51 -07:00
|
|
|
case 'Keyboard:SetSelectionRange':
|
|
|
|
this.setSelectionRange(msg);
|
|
|
|
break;
|
2012-11-07 03:53:24 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleFormsInput: function keyboardHandleFormsInput(msg) {
|
|
|
|
this.messageManager = msg.target.QueryInterface(Ci.nsIFrameLoaderOwner)
|
|
|
|
.frameLoader.messageManager;
|
|
|
|
|
|
|
|
ppmm.broadcastAsyncMessage('Keyboard:FocusChange', msg.data);
|
|
|
|
},
|
|
|
|
|
2013-03-15 05:28:51 -07:00
|
|
|
handleFormsSelectionChange: function keyboardHandleFormsSelectionChange(msg) {
|
|
|
|
this.messageManager = msg.target.QueryInterface(Ci.nsIFrameLoaderOwner)
|
|
|
|
.frameLoader.messageManager;
|
|
|
|
|
|
|
|
ppmm.broadcastAsyncMessage('Keyboard:SelectionChange', msg.data);
|
|
|
|
},
|
|
|
|
|
2012-11-07 03:53:24 -08:00
|
|
|
setSelectedOption: function keyboardSetSelectedOption(msg) {
|
|
|
|
this.messageManager.sendAsyncMessage('Forms:Select:Choice', msg.data);
|
|
|
|
},
|
|
|
|
|
|
|
|
setSelectedOptions: function keyboardSetSelectedOptions(msg) {
|
|
|
|
this.messageManager.sendAsyncMessage('Forms:Select:Choice', msg.data);
|
|
|
|
},
|
|
|
|
|
2013-03-15 05:28:51 -07:00
|
|
|
setSelectionRange: function keyboardSetSelectionRange(msg) {
|
|
|
|
this.messageManager.sendAsyncMessage('Forms:SetSelectionRange', msg.data);
|
|
|
|
},
|
|
|
|
|
2012-11-07 03:53:24 -08:00
|
|
|
setValue: function keyboardSetValue(msg) {
|
|
|
|
this.messageManager.sendAsyncMessage('Forms:Input:Value', msg.data);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeFocus: function keyboardRemoveFocus() {
|
|
|
|
this.messageManager.sendAsyncMessage('Forms:Select:Blur', {});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Keyboard.init();
|