Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(593)

Side by Side Diff: chrome/browser/resources/bluetooth_internals/device_collection.js

Issue 2448713002: bluetooth: Add Device connection logic and accompanying user interface. (Closed)
Patch Set: Fix comments, add ConnectionStatus enum, other fixes Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 * Javascript for DeviceCollection, served from 6 * Javascript for DeviceCollection, served from
7 * chrome://bluetooth-internals/. 7 * chrome://bluetooth-internals/.
8 */ 8 */
9 9
10 cr.define('device_collection', function() { 10 cr.define('device_collection', function() {
11 /**
12 * Enum of connection status for a device. Used for
13 * DeviceCollection.updateConnectionStatus which sets the connectionStatus
14 * on the DeviceInfo object. New DeviceInfo objects have a DISCONNECTED status
15 * by default.
dpapad 2016/11/10 22:33:20 @enum {number}
mbrunson 2016/11/11 21:26:14 Done.
16 */
17 var ConnectionStatus = {
18 DISCONNECTED: 0,
19 CONNECTING: 1,
20 CONNECTED: 2,
21 };
22
11 /* 23 /*
12 * Collection of devices. Extends ArrayDataModel which provides a set of 24 * Collection of devices. Extends ArrayDataModel which provides a set of
13 * functions and events that notifies observers when the collection changes. 25 * functions and events that notifies observers when the collection changes.
14 * @constructor 26 * @constructor
15 * @param {!Array<device_collection.Device>} array The starting collection of 27 * @param {!Array<!device_collection.DeviceInfo>} array The starting
16 * devices. 28 * collection of devices.
17 * @extends {cr.ui.ArrayDataModel} 29 * @extends {cr.ui.ArrayDataModel}
18 */ 30 */
19 var DeviceCollection = function(array) { 31 var DeviceCollection = function(array) {
20 cr.ui.ArrayDataModel.call(this, array); 32 cr.ui.ArrayDataModel.call(this, array);
21 }; 33 };
22 DeviceCollection.prototype = { 34 DeviceCollection.prototype = {
23 __proto__: cr.ui.ArrayDataModel.prototype, 35 __proto__: cr.ui.ArrayDataModel.prototype,
24 36
25 /** 37 /**
26 * Finds the Device in the collection with the matching address. 38 * Finds the Device in the collection with the matching address.
27 * @param {string} address 39 * @param {string} address
28 */ 40 */
29 getByAddress: function(address) { 41 getByAddress: function(address) {
30 for (var i = 0; i < this.length; i++) { 42 for (var i = 0; i < this.length; i++) {
31 var device = this.item(i); 43 var device = this.item(i);
32 if (address == device.info.address) 44 if (address == device.address)
33 return device; 45 return device;
34 } 46 }
35 return null; 47 return null;
36 }, 48 },
37 49
38 /** 50 /**
39 * Adds or updates a Device with new DeviceInfo. 51 * Adds or updates a Device with new DeviceInfo.
40 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo 52 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo
41 */ 53 */
42 addOrUpdate: function(deviceInfo) { 54 addOrUpdate: function(deviceInfo) {
43 var oldDevice = this.getByAddress(deviceInfo.address); 55 deviceInfo.removed = false;
44 if (oldDevice) { 56 var oldDeviceInfo = this.getByAddress(deviceInfo.address);
57
58 if (oldDeviceInfo) {
45 // Update rssi if it's valid 59 // Update rssi if it's valid
46 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || 60 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) ||
47 (oldDevice.info.rssi && oldDevice.info.rssi.value); 61 (oldDeviceInfo.rssi && oldDeviceInfo.rssi.value);
48 62
49 oldDevice.info = deviceInfo; 63 // The connectionStatus and connectionMessage properties may not exist
50 oldDevice.info.rssi = { value: rssi }; 64 // on |deviceInfo|. The rssi property may be null, so it must be
51 oldDevice.removed = false; 65 // re-assigned.
52 66 Object.assign(oldDeviceInfo, deviceInfo);
53 this.updateIndex(this.indexOf(oldDevice)); 67 oldDeviceInfo.rssi = { value: rssi };
68 this.updateIndex(this.indexOf(oldDeviceInfo));
54 } else { 69 } else {
55 this.push(new Device(deviceInfo)); 70 deviceInfo.connectionStatus = ConnectionStatus.DISCONNECTED;
71 this.push(deviceInfo);
56 } 72 }
57 }, 73 },
58 74
59 /** 75 /**
60 * Marks the Device as removed. 76 * Marks the Device as removed.
61 * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo 77 * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo
62 */ 78 */
63 remove: function(deviceInfo) { 79 remove: function(deviceInfo) {
64 var device = this.getByAddress(deviceInfo.address); 80 var device = this.getByAddress(deviceInfo.address);
65 assert(device, 'Device does not exist.'); 81 assert(device, 'Device does not exist.');
66 device.removed = true; 82 device.removed = true;
67 this.updateIndex(this.indexOf(device)); 83 this.updateIndex(this.indexOf(device));
68 } 84 },
69 };
70 85
71 /* 86 /**
72 * Data model for a cached device. 87 * Updates the device connection status.
73 * @constructor 88 * @param {string} address The address of the device.
74 * @param {!interfaces.BluetoothDevice.DeviceInfo} info 89 * @param {number} status .
75 */ 90 * @param {?Error} opt_error Optional Error from connection.
76 var Device = function(info) { 91 */
77 this.info = info; 92 updateConnectionStatus: function(address, status, opt_error) {
78 this.removed = false; 93 var message = (opt_error && opt_error.message) || '';
94
95 var device = assert(this.getByAddress(address), 'Device does not exist');
96 device.connectionStatus = status;
97
98 // TODO(crbug.com/663830): Replace connection error column with better
99 // notification system.
100 device.connectionMessage = message;
101 this.updateIndex(this.indexOf(device));
102 },
79 }; 103 };
80 104
81 return { 105 return {
82 Device: Device, 106 ConnectionStatus: ConnectionStatus,
83 DeviceCollection: DeviceCollection, 107 DeviceCollection: DeviceCollection,
84 }; 108 };
85 }); 109 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698