summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/login/oobe_screen_hid_detection.js
blob: 7610e2b788a4a22072d8d6b01f137c481a5b8c63 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2014 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.

/**
 * @fileoverview Oobe HID detection screen implementation.
 */

login.createScreen('HIDDetectionScreen', 'hid-detection', function() {
  return {
    EXTERNAL_API: [
      'setPointingDeviceState',
      'setKeyboardDeviceState',
    ],

  /**
   * Enumeration of possible states during pairing.  The value associated with
   * each state maps to a localized string in the global variable
   * |loadTimeData|.
   * @enum {string}
   */
   PAIRING: {
     STARTUP: 'bluetoothStartConnecting',
     REMOTE_PIN_CODE: 'bluetoothRemotePinCode',
     REMOTE_PASSKEY: 'bluetoothRemotePasskey',
     CONNECT_FAILED: 'bluetoothConnectFailed',
     CANCELED: 'bluetoothPairingCanceled',
     // Pairing dismissed (succeeded or canceled).
     DISMISSED: 'bluetoothPairingDismissed'
   },

    /**
     * Button to move to usual OOBE flow after detection.
     * @private
     */
    continueButton_: null,

    /**
     * Buttons in oobe wizard's button strip.
     * @type {array} Array of Buttons.
     */
    get buttons() {
      var buttons = [];
      var continueButton = this.ownerDocument.createElement('button');
      continueButton.id = 'hid-continue-button';
      continueButton.textContent = loadTimeData.getString(
          'hidDetectionContinue');
      continueButton.addEventListener('click', function(e) {
        chrome.send('HIDDetectionOnContinue');
        e.stopPropagation();
      });
      buttons.push(continueButton);
      this.continueButton_ = continueButton;

      return buttons;
    },

    /**
     * Returns a control which should receive an initial focus.
     */
    get defaultControl() {
      return this.continueButton_;
    },

    /**
     * Sets a device-block css class to reflect device state of searching,
     * connected, pairing or paired (for BT devices).
     * @param {blockId} id one of 'hid-mouse-block' or 'hid-keyboard-block'.
     * @param {state} one of 'searching', 'connected', 'pairing', 'paired',
     * @private
     */
    setDeviceBlockState_: function(blockId, state) {
      if (state == 'update')
        return;
      var deviceBlock = $(blockId);
      var states = ['searching', 'connected', 'pairing', 'paired'];
      for (var i = 0; i < states.length; ++i) {
        if (states[i] != state)
          deviceBlock.classList.remove(states[i]);
      }
      deviceBlock.classList.add(state);
    },

    /**
     * Sets state for mouse-block.
     * @param {state} one of 'searching', 'connected', 'paired'.
     */
    setPointingDeviceState: function(state) {
      if (state === undefined)
        return;
      this.setDeviceBlockState_('hid-mouse-block', state);
    },

    /**
     * Sets state for pincode key elements.
     * @param {entered} int, number of typed keys of pincode, -1 if keys press
     * detection is not supported by device.
     */
    setPincodeKeysState_: function(entered) {
      var pincodeLength = 7; // including enter-key
      for (var i = 0; i < pincodeLength; i++) {
        var pincodeSymbol = $('hid-keyboard-pincode-sym-' + (i + 1));
        pincodeSymbol.classList.remove('key-typed');
        pincodeSymbol.classList.remove('key-untyped');
        pincodeSymbol.classList.remove('key-next');
        if (i < entered)
          pincodeSymbol.classList.add('key-typed');
        else if (i == entered)
          pincodeSymbol.classList.add('key-next');
        else if (entered != -1)
          pincodeSymbol.classList.add('key-untyped');
      }
    },

    /**
     * Sets state for keyboard-block.
     * @param {data} dict with parameters.
     */
    setKeyboardDeviceState: function(data) {
      if (data === undefined || !('state' in data))
        return;
      var state = data['state'];
      this.setDeviceBlockState_('hid-keyboard-block', state);
      if (state == 'paired')
        $('hid-keyboard-label-paired').textContent = data['keyboard-label'];
      else if (state == 'pairing') {
        $('hid-keyboard-label-pairing').textContent = data['keyboard-label'];
        if (data['pairing-state'] == this.PAIRING.REMOTE_PIN_CODE ||
            data['pairing-state'] == this.PAIRING.REMOTE_PASSKEY) {
          this.setPincodeKeysState_(-1);
          for (var i = 0, len = data['pincode'].length; i < len; i++) {
            var pincodeSymbol = $('hid-keyboard-pincode-sym-' + (i + 1));
            pincodeSymbol.textContent = data['pincode'][i];
          }
          announceAccessibleMessage(
              data['keyboard-label'] + ' ' + data['pincode'] + ' ' +
              loadTimeData.getString('hidDetectionBTEnterKey'));
        }
      } else if (state == 'update') {
        if ('keysEntered' in data) {
          this.setPincodeKeysState_(data['keysEntered']);
        }
      }
    },

    /**
     * Event handler that is invoked just before the screen in shown.
     * @param {Object} data Screen init payload.
     */
    onBeforeShow: function(data) {
      $('hid-continue-button').disabled = true;
      this.setDeviceBlockState_('hid-mouse-block', 'searching');
      this.setDeviceBlockState_('hid-keyboard-block', 'searching');
    },
  };
});