Commit 6d4a3203 authored by Michael Hansen's avatar Michael Hansen Committed by Commit Bot

[Nearby] Clean up stored callbacks in FastInitiationManager.

This cleans up the pattern in which we were storing callbacks as
instance variables in FastInitiationManager. This is no longer required
because BluetoothAdapter now accepts OnceCallbacks, so we can just carry
them through the flow as bound input parameters.

Fixed: 1119970
Change-Id: Iee6bc637dc847b6810694bd63b6b008e1738c62a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2468786
Commit-Queue: Michael Hansen <hansenmichael@google.com>
Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Reviewed-by: default avatarJames Vecore <vecore@google.com>
Cr-Commit-Position: refs/heads/master@{#820047}
parent 84aeb0f7
......@@ -68,31 +68,24 @@ void FastInitiationManager::StartAdvertising(
base::OnceCallback<void()> error_callback) {
DCHECK(adapter_->IsPresent() && adapter_->IsPowered());
DCHECK(!advertisement_);
// These callbacks are instances of OnceCallback, but BluetoothAdapter methods
// expect RepeatingCallbacks. Passing these as arguments is possible using
// Passed(), but this is dangerous so we just store them to run later.
start_callback_ = std::move(callback);
start_error_callback_ = std::move(error_callback);
RegisterAdvertisement(type);
RegisterAdvertisement(type, std::move(callback), std::move(error_callback));
}
void FastInitiationManager::StopAdvertising(
base::OnceCallback<void()> callback) {
stop_callback_ = std::move(callback);
if (!advertisement_) {
std::move(stop_callback_).Run();
std::move(callback).Run();
// |this| might be destroyed here, do not access local fields.
return;
}
UnregisterAdvertisement();
UnregisterAdvertisement(std::move(callback));
}
void FastInitiationManager::RegisterAdvertisement(
FastInitiationManager::FastInitType type) {
FastInitiationManager::FastInitType type,
base::OnceClosure callback,
base::OnceClosure error_callback) {
auto advertisement_data =
std::make_unique<device::BluetoothAdvertisement::Data>(
device::BluetoothAdvertisement::ADVERTISEMENT_TYPE_BROADCAST);
......@@ -114,51 +107,55 @@ void FastInitiationManager::RegisterAdvertisement(
adapter_->RegisterAdvertisement(
std::move(advertisement_data),
base::BindOnce(&FastInitiationManager::OnRegisterAdvertisement,
weak_ptr_factory_.GetWeakPtr()),
weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
base::BindOnce(&FastInitiationManager::OnRegisterAdvertisementError,
weak_ptr_factory_.GetWeakPtr()));
weak_ptr_factory_.GetWeakPtr(),
std::move(error_callback)));
}
void FastInitiationManager::OnRegisterAdvertisement(
base::OnceClosure callback,
scoped_refptr<device::BluetoothAdvertisement> advertisement) {
advertisement_ = advertisement;
advertisement_->AddObserver(this);
std::move(start_callback_).Run();
start_error_callback_.Reset();
std::move(callback).Run();
}
void FastInitiationManager::OnRegisterAdvertisementError(
base::OnceClosure error_callback,
device::BluetoothAdvertisement::ErrorCode error_code) {
NS_LOG(ERROR)
<< "FastInitiationManager::StartAdvertising() failed with error code = "
<< error_code;
start_callback_.Reset();
std::move(start_error_callback_).Run();
std::move(error_callback).Run();
// |this| might be destroyed here, do not access local fields.
}
void FastInitiationManager::UnregisterAdvertisement() {
void FastInitiationManager::UnregisterAdvertisement(
base::OnceClosure callback) {
advertisement_->RemoveObserver(this);
advertisement_->Unregister(
base::BindOnce(&FastInitiationManager::OnUnregisterAdvertisement,
weak_ptr_factory_.GetWeakPtr()),
weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
base::BindOnce(&FastInitiationManager::OnUnregisterAdvertisementError,
weak_ptr_factory_.GetWeakPtr()));
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void FastInitiationManager::OnUnregisterAdvertisement() {
void FastInitiationManager::OnUnregisterAdvertisement(
base::OnceClosure callback) {
advertisement_.reset();
std::move(stop_callback_).Run();
std::move(callback).Run();
// |this| might be destroyed here, do not access local fields.
}
void FastInitiationManager::OnUnregisterAdvertisementError(
base::OnceClosure callback,
device::BluetoothAdvertisement::ErrorCode error_code) {
NS_LOG(WARNING)
<< "FastInitiationManager::StopAdvertising() failed with error code = "
<< error_code;
advertisement_.reset();
std::move(stop_callback_).Run();
std::move(callback).Run();
// |this| might be destroyed here, do not access local fields.
}
......
......@@ -51,25 +51,30 @@ class FastInitiationManager : public device::BluetoothAdvertisement::Observer {
// Begin broadcasting Fast Initiation advertisement.
virtual void StartAdvertising(FastInitType type,
base::OnceCallback<void()> callback,
base::OnceCallback<void()> error_callback);
base::OnceClosure callback,
base::OnceClosure error_callback);
// Stop broadcasting Fast Initiation advertisement.
virtual void StopAdvertising(base::OnceCallback<void()> callback);
virtual void StopAdvertising(base::OnceClosure callback);
private:
// device::BluetoothAdvertisement::Observer:
void AdvertisementReleased(
device::BluetoothAdvertisement* advertisement) override;
void RegisterAdvertisement(FastInitType type);
void RegisterAdvertisement(FastInitType type,
base::OnceClosure callback,
base::OnceClosure error_callback);
void OnRegisterAdvertisement(
base::OnceClosure callback,
scoped_refptr<device::BluetoothAdvertisement> advertisement);
void OnRegisterAdvertisementError(
base::OnceClosure error_callback,
device::BluetoothAdvertisement::ErrorCode error_code);
void UnregisterAdvertisement();
void OnUnregisterAdvertisement();
void UnregisterAdvertisement(base::OnceClosure callback);
void OnUnregisterAdvertisement(base::OnceClosure callback);
void OnUnregisterAdvertisementError(
base::OnceClosure callback,
device::BluetoothAdvertisement::ErrorCode error_code);
// Fast Init V1 metadata has 2 bytes, in format
......@@ -79,9 +84,6 @@ class FastInitiationManager : public device::BluetoothAdvertisement::Observer {
scoped_refptr<device::BluetoothAdapter> adapter_;
scoped_refptr<device::BluetoothAdvertisement> advertisement_;
base::OnceCallback<void()> start_callback_;
base::OnceCallback<void()> start_error_callback_;
base::OnceCallback<void()> stop_callback_;
base::WeakPtrFactory<FastInitiationManager> weak_ptr_factory_{this};
};
......
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