Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * A bluetooth device. | 6 * A bluetooth device. |
| 7 * @constructor | 7 * @constructor |
| 8 */ | 8 */ |
| 9 var BluetoothDevice = function() { | 9 var BluetoothDevice = function() { |
| 10 this.address = ''; | 10 this.address = ''; |
| 11 this.alias = ''; | 11 this.alias = ''; |
| 12 | |
| 13 // The text label of the selected device class. | |
| 12 this.class = 'Computer'; | 14 this.class = 'Computer'; |
| 13 this.classValue = 0; | 15 |
| 14 this.isPairable = true; | 16 // The uint32 value of the selected device class. |
| 17 this.classValue = 0x104; | |
| 18 | |
| 15 this.isTrusted = true; | 19 this.isTrusted = true; |
| 16 this.name = ''; | 20 this.name = ''; |
| 17 this.path = ''; | 21 this.path = ''; |
| 18 this.pairMethod = 'None'; | 22 |
| 23 // The label of the selected pairing method option. | |
| 24 this.pairingMethod = 'None'; | |
| 25 | |
| 26 // The text containing a PIN key or passkey for pairing. | |
| 27 this.pairingAuthToken = ''; | |
| 19 }; | 28 }; |
| 20 | 29 |
| 21 Polymer({ | 30 Polymer({ |
| 22 is: 'bluetooth-settings', | 31 is: 'bluetooth-settings', |
| 23 | 32 |
| 24 properties: { | 33 properties: { |
| 25 /** | 34 /** |
| 26 * The title to be displayed in a heading element for the element. | 35 * The title to be displayed in a heading element for the element. |
| 27 */ | 36 */ |
| 28 title: { | 37 title: { |
| 29 type: String | 38 type: String |
| 30 }, | 39 }, |
| 31 | 40 |
| 32 /** | 41 /** |
| 33 * Indicates whether or not the main bluetooth adapter is turned on. | |
| 34 */ | |
| 35 powerToMainAdapter: { | |
| 36 type: Boolean | |
| 37 }, | |
| 38 | |
| 39 /** | |
| 40 * A set of bluetooth devices. | 42 * A set of bluetooth devices. |
| 41 * @type !Array<!BluetoothDevice> | 43 * @type !Array<!BluetoothDevice> |
| 42 */ | 44 */ |
| 43 devices: { | 45 devices: { |
| 44 type: Array, | 46 type: Array, |
| 45 value: function() { return []; } | 47 value: function() { return []; } |
| 46 }, | 48 }, |
| 47 | 49 |
| 48 /** | 50 /** |
| 49 * A set of options for the possible bluetooth device classes/types. | 51 * A set of options for the possible bluetooth device classes/types. |
| 50 * Object |value| attribute comes from values in the WebUI, set in | 52 * Object |value| attribute comes from values in the WebUI, set in |
| 51 * setDeviceClassOptions. | 53 * setDeviceClassOptions. |
| 52 * @type !Array<! { text: string, value: int }} > | 54 * @type !Array<! {text: string, value: int} > |
| 53 */ | 55 */ |
| 54 deviceClassOptions: { | 56 deviceClassOptions: { |
| 55 type: Array, | 57 type: Array, |
| 56 value: function() { | 58 value: function() { |
| 57 return [{ text: 'Unknown', value: 0 }, | 59 return [{ text: 'Unknown', value: 0 }, |
| 58 { text: 'Mouse', value: 0x2580 }, | 60 { text: 'Mouse', value: 0x2580 }, |
| 59 { text: 'Keyboard', value: 0x2540 }, | 61 { text: 'Keyboard', value: 0x2540 }, |
| 60 { text: 'Audio', value: 0x240408 }, | 62 { text: 'Audio', value: 0x240408 }, |
| 61 { text: 'Phone', value: 0x7a020c }, | 63 { text: 'Phone', value: 0x7a020c }, |
| 62 { text: 'Computer', value: 0x100 }]; | 64 { text: 'Computer', value: 0x104 }]; |
| 63 } | 65 } |
| 64 }, | 66 }, |
| 65 | 67 |
| 66 /** | 68 /** |
| 67 * A set of strings representing the method to be used for | 69 * A set of strings representing the method to be used for |
| 68 * authenticating a device during a pair request. | 70 * authenticating a device during a pair request. |
| 69 * @type !Array<!String> | 71 * @type !Array<!string> |
| 70 */ | 72 */ |
| 71 deviceAuthenticationMethods: { | 73 deviceAuthenticationMethods: { |
| 72 type: Array, | 74 type: Array, |
| 73 value: function() { return ['None', 'Pin Code', 'Pass Key']; } | 75 value: function() { return ['None', 'PIN Code', 'PassKey']; } |
| 76 }, | |
| 77 | |
| 78 /** | |
| 79 * Contains keys for all the device paths which have been discovered. Used | |
| 80 * to look up whether or not a device is listed already. | |
| 81 * @type {Object} | |
| 82 */ | |
| 83 devicePaths: { | |
| 84 type: Object, | |
| 85 value: function() { return {}; } | |
| 74 }, | 86 }, |
| 75 }, | 87 }, |
| 76 | 88 |
| 77 ready: function() { | 89 ready: function() { |
| 78 this.title = 'Bluetooth Settings'; | 90 this.title = 'Bluetooth Settings'; |
| 79 }, | 91 }, |
| 80 | 92 |
| 93 /** | |
| 94 * Checks whether or not the PIN/passkey input field should be shown. | |
| 95 * It should only be shown when the pair method is not 'None' or empty. | |
| 96 * @param {string} pairMethod The label of the selected pair method option | |
| 97 * for a particular device. | |
| 98 * @return {boolean} Whether the PIN/passkey input field should be shown. | |
| 99 */ | |
| 100 showAuthToken: function(pairMethod) { | |
| 101 return pairMethod != 'None' && pairMethod != undefined && pairMethod != ''; | |
|
xiyuan
2015/07/31 20:09:47
nit: how about
return pairMethod && pairMethod !=
rfrappier
2015/07/31 22:43:03
Done.
| |
| 102 }, | |
| 103 | |
| 104 /** | |
| 105 * Called by the WebUI which provides a list of devices which are connected | |
| 106 * to the main adapter. | |
| 107 * @param {!Array<!BluetoothDevice>} devices A list of bluetooth devices. | |
| 108 */ | |
| 109 updateBluetoothInfo: function(devices) { | |
| 110 /** @type {!Array<!BluetoothDevice>} */ var deviceList = []; | |
| 111 | |
| 112 for (var i = 0; i < devices.length; ++i) { | |
| 113 // Get the label for the device class which should be selected. | |
| 114 devices[i].class = this.getTextForDeviceClass(devices[i]['classValue']); | |
| 115 deviceList.push(devices[i]); | |
| 116 this.devicePaths[devices[i]['path']] = true; | |
| 117 } | |
| 118 | |
| 119 this.devices = deviceList; | |
| 120 }, | |
| 121 | |
| 81 pairDevice: function(e) { | 122 pairDevice: function(e) { |
| 82 var device = this.devices[e.path[2].dataIndex]; | 123 var device = this.devices[e.path[2].dataIndex]; |
| 83 device.classValue = this.getValueForDeviceClass(device.class); | 124 device.classValue = this.getValueForDeviceClass(device.class); |
| 125 this.devicePaths[device.path] = true; | |
| 84 | 126 |
| 85 // Send device info to the WebUI. | 127 // Send device info to the WebUI. |
| 86 chrome.send('requestBluetoothPair', [device]); | 128 chrome.send('requestBluetoothPair', [device]); |
| 87 }, | 129 }, |
| 88 | 130 |
| 89 discoverDevice: function(e) { | 131 discoverDevice: function(e) { |
| 90 var device = this.devices[e.path[2].dataIndex]; | 132 var device = this.devices[e.path[2].dataIndex]; |
| 91 device.classValue = this.getValueForDeviceClass(device.class); | 133 device.classValue = this.getValueForDeviceClass(device.class); |
| 134 this.devicePaths[device.path] = true; | |
| 92 | 135 |
| 93 // Send device info to WebUI. | 136 // Send device info to WebUI. |
| 94 chrome.send('requestBluetoothDiscover', [device]); | 137 chrome.send('requestBluetoothDiscover', [device]); |
| 95 }, | 138 }, |
| 96 | 139 |
| 140 // Adds a new device with default settings to the list of devices. | |
| 97 appendNewDevice: function() { | 141 appendNewDevice: function() { |
| 98 this.push('devices', new BluetoothDevice()); | 142 this.push('devices', new BluetoothDevice()); |
| 99 }, | 143 }, |
| 100 | 144 |
| 101 getValueForDeviceClass: function(classValue) { | 145 /** |
| 146 * This is called when a new device is discovered by the main adapter. | |
| 147 * The device is only added to the view's list if it is not already in | |
| 148 * the list (i.e. its path has not yet been recorded in |devicePaths|). | |
| 149 * @param {BluetoothDevice} device A bluetooth device. | |
| 150 */ | |
| 151 addBluetoothDevice: function(device) { | |
| 152 if (this.devicePaths[device['path']] != undefined) | |
| 153 return; | |
| 154 | |
| 155 device.class = this.getTextForDeviceClass(device['classValue']); | |
| 156 this.push('devices', device); | |
| 157 this.devicePaths[device['path']] = true; | |
| 158 }, | |
| 159 | |
| 160 /** | |
| 161 * Removes the bluetooth device with path |path|. | |
| 162 * @param {string} path A bluetooth device's path. | |
| 163 */ | |
| 164 removeBluetoothDevice: function(path) { | |
| 165 if (this.devicePaths[path] == undefined) | |
| 166 return; | |
| 167 | |
| 168 for (var i = 0; i < this.devices.length; ++i) { | |
| 169 if (this.devices[i].path == path) { | |
| 170 this.splice('devices', i, 1); | |
| 171 break; | |
| 172 } | |
| 173 } | |
| 174 }, | |
| 175 | |
| 176 /** | |
| 177 * Returns the text for the label that corresponds to |classValue|. | |
| 178 * @param {number} classValue A number representing the bluetooth class | |
| 179 * of a device. | |
| 180 * @return {string} The label which represents |classValue|. | |
| 181 */ | |
| 182 getTextForDeviceClass: function(classValue) { | |
| 102 for (var i = 0; i < this.deviceClassOptions.length; ++i) { | 183 for (var i = 0; i < this.deviceClassOptions.length; ++i) { |
| 103 if (this.deviceClassOptions[i].text == classValue) | 184 if (this.deviceClassOptions[i].value == classValue) |
| 185 return this.deviceClassOptions[i].text; | |
| 186 } | |
| 187 }, | |
| 188 | |
| 189 /** | |
| 190 * Returns the integer value which corresponds with the label |classText|. | |
| 191 * @param {string} classText The label for a device class option. | |
| 192 * @return {number} The value which |classText| represents. | |
| 193 */ | |
| 194 getValueForDeviceClass: function(classText) { | |
| 195 for (var i = 0; i < this.deviceClassOptions.length; ++i) { | |
| 196 if (this.deviceClassOptions[i].text == classText) | |
| 104 return this.deviceClassOptions[i].value; | 197 return this.deviceClassOptions[i].value; |
| 105 } | 198 } |
| 106 return 0; | 199 return 0; |
| 107 }, | 200 }, |
| 108 }); | 201 }); |
| OLD | NEW |