Commit ddf658cc authored by Austin Tankiang's avatar Austin Tankiang Committed by Commit Bot

Convert ErrorTolerantBleAdertisement to BindOnce/OnceCallback

|stop_callback_| can no longer be used to indicate that Stop() has been
called as running the callback will make it null, so add a |stopped_|
field instead.

Bug: 1007662
Change-Id: Ifea869fa1a80f1c6eeeced5abf8a56bf55808365
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2374911Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Commit-Queue: Austin Tankiang <austinct@chromium.org>
Cr-Commit-Position: refs/heads/master@{#802074}
parent b051a0b5
......@@ -28,7 +28,7 @@ class ErrorTolerantBleAdvertisement {
// of simply deleting an ErrorTolerantBleAdvertisement object. Clients should
// not assume that advertising has actually stopped until |callback| has been
// invoked.
virtual void Stop(const base::Closure& callback) = 0;
virtual void Stop(base::OnceClosure callback) = 0;
// Returns whether Stop() has been called.
virtual bool HasBeenStopped() = 0;
......
......@@ -67,16 +67,18 @@ ErrorTolerantBleAdvertisementImpl::~ErrorTolerantBleAdvertisementImpl() {
advertisement_->RemoveObserver(this);
}
void ErrorTolerantBleAdvertisementImpl::Stop(const base::Closure& callback) {
void ErrorTolerantBleAdvertisementImpl::Stop(base::OnceClosure callback) {
// Stop() should only be called once per instance.
DCHECK(!stopped_);
DCHECK(stop_callback_.is_null());
stop_callback_ = callback;
stopped_ = true;
stop_callback_ = std::move(callback);
UpdateRegistrationStatus();
}
bool ErrorTolerantBleAdvertisementImpl::HasBeenStopped() {
return !stop_callback_.is_null();
return stopped_;
}
void ErrorTolerantBleAdvertisementImpl::AdvertisementReleased(
......@@ -119,9 +121,10 @@ void ErrorTolerantBleAdvertisementImpl::AttemptRegistration() {
ble_synchronizer_->RegisterAdvertisement(
std::move(advertisement_data),
base::Bind(&ErrorTolerantBleAdvertisementImpl::OnAdvertisementRegistered,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(
base::BindOnce(
&ErrorTolerantBleAdvertisementImpl::OnAdvertisementRegistered,
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(
&ErrorTolerantBleAdvertisementImpl::OnErrorRegisteringAdvertisement,
weak_ptr_factory_.GetWeakPtr()));
}
......@@ -140,10 +143,10 @@ void ErrorTolerantBleAdvertisementImpl::AttemptUnregistration() {
ble_synchronizer_->UnregisterAdvertisement(
advertisement_,
base::Bind(
base::BindOnce(
&ErrorTolerantBleAdvertisementImpl::OnAdvertisementUnregistered,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(
base::BindOnce(
&ErrorTolerantBleAdvertisementImpl::OnErrorUnregisteringAdvertisement,
weak_ptr_factory_.GetWeakPtr()));
}
......@@ -207,7 +210,7 @@ void ErrorTolerantBleAdvertisementImpl::OnAdvertisementUnregistered() {
advertisement_ = nullptr;
DCHECK(!stop_callback_.is_null());
stop_callback_.Run();
std::move(stop_callback_).Run();
}
void ErrorTolerantBleAdvertisementImpl::OnErrorUnregisteringAdvertisement(
......
......@@ -56,7 +56,7 @@ class ErrorTolerantBleAdvertisementImpl
BleSynchronizerBase* ble_synchronizer);
// ErrorTolerantBleAdvertisement:
void Stop(const base::Closure& callback) override;
void Stop(base::OnceClosure callback) override;
bool HasBeenStopped() override;
// device::BluetoothAdvertisement::Observer
......@@ -93,7 +93,8 @@ class ErrorTolerantBleAdvertisementImpl
scoped_refptr<device::BluetoothAdvertisement> advertisement_;
base::Closure stop_callback_;
bool stopped_ = false;
base::OnceClosure stop_callback_;
base::WeakPtrFactory<ErrorTolerantBleAdvertisementImpl> weak_ptr_factory_{
this};
......
......@@ -97,7 +97,7 @@ class SecureChannelErrorTolerantBleAdvertisementImplTest
}
void CallStop() {
advertisement_->Stop(base::Bind(
advertisement_->Stop(base::BindOnce(
&SecureChannelErrorTolerantBleAdvertisementImplTest::OnStopped,
base::Unretained(this)));
}
......
......@@ -22,17 +22,18 @@ FakeErrorTolerantBleAdvertisement::~FakeErrorTolerantBleAdvertisement() {
}
bool FakeErrorTolerantBleAdvertisement::HasBeenStopped() {
return !stop_callback_.is_null();
return stopped_;
}
void FakeErrorTolerantBleAdvertisement::InvokeStopCallback() {
DCHECK(HasBeenStopped());
stop_callback_.Run();
std::move(stop_callback_).Run();
}
void FakeErrorTolerantBleAdvertisement::Stop(const base::Closure& callback) {
void FakeErrorTolerantBleAdvertisement::Stop(base::OnceClosure callback) {
DCHECK(!HasBeenStopped());
stop_callback_ = callback;
stopped_ = true;
stop_callback_ = std::move(callback);
}
} // namespace secure_channel
......
......@@ -28,13 +28,14 @@ class FakeErrorTolerantBleAdvertisement : public ErrorTolerantBleAdvertisement {
void InvokeStopCallback();
// ErrorTolerantBleAdvertisement:
void Stop(const base::Closure& callback) override;
void Stop(base::OnceClosure callback) override;
bool HasBeenStopped() override;
private:
base::UnguessableToken id_;
base::OnceCallback<void(const DeviceIdPair&)> destructor_callback_;
base::Closure stop_callback_;
base::OnceClosure stop_callback_;
bool stopped_ = false;
DISALLOW_COPY_AND_ASSIGN(FakeErrorTolerantBleAdvertisement);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment