Skip to content

Commit

Permalink
[bluetooth] Make InitCallback a base::OnceClosure
Browse files Browse the repository at this point in the history
This change turns BluetoothAdapter::InitCallback into a OnceClosure and
updates the corresponding subclasses.

Bug: 834319
Change-Id: I3ac35b38919320a30c332fbd156eb787c203db9f
Reviewed-on: https://chromium-review.googlesource.com/1050125
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: Giovanni Ortuño Urquidi <ortuno@chromium.org>
Cr-Commit-Position: refs/heads/master@{#558279}
  • Loading branch information
jdoerrie authored and Commit Bot committed May 14, 2018
1 parent cfaaff8 commit 0b6af5d
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 34 deletions.
2 changes: 1 addition & 1 deletion device/bluetooth/bluetooth_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ BluetoothAdapter::ServiceOptions::~ServiceOptions() = default;
!defined(OS_WIN) && !defined(OS_LINUX)
// static
base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter(
const InitCallback& init_callback) {
InitCallback init_callback) {
return base::WeakPtr<BluetoothAdapter>();
}
#endif // !defined(OS_CHROMEOS) && !defined(OS_WIN) && !defined(OS_MACOSX)
Expand Down
4 changes: 2 additions & 2 deletions device/bluetooth/bluetooth_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapter

// The InitCallback is used to trigger a callback after asynchronous
// initialization, if initialization is asynchronous on the platform.
using InitCallback = base::Callback<void()>;
using InitCallback = base::OnceClosure;

using DiscoverySessionCallback =
base::Callback<void(std::unique_ptr<BluetoothDiscoverySession>)>;
Expand All @@ -294,7 +294,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapter
// caller is expected to call |AddRef()| on the returned pointer, typically by
// storing it into a |scoped_refptr|.
static base::WeakPtr<BluetoothAdapter> CreateAdapter(
const InitCallback& init_callback);
InitCallback init_callback);

