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 AdapterBroker, served from | 6 * Javascript for AdapterBroker, served from |
7 * chrome://bluetooth-internals/. | 7 * chrome://bluetooth-internals/. |
8 */ | 8 */ |
9 cr.define('adapter_broker', function() { | 9 cr.define('adapter_broker', function() { |
10 /** | 10 /** |
11 * The proxy class of an adapter and router of adapter events. | 11 * The proxy class of an adapter and router of adapter events. |
12 * Exposes an EventTarget interface that allows other object to subscribe to | 12 * Exposes an EventTarget interface that allows other object to subscribe to |
13 * to specific AdapterClient events. | 13 * to specific AdapterClient events. |
14 * Provides proxy access to Adapter functions. Converts parameters to Mojo | 14 * Provides proxy access to Adapter functions. Converts parameters to Mojo |
15 * handles and back when necessary. | 15 * handles and back when necessary. |
16 * @constructor | 16 * @constructor |
17 * @extends {cr.EventTarget} | 17 * @extends {cr.EventTarget} |
18 * @param {!interfaces.BluetoothAdapter.AdapterPtr} adapter | 18 * @param {!interfaces.BluetoothAdapter.Adapter.proxyClass} adapter |
19 */ | 19 */ |
20 var AdapterBroker = function(adapter) { | 20 var AdapterBroker = function(adapter) { |
21 this.adapter_ = adapter; | 21 this.adapter_ = adapter; |
22 this.adapterClient_ = new AdapterClient(this); | 22 this.adapterClient_ = new AdapterClient(this); |
23 this.setClient(this.adapterClient_); | 23 this.setClient(this.adapterClient_); |
24 }; | 24 }; |
25 | 25 |
26 AdapterBroker.prototype = { | 26 AdapterBroker.prototype = { |
27 __proto__: cr.EventTarget.prototype, | 27 __proto__: cr.EventTarget.prototype, |
28 | 28 |
29 /** | 29 /** |
30 * Creates a GATT connection to the device with |address|. | 30 * Creates a GATT connection to the device with |address|. |
31 * @param {string} address | 31 * @param {string} address |
32 * @return {!Promise<!interfaces.BluetoothDevice.DevicePtr>} | 32 * @return {!Promise<!interfaces.BluetoothDevice.Device.proxyClass>} |
33 */ | 33 */ |
34 connectToDevice: function(address) { | 34 connectToDevice: function(address) { |
35 return this.adapter_.connectToDevice(address).then(function(response) { | 35 return this.adapter_.connectToDevice(address).then(function(response) { |
36 if (response.result != | 36 if (response.result != |
37 interfaces.BluetoothAdapter.ConnectResult.SUCCESS) { | 37 interfaces.BluetoothAdapter.ConnectResult.SUCCESS) { |
38 // TODO(crbug.com/663394): Replace with more descriptive error | 38 // TODO(crbug.com/663394): Replace with more descriptive error |
39 // messages. | 39 // messages. |
40 var ConnectResult = interfaces.BluetoothAdapter.ConnectResult; | 40 var ConnectResult = interfaces.BluetoothAdapter.ConnectResult; |
41 var errorString = Object.keys(ConnectResult).find(function(key) { | 41 var errorString = Object.keys(ConnectResult).find(function(key) { |
42 return ConnectResult[key] === response.result; | 42 return ConnectResult[key] === response.result; |
43 }); | 43 }); |
44 | 44 |
45 throw new Error(errorString); | 45 throw new Error(errorString); |
46 } | 46 } |
47 | 47 |
48 return response.device; | 48 return interfaces.Connection.bindHandleToProxy( |
| 49 response.device.ptr.passInterface().handle, |
| 50 interfaces.BluetoothDevice.Device); |
49 }); | 51 }); |
50 }, | 52 }, |
51 | 53 |
52 /** | 54 /** |
53 * Sets client of Adapter service. | 55 * Sets client of Adapter service. |
54 * @param {!interfaces.BluetoothAdapter.AdapterClient} adapterClient | 56 * @param {!interfaces.BluetoothAdapter.AdapterClient} adapterClient |
55 */ | 57 */ |
56 setClient: function(adapterClient) { | 58 setClient: function(adapterClient) { |
57 adapterClient.binding = new interfaces.Bindings.Binding( | 59 adapterClient.binding = new interfaces.Bindings.Binding( |
58 interfaces.BluetoothAdapter.AdapterClient, | 60 interfaces.BluetoothAdapter.AdapterClient, |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 | 138 |
137 /** | 139 /** |
138 * Initializes an AdapterBroker if one doesn't exist. | 140 * Initializes an AdapterBroker if one doesn't exist. |
139 * @return {!Promise<!AdapterBroker>} resolves with AdapterBroker, | 141 * @return {!Promise<!AdapterBroker>} resolves with AdapterBroker, |
140 * rejects if Bluetooth is not supported. | 142 * rejects if Bluetooth is not supported. |
141 */ | 143 */ |
142 function getAdapterBroker() { | 144 function getAdapterBroker() { |
143 if (adapterBroker) return Promise.resolve(adapterBroker); | 145 if (adapterBroker) return Promise.resolve(adapterBroker); |
144 | 146 |
145 return interfaces.setupInterfaces().then(function(adapter) { | 147 return interfaces.setupInterfaces().then(function(adapter) { |
146 var adapterFactory = new interfaces.BluetoothAdapter.AdapterFactoryPtr( | 148 // Hook up the instance properties. |
| 149 AdapterClient.prototype.__proto__ = |
| 150 interfaces.BluetoothAdapter.AdapterClient.stubClass.prototype; |
| 151 |
| 152 var adapterFactory = interfaces.Connection.bindHandleToProxy( |
147 interfaces.FrameInterfaces.getInterface( | 153 interfaces.FrameInterfaces.getInterface( |
148 interfaces.BluetoothAdapter.AdapterFactory.name)); | 154 interfaces.BluetoothAdapter.AdapterFactory.name), |
| 155 interfaces.BluetoothAdapter.AdapterFactory); |
149 | 156 |
150 // Get an Adapter service. | 157 // Get an Adapter service. |
151 return adapterFactory.getAdapter(); | 158 return adapterFactory.getAdapter(); |
152 }).then(function(response) { | 159 }).then(function(response) { |
153 if (!response.adapter.ptr.isBound()) { | 160 if (!response.adapter.ptr.isBound()) { |
154 throw new Error('Bluetooth Not Supported on this platform.'); | 161 throw new Error('Bluetooth Not Supported on this platform.'); |
155 } | 162 } |
156 | 163 |
157 adapterBroker = new AdapterBroker(response.adapter); | 164 var adapter = interfaces.Connection.bindHandleToProxy( |
| 165 response.adapter.ptr.passInterface().handle, |
| 166 interfaces.BluetoothAdapter.Adapter); |
| 167 |
| 168 adapterBroker = new AdapterBroker(adapter); |
158 return adapterBroker; | 169 return adapterBroker; |
159 }); | 170 }); |
160 } | 171 } |
161 | 172 |
162 return { | 173 return { |
163 getAdapterBroker: getAdapterBroker, | 174 getAdapterBroker: getAdapterBroker, |
164 }; | 175 }; |
165 }); | 176 }); |
OLD | NEW |