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