Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 { |
| 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 { return address_; } |
|
kevers
2012/09/07 15:39:25
Virtual methods with non-empty bodies shouldn't be
youngki
2012/09/07 18:18:37
Done.
| |
| 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 { return name_; } |
| 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; |
| 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; |
| 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 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 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); |
| 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; | |
| 137 virtual DeviceList GetDevices(); | 127 virtual DeviceList GetDevices(); |
| 138 typedef std::vector<const BluetoothDevice*> ConstDeviceList; | |
| 139 virtual ConstDeviceList GetDevices() const; | 128 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 virtual const BluetoothDeviceInterface* GetDevice( |
| 134 const std::string& address) const; | |
| 145 | 135 |
| 146 // Requests the local Out Of Band pairing data. | 136 // Requests the local Out Of Band pairing data. |
| 147 virtual void ReadLocalOutOfBandPairingData( | 137 virtual void ReadLocalOutOfBandPairingData( |
| 148 const BluetoothOutOfBandPairingDataCallback& callback, | 138 const BluetoothOutOfBandPairingDataCallback& callback, |
| 149 const ErrorCallback& error_callback); | 139 const ErrorCallback& error_callback); |
| 150 | 140 |
| 151 // Returns the shared instance for the default adapter, whichever that may | 141 // Returns the shared instance for the default adapter, whichever that may |
| 152 // be at the time. Use IsPresent() and the AdapterPresentChanged() observer | 142 // be at the time. Use IsPresent() and the AdapterPresentChanged() observer |
| 153 // method to determine whether an adapter is actually available or not. | 143 // method to determine whether an adapter is actually available or not. |
| 154 static scoped_refptr<BluetoothAdapter> DefaultAdapter(); | 144 static scoped_refptr<BluetoothAdapterDBus> DefaultAdapter(); |
| 155 | 145 |
| 156 // Creates an instance for a specific adapter named by |address|, which | 146 // 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 | 147 // may be the bluetooth address of the adapter or a device name such as |
| 158 // "hci0". | 148 // "hci0". |
| 159 static BluetoothAdapter* Create(const std::string& address); | 149 static BluetoothAdapterDBus* Create(const std::string& address); |
| 160 | 150 |
| 161 private: | 151 private: |
| 162 friend class base::RefCounted<BluetoothAdapter>; | 152 friend class base::RefCounted<BluetoothAdapterDBus>; |
| 163 friend class BluetoothDevice; | 153 friend class BluetoothDeviceDBus; |
| 164 friend class MockBluetoothAdapter; | 154 friend class MockBluetoothAdapterDBus; |
| 165 | 155 |
| 166 BluetoothAdapter(); | 156 BluetoothAdapterDBus(); |
| 167 virtual ~BluetoothAdapter(); | 157 virtual ~BluetoothAdapterDBus(); |
| 168 | 158 |
| 169 // Obtains the default adapter object path from the Bluetooth Daemon | 159 // Obtains the default adapter object path from the Bluetooth Daemon |
| 170 // and tracks future changes to it. | 160 // and tracks future changes to it. |
| 171 void TrackDefaultAdapter(); | 161 void TrackDefaultAdapter(); |
| 172 | 162 |
| 173 // Obtains the object paht for the adapter named by |address| from the | 163 // Obtains the object paht for the adapter named by |address| from the |
| 174 // Bluetooth Daemon. | 164 // Bluetooth Daemon. |
| 175 void FindAdapter(const std::string& address); | 165 void FindAdapter(const std::string& address); |
| 176 | 166 |
| 177 // Called by dbus:: in response to the method call sent by both | 167 // Called by dbus:: in response to the method call sent by both |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 297 | 287 |
| 298 // BluetoothAdapterClient::Observer override. | 288 // BluetoothAdapterClient::Observer override. |
| 299 // | 289 // |
| 300 // Called when the adapter with object path |object_path| can no | 290 // Called when the adapter with object path |object_path| can no |
| 301 // longer communicate with the discovered removed device with | 291 // longer communicate with the discovered removed device with |
| 302 // address |address|. | 292 // address |address|. |
| 303 virtual void DeviceDisappeared(const dbus::ObjectPath& object_path, | 293 virtual void DeviceDisappeared(const dbus::ObjectPath& object_path, |
| 304 const std::string& address) OVERRIDE; | 294 const std::string& address) OVERRIDE; |
| 305 | 295 |
| 306 // List of observers interested in event notifications from us. | 296 // List of observers interested in event notifications from us. |
| 307 ObserverList<BluetoothAdapter::Observer> observers_; | 297 ObserverList<BluetoothAdapterDBus::Observer> observers_; |
| 308 | 298 |
| 309 // Weak pointer factory for generating 'this' pointers that might live longer | 299 // Weak pointer factory for generating 'this' pointers that might live longer |
| 310 // than we do. | 300 // than we do. |
| 311 base::WeakPtrFactory<BluetoothAdapter> weak_ptr_factory_; | 301 base::WeakPtrFactory<BluetoothAdapterDBus> weak_ptr_factory_; |
| 312 | 302 |
| 313 // Object path of adapter for this instance, this is fixed at creation time | 303 // Object path of adapter for this instance, this is fixed at creation time |
| 314 // unless |track_default_| is true in which case we update it to always | 304 // unless |track_default_| is true in which case we update it to always |
| 315 // point at the default adapter. | 305 // point at the default adapter. |
| 316 bool track_default_; | 306 bool track_default_; |
| 317 dbus::ObjectPath object_path_; | 307 dbus::ObjectPath object_path_; |
| 318 | 308 |
| 319 // Address of the adapter. | 309 // Address of the adapter. |
| 320 std::string address_; | 310 std::string address_; |
| 321 | 311 |
| 322 // Name of the adapter. | 312 // Name of the adapter. |
| 323 std::string name_; | 313 std::string name_; |
| 324 | 314 |
| 325 // Tracked adapter state, cached locally so we only send change notifications | 315 // Tracked adapter state, cached locally so we only send change notifications |
| 326 // to observers on a genuine change. | 316 // to observers on a genuine change. |
| 327 bool powered_; | 317 bool powered_; |
| 328 bool discovering_; | 318 bool discovering_; |
| 329 | 319 |
| 330 // Devices paired with, connected to, discovered by, or visible to the | 320 // Devices paired with, connected to, discovered by, or visible to the |
| 331 // adapter. The key is the Bluetooth address of the device and the value | 321 // adapter. The key is the Bluetooth address of the device and the value |
| 332 // is the BluetoothDevice object whose lifetime is managed by the adapter | 322 // is the BluetoothDeviceDBus object whose lifetime is managed by the adapter |
| 333 // instance. | 323 // instance. |
| 334 typedef std::map<const std::string, BluetoothDevice*> DevicesMap; | 324 typedef std::map<const std::string, BluetoothDeviceDBus*> DevicesMap; |
| 335 DevicesMap devices_; | 325 DevicesMap devices_; |
| 336 | 326 |
| 337 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapter); | 327 DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterDBus); |
| 338 }; | 328 }; |
| 339 | 329 |
| 340 } // namespace chromeos | 330 } // namespace chromeos |
| 341 | 331 |
| 342 #endif // CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_ADAPTER_H_ | 332 #endif // CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_ADAPTER_DBUS_H_ |
| OLD | NEW |