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
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
const Cc = Components.classes;
|
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-06-06 05:19:33 -07:00
|
|
|
const messageManager = Cc["@mozilla.org/globalmessagemanager;1"]
|
|
|
|
.getService(Ci.nsIChromeFrameMessageManager);
|
|
|
|
|
|
|
|
|
2012-03-19 10:30:51 -07:00
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
// MozKeyboard
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
function MozKeyboard() { }
|
|
|
|
|
|
|
|
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) {
|
|
|
|
messageManager.loadFrameScript("chrome://browser/content/forms.js", true);
|
|
|
|
messageManager.addMessageListener("Forms:Input", this);
|
|
|
|
|
2012-03-19 10:30:51 -07:00
|
|
|
Services.obs.addObserver(this, "inner-window-destroyed", false);
|
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;
|
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");
|
|
|
|
messageManager.removeMessageListener("Forms:Input", this);
|
|
|
|
this._window = null;
|
|
|
|
this._utils = null;
|
|
|
|
this._focusHandler = 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) {
|
|
|
|
messageManager.sendAsyncMessage("Forms:Select:Choice", {
|
|
|
|
"index": index
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
setValue: function mozKeyboardSetValue(value) {
|
|
|
|
messageManager.sendAsyncMessage("Forms:Input:Value", {
|
|
|
|
"value": value
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-06-06 05:19:33 -07:00
|
|
|
setSelectedOptions: function mozKeyboardSetSelectedOptions(indexes) {
|
|
|
|
messageManager.sendAsyncMessage("Forms:Select:Choice", {
|
|
|
|
"indexes": indexes || []
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
set onfocuschange(val) {
|
|
|
|
this._focusHandler = val;
|
|
|
|
},
|
|
|
|
|
|
|
|
get onfocuschange() {
|
|
|
|
return this._focusHandler;
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function mozKeyboardReceiveMessage(msg) {
|
|
|
|
let handler = this._focusHandler;
|
|
|
|
if (!handler || !(handler instanceof Ci.nsIDOMEventListener))
|
|
|
|
return;
|
|
|
|
|
|
|
|
let detail = {
|
|
|
|
"detail": msg.json
|
|
|
|
};
|
|
|
|
|
|
|
|
let evt = new this._window.CustomEvent("focuschanged", detail);
|
|
|
|
handler.handleEvent(evt);
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function mozKeyboardObserve(subject, topic, data) {
|
|
|
|
if (topic == "inner-window-destroyed") {
|
|
|
|
let wId = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
|
|
|
|
if (wId == this.innerWindowID) {
|
|
|
|
this.uninit();
|
|
|
|
}
|
|
|
|
}
|
2012-03-19 10:30:51 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const NSGetFactory = XPCOMUtils.generateNSGetFactory([MozKeyboard]);
|
2012-06-06 05:19:33 -07:00
|
|
|
|