Skip to content

Commit

Permalink
Introduce SetName and SetDiscoverable functions to BluetoothAdapter.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tengs@chromium.org committed Feb 8, 2014
1 parent 0018f72 commit 473ad00
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 27 deletions.
5 changes: 4 additions & 1 deletion chromeos/dbus/fake_bluetooth_adapter_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ void FakeBluetoothAdapterClient::Properties::Set(
dbus::PropertyBase *property,
dbus::PropertySet::SetCallback callback) {
VLOG(1) << "Set " << property->name();
if (property->name() == powered.name() || property->name() == alias.name()) {
if (property->name() == powered.name() ||
property->name() == alias.name() ||
property->name() == discoverable.name() ||
property->name() == discoverable_timeout.name()) {
callback.Run(true);
property->ReplaceValueWithSetValue();
} else {
Expand Down
23 changes: 23 additions & 0 deletions device/bluetooth/bluetooth_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down
86 changes: 70 additions & 16 deletions device/bluetooth/bluetooth_adapter_chromeos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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));
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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()->
Expand All @@ -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);

Expand All @@ -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") {
Expand All @@ -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() {
Expand All @@ -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);

Expand All @@ -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_,
Expand All @@ -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
Expand Down
28 changes: 20 additions & 8 deletions device/bluetooth/bluetooth_adapter_chromeos.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,21 @@ 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;
virtual void SetPowered(
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,
Expand Down Expand Up @@ -95,28 +103,32 @@ 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.
void RemoveAdapter();

// 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);

// Announce to observers a change in device state that is not reflected by
// 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);
Expand Down
8 changes: 8 additions & 0 deletions device/bluetooth/bluetooth_adapter_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,21 @@ 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;
virtual void SetPowered(
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(
Expand Down
19 changes: 19 additions & 0 deletions device/bluetooth/bluetooth_adapter_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ - (void)deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender
return name_;
}

void BluetoothAdapterMac::SetName(const std::string& name,
const base::Closure& callback,
const ErrorCallback& error_callback) {
NOTIMPLEMENTED();
}

bool BluetoothAdapterMac::IsInitialized() const {
return true;
}
Expand All @@ -142,6 +148,19 @@ - (void)deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender
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 {
Expand Down
18 changes: 18 additions & 0 deletions device/bluetooth/bluetooth_adapter_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion device/bluetooth/bluetooth_adapter_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 473ad00

Please sign in to comment.