Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / resources / chromeos / login / oobe_screen_hid_detection.js
blobed60538fa33f1831cf7b28c056678ffa428a2e2e
1 // Copyright 2014 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.
5 /**
6  * @fileoverview Oobe HID detection screen implementation.
7  */
9 login.createScreen('HIDDetectionScreen', 'hid-detection', function() {
10   var CONTEXT_KEY_KEYBOARD_STATE = 'keyboard-state';
11   var CONTEXT_KEY_MOUSE_STATE = 'mouse-state';
12   var CONTEXT_KEY_KEYBOARD_PINCODE = 'keyboard-pincode';
13   var CONTEXT_KEY_KEYBOARD_ENTERED_PART_EXPECTED = 'num-keys-entered-expected';
14   var CONTEXT_KEY_KEYBOARD_ENTERED_PART_PINCODE = 'num-keys-entered-pincode';
15   var CONTEXT_KEY_MOUSE_DEVICE_NAME = 'mouse-device-name';
16   var CONTEXT_KEY_KEYBOARD_DEVICE_NAME = 'keyboard-device-name';
17   var CONTEXT_KEY_KEYBOARD_LABEL = 'keyboard-device-label';
18   var CONTEXT_KEY_CONTINUE_BUTTON_ENABLED = 'continue-button-enabled';
20   var PINCODE_LENGTH = 6;
22   return {
24   /**
25    * Enumeration of possible states during pairing.  The value associated with
26    * each state maps to a localized string in the global variable
27    * |loadTimeData|.
28    * @enum {string}
29    */
30    PAIRING: {
31      STARTUP: 'bluetoothStartConnecting',
32      REMOTE_PIN_CODE: 'bluetoothRemotePinCode',
33      CONNECT_FAILED: 'bluetoothConnectFailed',
34      CANCELED: 'bluetoothPairingCanceled',
35      // Pairing dismissed (succeeded or canceled).
36      DISMISSED: 'bluetoothPairingDismissed'
37    },
39    // Enumeration of possible connection states of a device.
40    CONNECTION: {
41      SEARCHING: 'searching',
42      CONNECTED: 'connected',
43      PAIRING: 'pairing',
44      PAIRED: 'paired',
45      // Special info state.
46      UPDATE: 'update'
47    },
49    // Possible ids of device blocks.
50    BLOCK: {
51      MOUSE: 'hid-mouse-block',
52      KEYBOARD: 'hid-keyboard-block'
53    },
55     /**
56      * Button to move to usual OOBE flow after detection.
57      * @private
58      */
59     continueButton_: null,
61     /** @override */
62     decorate: function() {
63       var self = this;
65       this.context.addObserver(
66           CONTEXT_KEY_MOUSE_STATE,
67           function(stateId) {
68             if (stateId === undefined)
69               return;
70             self.setDeviceBlockState_('hid-mouse-block', stateId);
71           }
72       );
73       this.context.addObserver(
74         CONTEXT_KEY_KEYBOARD_STATE,
75         function(stateId) {
76           self.updatePincodeKeysState_();
77           if (stateId === undefined)
78             return;
79           self.setDeviceBlockState_('hid-keyboard-block', stateId);
80           if (stateId == self.CONNECTION.PAIRED) {
81             $('hid-keyboard-label-paired').textContent = self.context.get(
82                 CONTEXT_KEY_KEYBOARD_LABEL, '');
83           } else if (stateId == self.CONNECTION.PAIRING) {
84             $('hid-keyboard-label-pairing').textContent = self.context.get(
85                 CONTEXT_KEY_KEYBOARD_LABEL, '');
86           }
87         }
88       );
89       this.context.addObserver(
90         CONTEXT_KEY_KEYBOARD_PINCODE,
91         this.updatePincodeKeysState_.bind(this));
92       this.context.addObserver(
93         CONTEXT_KEY_KEYBOARD_ENTERED_PART_EXPECTED,
94         this.updatePincodeKeysState_.bind(this));
95       this.context.addObserver(
96         CONTEXT_KEY_KEYBOARD_ENTERED_PART_PINCODE,
97         this.updatePincodeKeysState_.bind(this));
98       this.context.addObserver(
99         CONTEXT_KEY_CONTINUE_BUTTON_ENABLED,
100         function(enabled) {
101           $('hid-continue-button').disabled = !enabled;
102         }
103       );
104     },
106     /**
107      * Buttons in oobe wizard's button strip.
108      * @type {array} Array of Buttons.
109      */
110     get buttons() {
111       var buttons = [];
112       var continueButton = this.ownerDocument.createElement('button');
113       continueButton.id = 'hid-continue-button';
114       continueButton.textContent = loadTimeData.getString(
115           'hidDetectionContinue');
116       continueButton.addEventListener('click', function(e) {
117         chrome.send('HIDDetectionOnContinue');
118         e.stopPropagation();
119       });
120       buttons.push(continueButton);
122       return buttons;
123     },
125     /**
126      * Returns a control which should receive an initial focus.
127      */
128     get defaultControl() {
129       return $('hid-continue-button');
130     },
132     /**
133      * Sets a device-block css class to reflect device state of searching,
134      * connected, pairing or paired (for BT devices).
135      * @param {blockId} id one of keys of this.BLOCK dict.
136      * @param {state} one of keys of this.CONNECTION dict.
137      * @private
138      */
139     setDeviceBlockState_: function(blockId, state) {
140       if (state == 'update')
141         return;
142       var deviceBlock = $(blockId);
143       for (var key in this.CONNECTION) {
144         var stateCase = this.CONNECTION[key];
145         deviceBlock.classList.toggle(stateCase, stateCase == state);
146       }
147     },
149     /**
150      * Sets state for mouse-block.
151      * @param {state} one of keys of this.CONNECTION dict.
152      */
153     setPointingDeviceState: function(state) {
154       if (state === undefined)
155         return;
156       this.setDeviceBlockState_(this.BLOCK.MOUSE, state);
157     },
159     /**
160      * Updates state for pincode key elements based on context state.
161      */
162     updatePincodeKeysState_: function() {
163       var pincodeKeys = $('hid-keyboard-pincode');
164       var pincode = this.context.get(CONTEXT_KEY_KEYBOARD_PINCODE, '');
165       var state = this.context.get(CONTEXT_KEY_KEYBOARD_STATE, '');
167       if (!pincode || state !== this.CONNECTION.PAIRING) {
168         pincodeKeys.hidden = true;
169         return;
170       }
172       if (pincodeKeys.hidden) {
173         pincodeKeys.hidden = false;
174         announceAccessibleMessage(
175             this.context.get(CONTEXT_KEY_KEYBOARD_LABEL, '') + ' ' + pincode +
176             ' ' + loadTimeData.getString('hidDetectionBTEnterKey'));
177       }
179       var entered = this.context.get(
180           CONTEXT_KEY_KEYBOARD_ENTERED_PART_PINCODE, 0);
182       // whether the functionality of getting num of entered keys is available.
183       var expected = this.context.get(
184           CONTEXT_KEY_KEYBOARD_ENTERED_PART_EXPECTED, false);
186       if (pincode.length != PINCODE_LENGTH)
187         console.error('Wrong pincode length');
189       // Pincode keys plus Enter key.
190       for (var i = 0; i < (PINCODE_LENGTH + 1); i++) {
191         var pincodeSymbol = $('hid-keyboard-pincode-sym-' + (i + 1));
192         pincodeSymbol.classList.toggle('key-typed', i < entered && expected);
193         pincodeSymbol.classList.toggle('key-untyped', i > entered && expected);
194         pincodeSymbol.classList.toggle('key-next', i == entered && expected);
195         if (i < PINCODE_LENGTH)
196           pincodeSymbol.textContent = pincode[i] ? pincode[i] : '';
197       }
198     },
200      /*
201      * Event handler that is invoked just before the screen in shown.
202      * @param {Object} data Screen init payload.
203      */
204     onBeforeShow: function(data) {
205       this.setDeviceBlockState_('hid-mouse-block', this.CONNECTION.SEARCHING);
206       this.setDeviceBlockState_('hid-keyboard-block',
207                                 this.CONNECTION.SEARCHING);
208     },
209   };