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 /** @typedef {interfaces.BluetoothAdapter.Adapter.ptrClass} */ |
| 11 var AdapterPtr; |
| 12 /** @typedef {interfaces.BluetoothDevice.Device.ptrClass} */ |
| 13 var DevicePtr; |
| 14 /** @typedef {interfaces.BluetoothAdapter.DiscoverySession.ptrClass} */ |
| 15 var DiscoverySessionPtr; |
| 16 |
| 17 /** |
| 18 * Enum of adapter property names. Used for adapterchanged events. |
| 19 * @enum {string} |
| 20 */ |
| 21 var AdapterProperty = { |
| 22 DISCOVERING: 'discovering', |
| 23 }; |
| 24 |
10 /** | 25 /** |
11 * The proxy class of an adapter and router of adapter events. | 26 * The proxy class of an adapter and router of adapter events. |
12 * Exposes an EventTarget interface that allows other object to subscribe to | 27 * Exposes an EventTarget interface that allows other object to subscribe to |
13 * to specific AdapterClient events. | 28 * to specific AdapterClient events. |
14 * Provides proxy access to Adapter functions. Converts parameters to Mojo | 29 * Provides proxy access to Adapter functions. Converts parameters to Mojo |
15 * handles and back when necessary. | 30 * handles and back when necessary. |
16 * @constructor | 31 * @constructor |
17 * @extends {cr.EventTarget} | 32 * @extends {cr.EventTarget} |
18 * @param {!interfaces.BluetoothAdapter.AdapterPtr} adapter | 33 * @param {!AdapterPtr} adapter |
19 */ | 34 */ |
20 var AdapterBroker = function(adapter) { | 35 var AdapterBroker = function(adapter) { |
21 this.adapter_ = adapter; | 36 this.adapter_ = adapter; |
22 this.adapterClient_ = new AdapterClient(this); | 37 this.adapterClient_ = new AdapterClient(this); |
23 this.setClient(this.adapterClient_); | 38 this.setClient(this.adapterClient_); |
24 }; | 39 }; |
25 | 40 |
26 AdapterBroker.prototype = { | 41 AdapterBroker.prototype = { |
27 __proto__: cr.EventTarget.prototype, | 42 __proto__: cr.EventTarget.prototype, |
28 | 43 |
29 /** | 44 /** |
30 * Creates a GATT connection to the device with |address|. | 45 * Creates a GATT connection to the device with |address|. |
31 * @param {string} address | 46 * @param {string} address |
32 * @return {!Promise<!interfaces.BluetoothDevice.DevicePtr>} | 47 * @return {!Promise<!DevicePtr>} |
33 */ | 48 */ |
34 connectToDevice: function(address) { | 49 connectToDevice: function(address) { |
35 return this.adapter_.connectToDevice(address).then(function(response) { | 50 return this.adapter_.connectToDevice(address).then(function(response) { |
36 if (response.result != | 51 if (response.result != |
37 interfaces.BluetoothAdapter.ConnectResult.SUCCESS) { | 52 interfaces.BluetoothAdapter.ConnectResult.SUCCESS) { |
38 // TODO(crbug.com/663394): Replace with more descriptive error | 53 // TODO(crbug.com/663394): Replace with more descriptive error |
39 // messages. | 54 // messages. |
40 var ConnectResult = interfaces.BluetoothAdapter.ConnectResult; | 55 var ConnectResult = interfaces.BluetoothAdapter.ConnectResult; |
41 var errorString = Object.keys(ConnectResult).find(function(key) { | 56 var errorString = Object.keys(ConnectResult).find(function(key) { |
42 return ConnectResult[key] === response.result; | 57 return ConnectResult[key] === response.result; |
43 }); | 58 }); |
44 | 59 |
45 throw new Error(errorString); | 60 throw new Error(errorString); |
46 } | 61 } |
47 | 62 |
48 return response.device; | 63 return response.device; |
49 }); | 64 }); |
50 }, | 65 }, |
51 | 66 |
52 /** | 67 /** |
| 68 * Gets an array of currently detectable devices from the Adapter service. |
| 69 * @return {!Array<!interfaces.BluetoothDevice.DeviceInfo>} |
| 70 */ |
| 71 getDevices: function() { |
| 72 return this.adapter_.getDevices(); |
| 73 }, |
| 74 |
| 75 /** |
| 76 * Gets the current state of the Adapter. |
| 77 * @return {!interfaces.BluetoothAdapter.AdapterInfo} |
| 78 */ |
| 79 getInfo: function() { |
| 80 return this.adapter_.getInfo(); |
| 81 }, |
| 82 |
| 83 /** |
53 * Sets client of Adapter service. | 84 * Sets client of Adapter service. |
54 * @param {!interfaces.BluetoothAdapter.AdapterClient} adapterClient | 85 * @param {!interfaces.BluetoothAdapter.AdapterClient} adapterClient |
55 */ | 86 */ |
56 setClient: function(adapterClient) { | 87 setClient: function(adapterClient) { |
57 adapterClient.binding = new interfaces.Bindings.Binding( | 88 adapterClient.binding = new interfaces.Bindings.Binding( |
58 interfaces.BluetoothAdapter.AdapterClient, | 89 interfaces.BluetoothAdapter.AdapterClient, |
59 adapterClient); | 90 adapterClient); |
60 | 91 |
61 this.adapter_.setClient( | 92 this.adapter_.setClient( |
62 adapterClient.binding.createInterfacePtrAndBind()); | 93 adapterClient.binding.createInterfacePtrAndBind()); |
63 }, | 94 }, |
64 | 95 |
65 /** | 96 /** |
66 * Gets an array of currently detectable devices from the Adapter service. | 97 * Requests the adapter to start a new discovery session. |
67 * @return {!Array<!interfaces.BluetoothDevice.DeviceInfo>} | 98 * @return {!Promise<!DiscoverySessionPtr>} |
68 */ | 99 */ |
69 getDevices: function() { | 100 startDiscoverySession: function() { |
70 return this.adapter_.getDevices(); | 101 return this.adapter_.startDiscoverySession().then(function(response) { |
| 102 if (!response.session.ptr.isBound()) { |
| 103 throw new Error('Discovery session failed to start'); |
| 104 } |
| 105 |
| 106 return response.session; |
| 107 }); |
71 }, | 108 }, |
72 | |
73 /** | |
74 * Gets the current state of the Adapter. | |
75 * @return {!interfaces.BluetoothAdapter.AdapterInfo} | |
76 */ | |
77 getInfo: function() { | |
78 return this.adapter_.getInfo(); | |
79 } | |
80 }; | 109 }; |
81 | 110 |
82 /** | 111 /** |
83 * The implementation of AdapterClient in | 112 * The implementation of AdapterClient in |
84 * device/bluetooth/public/interfaces/adapter.mojom. Dispatches events | 113 * device/bluetooth/public/interfaces/adapter.mojom. Dispatches events |
85 * through AdapterBroker to notify client objects of changes to the Adapter | 114 * through AdapterBroker to notify client objects of changes to the Adapter |
86 * service. | 115 * service. |
87 * @constructor | 116 * @constructor |
88 * @param {!AdapterBroker} adapterBroker Broker to dispatch events through. | 117 * @param {!AdapterBroker} adapterBroker Broker to dispatch events through. |
89 */ | 118 */ |
90 var AdapterClient = function(adapterBroker) { | 119 var AdapterClient = function(adapterBroker) { |
91 this.adapterBroker_ = adapterBroker; | 120 this.adapterBroker_ = adapterBroker; |
92 }; | 121 }; |
93 | 122 |
94 AdapterClient.prototype = { | 123 AdapterClient.prototype = { |
95 /** | 124 /** |
| 125 * Fires adapterchanged event. |
| 126 * @param {boolean} discovering |
| 127 */ |
| 128 discoveringChanged: function(discovering) { |
| 129 var event = new CustomEvent('adapterchanged', { |
| 130 detail: { |
| 131 property: AdapterProperty.DISCOVERING, |
| 132 value: discovering, |
| 133 } |
| 134 }); |
| 135 this.adapterBroker_.dispatchEvent(event); |
| 136 }, |
| 137 |
| 138 /** |
96 * Fires deviceadded event. | 139 * Fires deviceadded event. |
97 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo | 140 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
98 */ | 141 */ |
99 deviceAdded: function(deviceInfo) { | 142 deviceAdded: function(deviceInfo) { |
100 var event = new CustomEvent('deviceadded', { | 143 var event = new CustomEvent('deviceadded', { |
101 detail: { | 144 detail: { |
102 deviceInfo: deviceInfo | 145 deviceInfo: deviceInfo |
103 } | 146 } |
104 }); | 147 }); |
105 this.adapterBroker_.dispatchEvent(event); | 148 this.adapterBroker_.dispatchEvent(event); |
106 }, | 149 }, |
107 | 150 |
108 /** | 151 /** |
109 * Fires deviceremoved event. | 152 * Fires devicechanged event. |
110 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo | 153 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
111 */ | 154 */ |
112 deviceRemoved: function(deviceInfo) { | 155 deviceChanged: function(deviceInfo) { |
113 var event = new CustomEvent('deviceremoved', { | 156 var event = new CustomEvent('devicechanged', { |
114 detail: { | 157 detail: { |
115 deviceInfo: deviceInfo | 158 deviceInfo: deviceInfo |
116 } | 159 } |
117 }); | 160 }); |
118 this.adapterBroker_.dispatchEvent(event); | 161 this.adapterBroker_.dispatchEvent(event); |
119 }, | 162 }, |
120 | 163 |
121 /** | 164 /** |
122 * Fires devicechanged event. | 165 * Fires deviceremoved event. |
123 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo | 166 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo |
124 */ | 167 */ |
125 deviceChanged: function(deviceInfo) { | 168 deviceRemoved: function(deviceInfo) { |
126 var event = new CustomEvent('devicechanged', { | 169 var event = new CustomEvent('deviceremoved', { |
127 detail: { | 170 detail: { |
128 deviceInfo: deviceInfo | 171 deviceInfo: deviceInfo |
129 } | 172 } |
130 }); | 173 }); |
131 this.adapterBroker_.dispatchEvent(event); | 174 this.adapterBroker_.dispatchEvent(event); |
132 } | 175 }, |
133 }; | 176 }; |
134 | 177 |
135 var adapterBroker = null; | 178 var adapterBroker = null; |
136 | 179 |
137 /** | 180 /** |
138 * Initializes an AdapterBroker if one doesn't exist. | 181 * Initializes an AdapterBroker if one doesn't exist. |
139 * @return {!Promise<!AdapterBroker>} resolves with AdapterBroker, | 182 * @return {!Promise<!AdapterBroker>} resolves with AdapterBroker, |
140 * rejects if Bluetooth is not supported. | 183 * rejects if Bluetooth is not supported. |
141 */ | 184 */ |
142 function getAdapterBroker() { | 185 function getAdapterBroker() { |
(...skipping 10 matching lines...) Expand all Loading... |
153 if (!response.adapter.ptr.isBound()) { | 196 if (!response.adapter.ptr.isBound()) { |
154 throw new Error('Bluetooth Not Supported on this platform.'); | 197 throw new Error('Bluetooth Not Supported on this platform.'); |
155 } | 198 } |
156 | 199 |
157 adapterBroker = new AdapterBroker(response.adapter); | 200 adapterBroker = new AdapterBroker(response.adapter); |
158 return adapterBroker; | 201 return adapterBroker; |
159 }); | 202 }); |
160 } | 203 } |
161 | 204 |
162 return { | 205 return { |
| 206 AdapterProperty: AdapterProperty, |
163 getAdapterBroker: getAdapterBroker, | 207 getAdapterBroker: getAdapterBroker, |
164 }; | 208 }; |
165 }); | 209 }); |
OLD | NEW |