Commit 9a4c74dd authored by Alex Chau's avatar Alex Chau Committed by Commit Bot

Add SequenceChecker to NearbySharingService

- All NearbySharingService methods should run in same sequence, adding DCHECK_CALLED_ON_VALID_SEQUENCE to enforce that
- Also changed RegisterSendSurface/UnregisterSendSurface to synchronous

Bug: 1084644
Change-Id: I6eac1f5ad082144b2ff3d2c366b7855c93f42592
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2308410
Commit-Queue: Alex Chau <alexchau@chromium.org>
Reviewed-by: default avatarRichard Knoll <knollr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#790330}
parent bc54469a
...@@ -41,16 +41,14 @@ class NearbySharingService { ...@@ -41,16 +41,14 @@ class NearbySharingService {
// Registers a send surface for handling payload transfer status and device // Registers a send surface for handling payload transfer status and device
// discovery. // discovery.
virtual void RegisterSendSurface( virtual StatusCodes RegisterSendSurface(
TransferUpdateCallback* transfer_callback, TransferUpdateCallback* transfer_callback,
ShareTargetDiscoveredCallback* discovery_callback, ShareTargetDiscoveredCallback* discovery_callback) = 0;
StatusCodesCallback status_codes_callback) = 0;
// Unregisters the current send surface. // Unregisters the current send surface.
virtual void UnregisterSendSurface( virtual StatusCodes UnregisterSendSurface(
TransferUpdateCallback* transfer_callback, TransferUpdateCallback* transfer_callback,
ShareTargetDiscoveredCallback* discovery_callback, ShareTargetDiscoveredCallback* discovery_callback) = 0;
StatusCodesCallback status_codes_callback) = 0;
// Registers a receiver surface for handling payload transfer status. // Registers a receiver surface for handling payload transfer status.
virtual StatusCodes RegisterReceiveSurface( virtual StatusCodes RegisterReceiveSurface(
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <utility> #include <utility>
#include "base/bind.h" #include "base/bind.h"
#include "base/task_runner_util.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/nearby_sharing/fast_initiation_manager.h" #include "chrome/browser/nearby_sharing/fast_initiation_manager.h"
#include "chrome/browser/nearby_sharing/logging/logging.h" #include "chrome/browser/nearby_sharing/logging/logging.h"
...@@ -150,26 +151,28 @@ NearbySharingServiceImpl::~NearbySharingServiceImpl() { ...@@ -150,26 +151,28 @@ NearbySharingServiceImpl::~NearbySharingServiceImpl() {
bluetooth_adapter_->RemoveObserver(this); bluetooth_adapter_->RemoveObserver(this);
} }
void NearbySharingServiceImpl::RegisterSendSurface( NearbySharingService::StatusCodes NearbySharingServiceImpl::RegisterSendSurface(
TransferUpdateCallback* transfer_callback, TransferUpdateCallback* transfer_callback,
ShareTargetDiscoveredCallback* discovery_callback, ShareTargetDiscoveredCallback* discovery_callback) {
StatusCodesCallback status_codes_callback) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
register_send_surface_callback_ = std::move(status_codes_callback);
StartFastInitiationAdvertising(); StartFastInitiationAdvertising();
return StatusCodes::kOk;
} }
void NearbySharingServiceImpl::UnregisterSendSurface( NearbySharingService::StatusCodes
NearbySharingServiceImpl::UnregisterSendSurface(
TransferUpdateCallback* transfer_callback, TransferUpdateCallback* transfer_callback,
ShareTargetDiscoveredCallback* discovery_callback, ShareTargetDiscoveredCallback* discovery_callback) {
StatusCodesCallback status_codes_callback) { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
unregister_send_surface_callback_ = std::move(status_codes_callback);
StopFastInitiationAdvertising(); StopFastInitiationAdvertising();
return StatusCodes::kOk;
} }
NearbySharingService::StatusCodes NearbySharingService::StatusCodes
NearbySharingServiceImpl::RegisterReceiveSurface( NearbySharingServiceImpl::RegisterReceiveSurface(
TransferUpdateCallback* transfer_callback, TransferUpdateCallback* transfer_callback,
ReceiveSurfaceState state) { ReceiveSurfaceState state) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(transfer_callback); DCHECK(transfer_callback);
DCHECK_NE(state, ReceiveSurfaceState::kUnknown); DCHECK_NE(state, ReceiveSurfaceState::kUnknown);
if (foreground_receive_callbacks_.HasObserver(transfer_callback) || if (foreground_receive_callbacks_.HasObserver(transfer_callback) ||
...@@ -202,6 +205,7 @@ NearbySharingServiceImpl::RegisterReceiveSurface( ...@@ -202,6 +205,7 @@ NearbySharingServiceImpl::RegisterReceiveSurface(
NearbySharingService::StatusCodes NearbySharingService::StatusCodes
NearbySharingServiceImpl::UnregisterReceiveSurface( NearbySharingServiceImpl::UnregisterReceiveSurface(
TransferUpdateCallback* transfer_callback) { TransferUpdateCallback* transfer_callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(transfer_callback); DCHECK(transfer_callback);
bool is_foreground = bool is_foreground =
foreground_receive_callbacks_.HasObserver(transfer_callback); foreground_receive_callbacks_.HasObserver(transfer_callback);
...@@ -307,6 +311,7 @@ void NearbySharingServiceImpl::OnIncomingConnection( ...@@ -307,6 +311,7 @@ void NearbySharingServiceImpl::OnIncomingConnection(
const std::string& endpoint_id, const std::string& endpoint_id,
const std::vector<uint8_t>& endpoint_info, const std::vector<uint8_t>& endpoint_info,
std::unique_ptr<NearbyConnection> connection) { std::unique_ptr<NearbyConnection> connection) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// TODO(crbug/1085068): Handle incoming connection; use CertificateManager // TODO(crbug/1085068): Handle incoming connection; use CertificateManager
} }
...@@ -384,15 +389,13 @@ void NearbySharingServiceImpl::OnDataUsagePrefChanged() { ...@@ -384,15 +389,13 @@ void NearbySharingServiceImpl::OnDataUsagePrefChanged() {
void NearbySharingServiceImpl::StartFastInitiationAdvertising() { void NearbySharingServiceImpl::StartFastInitiationAdvertising() {
if (!IsBluetoothPresent() || !IsBluetoothPowered()) { if (!IsBluetoothPresent() || !IsBluetoothPowered()) {
std::move(register_send_surface_callback_).Run(StatusCodes::kError); NS_LOG(INFO) << "Failed to advertise FastInitiation. Bluetooth is not "
"present or powered.";
return; return;
} }
if (fast_initiation_manager_) { if (fast_initiation_manager_) {
// TODO(hansenmichael): Do not invoke NS_LOG(INFO) << "Failed to advertise FastInitiation. Already advertising.";
// |register_send_surface_callback_| until Nearby Connections
// scanning is kicked off.
std::move(register_send_surface_callback_).Run(StatusCodes::kOk);
return; return;
} }
...@@ -413,8 +416,7 @@ void NearbySharingServiceImpl::StartFastInitiationAdvertising() { ...@@ -413,8 +416,7 @@ void NearbySharingServiceImpl::StartFastInitiationAdvertising() {
void NearbySharingServiceImpl::StopFastInitiationAdvertising() { void NearbySharingServiceImpl::StopFastInitiationAdvertising() {
if (!fast_initiation_manager_) { if (!fast_initiation_manager_) {
if (unregister_send_surface_callback_) NS_LOG(INFO) << "Can't stop advertising FastInitiation. Not advertising.";
std::move(unregister_send_surface_callback_).Run(StatusCodes::kOk);
return; return;
} }
...@@ -431,7 +433,7 @@ void NearbySharingServiceImpl::GetBluetoothAdapter() { ...@@ -431,7 +433,7 @@ void NearbySharingServiceImpl::GetBluetoothAdapter() {
// Because this will be called from the constructor, GetAdapter() may call // Because this will be called from the constructor, GetAdapter() may call
// OnGetBluetoothAdapter() immediately which can cause problems during tests // OnGetBluetoothAdapter() immediately which can cause problems during tests
// since the class is not fully constructed yet. // since the class is not fully constructed yet.
base::ThreadTaskRunnerHandle::Get()->PostTask( base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, FROM_HERE,
base::BindOnce( base::BindOnce(
&device::BluetoothAdapterFactory::GetAdapter, &device::BluetoothAdapterFactory::GetAdapter,
...@@ -450,22 +452,17 @@ void NearbySharingServiceImpl::OnStartFastInitiationAdvertising() { ...@@ -450,22 +452,17 @@ void NearbySharingServiceImpl::OnStartFastInitiationAdvertising() {
// TODO(hansenmichael): Do not invoke // TODO(hansenmichael): Do not invoke
// |register_send_surface_callback_| until Nearby Connections // |register_send_surface_callback_| until Nearby Connections
// scanning is kicked off. // scanning is kicked off.
std::move(register_send_surface_callback_).Run(StatusCodes::kOk); NS_LOG(VERBOSE) << "Started advertising FastInitiation.";
} }
void NearbySharingServiceImpl::OnStartFastInitiationAdvertisingError() { void NearbySharingServiceImpl::OnStartFastInitiationAdvertisingError() {
fast_initiation_manager_.reset(); fast_initiation_manager_.reset();
std::move(register_send_surface_callback_).Run(StatusCodes::kError); NS_LOG(ERROR) << "Failed to start FastInitiation advertising.";
} }
void NearbySharingServiceImpl::OnStopFastInitiationAdvertising() { void NearbySharingServiceImpl::OnStopFastInitiationAdvertising() {
fast_initiation_manager_.reset(); fast_initiation_manager_.reset();
NS_LOG(VERBOSE) << "Stopped advertising FastInitiation";
// TODO(hansenmichael): Do not invoke
// |unregister_send_surface_callback_| until Nearby Connections
// scanning is stopped.
if (unregister_send_surface_callback_)
std::move(unregister_send_surface_callback_).Run(StatusCodes::kOk);
} }
bool NearbySharingServiceImpl::IsBluetoothPresent() const { bool NearbySharingServiceImpl::IsBluetoothPresent() const {
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/scoped_observer.h" #include "base/scoped_observer.h"
#include "base/sequence_checker.h"
#include "chrome/browser/nearby_sharing/nearby_connections_manager.h" #include "chrome/browser/nearby_sharing/nearby_connections_manager.h"
#include "chrome/browser/nearby_sharing/nearby_constants.h" #include "chrome/browser/nearby_sharing/nearby_constants.h"
#include "chrome/browser/nearby_sharing/nearby_notification_manager.h" #include "chrome/browser/nearby_sharing/nearby_notification_manager.h"
...@@ -26,6 +27,7 @@ class NearbyConnectionsManager; ...@@ -26,6 +27,7 @@ class NearbyConnectionsManager;
class PrefService; class PrefService;
class Profile; class Profile;
// All methods should be called from the same sequence that created the service.
class NearbySharingServiceImpl class NearbySharingServiceImpl
: public NearbySharingService, : public NearbySharingService,
public KeyedService, public KeyedService,
...@@ -40,13 +42,12 @@ class NearbySharingServiceImpl ...@@ -40,13 +42,12 @@ class NearbySharingServiceImpl
~NearbySharingServiceImpl() override; ~NearbySharingServiceImpl() override;
// NearbySharingService: // NearbySharingService:
void RegisterSendSurface(TransferUpdateCallback* transfer_callback, StatusCodes RegisterSendSurface(
ShareTargetDiscoveredCallback* discovery_callback,
StatusCodesCallback status_codes_callback) override;
void UnregisterSendSurface(
TransferUpdateCallback* transfer_callback, TransferUpdateCallback* transfer_callback,
ShareTargetDiscoveredCallback* discovery_callback, ShareTargetDiscoveredCallback* discovery_callback) override;
StatusCodesCallback status_codes_callback) override; StatusCodes UnregisterSendSurface(
TransferUpdateCallback* transfer_callback,
ShareTargetDiscoveredCallback* discovery_callback) override;
StatusCodes RegisterReceiveSurface(TransferUpdateCallback* transfer_callback, StatusCodes RegisterReceiveSurface(TransferUpdateCallback* transfer_callback,
ReceiveSurfaceState state) override; ReceiveSurfaceState state) override;
StatusCodes UnregisterReceiveSurface( StatusCodes UnregisterReceiveSurface(
...@@ -110,8 +111,6 @@ class NearbySharingServiceImpl ...@@ -110,8 +111,6 @@ class NearbySharingServiceImpl
nearby_process_observer_{this}; nearby_process_observer_{this};
scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_; scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_;
std::unique_ptr<FastInitiationManager> fast_initiation_manager_; std::unique_ptr<FastInitiationManager> fast_initiation_manager_;
StatusCodesCallback register_send_surface_callback_;
StatusCodesCallback unregister_send_surface_callback_;
NearbyNotificationManager nearby_notification_manager_; NearbyNotificationManager nearby_notification_manager_;
// A list of foreground receivers. // A list of foreground receivers.
...@@ -143,6 +142,7 @@ class NearbySharingServiceImpl ...@@ -143,6 +142,7 @@ class NearbySharingServiceImpl
// True if we're currently sending or receiving a file. // True if we're currently sending or receiving a file.
bool is_transferring_files_ = false; bool is_transferring_files_ = false;
SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<NearbySharingServiceImpl> weak_ptr_factory_{this}; base::WeakPtrFactory<NearbySharingServiceImpl> weak_ptr_factory_{this};
}; };
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/test/bind_test_util.h"
#include "chrome/browser/nearby_sharing/fake_nearby_connections_manager.h" #include "chrome/browser/nearby_sharing/fake_nearby_connections_manager.h"
#include "chrome/browser/nearby_sharing/fast_initiation_manager.h" #include "chrome/browser/nearby_sharing/fast_initiation_manager.h"
#include "chrome/browser/nearby_sharing/nearby_connections_manager.h" #include "chrome/browser/nearby_sharing/nearby_connections_manager.h"
...@@ -181,40 +182,6 @@ class NearbySharingServiceImplTest : public testing::Test { ...@@ -181,40 +182,6 @@ class NearbySharingServiceImplTest : public testing::Test {
fast_initiation_manager_factory_.get()); fast_initiation_manager_factory_.get());
} }
NearbySharingService::StatusCodes RegisterSendSurfaceAndWait() {
base::RunLoop run_loop;
NearbySharingService::StatusCodes result;
service_->RegisterSendSurface(
/*transfer_callback=*/nullptr, /*discovery_callback=*/nullptr,
base::BindOnce(
[](base::OnceClosure quit_closure,
NearbySharingService::StatusCodes* result,
NearbySharingService::StatusCodes code) {
*result = code;
std::move(quit_closure).Run();
},
run_loop.QuitClosure(), &result));
run_loop.Run();
return result;
}
NearbySharingService::StatusCodes UnregisterSendSurfaceAndWait() {
base::RunLoop run_loop;
NearbySharingService::StatusCodes result;
service_->UnregisterSendSurface(
/*transfer_callback=*/nullptr, /*discovery_callback=*/nullptr,
base::BindOnce(
[](base::OnceClosure quit_closure,
NearbySharingService::StatusCodes* result,
NearbySharingService::StatusCodes code) {
*result = code;
std::move(quit_closure).Run();
},
run_loop.QuitClosure(), &result));
run_loop.Run();
return result;
}
bool IsBluetoothPresent() { return is_bluetooth_present_; } bool IsBluetoothPresent() { return is_bluetooth_present_; }
bool IsBluetoothPowered() { return is_bluetooth_powered_; } bool IsBluetoothPowered() { return is_bluetooth_powered_; }
...@@ -265,42 +232,49 @@ TEST_F(NearbySharingServiceImplTest, DisableNearbyShutdownConnections) { ...@@ -265,42 +232,49 @@ TEST_F(NearbySharingServiceImplTest, DisableNearbyShutdownConnections) {
TEST_F(NearbySharingServiceImplTest, StartFastInitiationAdvertising) { TEST_F(NearbySharingServiceImplTest, StartFastInitiationAdvertising) {
EXPECT_EQ(NearbySharingService::StatusCodes::kOk, EXPECT_EQ(NearbySharingService::StatusCodes::kOk,
RegisterSendSurfaceAndWait()); service_->RegisterSendSurface(/*transfer_callback=*/nullptr,
/*discovery_callback=*/nullptr));
EXPECT_EQ(1u, fast_initiation_manager_factory_->StartAdvertisingCount()); EXPECT_EQ(1u, fast_initiation_manager_factory_->StartAdvertisingCount());
// Call RegisterSendSurface() a second time and make sure StartAdvertising is // Call RegisterSendSurface() a second time and make sure StartAdvertising is
// not called again. // not called again.
EXPECT_EQ(NearbySharingService::StatusCodes::kOk, EXPECT_EQ(NearbySharingService::StatusCodes::kOk,
RegisterSendSurfaceAndWait()); service_->RegisterSendSurface(/*transfer_callback=*/nullptr,
/*discovery_callback=*/nullptr));
EXPECT_EQ(1u, fast_initiation_manager_factory_->StartAdvertisingCount()); EXPECT_EQ(1u, fast_initiation_manager_factory_->StartAdvertisingCount());
} }
TEST_F(NearbySharingServiceImplTest, StartFastInitiationAdvertisingError) { TEST_F(NearbySharingServiceImplTest, StartFastInitiationAdvertisingError) {
SetFakeFastInitiationManagerFactory(/*should_succeed_on_start=*/false); SetFakeFastInitiationManagerFactory(/*should_succeed_on_start=*/false);
EXPECT_EQ(NearbySharingService::StatusCodes::kError, EXPECT_EQ(NearbySharingService::StatusCodes::kOk,
RegisterSendSurfaceAndWait()); service_->RegisterSendSurface(/*transfer_callback=*/nullptr,
/*discovery_callback=*/nullptr));
} }
TEST_F(NearbySharingServiceImplTest, TEST_F(NearbySharingServiceImplTest,
StartFastInitiationAdvertising_BluetoothNotPresent) { StartFastInitiationAdvertising_BluetoothNotPresent) {
is_bluetooth_present_ = false; is_bluetooth_present_ = false;
EXPECT_EQ(NearbySharingService::StatusCodes::kError, EXPECT_EQ(NearbySharingService::StatusCodes::kOk,
RegisterSendSurfaceAndWait()); service_->RegisterSendSurface(/*transfer_callback=*/nullptr,
/*discovery_callback=*/nullptr));
} }
TEST_F(NearbySharingServiceImplTest, TEST_F(NearbySharingServiceImplTest,
StartFastInitiationAdvertising_BluetoothNotPowered) { StartFastInitiationAdvertising_BluetoothNotPowered) {
is_bluetooth_powered_ = false; is_bluetooth_powered_ = false;
EXPECT_EQ(NearbySharingService::StatusCodes::kError, EXPECT_EQ(NearbySharingService::StatusCodes::kOk,
RegisterSendSurfaceAndWait()); service_->RegisterSendSurface(/*transfer_callback=*/nullptr,
/*discovery_callback=*/nullptr));
} }
TEST_F(NearbySharingServiceImplTest, StopFastInitiationAdvertising) { TEST_F(NearbySharingServiceImplTest, StopFastInitiationAdvertising) {
EXPECT_EQ(NearbySharingService::StatusCodes::kOk, EXPECT_EQ(NearbySharingService::StatusCodes::kOk,
RegisterSendSurfaceAndWait()); service_->RegisterSendSurface(/*transfer_callback=*/nullptr,
/*discovery_callback=*/nullptr));
EXPECT_EQ(1u, fast_initiation_manager_factory_->StartAdvertisingCount()); EXPECT_EQ(1u, fast_initiation_manager_factory_->StartAdvertisingCount());
EXPECT_EQ(NearbySharingService::StatusCodes::kOk, EXPECT_EQ(NearbySharingService::StatusCodes::kOk,
UnregisterSendSurfaceAndWait()); service_->UnregisterSendSurface(/*transfer_callback=*/nullptr,
/*discovery_callback=*/nullptr));
EXPECT_TRUE(fast_initiation_manager_factory_ EXPECT_TRUE(fast_initiation_manager_factory_
->StopAdvertisingCalledAndManagerDestroyed()); ->StopAdvertisingCalledAndManagerDestroyed());
} }
...@@ -308,7 +282,8 @@ TEST_F(NearbySharingServiceImplTest, StopFastInitiationAdvertising) { ...@@ -308,7 +282,8 @@ TEST_F(NearbySharingServiceImplTest, StopFastInitiationAdvertising) {
TEST_F(NearbySharingServiceImplTest, TEST_F(NearbySharingServiceImplTest,
StopFastInitiationAdvertising_BluetoothBecomesNotPresent) { StopFastInitiationAdvertising_BluetoothBecomesNotPresent) {
EXPECT_EQ(NearbySharingService::StatusCodes::kOk, EXPECT_EQ(NearbySharingService::StatusCodes::kOk,
RegisterSendSurfaceAndWait()); service_->RegisterSendSurface(/*transfer_callback=*/nullptr,
/*discovery_callback=*/nullptr));
adapter_observer_->AdapterPresentChanged(mock_bluetooth_adapter_.get(), adapter_observer_->AdapterPresentChanged(mock_bluetooth_adapter_.get(),
false); false);
EXPECT_TRUE(fast_initiation_manager_factory_ EXPECT_TRUE(fast_initiation_manager_factory_
...@@ -318,7 +293,8 @@ TEST_F(NearbySharingServiceImplTest, ...@@ -318,7 +293,8 @@ TEST_F(NearbySharingServiceImplTest,
TEST_F(NearbySharingServiceImplTest, TEST_F(NearbySharingServiceImplTest,
StopFastInitiationAdvertising_BluetoothBecomesNotPowered) { StopFastInitiationAdvertising_BluetoothBecomesNotPowered) {
EXPECT_EQ(NearbySharingService::StatusCodes::kOk, EXPECT_EQ(NearbySharingService::StatusCodes::kOk,
RegisterSendSurfaceAndWait()); service_->RegisterSendSurface(/*transfer_callback=*/nullptr,
/*discovery_callback=*/nullptr));
adapter_observer_->AdapterPoweredChanged(mock_bluetooth_adapter_.get(), adapter_observer_->AdapterPoweredChanged(mock_bluetooth_adapter_.get(),
false); false);
EXPECT_TRUE(fast_initiation_manager_factory_ EXPECT_TRUE(fast_initiation_manager_factory_
......
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