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