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
,
68 const CreateServiceCallback
& callback
,
69 const CreateServiceErrorCallback
& error_callback
) OVERRIDE
;
70 virtual void CreateL2capService(
71 const device::BluetoothUUID
& uuid
,
73 const CreateServiceCallback
& callback
,
74 const CreateServiceErrorCallback
& error_callback
) OVERRIDE
;
76 // Locates the device object by object path (the devices map and
77 // BluetoothDevice methods are by address).
78 BluetoothDeviceChromeOS
* GetDeviceWithPath(
79 const dbus::ObjectPath
& object_path
);
81 // Announce to observers a change in device state that is not reflected by
82 // its D-Bus properties.
83 void NotifyDeviceChanged(BluetoothDeviceChromeOS
* device
);
85 // Returns the object path of the adapter.
86 const dbus::ObjectPath
& object_path() const { return object_path_
; }
90 virtual void RemovePairingDelegateInternal(
91 device::BluetoothDevice::PairingDelegate
* pairing_delegate
) OVERRIDE
;
94 friend class BluetoothChromeOSTest
;
96 // typedef for callback parameters that are passed to AddDiscoverySession
97 // and RemoveDiscoverySession. This is used to queue incoming requests while
98 // a call to BlueZ is pending.
99 typedef std::pair
<base::Closure
, ErrorCallback
> DiscoveryCallbackPair
;
100 typedef std::queue
<DiscoveryCallbackPair
> DiscoveryCallbackQueue
;
102 BluetoothAdapterChromeOS();
103 virtual ~BluetoothAdapterChromeOS();
105 // BluetoothAdapterClient::Observer override.
106 virtual void AdapterAdded(const dbus::ObjectPath
& object_path
) OVERRIDE
;
107 virtual void AdapterRemoved(const dbus::ObjectPath
& object_path
) OVERRIDE
;
108 virtual void AdapterPropertyChanged(
109 const dbus::ObjectPath
& object_path
,
110 const std::string
& property_name
) OVERRIDE
;
112 // BluetoothDeviceClient::Observer override.
113 virtual void DeviceAdded(const dbus::ObjectPath
& object_path
) OVERRIDE
;
114 virtual void DeviceRemoved(const dbus::ObjectPath
& object_path
) OVERRIDE
;
115 virtual void DevicePropertyChanged(const dbus::ObjectPath
& object_path
,
116 const std::string
& property_name
) OVERRIDE
;
118 // BluetoothInputClient::Observer override.
119 virtual void InputPropertyChanged(const dbus::ObjectPath
& object_path
,
120 const std::string
& property_name
) OVERRIDE
;
122 // BluetoothAgentServiceProvider::Delegate override.
123 virtual void Released() OVERRIDE
;
124 virtual void RequestPinCode(const dbus::ObjectPath
& device_path
,
125 const PinCodeCallback
& callback
) OVERRIDE
;
126 virtual void DisplayPinCode(const dbus::ObjectPath
& device_path
,
127 const std::string
& pincode
) OVERRIDE
;
128 virtual void RequestPasskey(const dbus::ObjectPath
& device_path
,
129 const PasskeyCallback
& callback
) OVERRIDE
;
130 virtual void DisplayPasskey(const dbus::ObjectPath
& device_path
,
131 uint32 passkey
, uint16 entered
) OVERRIDE
;
132 virtual void RequestConfirmation(const dbus::ObjectPath
& device_path
,
134 const ConfirmationCallback
& callback
)
136 virtual void RequestAuthorization(const dbus::ObjectPath
& device_path
,
137 const ConfirmationCallback
& callback
)
139 virtual void AuthorizeService(const dbus::ObjectPath
& device_path
,
140 const std::string
& uuid
,
141 const ConfirmationCallback
& callback
) OVERRIDE
;
142 virtual void Cancel() OVERRIDE
;
144 // Called by dbus:: on completion of the D-Bus method call to register the
146 void OnRegisterAgent();
147 void OnRegisterAgentError(const std::string
& error_name
,
148 const std::string
& error_message
);
150 // Called by dbus:: on completion of the D-Bus method call to request that
151 // the pairing agent be made the default.
152 void OnRequestDefaultAgent();
153 void OnRequestDefaultAgentError(const std::string
& error_name
,
154 const std::string
& error_message
);
156 // Internal method to obtain a BluetoothPairingChromeOS object for the device
157 // with path |object_path|. Returns the existing pairing object if the device
158 // already has one (usually an outgoing connection in progress) or a new
159 // pairing object with the default pairing delegate if not. If no default
160 // pairing object exists, NULL will be returned.
161 BluetoothPairingChromeOS
* GetPairing(const dbus::ObjectPath
& object_path
);
163 // Set the tracked adapter to the one in |object_path|, this object will
164 // subsequently operate on that adapter until it is removed.
165 void SetAdapter(const dbus::ObjectPath
& object_path
);
167 // Set the adapter name to one chosen from the system information.
168 void SetDefaultAdapterName();
170 // Remove the currently tracked adapter. IsPresent() will return false after
172 void RemoveAdapter();
174 // Announce to observers a change in the adapter state.
175 void PoweredChanged(bool powered
);
176 void DiscoverableChanged(bool discoverable
);
177 void DiscoveringChanged(bool discovering
);
178 void PresentChanged(bool present
);
180 // Called by dbus:: on completion of the discoverable property change.
181 void OnSetDiscoverable(const base::Closure
& callback
,
182 const ErrorCallback
& error_callback
,
185 // Called by dbus:: on completion of an adapter property change.
186 void OnPropertyChangeCompleted(const base::Closure
& callback
,
187 const ErrorCallback
& error_callback
,
191 virtual void AddDiscoverySession(
192 const base::Closure
& callback
,
193 const ErrorCallback
& error_callback
) OVERRIDE
;
194 virtual void RemoveDiscoverySession(
195 const base::Closure
& callback
,
196 const ErrorCallback
& error_callback
) OVERRIDE
;
198 // Called by dbus:: on completion of the D-Bus method call to start discovery.
199 void OnStartDiscovery(const base::Closure
& callback
);
200 void OnStartDiscoveryError(const base::Closure
& callback
,
201 const ErrorCallback
& error_callback
,
202 const std::string
& error_name
,
203 const std::string
& error_message
);
205 // Called by dbus:: on completion of the D-Bus method call to stop discovery.
206 void OnStopDiscovery(const base::Closure
& callback
);
207 void OnStopDiscoveryError(const ErrorCallback
& error_callback
,
208 const std::string
& error_name
,
209 const std::string
& error_message
);
211 // Processes the queued discovery requests. For each DiscoveryCallbackPair in
212 // the queue, this method will try to add a new discovery session. This method
213 // is called whenever a pending D-Bus call to start or stop discovery has
214 // ended (with either success or failure).
215 void ProcessQueuedDiscoveryRequests();
217 // Number of discovery sessions that have been added.
218 int num_discovery_sessions_
;
220 // True, if there is a pending request to start or stop discovery.
221 bool discovery_request_pending_
;
223 // List of queued requests to add new discovery sessions. While there is a
224 // pending request to BlueZ to start or stop discovery, many requests from
225 // within Chrome to start or stop discovery sessions may occur. We only
226 // queue requests to add new sessions to be processed later. All requests to
227 // remove a session while a call is pending immediately return failure. Note
228 // that since BlueZ keeps its own reference count of applications that have
229 // requested discovery, dropping our count to 0 won't necessarily result in
230 // the controller actually stopping discovery if, for example, an application
231 // other than Chrome, such as bt_console, was also used to start discovery.
232 DiscoveryCallbackQueue discovery_request_queue_
;
234 // Object path of the adapter we track.
235 dbus::ObjectPath object_path_
;
237 // List of observers interested in event notifications from us.
238 ObserverList
<device::BluetoothAdapter::Observer
> observers_
;
240 // Instance of the D-Bus agent object used for pairing, initialized with
241 // our own class as its delegate.
242 scoped_ptr
<BluetoothAgentServiceProvider
> agent_
;
244 // UI thread task runner and socket thread object used to create sockets.
245 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
246 scoped_refptr
<device::BluetoothSocketThread
> socket_thread_
;
248 // Note: This should remain the last member so it'll be destroyed and
249 // invalidate its weak pointers before any other members are destroyed.
250 base::WeakPtrFactory
<BluetoothAdapterChromeOS
> weak_ptr_factory_
;
252 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterChromeOS
);
255 } // namespace chromeos
257 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_