Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / extensions / browser / api / bluetooth_low_energy / bluetooth_low_energy_event_router.h
blobf265505dd05e113f03e9de5eb65f79208e0f84e0
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/common/api/bluetooth_low_energy.h"
22 namespace base {
24 class ListValue;
26 } // namespace base
28 namespace content {
30 class BrowserContext;
32 } // namespace content
34 namespace device {
36 class BluetoothGattNotifySession;
38 } // namespace device
40 namespace extensions {
42 class BluetoothLowEnergyConnection;
43 class BluetoothLowEnergyNotifySession;
44 class Extension;
46 // The BluetoothLowEnergyEventRouter is used by the bluetoothLowEnergy API to
47 // interface with the internal Bluetooth API in device/bluetooth.
48 class BluetoothLowEnergyEventRouter
49 : public device::BluetoothAdapter::Observer {
50 public:
51 explicit BluetoothLowEnergyEventRouter(content::BrowserContext* context);
52 ~BluetoothLowEnergyEventRouter() override;
54 // Possible ways that an API method can fail or succeed.
55 enum Status {
56 kStatusSuccess = 0,
57 kStatusErrorAlreadyConnected,
58 kStatusErrorAlreadyNotifying,
59 kStatusErrorAuthenticationFailed,
60 kStatusErrorCanceled,
61 kStatusErrorFailed,
62 kStatusErrorGattNotSupported,
63 kStatusErrorHigherSecurity,
64 kStatusErrorInProgress,
65 kStatusErrorInsufficientAuthorization,
66 kStatusErrorInvalidLength,
67 kStatusErrorNotConnected,
68 kStatusErrorNotFound,
69 kStatusErrorNotNotifying,
70 kStatusErrorPermissionDenied,
71 kStatusErrorTimeout,
72 kStatusErrorUnsupportedDevice,
73 kStatusErrorInvalidArguments,
76 // Error callback is used by asynchronous methods to report failures.
77 typedef base::Callback<void(Status)> ErrorCallback;
79 // Returns true if Bluetooth is supported on the current platform or if the
80 // internal |adapter_| instance has been initialized for testing.
81 bool IsBluetoothSupported() const;
83 // Obtains a handle on the BluetoothAdapter and invokes |callback|. Returns
84 // false, if Bluetooth is not supported. Otherwise, asynchronously initializes
85 // it and invokes |callback|. Until the first successful call to this method,
86 // none of the methods in this class will succeed and no device::Bluetooth*
87 // API events will be observed.
88 bool InitializeAdapterAndInvokeCallback(const base::Closure& callback);
90 // Returns true, if the BluetoothAdapter was initialized.
91 bool HasAdapter() const;
93 // Creates a GATT connection to the device with address |device_address| for
94 // extension |extension|. The connection is kept alive until the extension is
95 // unloaded, the device is removed, or is disconnect by the host subsystem.
96 // |error_callback| is called with an error status in case of failure. If
97 // |persistent| is true, then the allocated connection resource is persistent
98 // across unloads.
99 void Connect(bool persistent,
100 const Extension* extension,
101 const std::string& device_address,
102 const base::Closure& callback,
103 const ErrorCallback& error_callback);
105 // Disconnects the currently open GATT connection of extension |extension| to
106 // device with address |device_address|. |error_callback| is called with an
107 // error status in case of failure, e.g. if the device is not found or the
108 // given
109 // extension does not have an open connection to the device.
110 void Disconnect(const Extension* extension,
111 const std::string& device_address,
112 const base::Closure& callback,
113 const ErrorCallback& error_callback);
115 // Returns the list of core_api::bluetooth_low_energy::Service objects
116 // associated with the Bluetooth device with address |device_address| in
117 // |out_services|.
118 // Returns false, if no device with the given address is known. If the device
119 // is found but it has no GATT services, then returns true and leaves
120 // |out_services| empty. Returns true, on success. |out_services| must not
121 // be NULL. If it is non-empty, then its contents will be cleared.
122 typedef std::vector<linked_ptr<core_api::bluetooth_low_energy::Service> >
123 ServiceList;
124 bool GetServices(const std::string& device_address,
125 ServiceList* out_services) const;
127 // Populates |out_service| based on GATT service with instance ID
128 // |instance_id|. |out_service| must not be NULL.
129 Status GetService(const std::string& instance_id,
130 core_api::bluetooth_low_energy::Service* out_service) const;
132 // Populates |out_services| with the list of GATT services that are included
133 // by the GATT service with instance ID |instance_id|. Returns false, if not
134 // GATT service with the given ID is known. If the given service has no
135 // included services, then |out_service| will be empty. |out_service| must not
136 // be NULL. If it is non-empty, then its contents will be cleared.
137 Status GetIncludedServices(const std::string& instance_id,
138 ServiceList* out_services) const;
140 // Returns the list of core_api::bluetooth_low_energy::Characteristic objects
141 // associated with the GATT service with instance ID |instance_id| in
142 // |out_characteristics|. Returns false, if no service with the given instance
143 // ID is known. If the service is found but it has no characteristics, then
144 // returns true and leaves |out_characteristics| empty.
145 // |out_characteristics| must not be NULL and if it is non-empty,
146 // then its contents will be cleared. |extension| is the extension that made
147 // the call.
148 typedef std::vector<
149 linked_ptr<core_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 core_api::bluetooth_low_energy::Characteristic* out_characteristic) const;
163 // Returns the list of core_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<core_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 core_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 const std::string& event_name,
290 const device::BluetoothUUID& uuid,
291 const std::string& characteristic_id,
292 scoped_ptr<base::ListValue> args);
294 // Returns a BluetoothGattService by its instance ID |instance_id|. Returns
295 // NULL, if the service cannot be found.
296 device::BluetoothGattService* FindServiceById(
297 const std::string& instance_id) const;
299 // Returns a BluetoothGattCharacteristic by its instance ID |instance_id|.
300 // Returns NULL, if the characteristic cannot be found.
301 device::BluetoothGattCharacteristic* FindCharacteristicById(
302 const std::string& instance_id) const;
304 // Returns a BluetoothGattDescriptor by its instance ID |instance_id|.
305 // Returns NULL, if the descriptor cannot be found.
306 device::BluetoothGattDescriptor* FindDescriptorById(
307 const std::string& instance_id) const;
309 // Called by BluetoothGattCharacteristic and BluetoothGattDescriptor in
310 // response to ReadRemoteCharacteristic and ReadRemoteDescriptor.
311 void OnValueSuccess(const base::Closure& callback,
312 const std::vector<uint8>& value);
314 // Called by BluetoothDevice in response to a call to CreateGattConnection.
315 void OnCreateGattConnection(
316 bool persistent,
317 const std::string& extension_id,
318 const std::string& device_address,
319 const base::Closure& callback,
320 scoped_ptr<device::BluetoothGattConnection> connection);
322 // Called by BluetoothGattConnection in response to a call to Disconnect.
323 void OnDisconnect(const std::string& extension_id,
324 const std::string& device_address,
325 const base::Closure& callback);
327 // Called by BluetoothGattCharacteristic and BluetoothGattDescriptor in
328 // case of an error during the read/write operations.
329 void OnError(const ErrorCallback& error_callback,
330 device::BluetoothGattService::GattErrorCode error_code);
332 // Called by BluetoothDevice in response to a call to CreateGattConnection.
333 void OnConnectError(const std::string& extension_id,
334 const std::string& device_address,
335 const ErrorCallback& error_callback,
336 device::BluetoothDevice::ConnectErrorCode error_code);
338 // Called by BluetoothGattCharacteristic in response to a call to
339 // StartNotifySession.
340 void OnStartNotifySession(
341 bool persistent,
342 const std::string& extension_id,
343 const std::string& characteristic_id,
344 const base::Closure& callback,
345 scoped_ptr<device::BluetoothGattNotifySession> session);
347 // Called by BluetoothGattCharacteristic in response to a call to
348 // StartNotifySession.
349 void OnStartNotifySessionError(
350 const std::string& extension_id,
351 const std::string& characteristic_id,
352 const ErrorCallback& error_callback,
353 device::BluetoothGattService::GattErrorCode error_code);
355 // Called by BluetoothGattNotifySession in response to a call to Stop.
356 void OnStopNotifySession(const std::string& extension_id,
357 const std::string& characteristic_id,
358 const base::Closure& callback);
360 // Finds and returns a BluetoothLowEnergyConnection to device with address
361 // |device_address| from the managed API resources for extension with ID
362 // |extension_id|.
363 BluetoothLowEnergyConnection* FindConnection(
364 const std::string& extension_id,
365 const std::string& device_address);
367 // Removes the connection to device with address |device_address| from the
368 // managed API resources for extension with ID |extension_id|. Returns false,
369 // if the connection could not be found.
370 bool RemoveConnection(const std::string& extension_id,
371 const std::string& device_address);
373 // Finds and returns a BluetoothLowEnergyNotifySession associated with
374 // characteristic with instance ID |characteristic_id| from the managed API
375 // API resources for extension with ID |extension_id|.
376 BluetoothLowEnergyNotifySession* FindNotifySession(
377 const std::string& extension_id,
378 const std::string& characteristic_id);
380 // Removes the notify session associated with characteristic with
381 // instance ID |characteristic_id| from the managed API resources for
382 // extension with ID |extension_id|. Returns false, if the session could
383 // not be found.
384 bool RemoveNotifySession(const std::string& extension_id,
385 const std::string& characteristic_id);
387 // Mapping from instance ids to identifiers of owning instances. The keys are
388 // used to identify individual instances of GATT objects and are used by
389 // bluetoothLowEnergy API functions to obtain the correct GATT object to
390 // operate on. Instance IDs are string identifiers that are returned by the
391 // device/bluetooth API, by calling GetIdentifier() on the corresponding
392 // device::BluetoothGatt* instance.
394 // This mapping is necessary, as GATT object instances can only be obtained
395 // from the object that owns it, where raw pointers should not be cached. E.g.
396 // to obtain a device::BluetoothGattCharacteristic, it is necessary to obtain
397 // a pointer to the associated device::BluetoothDevice, and then to the
398 // device::BluetoothGattService that owns the characteristic.
399 typedef std::map<std::string, std::string> InstanceIdMap;
400 InstanceIdMap service_id_to_device_address_;
401 InstanceIdMap chrc_id_to_service_id_;
402 InstanceIdMap desc_id_to_chrc_id_;
404 // Pointer to the current BluetoothAdapter instance. This represents a local
405 // Bluetooth adapter of the system.
406 scoped_refptr<device::BluetoothAdapter> adapter_;
408 // Set of extension ID + device addresses to which a connect/disconnect is
409 // currently pending.
410 std::set<std::string> connecting_devices_;
411 std::set<std::string> disconnecting_devices_;
413 // Set of extension ID + characteristic ID to which a request to start a
414 // notify session is currently pending.
415 std::set<std::string> pending_session_calls_;
417 // BrowserContext passed during initialization.
418 content::BrowserContext* browser_context_;
420 // Note: This should remain the last member so it'll be destroyed and
421 // invalidate its weak pointers before any other members are destroyed.
422 base::WeakPtrFactory<BluetoothLowEnergyEventRouter> weak_ptr_factory_;
424 DISALLOW_COPY_AND_ASSIGN(BluetoothLowEnergyEventRouter);
427 } // namespace extensions
429 #endif // EXTENSIONS_BROWSER_API_BLUETOOTH_LOW_ENERGY_BLUETOOTH_LOW_ENERGY_EVENT_ROUTER_H_