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_
11 #include "base/memory/weak_ptr.h"
12 #include "base/sequenced_task_runner.h"
13 #include "chromeos/dbus/bluetooth_adapter_client.h"
14 #include "chromeos/dbus/bluetooth_agent_service_provider.h"
15 #include "chromeos/dbus/bluetooth_device_client.h"
16 #include "chromeos/dbus/bluetooth_input_client.h"
17 #include "dbus/object_path.h"
18 #include "device/bluetooth/bluetooth_adapter.h"
19 #include "device/bluetooth/bluetooth_device.h"
22 class BluetoothSocketThread
;
27 class BluetoothChromeOSTest
;
28 class BluetoothDeviceChromeOS
;
29 class BluetoothPairingChromeOS
;
31 // The BluetoothAdapterChromeOS class implements BluetoothAdapter for the
32 // Chrome OS platform.
33 class BluetoothAdapterChromeOS
34 : public device::BluetoothAdapter
,
35 public chromeos::BluetoothAdapterClient::Observer
,
36 public chromeos::BluetoothDeviceClient::Observer
,
37 public chromeos::BluetoothInputClient::Observer
,
38 public chromeos::BluetoothAgentServiceProvider::Delegate
{
40 static base::WeakPtr
<BluetoothAdapter
> CreateAdapter();
43 virtual void AddObserver(
44 device::BluetoothAdapter::Observer
* observer
) OVERRIDE
;
45 virtual void RemoveObserver(
46 device::BluetoothAdapter::Observer
* observer
) OVERRIDE
;
47 virtual std::string
GetAddress() const OVERRIDE
;
48 virtual std::string
GetName() const OVERRIDE
;
49 virtual void SetName(const std::string
& name
,
50 const base::Closure
& callback
,
51 const ErrorCallback
& error_callback
) OVERRIDE
;
52 virtual bool IsInitialized() const OVERRIDE
;
53 virtual bool IsPresent() const OVERRIDE
;
54 virtual bool IsPowered() const OVERRIDE
;
55 virtual void SetPowered(
57 const base::Closure
& callback
,
58 const ErrorCallback
& error_callback
) OVERRIDE
;
59 virtual bool IsDiscoverable() const OVERRIDE
;
60 virtual void SetDiscoverable(
62 const base::Closure
& callback
,
63 const ErrorCallback
& error_callback
) OVERRIDE
;
64 virtual bool IsDiscovering() const OVERRIDE
;
65 virtual void CreateRfcommService(
66 const device::BluetoothUUID
& uuid
,
69 const CreateServiceCallback
& callback
,
70 const CreateServiceErrorCallback
& error_callback
) OVERRIDE
;
71 virtual void CreateL2capService(
72 const device::BluetoothUUID
& uuid
,
74 const CreateServiceCallback
& callback
,
75 const CreateServiceErrorCallback
& error_callback
) OVERRIDE
;
77 // Locates the device object by object path (the devices map and
78 // BluetoothDevice methods are by address).
79 BluetoothDeviceChromeOS
* GetDeviceWithPath(
80 const dbus::ObjectPath
& object_path
);
82 // Announce to observers a change in device state that is not reflected by
83 // its D-Bus properties.
84 void NotifyDeviceChanged(BluetoothDeviceChromeOS
* device
);
86 // Returns the object path of the adapter.
87 const dbus::ObjectPath
& object_path() const { return object_path_
; }
91 virtual void RemovePairingDelegateInternal(
92 device::BluetoothDevice::PairingDelegate
* pairing_delegate
) OVERRIDE
;
95 friend class BluetoothChromeOSTest
;
97 // typedef for callback parameters that are passed to AddDiscoverySession
98 // and RemoveDiscoverySession. This is used to queue incoming requests while
99 // a call to BlueZ is pending.
100 typedef std::pair
<base::Closure
, ErrorCallback
> DiscoveryCallbackPair
;
101 typedef std::queue
<DiscoveryCallbackPair
> DiscoveryCallbackQueue
;
103 BluetoothAdapterChromeOS();
104 virtual ~BluetoothAdapterChromeOS();
106 // BluetoothAdapterClient::Observer override.
107 virtual void AdapterAdded(const dbus::ObjectPath
& object_path
) OVERRIDE
;
108 virtual void AdapterRemoved(const dbus::ObjectPath
& object_path
) OVERRIDE
;
109 virtual void AdapterPropertyChanged(
110 const dbus::ObjectPath
& object_path
,
111 const std::string
& property_name
) OVERRIDE
;
113 // BluetoothDeviceClient::Observer override.
114 virtual void DeviceAdded(const dbus::ObjectPath
& object_path
) OVERRIDE
;
115 virtual void DeviceRemoved(const dbus::ObjectPath
& object_path
) OVERRIDE
;
116 virtual void DevicePropertyChanged(const dbus::ObjectPath
& object_path
,
117 const std::string
& property_name
) OVERRIDE
;
119 // BluetoothInputClient::Observer override.
120 virtual void InputPropertyChanged(const dbus::ObjectPath
& object_path
,
121 const std::string
& property_name
) OVERRIDE
;
123 // BluetoothAgentServiceProvider::Delegate override.
124 virtual void Released() OVERRIDE
;
125 virtual void RequestPinCode(const dbus::ObjectPath
& device_path
,
126 const PinCodeCallback
& callback
) OVERRIDE
;
127 virtual void DisplayPinCode(const dbus::ObjectPath
& device_path
,
128 const std::string
& pincode
) OVERRIDE
;
129 virtual void RequestPasskey(const dbus::ObjectPath
& device_path
,
130 const PasskeyCallback
& callback
) OVERRIDE
;
131 virtual void DisplayPasskey(const dbus::ObjectPath
& device_path
,
132 uint32 passkey
, uint16 entered
) OVERRIDE
;
133 virtual void RequestConfirmation(const dbus::ObjectPath
& device_path
,
135 const ConfirmationCallback
& callback
)
137 virtual void RequestAuthorization(const dbus::ObjectPath
& device_path
,
138 const ConfirmationCallback
& callback
)
140 virtual void AuthorizeService(const dbus::ObjectPath
& device_path
,
141 const std::string
& uuid
,
142 const ConfirmationCallback
& callback
) OVERRIDE
;
143 virtual void Cancel() OVERRIDE
;
145 // Called by dbus:: on completion of the D-Bus method call to register the
147 void OnRegisterAgent();
148 void OnRegisterAgentError(const std::string
& error_name
,
149 const std::string
& error_message
);
151 // Called by dbus:: on completion of the D-Bus method call to request that
152 // the pairing agent be made the default.
153 void OnRequestDefaultAgent();
154 void OnRequestDefaultAgentError(const std::string
& error_name
,
155 const std::string
& error_message
);
157 // Internal method to obtain a BluetoothPairingChromeOS object for the device
158 // with path |object_path|. Returns the existing pairing object if the device
159 // already has one (usually an outgoing connection in progress) or a new
160 // pairing object with the default pairing delegate if not. If no default
161 // pairing object exists, NULL will be returned.
162 BluetoothPairingChromeOS
* GetPairing(const dbus::ObjectPath
& object_path
);
164 // Set the tracked adapter to the one in |object_path|, this object will
165 // subsequently operate on that adapter until it is removed.
166 void SetAdapter(const dbus::ObjectPath
& object_path
);
168 // Set the adapter name to one chosen from the system information.
169 void SetDefaultAdapterName();
171 // Remove the currently tracked adapter. IsPresent() will return false after
173 void RemoveAdapter();
175 // Announce to observers a change in the adapter state.
176 void PoweredChanged(bool powered
);
177 void DiscoverableChanged(bool discoverable
);
178 void DiscoveringChanged(bool discovering
);
179 void PresentChanged(bool present
);
181 // Called by dbus:: on completion of the discoverable property change.
182 void OnSetDiscoverable(const base::Closure
& callback
,
183 const ErrorCallback
& error_callback
,
186 // Called by dbus:: on completion of an adapter property change.
187 void OnPropertyChangeCompleted(const base::Closure
& callback
,
188 const ErrorCallback
& error_callback
,
192 virtual void AddDiscoverySession(
193 const base::Closure
& callback
,
194 const ErrorCallback
& error_callback
) OVERRIDE
;
195 virtual void RemoveDiscoverySession(
196 const base::Closure
& callback
,
197 const ErrorCallback
& error_callback
) OVERRIDE
;
199 // Called by dbus:: on completion of the D-Bus method call to start discovery.
200 void OnStartDiscovery(const base::Closure
& callback
);
201 void OnStartDiscoveryError(const base::Closure
& callback
,
202 const ErrorCallback
& error_callback
,
203 const std::string
& error_name
,
204 const std::string
& error_message
);
206 // Called by dbus:: on completion of the D-Bus method call to stop discovery.
207 void OnStopDiscovery(const base::Closure
& callback
);
208 void OnStopDiscoveryError(const ErrorCallback
& error_callback
,
209 const std::string
& error_name
,
210 const std::string
& error_message
);
212 // Processes the queued discovery requests. For each DiscoveryCallbackPair in
213 // the queue, this method will try to add a new discovery session. This method
214 // is called whenever a pending D-Bus call to start or stop discovery has
215 // ended (with either success or failure).
216 void ProcessQueuedDiscoveryRequests();
218 // Number of discovery sessions that have been added.
219 int num_discovery_sessions_
;
221 // True, if there is a pending request to start or stop discovery.
222 bool discovery_request_pending_
;
224 // List of queued requests to add new discovery sessions. While there is a
225 // pending request to BlueZ to start or stop discovery, many requests from
226 // within Chrome to start or stop discovery sessions may occur. We only
227 // queue requests to add new sessions to be processed later. All requests to
228 // remove a session while a call is pending immediately return failure. Note
229 // that since BlueZ keeps its own reference count of applications that have
230 // requested discovery, dropping our count to 0 won't necessarily result in
231 // the controller actually stopping discovery if, for example, an application
232 // other than Chrome, such as bt_console, was also used to start discovery.
233 DiscoveryCallbackQueue discovery_request_queue_
;
235 // Object path of the adapter we track.
236 dbus::ObjectPath object_path_
;
238 // List of observers interested in event notifications from us.
239 ObserverList
<device::BluetoothAdapter::Observer
> observers_
;
241 // Instance of the D-Bus agent object used for pairing, initialized with
242 // our own class as its delegate.
243 scoped_ptr
<BluetoothAgentServiceProvider
> agent_
;
245 // UI thread task runner and socket thread object used to create sockets.
246 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
247 scoped_refptr
<device::BluetoothSocketThread
> socket_thread_
;
249 // Note: This should remain the last member so it'll be destroyed and
250 // invalidate its weak pointers before any other members are destroyed.
251 base::WeakPtrFactory
<BluetoothAdapterChromeOS
> weak_ptr_factory_
;
253 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterChromeOS
);
256 } // namespace chromeos
258 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_