summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/resources/extensions/input.ime_custom_bindings.js
blob: 8f400c780fd3502184137a6a32262de32ff0a27b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Custom bindings for the input ime API. Only injected into the
// v8 contexts for extensions which have permission for the API.

var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();

chromeHidden.registerCustomHook('input.ime', function() {
  chrome.input.ime.onKeyEvent.dispatch = function(engineID, keyData) {
    var args = Array.prototype.slice.call(arguments);
    if (this.validate_) {
      var validationErrors = this.validate_(args);
      if (validationErrors) {
        chrome.input.ime.eventHandled(requestId, false);
        return validationErrors;
      }
    }
    if (this.listeners_.length > 1) {
      console.error('Too many listeners for onKeyEvent: ' + e.stack);
      chrome.input.ime.eventHandled(requestId, false);
      return;
    }
    for (var i = 0; i < this.listeners_.length; i++) {
      try {
        var requestId = keyData.requestId;
        var result = this.listeners_[i].apply(null, args);
        chrome.input.ime.eventHandled(requestId, result);
      } catch (e) {
        console.error('Error in event handler for onKeyEvent: ' + e.stack);
        chrome.input.ime.eventHandled(requestId, false);
      }
    }
  };
});