// 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);
      }
    }
  };
});

// 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].callback.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);
      }
    }
  };
});

Use lisnters_[i].callback rather than the listener itself.
Use lisnters_[i].callback rather than the listener itself.

In crrev.com/145145, Chrome extension's addListener doesn't add the callback function
to the listeners_, rather it adds an object wrapping the callback and some other metadata.
So here needs to refer .callback too.

R=zork@chromium.org
BUG=136058
TEST=manually verified on lumpy device with installing an IME extension.


Review URL: https://chromiumcodereview.appspot.com/10698102

git-svn-id: de016e52bd170d2d4f2344f9bf92d50478b649e0@145510 0039d316-1c4b-4281-b951-d872f2087c98
