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.Adapter.proxyClass} adapter | 18 * @param {!interfaces.BluetoothAdapter.AdapterPtr} 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.Device.proxyClass>} | 32 * @return {!Promise<!interfaces.BluetoothDevice.DevicePtr>} |
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 interfaces.Connection.bindHandleToProxy( | 48 return response.device; |
49 response.device.ptr.passInterface().handle, | |
50 interfaces.BluetoothDevice.Device); | |
51 }); | 49 }); |
52 }, | 50 }, |
53 | 51 |
54 /** | 52 /** |
55 * Sets client of Adapter service. | 53 * Sets client of Adapter service. |
56 * @param {!interfaces.BluetoothAdapter.AdapterClient} adapterClient | 54 * @param {!interfaces.BluetoothAdapter.AdapterClient} adapterClient |
57 */ | 55 */ |
58 setClient: function(adapterClient) { | 56 setClient: function(adapterClient) { |
59 adapterClient.binding = new interfaces.Bindings.Binding( | 57 adapterClient.binding = new interfaces.Bindings.Binding( |
60 interfaces.BluetoothAdapter.AdapterClient, | 58 interfaces.BluetoothAdapter.AdapterClient, |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 | 136 |
139 /** | 137 /** |
140 * Initializes an AdapterBroker if one doesn't exist. | 138 * Initializes an AdapterBroker if one doesn't exist. |
141 * @return {!Promise<!AdapterBroker>} resolves with AdapterBroker, | 139 * @return {!Promise<!AdapterBroker>} resolves with AdapterBroker, |
142 * rejects if Bluetooth is not supported. | 140 * rejects if Bluetooth is not supported. |
143 */ | 141 */ |
144 function getAdapterBroker() { | 142 function getAdapterBroker() { |
145 if (adapterBroker) return Promise.resolve(adapterBroker); | 143 if (adapterBroker) return Promise.resolve(adapterBroker); |
146 | 144 |
147 return interfaces.setupInterfaces().then(function(adapter) { | 145 return interfaces.setupInterfaces().then(function(adapter) { |
148 // Hook up the instance properties. | 146 var adapterFactory = new interfaces.BluetoothAdapter.AdapterFactoryPtr( |
149 AdapterClient.prototype.__proto__ = | |
150 interfaces.BluetoothAdapter.AdapterClient.stubClass.prototype; | |
151 | |
152 var adapterFactory = interfaces.Connection.bindHandleToProxy( | |
153 interfaces.FrameInterfaces.getInterface( | 147 interfaces.FrameInterfaces.getInterface( |
154 interfaces.BluetoothAdapter.AdapterFactory.name), | 148 interfaces.BluetoothAdapter.AdapterFactory.name)); |
155 interfaces.BluetoothAdapter.AdapterFactory); | |
156 | 149 |
157 // Get an Adapter service. | 150 // Get an Adapter service. |
158 return adapterFactory.getAdapter(); | 151 return adapterFactory.getAdapter(); |
159 }).then(function(response) { | 152 }).then(function(response) { |
160 if (!response.adapter.ptr.isBound()) { | 153 if (!response.adapter.ptr.isBound()) { |
161 throw new Error('Bluetooth Not Supported on this platform.'); | 154 throw new Error('Bluetooth Not Supported on this platform.'); |
162 } | 155 } |
163 | 156 |
164 var adapter = interfaces.Connection.bindHandleToProxy( | 157 adapterBroker = new AdapterBroker(response.adapter); |
165 response.adapter.ptr.passInterface().handle, | |
166 interfaces.BluetoothAdapter.Adapter); | |
167 | |
168 adapterBroker = new AdapterBroker(adapter); | |
169 return adapterBroker; | 158 return adapterBroker; |
170 }); | 159 }); |
171 } | 160 } |
172 | 161 |
173 return { | 162 return { |
174 getAdapterBroker: getAdapterBroker, | 163 getAdapterBroker: getAdapterBroker, |
175 }; | 164 }; |
176 }); | 165 }); |
OLD | NEW |