2012-03-19 21:30:51 +04: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 16:19:33 +04:00
|
|
|
"use strict";
|
2012-03-19 21:30:51 +04:00
|
|
|
|
2013-08-09 13:41:48 +04:00
|
|
|
const Cc = Components.classes;
|
2012-03-19 21:30:51 +04:00
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
2015-09-16 23:11:00 +03:00
|
|
|
const Cr = Components.results;
|
2012-03-19 21:30:51 +04:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2013-08-08 04:07:41 +04:00
|
|
|
Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
|
2012-03-19 21:30:51 +04:00
|
|
|
|
2012-11-07 15:53:24 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
2014-08-07 10:44:46 +04:00
|
|
|
"@mozilla.org/childprocessmessagemanager;1", "nsISyncMessageSender");
|
2012-11-07 15:53:24 +04:00
|
|
|
|
2013-04-07 13:38:40 +04:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "tm",
|
|
|
|
"@mozilla.org/thread-manager;1", "nsIThreadManager");
|
|
|
|
|
2013-09-23 17:40:59 +04:00
|
|
|
/*
|
2015-07-03 00:47:00 +03:00
|
|
|
* A WeakMap to map input method iframe window to
|
|
|
|
* it's active status, kbID, and ipcHelper.
|
2013-09-23 17:40:59 +04:00
|
|
|
*/
|
2015-09-15 21:19:45 +03:00
|
|
|
var WindowMap = {
|
2014-08-07 10:44:46 +04:00
|
|
|
// WeakMap of <window, object> pairs.
|
2013-09-23 17:40:59 +04:00
|
|
|
_map: null,
|
|
|
|
|
2015-07-03 00:47:00 +03:00
|
|
|
/*
|
|
|
|
* Set the object associated to the window and return it.
|
|
|
|
*/
|
|
|
|
_getObjForWin: function(win) {
|
|
|
|
if (!this._map) {
|
|
|
|
this._map = new WeakMap();
|
|
|
|
}
|
|
|
|
if (this._map.has(win)) {
|
|
|
|
return this._map.get(win);
|
|
|
|
} else {
|
|
|
|
let obj = {
|
|
|
|
active: false,
|
|
|
|
kbID: undefined,
|
|
|
|
ipcHelper: null
|
|
|
|
};
|
|
|
|
this._map.set(win, obj);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-09-23 17:40:59 +04:00
|
|
|
/*
|
|
|
|
* Check if the given window is active.
|
|
|
|
*/
|
|
|
|
isActive: function(win) {
|
|
|
|
if (!this._map || !win) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-07 10:44:46 +04:00
|
|
|
|
2015-07-03 00:47:00 +03:00
|
|
|
return this._getObjForWin(win).active;
|
2013-09-23 17:40:59 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the active status of the given window.
|
|
|
|
*/
|
|
|
|
setActive: function(win, isActive) {
|
|
|
|
if (!win) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-03 00:47:00 +03:00
|
|
|
let obj = this._getObjForWin(win);
|
|
|
|
obj.active = isActive;
|
2014-08-07 10:44:46 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
2015-07-03 00:47:00 +03:00
|
|
|
* Get the keyboard ID (assigned by Keyboard.jsm) of the given window.
|
2014-08-07 10:44:46 +04:00
|
|
|
*/
|
|
|
|
getKbID: function(win) {
|
|
|
|
if (!this._map || !win) {
|
2015-07-03 00:47:00 +03:00
|
|
|
return undefined;
|
2014-08-07 10:44:46 +04:00
|
|
|
}
|
|
|
|
|
2015-07-03 00:47:00 +03:00
|
|
|
let obj = this._getObjForWin(win);
|
|
|
|
return obj.kbID;
|
2014-08-07 10:44:46 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
2015-07-03 00:47:00 +03:00
|
|
|
* Set the keyboard ID (assigned by Keyboard.jsm) of the given window.
|
2014-08-07 10:44:46 +04:00
|
|
|
*/
|
|
|
|
setKbID: function(win, kbID) {
|
|
|
|
if (!win) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-03 00:47:00 +03:00
|
|
|
let obj = this._getObjForWin(win);
|
|
|
|
obj.kbID = kbID;
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get InputContextDOMRequestIpcHelper instance attached to this window.
|
|
|
|
*/
|
|
|
|
getInputContextIpcHelper: function(win) {
|
|
|
|
if (!win) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let obj = this._getObjForWin(win);
|
|
|
|
if (!obj.ipcHelper) {
|
|
|
|
obj.ipcHelper = new InputContextDOMRequestIpcHelper(win);
|
2014-08-07 10:44:46 +04:00
|
|
|
}
|
2015-07-03 00:47:00 +03:00
|
|
|
return obj.ipcHelper;
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unset InputContextDOMRequestIpcHelper instance.
|
|
|
|
*/
|
|
|
|
unsetInputContextIpcHelper: function(win) {
|
|
|
|
if (!win) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let obj = this._getObjForWin(win);
|
|
|
|
if (!obj.ipcHelper) {
|
|
|
|
return;
|
2014-08-07 10:44:46 +04:00
|
|
|
}
|
2015-07-03 00:47:00 +03:00
|
|
|
obj.ipcHelper = null;
|
2013-09-23 17:40:59 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var cpmmSendAsyncMessageWithKbID = function (self, msg, data) {
|
2014-08-07 10:44:46 +04:00
|
|
|
data.kbID = WindowMap.getKbID(self._window);
|
|
|
|
cpmm.sendAsyncMessage(msg, data);
|
|
|
|
};
|
|
|
|
|
2013-08-01 21:16:43 +04:00
|
|
|
/**
|
|
|
|
* ==============================================
|
|
|
|
* InputMethodManager
|
|
|
|
* ==============================================
|
|
|
|
*/
|
2013-09-23 17:40:59 +04:00
|
|
|
function MozInputMethodManager(win) {
|
|
|
|
this._window = win;
|
|
|
|
}
|
2013-08-01 21:16:43 +04:00
|
|
|
|
|
|
|
MozInputMethodManager.prototype = {
|
2015-09-10 23:29:00 +03:00
|
|
|
supportsSwitchingForCurrentInputContext: false,
|
2013-09-23 17:40:59 +04:00
|
|
|
_window: null,
|
2013-09-12 21:02:36 +04:00
|
|
|
|
2013-08-01 21:16:43 +04:00
|
|
|
classID: Components.ID("{7e9d7280-ef86-11e2-b778-0800200c9a66}"),
|
|
|
|
|
2014-03-25 05:59:33 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([]),
|
2013-08-01 21:16:43 +04:00
|
|
|
|
2015-09-16 23:11:00 +03:00
|
|
|
set oninputcontextfocus(handler) {
|
|
|
|
this.__DOM_IMPL__.setEventHandler("oninputcontextfocus", handler);
|
|
|
|
},
|
|
|
|
|
|
|
|
get oninputcontextfocus() {
|
|
|
|
return this.__DOM_IMPL__.getEventHandler("oninputcontextfocus");
|
|
|
|
},
|
|
|
|
|
|
|
|
set oninputcontextblur(handler) {
|
|
|
|
this.__DOM_IMPL__.setEventHandler("oninputcontextblur", handler);
|
|
|
|
},
|
|
|
|
|
|
|
|
get oninputcontextblur() {
|
|
|
|
return this.__DOM_IMPL__.getEventHandler("oninputcontextblur");
|
|
|
|
},
|
|
|
|
|
|
|
|
set onshowallrequest(handler) {
|
|
|
|
this.__DOM_IMPL__.setEventHandler("onshowallrequest", handler);
|
|
|
|
},
|
|
|
|
|
|
|
|
get onshowallrequest() {
|
|
|
|
return this.__DOM_IMPL__.getEventHandler("onshowallrequest");
|
|
|
|
},
|
|
|
|
|
|
|
|
set onnextrequest(handler) {
|
|
|
|
this.__DOM_IMPL__.setEventHandler("onnextrequest", handler);
|
|
|
|
},
|
|
|
|
|
|
|
|
get onnextrequest() {
|
|
|
|
return this.__DOM_IMPL__.getEventHandler("onnextrequest");
|
|
|
|
},
|
|
|
|
|
|
|
|
set onaddinputrequest(handler) {
|
|
|
|
this.__DOM_IMPL__.setEventHandler("onaddinputrequest", handler);
|
|
|
|
},
|
|
|
|
|
|
|
|
get onaddinputrequest() {
|
|
|
|
return this.__DOM_IMPL__.getEventHandler("onaddinputrequest");
|
|
|
|
},
|
|
|
|
|
|
|
|
set onremoveinputrequest(handler) {
|
|
|
|
this.__DOM_IMPL__.setEventHandler("onremoveinputrequest", handler);
|
|
|
|
},
|
|
|
|
|
|
|
|
get onremoveinputrequest() {
|
|
|
|
return this.__DOM_IMPL__.getEventHandler("onremoveinputrequest");
|
|
|
|
},
|
|
|
|
|
2013-08-01 21:16:43 +04:00
|
|
|
showAll: function() {
|
2013-09-23 17:40:59 +04:00
|
|
|
if (!WindowMap.isActive(this._window)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-07 10:44:46 +04:00
|
|
|
cpmmSendAsyncMessageWithKbID(this, 'Keyboard:ShowInputMethodPicker', {});
|
2013-08-01 21:16:43 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
next: function() {
|
2013-09-23 17:40:59 +04:00
|
|
|
if (!WindowMap.isActive(this._window)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-07 10:44:46 +04:00
|
|
|
cpmmSendAsyncMessageWithKbID(this, 'Keyboard:SwitchToNextInputMethod', {});
|
2013-08-01 21:16:43 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
supportsSwitching: function() {
|
2013-09-23 17:40:59 +04:00
|
|
|
if (!WindowMap.isActive(this._window)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-10 23:29:00 +03:00
|
|
|
return this.supportsSwitchingForCurrentInputContext;
|
2013-08-01 21:16:43 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
hide: function() {
|
2013-09-23 17:40:59 +04:00
|
|
|
if (!WindowMap.isActive(this._window)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-07 10:44:46 +04:00
|
|
|
cpmmSendAsyncMessageWithKbID(this, 'Keyboard:RemoveFocus', {});
|
2015-09-10 23:29:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
setSupportsSwitchingTypes: function(types) {
|
|
|
|
cpmm.sendAsyncMessage('System:SetSupportsSwitchingTypes', {
|
|
|
|
types: types
|
|
|
|
});
|
2015-09-16 23:11:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
handleFocus: function(data) {
|
|
|
|
let detail = new MozInputContextFocusEventDetail(this._window, data);
|
|
|
|
let wrappedDetail =
|
|
|
|
this._window.MozInputContextFocusEventDetail._create(this._window, detail);
|
|
|
|
let event = new this._window.CustomEvent('inputcontextfocus',
|
|
|
|
{ cancelable: true, detail: wrappedDetail });
|
|
|
|
|
|
|
|
let handled = !this.__DOM_IMPL__.dispatchEvent(event);
|
|
|
|
|
|
|
|
// A gentle warning if the event is not preventDefault() by the content.
|
|
|
|
if (!handled) {
|
|
|
|
dump('MozKeyboard.js: A frame with input-manage permission did not' +
|
|
|
|
' handle the inputcontextfocus event dispatched.\n');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleBlur: function(data) {
|
|
|
|
let event =
|
|
|
|
new this._window.Event('inputcontextblur', { cancelable: true });
|
|
|
|
|
|
|
|
let handled = !this.__DOM_IMPL__.dispatchEvent(event);
|
|
|
|
|
|
|
|
// A gentle warning if the event is not preventDefault() by the content.
|
|
|
|
if (!handled) {
|
|
|
|
dump('MozKeyboard.js: A frame with input-manage permission did not' +
|
|
|
|
' handle the inputcontextblur event dispatched.\n');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
dispatchShowAllRequestEvent: function() {
|
|
|
|
this._fireSimpleEvent('showallrequest');
|
|
|
|
},
|
|
|
|
|
|
|
|
dispatchNextRequestEvent: function() {
|
|
|
|
this._fireSimpleEvent('nextrequest');
|
|
|
|
},
|
|
|
|
|
|
|
|
_fireSimpleEvent: function(eventType) {
|
|
|
|
let event = new this._window.Event(eventType);
|
|
|
|
let handled = !this.__DOM_IMPL__.dispatchEvent(event, { cancelable: true });
|
|
|
|
|
|
|
|
// A gentle warning if the event is not preventDefault() by the content.
|
|
|
|
if (!handled) {
|
|
|
|
dump('MozKeyboard.js: A frame with input-manage permission did not' +
|
|
|
|
' handle the ' + eventType + ' event dispatched.\n');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleAddInput: function(data) {
|
|
|
|
let p = this._fireInputRegistryEvent('addinputrequest', data);
|
|
|
|
if (!p) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.then(() => {
|
|
|
|
cpmm.sendAsyncMessage('System:InputRegistry:Add:Done', {
|
|
|
|
id: data.id
|
|
|
|
});
|
|
|
|
}, (error) => {
|
|
|
|
cpmm.sendAsyncMessage('System:InputRegistry:Add:Done', {
|
|
|
|
id: data.id,
|
|
|
|
error: error || 'Unknown Error'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
handleRemoveInput: function(data) {
|
|
|
|
let p = this._fireInputRegistryEvent('removeinputrequest', data);
|
|
|
|
if (!p) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.then(() => {
|
|
|
|
cpmm.sendAsyncMessage('System:InputRegistry:Remove:Done', {
|
|
|
|
id: data.id
|
|
|
|
});
|
|
|
|
}, (error) => {
|
|
|
|
cpmm.sendAsyncMessage('System:InputRegistry:Remove:Done', {
|
|
|
|
id: data.id,
|
|
|
|
error: error || 'Unknown Error'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_fireInputRegistryEvent: function(eventType, data) {
|
|
|
|
let detail = new MozInputRegistryEventDetail(this._window, data);
|
|
|
|
let wrappedDetail =
|
|
|
|
this._window.MozInputRegistryEventDetail._create(this._window, detail);
|
|
|
|
let event = new this._window.CustomEvent(eventType,
|
|
|
|
{ cancelable: true, detail: wrappedDetail });
|
|
|
|
let handled = !this.__DOM_IMPL__.dispatchEvent(event);
|
|
|
|
|
|
|
|
// A gentle warning if the event is not preventDefault() by the content.
|
|
|
|
if (!handled) {
|
|
|
|
dump('MozKeyboard.js: A frame with input-manage permission did not' +
|
|
|
|
' handle the ' + eventType + ' event dispatched.\n');
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return detail.takeChainedPromise();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function MozInputContextFocusEventDetail(win, data) {
|
|
|
|
this.type = data.type;
|
|
|
|
this.inputType = data.inputType;
|
|
|
|
this.value = data.value;
|
|
|
|
// Exposed as MozInputContextChoicesInfo dictionary defined in WebIDL
|
|
|
|
this.choices = data.choices;
|
|
|
|
this.min = data.min;
|
|
|
|
this.max = data.max;
|
|
|
|
}
|
|
|
|
MozInputContextFocusEventDetail.prototype = {
|
|
|
|
classID: Components.ID("{e0794208-ac50-40e8-b22e-6ee0b4c4e6e8}"),
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([]),
|
|
|
|
|
|
|
|
type: undefined,
|
|
|
|
inputType: undefined,
|
|
|
|
value: '',
|
|
|
|
choices: null,
|
|
|
|
min: undefined,
|
|
|
|
max: undefined
|
|
|
|
};
|
|
|
|
|
|
|
|
function MozInputRegistryEventDetail(win, data) {
|
|
|
|
this._window = win;
|
|
|
|
|
|
|
|
this.manifestURL = data.manifestURL;
|
|
|
|
this.inputId = data.inputId;
|
|
|
|
// Exposed as MozInputMethodInputManifest dictionary defined in WebIDL
|
|
|
|
this.inputManifest = data.inputManifest;
|
|
|
|
|
|
|
|
this._chainedPromise = Promise.resolve();
|
|
|
|
}
|
|
|
|
MozInputRegistryEventDetail.prototype = {
|
|
|
|
classID: Components.ID("{02130070-9b3e-4f38-bbd9-f0013aa36717}"),
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([]),
|
|
|
|
|
|
|
|
_window: null,
|
|
|
|
|
|
|
|
manifestURL: undefined,
|
|
|
|
inputId: undefined,
|
|
|
|
inputManifest: null,
|
|
|
|
|
|
|
|
waitUntil: function(p) {
|
|
|
|
// Need an extra protection here since waitUntil will be an no-op
|
|
|
|
// when chainedPromise is already returned.
|
|
|
|
if (!this._chainedPromise) {
|
|
|
|
throw new this._window.DOMException(
|
|
|
|
'Must call waitUntil() within the event handling loop.',
|
|
|
|
'InvalidStateError');
|
|
|
|
}
|
|
|
|
|
|
|
|
this._chainedPromise = this._chainedPromise
|
|
|
|
.then(function() { return p; });
|
|
|
|
},
|
|
|
|
|
|
|
|
takeChainedPromise: function() {
|
|
|
|
var p = this._chainedPromise;
|
|
|
|
this._chainedPromise = null;
|
|
|
|
return p;
|
2013-08-01 21:16:43 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ==============================================
|
|
|
|
* InputMethod
|
|
|
|
* ==============================================
|
|
|
|
*/
|
|
|
|
function MozInputMethod() { }
|
|
|
|
|
|
|
|
MozInputMethod.prototype = {
|
2014-12-09 22:20:40 +03:00
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
|
|
|
|
2015-09-16 23:11:00 +03:00
|
|
|
_window: null,
|
2013-08-08 04:07:41 +04:00
|
|
|
_inputcontext: null,
|
2014-07-03 14:10:13 +04:00
|
|
|
_wrappedInputContext: null,
|
2015-09-16 23:11:00 +03:00
|
|
|
_mgmt: null,
|
|
|
|
_wrappedMgmt: null,
|
2015-09-10 23:29:00 +03:00
|
|
|
_supportsSwitchingTypes: [],
|
2015-09-16 23:11:00 +03:00
|
|
|
_inputManageId: undefined,
|
2013-08-08 04:07:41 +04:00
|
|
|
|
|
|
|
classID: Components.ID("{4607330d-e7d2-40a4-9eb8-43967eae0142}"),
|
2013-08-01 21:16:43 +04:00
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
2013-08-08 04:07:41 +04:00
|
|
|
Ci.nsIDOMGlobalPropertyInitializer,
|
2014-03-31 06:38:45 +04:00
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference
|
2013-08-01 21:16:43 +04:00
|
|
|
]),
|
|
|
|
|
|
|
|
init: function mozInputMethodInit(win) {
|
2013-08-08 04:07:41 +04:00
|
|
|
this._window = win;
|
2013-09-23 17:40:59 +04:00
|
|
|
this._mgmt = new MozInputMethodManager(win);
|
2015-09-16 23:11:00 +03:00
|
|
|
this._wrappedMgmt = win.MozInputMethodManager._create(win, this._mgmt);
|
2013-08-08 04:07:41 +04:00
|
|
|
this.innerWindowID = win.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIDOMWindowUtils)
|
|
|
|
.currentInnerWindowID;
|
|
|
|
|
2014-04-09 22:03:00 +04:00
|
|
|
Services.obs.addObserver(this, "inner-window-destroyed", false);
|
|
|
|
|
2015-09-10 23:27:00 +03:00
|
|
|
cpmm.addWeakMessageListener('Keyboard:Focus', this);
|
|
|
|
cpmm.addWeakMessageListener('Keyboard:Blur', this);
|
2014-03-31 06:38:45 +04:00
|
|
|
cpmm.addWeakMessageListener('Keyboard:SelectionChange', this);
|
|
|
|
cpmm.addWeakMessageListener('Keyboard:GetContext:Result:OK', this);
|
2015-09-10 23:29:00 +03:00
|
|
|
cpmm.addWeakMessageListener('Keyboard:SupportsSwitchingTypesChange', this);
|
2014-12-09 22:20:40 +03:00
|
|
|
cpmm.addWeakMessageListener('InputRegistry:Result:OK', this);
|
|
|
|
cpmm.addWeakMessageListener('InputRegistry:Result:Error', this);
|
2015-09-16 23:11:00 +03:00
|
|
|
|
|
|
|
if (this._hasInputManagePerm(win)) {
|
|
|
|
this._inputManageId = cpmm.sendSyncMessage('System:RegisterSync', {})[0];
|
|
|
|
cpmm.addWeakMessageListener('System:Focus', this);
|
|
|
|
cpmm.addWeakMessageListener('System:Blur', this);
|
|
|
|
cpmm.addWeakMessageListener('System:ShowAll', this);
|
|
|
|
cpmm.addWeakMessageListener('System:Next', this);
|
|
|
|
cpmm.addWeakMessageListener('System:InputRegistry:Add', this);
|
|
|
|
cpmm.addWeakMessageListener('System:InputRegistry:Remove', this);
|
|
|
|
}
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function mozInputMethodUninit() {
|
2014-04-09 22:03:00 +04:00
|
|
|
this._window = null;
|
|
|
|
this._mgmt = null;
|
2015-09-16 23:11:00 +03:00
|
|
|
this._wrappedMgmt = null;
|
2014-04-09 22:03:00 +04:00
|
|
|
|
2015-09-10 23:27:00 +03:00
|
|
|
cpmm.removeWeakMessageListener('Keyboard:Focus', this);
|
|
|
|
cpmm.removeWeakMessageListener('Keyboard:Blur', this);
|
2014-03-31 06:38:45 +04:00
|
|
|
cpmm.removeWeakMessageListener('Keyboard:SelectionChange', this);
|
|
|
|
cpmm.removeWeakMessageListener('Keyboard:GetContext:Result:OK', this);
|
2015-09-10 23:29:00 +03:00
|
|
|
cpmm.removeWeakMessageListener('Keyboard:SupportsSwitchingTypesChange', this);
|
2014-12-09 22:20:40 +03:00
|
|
|
cpmm.removeWeakMessageListener('InputRegistry:Result:OK', this);
|
|
|
|
cpmm.removeWeakMessageListener('InputRegistry:Result:Error', this);
|
2014-04-09 22:03:00 +04:00
|
|
|
this.setActive(false);
|
2015-09-16 23:11:00 +03:00
|
|
|
|
|
|
|
if (typeof this._inputManageId === 'number') {
|
|
|
|
cpmm.sendAsyncMessage('System:Unregister', {
|
|
|
|
'id': this._inputManageId
|
|
|
|
});
|
|
|
|
cpmm.removeWeakMessageListener('System:Focus', this);
|
|
|
|
cpmm.removeWeakMessageListener('System:Blur', this);
|
|
|
|
cpmm.removeWeakMessageListener('System:ShowAll', this);
|
|
|
|
cpmm.removeWeakMessageListener('System:Next', this);
|
|
|
|
cpmm.removeWeakMessageListener('System:InputRegistry:Add', this);
|
|
|
|
cpmm.removeWeakMessageListener('System:InputRegistry:Remove', this);
|
|
|
|
}
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function mozInputMethodReceiveMsg(msg) {
|
2015-09-16 23:11:00 +03:00
|
|
|
if (msg.name.startsWith('Keyboard') &&
|
2014-12-09 22:20:40 +03:00
|
|
|
!WindowMap.isActive(this._window)) {
|
2013-09-23 17:40:59 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-09 22:20:40 +03:00
|
|
|
let data = msg.data;
|
2015-09-16 23:11:00 +03:00
|
|
|
|
|
|
|
if (msg.name.startsWith('System') &&
|
|
|
|
this._inputManageId !== data.inputManageId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delete data.inputManageId;
|
|
|
|
|
2014-12-09 22:20:40 +03:00
|
|
|
let resolver = ('requestId' in data) ?
|
|
|
|
this.takePromiseResolver(data.requestId) : null;
|
2013-08-08 04:07:41 +04:00
|
|
|
|
|
|
|
switch(msg.name) {
|
2015-09-10 23:27:00 +03:00
|
|
|
case 'Keyboard:Focus':
|
|
|
|
// XXX Bug 904339 could receive 'text' event twice
|
|
|
|
this.setInputContext(data);
|
|
|
|
break;
|
|
|
|
case 'Keyboard:Blur':
|
|
|
|
this.setInputContext(null);
|
2013-08-08 04:07:41 +04:00
|
|
|
break;
|
|
|
|
case 'Keyboard:SelectionChange':
|
2013-09-25 22:42:40 +04:00
|
|
|
if (this.inputcontext) {
|
2014-12-09 22:20:40 +03:00
|
|
|
this._inputcontext.updateSelectionContext(data, false);
|
2013-09-25 22:42:40 +04:00
|
|
|
}
|
2013-08-08 04:07:41 +04:00
|
|
|
break;
|
|
|
|
case 'Keyboard:GetContext:Result:OK':
|
2014-12-09 22:20:40 +03:00
|
|
|
this.setInputContext(data);
|
2013-08-08 04:07:41 +04:00
|
|
|
break;
|
2015-09-10 23:29:00 +03:00
|
|
|
case 'Keyboard:SupportsSwitchingTypesChange':
|
|
|
|
this._supportsSwitchingTypes = data.types;
|
2014-12-09 22:20:40 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'InputRegistry:Result:OK':
|
|
|
|
resolver.resolve();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'InputRegistry:Result:Error':
|
|
|
|
resolver.reject(data.error);
|
|
|
|
|
2013-09-12 21:02:36 +04:00
|
|
|
break;
|
2015-09-16 23:11:00 +03:00
|
|
|
|
|
|
|
case 'System:Focus':
|
|
|
|
this._mgmt.handleFocus(data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'System:Blur':
|
|
|
|
this._mgmt.handleBlur(data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'System:ShowAll':
|
|
|
|
this._mgmt.dispatchShowAllRequestEvent();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'System:Next':
|
|
|
|
this._mgmt.dispatchNextRequestEvent();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'System:InputRegistry:Add':
|
|
|
|
this._mgmt.handleAddInput(data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'System:InputRegistry:Remove':
|
|
|
|
this._mgmt.handleRemoveInput(data);
|
|
|
|
break;
|
2013-08-08 04:07:41 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function mozInputMethodObserve(subject, topic, data) {
|
|
|
|
let wId = subject.QueryInterface(Ci.nsISupportsPRUint64).data;
|
|
|
|
if (wId == this.innerWindowID)
|
|
|
|
this.uninit();
|
2013-08-01 21:16:43 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
get mgmt() {
|
2015-09-16 23:11:00 +03:00
|
|
|
return this._wrappedMgmt;
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
get inputcontext() {
|
2013-09-23 17:40:59 +04:00
|
|
|
if (!WindowMap.isActive(this._window)) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-07-03 14:10:13 +04:00
|
|
|
return this._wrappedInputContext;
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
2013-08-09 13:41:48 +04:00
|
|
|
set oninputcontextchange(handler) {
|
|
|
|
this.__DOM_IMPL__.setEventHandler("oninputcontextchange", handler);
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
get oninputcontextchange() {
|
2013-08-09 13:41:48 +04:00
|
|
|
return this.__DOM_IMPL__.getEventHandler("oninputcontextchange");
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
setInputContext: function mozKeyboardContextChange(data) {
|
|
|
|
if (this._inputcontext) {
|
|
|
|
this._inputcontext.destroy();
|
|
|
|
this._inputcontext = null;
|
2014-07-03 14:10:13 +04:00
|
|
|
this._wrappedInputContext = null;
|
2015-09-10 23:29:00 +03:00
|
|
|
this._mgmt.supportsSwitchingForCurrentInputContext = false;
|
2013-08-08 04:07:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data) {
|
2015-09-10 23:29:00 +03:00
|
|
|
this._mgmt.supportsSwitchingForCurrentInputContext =
|
|
|
|
(this._supportsSwitchingTypes.indexOf(data.inputType) !== -1);
|
2013-09-12 21:02:36 +04:00
|
|
|
|
2013-08-08 04:07:41 +04:00
|
|
|
this._inputcontext = new MozInputContext(data);
|
|
|
|
this._inputcontext.init(this._window);
|
2014-07-03 14:10:13 +04: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 04:07:41 +04:00
|
|
|
}
|
|
|
|
|
2015-09-16 23:11:00 +03:00
|
|
|
let event = new this._window.Event("inputcontextchange");
|
2013-08-09 13:41:48 +04:00
|
|
|
this.__DOM_IMPL__.dispatchEvent(event);
|
2013-09-23 17:40:59 +04: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 10:44:46 +04: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);
|
2015-07-03 00:47:00 +03:00
|
|
|
if (kbID) {
|
2015-09-16 23:11:00 +03:00
|
|
|
cpmmSendAsyncMessageWithKbID(this, 'Keyboard:RegisterSync', {});
|
2015-07-03 00:47:00 +03:00
|
|
|
} else {
|
2015-09-16 23:11:00 +03:00
|
|
|
let res = cpmm.sendSyncMessage('Keyboard:RegisterSync', {});
|
2014-08-07 10:44:46 +04:00
|
|
|
WindowMap.setKbID(this._window, res[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
cpmmSendAsyncMessageWithKbID(this, 'Keyboard:GetContext', {});
|
2013-09-23 17:40:59 +04:00
|
|
|
} else {
|
|
|
|
// Deactive current input method.
|
2014-08-07 10:44:46 +04:00
|
|
|
cpmmSendAsyncMessageWithKbID(this, 'Keyboard:Unregister', {});
|
2013-09-23 17:40:59 +04:00
|
|
|
if (this._inputcontext) {
|
|
|
|
this.setInputContext(null);
|
|
|
|
}
|
|
|
|
}
|
2014-03-31 06:38:45 +04:00
|
|
|
},
|
|
|
|
|
2014-12-09 22:20:40 +03:00
|
|
|
addInput: function(inputId, inputManifest) {
|
2015-08-24 04:28:00 +03:00
|
|
|
return this.createPromiseWithId(function(resolverId) {
|
2014-12-09 22:20:40 +03:00
|
|
|
let appId = this._window.document.nodePrincipal.appId;
|
|
|
|
|
|
|
|
cpmm.sendAsyncMessage('InputRegistry:Add', {
|
|
|
|
requestId: resolverId,
|
|
|
|
inputId: inputId,
|
|
|
|
inputManifest: inputManifest,
|
|
|
|
appId: appId
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
removeInput: function(inputId) {
|
2015-08-20 00:53:22 +03:00
|
|
|
return this.createPromiseWithId(function(resolverId) {
|
2014-12-09 22:20:40 +03:00
|
|
|
let appId = this._window.document.nodePrincipal.appId;
|
|
|
|
|
|
|
|
cpmm.sendAsyncMessage('InputRegistry:Remove', {
|
|
|
|
requestId: resolverId,
|
|
|
|
inputId: inputId,
|
|
|
|
appId: appId
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2014-03-31 06:38:45 +04:00
|
|
|
setValue: function(value) {
|
|
|
|
cpmm.sendAsyncMessage('System:SetValue', {
|
|
|
|
'value': value
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
setSelectedOption: function(index) {
|
|
|
|
cpmm.sendAsyncMessage('System:SetSelectedOption', {
|
|
|
|
'index': index
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
setSelectedOptions: function(indexes) {
|
|
|
|
cpmm.sendAsyncMessage('System:SetSelectedOptions', {
|
|
|
|
'indexes': indexes
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
removeFocus: function() {
|
|
|
|
cpmm.sendAsyncMessage('System:RemoveFocus', {});
|
2015-09-16 23:11:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_hasInputManagePerm: function(win) {
|
|
|
|
let principal = win.document.nodePrincipal;
|
|
|
|
let perm = Services.perms.testExactPermissionFromPrincipal(principal,
|
|
|
|
"input-manage");
|
|
|
|
return (perm === Ci.nsIPermissionManager.ALLOW_ACTION);
|
2013-08-08 04:07:41 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-03 00:47:00 +03:00
|
|
|
/**
|
|
|
|
* ==============================================
|
|
|
|
* InputContextDOMRequestIpcHelper
|
|
|
|
* ==============================================
|
|
|
|
*/
|
|
|
|
function InputContextDOMRequestIpcHelper(win) {
|
|
|
|
this.initDOMRequestHelper(win,
|
|
|
|
["Keyboard:GetText:Result:OK",
|
|
|
|
"Keyboard:GetText:Result:Error",
|
|
|
|
"Keyboard:SetSelectionRange:Result:OK",
|
|
|
|
"Keyboard:ReplaceSurroundingText:Result:OK",
|
|
|
|
"Keyboard:SendKey:Result:OK",
|
|
|
|
"Keyboard:SendKey:Result:Error",
|
|
|
|
"Keyboard:SetComposition:Result:OK",
|
|
|
|
"Keyboard:EndComposition:Result:OK",
|
|
|
|
"Keyboard:SequenceError"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
InputContextDOMRequestIpcHelper.prototype = {
|
|
|
|
__proto__: DOMRequestIpcHelper.prototype,
|
|
|
|
_inputContext: null,
|
|
|
|
|
|
|
|
attachInputContext: function(inputCtx) {
|
|
|
|
if (this._inputContext) {
|
|
|
|
throw new Error("InputContextDOMRequestIpcHelper: detach the context first.");
|
|
|
|
}
|
|
|
|
|
|
|
|
this._inputContext = inputCtx;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Unset ourselves when the window is destroyed.
|
|
|
|
uninit: function() {
|
|
|
|
WindowMap.unsetInputContextIpcHelper(this._window);
|
|
|
|
},
|
|
|
|
|
|
|
|
detachInputContext: function() {
|
|
|
|
// All requests that are still pending need to be invalidated
|
|
|
|
// because the context is no longer valid.
|
|
|
|
this.forEachPromiseResolver(k => {
|
|
|
|
this.takePromiseResolver(k).reject("InputContext got destroyed");
|
|
|
|
});
|
|
|
|
|
|
|
|
this._inputContext = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(msg) {
|
|
|
|
if (!this._inputContext) {
|
|
|
|
dump('InputContextDOMRequestIpcHelper received message without context attached.\n');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._inputContext.receiveMessage(msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-01-05 02:37:00 +03:00
|
|
|
function MozInputContextSelectionChangeEventDetail(ctx, ownAction) {
|
|
|
|
this._ctx = ctx;
|
|
|
|
this.ownAction = ownAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
MozInputContextSelectionChangeEventDetail.prototype = {
|
|
|
|
classID: Components.ID("ef35443e-a400-4ae3-9170-c2f4e05f7aed"),
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([]),
|
|
|
|
|
|
|
|
ownAction: false,
|
|
|
|
|
|
|
|
get selectionStart() {
|
|
|
|
return this._ctx.selectionStart;
|
|
|
|
},
|
|
|
|
|
|
|
|
get selectionEnd() {
|
|
|
|
return this._ctx.selectionEnd;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function MozInputContextSurroundingTextChangeEventDetail(ctx, ownAction) {
|
|
|
|
this._ctx = ctx;
|
|
|
|
this.ownAction = ownAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
MozInputContextSurroundingTextChangeEventDetail.prototype = {
|
|
|
|
classID: Components.ID("1c50fdaf-74af-4b2e-814f-792caf65a168"),
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([]),
|
|
|
|
|
|
|
|
ownAction: false,
|
|
|
|
|
|
|
|
get text() {
|
|
|
|
return this._ctx.text;
|
|
|
|
},
|
|
|
|
|
|
|
|
get textBeforeCursor() {
|
|
|
|
return this._ctx.textBeforeCursor;
|
|
|
|
},
|
|
|
|
|
|
|
|
get textAfterCursor() {
|
|
|
|
return this._ctx.textAfterCursor;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-08 04:07:41 +04:00
|
|
|
/**
|
|
|
|
* ==============================================
|
|
|
|
* InputContext
|
|
|
|
* ==============================================
|
|
|
|
*/
|
2016-01-05 02:37:00 +03:00
|
|
|
function MozInputContext(data) {
|
2013-08-08 04:07:41 +04:00
|
|
|
this._context = {
|
2016-01-05 02:37:00 +03:00
|
|
|
type: data.type,
|
|
|
|
inputType: data.inputType,
|
|
|
|
inputMode: data.inputMode,
|
|
|
|
lang: data.lang,
|
|
|
|
selectionStart: data.selectionStart,
|
|
|
|
selectionEnd: data.selectionEnd,
|
|
|
|
text: data.value
|
2013-08-08 04:07:41 +04:00
|
|
|
};
|
|
|
|
|
2016-01-05 02:37:00 +03:00
|
|
|
this._contextId = data.contextId;
|
2013-08-08 04:07:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MozInputContext.prototype = {
|
2013-09-23 17:40:59 +04:00
|
|
|
_window: null,
|
2013-08-08 04:07:41 +04:00
|
|
|
_context: null,
|
|
|
|
_contextId: -1,
|
2015-07-03 00:47:00 +03:00
|
|
|
_ipcHelper: null,
|
2013-08-08 04:07:41 +04:00
|
|
|
|
|
|
|
classID: Components.ID("{1e38633d-d08b-4867-9944-afa5c648adb6}"),
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
2013-10-05 01:59:48 +04:00
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference
|
2013-08-08 04:07:41 +04:00
|
|
|
]),
|
|
|
|
|
|
|
|
init: function ic_init(win) {
|
|
|
|
this._window = win;
|
2015-07-03 00:47:00 +03:00
|
|
|
|
|
|
|
this._ipcHelper = WindowMap.getInputContextIpcHelper(win);
|
|
|
|
this._ipcHelper.attachInputContext(this);
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function ic_destroy() {
|
2013-08-09 13:41:48 +04: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 04:07:41 +04:00
|
|
|
// (outdated) information in the context. So let's clear that out.
|
2013-08-09 13:41:48 +04:00
|
|
|
for (var k in this._context) {
|
|
|
|
if (this._context.hasOwnProperty(k)) {
|
2013-08-08 04:07:41 +04:00
|
|
|
this._context[k] = null;
|
2013-08-09 13:41:48 +04:00
|
|
|
}
|
|
|
|
}
|
2013-09-23 17:40:59 +04:00
|
|
|
|
2015-07-03 00:47:00 +03:00
|
|
|
this._ipcHelper.detachInputContext();
|
|
|
|
this._ipcHelper = null;
|
|
|
|
|
2013-09-23 17:40:59 +04:00
|
|
|
this._window = null;
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function ic_receiveMessage(msg) {
|
|
|
|
if (!msg || !msg.json) {
|
|
|
|
dump('InputContext received message without data\n');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let json = msg.json;
|
2015-07-03 00:47:00 +03:00
|
|
|
let resolver = this._ipcHelper.takePromiseResolver(json.requestId);
|
2013-08-08 04:07:41 +04:00
|
|
|
|
2013-08-09 13:41:48 +04:00
|
|
|
if (!resolver) {
|
2015-07-03 00:47:00 +03:00
|
|
|
dump('InputContext received invalid requestId.\n');
|
2013-08-08 04:07:41 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-13 10:12:00 +04:00
|
|
|
// Update context first before resolving promise to avoid race condition
|
|
|
|
if (json.selectioninfo) {
|
2014-08-26 00:47:00 +04:00
|
|
|
this.updateSelectionContext(json.selectioninfo, true);
|
2014-08-13 10:12:00 +04:00
|
|
|
}
|
|
|
|
|
2013-08-08 04:07:41 +04:00
|
|
|
switch (msg.name) {
|
|
|
|
case "Keyboard:SendKey:Result:OK":
|
2015-08-24 04:19:00 +03:00
|
|
|
resolver.resolve(true);
|
2013-08-08 04:07:41 +04:00
|
|
|
break;
|
2014-01-02 19:39:26 +04:00
|
|
|
case "Keyboard:SendKey:Result:Error":
|
|
|
|
resolver.reject(json.error);
|
|
|
|
break;
|
2013-08-08 04:07:41 +04:00
|
|
|
case "Keyboard:GetText:Result:OK":
|
2013-08-09 13:41:48 +04:00
|
|
|
resolver.resolve(json.text);
|
2013-08-08 04:07:41 +04:00
|
|
|
break;
|
|
|
|
case "Keyboard:GetText:Result:Error":
|
2013-08-09 13:41:48 +04:00
|
|
|
resolver.reject(json.error);
|
2013-08-08 04:07:41 +04:00
|
|
|
break;
|
|
|
|
case "Keyboard:SetSelectionRange:Result:OK":
|
|
|
|
case "Keyboard:ReplaceSurroundingText:Result:OK":
|
2013-08-09 13:41:48 +04:00
|
|
|
resolver.resolve(
|
2014-02-01 23:06:59 +04:00
|
|
|
Cu.cloneInto(json.selectioninfo, this._window));
|
2013-08-08 04:07:41 +04:00
|
|
|
break;
|
|
|
|
case "Keyboard:SequenceError":
|
|
|
|
// Occurs when a new element got focus, but the inputContext was
|
|
|
|
// not invalidated yet...
|
2013-08-09 13:41:48 +04:00
|
|
|
resolver.reject("InputContext has expired");
|
2013-08-08 04:07:41 +04:00
|
|
|
break;
|
2013-08-30 16:07:16 +04:00
|
|
|
case "Keyboard:SetComposition:Result:OK": // Fall through.
|
|
|
|
case "Keyboard:EndComposition:Result:OK":
|
2015-08-24 04:19:00 +03:00
|
|
|
resolver.resolve(true);
|
2013-08-30 16:07:16 +04:00
|
|
|
break;
|
2013-08-08 04:07:41 +04:00
|
|
|
default:
|
2013-08-09 13:41:48 +04:00
|
|
|
dump("Could not find a handler for " + msg.name);
|
|
|
|
resolver.reject();
|
2013-08-08 04:07:41 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-01-05 02:37:00 +03:00
|
|
|
updateSelectionContext: function ic_updateSelectionContext(data, ownAction) {
|
2013-08-08 04:07:41 +04:00
|
|
|
if (!this._context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-05 02:37:00 +03:00
|
|
|
let selectionDirty =
|
|
|
|
this._context.selectionStart !== data.selectionStart ||
|
|
|
|
this._context.selectionEnd !== data.selectionEnd;
|
|
|
|
let surroundDirty = selectionDirty || data.text !== this._contextId.text;
|
2013-08-08 04:07:41 +04:00
|
|
|
|
2016-01-05 02:37:00 +03:00
|
|
|
this._context.text = data.text;
|
|
|
|
this._context.selectionStart = data.selectionStart;
|
|
|
|
this._context.selectionEnd = data.selectionEnd;
|
2013-08-08 04:07:41 +04:00
|
|
|
|
|
|
|
if (selectionDirty) {
|
2016-01-05 02:37:00 +03:00
|
|
|
let selectionChangeDetail =
|
|
|
|
new MozInputContextSelectionChangeEventDetail(this, ownAction);
|
|
|
|
let wrappedSelectionChangeDetail =
|
|
|
|
this._window.MozInputContextSelectionChangeEventDetail
|
|
|
|
._create(this._window, selectionChangeDetail);
|
|
|
|
let selectionChangeEvent = new this._window.CustomEvent("selectionchange",
|
|
|
|
{ cancelable: false, detail: wrappedSelectionChangeDetail });
|
2013-08-08 04:07:41 +04:00
|
|
|
|
2016-01-05 02:37:00 +03:00
|
|
|
this.__DOM_IMPL__.dispatchEvent(selectionChangeEvent);
|
2013-08-08 04:07:41 +04:00
|
|
|
}
|
|
|
|
|
2016-01-05 02:37:00 +03:00
|
|
|
if (surroundDirty) {
|
|
|
|
let surroundingTextChangeDetail =
|
|
|
|
new MozInputContextSurroundingTextChangeEventDetail(this, ownAction);
|
|
|
|
let wrappedSurroundingTextChangeDetail =
|
|
|
|
this._window.MozInputContextSurroundingTextChangeEventDetail
|
|
|
|
._create(this._window, surroundingTextChangeDetail);
|
|
|
|
let selectionChangeEvent = new this._window.CustomEvent("surroundingtextchange",
|
|
|
|
{ cancelable: false, detail: wrappedSurroundingTextChangeDetail });
|
2013-08-08 04:07:41 +04:00
|
|
|
|
2016-01-05 02:37:00 +03:00
|
|
|
this.__DOM_IMPL__.dispatchEvent(selectionChangeEvent);
|
|
|
|
}
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// tag name of the input field
|
|
|
|
get type() {
|
|
|
|
return this._context.type;
|
|
|
|
},
|
|
|
|
|
|
|
|
// type of the input field
|
|
|
|
get inputType() {
|
2015-09-10 23:27:00 +03:00
|
|
|
return this._context.inputType;
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
get inputMode() {
|
2015-09-10 23:27:00 +03:00
|
|
|
return this._context.inputMode;
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
get lang() {
|
|
|
|
return this._context.lang;
|
|
|
|
},
|
|
|
|
|
|
|
|
getText: function ic_getText(offset, length) {
|
2016-01-05 02:37:00 +03:00
|
|
|
let text;
|
|
|
|
if (offset && length) {
|
|
|
|
text = this._context.text.substr(offset, length);
|
|
|
|
} else if (offset) {
|
|
|
|
text = this._context.text.substr(offset);
|
|
|
|
} else {
|
|
|
|
text = this._context.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._window.Promise.resolve(text);
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
get selectionStart() {
|
|
|
|
return this._context.selectionStart;
|
|
|
|
},
|
|
|
|
|
|
|
|
get selectionEnd() {
|
|
|
|
return this._context.selectionEnd;
|
|
|
|
},
|
|
|
|
|
2016-01-05 02:37:00 +03:00
|
|
|
get text() {
|
|
|
|
return this._context.text;
|
|
|
|
},
|
|
|
|
|
2013-08-08 04:07:41 +04:00
|
|
|
get textBeforeCursor() {
|
2016-01-05 02:37:00 +03:00
|
|
|
let text = this._context.text;
|
|
|
|
let start = this._context.selectionStart;
|
|
|
|
return (start < 100) ?
|
|
|
|
text.substr(0, start) :
|
|
|
|
text.substr(start - 100, 100);
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
get textAfterCursor() {
|
2016-01-05 02:37:00 +03:00
|
|
|
let text = this._context.text;
|
|
|
|
let start = this._context.selectionStart;
|
|
|
|
let end = this._context.selectionEnd;
|
|
|
|
return text.substr(start, end - start + 100);
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
setSelectionRange: function ic_setSelectionRange(start, length) {
|
2013-08-09 13:41:48 +04:00
|
|
|
let self = this;
|
2013-09-23 17:40:59 +04:00
|
|
|
return this._sendPromise(function(resolverId) {
|
2014-08-07 10:44:46 +04:00
|
|
|
cpmmSendAsyncMessageWithKbID(self, 'Keyboard:SetSelectionRange', {
|
2013-08-09 13:41:48 +04:00
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
selectionStart: start,
|
|
|
|
selectionEnd: start + length
|
|
|
|
});
|
2013-08-08 04:07:41 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
get onsurroundingtextchange() {
|
2013-08-09 13:41:48 +04:00
|
|
|
return this.__DOM_IMPL__.getEventHandler("onsurroundingtextchange");
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
set onsurroundingtextchange(handler) {
|
2013-11-18 10:56:56 +04:00
|
|
|
this.__DOM_IMPL__.setEventHandler("onsurroundingtextchange", handler);
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
get onselectionchange() {
|
2013-08-09 13:41:48 +04:00
|
|
|
return this.__DOM_IMPL__.getEventHandler("onselectionchange");
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
set onselectionchange(handler) {
|
2013-11-18 10:56:56 +04:00
|
|
|
this.__DOM_IMPL__.setEventHandler("onselectionchange", handler);
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
replaceSurroundingText: function ic_replaceSurrText(text, offset, length) {
|
2013-08-09 13:41:48 +04:00
|
|
|
let self = this;
|
2013-09-23 17:40:59 +04:00
|
|
|
return this._sendPromise(function(resolverId) {
|
2014-08-07 10:44:46 +04:00
|
|
|
cpmmSendAsyncMessageWithKbID(self, 'Keyboard:ReplaceSurroundingText', {
|
2013-08-09 13:41:48 +04:00
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
text: text,
|
2014-01-03 18:17:28 +04:00
|
|
|
offset: offset || 0,
|
|
|
|
length: length || 0
|
2013-08-09 13:41:48 +04:00
|
|
|
});
|
2013-08-08 04:07:41 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteSurroundingText: function ic_deleteSurrText(offset, length) {
|
|
|
|
return this.replaceSurroundingText(null, offset, length);
|
|
|
|
},
|
|
|
|
|
2015-08-24 04:19:00 +03:00
|
|
|
sendKey: function ic_sendKey(dictOrKeyCode, charCode, modifiers, repeat) {
|
|
|
|
if (typeof dictOrKeyCode === 'number') {
|
|
|
|
// XXX: modifiers are ignored in this API method.
|
2015-07-17 11:41:00 +03:00
|
|
|
|
2015-08-24 04:19:00 +03:00
|
|
|
return this._sendPromise((resolverId) => {
|
|
|
|
cpmmSendAsyncMessageWithKbID(this, 'Keyboard:SendKey', {
|
|
|
|
contextId: this._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
method: 'sendKey',
|
|
|
|
keyCode: dictOrKeyCode,
|
|
|
|
charCode: charCode,
|
|
|
|
repeat: repeat
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else if (typeof dictOrKeyCode === 'object') {
|
|
|
|
return this._sendPromise((resolverId) => {
|
|
|
|
cpmmSendAsyncMessageWithKbID(this, 'Keyboard:SendKey', {
|
|
|
|
contextId: this._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
method: 'sendKey',
|
|
|
|
keyboardEventDict: this._getkeyboardEventDict(dictOrKeyCode)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// XXX: Should not reach here; implies WebIDL binding error.
|
|
|
|
throw new TypeError('Unknown argument passed.');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
keydown: function ic_keydown(dict) {
|
|
|
|
return this._sendPromise((resolverId) => {
|
|
|
|
cpmmSendAsyncMessageWithKbID(this, 'Keyboard:SendKey', {
|
|
|
|
contextId: this._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
method: 'keydown',
|
|
|
|
keyboardEventDict: this._getkeyboardEventDict(dict)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
keyup: function ic_keyup(dict) {
|
|
|
|
return this._sendPromise((resolverId) => {
|
|
|
|
cpmmSendAsyncMessageWithKbID(this, 'Keyboard:SendKey', {
|
|
|
|
contextId: this._contextId,
|
2013-08-09 13:41:48 +04:00
|
|
|
requestId: resolverId,
|
2015-08-24 04:19:00 +03:00
|
|
|
method: 'keyup',
|
|
|
|
keyboardEventDict: this._getkeyboardEventDict(dict)
|
2013-08-09 13:41:48 +04:00
|
|
|
});
|
2013-08-08 04:07:41 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-08-24 04:19:00 +03:00
|
|
|
setComposition: function ic_setComposition(text, cursor, clauses, dict) {
|
2013-08-30 16:07:16 +04:00
|
|
|
let self = this;
|
2015-08-24 04:19:00 +03:00
|
|
|
return this._sendPromise((resolverId) => {
|
2014-08-07 10:44:46 +04:00
|
|
|
cpmmSendAsyncMessageWithKbID(self, 'Keyboard:SetComposition', {
|
2013-08-30 16:07:16 +04:00
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
|
|
|
text: text,
|
2014-06-17 08:05:49 +04:00
|
|
|
cursor: (typeof cursor !== 'undefined') ? cursor : text.length,
|
2015-08-24 04:19:00 +03:00
|
|
|
clauses: clauses || null,
|
|
|
|
keyboardEventDict: this._getkeyboardEventDict(dict)
|
2013-08-30 16:07:16 +04:00
|
|
|
});
|
|
|
|
});
|
2013-08-08 04:07:41 +04:00
|
|
|
},
|
|
|
|
|
2015-08-24 04:19:00 +03:00
|
|
|
endComposition: function ic_endComposition(text, dict) {
|
2013-08-30 16:07:16 +04:00
|
|
|
let self = this;
|
2015-08-24 04:19:00 +03:00
|
|
|
return this._sendPromise((resolverId) => {
|
2014-08-07 10:44:46 +04:00
|
|
|
cpmmSendAsyncMessageWithKbID(self, 'Keyboard:EndComposition', {
|
2013-08-30 16:07:16 +04:00
|
|
|
contextId: self._contextId,
|
|
|
|
requestId: resolverId,
|
2015-08-24 04:19:00 +03:00
|
|
|
text: text || '',
|
|
|
|
keyboardEventDict: this._getkeyboardEventDict(dict)
|
2013-08-30 16:07:16 +04:00
|
|
|
});
|
|
|
|
});
|
2013-09-23 17:40:59 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
_sendPromise: function(callback) {
|
|
|
|
let self = this;
|
2015-08-20 00:53:22 +03:00
|
|
|
return this._ipcHelper.createPromiseWithId(function(aResolverId) {
|
2013-09-23 17:40:59 +04:00
|
|
|
if (!WindowMap.isActive(self._window)) {
|
2015-08-20 00:53:22 +03:00
|
|
|
self._ipcHelper.removePromiseResolver(aResolverId);
|
2013-09-23 17:40:59 +04:00
|
|
|
reject('Input method is not active.');
|
|
|
|
return;
|
|
|
|
}
|
2015-08-20 00:53:22 +03:00
|
|
|
callback(aResolverId);
|
2013-09-23 17:40:59 +04:00
|
|
|
});
|
2015-08-24 04:19:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// Take a MozInputMethodKeyboardEventDict dict, creates a keyboardEventDict
|
|
|
|
// object that can be sent to forms.js
|
|
|
|
_getkeyboardEventDict: function(dict) {
|
|
|
|
if (typeof dict !== 'object' || !dict.key) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var keyboardEventDict = {
|
|
|
|
key: dict.key,
|
|
|
|
code: dict.code,
|
|
|
|
repeat: dict.repeat,
|
|
|
|
flags: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
if (dict.printable) {
|
|
|
|
keyboardEventDict.flags |=
|
|
|
|
Ci.nsITextInputProcessor.KEY_FORCE_PRINTABLE_KEY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (/^[a-zA-Z0-9]$/.test(dict.key)) {
|
|
|
|
// keyCode must follow the key value in this range;
|
|
|
|
// disregard the keyCode from content.
|
|
|
|
keyboardEventDict.keyCode = dict.key.toUpperCase().charCodeAt(0);
|
|
|
|
} else if (typeof dict.keyCode === 'number') {
|
|
|
|
// Allow keyCode to be specified for other key values.
|
|
|
|
keyboardEventDict.keyCode = dict.keyCode;
|
|
|
|
|
|
|
|
// Allow keyCode to be explicitly set to zero.
|
|
|
|
if (dict.keyCode === 0) {
|
|
|
|
keyboardEventDict.flags |=
|
|
|
|
Ci.nsITextInputProcessor.KEY_KEEP_KEYCODE_ZERO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyboardEventDict;
|
2013-08-01 21:16:43 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-09 22:03:00 +04:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MozInputMethod]);
|