Chromium Code Reviews

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: Add pub/sub system for device events, various issue fixes Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
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() {
(...skipping 28 matching lines...)
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 var oldDevice = this.getByAddress(deviceInfo.address);
44 if (oldDevice) { 44 if (oldDevice) {
45 // Update rssi if it's valid 45 // Update rssi if it's valid
46 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || 46 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) ||
47 (oldDevice.info.rssi && oldDevice.info.rssi.value); 47 (oldDevice.info.rssi && oldDevice.info.rssi.value);
48 48
49 oldDevice.info = deviceInfo; 49 Object.assign(oldDevice.info, deviceInfo);
50 oldDevice.info.rssi = { value: rssi }; 50 oldDevice.info.rssi = { value: rssi };
51 oldDevice.removed = false; 51 oldDevice.removed = false;
52 52
53 this.updateIndex(this.indexOf(oldDevice)); 53 this.updateIndex(this.indexOf(oldDevice));
54 } else { 54 } else {
55 this.push(new Device(deviceInfo)); 55 this.push(new Device(deviceInfo));
56 } 56 }
57 }, 57 },
58 58
59 /** 59 /**
60 * Marks the Device as removed. 60 * Marks the Device as removed.
61 * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo 61 * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo
62 */ 62 */
63 remove: function(deviceInfo) { 63 remove: function(deviceInfo) {
64 var device = this.getByAddress(deviceInfo.address); 64 var device = this.getByAddress(deviceInfo.address);
65 assert(device, 'Device does not exist.'); 65 assert(device, 'Device does not exist.');
66
66 device.removed = true; 67 device.removed = true;
68 device.proxy = null;
67 this.updateIndex(this.indexOf(device)); 69 this.updateIndex(this.indexOf(device));
68 } 70 },
71
72 /**
73 *
74 */
75 updateConnectionStatus: function(index, optional_error) {
ortuno 2016/11/09 03:24:03 I would use the address instead. If I understand c
mbrunson 2016/11/09 23:39:37 Done.
76 var message = (optional_error && optional_error.message) || '';
ortuno 2016/11/09 03:24:03 I would save the information in the device object
mbrunson 2016/11/09 23:39:37 Done.
77 var event = new CustomEvent('connectstatus', {
78 detail: {
79 index: index,
80 message: message,
81 }
82 });
83 this.dispatchEvent(event);
84 },
69 }; 85 };
70 86
71 /* 87 /*
72 * Data model for a cached device. 88 * Data model for a cached device.
73 * @constructor 89 * @constructor
74 * @param {!interfaces.BluetoothDevice.DeviceInfo} info 90 * @param {!interfaces.BluetoothDevice.DeviceInfo} info
75 */ 91 */
76 var Device = function(info) { 92 var Device = function(info) {
77 this.info = info; 93 this.info = info;
78 this.removed = false; 94 this.removed = false;
79 }; 95 };
80 96
97 Device.prototype = {
ortuno 2016/11/09 03:24:03 Before this patch this class used to only hold inf
mbrunson 2016/11/09 23:39:37 Hmm ok. So in the final design, DeviceDetailsView
98 /**
99 * Creates a connection to this device and updates the service list.
100 * @return {Promise} rejects if connection failed, resolves otherwise.
101 */
102 connect: function() {
103 return adapter_broker.getAdapterBroker().then(function(broker) {
104 return broker.connectToDevice(this.info.address);
105 }.bind(this)).then(function(response) {
106 this.proxy = response.device;
107 }.bind(this));
108 },
109
110 /**
111 * Disconnects the device and removes the Device interface proxy.
112 */
113 disconnect: function() {
114 if (this.proxy) {
115 this.proxy.disconnect();
116 this.proxy = null;
117 }
118 },
119
120 getServices: function() {
121 return this.proxy.getServices().then(function(response) {
122 this.info.services = response.services;
123 return this.info.services;
124 }.bind(this));
125 }
126 };
127
81 return { 128 return {
82 Device: Device, 129 Device: Device,
83 DeviceCollection: DeviceCollection, 130 DeviceCollection: DeviceCollection,
84 }; 131 };
85 }); 132 });
OLDNEW

Powered by Google App Engine