Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / api / bluetooth_low_energy / bluetooth_low_energy_event_router.h
bloba420918888dc294ebf8521dd47236d006db2324e
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_BROWSER_API_BLUETOOTH_LOW_ENERGY_BLUETOOTH_LOW_ENERGY_EVENT_ROUTER_H_
6 #define EXTENSIONS_BROWSER_API_BLUETOOTH_LOW_ENERGY_BLUETOOTH_LOW_ENERGY_EVENT_ROUTER_H_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/callback.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "device/bluetooth/bluetooth_adapter.h"
18 #include "device/bluetooth/bluetooth_device.h"
19 #include "device/bluetooth/bluetooth_gatt_service.h"
20 #include "extensions/browser/extension_event_histogram_value.h"
21 #include "extensions/common/api/bluetooth_low_energy.h"
23 namespace base {
25 class ListValue;
27 } // namespace base
29 namespace content {
31 class BrowserContext;
33 } // namespace content
35 namespace device {
37 class BluetoothGattNotifySession;
39 } // namespace device
41 namespace extensions {
43 class BluetoothLowEnergyConnection;
44 class BluetoothLowEnergyNotifySession;
45 class Extension;
47 // The BluetoothLowEnergyEventRouter is used by the bluetoothLowEnergy API to
48 // interface with the internal Bluetooth API in device/bluetooth.
49 class BluetoothLowEnergyEventRouter
50 : public device::BluetoothAdapter::Observer {
51 public:
52 explicit BluetoothLowEnergyEventRouter(content::BrowserContext* context);
53 ~BluetoothLowEnergyEventRouter() override;
55 // Possible ways that an API method can fail or succeed.
56 enum Status {
57 kStatusSuccess = 0,
58 kStatusErrorAlreadyConnected,
59 kStatusErrorAlreadyNotifying,
60 kStatusErrorAuthenticationFailed,
61 kStatusErrorCanceled,
62 kStatusErrorFailed,
63 kStatusErrorGattNotSupported,
64 kStatusErrorHigherSecurity,
65 kStatusErrorInProgress,
66 kStatusErrorInsufficientAuthorization,
67 kStatusErrorInvalidLength,
68 kStatusErrorNotConnected,
69 kStatusErrorNotFound,
70 kStatusErrorNotNotifying,
71 kStatusErrorPermissionDenied,
72 kStatusErrorTimeout,
73 kStatusErrorUnsupportedDevice,
74 kStatusErrorInvalidArguments,
77 // Error callback is used by asynchronous methods to report failures.
78 typedef base::Callback<void(Status)> ErrorCallback;
80 // Returns true if Bluetooth is supported on the current platform or if the
81 // internal |adapter_| instance has been initialized for testing.
82 bool IsBluetoothSupported() const;
84 // Obtains a handle on the BluetoothAdapter and invokes |callback|. Returns
85 // false, if Bluetooth is not supported. Otherwise, asynchronously initializes
86 // it and invokes |callback|. Until the first successful call to this method,
87 // none of the methods in this class will succeed and no device::Bluetooth*
88 // API events will be observed.
89 bool InitializeAdapterAndInvokeCallback(const base::Closure& callback);
91 // Returns true, if the BluetoothAdapter was initialized.
92 bool HasAdapter() const;
94 // Creates a GATT connection to the device with address |device_address| for
95 // extension |extension|. The connection is kept alive until the extension is
96 // unloaded, the device is removed, or is disconnect by the host subsystem.
97 // |error_callback| is called with an error status in case of failure. If
98 // |persistent| is true, then the allocated connection resource is persistent
99 // across unloads.
100 void Connect(bool persistent,
101 const Extension* extension,
102 const std::string& device_address,
103 const base::Closure& callback,
104 const ErrorCallback& error_callback);
106 // Disconnects the currently open GATT connection of extension |extension| to
107 // device with address |device_address|. |error_callback| is called with an
108 // error status in case of failure, e.g. if the device is not found or the
109 // given
110 // extension does not have an open connection to the device.
111 void Disconnect(const Extension* extension,
112 const std::string& device_address,
113 const base::Closure& callback,
114 const ErrorCallback& error_callback);
116 // Returns the list of api::bluetooth_low_energy::Service objects
117 // associated with the Bluetooth device with address |device_address| in
118 // |out_services|.
119 // Returns false, if no device with the given address is known. If the device
120 // is found but it has no GATT services, then returns true and leaves
121 // |out_services| empty. Returns true, on success. |out_services| must not
122 // be NULL. If it is non-empty, then its contents will be cleared.
123 typedef std::vector<linked_ptr<api::bluetooth_low_energy::Service>>
124 ServiceList;
125 bool GetServices(const std::string& device_address,
126 ServiceList* out_services) const;
128 // Populates |out_service| based on GATT service with instance ID
129 // |instance_id|. |out_service| must not be NULL.
130 Status GetService(const std::string& instance_id,
131 api::bluetooth_low_energy::Service* out_service) const;
133 // Populates |out_services| with the list of GATT services that are included
134 // by the GATT service with instance ID |instance_id|. Returns false, if not
135 // GATT service with the given ID is known. If the given service has no
136 // included services, then |out_service| will be empty. |out_service| must not
137 // be NULL. If it is non-empty, then its contents will be cleared.
138 Status GetIncludedServices(const std::string& instance_id,
139 ServiceList* out_services) const;
141 // Returns the list of api::bluetooth_low_energy::Characteristic objects
142 // associated with the GATT service with instance ID |instance_id| in
143 // |out_characteristics|. Returns false, if no service with the given instance
144 // ID is known. If the service is found but it has no characteristics, then
145 // returns true and leaves |out_characteristics| empty.
146 // |out_characteristics| must not be NULL and if it is non-empty,
147 // then its contents will be cleared. |extension| is the extension that made
148 // the call.
149 typedef std::vector<linked_ptr<api::bluetooth_low_energy::Characteristic>>
150 CharacteristicList;
151 Status GetCharacteristics(const Extension* extension,
152 const std::string& instance_id,
153 CharacteristicList* out_characteristics) const;
155 // Populates |out_characteristic| based on GATT characteristic with instance
156 // ID |instance_id|. |out_characteristic| must not be NULL. |extension| is the
157 // extension that made the call.
158 Status GetCharacteristic(
159 const Extension* extension,
160 const std::string& instance_id,
161 api::bluetooth_low_energy::Characteristic* out_characteristic) const;
163 // Returns the list of api::bluetooth_low_energy::Descriptor objects
164 // associated with the GATT characteristic with instance ID |instance_id| in
165 // |out_descriptors|. If the characteristic is found but it has no
166 // descriptors, then returns true and leaves |out_descriptors| empty.
167 // |out_descriptors| must not be NULL and if it is non-empty,
168 // then its contents will be cleared. |extension| is the extension that made
169 // the call.
170 typedef std::vector<linked_ptr<api::bluetooth_low_energy::Descriptor>>
171 DescriptorList;
172 Status GetDescriptors(const Extension* extension,
173 const std::string& instance_id,
174 DescriptorList* out_descriptors) const;
176 // Populates |out_descriptor| based on GATT characteristic descriptor with
177 // instance ID |instance_id|. |out_descriptor| must not be NULL.
178 // |extension| is the extension that made the call.
179 Status GetDescriptor(
180 const Extension* extension,
181 const std::string& instance_id,
182 api::bluetooth_low_energy::Descriptor* out_descriptor) const;
184 // Sends a request to read the value of the characteristic with intance ID
185 // |instance_id|. Invokes |callback| on success and |error_callback| on
186 // failure. |extension| is the extension that made the call.
187 void ReadCharacteristicValue(const Extension* extension,
188 const std::string& instance_id,
189 const base::Closure& callback,
190 const ErrorCallback& error_callback);
192 // Sends a request to write the value of the characteristic with instance ID
193 // |instance_id|. Invokes |callback| on success and |error_callback| on
194 // failure. |extension| is the extension that made the call.
195 void WriteCharacteristicValue(const Extension* extension,
196 const std::string& instance_id,
197 const std::vector<uint8>& value,
198 const base::Closure& callback,
199 const ErrorCallback& error_callback);
201 // Sends a request to start characteristic notifications from characteristic
202 // with instance ID |instance_id|, for extension |extension|. Invokes
203 // |callback| on success and |error_callback| on failure. If |persistent| is
204 // true, then the allocated connection resource is persistent across unloads.
205 void StartCharacteristicNotifications(bool persistent,
206 const Extension* extension,
207 const std::string& instance_id,
208 const base::Closure& callback,
209 const ErrorCallback& error_callback);
211 // Sends a request to stop characteristic notifications from characteristic
212 // with instance ID |instance_id|, for extension |extension|. Invokes
213 // |callback| on success and |error_callback| on failure.
214 void StopCharacteristicNotifications(const Extension* extension,
215 const std::string& instance_id,
216 const base::Closure& callback,
217 const ErrorCallback& error_callback);
219 // Sends a request to read the value of the descriptor with instance ID
220 // |instance_id|. Invokes |callback| on success and |error_callback| on
221 // failure. |extension| is the extension that made the call.
222 void ReadDescriptorValue(const Extension* extension,
223 const std::string& instance_id,
224 const base::Closure& callback,
225 const ErrorCallback& error_callback);
227 // Sends a request to write the value of the descriptor with instance ID
228 // |instance_id|. Invokes |callback| on success and |error_callback| on
229 // failure. |extension| is the extension that made the call.
230 void WriteDescriptorValue(const Extension* extension,
231 const std::string& instance_id,
232 const std::vector<uint8>& value,
233 const base::Closure& callback,
234 const ErrorCallback& error_callback);
236 // Initializes the adapter for testing. Used by unit tests only.
237 void SetAdapterForTesting(device::BluetoothAdapter* adapter);
239 // device::BluetoothAdapter::Observer overrides.
240 void GattServiceAdded(device::BluetoothAdapter* adapter,
241 device::BluetoothDevice* device,
242 device::BluetoothGattService* service) override;
243 void GattServiceRemoved(device::BluetoothAdapter* adapter,
244 device::BluetoothDevice* device,
245 device::BluetoothGattService* service) override;
246 void GattDiscoveryCompleteForService(
247 device::BluetoothAdapter* adapter,
248 device::BluetoothGattService* service) override;
249 void GattServiceChanged(device::BluetoothAdapter* adapter,
250 device::BluetoothGattService* service) override;
251 void GattCharacteristicAdded(
252 device::BluetoothAdapter* adapter,
253 device::BluetoothGattCharacteristic* characteristic) override;
254 void GattCharacteristicRemoved(
255 device::BluetoothAdapter* adapter,
256 device::BluetoothGattCharacteristic* characteristic) override;
257 void GattDescriptorAdded(
258 device::BluetoothAdapter* adapter,
259 device::BluetoothGattDescriptor* descriptor) override;
260 void GattDescriptorRemoved(
261 device::BluetoothAdapter* adapter,
262 device::BluetoothGattDescriptor* descriptor) override;
263 void GattCharacteristicValueChanged(
264 device::BluetoothAdapter* adapter,
265 device::BluetoothGattCharacteristic* characteristic,
266 const std::vector<uint8>& value) override;
267 void GattDescriptorValueChanged(device::BluetoothAdapter* adapter,
268 device::BluetoothGattDescriptor* descriptor,
269 const std::vector<uint8>& value) override;
271 device::BluetoothAdapter* adapter() { return adapter_.get(); }
273 private:
274 // Called by BluetoothAdapterFactory.
275 void OnGetAdapter(const base::Closure& callback,
276 scoped_refptr<device::BluetoothAdapter> adapter);
278 // Initializes the identifier for all existing GATT objects and devices.
279 // Called by OnGetAdapter and SetAdapterForTesting.
280 void InitializeIdentifierMappings();
282 // Sends the event named |event_name| to all listeners of that event that
283 // have the Bluetooth UUID manifest permission for UUID |uuid| and the
284 // "low_energy" manifest permission, with |args| as the argument to that
285 // event. If the event involves a characteristic, then |characteristic_id|
286 // should be the instance ID of the involved characteristic. Otherwise, an
287 // empty string should be passed.
288 void DispatchEventToExtensionsWithPermission(
289 events::HistogramValue histogram_value,
290 const std::string& event_name,
291 const device::BluetoothUUID& uuid,
292 const std::string& characteristic_id,
293 scoped_ptr<base::ListValue> args);
295 // Returns a BluetoothGattService by its instance ID |instance_id|. Returns
296 // NULL, if the service cannot be found.
297 device::BluetoothGattService* FindServiceById(
298 const std::string& instance_id) const;
300 // Returns a BluetoothGattCharacteristic by its instance ID |instance_id|.
301 // Returns NULL, if the characteristic cannot be found.
302 device::BluetoothGattCharacteristic* FindCharacteristicById(
303 const std::string& instance_id) const;
305 // Returns a BluetoothGattDescriptor by its instance ID |instance_id|.
306 // Returns NULL, if the descriptor cannot be found.
307 device::BluetoothGattDescriptor* FindDescriptorById(
308 const std::string& instance_id) const;
310 // Called by BluetoothGattCharacteristic and BluetoothGattDescriptor in
311 // response to ReadRemoteCharacteristic and ReadRemoteDescriptor.
312 void OnValueSuccess(const base::Closure& callback,
313 const std::vector<uint8>& value);
315 // Called by BluetoothDevice in response to a call to CreateGattConnection.
316 void OnCreateGattConnection(
317 bool persistent,
318 const std::string& extension_id,
319 const std::string& device_address,
320 const base::Closure& callback,
321 scoped_ptr<device::BluetoothGattConnection> connection);
323 // Called by BluetoothGattConnection in response to a call to Disconnect.
324 void OnDisconnect(const std::string& extension_id,
325 const std::string& device_address,
326 const base::Closure& callback);
328 // Called by BluetoothGattCharacteristic and BluetoothGattDescriptor in
329 // case of an error during the read/write operations.
330 void OnError(const ErrorCallback& error_callback,
331 device::BluetoothGattService::GattErrorCode error_code);
333 // Called by BluetoothDevice in response to a call to CreateGattConnection.
334 void OnConnectError(const std::string& extension_id,
335 const std::string& device_address,
336 const ErrorCallback& error_callback,
337 device::BluetoothDevice::ConnectErrorCode error_code);
339 // Called by BluetoothGattCharacteristic in response to a call to
340 // StartNotifySession.
341 void OnStartNotifySession(
342 bool persistent,
343 const std::string& extension_id,
344 const std::string& characteristic_id,
345 const base::Closure& callback,
346 scoped_ptr<device::BluetoothGattNotifySession> session);
348 // Called by BluetoothGattCharacteristic in response to a call to
349 // StartNotifySession.
350 void OnStartNotifySessionError(
351 const std::string& extension_id,
352 const std::string& characteristic_id,
353 const ErrorCallback& error_callback,
354 device::BluetoothGattService::GattErrorCode error_code);
356 // Called by BluetoothGattNotifySession in response to a call to Stop.
357 void OnStopNotifySession(const std::string& extension_id,
358 const std::string& characteristic_id,
359 const base::Closure& callback);
361 // Finds and returns a BluetoothLowEnergyConnection to device with address
362 // |device_address| from the managed API resources for extension with ID
363 // |extension_id|.
364 BluetoothLowEnergyConnection* FindConnection(
365 const std::string& extension_id,
366 const std::string& device_address);
368 // Removes the connection to device with address |device_address| from the
369 // managed API resources for extension with ID |extension_id|. Returns false,
370 // if the connection could not be found.
371 bool RemoveConnection(const std::string& extension_id,
372 const std::string& device_address);
374 // Finds and returns a BluetoothLowEnergyNotifySession associated with
375 // characteristic with instance ID |characteristic_id| from the managed API
376 // API resources for extension with ID |extension_id|.
377 BluetoothLowEnergyNotifySession* FindNotifySession(
378 const std::string& extension_id,
379 const std::string& characteristic_id);
381 // Removes the notify session associated with characteristic with
382 // instance ID |characteristic_id| from the managed API resources for
383 // extension with ID |extension_id|. Returns false, if the session could
384 // not be found.
385 bool RemoveNotifySession(const std::string& extension_id,
386 const std::string& characteristic_id);
388 // Mapping from instance ids to identifiers of owning instances. The keys are
389 // used to identify individual instances of GATT objects and are used by
390 // bluetoothLowEnergy API functions to obtain the correct GATT object to
391 // operate on. Instance IDs are string identifiers that are returned by the
392 // device/bluetooth API, by calling GetIdentifier() on the corresponding
393 // device::BluetoothGatt* instance.
395 // This mapping is necessary, as GATT object instances can only be obtained
396 // from the object that owns it, where raw pointers should not be cached. E.g.
397 // to obtain a device::BluetoothGattCharacteristic, it is necessary to obtain
398 // a pointer to the associated device::BluetoothDevice, and then to the
399 // device::BluetoothGattService that owns the characteristic.
400 typedef std::map<std::string, std::string> InstanceIdMap;
401 InstanceIdMap service_id_to_device_address_;
402 InstanceIdMap chrc_id_to_service_id_;
403 InstanceIdMap desc_id_to_chrc_id_;
405 // Pointer to the current BluetoothAdapter instance. This represents a local
406 // Bluetooth adapter of the system.
407 scoped_refptr<device::BluetoothAdapter> adapter_;
409 // Set of extension ID + device addresses to which a connect/disconnect is
410 // currently pending.
411 std::set<std::string> connecting_devices_;
412 std::set<std::string> disconnecting_devices_;
414 // Set of extension ID + characteristic ID to which a request to start a
415 // notify session is currently pending.
416 std::set<std::string> pending_session_calls_;
418 // BrowserContext passed during initialization.
419 content::BrowserContext* browser_context_;
421 // Note: This should remain the last member so it'll be destroyed and
422 // invalidate its weak pointers before any other members are destroyed.
423 base::WeakPtrFactory<BluetoothLowEnergyEventRouter> weak_ptr_factory_;
425 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyEventRouter);
428 } // namespace extensions
430 #endif // EXTENSIONS_BROWSER_API_BLUETOOTH_LOW_ENERGY_BLUETOOTH_LOW_ENERGY_EVENT_ROUTER_H_