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

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: More tests, more comments 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 Device-related features, served from 6 * Javascript for Device-related features, served from
7 * chrome://bluetooth-internals/. 7 * chrome://bluetooth-internals/.
8 */ 8 */
9
10 cr.define('device_collection', function() { 9 cr.define('device_collection', function() {
11 /* 10 /*
12 * Collection of devices. 11 * Collection of devices.
13 * @constructor 12 * @constructor
14 * @param {!Array} array the starting collection of devices. 13 * @param {!Array} array the starting collection of devices.
15 * @extends {cr.ui.ArrayDataModel} 14 * @extends {cr.ui.ArrayDataModel}
16 */ 15 */
17 var DeviceCollection = function(array) { 16 var DeviceCollection = function(array) {
18 cr.ui.ArrayDataModel.call(this, array); 17 cr.ui.ArrayDataModel.call(this, array);
19 }; 18 };
20 DeviceCollection.prototype = { 19 DeviceCollection.prototype = {
21 __proto__: cr.ui.ArrayDataModel.prototype, 20 __proto__: cr.ui.ArrayDataModel.prototype,
22 /** 21 /**
23 * Finds the Device in the collection with the matching address. 22 * Finds the Device in the collection with the matching address.
24 * @param {!string} address 23 * @param {!string} address
25 */ 24 */
26 getByAddress: function(address) { 25 getByAddress: function(address) {
27 for (var i = 0; i < this.length; i++) { 26 for (var i = 0; i < this.length; i++) {
28 var device = this.item(i); 27 var device = this.item(i);
29 if (address == device.info.address) 28 if (address == device.info.address)
30 return device; 29 return device;
31 } 30 }
32 return null; 31 return null;
33 }, 32 },
33
34 /** 34 /**
35 * Adds or updates a Device with new DeviceInfo. 35 * Adds or updates a Device with new DeviceInfo.
36 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo 36 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo
37 */ 37 */
38 addOrUpdate: function(deviceInfo) { 38 addOrUpdate: function(deviceInfo) {
39 var oldDevice = this.getByAddress(deviceInfo.address); 39 var oldDevice = this.getByAddress(deviceInfo.address);
40 if (oldDevice) { 40 if (oldDevice) {
41 // Update rssi if it's valid 41 // Update rssi if it's valid
42 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || 42 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) ||
43 (oldDevice.info.rssi && oldDevice.info.rssi.value); 43 (oldDevice.info.rssi && oldDevice.info.rssi.value);
44 44
45 oldDevice.info = deviceInfo; 45 oldDevice.info = deviceInfo;
46 oldDevice.info.rssi = { value: rssi }; 46 oldDevice.info.rssi = { value: rssi };
47 oldDevice.removed = false; 47 oldDevice.removed = false;
48 48
49 this.updateIndex(this.indexOf(oldDevice)); 49 this.updateIndex(this.indexOf(oldDevice));
50 } else { 50 } else {
51 var device = new Device(deviceInfo); 51 var device = new Device(deviceInfo);
52 this.push(device); 52 this.push(device);
53 } 53 }
54 }, 54 },
55
56 /**
57 * Marks the Device as removed.
58 * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo
59 */
55 remove: function(deviceInfo) { 60 remove: function(deviceInfo) {
56 var device = this.getByAddress(deviceInfo.address); 61 var device = this.getByAddress(deviceInfo.address);
57 assert(device, 62 assert(device,
58 'Device does not exist.'); 63 'Device does not exist.');
59 device.removed = true; 64 device.removed = true;
65 this.updateIndex(this.indexOf(device));
60 } 66 }
61 }; 67 };
62 68
63 /* 69 /*
64 * Data model for a cached device. 70 * Data model for a cached device.
65 * @constructor 71 * @constructor
66 * @param {!interfaces.BluetoothDevice.DeviceInfo} info 72 * @param {!interfaces.BluetoothDevice.DeviceInfo} info
67 */ 73 */
68 var Device = function(info) { 74 var Device = function(info) {
69 this.info = info; 75 this.info = info;
70 this.removed = false; 76 this.removed = false;
77 this.connectionPending = false;
78 };
79 Device.prototype = {
80 /**
81 * Creates a connection to the device and updates the service listing.
82 * @return {Promise} rejects if connection failed, resolves otherwise.
83 */
84 connect: function() {
85 this.connectionPending = true;
86 return interfaces.DefaultAdapter.connectToDevice(
87 this.info.address).then(
88 function(response) {
89 this.connectionPending = false;
90 if (response.error ==
91 interfaces.BluetoothAdapter.ConnectResult.SUCCESS) {
92 this.proxy = interfaces.Connection.bindHandleToProxy(
93 response.device,
94 interfaces.BluetoothDevice.Device);
95 return this.proxy.getServices();
96 }
97
98 // TODO(mbrunson): Replace with more descriptive error messages.
99 var errorString = Object.keys(
100 interfaces.BluetoothAdapter.ConnectResult)[response.error];
101
102 throw new Error(errorString);
103 }.bind(this)).then(function(response) {
104 this.info.services = response.services;
105 }.bind(this));
106 },
107
108 /**
109 * Disconnects a device and removes the Device interface proxy.
110 */
111 disconnect: function() {
112 if (this.proxy) {
113 this.proxy.disconnect();
114 this.proxy = null;
115 }
116 }
71 }; 117 };
72 118
73 return { 119 return {
74 DeviceCollection, DeviceCollection, 120 DeviceCollection, DeviceCollection,
75 }; 121 };
76 }); 122 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698