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");
|
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");
|
|
|
|
|
2013-09-23 06:40:59 -07:00
|
|
|
/*
|
|
|
|
* A WeakMap to map input method iframe window to its active status.
|
|
|
|
*/
|
|
|
|
let WindowMap = {
|
|
|
|
// WeakMap of <window, boolean> pairs.
|
|
|
|
_map: null,
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the given window is active.
|
|
|
|
*/
|
|
|
|
isActive: function(win) {
|
|
|
|
if (!this._map || !win) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this._map.get(win, false);
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the active status of the given window.
|
|
|
|
*/
|
|
|
|
setActive: function(win, isActive) {
|
|
|
|
if (!win) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this._map) {
|
|
|
|
this._map = new WeakMap();
|
|
|
|
}
|
|
|
|
this._map.set(win, isActive);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-01 10:16:43 -07:00
|
|
|
/**
|
|
|
|
* ==============================================
|
|
|
|
* InputMethodManager
|
|
|
|
* ==============================================
|
|
|
|
*/
|
2013-09-23 06:40:59 -07:00
|
|
|
function MozInputMethodManager(win) {
|
|
|
|
this._window = win;
|
|
|
|
}
|
2013-08-01 10:16:43 -07:00
|
|
|
|
|
|
|
MozInputMethodManager.prototype = {
|
2013-09-12 10:02:36 -07:00
|
|
|
_supportsSwitching: false,
|
2013-09-23 06:40:59 -07:00
|
|
|
_window: null,
|
2013-09-12 10:02:36 -07:00
|
|
|
|
2013-08-01 10:16:43 -07:00
|
|
|
classID: Components.ID("{7e9d7280-ef86-11e2-b778-0800200c9a66}"),
|
|
|
|
|
2014-03-24 18:59:33 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([]),
|
2013-08-01 10:16:43 -07:00
|
|
|
|
|
|
|
showAll: function() {
|
2013-09-23 06:40:59 -07:00
|
|
|
if (!WindowMap.isActive(this._window)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-01 10:16:43 -07:00
|
|
|
cpmm.sendAsyncMessage('Keyboard:ShowInputMethodPicker', {});
|
|
|
|
},
|
|
|
|
|
|
|
|
next: function() {
|
2013-09-23 06:40:59 -07:00
|
|
|
if (!WindowMap.isActive(this._window)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-01 10:16:43 -07:00
|
|
|
cpmm.sendAsyncMessage('Keyboard:SwitchToNextInputMethod', {});
|
|
|
|
},
|
|
|
|
|
|
|
|
supportsSwitching: function() {
|
2013-09-23 06:40:59 -07:00
|
|
|
if (!WindowMap.isActive(this._window)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-12 10:02:36 -07:00
|
|
|
return this._supportsSwitching;
|
2013-08-01 10:16:43 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
hide: function() {
|
2013-09-23 06:40:59 -07:00
|
|
|
if (!WindowMap.isActive(this._window)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-01 10:16:43 -07:00
|
|
|
cpmm.sendAsyncMessage('Keyboard:RemoveFocus', {});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ==============================================
|
|
|
|
* InputMethod
|
|
|
|
* ==============================================
|
|
|
|
*/
|
|
|
|
function MozInputMethod() { }
|
|
|
|
|
|
|
|
MozInputMethod.prototype = {
|
2013-08-07 17:07:41 -07:00
|
|
|
_inputcontext: null,
|
2013-09-12 10:02:36 -07:00
|
|
|
_layouts: {},
|
2013-09-23 06:40:59 -07:00
|
|
|
_window: null,
|
2014-03-30 19:38:45 -07:00
|
|
|
_isSystem: false,
|
2013-08-07 17:07:41 -07:00
|
|
|
|
|
|
|
classID: Components.ID("{4607330d-e7d2-40a4-9eb8-43967eae0142}"),
|
2013-08-01 10:16:43 -07:00
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
2013-08-07 17:07:41 -07:00
|
|
|
Ci.nsIDOMGlobalPropertyInitializer,
|
2014-03-30 19:38:45 -07:00
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference
|
2013-08-01 10:16:43 -07:00
|
|
|
]),
|
|
|
|
|
|
|
|
init: function mozInputMethodInit(win) {
|
2013-08-07 17:07:41 -07:00
|
|
|
this._window = win;
|
2013-09-23 06:40:59 -07:00
|
|
|
this._mgmt = new MozInputMethodManager(win);
|
2013-08-07 17:07:41 -07:00
|
|
|
this.innerWindowID = win.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils)
|
|
|
|
.currentInnerWindowID;
|
|
|
|
|
2014-03-30 19:38:45 -07:00
|
|
|
let principal = win.document.nodePrincipal;
|
|
|
|
let perm = Services.perms.testExactPermissionFromPrincipal(principal,
|
|
|
|
"input-manage");
|
|
|
|
if (perm === Ci.nsIPermissionManager.ALLOW_ACTION) {
|
|
|
|
this._isSystem = true;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:07:41 -07:00
|
|
|
Services.obs.addObserver(this, "inner-window-destroyed", false);
|
2014-03-30 19:38:45 -07:00
|
|
|
cpmm.addWeakMessageListener('Keyboard:FocusChange', this);
|
|
|
|
cpmm.addWeakMessageListener('Keyboard:SelectionChange', this);
|
|
|
|
cpmm.addWeakMessageListener('Keyboard:GetContext:Result:OK', this);
|
|
|
|
cpmm.addWeakMessageListener('Keyboard:LayoutsChange', this);
|
2013-08-07 17:07:41 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function mozInputMethodUninit() {
|
2014-02-12 22:29:50 -08:00
|
|
|
this.setActive(false);
|
2013-08-07 17:07:41 -07:00
|
|
|
Services.obs.removeObserver(this, "inner-window-destroyed");
|
2014-03-30 19:38:45 -07:00
|
|
|
cpmm.removeWeakMessageListener('Keyboard:FocusChange', this);
|
|
|
|
cpmm.removeWeakMessageListener('Keyboard:SelectionChange', this);
|
|
|
|
cpmm.removeWeakMessageListener('Keyboard:GetContext:Result:OK', this);
|
|
|
|
cpmm.removeWeakMessageListener('Keyboard:LayoutsChange', this);
|
2013-08-07 17:07:41 -07:00
|
|
|
|
|
|
|
this._window = null;
|
|
|
|
this._mgmt = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function mozInputMethodReceiveMsg(msg) {
|
2013-09-23 06:40:59 -07:00
|
|
|
if (!WindowMap.isActive(this._window)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:07:41 -07:00
|
|
|
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':
|
2013-09-25 11:42:40 -07:00
|
|
|
if (this.inputcontext) {
|
|
|
|
this._inputcontext.updateSelectionContext(json);
|
|
|
|
}
|
2013-08-07 17:07:41 -07:00
|
|
|
break;
|
|
|
|
case 'Keyboard:GetContext:Result:OK':
|
|
|
|
this.setInputContext(json);
|
|
|
|
break;
|
2013-09-12 10:02:36 -07:00
|
|
|
case 'Keyboard:LayoutsChange':
|
|
|
|
this._layouts = json;
|
|
|
|
break;
|
2013-08-07 17:07:41 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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() {
|
2013-09-23 06:40:59 -07:00
|
|
|
if (!WindowMap.isActive(this._window)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return this._inputcontext;
|
2013-08-07 17:07:41 -07:00
|
|
|
},
|
|
|
|
|
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;
|
2013-09-12 10:02:36 -07:00
|
|
|
this._mgmt._supportsSwitching = false;
|
2013-08-07 17:07:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data) {
|
2013-09-12 10:02:36 -07:00
|
|
|
this._mgmt._supportsSwitching = this._layouts[data.type] ?
|
|
|
|
this._layouts[data.type] > 1 :
|
|
|
|
false;
|
|
|
|
|
2013-08-07 17:07:41 -07:00
|
|
|
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",
|
2014-02-01 11:06:59 -08:00
|
|
|
Cu.cloneInto({}, this._window));
|
2013-08-09 02:41:48 -07:00
|
|
|
this.__DOM_IMPL__.dispatchEvent(event);
|
2013-09-23 06:40:59 -07: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-02-12 22:29:50 -08:00
|
|
|
cpmm.sendAsyncMessage('Keyboard:Register', {});
|
2013-09-23 06:40:59 -07:00
|
|
|
cpmm.sendAsyncMessage("Keyboard:GetContext", {});
|
|
|
|
} else {
|
|
|
|
// Deactive current input method.
|
2014-02-12 22:29:50 -08:00
|
|
|
cpmm.sendAsyncMessage('Keyboard:Unregister', {});
|
2013-09-23 06:40:59 -07:00
|
|
|
if (this._inputcontext) {
|
|
|
|
this.setInputContext(null);
|
|
|
|
}
|
|
|
|
}
|
2014-03-30 19:38:45 -07: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.");
|
|
|
|
}
|
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,
|
|
|
|
|
2013-09-23 06:40:59 -07:00
|
|
|
_window: null,
|
2013-08-07 17:07:41 -07:00
|
|
|
_context: null,
|
|
|
|
_contextId: -1,
|
|
|
|
|
|
|
|
classID: Components.ID("{1e38633d-d08b-4867-9944-afa5c648adb6}"),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
2013-10-04 14:59:48 -07:00
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference
|
2013-08-07 17:07:41 -07: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 07:39:26 -08:00
|
|
|
"Keyboard:SendKey:Result:Error",
|
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-09-23 06:40:59 -07:00
|
|
|
|
|
|
|
this._window = null;
|
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;
|
2014-01-02 07:39:26 -08:00
|
|
|
case "Keyboard:SendKey:Result:Error":
|
|
|
|
resolver.reject(json.error);
|
|
|
|
break;
|
2013-08-07 17:07:41 -07:00
|
|
|
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(
|
2014-02-01 11:06:59 -08:00
|
|
|
Cu.cloneInto(json.selectioninfo, this._window));
|
2013-08-07 17:07:41 -07:00
|
|
|
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,
|
2014-02-01 11:06:59 -08:00
|
|
|
Cu.cloneInto(aDetail, this._window));
|
2013-08-09 02:41:48 -07:00
|
|
|
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-23 06:40:59 -07:00
|
|
|
return this._sendPromise(function(resolverId) {
|
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-23 06:40:59 -07:00
|
|
|
return this._sendPromise(function(resolverId) {
|
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-11-17 22:56:56 -08:00
|
|
|
this.__DOM_IMPL__.setEventHandler("onsurroundingtextchange", handler);
|
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-11-17 22:56:56 -08:00
|
|
|
this.__DOM_IMPL__.setEventHandler("onselectionchange", handler);
|
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-23 06:40:59 -07:00
|
|
|
return this._sendPromise(function(resolverId) {
|
2013-08-09 02:41:48 -07:00
|
|
|
cpmm.sendAsyncMessage('Keyboard:ReplaceSurroundingText', {
|
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
text: text,
|
2014-01-03 06:17:28 -08:00
|
|
|
offset: offset || 0,
|
|
|
|
length: length || 0
|
2013-08-09 02:41:48 -07:00
|
|
|
});
|
2013-08-07 17:07:41 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteSurroundingText: function ic_deleteSurrText(offset, length) {
|
|
|
|
return this.replaceSurroundingText(null, offset, length);
|
|
|
|
},
|
|
|
|
|
2014-03-25 00:46:52 -07:00
|
|
|
sendKey: function ic_sendKey(keyCode, charCode, modifiers, repeat) {
|
2013-08-09 02:41:48 -07:00
|
|
|
let self = this;
|
2013-09-23 06:40:59 -07:00
|
|
|
return this._sendPromise(function(resolverId) {
|
2013-08-09 02:41:48 -07:00
|
|
|
cpmm.sendAsyncMessage('Keyboard:SendKey', {
|
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
keyCode: keyCode,
|
|
|
|
charCode: charCode,
|
2014-03-25 00:46:52 -07:00
|
|
|
modifiers: modifiers,
|
|
|
|
repeat: repeat
|
2013-08-09 02:41:48 -07:00
|
|
|
});
|
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-23 06:40:59 -07:00
|
|
|
return this._sendPromise(function(resolverId) {
|
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-23 06:40:59 -07:00
|
|
|
return this._sendPromise(function(resolverId) {
|
2013-08-30 05:07:16 -07:00
|
|
|
cpmm.sendAsyncMessage('Keyboard:EndComposition', {
|
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
text: text || ''
|
|
|
|
});
|
|
|
|
});
|
2013-09-23 06:40:59 -07: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 10:16:43 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-30 19:41:08 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MozInputMethod]);
|