Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 /* | 71 /* |
| 72 * Data model for a cached device. | 72 * Data model for a cached device. |
| 73 * @constructor | 73 * @constructor |
| 74 * @param {!interfaces.BluetoothDevice.DeviceInfo} info | 74 * @param {!interfaces.BluetoothDevice.DeviceInfo} info |
| 75 */ | 75 */ |
| 76 var Device = function(info) { | 76 var Device = function(info) { |
| 77 this.info = info; | 77 this.info = info; |
| 78 this.removed = false; | 78 this.removed = false; |
| 79 this.connectionPending = false; | |
| 80 }; | |
| 81 Device.prototype = { | |
| 82 /** | |
| 83 * Creates a connection to the device and updates the service listing. | |
| 84 * @return {Promise} rejects if connection failed, resolves otherwise. | |
| 85 */ | |
| 86 connect: function() { | |
| 87 this.connectionPending = true; | |
| 88 return adapter_broker.getAdapterBroker().then(function(broker) { | |
| 89 return broker.connectToDevice(this.info.address); | |
| 90 }.bind(this)).then(function(response) { | |
| 91 this.connectionPending = false; | |
| 92 if (response.error == | |
|
ortuno
2016/11/08 04:38:02
I would move this (if (response.error)... throw ne
mbrunson
2016/11/08 22:45:12
Done.
| |
| 93 interfaces.BluetoothAdapter.ConnectResult.SUCCESS) { | |
| 94 this.proxy = response.device; | |
| 95 return this.proxy.getServices(); | |
| 96 } | |
| 97 | |
| 98 // TODO(mbrunson): Replace with more descriptive error messages. | |
|
ortuno
2016/11/08 04:38:02
TODOs are usually used when there are other depend
mbrunson
2016/11/08 22:45:12
Ok. I'll make an issue. Done.
| |
| 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 } | |
| 79 }; | 117 }; |
| 80 | 118 |
| 81 return { | 119 return { |
| 82 Device: Device, | 120 Device: Device, |
| 83 DeviceCollection: DeviceCollection, | 121 DeviceCollection: DeviceCollection, |
| 84 }; | 122 }; |
| 85 }); | 123 }); |
| OLD | NEW |