2012-03-19 17:30:51 +00: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 12:19:33 +00:00
|
|
|
"use strict";
|
2012-03-19 17:30:51 +00:00
|
|
|
|
2013-08-09 09:41:48 +00:00
|
|
|
const Cc = Components.classes;
|
2012-03-19 17:30:51 +00:00
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2013-08-08 00:07:41 +00:00
|
|
|
Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
|
2012-03-19 17:30:51 +00:00
|
|
|
|
2012-11-07 11:53:24 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
2014-08-07 06:44:46 +00:00
|
|
|
"@mozilla.org/childprocessmessagemanager;1", "nsISyncMessageSender");
|
2012-11-07 11:53:24 +00:00
|
|
|
|
2013-04-07 09:38:40 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "tm",
|
|
|
|
"@mozilla.org/thread-manager;1", "nsIThreadManager");
|
|
|
|
|
2013-09-23 13:40:59 +00:00
|
|
|
/*
|
2014-08-07 06:44:46 +00:00
|
|
|
* A WeakMap to map input method iframe window to its active status and kbID.
|
2013-09-23 13:40:59 +00:00
|
|
|
*/
|
|
|
|
let WindowMap = {
|
2014-08-07 06:44:46 +00:00
|
|
|
// WeakMap of <window, object> pairs.
|
2013-09-23 13:40:59 +00:00
|
|
|
_map: null,
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the given window is active.
|
|
|
|
*/
|
|
|
|
isActive: function(win) {
|
|
|
|
if (!this._map || !win) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-07 06:44:46 +00:00
|
|
|
|
|
|
|
let obj = this._map.get(win);
|
|
|
|
if (obj && 'active' in obj) {
|
|
|
|
return obj.active;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-23 13:40:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the active status of the given window.
|
|
|
|
*/
|
|
|
|
setActive: function(win, isActive) {
|
|
|
|
if (!win) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this._map) {
|
|
|
|
this._map = new WeakMap();
|
|
|
|
}
|
2014-08-07 06:44:46 +00:00
|
|
|
if (!this._map.has(win)) {
|
|
|
|
this._map.set(win, {});
|
|
|
|
}
|
|
|
|
this._map.get(win).active = isActive;
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the keyboard ID (assigned by Keyboard.ksm) of the given window.
|
|
|
|
*/
|
|
|
|
getKbID: function(win) {
|
|
|
|
if (!this._map || !win) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let obj = this._map.get(win);
|
|
|
|
if (obj && 'kbID' in obj) {
|
|
|
|
return obj.kbID;
|
|
|
|
}else{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the keyboard ID (assigned by Keyboard.ksm) of the given window.
|
|
|
|
*/
|
|
|
|
setKbID: function(win, kbID) {
|
|
|
|
if (!win) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this._map) {
|
|
|
|
this._map = new WeakMap();
|
|
|
|
}
|
|
|
|
if (!this._map.has(win)) {
|
|
|
|
this._map.set(win, {});
|
|
|
|
}
|
|
|
|
this._map.get(win).kbID = kbID;
|
2013-09-23 13:40:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-07 06:44:46 +00:00
|
|
|
let cpmmSendAsyncMessageWithKbID = function (self, msg, data) {
|
|
|
|
data.kbID = WindowMap.getKbID(self._window);
|
|
|
|
cpmm.sendAsyncMessage(msg, data);
|
|
|
|
};
|
|
|
|
|
2013-08-01 17:16:43 +00:00
|
|
|
/**
|
|
|
|
* ==============================================
|
|
|
|
* InputMethodManager
|
|
|
|
* ==============================================
|
|
|
|
*/
|
2013-09-23 13:40:59 +00:00
|
|
|
function MozInputMethodManager(win) {
|
|
|
|
this._window = win;
|
|
|
|
}
|
2013-08-01 17:16:43 +00:00
|
|
|
|
|
|
|
MozInputMethodManager.prototype = {
|
2013-09-12 17:02:36 +00:00
|
|
|
_supportsSwitching: false,
|
2013-09-23 13:40:59 +00:00
|
|
|
_window: null,
|
2013-09-12 17:02:36 +00:00
|
|
|
|
2013-08-01 17:16:43 +00:00
|
|
|
classID: Components.ID("{7e9d7280-ef86-11e2-b778-0800200c9a66}"),
|
|
|
|
|
2014-03-25 01:59:33 +00:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([]),
|
2013-08-01 17:16:43 +00:00
|
|
|
|
|
|
|
showAll: function() {
|
2013-09-23 13:40:59 +00:00
|
|
|
if (!WindowMap.isActive(this._window)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-07 06:44:46 +00:00
|
|
|
cpmmSendAsyncMessageWithKbID(this, 'Keyboard:ShowInputMethodPicker', {});
|
2013-08-01 17:16:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
next: function() {
|
2013-09-23 13:40:59 +00:00
|
|
|
if (!WindowMap.isActive(this._window)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-07 06:44:46 +00:00
|
|
|
cpmmSendAsyncMessageWithKbID(this, 'Keyboard:SwitchToNextInputMethod', {});
|
2013-08-01 17:16:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
supportsSwitching: function() {
|
2013-09-23 13:40:59 +00:00
|
|
|
if (!WindowMap.isActive(this._window)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-12 17:02:36 +00:00
|
|
|
return this._supportsSwitching;
|
2013-08-01 17:16:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
hide: function() {
|
2013-09-23 13:40:59 +00:00
|
|
|
if (!WindowMap.isActive(this._window)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-07 06:44:46 +00:00
|
|
|
cpmmSendAsyncMessageWithKbID(this, 'Keyboard:RemoveFocus', {});
|
2013-08-01 17:16:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ==============================================
|
|
|
|
* InputMethod
|
|
|
|
* ==============================================
|
|
|
|
*/
|
|
|
|
function MozInputMethod() { }
|
|
|
|
|
|
|
|
MozInputMethod.prototype = {
|
2014-12-09 19:20:40 +00:00
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
|
|
|
|
2013-08-08 00:07:41 +00:00
|
|
|
_inputcontext: null,
|
2014-07-03 10:10:13 +00:00
|
|
|
_wrappedInputContext: null,
|
2013-09-12 17:02:36 +00:00
|
|
|
_layouts: {},
|
2013-09-23 13:40:59 +00:00
|
|
|
_window: null,
|
2014-03-31 02:38:45 +00:00
|
|
|
_isSystem: false,
|
2014-04-09 18:03:00 +00:00
|
|
|
_isKeyboard: true,
|
2013-08-08 00:07:41 +00:00
|
|
|
|
|
|
|
classID: Components.ID("{4607330d-e7d2-40a4-9eb8-43967eae0142}"),
|
2013-08-01 17:16:43 +00:00
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
2013-08-08 00:07:41 +00:00
|
|
|
Ci.nsIDOMGlobalPropertyInitializer,
|
2014-03-31 02:38:45 +00:00
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference
|
2013-08-01 17:16:43 +00:00
|
|
|
]),
|
|
|
|
|
|
|
|
init: function mozInputMethodInit(win) {
|
2013-08-08 00:07:41 +00:00
|
|
|
this._window = win;
|
2013-09-23 13:40:59 +00:00
|
|
|
this._mgmt = new MozInputMethodManager(win);
|
2013-08-08 00:07:41 +00:00
|
|
|
this.innerWindowID = win.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils)
|
|
|
|
.currentInnerWindowID;
|
|
|
|
|
2014-04-09 18:03:00 +00:00
|
|
|
Services.obs.addObserver(this, "inner-window-destroyed", false);
|
|
|
|
|
2014-03-31 02:38:45 +00:00
|
|
|
let principal = win.document.nodePrincipal;
|
|
|
|
let perm = Services.perms.testExactPermissionFromPrincipal(principal,
|
|
|
|
"input-manage");
|
|
|
|
if (perm === Ci.nsIPermissionManager.ALLOW_ACTION) {
|
|
|
|
this._isSystem = true;
|
|
|
|
}
|
|
|
|
|
2014-04-09 18:03:00 +00:00
|
|
|
// Check if we can use keyboard related APIs.
|
|
|
|
let testing = false;
|
|
|
|
try {
|
|
|
|
testing = Services.prefs.getBoolPref("dom.mozInputMethod.testing");
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
perm = Services.perms.testExactPermissionFromPrincipal(principal, "input");
|
|
|
|
if (!testing && perm !== Ci.nsIPermissionManager.ALLOW_ACTION) {
|
|
|
|
this._isKeyboard = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-31 02:38:45 +00:00
|
|
|
cpmm.addWeakMessageListener('Keyboard:FocusChange', this);
|
|
|
|
cpmm.addWeakMessageListener('Keyboard:SelectionChange', this);
|
|
|
|
cpmm.addWeakMessageListener('Keyboard:GetContext:Result:OK', this);
|
|
|
|
cpmm.addWeakMessageListener('Keyboard:LayoutsChange', this);
|
2014-12-09 19:20:40 +00:00
|
|
|
cpmm.addWeakMessageListener('InputRegistry:Result:OK', this);
|
|
|
|
cpmm.addWeakMessageListener('InputRegistry:Result:Error', this);
|
2013-08-08 00:07:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function mozInputMethodUninit() {
|
2014-04-09 18:03:00 +00:00
|
|
|
this._window = null;
|
|
|
|
this._mgmt = null;
|
2013-08-08 00:07:41 +00:00
|
|
|
Services.obs.removeObserver(this, "inner-window-destroyed");
|
2014-04-09 18:03:00 +00:00
|
|
|
if (!this._isKeyboard) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-31 02:38:45 +00:00
|
|
|
cpmm.removeWeakMessageListener('Keyboard:FocusChange', this);
|
|
|
|
cpmm.removeWeakMessageListener('Keyboard:SelectionChange', this);
|
|
|
|
cpmm.removeWeakMessageListener('Keyboard:GetContext:Result:OK', this);
|
|
|
|
cpmm.removeWeakMessageListener('Keyboard:LayoutsChange', this);
|
2014-12-09 19:20:40 +00:00
|
|
|
cpmm.removeWeakMessageListener('InputRegistry:Result:OK', this);
|
|
|
|
cpmm.removeWeakMessageListener('InputRegistry:Result:Error', this);
|
2014-04-09 18:03:00 +00:00
|
|
|
this.setActive(false);
|
2013-08-08 00:07:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function mozInputMethodReceiveMsg(msg) {
|
2014-12-09 19:20:40 +00:00
|
|
|
if (!msg.name.startsWith('InputRegistry') &&
|
|
|
|
!WindowMap.isActive(this._window)) {
|
2013-09-23 13:40:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:20:40 +00:00
|
|
|
let data = msg.data;
|
|
|
|
let resolver = ('requestId' in data) ?
|
|
|
|
this.takePromiseResolver(data.requestId) : null;
|
2013-08-08 00:07:41 +00:00
|
|
|
|
|
|
|
switch(msg.name) {
|
|
|
|
case 'Keyboard:FocusChange':
|
2014-12-09 19:20:40 +00:00
|
|
|
if (data.type !== 'blur') {
|
2013-08-09 09:41:48 +00:00
|
|
|
// XXX Bug 904339 could receive 'text' event twice
|
2014-12-09 19:20:40 +00:00
|
|
|
this.setInputContext(data);
|
2013-08-08 00:07:41 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.setInputContext(null);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Keyboard:SelectionChange':
|
2013-09-25 18:42:40 +00:00
|
|
|
if (this.inputcontext) {
|
2014-12-09 19:20:40 +00:00
|
|
|
this._inputcontext.updateSelectionContext(data, false);
|
2013-09-25 18:42:40 +00:00
|
|
|
}
|
2013-08-08 00:07:41 +00:00
|
|
|
break;
|
|
|
|
case 'Keyboard:GetContext:Result:OK':
|
2014-12-09 19:20:40 +00:00
|
|
|
this.setInputContext(data);
|
2013-08-08 00:07:41 +00:00
|
|
|
break;
|
2013-09-12 17:02:36 +00:00
|
|
|
case 'Keyboard:LayoutsChange':
|
2014-12-09 19:20:40 +00:00
|
|
|
this._layouts = data;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'InputRegistry:Result:OK':
|
|
|
|
resolver.resolve();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'InputRegistry:Result:Error':
|
|
|
|
resolver.reject(data.error);
|
|
|
|
|
2013-09-12 17:02:36 +00:00
|
|
|
break;
|
2013-08-08 00:07:41 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function mozInputMethodObserve(subject, topic, data) {
|
|
|
|
let wId = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
|
|
|
|
if (wId == this.innerWindowID)
|
|
|
|
this.uninit();
|
2013-08-01 17:16:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get mgmt() {
|
|
|
|
return this._mgmt;
|
2013-08-08 00:07:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get inputcontext() {
|
2013-09-23 13:40:59 +00:00
|
|
|
if (!WindowMap.isActive(this._window)) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-07-03 10:10:13 +00:00
|
|
|
return this._wrappedInputContext;
|
2013-08-08 00:07:41 +00:00
|
|
|
},
|
|
|
|
|
2013-08-09 09:41:48 +00:00
|
|
|
set oninputcontextchange(handler) {
|
|
|
|
this.__DOM_IMPL__.setEventHandler("oninputcontextchange", handler);
|
2013-08-08 00:07:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get oninputcontextchange() {
|
2013-08-09 09:41:48 +00:00
|
|
|
return this.__DOM_IMPL__.getEventHandler("oninputcontextchange");
|
2013-08-08 00:07:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setInputContext: function mozKeyboardContextChange(data) {
|
|
|
|
if (this._inputcontext) {
|
|
|
|
this._inputcontext.destroy();
|
|
|
|
this._inputcontext = null;
|
2014-07-03 10:10:13 +00:00
|
|
|
this._wrappedInputContext = null;
|
2013-09-12 17:02:36 +00:00
|
|
|
this._mgmt._supportsSwitching = false;
|
2013-08-08 00:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data) {
|
2013-09-12 17:02:36 +00:00
|
|
|
this._mgmt._supportsSwitching = this._layouts[data.type] ?
|
|
|
|
this._layouts[data.type] > 1 :
|
|
|
|
false;
|
|
|
|
|
2013-08-08 00:07:41 +00:00
|
|
|
this._inputcontext = new MozInputContext(data);
|
|
|
|
this._inputcontext.init(this._window);
|
2014-07-03 10:10:13 +00:00
|
|
|
// inputcontext will be exposed as a WebIDL object. Create its
|
|
|
|
// content-side object explicitly to avoid Bug 1001325.
|
|
|
|
this._wrappedInputContext =
|
|
|
|
this._window.MozInputContext._create(this._window, this._inputcontext);
|
2013-08-08 00:07:41 +00:00
|
|
|
}
|
|
|
|
|
2013-08-09 09:41:48 +00:00
|
|
|
let event = new this._window.Event("inputcontextchange",
|
2014-02-01 19:06:59 +00:00
|
|
|
Cu.cloneInto({}, this._window));
|
2013-08-09 09:41:48 +00:00
|
|
|
this.__DOM_IMPL__.dispatchEvent(event);
|
2013-09-23 13:40:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setActive: function mozInputMethodSetActive(isActive) {
|
|
|
|
if (WindowMap.isActive(this._window) === isActive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WindowMap.setActive(this._window, isActive);
|
|
|
|
|
|
|
|
if (isActive) {
|
|
|
|
// Activate current input method.
|
|
|
|
// If there is already an active context, then this will trigger
|
|
|
|
// a GetContext:Result:OK event, and we can initialize ourselves.
|
|
|
|
// Otherwise silently ignored.
|
2014-08-07 06:44:46 +00:00
|
|
|
|
|
|
|
// get keyboard ID from Keyboard.jsm,
|
|
|
|
// or if we already have it, get it from our map
|
|
|
|
// Note: if we need to get it from Keyboard.jsm,
|
|
|
|
// we have to use a synchronous message
|
|
|
|
var kbID = WindowMap.getKbID(this._window);
|
|
|
|
if (kbID !== null) {
|
|
|
|
cpmmSendAsyncMessageWithKbID(this, 'Keyboard:Register', {});
|
|
|
|
}else{
|
|
|
|
let res = cpmm.sendSyncMessage('Keyboard:Register', {});
|
|
|
|
WindowMap.setKbID(this._window, res[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
cpmmSendAsyncMessageWithKbID(this, 'Keyboard:GetContext', {});
|
2013-09-23 13:40:59 +00:00
|
|
|
} else {
|
|
|
|
// Deactive current input method.
|
2014-08-07 06:44:46 +00:00
|
|
|
cpmmSendAsyncMessageWithKbID(this, 'Keyboard:Unregister', {});
|
2013-09-23 13:40:59 +00:00
|
|
|
if (this._inputcontext) {
|
|
|
|
this.setInputContext(null);
|
|
|
|
}
|
|
|
|
}
|
2014-03-31 02:38:45 +00:00
|
|
|
},
|
|
|
|
|
2014-12-09 19:20:40 +00:00
|
|
|
addInput: function(inputId, inputManifest) {
|
|
|
|
return this._sendPromise(function(resolverId) {
|
|
|
|
let appId = this._window.document.nodePrincipal.appId;
|
|
|
|
|
|
|
|
cpmm.sendAsyncMessage('InputRegistry:Add', {
|
|
|
|
requestId: resolverId,
|
|
|
|
inputId: inputId,
|
|
|
|
inputManifest: inputManifest,
|
|
|
|
appId: appId
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
removeInput: function(inputId) {
|
|
|
|
return this._sendPromise(function(resolverId) {
|
|
|
|
let appId = this._window.document.nodePrincipal.appId;
|
|
|
|
|
|
|
|
cpmm.sendAsyncMessage('InputRegistry:Remove', {
|
|
|
|
requestId: resolverId,
|
|
|
|
inputId: inputId,
|
|
|
|
appId: appId
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2014-03-31 02:38:45 +00:00
|
|
|
setValue: function(value) {
|
|
|
|
this._ensureIsSystem();
|
|
|
|
cpmm.sendAsyncMessage('System:SetValue', {
|
|
|
|
'value': value
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
setSelectedOption: function(index) {
|
|
|
|
this._ensureIsSystem();
|
|
|
|
cpmm.sendAsyncMessage('System:SetSelectedOption', {
|
|
|
|
'index': index
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
setSelectedOptions: function(indexes) {
|
|
|
|
this._ensureIsSystem();
|
|
|
|
cpmm.sendAsyncMessage('System:SetSelectedOptions', {
|
|
|
|
'indexes': indexes
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
removeFocus: function() {
|
|
|
|
this._ensureIsSystem();
|
|
|
|
cpmm.sendAsyncMessage('System:RemoveFocus', {});
|
|
|
|
},
|
|
|
|
|
|
|
|
_ensureIsSystem: function() {
|
|
|
|
if (!this._isSystem) {
|
|
|
|
throw new this._window.DOMError("Security",
|
|
|
|
"Should have 'input-manage' permssion.");
|
|
|
|
}
|
2014-12-09 19:20:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_sendPromise: function(callback) {
|
|
|
|
let self = this;
|
|
|
|
return this.createPromise(function(resolve, reject) {
|
|
|
|
let resolverId = self.getPromiseResolverId({ resolve: resolve, reject: reject });
|
|
|
|
callback(resolverId);
|
|
|
|
});
|
2013-08-08 00:07:41 +00: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,
|
|
|
|
|
2013-09-23 13:40:59 +00:00
|
|
|
_window: null,
|
2013-08-08 00:07:41 +00:00
|
|
|
_context: null,
|
|
|
|
_contextId: -1,
|
|
|
|
|
|
|
|
classID: Components.ID("{1e38633d-d08b-4867-9944-afa5c648adb6}"),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
2013-10-04 21:59:48 +00:00
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference
|
2013-08-08 00:07:41 +00:00
|
|
|
]),
|
|
|
|
|
|
|
|
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",
|
2014-01-02 15:39:26 +00:00
|
|
|
"Keyboard:SendKey:Result:Error",
|
2013-08-30 12:07:16 +00:00
|
|
|
"Keyboard:SetComposition:Result:OK",
|
|
|
|
"Keyboard:EndComposition:Result:OK",
|
2013-08-08 00:07:41 +00: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 09:41:48 +00:00
|
|
|
this.forEachPromiseResolver(function(k) {
|
|
|
|
self.takePromiseResolver(k).reject("InputContext got destroyed");
|
2013-08-08 00:07:41 +00:00
|
|
|
});
|
|
|
|
this.destroyDOMRequestHelper();
|
|
|
|
|
2013-08-09 09:41:48 +00: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-08 00:07:41 +00:00
|
|
|
// (outdated) information in the context. So let's clear that out.
|
2013-08-09 09:41:48 +00:00
|
|
|
for (var k in this._context) {
|
|
|
|
if (this._context.hasOwnProperty(k)) {
|
2013-08-08 00:07:41 +00:00
|
|
|
this._context[k] = null;
|
2013-08-09 09:41:48 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-23 13:40:59 +00:00
|
|
|
|
|
|
|
this._window = null;
|
2013-08-08 00:07:41 +00: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 09:41:48 +00:00
|
|
|
let resolver = this.takePromiseResolver(json.requestId);
|
2013-08-08 00:07:41 +00:00
|
|
|
|
2013-08-09 09:41:48 +00:00
|
|
|
if (!resolver) {
|
2013-08-08 00:07:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-13 06:12:00 +00:00
|
|
|
// Update context first before resolving promise to avoid race condition
|
|
|
|
if (json.selectioninfo) {
|
2014-08-25 20:47:00 +00:00
|
|
|
this.updateSelectionContext(json.selectioninfo, true);
|
2014-08-13 06:12:00 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 00:07:41 +00:00
|
|
|
switch (msg.name) {
|
|
|
|
case "Keyboard:SendKey:Result:OK":
|
2013-08-09 09:41:48 +00:00
|
|
|
resolver.resolve();
|
2013-08-08 00:07:41 +00:00
|
|
|
break;
|
2014-01-02 15:39:26 +00:00
|
|
|
case "Keyboard:SendKey:Result:Error":
|
|
|
|
resolver.reject(json.error);
|
|
|
|
break;
|
2013-08-08 00:07:41 +00:00
|
|
|
case "Keyboard:GetText:Result:OK":
|
2013-08-09 09:41:48 +00:00
|
|
|
resolver.resolve(json.text);
|
2013-08-08 00:07:41 +00:00
|
|
|
break;
|
|
|
|
case "Keyboard:GetText:Result:Error":
|
2013-08-09 09:41:48 +00:00
|
|
|
resolver.reject(json.error);
|
2013-08-08 00:07:41 +00:00
|
|
|
break;
|
|
|
|
case "Keyboard:SetSelectionRange:Result:OK":
|
|
|
|
case "Keyboard:ReplaceSurroundingText:Result:OK":
|
2013-08-09 09:41:48 +00:00
|
|
|
resolver.resolve(
|
2014-02-01 19:06:59 +00:00
|
|
|
Cu.cloneInto(json.selectioninfo, this._window));
|
2013-08-08 00:07:41 +00:00
|
|
|
break;
|
|
|
|
case "Keyboard:SequenceError":
|
|
|
|
// Occurs when a new element got focus, but the inputContext was
|
|
|
|
// not invalidated yet...
|
2013-08-09 09:41:48 +00:00
|
|
|
resolver.reject("InputContext has expired");
|
2013-08-08 00:07:41 +00:00
|
|
|
break;
|
2013-08-30 12:07:16 +00:00
|
|
|
case "Keyboard:SetComposition:Result:OK": // Fall through.
|
|
|
|
case "Keyboard:EndComposition:Result:OK":
|
|
|
|
resolver.resolve();
|
|
|
|
break;
|
2013-08-08 00:07:41 +00:00
|
|
|
default:
|
2013-08-09 09:41:48 +00:00
|
|
|
dump("Could not find a handler for " + msg.name);
|
|
|
|
resolver.reject();
|
2013-08-08 00:07:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-08-25 20:47:00 +00:00
|
|
|
updateSelectionContext: function ic_updateSelectionContext(ctx, ownAction) {
|
2013-08-08 00:07:41 +00:00
|
|
|
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 09:41:48 +00:00
|
|
|
this._fireEvent("selectionchange", {
|
2013-08-08 00:07:41 +00:00
|
|
|
selectionStart: ctx.selectionStart,
|
2014-08-25 20:47:00 +00:00
|
|
|
selectionEnd: ctx.selectionEnd,
|
|
|
|
ownAction: ownAction
|
2013-08-08 00:07:41 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (surroundDirty) {
|
2013-08-09 09:41:48 +00:00
|
|
|
this._fireEvent("surroundingtextchange", {
|
2013-08-08 00:07:41 +00:00
|
|
|
beforeString: ctx.textBeforeCursor,
|
2014-08-25 20:47:00 +00:00
|
|
|
afterString: ctx.textAfterCursor,
|
|
|
|
ownAction: ownAction
|
2013-08-08 00:07:41 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-08-09 09:41:48 +00:00
|
|
|
_fireEvent: function ic_fireEvent(eventName, aDetail) {
|
2013-08-08 00:07:41 +00:00
|
|
|
let detail = {
|
|
|
|
detail: aDetail
|
|
|
|
};
|
|
|
|
|
2014-08-25 20:47:00 +00:00
|
|
|
let event = new this._window.CustomEvent(eventName,
|
|
|
|
Cu.cloneInto(detail, this._window));
|
2013-08-09 09:41:48 +00:00
|
|
|
this.__DOM_IMPL__.dispatchEvent(event);
|
2013-08-08 00:07:41 +00: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 09:41:48 +00:00
|
|
|
let self = this;
|
2013-09-23 13:40:59 +00:00
|
|
|
return this._sendPromise(function(resolverId) {
|
2014-08-07 06:44:46 +00:00
|
|
|
cpmmSendAsyncMessageWithKbID(self, 'Keyboard:GetText', {
|
2013-08-09 09:41:48 +00:00
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
offset: offset,
|
|
|
|
length: length
|
|
|
|
});
|
2013-08-08 00:07:41 +00: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 09:41:48 +00:00
|
|
|
let self = this;
|
2013-09-23 13:40:59 +00:00
|
|
|
return this._sendPromise(function(resolverId) {
|
2014-08-07 06:44:46 +00:00
|
|
|
cpmmSendAsyncMessageWithKbID(self, 'Keyboard:SetSelectionRange', {
|
2013-08-09 09:41:48 +00:00
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
selectionStart: start,
|
|
|
|
selectionEnd: start + length
|
|
|
|
});
|
2013-08-08 00:07:41 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
get onsurroundingtextchange() {
|
2013-08-09 09:41:48 +00:00
|
|
|
return this.__DOM_IMPL__.getEventHandler("onsurroundingtextchange");
|
2013-08-08 00:07:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set onsurroundingtextchange(handler) {
|
2013-11-18 06:56:56 +00:00
|
|
|
this.__DOM_IMPL__.setEventHandler("onsurroundingtextchange", handler);
|
2013-08-08 00:07:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get onselectionchange() {
|
2013-08-09 09:41:48 +00:00
|
|
|
return this.__DOM_IMPL__.getEventHandler("onselectionchange");
|
2013-08-08 00:07:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set onselectionchange(handler) {
|
2013-11-18 06:56:56 +00:00
|
|
|
this.__DOM_IMPL__.setEventHandler("onselectionchange", handler);
|
2013-08-08 00:07:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
replaceSurroundingText: function ic_replaceSurrText(text, offset, length) {
|
2013-08-09 09:41:48 +00:00
|
|
|
let self = this;
|
2013-09-23 13:40:59 +00:00
|
|
|
return this._sendPromise(function(resolverId) {
|
2014-08-07 06:44:46 +00:00
|
|
|
cpmmSendAsyncMessageWithKbID(self, 'Keyboard:ReplaceSurroundingText', {
|
2013-08-09 09:41:48 +00:00
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
text: text,
|
2014-01-03 14:17:28 +00:00
|
|
|
offset: offset || 0,
|
|
|
|
length: length || 0
|
2013-08-09 09:41:48 +00:00
|
|
|
});
|
2013-08-08 00:07:41 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteSurroundingText: function ic_deleteSurrText(offset, length) {
|
|
|
|
return this.replaceSurroundingText(null, offset, length);
|
|
|
|
},
|
|
|
|
|
2014-03-25 07:46:52 +00:00
|
|
|
sendKey: function ic_sendKey(keyCode, charCode, modifiers, repeat) {
|
2013-08-09 09:41:48 +00:00
|
|
|
let self = this;
|
2013-09-23 13:40:59 +00:00
|
|
|
return this._sendPromise(function(resolverId) {
|
2014-08-07 06:44:46 +00:00
|
|
|
cpmmSendAsyncMessageWithKbID(self, 'Keyboard:SendKey', {
|
2013-08-09 09:41:48 +00:00
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
keyCode: keyCode,
|
|
|
|
charCode: charCode,
|
2014-03-25 07:46:52 +00:00
|
|
|
modifiers: modifiers,
|
|
|
|
repeat: repeat
|
2013-08-09 09:41:48 +00:00
|
|
|
});
|
2013-08-08 00:07:41 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-30 12:07:16 +00:00
|
|
|
setComposition: function ic_setComposition(text, cursor, clauses) {
|
|
|
|
let self = this;
|
2013-09-23 13:40:59 +00:00
|
|
|
return this._sendPromise(function(resolverId) {
|
2014-08-07 06:44:46 +00:00
|
|
|
cpmmSendAsyncMessageWithKbID(self, 'Keyboard:SetComposition', {
|
2013-08-30 12:07:16 +00:00
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
text: text,
|
2014-06-17 04:05:49 +00:00
|
|
|
cursor: (typeof cursor !== 'undefined') ? cursor : text.length,
|
2013-08-30 12:07:16 +00:00
|
|
|
clauses: clauses || null
|
|
|
|
});
|
|
|
|
});
|
2013-08-08 00:07:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
endComposition: function ic_endComposition(text) {
|
2013-08-30 12:07:16 +00:00
|
|
|
let self = this;
|
2013-09-23 13:40:59 +00:00
|
|
|
return this._sendPromise(function(resolverId) {
|
2014-08-07 06:44:46 +00:00
|
|
|
cpmmSendAsyncMessageWithKbID(self, 'Keyboard:EndComposition', {
|
2013-08-30 12:07:16 +00:00
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
text: text || ''
|
|
|
|
});
|
|
|
|
});
|
2013-09-23 13:40:59 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_sendPromise: function(callback) {
|
|
|
|
let self = this;
|
|
|
|
return this.createPromise(function(resolve, reject) {
|
|
|
|
let resolverId = self.getPromiseResolverId({ resolve: resolve, reject: reject });
|
|
|
|
if (!WindowMap.isActive(self._window)) {
|
|
|
|
self.removePromiseResolver(resolverId);
|
|
|
|
reject('Input method is not active.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
callback(resolverId);
|
|
|
|
});
|
2013-08-01 17:16:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-09 18:03:00 +00:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MozInputMethod]);
|