OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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. | |
4 | |
5 cr.define('options', function() { | |
6 const OptionsPage = options.OptionsPage; | |
7 | |
8 /** | |
9 * Enumeration of possible states during pairing. The value associated with | |
10 * each state maps to a localized string in the global variable | |
11 * 'templateData'. | |
12 * @enum {string} | |
13 */ | |
14 var PAIRING = { | |
15 CONFIRM_PASSKEY: 'bluetoothConfirmPasskey', | |
16 ENTER_PASSKEY: 'bluetoothEnterPasskey', | |
17 REMOTE_PASSKEY: 'bluetoothRemotePasskey', | |
18 ERROR_NO_DEVICE: 'bluetoothErrorNoDevice', | |
19 ERROR_INCORRECT_PIN: 'bluetoothErrorIncorrectPin', | |
20 ERROR_CONNECTION_TIMEOUT: 'bluetoothErrorTimeout', | |
21 ERROR_CONNECTION_FAILED: 'bluetoothErrorConnectionFailed' | |
22 }; | |
23 | |
24 /** | |
25 * List of IDs for conditionally visible elements in the dialog. | |
26 * @type {Array.<String>} | |
27 * @const | |
28 */ | |
29 var ELEMENTS = ['bluetooth-pairing-passkey-display', | |
30 'bluetooth-pairing-passkey-entry', | |
31 'bluetooth-pair-device-connect-button', | |
32 'bluetooth-pair-device-cancel-button', | |
33 'bluetooth-pair-device-accept-button', | |
34 'bluetooth-pair-device-reject-button', | |
35 'bluetooth-pair-device-dismiss-button']; | |
36 | |
37 /** | |
38 * Encapsulated handling of the Bluetooth device pairing page. | |
39 * @constructor | |
40 */ | |
41 function BluetoothPairing() { | |
42 OptionsPage.call(this, | |
43 'bluetoothPairing', | |
44 templateData.bluetoothOptionsPageTabTitle, | |
45 'bluetooth-pairing'); | |
46 } | |
47 | |
48 cr.addSingletonGetter(BluetoothPairing); | |
49 | |
50 BluetoothPairing.prototype = { | |
51 __proto__: OptionsPage.prototype, | |
52 | |
53 /** | |
54 * Description of the bluetooth device. | |
55 * @type {{name: string, | |
56 * address: string, | |
57 * icon: Constants.DEVICE_TYPE, | |
58 * paired: boolean, | |
59 * connected: boolean, | |
60 * pairing: string|undefined, | |
61 * passkey: number|undefined, | |
62 * entered: number|undefined}} | |
63 * @private. | |
64 */ | |
65 device_: null, | |
66 | |
67 /** @inheritDoc */ | |
68 initializePage: function() { | |
69 OptionsPage.prototype.initializePage.call(this); | |
70 var self = this; | |
71 var cancel = function() { | |
72 chrome.send('updateBluetoothDevice', | |
73 [self.device_.address, 'cancel']); | |
74 OptionsPage.closeOverlay(); | |
75 }; | |
76 var connect = function() { | |
77 var args = [self.device_.address, 'connect']; | |
78 var passkey = self.device_.passkey; | |
79 if (!passkey && !$('bluetooth-pairing-passkey-entry').hidden) | |
80 passkey = $('bluetooth-passkey').value; | |
81 if (passkey) | |
82 args.push(String(passkey)); | |
83 chrome.send('updateBluetoothDevice', args); | |
84 OptionsPage.closeOverlay(); | |
85 }; | |
86 $('bluetooth-pair-device-cancel-button').onclick = cancel; | |
87 $('bluetooth-pair-device-reject-button').onclick = cancel; | |
88 $('bluetooth-pair-device-connect-button').onclick = connect; | |
89 $('bluetooth-pair-device-accept-button').onclick = connect; | |
90 $('bluetooth-pair-device-dismiss-button').onclick = function() { | |
91 OptionsPage.closeOverlay(); | |
92 }; | |
93 $('bluetooth-passkey').oninput = function() { | |
94 $('bluetooth-pair-device-connect-button').disabled = | |
95 $('bluetooth-passkey').value.length == 0; | |
96 } | |
97 }, | |
98 | |
99 /** | |
100 * Override to prevent showing the overlay if the Bluetooth device details | |
101 * have not been specified. Prevents showing an empty dialog if the user | |
102 * quits and restarts Chrome while in the process of pairing with a device. | |
103 " @return {boolean} True if the overlay can be displayed. | |
104 */ | |
105 canShowPage: function() { | |
106 return this.device_ && this.device_.address && this.device_.pairing; | |
107 }, | |
108 | |
109 /** | |
110 * Configures the overlay for pairing a device. | |
111 * @param {Object} device Description of the bluetooth device. | |
112 */ | |
113 update: function(device) { | |
114 this.device_ = {}; | |
115 for (key in device) | |
116 this.device_[key] = device[key]; | |
117 // Update the pairing instructions. | |
118 var instructionsEl = $('bluetooth-pairing-instructions'); | |
119 this.clearElement_(instructionsEl); | |
120 | |
121 var message = templateData[device.pairing]; | |
122 message = message.replace('%1', this.device_.name); | |
123 instructionsEl.textContent = message; | |
124 | |
125 // Update visibility of dialog elements. | |
126 if (this.device_.passkey) { | |
127 this.updatePasskey_(); | |
128 if (this.device_.pairing == PAIRING.CONFIRM_PASSKEY) { | |
129 // Confirming a match between displayed passkeys. | |
130 this.displayElements_(['bluetooth-pairing-passkey-display', | |
131 'bluetooth-pair-device-accept-button', | |
132 'bluetooth-pair-device-reject-button']); | |
133 } else { | |
134 // Remote entering a passkey. | |
135 this.displayElements_(['bluetooth-pairing-passkey-display', | |
136 'bluetooth-pair-device-cancel-button']); | |
137 } | |
138 } else if (this.device_.pairing == PAIRING.ENTER_PASSKEY) { | |
139 // Prompting the user to enter a passkey. | |
140 this.displayElements_(['bluetooth-pairing-passkey-entry', | |
141 'bluetooth-pair-device-connect-button', | |
142 'bluetooth-pair-device-cancel-button']); | |
143 } else { | |
144 // Displaying an error message. | |
145 this.displayElements_(['bluetooth-pair-device-dismiss-button']); | |
146 } | |
147 $('bluetooth-pair-device-connect-button').disabled = | |
148 $('bluetooth-passkey').value.length == 0; | |
149 }, | |
150 | |
151 /** | |
152 * Updates the visibility of elements in the dialog. | |
153 * @param {Array.<string>} list List of conditionally visible elements that | |
154 * are to be made visible. | |
155 * @private | |
156 */ | |
157 displayElements_: function(list) { | |
158 var enabled = {}; | |
159 for (var i = 0; i < list.length; i++) { | |
160 var key = list[i]; | |
161 enabled[key] = true; | |
162 } | |
163 for (var i = 0; i < ELEMENTS.length; i++) { | |
164 var key = ELEMENTS[i]; | |
165 $(key).hidden = !enabled[key]; | |
166 } | |
167 }, | |
168 | |
169 /** | |
170 * Removes all children from an element. | |
171 * @param {!Element} element Target element to clear. | |
172 */ | |
173 clearElement_: function(element) { | |
174 var child = element.firstChild; | |
175 while (child) { | |
176 element.removeChild(child); | |
177 child = element.firstChild; | |
178 } | |
179 }, | |
180 | |
181 /** | |
182 * Formats an element for displaying the passkey. | |
183 * @return {Element} Element containing the passkey. | |
184 */ | |
185 updatePasskey_: function() { | |
186 var passkeyEl = $('bluetooth-pairing-passkey-display'); | |
187 var keyClass = this.device_.pairing == PAIRING.REMOTE_PASSKEY ? | |
188 'bluetooth-keyboard-button' : 'bluetooth-passkey-char'; | |
189 this.clearElement_(passkeyEl); | |
190 var key = String(this.device_.passkey); | |
191 var progress = this.device_.entered | 0; | |
192 for (var i = 0; i < key.length; i++) { | |
193 var keyEl = document.createElement('span'); | |
194 keyEl.textContent = key.charAt(i); | |
195 keyEl.className = keyClass; | |
196 if (i < progress) | |
197 keyEl.classList.add('key-typed'); | |
198 passkeyEl.appendChild(keyEl); | |
199 } | |
200 if (this.device_.pairing == PAIRING.REMOTE_PASSKEY) { | |
201 // Add enter key. | |
202 var label = templateData['bluetoothEnterKey']; | |
203 var keyEl = document.createElement('span'); | |
204 keyEl.textContent = label; | |
205 keyEl.className = keyClass; | |
206 keyEl.id = "bluetooth-enter-key"; | |
207 passkeyEl.appendChild(keyEl); | |
208 } | |
209 passkeyEl.hidden = false; | |
210 }, | |
211 }; | |
212 | |
213 /** | |
214 * Configures the device pairing instructions and displays the pairing | |
215 * overlay. | |
216 * @param {Object} device Description of the Bluetooth device. | |
217 */ | |
218 BluetoothPairing.showDialog = function(device) { | |
219 BluetoothPairing.getInstance().update(device); | |
220 OptionsPage.navigateToPage('bluetoothPairing'); | |
221 }; | |
222 | |
223 // Export | |
224 return { | |
225 BluetoothPairing: BluetoothPairing | |
226 }; | |
227 }); | |
OLD | NEW |