1 // Copyright (c) 2012 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/basictypes.h"
12 #include "base/callback.h"
13 #include "base/observer_list.h"
14 #include "chromeos/dbus/bluetooth_adapter_client.h"
15 #include "chromeos/dbus/bluetooth_device_client.h"
16 #include "chromeos/dbus/bluetooth_manager_client.h"
17 #include "dbus/object_path.h"
18 #include "device/bluetooth/bluetooth_adapter.h"
22 class BluetoothAdapterFactory
;
23 class MockBluetoothAdapter
;
24 struct BluetoothOutOfBandPairingData
;
30 class BluetoothDeviceChromeOS
;
32 // The BluetoothAdapterChromeOS class is an implementation of BluetoothAdapter
33 // for Chrome OS platform.
34 class BluetoothAdapterChromeOS
35 : public device::BluetoothAdapter
,
36 public BluetoothManagerClient::Observer
,
37 public BluetoothAdapterClient::Observer
,
38 public BluetoothDeviceClient::Observer
{
40 // BluetoothAdapter override
41 virtual void AddObserver(
42 device::BluetoothAdapter::Observer
* observer
) OVERRIDE
;
43 virtual void RemoveObserver(
44 device::BluetoothAdapter::Observer
* observer
) OVERRIDE
;
45 virtual std::string
GetAddress() const OVERRIDE
;
46 virtual std::string
GetName() const OVERRIDE
;
47 virtual bool IsInitialized() const OVERRIDE
;
48 virtual bool IsPresent() const OVERRIDE
;
49 virtual bool IsPowered() const OVERRIDE
;
50 virtual void SetPowered(
52 const base::Closure
& callback
,
53 const ErrorCallback
& error_callback
) OVERRIDE
;
54 virtual bool IsDiscovering() const OVERRIDE
;
55 virtual void StartDiscovering(
56 const base::Closure
& callback
,
57 const ErrorCallback
& error_callback
) OVERRIDE
;
58 virtual void StopDiscovering(
59 const base::Closure
& callback
,
60 const ErrorCallback
& error_callback
) OVERRIDE
;
61 virtual void ReadLocalOutOfBandPairingData(
62 const device::BluetoothAdapter::BluetoothOutOfBandPairingDataCallback
&
64 const ErrorCallback
& error_callback
) OVERRIDE
;
67 friend class BluetoothDeviceChromeOS
;
68 friend class device::BluetoothAdapterFactory
;
69 friend class device::MockBluetoothAdapter
;
71 BluetoothAdapterChromeOS();
72 virtual ~BluetoothAdapterChromeOS();
74 // Called by dbus:: in response to the method call sent by DefaultAdapter().
75 // |object_path| will contain the dbus object path of the requested adapter
76 // when |success| is true.
77 void AdapterCallback(const dbus::ObjectPath
& adapter_path
, bool success
);
79 // BluetoothManagerClient::Observer override.
81 // Called when the default local bluetooth adapter changes.
82 // |object_path| is the dbus object path of the new default adapter.
83 // Not called if all adapters are removed.
84 virtual void DefaultAdapterChanged(const dbus::ObjectPath
& adapter_path
)
87 // BluetoothManagerClient::Observer override.
89 // Called when a local bluetooth adapter is removed.
90 // |object_path| is the dbus object path of the adapter.
91 virtual void AdapterRemoved(const dbus::ObjectPath
& adapter_path
) OVERRIDE
;
93 // Changes the tracked adapter to the dbus object path |adapter_path|,
94 // clearing information from the previously tracked adapter and updating
95 // to the new adapter.
96 void ChangeAdapter(const dbus::ObjectPath
& adapter_path
);
98 // Clears the tracked adapter information.
101 // Called by dbus:: in response to the method call send by SetPowered().
102 // |callback| and |error_callback| are the callbacks passed to SetPowered().
103 void OnSetPowered(const base::Closure
& callback
,
104 const ErrorCallback
& error_callback
,
107 // Updates the tracked state of the adapter's radio power to |powered|
108 // and notifies observers. Called on receipt of a property changed signal,
109 // and directly using values obtained from properties.
110 void PoweredChanged(bool powered
);
112 // Called by BluetoothAdapterClient in response to the method call sent
113 // by StartDiscovering(), |callback| and |error_callback| are the callbacks
114 // provided to that method.
115 void OnStartDiscovery(const base::Closure
& callback
,
116 const ErrorCallback
& error_callback
,
117 const dbus::ObjectPath
& adapter_path
,
120 // Called by BluetoothAdapterClient in response to the method call sent
121 // by StopDiscovering(), |callback| and |error_callback| are the callbacks
122 // provided to that method.
123 void OnStopDiscovery(const base::Closure
& callback
,
124 const ErrorCallback
& error_callback
,
125 const dbus::ObjectPath
& adapter_path
,
128 // Updates the tracked state of the adapter's discovering state to
129 // |discovering| and notifies observers. Called on receipt of a property
130 // changed signal, and directly using values obtained from properties.
131 void DiscoveringChanged(bool discovering
);
133 // Called by dbus:: in response to the ReadLocalData method call.
134 void OnReadLocalData(
135 const device::BluetoothAdapter::BluetoothOutOfBandPairingDataCallback
&
137 const ErrorCallback
& error_callback
,
138 const device::BluetoothOutOfBandPairingData
& data
,
141 // BluetoothAdapterClient::Observer override.
143 // Called when the adapter with object path |adapter_path| has a
144 // change in value of the property named |property_name|.
145 virtual void AdapterPropertyChanged(const dbus::ObjectPath
& adapter_path
,
146 const std::string
& property_name
)
149 // BluetoothDeviceClient::Observer override.
151 // Called when the device with object path |device_path| has a
152 // change in value of the property named |property_name|.
153 virtual void DevicePropertyChanged(const dbus::ObjectPath
& device_path
,
154 const std::string
& property_name
)
157 // Updates information on the device with object path |device_path|,
158 // adding it to the |devices_| map if not already present.
159 void UpdateDevice(const dbus::ObjectPath
& device_path
);
161 // Clears the |devices_| list, notifying obsevers of the device removal.
164 // BluetoothAdapterClient::Observer override.
166 // Called when the adapter with object path |object_path| has a
167 // new known device with object path |object_path|.
168 virtual void DeviceCreated(const dbus::ObjectPath
& adapter_path
,
169 const dbus::ObjectPath
& device_path
) OVERRIDE
;
171 // BluetoothAdapterClient::Observer override.
173 // Called when the adapter with object path |object_path| removes
174 // the known device with object path |object_path|.
175 virtual void DeviceRemoved(const dbus::ObjectPath
& adapter_path
,
176 const dbus::ObjectPath
& device_path
) OVERRIDE
;
178 // Updates the adapter |devices_| list, adding or updating devices using
179 // the object paths in the|devices| list. This doesn't remove devices,
180 // relying instead on the DeviceRemoved() signal for that. Called on
181 // receipt of a property changed signal, and directly using values obtained
183 void DevicesChanged(const std::vector
<dbus::ObjectPath
>& devices
);
185 // Clears discovered devices from the |devices_| list, notifying
186 // observers, and leaving only those devices with a dbus object path.
187 void ClearDiscoveredDevices();
189 // BluetoothAdapterClient::Observer override.
191 // Called when the adapter with object path |object_path| discovers
192 // a new remote device with address |address| and properties
193 // |properties|, there is no device object path until connected.
195 // |properties| supports only value() calls, not Get() or Set(), and
196 // should be copied if needed.
197 virtual void DeviceFound(
198 const dbus::ObjectPath
& adapter_path
,
199 const std::string
& address
,
200 const BluetoothDeviceClient::Properties
& properties
) OVERRIDE
;
202 // BluetoothAdapterClient::Observer override.
204 // Called when the adapter with object path |object_path| can no
205 // longer communicate with the discovered removed device with
206 // address |address|.
207 virtual void DeviceDisappeared(const dbus::ObjectPath
& object_path
,
208 const std::string
& address
) OVERRIDE
;
210 // List of observers interested in event notifications from us.
211 ObserverList
<device::BluetoothAdapter::Observer
> observers_
;
213 // Object path of adapter for this instance, we update it to always
214 // point at the default adapter.
215 dbus::ObjectPath object_path_
;
217 // Tracked adapter state, cached locally so we only send change notifications
218 // to observers on a genuine change.
219 std::string address_
;
224 // Count of callers to StartDiscovering() and StopDiscovering(), used to
225 // track whether to clear the discovered devices list on start.
226 int discovering_count_
;
228 // Note: This should remain the last member so it'll be destroyed and
229 // invalidate its weak pointers before any other members are destroyed.
230 base::WeakPtrFactory
<BluetoothAdapterChromeOS
> weak_ptr_factory_
;
232 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterChromeOS
);
235 } // namespace chromeos
237 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_CHROMEOS_H_