Revert of [Android WebView] Synthesize a fake page loading event on page source modif...
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter_chromeos.h
blob61e4a3a3da9c3c12e5545a96e13a7a059cb80b00
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_export.h"
28 namespace device {
29 class BluetoothSocketThread;
30 } // namespace device
32 namespace chromeos {
34 class BluetoothChromeOSTest;
35 class BluetoothAdapterProfileChromeOS;
36 class BluetoothDeviceChromeOS;
37 class BluetoothPairingChromeOS;
38 class BluetoothRemoteGattCharacteristicChromeOS;
39 class BluetoothRemoteGattDescriptorChromeOS;
40 class BluetoothRemoteGattServiceChromeOS;
42 // The BluetoothAdapterChromeOS class implements BluetoothAdapter for the
43 // Chrome OS platform.
45 // Methods tolerate a shutdown scenario where BluetoothAdapterChromeOS::Shutdown
46 // causes IsPresent to return false just before the dbus system is shutdown but
47 // while references to the BluetoothAdapterChromeOS object still exists.
49 // When adding methods to this class verify shutdown behavior in
50 // BluetoothChromeOSTest, Shutdown.
51 class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterChromeOS
52 : public device::BluetoothAdapter,
53 public chromeos::BluetoothAdapterClient::Observer,
54 public chromeos::BluetoothDeviceClient::Observer,
55 public chromeos::BluetoothInputClient::Observer,
56 public chromeos::BluetoothAgentServiceProvider::Delegate {
57 public:
58 typedef base::Callback<void(const std::string& error_message)>
59 ErrorCompletionCallback;
60 typedef base::Callback<void(BluetoothAdapterProfileChromeOS* profile)>
61 ProfileRegisteredCallback;
63 static base::WeakPtr<BluetoothAdapter> CreateAdapter();
65 // BluetoothAdapter:
66 void Shutdown() override;
67 void DeleteOnCorrectThread() const override;
68 void AddObserver(device::BluetoothAdapter::Observer* observer) override;
69 void RemoveObserver(device::BluetoothAdapter::Observer* observer) override;
70 std::string GetAddress() const override;
71 std::string GetName() const override;
72 void SetName(const std::string& name,
73 const base::Closure& callback,
74 const ErrorCallback& error_callback) override;
75 bool IsInitialized() const override;
76 bool IsPresent() const override;
77 bool IsPowered() const override;
78 void SetPowered(bool powered,
79 const base::Closure& callback,
80 const ErrorCallback& error_callback) override;
81 bool IsDiscoverable() const override;
82 void SetDiscoverable(bool discoverable,
83 const base::Closure& callback,
84 const ErrorCallback& error_callback) override;
85 bool IsDiscovering() const override;
86 void CreateRfcommService(
87 const device::BluetoothUUID& uuid,
88 const ServiceOptions& options,
89 const CreateServiceCallback& callback,
90 const CreateServiceErrorCallback& error_callback) override;
91 void CreateL2capService(
92 const device::BluetoothUUID& uuid,
93 const ServiceOptions& options,
94 const CreateServiceCallback& callback,
95 const CreateServiceErrorCallback& error_callback) override;
96 void RegisterAudioSink(
97 const device::BluetoothAudioSink::Options& options,
98 const device::BluetoothAdapter::AcquiredCallback& callback,
99 const device::BluetoothAudioSink::ErrorCallback& error_callback) override;
101 // Locates the device object by object path (the devices map and
102 // BluetoothDevice methods are by address).
103 BluetoothDeviceChromeOS* GetDeviceWithPath(
104 const dbus::ObjectPath& object_path);
106 // Announce to observers a change in device state that is not reflected by
107 // its D-Bus properties.
108 void NotifyDeviceChanged(BluetoothDeviceChromeOS* device);
110 // The following methods are used to send various GATT observer events to
111 // observers.
112 void NotifyGattServiceAdded(BluetoothRemoteGattServiceChromeOS* service);
113 void NotifyGattServiceRemoved(BluetoothRemoteGattServiceChromeOS* service);
114 void NotifyGattServiceChanged(BluetoothRemoteGattServiceChromeOS* service);
115 void NotifyGattDiscoveryComplete(BluetoothRemoteGattServiceChromeOS* service);
116 void NotifyGattCharacteristicAdded(
117 BluetoothRemoteGattCharacteristicChromeOS* characteristic);
118 void NotifyGattCharacteristicRemoved(
119 BluetoothRemoteGattCharacteristicChromeOS* characteristic);
120 void NotifyGattDescriptorAdded(
121 BluetoothRemoteGattDescriptorChromeOS* descriptor);
122 void NotifyGattDescriptorRemoved(
123 BluetoothRemoteGattDescriptorChromeOS* descriptor);
124 void NotifyGattCharacteristicValueChanged(
125 BluetoothRemoteGattCharacteristicChromeOS* characteristic,
126 const std::vector<uint8>& value);
127 void NotifyGattDescriptorValueChanged(
128 BluetoothRemoteGattDescriptorChromeOS* descriptor,
129 const std::vector<uint8>& value);
131 // Returns the object path of the adapter.
132 const dbus::ObjectPath& object_path() const { return object_path_; }
134 // Request a profile on the adapter for a custom service with a
135 // specific UUID for the device at |device_path| to be sent to |delegate|.
136 // If |device_path| is the empty string, incoming connections will be
137 // assigned to |delegate|. When the profile is
138 // successfully registered, |success_callback| will be called with a pointer
139 // to the profile which is managed by BluetoothAdapterChromeOS. On failure,
140 // |error_callback| will be called.
141 void UseProfile(const device::BluetoothUUID& uuid,
142 const dbus::ObjectPath& device_path,
143 const BluetoothProfileManagerClient::Options& options,
144 BluetoothProfileServiceProvider::Delegate* delegate,
145 const ProfileRegisteredCallback& success_callback,
146 const ErrorCompletionCallback& error_callback);
148 // Releases the profile associated with |uuid|
149 void ReleaseProfile(const device::BluetoothUUID& uuid);
151 protected:
152 // BluetoothAdapter:
153 void RemovePairingDelegateInternal(
154 device::BluetoothDevice::PairingDelegate* pairing_delegate) override;
156 private:
157 friend class base::DeleteHelper<BluetoothAdapterChromeOS>;
158 friend class BluetoothChromeOSTest;
159 friend class BluetoothChromeOSTest_Shutdown_Test;
160 friend class BluetoothChromeOSTest_Shutdown_OnStartDiscovery_Test;
161 friend class BluetoothChromeOSTest_Shutdown_OnStartDiscoveryError_Test;
162 friend class BluetoothChromeOSTest_Shutdown_OnStopDiscovery_Test;
163 friend class BluetoothChromeOSTest_Shutdown_OnStopDiscoveryError_Test;
165 // typedef for callback parameters that are passed to AddDiscoverySession
166 // and RemoveDiscoverySession. This is used to queue incoming requests while
167 // a call to BlueZ is pending.
168 typedef std::pair<base::Closure, ErrorCallback> DiscoveryCallbackPair;
169 typedef std::queue<DiscoveryCallbackPair> DiscoveryCallbackQueue;
171 BluetoothAdapterChromeOS();
172 ~BluetoothAdapterChromeOS() override;
174 // BluetoothAdapterClient::Observer override.
175 void AdapterAdded(const dbus::ObjectPath& object_path) override;
176 void AdapterRemoved(const dbus::ObjectPath& object_path) override;
177 void AdapterPropertyChanged(const dbus::ObjectPath& object_path,
178 const std::string& property_name) override;
180 // BluetoothDeviceClient::Observer override.
181 void DeviceAdded(const dbus::ObjectPath& object_path) override;
182 void DeviceRemoved(const dbus::ObjectPath& object_path) override;
183 void DevicePropertyChanged(const dbus::ObjectPath& object_path,
184 const std::string& property_name) override;
186 // BluetoothInputClient::Observer override.
187 void InputPropertyChanged(const dbus::ObjectPath& object_path,
188 const std::string& property_name) override;
190 // BluetoothAgentServiceProvider::Delegate override.
191 void Released() override;
192 void RequestPinCode(const dbus::ObjectPath& device_path,
193 const PinCodeCallback& callback) override;
194 void DisplayPinCode(const dbus::ObjectPath& device_path,
195 const std::string& pincode) override;
196 void RequestPasskey(const dbus::ObjectPath& device_path,
197 const PasskeyCallback& callback) override;
198 void DisplayPasskey(const dbus::ObjectPath& device_path,
199 uint32 passkey,
200 uint16 entered) override;
201 void RequestConfirmation(const dbus::ObjectPath& device_path,
202 uint32 passkey,
203 const ConfirmationCallback& callback) override;
204 void RequestAuthorization(const dbus::ObjectPath& device_path,
205 const ConfirmationCallback& callback) override;
206 void AuthorizeService(const dbus::ObjectPath& device_path,
207 const std::string& uuid,
208 const ConfirmationCallback& callback) override;
209 void Cancel() override;
211 // Called by dbus:: on completion of the D-Bus method call to register the
212 // pairing agent.
213 void OnRegisterAgent();
214 void OnRegisterAgentError(const std::string& error_name,
215 const std::string& error_message);
217 // Called by dbus:: on completion of the D-Bus method call to request that
218 // the pairing agent be made the default.
219 void OnRequestDefaultAgent();
220 void OnRequestDefaultAgentError(const std::string& error_name,
221 const std::string& error_message);
223 // Called by BluetoothAudioSinkChromeOS on completion of registering an audio
224 // sink.
225 void OnRegisterAudioSink(
226 const device::BluetoothAdapter::AcquiredCallback& callback,
227 const device::BluetoothAudioSink::ErrorCallback& error_callback,
228 scoped_refptr<device::BluetoothAudioSink> audio_sink);
230 // Internal method to obtain a BluetoothPairingChromeOS object for the device
231 // with path |object_path|. Returns the existing pairing object if the device
232 // already has one (usually an outgoing connection in progress) or a new
233 // pairing object with the default pairing delegate if not. If no default
234 // pairing object exists, NULL will be returned.
235 BluetoothPairingChromeOS* GetPairing(const dbus::ObjectPath& object_path);
237 // Set the tracked adapter to the one in |object_path|, this object will
238 // subsequently operate on that adapter until it is removed.
239 void SetAdapter(const dbus::ObjectPath& object_path);
241 // Set the adapter name to one chosen from the system information.
242 void SetDefaultAdapterName();
244 // Remove the currently tracked adapter. IsPresent() will return false after
245 // this is called.
246 void RemoveAdapter();
248 // Announce to observers a change in the adapter state.
249 void PoweredChanged(bool powered);
250 void DiscoverableChanged(bool discoverable);
251 void DiscoveringChanged(bool discovering);
252 void PresentChanged(bool present);
254 // Called by dbus:: on completion of the discoverable property change.
255 void OnSetDiscoverable(const base::Closure& callback,
256 const ErrorCallback& error_callback,
257 bool success);
259 // Called by dbus:: on completion of an adapter property change.
260 void OnPropertyChangeCompleted(const base::Closure& callback,
261 const ErrorCallback& error_callback,
262 bool success);
264 // BluetoothAdapter:
265 void AddDiscoverySession(const base::Closure& callback,
266 const ErrorCallback& error_callback) override;
267 void RemoveDiscoverySession(const base::Closure& callback,
268 const ErrorCallback& error_callback) override;
270 // Called by dbus:: on completion of the D-Bus method call to start discovery.
271 void OnStartDiscovery(const base::Closure& callback,
272 const ErrorCallback& error_callback);
273 void OnStartDiscoveryError(const base::Closure& callback,
274 const ErrorCallback& error_callback,
275 const std::string& error_name,
276 const std::string& error_message);
278 // Called by dbus:: on completion of the D-Bus method call to stop discovery.
279 void OnStopDiscovery(const base::Closure& callback);
280 void OnStopDiscoveryError(const ErrorCallback& error_callback,
281 const std::string& error_name,
282 const std::string& error_message);
284 // Called by dbus:: on completion of the D-Bus method to register a profile.
285 void OnRegisterProfile(const device::BluetoothUUID& uuid,
286 const dbus::ObjectPath& device_path,
287 BluetoothProfileServiceProvider::Delegate* delegate,
288 const ProfileRegisteredCallback& success_callback,
289 const ErrorCompletionCallback& error_callback);
290 bool SetProfileDelegate(const device::BluetoothUUID& uuid,
291 const dbus::ObjectPath& device_path,
292 BluetoothProfileServiceProvider::Delegate* delegate,
293 const ProfileRegisteredCallback& success_callback,
294 const ErrorCompletionCallback& error_callback);
295 void OnRegisterProfileError(const device::BluetoothUUID& uuid,
296 const ErrorCompletionCallback& error_callback,
297 const std::string& error_name,
298 const std::string& error_message);
300 // Processes the queued discovery requests. For each DiscoveryCallbackPair in
301 // the queue, this method will try to add a new discovery session. This method
302 // is called whenever a pending D-Bus call to start or stop discovery has
303 // ended (with either success or failure).
304 void ProcessQueuedDiscoveryRequests();
306 // Set in |Shutdown()|, makes IsPresent()| return false.
307 bool dbus_is_shutdown_;
309 // Number of discovery sessions that have been added.
310 int num_discovery_sessions_;
312 // True, if there is a pending request to start or stop discovery.
313 bool discovery_request_pending_;
315 // List of queued requests to add new discovery sessions. While there is a
316 // pending request to BlueZ to start or stop discovery, many requests from
317 // within Chrome to start or stop discovery sessions may occur. We only
318 // queue requests to add new sessions to be processed later. All requests to
319 // remove a session while a call is pending immediately return failure. Note
320 // that since BlueZ keeps its own reference count of applications that have
321 // requested discovery, dropping our count to 0 won't necessarily result in
322 // the controller actually stopping discovery if, for example, an application
323 // other than Chrome, such as bt_console, was also used to start discovery.
324 DiscoveryCallbackQueue discovery_request_queue_;
326 // Object path of the adapter we track.
327 dbus::ObjectPath object_path_;
329 // List of observers interested in event notifications from us.
330 ObserverList<device::BluetoothAdapter::Observer> observers_;
332 // Instance of the D-Bus agent object used for pairing, initialized with
333 // our own class as its delegate.
334 scoped_ptr<BluetoothAgentServiceProvider> agent_;
336 // UI thread task runner and socket thread object used to create sockets.
337 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
338 scoped_refptr<device::BluetoothSocketThread> socket_thread_;
340 // The profiles we have registered with the bluetooth daemon.
341 std::map<device::BluetoothUUID, BluetoothAdapterProfileChromeOS*> profiles_;
343 // Note: This should remain the last member so it'll be destroyed and
344 // invalidate its weak pointers before any other members are destroyed.
345 base::WeakPtrFactory<BluetoothAdapterChromeOS> weak_ptr_factory_;
347 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterChromeOS);
350 } // namespace chromeos
352 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_