summaryrefslogtreecommitdiff
path: root/chromium/ui/keyboard/resources/webui/api_adapter.js
blob: 17d84b9d7c35c5c4c1a953bca5406d9a7e865bbb (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) 2013 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.

function insertText(text) {
  chrome.send('insertText', [ text ]);
}

function sendKeyEvent(event) {
  chrome.send('sendKeyEvent', [ event ]);
}

function hideKeyboard() {
  chrome.send('hideKeyboard');
}

(function(exports) {
  /**
   * An array to save callbacks of each request.
   * @type {Array.<function(Object)>}
   */
  var requestIdCallbackMap = [];

  /**
   * An incremental integer that represents a unique requestId.
   * @type {number}
   */
  var requestId = 0;

  /**
   * Called when a text input box gets focus.
   * @param {object} inputContext Describes an input context. It only contains
   *     the type of text input box at present and only "password", "number" and
   *     "text" are supported.
   */
  function OnTextInputBoxFocused(inputContext) {
    keyboard.inputType = inputContext.type;
  }

  /**
   * Gets the context of the focused input field. The context is returned as a
   * paramter in the |callback|.
   * @param {function(Object)} callback The callback function after the webui
   *     function finished.
   * @return {number} The ID of the new request.
   */
  function GetInputContext(callback) {
    var id = requestId;
    requestIdCallbackMap[id] = callback;
    chrome.send('getInputContext', [ id ]);
    requestId++;
    return id;
  }

  /**
   * Cancel the callback specified by requestId.
   * @param {number} requestId The requestId of the callback that about to
   *     cancel.
   */
  function CancelRequest(requestId) {
    requestIdCallbackMap[requestId] = undefined;
  }

  /**
   * Webui function callback. Any call to chrome.send('getInputContext', [id])
   * should trigger this function being called with the parameter
   * inputContext.requestId == id.
   * @param {Object} inputContext The context of focused input field. Note we
   *     only have type(input box type) and requestId fields now.
   */
  function GetInputContextCallback(inputContext) {
    var requestId = inputContext.requestId;
    if (!requestIdCallbackMap[requestId])
      return;
    requestIdCallbackMap[requestId](inputContext);
  }

  exports.OnTextInputBoxFocused = OnTextInputBoxFocused;
  exports.getInputContext = GetInputContext;
  exports.cancelRequest = CancelRequest;
  exports.GetInputContextCallback = GetInputContextCallback;
})(this);