2012-03-19 10:30:51 -07: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/. */
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
"use strict";
|
2012-03-19 10:30:51 -07:00
|
|
|
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2012-08-19 12:00:22 -07:00
|
|
|
Cu.import("resource://gre/modules/ObjectWrapper.jsm");
|
2012-03-19 10:30:51 -07:00
|
|
|
|
2012-11-07 03:53:24 -08:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1", "nsIMessageSender");
|
|
|
|
|
2012-03-19 10:30:51 -07:00
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
// MozKeyboard
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
2013-02-15 12:35:18 -08:00
|
|
|
function MozKeyboard() { }
|
2012-03-19 10:30:51 -07:00
|
|
|
|
|
|
|
MozKeyboard.prototype = {
|
|
|
|
classID: Components.ID("{397a7fdf-2254-47be-b74e-76625a1a66d5}"),
|
2012-06-06 05:19:33 -07:00
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
|
|
|
Ci.nsIB2GKeyboard, Ci.nsIDOMGlobalPropertyInitializer, Ci.nsIObserver
|
|
|
|
]),
|
|
|
|
|
|
|
|
classInfo: XPCOMUtils.generateCI({
|
|
|
|
"classID": Components.ID("{397a7fdf-2254-47be-b74e-76625a1a66d5}"),
|
|
|
|
"contractID": "@mozilla.org/b2g-keyboard;1",
|
|
|
|
"interfaces": [Ci.nsIB2GKeyboard],
|
|
|
|
"flags": Ci.nsIClassInfo.DOM_OBJECT,
|
|
|
|
"classDescription": "B2G Virtual Keyboard"
|
|
|
|
}),
|
|
|
|
|
|
|
|
init: function mozKeyboardInit(win) {
|
2013-02-15 12:35:18 -08:00
|
|
|
let principal = win.document.nodePrincipal;
|
|
|
|
let perm = Services.perms
|
|
|
|
.testExactPermissionFromPrincipal(principal, "keyboard");
|
|
|
|
if (perm != Ci.nsIPermissionManager.ALLOW_ACTION) {
|
|
|
|
dump("No permission to use the keyboard API for " +
|
|
|
|
principal.origin + "\n");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-03-19 10:30:51 -07:00
|
|
|
Services.obs.addObserver(this, "inner-window-destroyed", false);
|
2012-11-07 03:53:24 -08:00
|
|
|
cpmm.addMessageListener('Keyboard:FocusChange', this);
|
2013-03-15 05:28:51 -07:00
|
|
|
cpmm.addMessageListener('Keyboard:SelectionChange', this);
|
2012-06-06 05:19:33 -07:00
|
|
|
|
|
|
|
this._window = win;
|
|
|
|
this._utils = win.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils);
|
2012-03-19 10:30:51 -07:00
|
|
|
this.innerWindowID = this._utils.currentInnerWindowID;
|
2012-06-06 05:19:33 -07:00
|
|
|
this._focusHandler = null;
|
2013-03-15 05:28:51 -07:00
|
|
|
this._selectionHandler = null;
|
|
|
|
this._selectionStart = -1;
|
|
|
|
this._selectionEnd = -1;
|
2012-03-19 10:30:51 -07:00
|
|
|
},
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
uninit: function mozKeyboardUninit() {
|
|
|
|
Services.obs.removeObserver(this, "inner-window-destroyed");
|
2012-11-07 03:53:24 -08:00
|
|
|
cpmm.removeMessageListener('Keyboard:FocusChange', this);
|
2013-03-15 05:28:51 -07:00
|
|
|
cpmm.removeMessageListener('Keyboard:SelectionChange', this);
|
2012-11-07 03:53:24 -08:00
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
this._window = null;
|
|
|
|
this._utils = null;
|
|
|
|
this._focusHandler = null;
|
2013-03-15 05:28:51 -07:00
|
|
|
this._selectionHandler = null;
|
2012-03-19 10:30:51 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
sendKey: function mozKeyboardSendKey(keyCode, charCode) {
|
|
|
|
charCode = (charCode == undefined) ? keyCode : charCode;
|
2012-06-06 05:19:33 -07:00
|
|
|
["keydown", "keypress", "keyup"].forEach((function sendKey(type) {
|
2012-03-19 10:30:51 -07:00
|
|
|
this._utils.sendKeyEvent(type, keyCode, charCode, null);
|
|
|
|
}).bind(this));
|
2012-06-06 05:19:33 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
setSelectedOption: function mozKeyboardSetSelectedOption(index) {
|
2012-11-07 03:53:24 -08:00
|
|
|
cpmm.sendAsyncMessage('Keyboard:SetSelectedOption', {
|
|
|
|
'index': index
|
2012-06-06 05:19:33 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
setValue: function mozKeyboardSetValue(value) {
|
2012-11-07 03:53:24 -08:00
|
|
|
cpmm.sendAsyncMessage('Keyboard:SetValue', {
|
|
|
|
'value': value
|
2012-06-06 05:19:33 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
setSelectedOptions: function mozKeyboardSetSelectedOptions(indexes) {
|
2012-11-07 03:53:24 -08:00
|
|
|
cpmm.sendAsyncMessage('Keyboard:SetSelectedOptions', {
|
|
|
|
'indexes': indexes
|
2012-06-06 05:19:33 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-03-15 05:28:51 -07:00
|
|
|
set onselectionchange(val) {
|
|
|
|
this._selectionHandler = val;
|
|
|
|
},
|
|
|
|
|
|
|
|
get onselectionchange() {
|
|
|
|
return this._selectionHandler;
|
|
|
|
},
|
|
|
|
|
|
|
|
get selectionStart() {
|
|
|
|
return this._selectionStart;
|
|
|
|
},
|
|
|
|
|
|
|
|
get selectionEnd() {
|
|
|
|
return this._selectionEnd;
|
|
|
|
},
|
|
|
|
|
|
|
|
setSelectionRange: function mozKeyboardSetSelectionRange(start, end) {
|
|
|
|
cpmm.sendAsyncMessage('Keyboard:SetSelectionRange', {
|
|
|
|
'selectionStart': start,
|
|
|
|
'selectionEnd': end
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-10-17 04:18:14 -07:00
|
|
|
removeFocus: function mozKeyboardRemoveFocus() {
|
2012-11-07 03:53:24 -08:00
|
|
|
cpmm.sendAsyncMessage('Keyboard:RemoveFocus', {});
|
2012-10-17 04:18:14 -07:00
|
|
|
},
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
set onfocuschange(val) {
|
|
|
|
this._focusHandler = val;
|
|
|
|
},
|
|
|
|
|
|
|
|
get onfocuschange() {
|
|
|
|
return this._focusHandler;
|
|
|
|
},
|
|
|
|
|
2012-11-07 03:53:24 -08:00
|
|
|
receiveMessage: function mozKeyboardReceiveMessage(msg) {
|
2013-03-15 05:28:51 -07:00
|
|
|
if (msg.name == "Keyboard:FocusChange") {
|
|
|
|
let msgJson = msg.json;
|
|
|
|
if (msgJson.type != "blur") {
|
|
|
|
this._selectionStart = msgJson.selectionStart;
|
|
|
|
this._selectionEnd = msgJson.selectionEnd;
|
|
|
|
} else {
|
|
|
|
this._selectionStart = 0;
|
|
|
|
this._selectionEnd = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
let handler = this._focusHandler;
|
|
|
|
if (!handler || !(handler instanceof Ci.nsIDOMEventListener))
|
|
|
|
return;
|
|
|
|
|
|
|
|
let detail = {
|
|
|
|
"detail": msgJson
|
|
|
|
};
|
|
|
|
|
|
|
|
let evt = new this._window.CustomEvent("focuschanged",
|
|
|
|
ObjectWrapper.wrap(detail, this._window));
|
|
|
|
handler.handleEvent(evt);
|
|
|
|
} else if (msg.name == "Keyboard:SelectionChange") {
|
|
|
|
let msgJson = msg.json;
|
|
|
|
|
|
|
|
this._selectionStart = msgJson.selectionStart;
|
|
|
|
this._selectionEnd = msgJson.selectionEnd;
|
|
|
|
|
|
|
|
let handler = this._selectionHandler;
|
|
|
|
if (!handler || !(handler instanceof Ci.nsIDOMEventListener))
|
|
|
|
return;
|
|
|
|
|
|
|
|
let evt = new this._window.CustomEvent("selectionchange",
|
|
|
|
ObjectWrapper.wrap({}, this._window));
|
|
|
|
handler.handleEvent(evt);
|
|
|
|
}
|
2012-06-06 05:19:33 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
observe: function mozKeyboardObserve(subject, topic, data) {
|
2012-11-07 03:53:24 -08:00
|
|
|
let wId = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
|
|
|
|
if (wId == this.innerWindowID)
|
|
|
|
this.uninit();
|
2012-03-19 10:30:51 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MozKeyboard]);
|