1// Copyright (c) 2012 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.
4
5// Custom binding for the input ime API. Only injected into the
6// v8 contexts for extensions which have permission for the API.
7
8var binding = require('binding').Binding.create('input.ime');
9
10var Event = require('event_bindings').Event;
11
12binding.registerCustomHook(function(api) {
13  var input_ime = api.compiledApi;
14
15  input_ime.onKeyEvent.dispatchToListener = function(callback, args) {
16    var engineID = args[0];
17    var keyData = args[1];
18
19    var result = false;
20    try {
21      result = $Function.call(Event.prototype.dispatchToListener,
22          this, callback, args);
23    } catch (e) {
24      console.error('Error in event handler for onKeyEvent: ' + e.stack);
25    }
26    if (!input_ime.onKeyEvent.async) {
27      input_ime.keyEventHandled(keyData.requestId, result);
28    }
29  };
30
31  input_ime.onKeyEvent.addListener = function(cb, opt_extraInfo) {
32    input_ime.onKeyEvent.async = false;
33    if (opt_extraInfo instanceof Array) {
34      for (var i = 0; i < opt_extraInfo.length; ++i) {
35        if (opt_extraInfo[i] == "async") {
36          input_ime.onKeyEvent.async = true;
37        }
38      }
39    }
40    $Function.call(Event.prototype.addListener, this, cb);
41  };
42});
43
44exports.binding = binding.generate();
45