diff options
author | tengs@chromium.org <tengs@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-08 03:13:30 +0000 |
---|---|---|
committer | tengs@chromium.org <tengs@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-08 03:13:30 +0000 |
commit | 473ad002ab402ea336a2c311917fce2903bcd11d (patch) | |
tree | d5de24b7179332dae4132edfb83a6dcd1e8a7370 /device | |
parent | 0018f7209a0d9abb9cf58c07234b494e6d17bcb7 (diff) | |
download | chromium_src-473ad002ab402ea336a2c311917fce2903bcd11d.zip chromium_src-473ad002ab402ea336a2c311917fce2903bcd11d.tar.gz chromium_src-473ad002ab402ea336a2c311917fce2903bcd11d.tar.bz2 |
Introduce SetName and SetDiscoverable functions to BluetoothAdapter.
These patch only implements these functions for ChromeOS.
BUG=332938
TEST=new unit tests
Review URL: https://codereview.chromium.org/156923003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@249917 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'device')
-rw-r--r-- | device/bluetooth/bluetooth_adapter.h | 23 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter_chromeos.cc | 86 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter_chromeos.h | 28 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter_mac.h | 8 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter_mac.mm | 19 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter_win.cc | 18 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_adapter_win.h | 10 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_chromeos_unittest.cc | 82 | ||||
-rw-r--r-- | device/bluetooth/test/mock_bluetooth_adapter.h | 11 |
9 files changed, 259 insertions, 26 deletions
diff --git a/device/bluetooth/bluetooth_adapter.h b/device/bluetooth/bluetooth_adapter.h index ed61586..24e9dcd 100644 --- a/device/bluetooth/bluetooth_adapter.h +++ b/device/bluetooth/bluetooth_adapter.h @@ -43,6 +43,12 @@ class BluetoothAdapter : public base::RefCounted<BluetoothAdapter> { virtual void AdapterPoweredChanged(BluetoothAdapter* adapter, bool powered) {} + // Called when the discoverability state of the adapter |adapter| changes, + // when |discoverable| is true the adapter is discoverable by other devices, + // false means the adapter is not discoverable. + virtual void AdapterDiscoverableChanged(BluetoothAdapter* adapter, + bool discoverable) {} + // Called when the discovering state of the adapter |adapter| changes, // when |discovering| is true the adapter is seeking new devices, false // means it is not. @@ -91,6 +97,12 @@ class BluetoothAdapter : public base::RefCounted<BluetoothAdapter> { // The name of the adapter. virtual std::string GetName() const = 0; + // Set the human-readable name of the adapter to |name|. On success, + // |callback| will be called. On failure, |error_callback| will be called. + virtual void SetName(const std::string& name, + const base::Closure& callback, + const ErrorCallback& error_callback) = 0; + // Indicates whether the adapter is initialized and ready to use. virtual bool IsInitialized() const = 0; @@ -109,6 +121,17 @@ class BluetoothAdapter : public base::RefCounted<BluetoothAdapter> { const base::Closure& callback, const ErrorCallback& error_callback) = 0; + // Indicates whether the adapter radio is discoverable. + virtual bool IsDiscoverable() const = 0; + + // Requests that the adapter change its discoverability state. If + // |discoverable| is true, then it will be discoverable by other Bluetooth + // devices. On successly changing the adapter's discoverability, |callback| + // will be called. On failure, |error_callback| will be called. + virtual void SetDiscoverable(bool discoverable, + const base::Closure& callback, + const ErrorCallback& error_callback) = 0; + // Indicates whether the adapter is currently discovering new devices. virtual bool IsDiscovering() const = 0; diff --git a/device/bluetooth/bluetooth_adapter_chromeos.cc b/device/bluetooth/bluetooth_adapter_chromeos.cc index 319a6e9..0d962b9 100644 --- a/device/bluetooth/bluetooth_adapter_chromeos.cc +++ b/device/bluetooth/bluetooth_adapter_chromeos.cc @@ -79,6 +79,18 @@ std::string BluetoothAdapterChromeOS::GetName() const { return properties->alias.value(); } +void BluetoothAdapterChromeOS::SetName(const std::string& name, + const base::Closure& callback, + const ErrorCallback& error_callback) { + DBusThreadManager::Get()->GetBluetoothAdapterClient()-> + GetProperties(object_path_)->alias.Set( + name, + base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, + weak_ptr_factory_.GetWeakPtr(), + callback, + error_callback)); +} + bool BluetoothAdapterChromeOS::IsInitialized() const { return true; } @@ -105,7 +117,31 @@ void BluetoothAdapterChromeOS::SetPowered( DBusThreadManager::Get()->GetBluetoothAdapterClient()-> GetProperties(object_path_)->powered.Set( powered, - base::Bind(&BluetoothAdapterChromeOS::OnSetPowered, + base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, + weak_ptr_factory_.GetWeakPtr(), + callback, + error_callback)); +} + +bool BluetoothAdapterChromeOS::IsDiscoverable() const { + if (!IsPresent()) + return false; + + BluetoothAdapterClient::Properties* properties = + DBusThreadManager::Get()->GetBluetoothAdapterClient()-> + GetProperties(object_path_); + + return properties->discoverable.value(); +} + +void BluetoothAdapterChromeOS::SetDiscoverable( + bool discoverable, + const base::Closure& callback, + const ErrorCallback& error_callback) { + DBusThreadManager::Get()->GetBluetoothAdapterClient()-> + GetProperties(object_path_)->discoverable.Set( + discoverable, + base::Bind(&BluetoothAdapterChromeOS::OnSetDiscoverable, weak_ptr_factory_.GetWeakPtr(), callback, error_callback)); @@ -185,6 +221,8 @@ void BluetoothAdapterChromeOS::AdapterPropertyChanged( if (property_name == properties->powered.name()) PoweredChanged(properties->powered.value()); + else if (property_name == properties->discoverable.name()) + DiscoverableChanged(properties->discoverable.value()); else if (property_name == properties->discovering.name()) DiscoveringChanged(properties->discovering.value()); } @@ -297,7 +335,7 @@ void BluetoothAdapterChromeOS::SetAdapter(const dbus::ObjectPath& object_path) { VLOG(1) << object_path_.value() << ": using adapter."; - SetAdapterName(); + SetDefaultAdapterName(); BluetoothAdapterClient::Properties* properties = DBusThreadManager::Get()->GetBluetoothAdapterClient()-> @@ -307,6 +345,8 @@ void BluetoothAdapterChromeOS::SetAdapter(const dbus::ObjectPath& object_path) { if (properties->powered.value()) PoweredChanged(true); + if (properties->discoverable.value()) + DiscoverableChanged(true); if (properties->discovering.value()) DiscoveringChanged(true); @@ -326,7 +366,7 @@ void BluetoothAdapterChromeOS::SetAdapter(const dbus::ObjectPath& object_path) { } } -void BluetoothAdapterChromeOS::SetAdapterName() { +void BluetoothAdapterChromeOS::SetDefaultAdapterName() { std::string board = base::SysInfo::GetLsbReleaseBoard(); std::string alias; if (board.substr(0, 6) == "stumpy") { @@ -337,16 +377,7 @@ void BluetoothAdapterChromeOS::SetAdapterName() { alias = "Chromebook"; } - DBusThreadManager::Get()->GetBluetoothAdapterClient()-> - GetProperties(object_path_)->alias.Set( - alias, - base::Bind(&BluetoothAdapterChromeOS::OnSetAlias, - weak_ptr_factory_.GetWeakPtr())); -} - -void BluetoothAdapterChromeOS::OnSetAlias(bool success) { - LOG_IF(WARNING, !success) << object_path_.value() - << ": Failed to set adapter alias"; + SetName(alias, base::Bind(&base::DoNothing), base::Bind(&base::DoNothing)); } void BluetoothAdapterChromeOS::RemoveAdapter() { @@ -361,6 +392,8 @@ void BluetoothAdapterChromeOS::RemoveAdapter() { if (properties->powered.value()) PoweredChanged(false); + if (properties->discoverable.value()) + DiscoverableChanged(false); if (properties->discovering.value()) DiscoveringChanged(false); @@ -384,6 +417,11 @@ void BluetoothAdapterChromeOS::PoweredChanged(bool powered) { AdapterPoweredChanged(this, powered)); } +void BluetoothAdapterChromeOS::DiscoverableChanged(bool discoverable) { + FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, + AdapterDiscoverableChanged(this, discoverable)); +} + void BluetoothAdapterChromeOS::DiscoveringChanged( bool discovering) { FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, @@ -403,9 +441,25 @@ void BluetoothAdapterChromeOS::NotifyDeviceChanged( DeviceChanged(this, device)); } -void BluetoothAdapterChromeOS::OnSetPowered(const base::Closure& callback, - const ErrorCallback& error_callback, - bool success) { +void BluetoothAdapterChromeOS::OnSetDiscoverable( + const base::Closure& callback, + const ErrorCallback& error_callback, + bool success) { + // Set the discoverable_timeout property to zero so the adapter remains + // discoverable forever. + DBusThreadManager::Get()->GetBluetoothAdapterClient()-> + GetProperties(object_path_)->discoverable_timeout.Set( + 0, + base::Bind(&BluetoothAdapterChromeOS::OnPropertyChangeCompleted, + weak_ptr_factory_.GetWeakPtr(), + callback, + error_callback)); +} + +void BluetoothAdapterChromeOS::OnPropertyChangeCompleted( + const base::Closure& callback, + const ErrorCallback& error_callback, + bool success) { if (success) callback.Run(); else diff --git a/device/bluetooth/bluetooth_adapter_chromeos.h b/device/bluetooth/bluetooth_adapter_chromeos.h index 74beb35..b400ed3 100644 --- a/device/bluetooth/bluetooth_adapter_chromeos.h +++ b/device/bluetooth/bluetooth_adapter_chromeos.h @@ -40,6 +40,9 @@ class BluetoothAdapterChromeOS device::BluetoothAdapter::Observer* observer) OVERRIDE; virtual std::string GetAddress() const OVERRIDE; virtual std::string GetName() const OVERRIDE; + virtual void SetName(const std::string& name, + const base::Closure& callback, + const ErrorCallback& error_callback) OVERRIDE; virtual bool IsInitialized() const OVERRIDE; virtual bool IsPresent() const OVERRIDE; virtual bool IsPowered() const OVERRIDE; @@ -47,6 +50,11 @@ class BluetoothAdapterChromeOS bool powered, const base::Closure& callback, const ErrorCallback& error_callback) OVERRIDE; + virtual bool IsDiscoverable() const OVERRIDE; + virtual void SetDiscoverable( + bool discoverable, + const base::Closure& callback, + const ErrorCallback& error_callback) OVERRIDE; virtual bool IsDiscovering() const OVERRIDE; virtual void StartDiscovering( const base::Closure& callback, @@ -95,10 +103,8 @@ class BluetoothAdapterChromeOS // subsequently operate on that adapter until it is removed. void SetAdapter(const dbus::ObjectPath& object_path); - // Set the adapter name to one chosen from the system information, and method - // called by dbus:: on completion of the alias property change. - void SetAdapterName(); - void OnSetAlias(bool success); + // Set the adapter name to one chosen from the system information. + void SetDefaultAdapterName(); // Remove the currently tracked adapter. IsPresent() will return false after // this is called. @@ -106,6 +112,7 @@ class BluetoothAdapterChromeOS // Announce to observers a change in the adapter state. void PoweredChanged(bool powered); + void DiscoverableChanged(bool discoverable); void DiscoveringChanged(bool discovering); void PresentChanged(bool present); @@ -113,10 +120,15 @@ class BluetoothAdapterChromeOS // its D-Bus properties. void NotifyDeviceChanged(BluetoothDeviceChromeOS* device); - // Called by dbus:: on completion of the powered property change. - void OnSetPowered(const base::Closure& callback, - const ErrorCallback& error_callback, - bool success); + // Called by dbus:: on completion of the discoverable property change. + void OnSetDiscoverable(const base::Closure& callback, + const ErrorCallback& error_callback, + bool success); + + // Called by dbus:: on completion of an adapter property change. + void OnPropertyChangeCompleted(const base::Closure& callback, + const ErrorCallback& error_callback, + bool success); // Called by dbus:: on completion of the D-Bus method call to start discovery. void OnStartDiscovery(const base::Closure& callback); diff --git a/device/bluetooth/bluetooth_adapter_mac.h b/device/bluetooth/bluetooth_adapter_mac.h index 7054acf..6639db0 100644 --- a/device/bluetooth/bluetooth_adapter_mac.h +++ b/device/bluetooth/bluetooth_adapter_mac.h @@ -47,6 +47,9 @@ class BluetoothAdapterMac : public BluetoothAdapter { virtual void RemoveObserver(BluetoothAdapter::Observer* observer) OVERRIDE; virtual std::string GetAddress() const OVERRIDE; virtual std::string GetName() const OVERRIDE; + virtual void SetName(const std::string& name, + const base::Closure& callback, + const ErrorCallback& error_callback) OVERRIDE; virtual bool IsInitialized() const OVERRIDE; virtual bool IsPresent() const OVERRIDE; virtual bool IsPowered() const OVERRIDE; @@ -54,6 +57,11 @@ class BluetoothAdapterMac : public BluetoothAdapter { bool powered, const base::Closure& callback, const ErrorCallback& error_callback) OVERRIDE; + virtual bool IsDiscoverable() const OVERRIDE; + virtual void SetDiscoverable( + bool discoverable, + const base::Closure& callback, + const ErrorCallback& error_callback) OVERRIDE; virtual bool IsDiscovering() const OVERRIDE; virtual void StartDiscovering( diff --git a/device/bluetooth/bluetooth_adapter_mac.mm b/device/bluetooth/bluetooth_adapter_mac.mm index e15b894..502ed81 100644 --- a/device/bluetooth/bluetooth_adapter_mac.mm +++ b/device/bluetooth/bluetooth_adapter_mac.mm @@ -127,6 +127,12 @@ std::string BluetoothAdapterMac::GetName() const { return name_; } +void BluetoothAdapterMac::SetName(const std::string& name, + const base::Closure& callback, + const ErrorCallback& error_callback) { + NOTIMPLEMENTED(); +} + bool BluetoothAdapterMac::IsInitialized() const { return true; } @@ -142,6 +148,19 @@ bool BluetoothAdapterMac::IsPowered() const { void BluetoothAdapterMac::SetPowered(bool powered, const base::Closure& callback, const ErrorCallback& error_callback) { + NOTIMPLEMENTED(); +} + +bool BluetoothAdapterMac::IsDiscoverable() const { + NOTIMPLEMENTED(); + return false; +} + +void BluetoothAdapterMac::SetDiscoverable( + bool discoverable, + const base::Closure& callback, + const ErrorCallback& error_callback) { + NOTIMPLEMENTED(); } bool BluetoothAdapterMac::IsDiscovering() const { diff --git a/device/bluetooth/bluetooth_adapter_win.cc b/device/bluetooth/bluetooth_adapter_win.cc index f6ca1d6..521afae 100644 --- a/device/bluetooth/bluetooth_adapter_win.cc +++ b/device/bluetooth/bluetooth_adapter_win.cc @@ -53,6 +53,12 @@ std::string BluetoothAdapterWin::GetName() const { return name_; } +void BluetoothAdapterWin::SetName(const std::string& name, + const base::Closure& callback, + const ErrorCallback& error_callback) { + NOTIMPLEMENTED(); +} + // TODO(youngki): Return true when |task_manager_| initializes the adapter // state. bool BluetoothAdapterWin::IsInitialized() const { @@ -74,6 +80,18 @@ void BluetoothAdapterWin::SetPowered( task_manager_->PostSetPoweredBluetoothTask(powered, callback, error_callback); } +bool BluetoothAdapterWin::IsDiscoverable() const { + NOTIMPLEMENTED(); + return false; +} + +void BluetoothAdapterWin::SetDiscoverable( + bool discoverable, + const base::Closure& callback, + const ErrorCallback& error_callback) { + NOTIMPLEMENTED(); +} + bool BluetoothAdapterWin::IsDiscovering() const { return discovery_status_ == DISCOVERING || discovery_status_ == DISCOVERY_STOPPING; diff --git a/device/bluetooth/bluetooth_adapter_win.h b/device/bluetooth/bluetooth_adapter_win.h index a017e29..773c6f3 100644 --- a/device/bluetooth/bluetooth_adapter_win.h +++ b/device/bluetooth/bluetooth_adapter_win.h @@ -39,11 +39,19 @@ class BluetoothAdapterWin : public BluetoothAdapter, virtual void RemoveObserver(BluetoothAdapter::Observer* observer) OVERRIDE; virtual std::string GetAddress() const OVERRIDE; virtual std::string GetName() const OVERRIDE; + virtual void SetName(const std::string& name, + const base::Closure& callback, + const ErrorCallback& error_callback) OVERRIDE; virtual bool IsInitialized() const OVERRIDE; virtual bool IsPresent() const OVERRIDE; virtual bool IsPowered() const OVERRIDE; virtual void SetPowered( - bool powered, + bool discoverable, + const base::Closure& callback, + const ErrorCallback& error_callback) OVERRIDE; + virtual bool IsDiscoverable() const OVERRIDE; + virtual void SetDiscoverable( + bool discoverable, const base::Closure& callback, const ErrorCallback& error_callback) OVERRIDE; virtual bool IsDiscovering() const OVERRIDE; diff --git a/device/bluetooth/bluetooth_chromeos_unittest.cc b/device/bluetooth/bluetooth_chromeos_unittest.cc index c49cee4..a1cc67d5 100644 --- a/device/bluetooth/bluetooth_chromeos_unittest.cc +++ b/device/bluetooth/bluetooth_chromeos_unittest.cc @@ -28,6 +28,7 @@ class TestObserver : public BluetoothAdapter::Observer { TestObserver(scoped_refptr<BluetoothAdapter> adapter) : present_changed_count_(0), powered_changed_count_(0), + discoverable_changed_count_(0), discovering_changed_count_(0), last_present_(false), last_powered_(false), @@ -56,6 +57,13 @@ class TestObserver : public BluetoothAdapter::Observer { last_powered_ = powered; } + virtual void AdapterDiscoverableChanged(BluetoothAdapter* adapter, + bool discoverable) OVERRIDE { + EXPECT_EQ(adapter_, adapter); + + ++discoverable_changed_count_; + } + virtual void AdapterDiscoveringChanged(BluetoothAdapter* adapter, bool discovering) OVERRIDE { EXPECT_EQ(adapter_, adapter); @@ -99,6 +107,7 @@ class TestObserver : public BluetoothAdapter::Observer { int present_changed_count_; int powered_changed_count_; + int discoverable_changed_count_; int discovering_changed_count_; bool last_present_; bool last_powered_; @@ -507,6 +516,79 @@ TEST_F(BluetoothChromeOSTest, BecomeNotPowered) { EXPECT_FALSE(adapter_->IsPowered()); } +TEST_F(BluetoothChromeOSTest, ChangeAdapterName) { + GetAdapter(); + + static const std::string new_name(".__."); + + adapter_->SetName( + new_name, + base::Bind(&BluetoothChromeOSTest::Callback, + base::Unretained(this)), + base::Bind(&BluetoothChromeOSTest::ErrorCallback, + base::Unretained(this))); + EXPECT_EQ(1, callback_count_); + EXPECT_EQ(0, error_callback_count_); + + EXPECT_EQ(new_name, adapter_->GetName()); +} + +TEST_F(BluetoothChromeOSTest, BecomeDiscoverable) { + GetAdapter(); + ASSERT_FALSE(adapter_->IsDiscoverable()); + + // Install an observer; expect the AdapterDiscoverableChanged to be called + // with true, and IsDiscoverable() to return true. + TestObserver observer(adapter_); + adapter_->AddObserver(&observer); + + adapter_->SetDiscoverable( + true, + base::Bind(&BluetoothChromeOSTest::Callback, + base::Unretained(this)), + base::Bind(&BluetoothChromeOSTest::ErrorCallback, + base::Unretained(this))); + EXPECT_EQ(1, callback_count_); + EXPECT_EQ(0, error_callback_count_); + + EXPECT_EQ(1, observer.discoverable_changed_count_); + + EXPECT_TRUE(adapter_->IsDiscoverable()); +} + +TEST_F(BluetoothChromeOSTest, BecomeNotDiscoverable) { + GetAdapter(); + adapter_->SetDiscoverable( + true, + base::Bind(&BluetoothChromeOSTest::Callback, + base::Unretained(this)), + base::Bind(&BluetoothChromeOSTest::ErrorCallback, + base::Unretained(this))); + EXPECT_EQ(1, callback_count_); + EXPECT_EQ(0, error_callback_count_); + callback_count_ = 0; + + ASSERT_TRUE(adapter_->IsDiscoverable()); + + // Install an observer; expect the AdapterDiscoverableChanged to be called + // with false, and IsDiscoverable() to return false. + TestObserver observer(adapter_); + adapter_->AddObserver(&observer); + + adapter_->SetDiscoverable( + false, + base::Bind(&BluetoothChromeOSTest::Callback, + base::Unretained(this)), + base::Bind(&BluetoothChromeOSTest::ErrorCallback, + base::Unretained(this))); + EXPECT_EQ(1, callback_count_); + EXPECT_EQ(0, error_callback_count_); + + EXPECT_EQ(1, observer.discoverable_changed_count_); + + EXPECT_FALSE(adapter_->IsDiscoverable()); +} + TEST_F(BluetoothChromeOSTest, StopDiscovery) { base::MessageLoop message_loop; diff --git a/device/bluetooth/test/mock_bluetooth_adapter.h b/device/bluetooth/test/mock_bluetooth_adapter.h index d2c9878..0266f03 100644 --- a/device/bluetooth/test/mock_bluetooth_adapter.h +++ b/device/bluetooth/test/mock_bluetooth_adapter.h @@ -35,11 +35,20 @@ class MockBluetoothAdapter : public BluetoothAdapter { MOCK_METHOD1(RemoveObserver, void(BluetoothAdapter::Observer*)); MOCK_CONST_METHOD0(GetAddress, std::string()); MOCK_CONST_METHOD0(GetName, std::string()); + MOCK_METHOD3(SetName, + void(const std::string& name, + const base::Closure& callback, + const ErrorCallback& error_callback)); MOCK_CONST_METHOD0(IsInitialized, bool()); MOCK_CONST_METHOD0(IsPresent, bool()); MOCK_CONST_METHOD0(IsPowered, bool()); MOCK_METHOD3(SetPowered, - void(bool discovering, + void(bool powered, + const base::Closure& callback, + const ErrorCallback& error_callback)); + MOCK_CONST_METHOD0(IsDiscoverable, bool()); + MOCK_METHOD3(SetDiscoverable, + void(bool discoverable, const base::Closure& callback, const ErrorCallback& error_callback)); MOCK_CONST_METHOD0(IsDiscovering, bool()); |