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

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 naming, merge upstream 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 /* 11 /*
12 * Collection of devices. Extends ArrayDataModel which provides a set of 12 * Collection of devices. Extends ArrayDataModel which provides a set of
13 * functions and events that notifies observers when the collection changes. 13 * functions and events that notifies observers when the collection changes.
14 * @constructor 14 * @constructor
15 * @param {!Array<device_collection.Device>} array The starting collection of 15 * @param {!Array<device_collection.DeviceInfo>} array The starting collection
dpapad 2016/11/10 19:05:02 I am guessing that the array should not hold null
mbrunson 2016/11/10 22:02:22 Done.
16 * devices. 16 * of devices.
17 * @extends {cr.ui.ArrayDataModel} 17 * @extends {cr.ui.ArrayDataModel}
18 */ 18 */
19 var DeviceCollection = function(array) { 19 var DeviceCollection = function(array) {
20 cr.ui.ArrayDataModel.call(this, array); 20 cr.ui.ArrayDataModel.call(this, array);
21 }; 21 };
22 DeviceCollection.prototype = { 22 DeviceCollection.prototype = {
23 __proto__: cr.ui.ArrayDataModel.prototype, 23 __proto__: cr.ui.ArrayDataModel.prototype,
24 24
25 /** 25 /**
26 * Finds the Device in the collection with the matching address. 26 * Finds the Device in the collection with the matching address.
27 * @param {string} address 27 * @param {string} address
28 */ 28 */
29 getByAddress: function(address) { 29 getByAddress: function(address) {
30 for (var i = 0; i < this.length; i++) { 30 for (var i = 0; i < this.length; i++) {
31 var device = this.item(i); 31 var device = this.item(i);
32 if (address == device.info.address) 32 if (address == device.address)
33 return device; 33 return device;
34 } 34 }
35 return null; 35 return null;
36 }, 36 },
37 37
38 /** 38 /**
39 * Adds or updates a Device with new DeviceInfo. 39 * Adds or updates a Device with new DeviceInfo.
40 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo 40 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo
41 */ 41 */
42 addOrUpdate: function(deviceInfo) { 42 addOrUpdate: function(deviceInfo) {
43 var oldDevice = this.getByAddress(deviceInfo.address); 43 deviceInfo.removed = false;
44 if (oldDevice) { 44 var oldDeviceInfo = this.getByAddress(deviceInfo.address);
45
46 if (oldDeviceInfo) {
45 // Update rssi if it's valid 47 // Update rssi if it's valid
46 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || 48 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) ||
47 (oldDevice.info.rssi && oldDevice.info.rssi.value); 49 (oldDeviceInfo.rssi && oldDeviceInfo.rssi.value);
48 50
49 oldDevice.info = deviceInfo; 51 Object.assign(oldDeviceInfo, deviceInfo);
dpapad 2016/11/10 19:05:02 Are all properties of oldDeviceInfo overwritten by
mbrunson 2016/11/10 22:02:22 Yes. deviceInfo.rssi may be null while oldDeviceIn
50 oldDevice.info.rssi = { value: rssi }; 52 oldDeviceInfo.rssi = { value: rssi };
51 oldDevice.removed = false; 53 this.updateIndex(this.indexOf(oldDeviceInfo));
52
53 this.updateIndex(this.indexOf(oldDevice));
54 } else { 54 } else {
55 this.push(new Device(deviceInfo)); 55 deviceInfo.connectionStatus = 'disconnected';
56 this.push(deviceInfo);
56 } 57 }
57 }, 58 },
58 59
59 /** 60 /**
60 * Marks the Device as removed. 61 * Marks the Device as removed.
61 * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo 62 * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo
62 */ 63 */
63 remove: function(deviceInfo) { 64 remove: function(deviceInfo) {
64 var device = this.getByAddress(deviceInfo.address); 65 var device = this.getByAddress(deviceInfo.address);
65 assert(device, 'Device does not exist.'); 66 assert(device, 'Device does not exist.');
66 device.removed = true; 67 device.removed = true;
67 this.updateIndex(this.indexOf(device)); 68 this.updateIndex(this.indexOf(device));
68 } 69 },
69 };
70 70
71 /* 71 /**
72 * Data model for a cached device. 72 * Updates the device connection status.
73 * @constructor 73 * @param {string} address The address of the device.
74 * @param {!interfaces.BluetoothDevice.DeviceInfo} info 74 * @param {string} status Disconnected, connecting, or connected string.
75 */ 75 * @param {Error} optional_error Optional Error object.
dpapad 2016/11/10 19:05:02 Please follow styleguide for naming optional param
mbrunson 2016/11/10 22:02:22 Done.
76 var Device = function(info) { 76 */
77 this.info = info; 77 updateConnectionStatus: function(address, status, optional_error) {
78 this.removed = false; 78 var message = (optional_error && optional_error.message) || '';
79
80 var device = this.getByAddress(address);
dpapad 2016/11/10 19:05:02 Nit (optional): You can also var device = assert(t
mbrunson 2016/11/10 22:02:22 Oh. That's cool! Done.
81 assert(device, 'Device does not exist');
82 device.connectionStatus = status;
83
84 // TODO(crbug.com/663830): Replace connection error column with better
85 // notification system.
86 device.connectionMessage = message;
87 this.updateIndex(this.indexOf(device));
88 },
79 }; 89 };
80 90
81 return { 91 return {
82 Device: Device,
83 DeviceCollection: DeviceCollection, 92 DeviceCollection: DeviceCollection,
84 }; 93 };
85 }); 94 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698