// Returns a weak pointer to an existing adapter for testing purposes only.
base::WeakPtr<BluetoothAdapter> GetWeakPtrForTesting();
Expand Down
2 changes: 1 addition & 1 deletion device/bluetooth/bluetooth_adapter_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace device {

// static
base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter(
const InitCallback& init_callback) {
InitCallback init_callback) {
return BluetoothAdapterAndroid::Create(
BluetoothAdapterWrapper_CreateWithDefaultAdapter());
}
Expand Down
2 changes: 1 addition & 1 deletion device/bluetooth/bluetooth_adapter_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ CBCentralManagerState GetCBManagerState(CBCentralManager* manager) {

// static
base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter(
const InitCallback& init_callback) {
InitCallback init_callback) {
return BluetoothAdapterMac::CreateAdapter();
}

Expand Down
2 changes: 1 addition & 1 deletion device/bluetooth/bluetooth_adapter_stub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace device {
// a particular platform.
// static
base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter(
const InitCallback& init_callback) {
InitCallback init_callback) {
return base::WeakPtr<BluetoothAdapter>();
}

Expand Down
15 changes: 8 additions & 7 deletions device/bluetooth/bluetooth_adapter_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,22 @@ namespace device {

// static
base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter(
const InitCallback& init_callback) {
return BluetoothAdapterWin::CreateAdapter(init_callback);
InitCallback init_callback) {
return BluetoothAdapterWin::CreateAdapter(std::move(init_callback));
}

// static
base::WeakPtr<BluetoothAdapter> BluetoothAdapterWin::CreateAdapter(
const InitCallback& init_callback) {
BluetoothAdapterWin* adapter = new BluetoothAdapterWin(init_callback);
InitCallback init_callback) {
BluetoothAdapterWin* adapter =
new BluetoothAdapterWin(std::move(init_callback));
adapter->Init();
return adapter->weak_ptr_factory_.GetWeakPtr();
}

BluetoothAdapterWin::BluetoothAdapterWin(const InitCallback& init_callback)
BluetoothAdapterWin::BluetoothAdapterWin(InitCallback init_callback)
: BluetoothAdapter(),
init_callback_(init_callback),
init_callback_(std::move(init_callback)),
initialized_(false),
powered_(false),
discovery_status_(NOT_DISCOVERING),
Expand Down Expand Up @@ -222,7 +223,7 @@ void BluetoothAdapterWin::AdapterStateChanged(
}
if (!initialized_) {
initialized_ = true;
init_callback_.Run();
std::move(init_callback_).Run();
}
}

Expand Down
4 changes: 2 additions & 2 deletions device/bluetooth/bluetooth_adapter_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterWin
public BluetoothTaskManagerWin::Observer {
public:
static base::WeakPtr<BluetoothAdapter> CreateAdapter(
const InitCallback& init_callback);
InitCallback init_callback);

// BluetoothAdapter:
std::string GetAddress() const override;
Expand Down Expand Up @@ -107,7 +107,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterWin
DISCOVERY_STOPPING
};

explicit BluetoothAdapterWin(const InitCallback& init_callback);
explicit BluetoothAdapterWin(InitCallback init_callback);
~BluetoothAdapterWin() override;

// BluetoothAdapter:
Expand Down
9 changes: 4 additions & 5 deletions device/bluetooth/bluetooth_adapter_winrt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,19 @@ BluetoothAdapterWinrt::BluetoothAdapterWinrt() : weak_ptr_factory_(this) {

BluetoothAdapterWinrt::~BluetoothAdapterWinrt() = default;

void BluetoothAdapterWinrt::Init(const InitCallback& init_cb) {
void BluetoothAdapterWinrt::Init(InitCallback init_cb) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

// We are wrapping |init_cb| in a ScopedClosureRunner to ensure it gets run no
// matter how the function exits. Furthermore, we set |is_initialized_| to
// true if adapter is still active when the callback gets run.
base::ScopedClosureRunner on_init(base::BindOnce(
[](base::WeakPtr<BluetoothAdapterWinrt> adapter,
const InitCallback& init_cb) {
[](base::WeakPtr<BluetoothAdapterWinrt> adapter, InitCallback init_cb) {
if (adapter)
adapter->is_initialized_ = true;
init_cb.Run();
std::move(init_cb).Run();
},
weak_ptr_factory_.GetWeakPtr(), init_cb));
weak_ptr_factory_.GetWeakPtr(), std::move(init_cb)));

if (!ResolveCoreWinRT())
return;
Expand Down
2 changes: 1 addition & 1 deletion device/bluetooth/bluetooth_adapter_winrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterWinrt : public BluetoothAdapter {
BluetoothAdapterWinrt();
~BluetoothAdapterWinrt() override;

void Init(const InitCallback& init_cb);
void Init(InitCallback init_cb);

// BluetoothAdapter:
bool SetPoweredImpl(bool powered) override;
Expand Down
17 changes: 9 additions & 8 deletions device/bluetooth/bluez/bluetooth_adapter_bluez.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ namespace device {

// static
base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter(
const InitCallback& init_callback) {
return bluez::BluetoothAdapterBlueZ::CreateAdapter(init_callback);
InitCallback init_callback) {
return bluez::BluetoothAdapterBlueZ::CreateAdapter(std::move(init_callback));
}

} // namespace device
Expand Down Expand Up @@ -161,8 +161,9 @@ void ResetAdvertisingErrorCallbackConnector(

// static
base::WeakPtr<BluetoothAdapter> BluetoothAdapterBlueZ::CreateAdapter(
const InitCallback& init_callback) {
BluetoothAdapterBlueZ* adapter = new BluetoothAdapterBlueZ(init_callback);
InitCallback init_callback) {
BluetoothAdapterBlueZ* adapter =
new BluetoothAdapterBlueZ(std::move(init_callback));
return adapter->weak_ptr_factory_.GetWeakPtr();
}

Expand Down Expand Up @@ -228,8 +229,8 @@ void BluetoothAdapterBlueZ::Shutdown() {
dbus_is_shutdown_ = true;
}

BluetoothAdapterBlueZ::BluetoothAdapterBlueZ(const InitCallback& init_callback)
: init_callback_(init_callback),
BluetoothAdapterBlueZ::BluetoothAdapterBlueZ(InitCallback init_callback)
: init_callback_(std::move(init_callback)),
initialized_(false),
dbus_is_shutdown_(false),
num_discovery_sessions_(0),
Expand Down Expand Up @@ -257,7 +258,7 @@ void BluetoothAdapterBlueZ::Init() {
if (dbus_is_shutdown_ ||
!bluez::BluezDBusManager::Get()->IsObjectManagerSupported()) {
initialized_ = true;
init_callback_.Run();
std::move(init_callback_).Run();
return;
}

Expand All @@ -282,7 +283,7 @@ void BluetoothAdapterBlueZ::Init() {
SetAdapter(object_paths[0]);
}
initialized_ = true;
init_callback_.Run();
std::move(init_callback_).Run();
}

BluetoothAdapterBlueZ::~BluetoothAdapterBlueZ() {
Expand Down
4 changes: 2 additions & 2 deletions device/bluetooth/bluez/bluetooth_adapter_bluez.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterBlueZ

// Calls |init_callback| after a BluetoothAdapter is fully initialized.
static base::WeakPtr<BluetoothAdapter> CreateAdapter(
const InitCallback& init_callback);
InitCallback init_callback);

// BluetoothAdapter:
void Shutdown() override;
Expand Down Expand Up @@ -243,7 +243,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterBlueZ
using RegisterProfileCompletionPair =
std::pair<base::Closure, ErrorCompletionCallback>;

explicit BluetoothAdapterBlueZ(const InitCallback& init_callback);
explicit BluetoothAdapterBlueZ(InitCallback init_callback);
~BluetoothAdapterBlueZ() override;

// Init will get asynchronouly called once we know if Object Manager is
Expand Down
2 changes: 1 addition & 1 deletion device/bluetooth/cast/bluetooth_adapter_cast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ base::WeakPtr<BluetoothAdapter> BluetoothAdapterCast::Create(

// static
base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter(
const InitCallback& init_callback) {
InitCallback init_callback) {
return BluetoothAdapterCast::Create(std::move(init_callback));
}

Expand Down
6 changes: 4 additions & 2 deletions device/bluetooth/test/bluetooth_test_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <wrl/client.h>
#include <wrl/implements.h>

#include <utility>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/containers/circular_deque.h"
Expand Down Expand Up @@ -36,9 +38,9 @@ using ABI::Windows::Devices::Bluetooth::IBluetoothAdapter;
class TestBluetoothAdapterWinrt : public device::BluetoothAdapterWinrt {
public:
TestBluetoothAdapterWinrt(ComPtr<IBluetoothAdapter> adapter,
const InitCallback& init_cb)
InitCallback init_cb)
: adapter_(std::move(adapter)) {
Init(init_cb);
Init(std::move(init_cb));
}

protected:
Expand Down

0 comments on commit 0b6af5d

Please sign in to comment.