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'; |
| 15 | |
| 16 // The uint32 value of the selected device class. | |
| 13 this.classValue = 0; | 17 this.classValue = 0; |
|
xiyuan
2015/07/31 15:51:38
class 'Computer' value is 0x104 in the |deviceClas
rfrappier
2015/07/31 16:38:53
Yes, we should.
Done.
| |
| 14 this.isPairable = true; | 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; | |
| 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 var selectedClass = | |
| 115 this.getTextForDeviceClass(devices[i]['classValue']); | |
|
oshima
2015/07/31 14:54:55
just assign to devices[i].class
rfrappier
2015/07/31 15:45:09
At this point, each device object does not have a
rfrappier
2015/07/31 15:47:34
Correction: device_emulator_message_handler.cc:288
xiyuan
2015/07/31 15:51:38
I think oshima means get rid of |selectedClass| si
rfrappier
2015/07/31 16:38:53
Oh, okay.
Done.
| |
| 116 devices[i].class = selectedClass; | |
| 117 deviceList.push(devices[i]); | |
| 118 this.devicePaths[devices[i]['path']] = true; | |
| 119 } | |
| 120 | |
| 121 this.devices = deviceList; | |
| 122 }, | |
| 123 | |
| 81 pairDevice: function(e) { | 124 pairDevice: function(e) { |
| 82 var device = this.devices[e.path[2].dataIndex]; | 125 var device = this.devices[e.path[2].dataIndex]; |
| 83 device.classValue = this.getValueForDeviceClass(device.class); | 126 device.classValue = this.getValueForDeviceClass(device.class); |
| 127 this.devicePaths[device.path] = true; | |
| 84 | 128 |
| 85 // Send device info to the WebUI. | 129 // Send device info to the WebUI. |
| 86 chrome.send('requestBluetoothPair', [device]); | 130 chrome.send('requestBluetoothPair', [device]); |
| 87 }, | 131 }, |
| 88 | 132 |
| 89 discoverDevice: function(e) { | 133 discoverDevice: function(e) { |
| 90 var device = this.devices[e.path[2].dataIndex]; | 134 var device = this.devices[e.path[2].dataIndex]; |
| 91 device.classValue = this.getValueForDeviceClass(device.class); | 135 device.classValue = this.getValueForDeviceClass(device.class); |
| 136 this.devicePaths[device.path] = true; | |
| 92 | 137 |
| 93 // Send device info to WebUI. | 138 // Send device info to WebUI. |
| 94 chrome.send('requestBluetoothDiscover', [device]); | 139 chrome.send('requestBluetoothDiscover', [device]); |
| 95 }, | 140 }, |
| 96 | 141 |
| 142 // Adds a new device with default settings to the list of devices. | |
| 97 appendNewDevice: function() { | 143 appendNewDevice: function() { |
| 98 this.push('devices', new BluetoothDevice()); | 144 this.push('devices', new BluetoothDevice()); |
| 99 }, | 145 }, |
| 100 | 146 |
| 101 getValueForDeviceClass: function(classValue) { | 147 /** |
| 148 * This is called when a new device is discovered by the main adapter. | |
| 149 * The device is only added to the view's list if it is not already in | |
| 150 * the list (i.e. its path has not yet been recorded in |devicePaths|). | |
| 151 * @param {BluetoothDevice} device A bluetooth device. | |
| 152 */ | |
| 153 addBluetoothDevice: function(device) { | |
| 154 if (this.devicePaths[device['path']] != undefined) | |
| 155 return; | |
| 156 | |
| 157 var selectedClass = this.getTextForDeviceClass(device['classValue']); | |
|
xiyuan
2015/07/31 15:51:38
similarly here
rfrappier
2015/07/31 16:38:53
Done.
| |
| 158 device.class = selectedClass; | |
| 159 this.push('devices', device); | |
| 160 this.devicePaths[device['path']] = true; | |
| 161 }, | |
| 162 | |
| 163 /** | |
| 164 * Removes the bluetooth device with path |path|. | |
| 165 * @param {string} path A bluetooth device's path. | |
| 166 */ | |
| 167 removeBluetoothDevice: function(path) { | |
| 168 if (this.devicePaths[path] == undefined) | |
| 169 return; | |
| 170 | |
| 171 var foundIndex = -1; | |
| 172 for (var i = 0; i < this.devices.length; ++i) { | |
| 173 if (this.devices[i].path == path) { | |
| 174 foundIndex = i; | |
| 175 break; | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 this.splice('devices', foundIndex, 1); | |
|
oshima
2015/07/31 14:54:55
Move this inside if block above and remove foundIn
rfrappier
2015/07/31 15:45:09
Done.
| |
| 180 }, | |
| 181 | |
| 182 /** | |
| 183 * Returns the text for the label that corresponds to |classValue|. | |
| 184 * @param {number} classValue A number representing the bluetooth class | |
| 185 * of a device. | |
|
xiyuan
2015/07/31 15:51:38
nit: 4 more spaces indent,
* @param {number} clas
rfrappier
2015/07/31 16:38:53
Done.
| |
| 186 * @return {string} The label which represents |classValue|. | |
| 187 */ | |
| 188 getTextForDeviceClass: function(classValue) { | |
| 102 for (var i = 0; i < this.deviceClassOptions.length; ++i) { | 189 for (var i = 0; i < this.deviceClassOptions.length; ++i) { |
| 103 if (this.deviceClassOptions[i].text == classValue) | 190 if (this.deviceClassOptions[i].value == classValue) |
| 191 return this.deviceClassOptions[i].text; | |
| 192 } | |
| 193 }, | |
| 194 | |
| 195 /** | |
| 196 * Returns the integer value which corresponds with the label |classText|. | |
| 197 * @param {string} classText The label for a device class option. | |
| 198 * @return {number} The value which |classText| represents. | |
| 199 */ | |
| 200 getValueForDeviceClass: function(classText) { | |
| 201 for (var i = 0; i < this.deviceClassOptions.length; ++i) { | |
| 202 if (this.deviceClassOptions[i].text == classText) | |
| 104 return this.deviceClassOptions[i].value; | 203 return this.deviceClassOptions[i].value; |
| 105 } | 204 } |
| 106 return 0; | 205 return 0; |
| 107 }, | 206 }, |
| 108 }); | 207 }); |
| OLD | NEW |