1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 * This class is a base class of each input method implementation.
10 var IMEBase = function() {};
12 onActivate: function() {},
13 onDeactivated: function() {},
14 onFocus: function(context) {},
15 onBlur: function(contextID) {},
16 onInputContextUpdate: function(context) {},
17 onKeyEvent: function(context, engine, keyData) { return false; },
18 onCandidateClicked: function(candidateID, button) {},
19 onMenuItemActivated: function(name) {},
20 onSurroundingTextChanged: function(text, focus, anchor) {},
21 onReset: function(engineID) {}
25 * This class provides simple identity input methods.
28 var IdentityIME = function() {};
29 IdentityIME.prototype = new IMEBase();
32 * This class provides an IME which capitalize given character.
35 var ToUpperIME = function() {};
36 ToUpperIME.prototype = new IMEBase();
39 * @param {Object} context A context object passed from input.ime.onFocus.
40 * @param {string} engine An engine ID.
41 * @param {Object} keyData A keyevent object passed from input.ime.onKeyEvent.
42 * @return {boolean} True on the key event is consumed.
44 ToUpperIME.prototype.onKeyEvent = function(context, engine, keyData) {
45 if (keyData.type == 'keydown' && /^[a-zA-Z]$/.test(keyData.key)) {
46 chrome.input.ime.commitText({
47 contextID: context.contextID,
48 text: keyData.key.toUpperCase()
56 * This class provide an IME which sneds message with API argument.
59 var APIArgumentIME = function() {};
60 APIArgumentIME.prototype = new IMEBase();
63 * @param {Object} context A context object passed from input.ime.onFocus.
64 * @param {string} engine An engine ID.
65 * @param {Object} keyData A keyevent object passed from input.ime.onKeyEvent.
66 * @return {boolean} True on the key event is consumed.
68 APIArgumentIME.prototype.onKeyEvent = function(context, engine, keyData) {
69 chrome.test.sendMessage('onKeyEvent:' +
73 keyData.ctrlKey + ':' +
74 keyData.altKey + ':' +
75 keyData.shiftKey + ':' +
81 * This class listens the event from chrome.input.ime and forwards it to the
85 var EngineBridge = function() {};
86 EngineBridge.prototype = {
89 * Map from engineID to actual engine instance.
96 * A current active engineID.
103 * A input context currently focused.
107 focusedContext_: null,
110 * Called from chrome.input.ime.onActivate.
114 onActivate_: function(engineID) {
115 this.activeEngine_ = engineID;
116 this.engineInstance_[engineID].onActivate();
117 chrome.test.sendMessage('onActivate');
121 * Called from chrome.input.ime.onDeactivated.
125 onDeactivated_: function(engineID) {
126 if (this.engineInstance_[engineID])
127 this.engineInstance_[engineID].onDeactivated();
128 this.activeEngine_ = null;
129 chrome.test.sendMessage('onDeactivated');
133 * Called from chrome.input.ime.onFocus.
137 onFocus_: function(context) {
138 this.focusedContext_ = context;
139 if (this.activeEngine_)
140 this.engineInstance_[this.activeEngine_].onFocus(context);
141 chrome.test.sendMessage('onFocus');
145 * Called from chrome.input.ime.onBlur.
149 onBlur_: function(contextID) {
150 if (this.activeEngine_)
151 this.engineInstance_[this.activeEngine_].onBlur(contextID);
152 this.focusedContext_ = null;
153 chrome.test.sendMessage('onBlur');
157 * Called from chrome.input.ime.onInputContextUpdate.
161 onInputContextUpdate_: function(context) {
162 this.focusedContext_ = context;
163 if (this.activeEngine_)
164 this.engineInstance_[this.activeEngine_].onInputContextUpdate(context);
165 chrome.test.sendMessage('onInputContextUpdate');
169 * Called from chrome.input.ime.onKeyEvent.
172 * @return {boolean} True on the key event is consumed.
174 onKeyEvent_: function(engineID, keyData) {
175 chrome.test.sendMessage('onKeyEvent');
176 if (this.engineInstance_[engineID])
177 return this.engineInstance_[engineID].onKeyEvent(
178 this.focusedContext_, this.activeEngine_, keyData);
183 * Called from chrome.input.ime.onCandidateClicked.
187 onCandidateClicked_: function(engineID, candidateID, button) {
188 if (this.engineInstance_[engineID])
189 this.engineInstance_[engineID].onCandidateClicked(candidateID, button);
190 chrome.test.sendMessage('onCandidateClicked');
194 * Called from chrome.input.ime.onMenuItemActivated.
198 onMenuItemActivated_: function(engineID, name) {
199 this.engineInstance_[engineID].onMenuItemActivated(name);
200 chrome.test.sendMessage('onMenuItemActivated');
204 * Called from chrome.input.ime.onSurroundingTextChanged.
208 onSurroundingTextChanged_: function(engineID, object) {
209 this.engineInstance_[engineID].onSurroundingTextChanged(
210 object.text, object.focus, object.anchor);
211 chrome.test.sendMessage('onSurroundingTextChanged');
215 * Called from chrome.input.ime.onReset.
219 onReset_: function(engineID) {
220 this.engineInstance_[engineID].onReset(engineID);
221 chrome.test.sendMessage('onReset');
225 * Add engine instance for |engineID|.
228 addEngine: function(engineID, engine) {
229 this.engineInstance_[engineID] = engine;
233 * Returns active engine ID. Returns null if there is no active engine.
235 * @return {string} An string which identify the engine.
237 getActiveEngineID: function() {
238 return this.activeEngine_;
242 * Returns currently focused context ID. Returns null if there is no focused
245 * @return {strine} An string which identify the context.
247 getFocusedContextID: function () {
248 return this.focusedContext_;
252 * Initialize EngineBridge by binding with chrome event.
255 Initialize: function() {
256 chrome.input.ime.onActivate.addListener(this.onActivate_.bind(this));
257 chrome.input.ime.onDeactivated.addListener(this.onDeactivated_.bind(this));
258 chrome.input.ime.onFocus.addListener(this.onFocus_.bind(this));
259 chrome.input.ime.onBlur.addListener(this.onBlur_.bind(this));
260 chrome.input.ime.onInputContextUpdate.addListener(
261 this.onInputContextUpdate_.bind(this));
262 chrome.input.ime.onKeyEvent.addListener(this. onKeyEvent_.bind(this));
263 chrome.input.ime.onCandidateClicked.addListener(
264 this.onCandidateClicked_.bind(this));
265 chrome.input.ime.onMenuItemActivated.addListener(
266 this.onMenuItemActivated_.bind(this));
267 chrome.input.ime.onSurroundingTextChanged.addListener(
268 this.onSurroundingTextChanged_.bind(this));
269 chrome.input.ime.onReset.addListener(this.onReset_.bind(this));
273 var engineBridge = new EngineBridge();
274 engineBridge.Initialize();
275 engineBridge.addEngine('IdentityIME', new IdentityIME());
276 engineBridge.addEngine('ToUpperIME', new ToUpperIME());
277 engineBridge.addEngine('APIArgumentIME', new APIArgumentIME());
278 chrome.test.sendMessage('ReadyToUseImeEvent');