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

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: Remove binding variable in Device.Create 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.
16 * @enum {number}
17 */
18 var ConnectionStatus = {
19 DISCONNECTED: 0,
20 CONNECTING: 1,
21 CONNECTED: 2,
22 };
23
11 /* 24 /*
12 * Collection of devices. Extends ArrayDataModel which provides a set of 25 * Collection of devices. Extends ArrayDataModel which provides a set of
13 * functions and events that notifies observers when the collection changes. 26 * functions and events that notifies observers when the collection changes.
14 * @constructor 27 * @constructor
15 * @param {!Array<device_collection.Device>} array The starting collection of 28 * @param {!Array<!device_collection.DeviceInfo>} array The starting
16 * devices. 29 * collection of devices.
17 * @extends {cr.ui.ArrayDataModel} 30 * @extends {cr.ui.ArrayDataModel}
18 */ 31 */
19 var DeviceCollection = function(array) { 32 var DeviceCollection = function(array) {
20 cr.ui.ArrayDataModel.call(this, array); 33 cr.ui.ArrayDataModel.call(this, array);
21 }; 34 };
22 DeviceCollection.prototype = { 35 DeviceCollection.prototype = {
23 __proto__: cr.ui.ArrayDataModel.prototype, 36 __proto__: cr.ui.ArrayDataModel.prototype,
24 37
25 /** 38 /**
26 * Finds the Device in the collection with the matching address. 39 * Finds the Device in the collection with the matching address.
27 * @param {string} address 40 * @param {string} address
28 */ 41 */
29 getByAddress: function(address) { 42 getByAddress: function(address) {
30 for (var i = 0; i < this.length; i++) { 43 for (var i = 0; i < this.length; i++) {
31 var device = this.item(i); 44 var device = this.item(i);
32 if (address == device.info.address) 45 if (address == device.address)
33 return device; 46 return device;
34 } 47 }
35 return null; 48 return null;
36 }, 49 },
37 50
38 /** 51 /**
39 * Adds or updates a Device with new DeviceInfo. 52 * Adds or updates a Device with new DeviceInfo.
40 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo 53 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo
41 */ 54 */
42 addOrUpdate: function(deviceInfo) { 55 addOrUpdate: function(deviceInfo) {
43 var oldDevice = this.getByAddress(deviceInfo.address); 56 deviceInfo.removed = false;
44 if (oldDevice) { 57 var oldDeviceInfo = this.getByAddress(deviceInfo.address);
58
59 if (oldDeviceInfo) {
45 // Update rssi if it's valid 60 // Update rssi if it's valid
46 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || 61 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) ||
47 (oldDevice.info.rssi && oldDevice.info.rssi.value); 62 (oldDeviceInfo.rssi && oldDeviceInfo.rssi.value);
48 63
49 oldDevice.info = deviceInfo; 64 // The connectionStatus and connectionMessage properties may not exist
50 oldDevice.info.rssi = { value: rssi }; 65 // on |deviceInfo|. The rssi property may be null, so it must be
51 oldDevice.removed = false; 66 // re-assigned.
52 67 Object.assign(oldDeviceInfo, deviceInfo);
53 this.updateIndex(this.indexOf(oldDevice)); 68 oldDeviceInfo.rssi = { value: rssi };
69 this.updateIndex(this.indexOf(oldDeviceInfo));
54 } else { 70 } else {
55 this.push(new Device(deviceInfo)); 71 deviceInfo.connectionStatus = ConnectionStatus.DISCONNECTED;
72 this.push(deviceInfo);
56 } 73 }
57 }, 74 },
58 75
59 /** 76 /**
60 * Marks the Device as removed. 77 * Marks the Device as removed.
61 * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo 78 * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo
62 */ 79 */
63 remove: function(deviceInfo) { 80 remove: function(deviceInfo) {
64 var device = this.getByAddress(deviceInfo.address); 81 var device = this.getByAddress(deviceInfo.address);
65 assert(device, 'Device does not exist.'); 82 assert(device, 'Device does not exist.');
66 device.removed = true; 83 device.removed = true;
67 this.updateIndex(this.indexOf(device)); 84 this.updateIndex(this.indexOf(device));
68 } 85 },
69 };
70 86
71 /* 87 /**
72 * Data model for a cached device. 88 * Updates the device connection status.
73 * @constructor 89 * @param {string} address The address of the device.
74 * @param {!interfaces.BluetoothDevice.DeviceInfo} info 90 * @param {number} status .
75 */ 91 * @param {?Error} opt_error Optional Error from connection.
76 var Device = function(info) { 92 */
77 this.info = info; 93 updateConnectionStatus: function(address, status, opt_error) {
78 this.removed = false; 94 var message = (opt_error && opt_error.message) || '';
95
96 var device = assert(this.getByAddress(address), 'Device does not exist');
97 device.connectionStatus = status;
98
99 // TODO(crbug.com/663830): Replace connection error column with better
100 // notification system.
101 device.connectionMessage = message;
102 this.updateIndex(this.indexOf(device));
103 },
79 }; 104 };
80 105
81 return { 106 return {
82 Device: Device, 107 ConnectionStatus: ConnectionStatus,
83 DeviceCollection: DeviceCollection, 108 DeviceCollection: DeviceCollection,
84 }; 109 };
85 }); 110 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698