third_party: Add OWNERS for re2 library.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter_chromeos.h
blob13942d8c387bd16ea96207c0bf377b2d2cb2fdd2
1 // Copyright 2013 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 DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_
8 #include <map>
9 #include <queue>
10 #include <string>
11 #include <utility>
12 #include <vector>
14 #include "base/memory/weak_ptr.h"
15 #include "chromeos/dbus/bluetooth_adapter_client.h"
16 #include "chromeos/dbus/bluetooth_agent_service_provider.h"
17 #include "chromeos/dbus/bluetooth_device_client.h"
18 #include "chromeos/dbus/bluetooth_input_client.h"
19 #include "chromeos/dbus/bluetooth_profile_manager_client.h"
20 #include "chromeos/dbus/bluetooth_profile_service_provider.h"
21 #include "dbus/object_path.h"
22 #include "device/bluetooth/bluetooth_adapter.h"
23 #include "device/bluetooth/bluetooth_audio_sink.h"
24 #include "device/bluetooth/bluetooth_device.h"
25 #include "device/bluetooth/bluetooth_discovery_session.h"
26 #include "device/bluetooth/bluetooth_export.h"
28 namespace base {
29 class SequencedTaskRunner;
30 } // namespace base
32 namespace device {
33 class BluetoothSocketThread;
34 } // namespace device
36 namespace chromeos {
38 class BluetoothChromeOSTest;
39 class BluetoothAdapterProfileChromeOS;
40 class BluetoothDeviceChromeOS;
41 class BluetoothPairingChromeOS;
42 class BluetoothRemoteGattCharacteristicChromeOS;
43 class BluetoothRemoteGattDescriptorChromeOS;
44 class BluetoothRemoteGattServiceChromeOS;
46 // The BluetoothAdapterChromeOS class implements BluetoothAdapter for the
47 // Chrome OS platform.
49 // Methods tolerate a shutdown scenario where BluetoothAdapterChromeOS::Shutdown
50 // causes IsPresent to return false just before the dbus system is shutdown but
51 // while references to the BluetoothAdapterChromeOS object still exists.
53 // When adding methods to this class verify shutdown behavior in
54 // BluetoothChromeOSTest, Shutdown.
55 class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterChromeOS
56 : public device::BluetoothAdapter,
57 public chromeos::BluetoothAdapterClient::Observer,
58 public chromeos::BluetoothDeviceClient::Observer,
59 public chromeos::BluetoothInputClient::Observer,
60 public chromeos::BluetoothAgentServiceProvider::Delegate {
61 public:
62 typedef base::Callback<void(const std::string& error_message)>
63 ErrorCompletionCallback;
64 typedef base::Callback<void(BluetoothAdapterProfileChromeOS* profile)>
65 ProfileRegisteredCallback;
67 static base::WeakPtr<BluetoothAdapter> CreateAdapter();
69 // BluetoothAdapter:
70 void Shutdown() override;
71 std::string GetAddress() const override;
72 std::string GetName() const override;
73 void SetName(const std::string& name,
74 const base::Closure& callback,
75 const ErrorCallback& error_callback) override;
76 bool IsInitialized() const override;
77 bool IsPresent() const override;
78 bool IsPowered() const override;
79 void SetPowered(bool powered,
80 const base::Closure& callback,
81 const ErrorCallback& error_callback) override;
82 bool IsDiscoverable() const override;
83 void SetDiscoverable(bool discoverable,
84 const base::Closure& callback,
85 const ErrorCallback& error_callback) override;
86 bool IsDiscovering() const override;
87 void CreateRfcommService(
88 const device::BluetoothUUID& uuid,
89 const ServiceOptions& options,
90 const CreateServiceCallback& callback,
91 const CreateServiceErrorCallback& error_callback) override;
92 void CreateL2capService(
93 const device::BluetoothUUID& uuid,
94 const ServiceOptions& options,
95 const CreateServiceCallback& callback,
96 const CreateServiceErrorCallback& error_callback) override;
97 void RegisterAudioSink(
98 const device::BluetoothAudioSink::Options& options,
99 const device::BluetoothAdapter::AcquiredCallback& callback,
100 const device::BluetoothAudioSink::ErrorCallback& error_callback) override;
102 void RegisterAdvertisement(
103 scoped_ptr<device::BluetoothAdvertisement::Data> advertisement_data,
104 const CreateAdvertisementCallback& callback,
105 const CreateAdvertisementErrorCallback& error_callback) override;
107 // Locates the device object by object path (the devices map and
108 // BluetoothDevice methods are by address).
109 BluetoothDeviceChromeOS* GetDeviceWithPath(
110 const dbus::ObjectPath& object_path);
112 // Announce to observers a change in device state that is not reflected by
113 // its D-Bus properties.
114 void NotifyDeviceChanged(BluetoothDeviceChromeOS* device);
116 // The following methods are used to send various GATT observer events to
117 // observers.
118 void NotifyGattServiceAdded(BluetoothRemoteGattServiceChromeOS* service);
119 void NotifyGattServiceRemoved(BluetoothRemoteGattServiceChromeOS* service);
120 void NotifyGattServiceChanged(BluetoothRemoteGattServiceChromeOS* service);
121 void NotifyGattDiscoveryComplete(BluetoothRemoteGattServiceChromeOS* service);
122 void NotifyGattCharacteristicAdded(
123 BluetoothRemoteGattCharacteristicChromeOS* characteristic);
124 void NotifyGattCharacteristicRemoved(
125 BluetoothRemoteGattCharacteristicChromeOS* characteristic);
126 void NotifyGattDescriptorAdded(
127 BluetoothRemoteGattDescriptorChromeOS* descriptor);
128 void NotifyGattDescriptorRemoved(
129 BluetoothRemoteGattDescriptorChromeOS* descriptor);
130 void NotifyGattCharacteristicValueChanged(
131 BluetoothRemoteGattCharacteristicChromeOS* characteristic,
132 const std::vector<uint8>& value);
133 void NotifyGattDescriptorValueChanged(
134 BluetoothRemoteGattDescriptorChromeOS* descriptor,
135 const std::vector<uint8>& value);
137 // Returns the object path of the adapter.
138 const dbus::ObjectPath& object_path() const { return object_path_; }
140 // Request a profile on the adapter for a custom service with a
141 // specific UUID for the device at |device_path| to be sent to |delegate|.
142 // If |device_path| is the empty string, incoming connections will be
143 // assigned to |delegate|. When the profile is
144 // successfully registered, |success_callback| will be called with a pointer
145 // to the profile which is managed by BluetoothAdapterChromeOS. On failure,
146 // |error_callback| will be called.
147 void UseProfile(const device::BluetoothUUID& uuid,
148 const dbus::ObjectPath& device_path,
149 const BluetoothProfileManagerClient::Options& options,
150 BluetoothProfileServiceProvider::Delegate* delegate,
151 const ProfileRegisteredCallback& success_callback,
152 const ErrorCompletionCallback& error_callback);
154 // Release use of a profile by a device.
155 void ReleaseProfile(const dbus::ObjectPath& device_path,
156 BluetoothAdapterProfileChromeOS* profile);
158 protected:
159 // BluetoothAdapter:
160 void RemovePairingDelegateInternal(
161 device::BluetoothDevice::PairingDelegate* pairing_delegate) override;
163 private:
164 friend class BluetoothChromeOSTest;
165 friend class BluetoothChromeOSTest_Shutdown_Test;
166 friend class BluetoothChromeOSTest_Shutdown_OnStartDiscovery_Test;
167 friend class BluetoothChromeOSTest_Shutdown_OnStartDiscoveryError_Test;
168 friend class BluetoothChromeOSTest_Shutdown_OnStopDiscovery_Test;
169 friend class BluetoothChromeOSTest_Shutdown_OnStopDiscoveryError_Test;
171 // typedef for callback parameters that are passed to AddDiscoverySession
172 // and RemoveDiscoverySession. This is used to queue incoming requests while
173 // a call to BlueZ is pending.
174 typedef std::tuple<device::BluetoothDiscoveryFilter*,
175 base::Closure,
176 ErrorCallback> DiscoveryParamTuple;
177 typedef std::queue<DiscoveryParamTuple> DiscoveryCallbackQueue;
179 // Callback pair for the profile registration queue.
180 typedef std::pair<base::Closure, ErrorCompletionCallback>
181 RegisterProfileCompletionPair;
183 BluetoothAdapterChromeOS();
184 ~BluetoothAdapterChromeOS() override;
186 // BluetoothAdapterClient::Observer override.
187 void AdapterAdded(const dbus::ObjectPath& object_path) override;
188 void AdapterRemoved(const dbus::ObjectPath& object_path) override;
189 void AdapterPropertyChanged(const dbus::ObjectPath& object_path,
190 const std::string& property_name) override;
192 // BluetoothDeviceClient::Observer override.
193 void DeviceAdded(const dbus::ObjectPath& object_path) override;
194 void DeviceRemoved(const dbus::ObjectPath& object_path) override;
195 void DevicePropertyChanged(const dbus::ObjectPath& object_path,
196 const std::string& property_name) override;
198 // BluetoothInputClient::Observer override.
199 void InputPropertyChanged(const dbus::ObjectPath& object_path,
200 const std::string& property_name) override;
202 // BluetoothAgentServiceProvider::Delegate override.
203 void Released() override;
204 void RequestPinCode(const dbus::ObjectPath& device_path,
205 const PinCodeCallback& callback) override;
206 void DisplayPinCode(const dbus::ObjectPath& device_path,
207 const std::string& pincode) override;
208 void RequestPasskey(const dbus::ObjectPath& device_path,
209 const PasskeyCallback& callback) override;
210 void DisplayPasskey(const dbus::ObjectPath& device_path,
211 uint32 passkey,
212 uint16 entered) override;
213 void RequestConfirmation(const dbus::ObjectPath& device_path,
214 uint32 passkey,
215 const ConfirmationCallback& callback) override;
216 void RequestAuthorization(const dbus::ObjectPath& device_path,
217 const ConfirmationCallback& callback) override;
218 void AuthorizeService(const dbus::ObjectPath& device_path,
219 const std::string& uuid,
220 const ConfirmationCallback& callback) override;
221 void Cancel() override;
223 // Called by dbus:: on completion of the D-Bus method call to register the
224 // pairing agent.
225 void OnRegisterAgent();
226 void OnRegisterAgentError(const std::string& error_name,
227 const std::string& error_message);
229 // Called by dbus:: on completion of the D-Bus method call to request that
230 // the pairing agent be made the default.
231 void OnRequestDefaultAgent();
232 void OnRequestDefaultAgentError(const std::string& error_name,
233 const std::string& error_message);
235 // Called by BluetoothAudioSinkChromeOS on completion of registering an audio
236 // sink.
237 void OnRegisterAudioSink(
238 const device::BluetoothAdapter::AcquiredCallback& callback,
239 const device::BluetoothAudioSink::ErrorCallback& error_callback,
240 scoped_refptr<device::BluetoothAudioSink> audio_sink);
242 // Internal method to obtain a BluetoothPairingChromeOS object for the device
243 // with path |object_path|. Returns the existing pairing object if the device
244 // already has one (usually an outgoing connection in progress) or a new
245 // pairing object with the default pairing delegate if not. If no default
246 // pairing object exists, NULL will be returned.
247 BluetoothPairingChromeOS* GetPairing(const dbus::ObjectPath& object_path);
249 // Set the tracked adapter to the one in |object_path|, this object will
250 // subsequently operate on that adapter until it is removed.
251 void SetAdapter(const dbus::ObjectPath& object_path);
253 // Set the adapter name to one chosen from the system information.
254 void SetDefaultAdapterName();
256 // Remove the currently tracked adapter. IsPresent() will return false after
257 // this is called.
258 void RemoveAdapter();
260 // Announce to observers a change in the adapter state.
261 void PoweredChanged(bool powered);
262 void DiscoverableChanged(bool discoverable);
263 void DiscoveringChanged(bool discovering);
264 void PresentChanged(bool present);
266 // Called by dbus:: on completion of the discoverable property change.
267 void OnSetDiscoverable(const base::Closure& callback,
268 const ErrorCallback& error_callback,
269 bool success);
271 // Called by dbus:: on completion of an adapter property change.
272 void OnPropertyChangeCompleted(const base::Closure& callback,
273 const ErrorCallback& error_callback,
274 bool success);
276 // BluetoothAdapter:
277 void AddDiscoverySession(device::BluetoothDiscoveryFilter* discovery_filter,
278 const base::Closure& callback,
279 const ErrorCallback& error_callback) override;
280 void RemoveDiscoverySession(
281 device::BluetoothDiscoveryFilter* discovery_filter,
282 const base::Closure& callback,
283 const ErrorCallback& error_callback) override;
284 void SetDiscoveryFilter(
285 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter,
286 const base::Closure& callback,
287 const ErrorCallback& error_callback) override;
289 // Called by dbus:: on completion of the D-Bus method call to start discovery.
290 void OnStartDiscovery(const base::Closure& callback,
291 const ErrorCallback& error_callback);
292 void OnStartDiscoveryError(const base::Closure& callback,
293 const ErrorCallback& error_callback,
294 const std::string& error_name,
295 const std::string& error_message);
297 // Called by dbus:: on completion of the D-Bus method call to stop discovery.
298 void OnStopDiscovery(const base::Closure& callback);
299 void OnStopDiscoveryError(const ErrorCallback& error_callback,
300 const std::string& error_name,
301 const std::string& error_message);
303 void OnPreSetDiscoveryFilter(const base::Closure& callback,
304 const ErrorCallback& error_callback);
305 void OnPreSetDiscoveryFilterError(const base::Closure& callback,
306 const ErrorCallback& error_callback);
307 void OnSetDiscoveryFilter(const base::Closure& callback,
308 const ErrorCallback& error_callback);
309 void OnSetDiscoveryFilterError(const base::Closure& callback,
310 const ErrorCallback& error_callback,
311 const std::string& error_name,
312 const std::string& error_message);
314 // Called by dbus:: on completion of the D-Bus method to register a profile.
315 void OnRegisterProfile(const device::BluetoothUUID& uuid,
316 scoped_ptr<BluetoothAdapterProfileChromeOS> profile);
318 void SetProfileDelegate(const device::BluetoothUUID& uuid,
319 const dbus::ObjectPath& device_path,
320 BluetoothProfileServiceProvider::Delegate* delegate,
321 const ProfileRegisteredCallback& success_callback,
322 const ErrorCompletionCallback& error_callback);
323 void OnRegisterProfileError(const device::BluetoothUUID& uuid,
324 const std::string& error_name,
325 const std::string& error_message);
327 // Called by BluetoothAdapterProfileChromeOS when no users of a profile
328 // remain.
329 void RemoveProfile(const device::BluetoothUUID& uuid);
331 // Processes the queued discovery requests. For each DiscoveryParamTuple in
332 // the queue, this method will try to add a new discovery session. This method
333 // is called whenever a pending D-Bus call to start or stop discovery has
334 // ended (with either success or failure).
335 void ProcessQueuedDiscoveryRequests();
337 // Set in |Shutdown()|, makes IsPresent()| return false.
338 bool dbus_is_shutdown_;
340 // Number of discovery sessions that have been added.
341 int num_discovery_sessions_;
343 // True, if there is a pending request to start or stop discovery.
344 bool discovery_request_pending_;
346 // List of queued requests to add new discovery sessions. While there is a
347 // pending request to BlueZ to start or stop discovery, many requests from
348 // within Chrome to start or stop discovery sessions may occur. We only
349 // queue requests to add new sessions to be processed later. All requests to
350 // remove a session while a call is pending immediately return failure. Note
351 // that since BlueZ keeps its own reference count of applications that have
352 // requested discovery, dropping our count to 0 won't necessarily result in
353 // the controller actually stopping discovery if, for example, an application
354 // other than Chrome, such as bt_console, was also used to start discovery.
355 DiscoveryCallbackQueue discovery_request_queue_;
357 // Object path of the adapter we track.
358 dbus::ObjectPath object_path_;
360 // Instance of the D-Bus agent object used for pairing, initialized with
361 // our own class as its delegate.
362 scoped_ptr<BluetoothAgentServiceProvider> agent_;
364 // UI thread task runner and socket thread object used to create sockets.
365 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
366 scoped_refptr<device::BluetoothSocketThread> socket_thread_;
368 // The profiles we have registered with the bluetooth daemon.
369 std::map<device::BluetoothUUID, BluetoothAdapterProfileChromeOS*> profiles_;
371 // Queue of delegates waiting for a profile to register.
372 std::map<device::BluetoothUUID, std::vector<RegisterProfileCompletionPair>*>
373 profile_queues_;
375 scoped_ptr<device::BluetoothDiscoveryFilter> current_filter_;
377 // Note: This should remain the last member so it'll be destroyed and
378 // invalidate its weak pointers before any other members are destroyed.
379 base::WeakPtrFactory<BluetoothAdapterChromeOS> weak_ptr_factory_;
381 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterChromeOS);
384 } // namespace chromeos
386 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_