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