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
|
|
|
|
2013-08-09 02:41:48 -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-08-19 12:00:22 -07:00
|
|
|
Cu.import("resource://gre/modules/ObjectWrapper.jsm");
|
2013-08-07 17:07:41 -07:00
|
|
|
Cu.import("resource://gre/modules/DOMRequestHelper.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");
|
|
|
|
|
2013-04-07 02:38:40 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "tm",
|
|
|
|
"@mozilla.org/thread-manager;1", "nsIThreadManager");
|
|
|
|
|
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;
|
2013-04-07 02:38:40 -07:00
|
|
|
|
|
|
|
let mainThread = tm.mainThread;
|
|
|
|
let utils = this._utils;
|
|
|
|
|
|
|
|
function send(type) {
|
|
|
|
mainThread.dispatch(function() {
|
2013-08-01 10:16:43 -07:00
|
|
|
utils.sendKeyEvent(type, keyCode, charCode, null);
|
2013-04-07 02:38:40 -07:00
|
|
|
}, mainThread.DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
send("keydown");
|
|
|
|
send("keypress");
|
|
|
|
send("keyup");
|
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;
|
|
|
|
},
|
|
|
|
|
2013-04-13 07:53:41 -07:00
|
|
|
replaceSurroundingText: function mozKeyboardReplaceSurroundingText(
|
|
|
|
text, beforeLength, afterLength) {
|
|
|
|
cpmm.sendAsyncMessage('Keyboard:ReplaceSurroundingText', {
|
|
|
|
'text': text || '',
|
|
|
|
'beforeLength': (typeof beforeLength === 'number' ? beforeLength : 0),
|
|
|
|
'afterLength': (typeof afterLength === 'number' ? afterLength: 0)
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-01 10:16:43 -07:00
|
|
|
/**
|
|
|
|
* ==============================================
|
|
|
|
* InputMethodManager
|
|
|
|
* ==============================================
|
|
|
|
*/
|
|
|
|
function MozInputMethodManager() { }
|
|
|
|
|
|
|
|
MozInputMethodManager.prototype = {
|
|
|
|
classID: Components.ID("{7e9d7280-ef86-11e2-b778-0800200c9a66}"),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
|
|
|
Ci.nsIInputMethodManager
|
|
|
|
]),
|
|
|
|
|
|
|
|
classInfo: XPCOMUtils.generateCI({
|
|
|
|
"classID": Components.ID("{7e9d7280-ef86-11e2-b778-0800200c9a66}"),
|
|
|
|
"contractID": "@mozilla.org/b2g-imm;1",
|
|
|
|
"interfaces": [Ci.nsIInputMethodManager],
|
|
|
|
"flags": Ci.nsIClassInfo.DOM_OBJECT,
|
|
|
|
"classDescription": "B2G Input Method Manager"
|
|
|
|
}),
|
|
|
|
|
|
|
|
showAll: function() {
|
|
|
|
cpmm.sendAsyncMessage('Keyboard:ShowInputMethodPicker', {});
|
|
|
|
},
|
|
|
|
|
|
|
|
next: function() {
|
|
|
|
cpmm.sendAsyncMessage('Keyboard:SwitchToNextInputMethod', {});
|
|
|
|
},
|
|
|
|
|
|
|
|
supportsSwitching: function() {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
hide: function() {
|
|
|
|
cpmm.sendAsyncMessage('Keyboard:RemoveFocus', {});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ==============================================
|
|
|
|
* InputMethod
|
|
|
|
* ==============================================
|
|
|
|
*/
|
|
|
|
function MozInputMethod() { }
|
|
|
|
|
|
|
|
MozInputMethod.prototype = {
|
2013-08-07 17:07:41 -07:00
|
|
|
_inputcontext: null,
|
|
|
|
|
|
|
|
classID: Components.ID("{4607330d-e7d2-40a4-9eb8-43967eae0142}"),
|
2013-08-01 10:16:43 -07:00
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
|
|
|
Ci.nsIInputMethod,
|
2013-08-07 17:07:41 -07:00
|
|
|
Ci.nsIDOMGlobalPropertyInitializer,
|
|
|
|
Ci.nsIObserver
|
2013-08-01 10:16:43 -07:00
|
|
|
]),
|
|
|
|
|
|
|
|
classInfo: XPCOMUtils.generateCI({
|
2013-08-07 17:07:41 -07:00
|
|
|
"classID": Components.ID("{4607330d-e7d2-40a4-9eb8-43967eae0142}"),
|
2013-08-01 10:16:43 -07:00
|
|
|
"contractID": "@mozilla.org/b2g-inputmethod;1",
|
|
|
|
"interfaces": [Ci.nsIInputMethod],
|
|
|
|
"flags": Ci.nsIClassInfo.DOM_OBJECT,
|
|
|
|
"classDescription": "B2G Input Method"
|
|
|
|
}),
|
|
|
|
|
|
|
|
init: function mozInputMethodInit(win) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:07:41 -07:00
|
|
|
this._window = win;
|
2013-08-01 10:16:43 -07:00
|
|
|
this._mgmt = new MozInputMethodManager();
|
2013-08-07 17:07:41 -07:00
|
|
|
this.innerWindowID = win.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils)
|
|
|
|
.currentInnerWindowID;
|
|
|
|
|
|
|
|
Services.obs.addObserver(this, "inner-window-destroyed", false);
|
|
|
|
cpmm.addMessageListener('Keyboard:FocusChange', this);
|
|
|
|
cpmm.addMessageListener('Keyboard:SelectionChange', this);
|
|
|
|
cpmm.addMessageListener('Keyboard:GetContext:Result:OK', this);
|
|
|
|
|
|
|
|
// If there already is an active context, then this will trigger
|
|
|
|
// a GetContext:Result:OK event, and we can initialize ourselves.
|
|
|
|
// Otherwise silently ignored.
|
|
|
|
cpmm.sendAsyncMessage("Keyboard:GetContext", {});
|
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function mozInputMethodUninit() {
|
|
|
|
Services.obs.removeObserver(this, "inner-window-destroyed");
|
|
|
|
cpmm.removeMessageListener('Keyboard:FocusChange', this);
|
|
|
|
cpmm.removeMessageListener('Keyboard:SelectionChange', this);
|
|
|
|
cpmm.removeMessageListener('Keyboard:GetContext:Result:OK', this);
|
|
|
|
|
|
|
|
this._window = null;
|
|
|
|
this._mgmt = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function mozInputMethodReceiveMsg(msg) {
|
|
|
|
let json = msg.json;
|
|
|
|
|
|
|
|
switch(msg.name) {
|
|
|
|
case 'Keyboard:FocusChange':
|
|
|
|
if (json.type !== 'blur') {
|
2013-08-09 02:41:48 -07:00
|
|
|
// XXX Bug 904339 could receive 'text' event twice
|
2013-08-07 17:07:41 -07:00
|
|
|
this.setInputContext(json);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.setInputContext(null);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Keyboard:SelectionChange':
|
|
|
|
this._inputcontext.updateSelectionContext(json);
|
|
|
|
break;
|
|
|
|
case 'Keyboard:GetContext:Result:OK':
|
|
|
|
this.setInputContext(json);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function mozInputMethodObserve(subject, topic, data) {
|
|
|
|
let wId = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
|
|
|
|
if (wId == this.innerWindowID)
|
|
|
|
this.uninit();
|
2013-08-01 10:16:43 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
get mgmt() {
|
|
|
|
return this._mgmt;
|
2013-08-07 17:07:41 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
get inputcontext() {
|
|
|
|
return this._inputcontext;
|
|
|
|
},
|
|
|
|
|
2013-08-09 02:41:48 -07:00
|
|
|
set oninputcontextchange(handler) {
|
|
|
|
this.__DOM_IMPL__.setEventHandler("oninputcontextchange", handler);
|
2013-08-07 17:07:41 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
get oninputcontextchange() {
|
2013-08-09 02:41:48 -07:00
|
|
|
return this.__DOM_IMPL__.getEventHandler("oninputcontextchange");
|
2013-08-07 17:07:41 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
setInputContext: function mozKeyboardContextChange(data) {
|
|
|
|
if (this._inputcontext) {
|
|
|
|
this._inputcontext.destroy();
|
|
|
|
this._inputcontext = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
this._inputcontext = new MozInputContext(data);
|
|
|
|
this._inputcontext.init(this._window);
|
|
|
|
}
|
|
|
|
|
2013-08-09 02:41:48 -07:00
|
|
|
let event = new this._window.Event("inputcontextchange",
|
|
|
|
ObjectWrapper.wrap({}, this._window));
|
|
|
|
this.__DOM_IMPL__.dispatchEvent(event);
|
2013-08-07 17:07:41 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ==============================================
|
|
|
|
* InputContext
|
|
|
|
* ==============================================
|
|
|
|
*/
|
|
|
|
function MozInputContext(ctx) {
|
|
|
|
this._context = {
|
|
|
|
inputtype: ctx.type,
|
|
|
|
inputmode: ctx.inputmode,
|
|
|
|
lang: ctx.lang,
|
|
|
|
type: ["textarea", "contenteditable"].indexOf(ctx.type) > -1 ?
|
|
|
|
ctx.type :
|
|
|
|
"text",
|
|
|
|
selectionStart: ctx.selectionStart,
|
|
|
|
selectionEnd: ctx.selectionEnd,
|
|
|
|
textBeforeCursor: ctx.textBeforeCursor,
|
|
|
|
textAfterCursor: ctx.textAfterCursor
|
|
|
|
};
|
|
|
|
|
|
|
|
this._contextId = ctx.contextId;
|
|
|
|
}
|
|
|
|
|
|
|
|
MozInputContext.prototype = {
|
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
|
|
|
|
|
|
|
_context: null,
|
|
|
|
_contextId: -1,
|
|
|
|
|
|
|
|
classID: Components.ID("{1e38633d-d08b-4867-9944-afa5c648adb6}"),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
|
|
|
Ci.nsIB2GInputContext,
|
|
|
|
Ci.nsIObserver
|
|
|
|
]),
|
|
|
|
|
|
|
|
classInfo: XPCOMUtils.generateCI({
|
|
|
|
"classID": Components.ID("{1e38633d-d08b-4867-9944-afa5c648adb6}"),
|
|
|
|
"contractID": "@mozilla.org/b2g-inputcontext;1",
|
|
|
|
"interfaces": [Ci.nsIB2GInputContext],
|
|
|
|
"flags": Ci.nsIClassInfo.DOM_OBJECT,
|
|
|
|
"classDescription": "B2G Input Context"
|
|
|
|
}),
|
|
|
|
|
|
|
|
init: function ic_init(win) {
|
|
|
|
this._window = win;
|
|
|
|
this._utils = win.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils);
|
|
|
|
this.initDOMRequestHelper(win,
|
|
|
|
["Keyboard:GetText:Result:OK",
|
|
|
|
"Keyboard:GetText:Result:Error",
|
|
|
|
"Keyboard:SetSelectionRange:Result:OK",
|
|
|
|
"Keyboard:ReplaceSurroundingText:Result:OK",
|
|
|
|
"Keyboard:SendKey:Result:OK",
|
2013-08-30 05:07:16 -07:00
|
|
|
"Keyboard:SetComposition:Result:OK",
|
|
|
|
"Keyboard:EndComposition:Result:OK",
|
2013-08-07 17:07:41 -07:00
|
|
|
"Keyboard:SequenceError"]);
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function ic_destroy() {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
// All requests that are still pending need to be invalidated
|
|
|
|
// because the context is no longer valid.
|
2013-08-09 02:41:48 -07:00
|
|
|
this.forEachPromiseResolver(function(k) {
|
|
|
|
self.takePromiseResolver(k).reject("InputContext got destroyed");
|
2013-08-07 17:07:41 -07:00
|
|
|
});
|
|
|
|
this.destroyDOMRequestHelper();
|
|
|
|
|
2013-08-09 02:41:48 -07:00
|
|
|
// A consuming application might still hold a cached version of
|
|
|
|
// this object. After destroying all methods will throw because we
|
|
|
|
// cannot create new promises anymore, but we still hold
|
2013-08-07 17:07:41 -07:00
|
|
|
// (outdated) information in the context. So let's clear that out.
|
2013-08-09 02:41:48 -07:00
|
|
|
for (var k in this._context) {
|
|
|
|
if (this._context.hasOwnProperty(k)) {
|
2013-08-07 17:07:41 -07:00
|
|
|
this._context[k] = null;
|
2013-08-09 02:41:48 -07:00
|
|
|
}
|
|
|
|
}
|
2013-08-07 17:07:41 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function ic_receiveMessage(msg) {
|
|
|
|
if (!msg || !msg.json) {
|
|
|
|
dump('InputContext received message without data\n');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let json = msg.json;
|
2013-08-09 02:41:48 -07:00
|
|
|
let resolver = this.takePromiseResolver(json.requestId);
|
2013-08-07 17:07:41 -07:00
|
|
|
|
2013-08-09 02:41:48 -07:00
|
|
|
if (!resolver) {
|
2013-08-07 17:07:41 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (msg.name) {
|
|
|
|
case "Keyboard:SendKey:Result:OK":
|
2013-08-09 02:41:48 -07:00
|
|
|
resolver.resolve();
|
2013-08-07 17:07:41 -07:00
|
|
|
break;
|
|
|
|
case "Keyboard:GetText:Result:OK":
|
2013-08-09 02:41:48 -07:00
|
|
|
resolver.resolve(json.text);
|
2013-08-07 17:07:41 -07:00
|
|
|
break;
|
|
|
|
case "Keyboard:GetText:Result:Error":
|
2013-08-09 02:41:48 -07:00
|
|
|
resolver.reject(json.error);
|
2013-08-07 17:07:41 -07:00
|
|
|
break;
|
|
|
|
case "Keyboard:SetSelectionRange:Result:OK":
|
|
|
|
case "Keyboard:ReplaceSurroundingText:Result:OK":
|
2013-08-09 02:41:48 -07:00
|
|
|
resolver.resolve(
|
2013-08-07 17:07:41 -07:00
|
|
|
ObjectWrapper.wrap(json.selectioninfo, this._window));
|
|
|
|
break;
|
|
|
|
case "Keyboard:SequenceError":
|
|
|
|
// Occurs when a new element got focus, but the inputContext was
|
|
|
|
// not invalidated yet...
|
2013-08-09 02:41:48 -07:00
|
|
|
resolver.reject("InputContext has expired");
|
2013-08-07 17:07:41 -07:00
|
|
|
break;
|
2013-08-30 05:07:16 -07:00
|
|
|
case "Keyboard:SetComposition:Result:OK": // Fall through.
|
|
|
|
case "Keyboard:EndComposition:Result:OK":
|
|
|
|
resolver.resolve();
|
|
|
|
break;
|
2013-08-07 17:07:41 -07:00
|
|
|
default:
|
2013-08-09 02:41:48 -07:00
|
|
|
dump("Could not find a handler for " + msg.name);
|
|
|
|
resolver.reject();
|
2013-08-07 17:07:41 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
updateSelectionContext: function ic_updateSelectionContext(ctx) {
|
|
|
|
if (!this._context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let selectionDirty = this._context.selectionStart !== ctx.selectionStart ||
|
|
|
|
this._context.selectionEnd !== ctx.selectionEnd;
|
|
|
|
let surroundDirty = this._context.textBeforeCursor !== ctx.textBeforeCursor ||
|
|
|
|
this._context.textAfterCursor !== ctx.textAfterCursor;
|
|
|
|
|
|
|
|
this._context.selectionStart = ctx.selectionStart;
|
|
|
|
this._context.selectionEnd = ctx.selectionEnd;
|
|
|
|
this._context.textBeforeCursor = ctx.textBeforeCursor;
|
|
|
|
this._context.textAfterCursor = ctx.textAfterCursor;
|
|
|
|
|
|
|
|
if (selectionDirty) {
|
2013-08-09 02:41:48 -07:00
|
|
|
this._fireEvent("selectionchange", {
|
2013-08-07 17:07:41 -07:00
|
|
|
selectionStart: ctx.selectionStart,
|
|
|
|
selectionEnd: ctx.selectionEnd
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (surroundDirty) {
|
2013-08-09 02:41:48 -07:00
|
|
|
this._fireEvent("surroundingtextchange", {
|
2013-08-07 17:07:41 -07:00
|
|
|
beforeString: ctx.textBeforeCursor,
|
|
|
|
afterString: ctx.textAfterCursor
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-08-09 02:41:48 -07:00
|
|
|
_fireEvent: function ic_fireEvent(eventName, aDetail) {
|
2013-08-07 17:07:41 -07:00
|
|
|
let detail = {
|
|
|
|
detail: aDetail
|
|
|
|
};
|
|
|
|
|
2013-08-09 02:41:48 -07:00
|
|
|
let event = new this._window.Event(eventName,
|
|
|
|
ObjectWrapper.wrap(aDetail, this._window));
|
|
|
|
this.__DOM_IMPL__.dispatchEvent(event);
|
2013-08-07 17:07:41 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// tag name of the input field
|
|
|
|
get type() {
|
|
|
|
return this._context.type;
|
|
|
|
},
|
|
|
|
|
|
|
|
// type of the input field
|
|
|
|
get inputType() {
|
|
|
|
return this._context.inputtype;
|
|
|
|
},
|
|
|
|
|
|
|
|
get inputMode() {
|
|
|
|
return this._context.inputmode;
|
|
|
|
},
|
|
|
|
|
|
|
|
get lang() {
|
|
|
|
return this._context.lang;
|
|
|
|
},
|
|
|
|
|
|
|
|
getText: function ic_getText(offset, length) {
|
2013-08-09 02:41:48 -07:00
|
|
|
let self = this;
|
2013-09-11 09:03:04 -07:00
|
|
|
return this.createPromise(function(resolve, reject) {
|
|
|
|
let resolverId = self.getPromiseResolverId({ resolve: resolve, reject: reject });
|
2013-08-09 02:41:48 -07:00
|
|
|
cpmm.sendAsyncMessage('Keyboard:GetText', {
|
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
offset: offset,
|
|
|
|
length: length
|
|
|
|
});
|
2013-08-07 17:07:41 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
get selectionStart() {
|
|
|
|
return this._context.selectionStart;
|
|
|
|
},
|
|
|
|
|
|
|
|
get selectionEnd() {
|
|
|
|
return this._context.selectionEnd;
|
|
|
|
},
|
|
|
|
|
|
|
|
get textBeforeCursor() {
|
|
|
|
return this._context.textBeforeCursor;
|
|
|
|
},
|
|
|
|
|
|
|
|
get textAfterCursor() {
|
|
|
|
return this._context.textAfterCursor;
|
|
|
|
},
|
|
|
|
|
|
|
|
setSelectionRange: function ic_setSelectionRange(start, length) {
|
2013-08-09 02:41:48 -07:00
|
|
|
let self = this;
|
2013-09-11 09:03:04 -07:00
|
|
|
return this.createPromise(function(resolve, reject) {
|
|
|
|
let resolverId = self.getPromiseResolverId({ resolve: resolve, reject: reject });
|
2013-08-09 02:41:48 -07:00
|
|
|
cpmm.sendAsyncMessage("Keyboard:SetSelectionRange", {
|
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
selectionStart: start,
|
|
|
|
selectionEnd: start + length
|
|
|
|
});
|
2013-08-07 17:07:41 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
get onsurroundingtextchange() {
|
2013-08-09 02:41:48 -07:00
|
|
|
return this.__DOM_IMPL__.getEventHandler("onsurroundingtextchange");
|
2013-08-07 17:07:41 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
set onsurroundingtextchange(handler) {
|
2013-08-09 02:41:48 -07:00
|
|
|
this.__DOM_IMPL__.setEventHandler("onsurroundingtextchange");
|
2013-08-07 17:07:41 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
get onselectionchange() {
|
2013-08-09 02:41:48 -07:00
|
|
|
return this.__DOM_IMPL__.getEventHandler("onselectionchange");
|
2013-08-07 17:07:41 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
set onselectionchange(handler) {
|
2013-08-09 02:41:48 -07:00
|
|
|
this.__DOM_IMPL__.setEventHandler("onselectionchange");
|
2013-08-07 17:07:41 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
replaceSurroundingText: function ic_replaceSurrText(text, offset, length) {
|
2013-08-09 02:41:48 -07:00
|
|
|
let self = this;
|
2013-09-11 09:03:04 -07:00
|
|
|
return this.createPromise(function(resolve, reject) {
|
|
|
|
let resolverId = self.getPromiseResolverId({ resolve: resolve, reject: reject });
|
2013-08-09 02:41:48 -07:00
|
|
|
cpmm.sendAsyncMessage('Keyboard:ReplaceSurroundingText', {
|
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
text: text,
|
|
|
|
beforeLength: offset || 0,
|
|
|
|
afterLength: length || 0
|
|
|
|
});
|
2013-08-07 17:07:41 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteSurroundingText: function ic_deleteSurrText(offset, length) {
|
|
|
|
return this.replaceSurroundingText(null, offset, length);
|
|
|
|
},
|
|
|
|
|
|
|
|
sendKey: function ic_sendKey(keyCode, charCode, modifiers) {
|
2013-08-09 02:41:48 -07:00
|
|
|
let self = this;
|
2013-09-11 09:03:04 -07:00
|
|
|
return this.createPromise(function(resolve, reject) {
|
|
|
|
let resolverId = self.getPromiseResolverId({ resolve: resolve, reject: reject });
|
2013-08-09 02:41:48 -07:00
|
|
|
cpmm.sendAsyncMessage('Keyboard:SendKey', {
|
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
keyCode: keyCode,
|
|
|
|
charCode: charCode,
|
|
|
|
modifiers: modifiers
|
|
|
|
});
|
2013-08-07 17:07:41 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-30 05:07:16 -07:00
|
|
|
setComposition: function ic_setComposition(text, cursor, clauses) {
|
|
|
|
let self = this;
|
2013-09-11 09:03:04 -07:00
|
|
|
return this.createPromise(function(resolve, reject) {
|
|
|
|
let resolverId = self.getPromiseResolverId({ resolve: resolve, reject: reject });
|
2013-08-30 05:07:16 -07:00
|
|
|
cpmm.sendAsyncMessage('Keyboard:SetComposition', {
|
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
text: text,
|
|
|
|
cursor: cursor || text.length,
|
|
|
|
clauses: clauses || null
|
|
|
|
});
|
|
|
|
});
|
2013-08-07 17:07:41 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
endComposition: function ic_endComposition(text) {
|
2013-08-30 05:07:16 -07:00
|
|
|
let self = this;
|
2013-09-11 09:03:04 -07:00
|
|
|
return this.createPromise(function(resolve, reject) {
|
|
|
|
let resolverId = self.getPromiseResolverId({ resolve: resolve, reject: reject });
|
2013-08-30 05:07:16 -07:00
|
|
|
cpmm.sendAsyncMessage('Keyboard:EndComposition', {
|
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
text: text || ''
|
|
|
|
});
|
|
|
|
});
|
2013-08-01 10:16:43 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory(
|
2013-08-09 02:41:48 -07:00
|
|
|
[MozKeyboard, MozInputMethod]);
|