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

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

Issue 10899037: Refactoring bluetooth API code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added #include <string> into bluetooth_adapter_dbus.cc. 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_DBUS_H_
6 #define CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_ADAPTER_H_ 6 #define CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_ADAPTER_DBUS_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/observer_list.h" 15 #include "base/observer_list.h"
16 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter_interface.h"
16 #include "chromeos/dbus/bluetooth_adapter_client.h" 17 #include "chromeos/dbus/bluetooth_adapter_client.h"
17 #include "chromeos/dbus/bluetooth_device_client.h" 18 #include "chromeos/dbus/bluetooth_device_client.h"
18 #include "chromeos/dbus/bluetooth_manager_client.h" 19 #include "chromeos/dbus/bluetooth_manager_client.h"
19 #include "chromeos/dbus/bluetooth_out_of_band_client.h" 20 #include "chromeos/dbus/bluetooth_out_of_band_client.h"
20 #include "dbus/object_path.h" 21 #include "dbus/object_path.h"
21 22
22 namespace chromeos { 23 namespace chromeos {
23 24
24 class BluetoothDevice; 25 class BluetoothDeviceDBus;
25 26
26 // The BluetoothAdapter class represents a local Bluetooth adapter which 27 struct BluetoothOutOfBandPairingData;
27 // may be used to interact with remote Bluetooth devices. As well as 28
28 // providing support for determining whether an adapter is present, and 29 // The BluetoothAdapterDBus class is an implementation of
29 // whether the radio is powered, this class also provides support for 30 // BluetoothAdapterInterface using DBus for Linux/ChromeOS platform.
30 // obtaining the list of remote devices known to the adapter, discovering
31 // new devices, and providing notification of updates to device information.
32 // 31 //
33 // The class may be instantiated for either a specific adapter, or for the 32 // This class may be instantiated for either a specific adapter, or for the
34 // generic "default adapter" which may change depending on availability. 33 // generic "default adapter" which may change depending on availability.
35 class BluetoothAdapter : public base::RefCounted<BluetoothAdapter>, 34 class BluetoothAdapterDBus : public base::RefCounted<BluetoothAdapterDBus>,
36 public BluetoothManagerClient::Observer, 35 public BluetoothAdapterInterface,
37 public BluetoothAdapterClient::Observer, 36 public BluetoothManagerClient::Observer,
38 public BluetoothDeviceClient::Observer { 37 public BluetoothAdapterClient::Observer,
38 public BluetoothDeviceClient::Observer {
39 public: 39 public:
40 // Interface for observing changes from bluetooth adapters. 40 // Interface for observing changes from bluetooth adapters.
41 class Observer { 41 class Observer {
bryeung 2012/09/07 18:35:57 The observer belongs in the interface.
youngki 2012/09/13 18:05:02 Done.
42 public: 42 public:
43 virtual ~Observer() {} 43 virtual ~Observer() {}
44 44
45 // Called when the presence of the adapter |adapter| changes, when 45 // Called when the presence of the adapter |adapter| changes, when
46 // |present| is true the adapter is now present, false means the adapter 46 // |present| is true the adapter is now present, false means the adapter
47 // has been removed from the system. 47 // has been removed from the system.
48 virtual void AdapterPresentChanged(BluetoothAdapter* adapter, 48 virtual void AdapterPresentChanged(BluetoothAdapterDBus* adapter,
49 bool present) {} 49 bool present) {}
50 50
51 // Called when the radio power state of the adapter |adapter| changes, 51 // Called when the radio power state of the adapter |adapter| changes,
52 // when |powered| is true the adapter radio is powered, false means the 52 // when |powered| is true the adapter radio is powered, false means the
53 // adapter radio is off. 53 // adapter radio is off.
54 virtual void AdapterPoweredChanged(BluetoothAdapter* adapter, 54 virtual void AdapterPoweredChanged(BluetoothAdapterDBus* adapter,
55 bool powered) {} 55 bool powered) {}
56 56
57 // Called when the discovering state of the adapter |adapter| changes, 57 // Called when the discovering state of the adapter |adapter| changes,
58 // when |discovering| is true the adapter is seeking new devices, false 58 // when |discovering| is true the adapter is seeking new devices, false
59 // means it is not. Note that device discovery involves both states when 59 // 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 60 // the adapter is seeking new devices and states when it is not because
61 // it is interrogating the devices it found. 61 // it is interrogating the devices it found.
62 virtual void AdapterDiscoveringChanged(BluetoothAdapter* adapter, 62 virtual void AdapterDiscoveringChanged(BluetoothAdapterDBus* adapter,
63 bool discovering) {} 63 bool discovering) {}
64 64
65 // Called when a new device |device| is added to the adapter |adapter|, 65 // Called when a new device |device| is added to the adapter |adapter|,
66 // either because it has been discovered or a connection made. |device| 66 // either because it has been discovered or a connection made. |device|
67 // should not be cached, instead copy its address. 67 // should not be cached, instead copy its address.
68 virtual void DeviceAdded(BluetoothAdapter* adapter, 68 virtual void DeviceAdded(BluetoothAdapterDBus* adapter,
69 BluetoothDevice* device) {} 69 BluetoothDeviceDBus* device) {}
70 70
71 // Called when properties of the device |device| known to the adapter 71 // Called when properties of the device |device| known to the adapter
72 // |adapter| change. |device| should not be cached, instead copy its 72 // |adapter| change. |device| should not be cached, instead copy its
73 // address. 73 // address.
74 virtual void DeviceChanged(BluetoothAdapter* adapter, 74 virtual void DeviceChanged(BluetoothAdapterDBus* adapter,
75 BluetoothDevice* device) {} 75 BluetoothDeviceDBus* device) {}
76 76
77 // Called when the device |device| is removed from the adapter |adapter|, 77 // Called when the device |device| is removed from the adapter |adapter|,
78 // either as a result of a discovered device being lost between discovering 78 // either as a result of a discovered device being lost between discovering
79 // phases or pairing information deleted. |device| should not be cached. 79 // phases or pairing information deleted. |device| should not be cached.
80 virtual void DeviceRemoved(BluetoothAdapter* adapter, 80 virtual void DeviceRemoved(BluetoothAdapterDBus* adapter,
81 BluetoothDevice* device) {} 81 BluetoothDeviceDBus* device) {}
82 }; 82 };
83 83
84 // Adds and removes observers for events on this bluetooth adapter, 84 // Adds and removes observers for events on this bluetooth adapter,
85 // if monitoring multiple adapters check the |adapter| parameter of 85 // if monitoring multiple adapters check the |adapter| parameter of
86 // observer methods to determine which adapter is issuing the event. 86 // observer methods to determine which adapter is issuing the event.
87 void AddObserver(Observer* observer); 87 void AddObserver(Observer* observer);
88 void RemoveObserver(Observer* observer); 88 void RemoveObserver(Observer* observer);
89 89
90 // 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.
92 typedef base::Callback<void()> ErrorCallback;
93
94 // The BluetoothOutOfBandPairingDataCallback is used to return
95 // BluetoothOutOfBandPairingData to the caller.
96 typedef base::Callback<void(const BluetoothOutOfBandPairingData& data)>
97 BluetoothOutOfBandPairingDataCallback;
98
99 // The address of this adapter. The address format is "XX:XX:XX:XX:XX:XX", 90 // The address of this adapter. The address format is "XX:XX:XX:XX:XX:XX",
100 // where each XX is a hexadecimal number. 91 // where each XX is a hexadecimal number.
101 const std::string& address() const { return address_; } 92 virtual const std::string& address() const OVERRIDE;
102 93
103 // The name of the adapter. 94 // The name of the adapter.
104 const std::string& name() const { return name_; } 95 virtual const std::string& name() const OVERRIDE;
105 96
106 // Indicates whether the adapter is actually present on the system, for 97 // Indicates whether the adapter is actually present on the system, for
107 // the default adapter this indicates whether any adapter is present. 98 // the default adapter this indicates whether any adapter is present.
108 virtual bool IsPresent() const; 99 virtual bool IsPresent() const OVERRIDE;
109 100
110 // Indicates whether the adapter radio is powered. 101 // Indicates whether the adapter radio is powered.
111 virtual bool IsPowered() const; 102 virtual bool IsPowered() const OVERRIDE;
112 103
113 // Requests a change to the adapter radio power, setting |powered| to true 104 // Requests a change to the adapter radio power, setting |powered| to true
114 // will turn on the radio and false will turn it off. On success, callback 105 // will turn on the radio and false will turn it off. On success, callback
115 // will be called. On failure, |error_callback| will be called. 106 // will be called. On failure, |error_callback| will be called.
116 void SetPowered(bool powered, 107 void SetPowered(bool powered,
117 const base::Closure& callback, 108 const base::Closure& callback,
118 const ErrorCallback& error_callback); 109 const ErrorCallback& error_callback);
119 110
120 // Indicates whether the adapter is currently discovering new devices, 111 // Indicates whether the adapter is currently discovering new devices,
121 // note that a typical discovery process has phases of this being true 112 // note that a typical discovery process has phases of this being true
122 // followed by phases of being false when the adapter interrogates the 113 // followed by phases of being false when the adapter interrogates the
123 // devices found. 114 // devices found.
124 virtual bool IsDiscovering() const; 115 virtual bool IsDiscovering() const OVERRIDE;
125 116
126 // Requests that the adapter either begin discovering new devices when 117 // Requests that the adapter either begin discovering new devices when
127 // |discovering| is true, or cease any discovery when false. On success, 118 // |discovering| is true, or cease any discovery when false. On success,
128 // callback will be called. On failure, |error_callback| will be called. 119 // callback will be called. On failure, |error_callback| will be called.
129 virtual void SetDiscovering(bool discovering, 120 virtual void SetDiscovering(bool discovering,
130 const base::Closure& callback, 121 const base::Closure& callback,
131 const ErrorCallback& error_callback); 122 const ErrorCallback& error_callback) OVERRIDE;
132 123
133 // Requests the list of devices from the adapter, all are returned 124 // Requests the list of devices from the adapter, all are returned
134 // including those currently connected and those paired. Use the 125 // including those currently connected and those paired. Use the
135 // returned device pointers to determine which they are. 126 // returned device pointers to determine which they are.
136 typedef std::vector<BluetoothDevice*> DeviceList; 127 virtual DeviceList GetDevices() OVERRIDE;
137 virtual DeviceList GetDevices(); 128 virtual ConstDeviceList GetDevices() const OVERRIDE;
138 typedef std::vector<const BluetoothDevice*> ConstDeviceList;
139 virtual ConstDeviceList GetDevices() const;
140 129
141 // Returns a pointer to the device with the given address |address| or 130 // Returns a pointer to the device with the given address |address| or
142 // NULL if no such device is known. 131 // NULL if no such device is known.
143 virtual BluetoothDevice* GetDevice(const std::string& address); 132 virtual BluetoothDeviceInterface* GetDevice(const std::string& address)
144 virtual const BluetoothDevice* GetDevice(const std::string& address) const; 133 OVERRIDE;
134 virtual const BluetoothDeviceInterface* GetDevice(
135 const std::string& address) const OVERRIDE;
145 136
146 // Requests the local Out Of Band pairing data. 137 // Requests the local Out Of Band pairing data.
147 virtual void ReadLocalOutOfBandPairingData( 138 virtual void ReadLocalOutOfBandPairingData(
148 const BluetoothOutOfBandPairingDataCallback& callback, 139 const BluetoothOutOfBandPairingDataCallback& callback,
149 const ErrorCallback& error_callback); 140 const ErrorCallback& error_callback) OVERRIDE;
150 141
151 // Returns the shared instance for the default adapter, whichever that may 142 // Returns the shared instance for the default adapter, whichever that may
152 // be at the time. Use IsPresent() and the AdapterPresentChanged() observer 143 // be at the time. Use IsPresent() and the AdapterPresentChanged() observer
153 // method to determine whether an adapter is actually available or not. 144 // method to determine whether an adapter is actually available or not.
154 static scoped_refptr<BluetoothAdapter> DefaultAdapter(); 145 static scoped_refptr<BluetoothAdapterDBus> DefaultAdapter();
155 146
156 // Creates an instance for a specific adapter named by |address|, which 147 // Creates an instance for a specific adapter named by |address|, which
157 // may be the bluetooth address of the adapter or a device name such as 148 // may be the bluetooth address of the adapter or a device name such as
158 // "hci0". 149 // "hci0".
159 static BluetoothAdapter* Create(const std::string& address); 150 static BluetoothAdapterDBus* Create(const std::string& address);
160 151
161 private: 152 private:
162 friend class base::RefCounted<BluetoothAdapter>; 153 friend class base::RefCounted<BluetoothAdapterDBus>;
163 friend class BluetoothDevice; 154 friend class BluetoothDeviceDBus;
164 friend class MockBluetoothAdapter; 155 friend class MockBluetoothAdapterDBus;
165 156
166 BluetoothAdapter(); 157 BluetoothAdapterDBus();
167 virtual ~BluetoothAdapter(); 158 virtual ~BluetoothAdapterDBus();
168 159
169 // Obtains the default adapter object path from the Bluetooth Daemon 160 // Obtains the default adapter object path from the Bluetooth Daemon
170 // and tracks future changes to it. 161 // and tracks future changes to it.
171 void TrackDefaultAdapter(); 162 void TrackDefaultAdapter();
172 163
173 // Obtains the object paht for the adapter named by |address| from the 164 // Obtains the object paht for the adapter named by |address| from the
174 // Bluetooth Daemon. 165 // Bluetooth Daemon.
175 void FindAdapter(const std::string& address); 166 void FindAdapter(const std::string& address);
176 167
177 // Called by dbus:: in response to the method call sent by both 168 // Called by dbus:: in response to the method call sent by both
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 288
298 // BluetoothAdapterClient::Observer override. 289 // BluetoothAdapterClient::Observer override.
299 // 290 //
300 // Called when the adapter with object path |object_path| can no 291 // Called when the adapter with object path |object_path| can no
301 // longer communicate with the discovered removed device with 292 // longer communicate with the discovered removed device with
302 // address |address|. 293 // address |address|.
303 virtual void DeviceDisappeared(const dbus::ObjectPath& object_path, 294 virtual void DeviceDisappeared(const dbus::ObjectPath& object_path,
304 const std::string& address) OVERRIDE; 295 const std::string& address) OVERRIDE;
305 296
306 // List of observers interested in event notifications from us. 297 // List of observers interested in event notifications from us.
307 ObserverList<BluetoothAdapter::Observer> observers_; 298 ObserverList<BluetoothAdapterDBus::Observer> observers_;
308 299
309 // Object path of adapter for this instance, this is fixed at creation time 300 // Object path of adapter for this instance, this is fixed at creation time
310 // unless |track_default_| is true in which case we update it to always 301 // unless |track_default_| is true in which case we update it to always
311 // point at the default adapter. 302 // point at the default adapter.
312 bool track_default_; 303 bool track_default_;
313 dbus::ObjectPath object_path_; 304 dbus::ObjectPath object_path_;
314 305
315 // Address of the adapter. 306 // Address of the adapter.
316 std::string address_; 307 std::string address_;
317 308
318 // Name of the adapter. 309 // Name of the adapter.
319 std::string name_; 310 std::string name_;
320 311
321 // Tracked adapter state, cached locally so we only send change notifications 312 // Tracked adapter state, cached locally so we only send change notifications
322 // to observers on a genuine change. 313 // to observers on a genuine change.
323 bool powered_; 314 bool powered_;
324 bool discovering_; 315 bool discovering_;
325 316
326 // Devices paired with, connected to, discovered by, or visible to the 317 // Devices paired with, connected to, discovered by, or visible to the
327 // adapter. The key is the Bluetooth address of the device and the value 318 // adapter. The key is the Bluetooth address of the device and the value
328 // is the BluetoothDevice object whose lifetime is managed by the adapter 319 // is the BluetoothDeviceDBus object whose lifetime is managed by the adapter
329 // instance. 320 // instance.
330 typedef std::map<const std::string, BluetoothDevice*> DevicesMap; 321 typedef std::map<const std::string, BluetoothDeviceDBus*> DevicesMap;
331 DevicesMap devices_; 322 DevicesMap devices_;
332 323
333 // Note: This should remain the last member so it'll be destroyed and 324 // Note: This should remain the last member so it'll be destroyed and
334 // invalidate its weak pointers before any other members are destroyed. 325 // invalidate its weak pointers before any other members are destroyed.
335 base::WeakPtrFactory<BluetoothAdapter> weak_ptr_factory_; 326 base::WeakPtrFactory<BluetoothAdapterDBus> weak_ptr_factory_;
336 327
337 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapter); 328 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterDBus);
338 }; 329 };
339 330
340 } // namespace chromeos 331 } // namespace chromeos
341 332
342 #endif // CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_ADAPTER_H_ 333 #endif // CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_ADAPTER_DBUS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698