Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(475)

Side by Side Diff: chrome/browser/chromeos/bluetooth/bluetooth_adapter.h

Issue 10899037: Refactoring bluetooth API code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing styles and tess. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_ADAPTER_H_ 5 #ifndef CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_ADAPTER_H_
6 #define CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_ADAPTER_H_ 6 #define CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_ADAPTER_H_
7 7
8 #include <map>
9 #include <string> 8 #include <string>
10 #include <vector> 9 #include <vector>
11 10
12 #include "base/basictypes.h"
13 #include "base/callback.h" 11 #include "base/callback.h"
14 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
15 #include "base/observer_list.h"
16 #include "chromeos/dbus/bluetooth_adapter_client.h"
17 #include "chromeos/dbus/bluetooth_device_client.h"
18 #include "chromeos/dbus/bluetooth_manager_client.h"
19 #include "chromeos/dbus/bluetooth_out_of_band_client.h"
20 #include "dbus/object_path.h"
21 13
22 namespace chromeos { 14 namespace chromeos {
23 15
24 class BluetoothDevice; 16 class BluetoothDevice;
25 17
26 // The BluetoothAdapter class represents a local Bluetooth adapter which 18 struct BluetoothOutOfBandPairingData;
27 // may be used to interact with remote Bluetooth devices. As well as 19
28 // providing support for determining whether an adapter is present, and 20 // The BluetoothAdapter represents a local Bluetooth adapter which may
29 // whether the radio is powered, this class also provides support for 21 // be used to interact with remote Bluetooth devices. As well as providing
30 // obtaining the list of remote devices known to the adapter, discovering 22 // support for determining whether an adapter is present, and whether the radio
31 // new devices, and providing notification of updates to device information. 23 // is powered, this class also provides support for obtaining the list of remote
32 // 24 // devices known to the adapter, discovering new devices, and providing
33 // The class may be instantiated for either a specific adapter, or for the 25 // notification of updates to device information.
34 // generic "default adapter" which may change depending on availability. 26 class BluetoothAdapter : public base::RefCounted<BluetoothAdapter> {
keybuk 2012/09/13 23:57:30 Why the removal of this comment? We need to deal
youngki 2012/09/17 21:53:02 Done. Initially I put the second paragraph into bl
bryeung 2012/09/13 19:52:57 If the interface is going to be refcounted, it wil
youngki 2012/09/17 21:53:02 I just made the destructor protected.
35 class BluetoothAdapter : public base::RefCounted<BluetoothAdapter>,
36 public BluetoothManagerClient::Observer,
37 public BluetoothAdapterClient::Observer,
38 public BluetoothDeviceClient::Observer {
39 public: 27 public:
40 // Interface for observing changes from bluetooth adapters. 28 // Interface for observing changes from bluetooth adapters.
41 class Observer { 29 class Observer {
42 public: 30 public:
43 virtual ~Observer() {} 31 virtual ~Observer() {}
44 32
45 // Called when the presence of the adapter |adapter| changes, when 33 // Called when the presence of the adapter |adapter| changes, when
46 // |present| is true the adapter is now present, false means the adapter 34 // |present| is true the adapter is now present, false means the adapter
47 // has been removed from the system. 35 // has been removed from the system.
48 virtual void AdapterPresentChanged(BluetoothAdapter* adapter, 36 virtual void AdapterPresentChanged(BluetoothAdapter* adapter,
49 bool present) {} 37 bool present) = 0;
bryeung 2012/09/13 19:52:57 why are these pure virtual now?
keybuk 2012/09/13 23:57:30 Observer methods should remain as empty implementa
youngki 2012/09/17 21:53:02 Put them back as empty implementations.
50 38
51 // Called when the radio power state of the adapter |adapter| changes, 39 // Called when the radio power state of the adapter |adapter| changes,
52 // when |powered| is true the adapter radio is powered, false means the 40 // when |powered| is true the adapter radio is powered, false means the
53 // adapter radio is off. 41 // adapter radio is off.
54 virtual void AdapterPoweredChanged(BluetoothAdapter* adapter, 42 virtual void AdapterPoweredChanged(BluetoothAdapter* adapter,
55 bool powered) {} 43 bool powered) = 0;
56 44
57 // Called when the discovering state of the adapter |adapter| changes, 45 // Called when the discovering state of the adapter |adapter| changes,
58 // when |discovering| is true the adapter is seeking new devices, false 46 // when |discovering| is true the adapter is seeking new devices, false
59 // means it is not. Note that device discovery involves both states when 47 // means it is not. Note that device discovery involves both states when
60 // the adapter is seeking new devices and states when it is not because 48 // the adapter is seeking new devices and states when it is not because
61 // it is interrogating the devices it found. 49 // it is interrogating the devices it found.
62 virtual void AdapterDiscoveringChanged(BluetoothAdapter* adapter, 50 virtual void AdapterDiscoveringChanged(BluetoothAdapter* adapter,
63 bool discovering) {} 51 bool discovering) = 0;
64 52
65 // Called when a new device |device| is added to the adapter |adapter|, 53 // Called when a new device |device| is added to the adapter |adapter|,
66 // either because it has been discovered or a connection made. |device| 54 // either because it has been discovered or a connection made. |device|
67 // should not be cached, instead copy its address. 55 // should not be cached, instead copy its address.
68 virtual void DeviceAdded(BluetoothAdapter* adapter, 56 virtual void DeviceAdded(BluetoothAdapter* adapter,
69 BluetoothDevice* device) {} 57 BluetoothDevice* device) = 0;
70 58
71 // Called when properties of the device |device| known to the adapter 59 // Called when properties of the device |device| known to the adapter
72 // |adapter| change. |device| should not be cached, instead copy its 60 // |adapter| change. |device| should not be cached, instead copy its
73 // address. 61 // address.
74 virtual void DeviceChanged(BluetoothAdapter* adapter, 62 virtual void DeviceChanged(BluetoothAdapter* adapter,
75 BluetoothDevice* device) {} 63 BluetoothDevice* device) = 0;
76 64
77 // Called when the device |device| is removed from the adapter |adapter|, 65 // Called when the device |device| is removed from the adapter |adapter|,
78 // either as a result of a discovered device being lost between discovering 66 // either as a result of a discovered device being lost between discovering
79 // phases or pairing information deleted. |device| should not be cached. 67 // phases or pairing information deleted. |device| should not be cached.
80 virtual void DeviceRemoved(BluetoothAdapter* adapter, 68 virtual void DeviceRemoved(BluetoothAdapter* adapter,
81 BluetoothDevice* device) {} 69 BluetoothDevice* device) = 0;
82 }; 70 };
83 71
84 // Adds and removes observers for events on this bluetooth adapter, 72 virtual ~BluetoothAdapter() {}
85 // if monitoring multiple adapters check the |adapter| parameter of
86 // observer methods to determine which adapter is issuing the event.
keybuk 2012/09/13 23:57:30 Why was this comment dropped?
youngki 2012/09/17 21:53:02 Done.
87 void AddObserver(Observer* observer);
88 void RemoveObserver(Observer* observer);
89 73
90 // The ErrorCallback is used for methods that can fail in which case it 74 // The ErrorCallback is used for methods that can fail in which case it
91 // is called, in the success case the callback is simply not called. 75 // is called, in the success case the callback is simply not called.
92 typedef base::Callback<void()> ErrorCallback; 76 typedef base::Callback<void()> ErrorCallback;
93 77
94 // The BluetoothOutOfBandPairingDataCallback is used to return 78 // The BluetoothOutOfBandPairingDataCallback is used to return
95 // BluetoothOutOfBandPairingData to the caller. 79 // BluetoothOutOfBandPairingData to the caller.
96 typedef base::Callback<void(const BluetoothOutOfBandPairingData& data)> 80 typedef base::Callback<void(const BluetoothOutOfBandPairingData& data)>
97 BluetoothOutOfBandPairingDataCallback; 81 BluetoothOutOfBandPairingDataCallback;
98 82
99 // The address of this adapter. The address format is "XX:XX:XX:XX:XX:XX", 83 // Adds and removes observers for events on this bluetooth adapter.
100 // where each XX is a hexadecimal number. 84 virtual void AddObserver(BluetoothAdapter::Observer* observer) = 0;
keybuk 2012/09/13 23:57:30 Why was this comment dropped? Please don't remove
youngki 2012/09/17 21:53:02 Done.
101 const std::string& address() const { return address_; } 85 virtual void RemoveObserver(
86 BluetoothAdapter::Observer* observer) = 0;
87
88 // The address of this adapter.
89 virtual const std::string& address() const = 0;
102 90
103 // The name of the adapter. 91 // The name of the adapter.
104 const std::string& name() const { return name_; } 92 virtual const std::string& name() const = 0;
105 93
106 // Indicates whether the adapter is actually present on the system, for 94 // Indicates whether the adapter is actually present on the system, for
107 // the default adapter this indicates whether any adapter is present. An 95 // the default adapter this indicates whether any adapter is present.
108 // adapter is only considered present if the address has been obtained. 96 virtual bool IsPresent() const = 0;
keybuk 2012/09/13 23:57:30 Why was this comment dropped?
youngki 2012/09/17 21:53:02 Done.
109 virtual bool IsPresent() const;
110 97
111 // Indicates whether the adapter radio is powered. 98 // Indicates whether the adapter radio is powered.
112 virtual bool IsPowered() const; 99 virtual bool IsPowered() const = 0;
113 100
114 // Requests a change to the adapter radio power, setting |powered| to true 101 // Requests a change to the adapter radio power. On success, callback will be
115 // will turn on the radio and false will turn it off. On success, callback 102 // called. On failure, |error_callback| will be called.
116 // will be called. On failure, |error_callback| will be called. 103 virtual void SetPowered(bool powered,
keybuk 2012/09/13 23:57:30 Why do you hate documentation?
youngki 2012/09/17 21:53:02 I thought those are implementation specific that's
117 void SetPowered(bool powered, 104 const base::Closure& callback,
118 const base::Closure& callback, 105 const ErrorCallback& error_callback) = 0;
119 const ErrorCallback& error_callback);
120 106
121 // Indicates whether the adapter is currently discovering new devices, 107 // Indicates whether the adapter is currently discovering new devices,
122 // note that a typical discovery process has phases of this being true 108 // note that a typical discovery process has phases of this being true
123 // followed by phases of being false when the adapter interrogates the 109 // followed by phases of being false when the adapter interrogates the
124 // devices found. 110 // devices found.
125 virtual bool IsDiscovering() const; 111 virtual bool IsDiscovering() const = 0;
126 112
127 // Requests that the adapter either begin discovering new devices when 113 // Requests that the adapter either begin discovering new devices when
128 // |discovering| is true, or cease any discovery when false. On success, 114 // |discovering| is true, or cease any discovery when false. On success,
129 // callback will be called. On failure, |error_callback| will be called. 115 // callback will be called. On failure, |error_callback| will be called.
130 virtual void SetDiscovering(bool discovering, 116 virtual void SetDiscovering(bool discovering,
131 const base::Closure& callback, 117 const base::Closure& callback,
132 const ErrorCallback& error_callback); 118 const ErrorCallback& error_callback) = 0;
133 119
134 // Requests the list of devices from the adapter, all are returned 120 // Requests the list of devices from the adapter, all are returned
135 // including those currently connected and those paired. Use the 121 // including those currently connected and those paired. Use the
136 // returned device pointers to determine which they are. 122 // returned device pointers to determine which they are.
137 typedef std::vector<BluetoothDevice*> DeviceList; 123 typedef std::vector<BluetoothDevice*> DeviceList;
138 virtual DeviceList GetDevices(); 124 virtual DeviceList GetDevices() = 0;
139 typedef std::vector<const BluetoothDevice*> ConstDeviceList; 125 typedef std::vector<const BluetoothDevice*> ConstDeviceList;
140 virtual ConstDeviceList GetDevices() const; 126 virtual ConstDeviceList GetDevices() const = 0;
141 127
142 // Returns a pointer to the device with the given address |address| or 128 // Returns a pointer to the device with the given address |address| or
143 // NULL if no such device is known. 129 // NULL if no such device is known.
144 virtual BluetoothDevice* GetDevice(const std::string& address); 130 virtual BluetoothDevice* GetDevice(const std::string& address) = 0;
145 virtual const BluetoothDevice* GetDevice(const std::string& address) const; 131 virtual const BluetoothDevice* GetDevice(
132 const std::string& address) const = 0;
146 133
147 // Requests the local Out Of Band pairing data. 134 // Requests the local Out Of Band pairing data.
148 virtual void ReadLocalOutOfBandPairingData( 135 virtual void ReadLocalOutOfBandPairingData(
149 const BluetoothOutOfBandPairingDataCallback& callback, 136 const BluetoothOutOfBandPairingDataCallback& callback,
150 const ErrorCallback& error_callback); 137 const ErrorCallback& error_callback) = 0;
151
152 // Returns the shared instance for the default adapter, whichever that may
153 // be at the time. Use IsPresent() and the AdapterPresentChanged() observer
154 // method to determine whether an adapter is actually available or not.
155 static scoped_refptr<BluetoothAdapter> DefaultAdapter();
bryeung 2012/09/13 19:52:57 this should probably still be in the interface
keybuk 2012/09/13 23:57:30 Agreed, we still want the concept of "Default Adap
youngki 2012/09/14 15:26:12 Just discussed with Bryan offline and I will make
keybuk 2012/09/14 17:16:36 sgtm
youngki 2012/09/17 21:53:02 Done.
156
157 // Creates an instance for a specific adapter named by |address|, which
158 // may be the bluetooth address of the adapter or a device name such as
159 // "hci0".
160 static BluetoothAdapter* Create(const std::string& address);
161
162 private:
163 friend class base::RefCounted<BluetoothAdapter>;
164 friend class BluetoothDevice;
165 friend class MockBluetoothAdapter;
166
167 BluetoothAdapter();
168 virtual ~BluetoothAdapter();
169
170 // Obtains the default adapter object path from the Bluetooth Daemon
171 // and tracks future changes to it.
172 void TrackDefaultAdapter();
173
174 // Obtains the object paht for the adapter named by |address| from the
175 // Bluetooth Daemon.
176 void FindAdapter(const std::string& address);
177
178 // Called by dbus:: in response to the method call sent by both
179 // DefaultAdapter() and FindAdapter(), |object_path| will contain the
180 // dbus object path of the requested adapter when |success| is true.
181 void AdapterCallback(const dbus::ObjectPath& adapter_path, bool success);
182
183 // BluetoothManagerClient::Observer override.
184 //
185 // Called when the default local bluetooth adapter changes.
186 // |object_path| is the dbus object path of the new default adapter.
187 // Not called if all adapters are removed.
188 virtual void DefaultAdapterChanged(const dbus::ObjectPath& adapter_path)
189 OVERRIDE;
190
191 // BluetoothManagerClient::Observer override.
192 //
193 // Called when a local bluetooth adapter is removed.
194 // |object_path| is the dbus object path of the adapter.
195 virtual void AdapterRemoved(const dbus::ObjectPath& adapter_path) OVERRIDE;
196
197 // Changes the tracked adapter to the dbus object path |adapter_path|,
198 // clearing information from the previously tracked adapter and updating
199 // to the new adapter.
200 void ChangeAdapter(const dbus::ObjectPath& adapter_path);
201
202 // Clears the tracked adapter information.
203 void RemoveAdapter();
204
205 // Called by dbus:: in response to the method call send by SetPowered().
206 // |callback| and |error_callback| are the callbacks passed to SetPowered().
207 void OnSetPowered(const base::Closure& callback,
208 const ErrorCallback& error_callback,
209 bool success);
210
211 // Updates the tracked state of the adapter's radio power to |powered|
212 // and notifies observers. Called on receipt of a property changed signal,
213 // and directly using values obtained from properties.
214 void PoweredChanged(bool powered);
215
216 // Called by dbus:: in response to the method calls send by SetDiscovering().
217 // |callback| and |error_callback| are the callbacks passed to
218 // SetDiscovering().
219 void OnStartDiscovery(const base::Closure& callback,
220 const ErrorCallback& error_callback,
221 const dbus::ObjectPath& adapter_path,
222 bool success);
223 void OnStopDiscovery(const base::Closure& callback,
224 const ErrorCallback& error_callback,
225 const dbus::ObjectPath& adapter_path,
226 bool success);
227
228 // Updates the tracked state of the adapter's discovering state to
229 // |discovering| and notifies observers. Called on receipt of a property
230 // changed signal, and directly using values obtained from properties.
231 void DiscoveringChanged(bool discovering);
232
233 // Called by dbus:: in response to the ReadLocalData method call.
234 void OnReadLocalData(const BluetoothOutOfBandPairingDataCallback& callback,
235 const ErrorCallback& error_callback,
236 const BluetoothOutOfBandPairingData& data,
237 bool success);
238
239 // BluetoothAdapterClient::Observer override.
240 //
241 // Called when the adapter with object path |adapter_path| has a
242 // change in value of the property named |property_name|.
243 virtual void AdapterPropertyChanged(const dbus::ObjectPath& adapter_path,
244 const std::string& property_name)
245 OVERRIDE;
246
247 // BluetoothDeviceClient::Observer override.
248 //
249 // Called when the device with object path |device_path| has a
250 // change in value of the property named |property_name|.
251 virtual void DevicePropertyChanged(const dbus::ObjectPath& device_path,
252 const std::string& property_name)
253 OVERRIDE;
254
255 // Updates information on the device with object path |device_path|,
256 // adding it to the |devices_| map if not already present.
257 void UpdateDevice(const dbus::ObjectPath& device_path);
258
259 // Clears the |devices_| list, notifying obsevers of the device removal.
260 void ClearDevices();
261
262 // BluetoothAdapterClient::Observer override.
263 //
264 // Called when the adapter with object path |object_path| has a
265 // new known device with object path |object_path|.
266 virtual void DeviceCreated(const dbus::ObjectPath& adapter_path,
267 const dbus::ObjectPath& device_path) OVERRIDE;
268
269 // BluetoothAdapterClient::Observer override.
270 //
271 // Called when the adapter with object path |object_path| removes
272 // the known device with object path |object_path|.
273 virtual void DeviceRemoved(const dbus::ObjectPath& adapter_path,
274 const dbus::ObjectPath& device_path) OVERRIDE;
275
276 // Updates the adapter |devices_| list, adding or updating devices using
277 // the object paths in the|devices| list. This doesn't remove devices,
278 // relying instead on the DeviceRemoved() signal for that. Called on
279 // receipt of a property changed signal, and directly using values obtained
280 // from properties.
281 void DevicesChanged(const std::vector<dbus::ObjectPath>& devices);
282
283 // Clears discovered devices from the |devices_| list, notifying
284 // observers, and leaving only those devices with a dbus object path.
285 void ClearDiscoveredDevices();
286
287 // BluetoothAdapterClient::Observer override.
288 //
289 // Called when the adapter with object path |object_path| discovers
290 // a new remote device with address |address| and properties
291 // |properties|, there is no device object path until connected.
292 //
293 // |properties| supports only value() calls, not Get() or Set(), and
294 // should be copied if needed.
295 virtual void DeviceFound(
296 const dbus::ObjectPath& adapter_path, const std::string& address,
297 const BluetoothDeviceClient::Properties& properties) OVERRIDE;
298
299 // BluetoothAdapterClient::Observer override.
300 //
301 // Called when the adapter with object path |object_path| can no
302 // longer communicate with the discovered removed device with
303 // address |address|.
304 virtual void DeviceDisappeared(const dbus::ObjectPath& object_path,
305 const std::string& address) OVERRIDE;
306
307 // List of observers interested in event notifications from us.
308 ObserverList<BluetoothAdapter::Observer> observers_;
309
310 // Object path of adapter for this instance, this is fixed at creation time
311 // unless |track_default_| is true in which case we update it to always
312 // point at the default adapter.
313 bool track_default_;
314 dbus::ObjectPath object_path_;
315
316 // Address of the adapter.
317 std::string address_;
318
319 // Name of the adapter.
320 std::string name_;
321
322 // Tracked adapter state, cached locally so we only send change notifications
323 // to observers on a genuine change.
324 bool powered_;
325 bool discovering_;
326
327 // Devices paired with, connected to, discovered by, or visible to the
328 // adapter. The key is the Bluetooth address of the device and the value
329 // is the BluetoothDevice object whose lifetime is managed by the adapter
330 // instance.
331 typedef std::map<const std::string, BluetoothDevice*> DevicesMap;
332 DevicesMap devices_;
333
334 // Note: This should remain the last member so it'll be destroyed and
335 // invalidate its weak pointers before any other members are destroyed.
336 base::WeakPtrFactory<BluetoothAdapter> weak_ptr_factory_;
337
338 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapter);
339 }; 138 };
340 139
341 } // namespace chromeos 140 } // namespace chromeos
342 141
343 #endif // CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_ADAPTER_H_ 142 #endif // CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_ADAPTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698