Docs: Update quota mgmt API docs to use methods that don't throw console warnings.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter_chromeos.h
blob39a23dec2c7a0edae10ec76aae2e890754746b55
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 void DeleteOnCorrectThread() const override;
69 void AddObserver(device::BluetoothAdapter::Observer* observer) override;
70 void RemoveObserver(device::BluetoothAdapter::Observer* observer) 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 // Locates the device object by object path (the devices map and
103 // BluetoothDevice methods are by address).
104 BluetoothDeviceChromeOS* GetDeviceWithPath(
105 const dbus::ObjectPath& object_path);
107 // Announce to observers a change in device state that is not reflected by
108 // its D-Bus properties.
109 void NotifyDeviceChanged(BluetoothDeviceChromeOS* device);
111 // The following methods are used to send various GATT observer events to
112 // observers.
113 void NotifyGattServiceAdded(BluetoothRemoteGattServiceChromeOS* service);
114 void NotifyGattServiceRemoved(BluetoothRemoteGattServiceChromeOS* service);
115 void NotifyGattServiceChanged(BluetoothRemoteGattServiceChromeOS* service);
116 void NotifyGattDiscoveryComplete(BluetoothRemoteGattServiceChromeOS* service);
117 void NotifyGattCharacteristicAdded(
118 BluetoothRemoteGattCharacteristicChromeOS* characteristic);
119 void NotifyGattCharacteristicRemoved(
120 BluetoothRemoteGattCharacteristicChromeOS* characteristic);
121 void NotifyGattDescriptorAdded(
122 BluetoothRemoteGattDescriptorChromeOS* descriptor);
123 void NotifyGattDescriptorRemoved(
124 BluetoothRemoteGattDescriptorChromeOS* descriptor);
125 void NotifyGattCharacteristicValueChanged(
126 BluetoothRemoteGattCharacteristicChromeOS* characteristic,
127 const std::vector<uint8>& value);
128 void NotifyGattDescriptorValueChanged(
129 BluetoothRemoteGattDescriptorChromeOS* descriptor,
130 const std::vector<uint8>& value);
132 // Returns the object path of the adapter.
133 const dbus::ObjectPath& object_path() const { return object_path_; }
135 // Request a profile on the adapter for a custom service with a
136 // specific UUID for the device at |device_path| to be sent to |delegate|.
137 // If |device_path| is the empty string, incoming connections will be
138 // assigned to |delegate|. When the profile is
139 // successfully registered, |success_callback| will be called with a pointer
140 // to the profile which is managed by BluetoothAdapterChromeOS. On failure,
141 // |error_callback| will be called.
142 void UseProfile(const device::BluetoothUUID& uuid,
143 const dbus::ObjectPath& device_path,
144 const BluetoothProfileManagerClient::Options& options,
145 BluetoothProfileServiceProvider::Delegate* delegate,
146 const ProfileRegisteredCallback& success_callback,
147 const ErrorCompletionCallback& error_callback);
149 // Release use of a profile by a device.
150 void ReleaseProfile(const dbus::ObjectPath& device_path,
151 BluetoothAdapterProfileChromeOS* profile);
153 protected:
154 // BluetoothAdapter:
155 void RemovePairingDelegateInternal(
156 device::BluetoothDevice::PairingDelegate* pairing_delegate) override;
158 private:
159 friend class base::DeleteHelper<BluetoothAdapterChromeOS>;
160 friend class BluetoothChromeOSTest;
161 friend class BluetoothChromeOSTest_Shutdown_Test;
162 friend class BluetoothChromeOSTest_Shutdown_OnStartDiscovery_Test;
163 friend class BluetoothChromeOSTest_Shutdown_OnStartDiscoveryError_Test;
164 friend class BluetoothChromeOSTest_Shutdown_OnStopDiscovery_Test;
165 friend class BluetoothChromeOSTest_Shutdown_OnStopDiscoveryError_Test;
167 // typedef for callback parameters that are passed to AddDiscoverySession
168 // and RemoveDiscoverySession. This is used to queue incoming requests while
169 // a call to BlueZ is pending.
170 typedef std::pair<base::Closure, ErrorCallback> DiscoveryCallbackPair;
171 typedef std::queue<DiscoveryCallbackPair> DiscoveryCallbackQueue;
173 BluetoothAdapterChromeOS();
174 ~BluetoothAdapterChromeOS() override;
176 // BluetoothAdapterClient::Observer override.
177 void AdapterAdded(const dbus::ObjectPath& object_path) override;
178 void AdapterRemoved(const dbus::ObjectPath& object_path) override;
179 void AdapterPropertyChanged(const dbus::ObjectPath& object_path,
180 const std::string& property_name) override;
182 // BluetoothDeviceClient::Observer override.
183 void DeviceAdded(const dbus::ObjectPath& object_path) override;
184 void DeviceRemoved(const dbus::ObjectPath& object_path) override;
185 void DevicePropertyChanged(const dbus::ObjectPath& object_path,
186 const std::string& property_name) override;
188 // BluetoothInputClient::Observer override.
189 void InputPropertyChanged(const dbus::ObjectPath& object_path,
190 const std::string& property_name) override;
192 // BluetoothAgentServiceProvider::Delegate override.
193 void Released() override;
194 void RequestPinCode(const dbus::ObjectPath& device_path,
195 const PinCodeCallback& callback) override;
196 void DisplayPinCode(const dbus::ObjectPath& device_path,
197 const std::string& pincode) override;
198 void RequestPasskey(const dbus::ObjectPath& device_path,
199 const PasskeyCallback& callback) override;
200 void DisplayPasskey(const dbus::ObjectPath& device_path,
201 uint32 passkey,
202 uint16 entered) override;
203 void RequestConfirmation(const dbus::ObjectPath& device_path,
204 uint32 passkey,
205 const ConfirmationCallback& callback) override;
206 void RequestAuthorization(const dbus::ObjectPath& device_path,
207 const ConfirmationCallback& callback) override;
208 void AuthorizeService(const dbus::ObjectPath& device_path,
209 const std::string& uuid,
210 const ConfirmationCallback& callback) override;
211 void Cancel() override;
213 // Called by dbus:: on completion of the D-Bus method call to register the
214 // pairing agent.
215 void OnRegisterAgent();
216 void OnRegisterAgentError(const std::string& error_name,
217 const std::string& error_message);
219 // Called by dbus:: on completion of the D-Bus method call to request that
220 // the pairing agent be made the default.
221 void OnRequestDefaultAgent();
222 void OnRequestDefaultAgentError(const std::string& error_name,
223 const std::string& error_message);
225 // Called by BluetoothAudioSinkChromeOS on completion of registering an audio
226 // sink.
227 void OnRegisterAudioSink(
228 const device::BluetoothAdapter::AcquiredCallback& callback,
229 const device::BluetoothAudioSink::ErrorCallback& error_callback,
230 scoped_refptr<device::BluetoothAudioSink> audio_sink);
232 // Internal method to obtain a BluetoothPairingChromeOS object for the device
233 // with path |object_path|. Returns the existing pairing object if the device
234 // already has one (usually an outgoing connection in progress) or a new
235 // pairing object with the default pairing delegate if not. If no default
236 // pairing object exists, NULL will be returned.
237 BluetoothPairingChromeOS* GetPairing(const dbus::ObjectPath& object_path);
239 // Set the tracked adapter to the one in |object_path|, this object will
240 // subsequently operate on that adapter until it is removed.
241 void SetAdapter(const dbus::ObjectPath& object_path);
243 // Set the adapter name to one chosen from the system information.
244 void SetDefaultAdapterName();
246 // Remove the currently tracked adapter. IsPresent() will return false after
247 // this is called.
248 void RemoveAdapter();
250 // Announce to observers a change in the adapter state.
251 void PoweredChanged(bool powered);
252 void DiscoverableChanged(bool discoverable);
253 void DiscoveringChanged(bool discovering);
254 void PresentChanged(bool present);
256 // Called by dbus:: on completion of the discoverable property change.
257 void OnSetDiscoverable(const base::Closure& callback,
258 const ErrorCallback& error_callback,
259 bool success);
261 // Called by dbus:: on completion of an adapter property change.
262 void OnPropertyChangeCompleted(const base::Closure& callback,
263 const ErrorCallback& error_callback,
264 bool success);
266 // BluetoothAdapter:
267 void AddDiscoverySession(device::BluetoothDiscoveryFilter* discovery_filter,
268 const base::Closure& callback,
269 const ErrorCallback& error_callback) override;
270 void RemoveDiscoverySession(
271 device::BluetoothDiscoveryFilter* discovery_filter,
272 const base::Closure& callback,
273 const ErrorCallback& error_callback) override;
274 void SetDiscoveryFilter(
275 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter,
276 const base::Closure& callback,
277 const ErrorCallback& error_callback) override;
279 // Called by dbus:: on completion of the D-Bus method call to start discovery.
280 void OnStartDiscovery(const base::Closure& callback,
281 const ErrorCallback& error_callback);
282 void OnStartDiscoveryError(const base::Closure& callback,
283 const ErrorCallback& error_callback,
284 const std::string& error_name,
285 const std::string& error_message);
287 // Called by dbus:: on completion of the D-Bus method call to stop discovery.
288 void OnStopDiscovery(const base::Closure& callback);
289 void OnStopDiscoveryError(const ErrorCallback& error_callback,
290 const std::string& error_name,
291 const std::string& error_message);
293 // Called by dbus:: on completion of the D-Bus method to register a profile.
294 void OnRegisterProfile(const device::BluetoothUUID& uuid,
295 scoped_ptr<BluetoothAdapterProfileChromeOS> profile);
297 void SetProfileDelegate(const device::BluetoothUUID& uuid,
298 const dbus::ObjectPath& device_path,
299 BluetoothProfileServiceProvider::Delegate* delegate,
300 const ProfileRegisteredCallback& success_callback,
301 const ErrorCompletionCallback& error_callback);
302 void OnRegisterProfileError(const device::BluetoothUUID& uuid,
303 const std::string& error_name,
304 const std::string& error_message);
306 // Called by BluetoothAdapterProfileChromeOS when no users of a profile
307 // remain.
308 void RemoveProfile(const device::BluetoothUUID& uuid);
310 // Processes the queued discovery requests. For each DiscoveryCallbackPair in
311 // the queue, this method will try to add a new discovery session. This method
312 // is called whenever a pending D-Bus call to start or stop discovery has
313 // ended (with either success or failure).
314 void ProcessQueuedDiscoveryRequests();
316 // Set in |Shutdown()|, makes IsPresent()| return false.
317 bool dbus_is_shutdown_;
319 // Number of discovery sessions that have been added.
320 int num_discovery_sessions_;
322 // True, if there is a pending request to start or stop discovery.
323 bool discovery_request_pending_;
325 // List of queued requests to add new discovery sessions. While there is a
326 // pending request to BlueZ to start or stop discovery, many requests from
327 // within Chrome to start or stop discovery sessions may occur. We only
328 // queue requests to add new sessions to be processed later. All requests to
329 // remove a session while a call is pending immediately return failure. Note
330 // that since BlueZ keeps its own reference count of applications that have
331 // requested discovery, dropping our count to 0 won't necessarily result in
332 // the controller actually stopping discovery if, for example, an application
333 // other than Chrome, such as bt_console, was also used to start discovery.
334 DiscoveryCallbackQueue discovery_request_queue_;
336 // Object path of the adapter we track.
337 dbus::ObjectPath object_path_;
339 // List of observers interested in event notifications from us.
340 ObserverList<device::BluetoothAdapter::Observer> observers_;
342 // Instance of the D-Bus agent object used for pairing, initialized with
343 // our own class as its delegate.
344 scoped_ptr<BluetoothAgentServiceProvider> agent_;
346 // UI thread task runner and socket thread object used to create sockets.
347 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
348 scoped_refptr<device::BluetoothSocketThread> socket_thread_;
350 // The profiles we have registered with the bluetooth daemon.
351 std::map<device::BluetoothUUID, BluetoothAdapterProfileChromeOS*> profiles_;
353 // Callback pair for the profile registration queue.
354 typedef std::pair<base::Closure, ErrorCompletionCallback>
355 RegisterProfileCompletionPair;
357 // Queue of delegates waiting for a profile to register.
358 std::map<device::BluetoothUUID, std::vector<RegisterProfileCompletionPair>*>
359 profile_queues_;
361 // Note: This should remain the last member so it'll be destroyed and
362 // invalidate its weak pointers before any other members are destroyed.
363 base::WeakPtrFactory<BluetoothAdapterChromeOS> weak_ptr_factory_;
365 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterChromeOS);
368 } // namespace chromeos
370 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_