Commit bdc21598 authored by khorimoto's avatar khorimoto Committed by Commit bot

[CrOS Tether] Create TetherHostResponseRecorder, which records...

[CrOS Tether] Create TetherHostResponseRecorder, which records ConnectTetheringResponses and TetherAvailabilityResponses from tether hosts.

This takes some existing functionality out of HostScanDevicePrioritizer and extends it to record the IDs of all ConnectTetheringResponses received instead of only the most recent one.

This refactor is needed because the networking stack needs to know whether to show a warning dialog (stating that tethering will use mobile data and battery on the host device) before connecting to a device for the first time. Thus, it is necessary to record all previously received ConnectTetheringResponses instead of only the most recent one. Because this logic is unrelated to the host prioritization logic, I moved it to its own class.

BUG=672263

Review-Url: https://codereview.chromium.org/2844973002
Cr-Commit-Position: refs/heads/master@{#467787}
parent 2b5a31c0
...@@ -55,6 +55,8 @@ static_library("tether") { ...@@ -55,6 +55,8 @@ static_library("tether") {
"tether_device_state_manager.h", "tether_device_state_manager.h",
"tether_host_fetcher.cc", "tether_host_fetcher.cc",
"tether_host_fetcher.h", "tether_host_fetcher.h",
"tether_host_response_recorder.cc",
"tether_host_response_recorder.h",
"tether_network_disconnection_handler.cc", "tether_network_disconnection_handler.cc",
"tether_network_disconnection_handler.h", "tether_network_disconnection_handler.h",
"wifi_hotspot_connector.cc", "wifi_hotspot_connector.cc",
...@@ -98,6 +100,8 @@ static_library("test_support") { ...@@ -98,6 +100,8 @@ static_library("test_support") {
"mock_host_scan_device_prioritizer.h", "mock_host_scan_device_prioritizer.h",
"mock_local_device_data_provider.cc", "mock_local_device_data_provider.cc",
"mock_local_device_data_provider.h", "mock_local_device_data_provider.h",
"mock_tether_host_response_recorder.cc",
"mock_tether_host_response_recorder.h",
] ]
public_deps = [ public_deps = [
...@@ -138,6 +142,7 @@ source_set("unit_tests") { ...@@ -138,6 +142,7 @@ source_set("unit_tests") {
"network_configuration_remover_unittest.cc", "network_configuration_remover_unittest.cc",
"tether_connector_unittest.cc", "tether_connector_unittest.cc",
"tether_host_fetcher_unittest.cc", "tether_host_fetcher_unittest.cc",
"tether_host_response_recorder_unittest.cc",
"tether_network_disconnection_handler_unittest.cc", "tether_network_disconnection_handler_unittest.cc",
"wifi_hotspot_connector_unittest.cc", "wifi_hotspot_connector_unittest.cc",
] ]
......
...@@ -4,9 +4,9 @@ ...@@ -4,9 +4,9 @@
#include "chromeos/components/tether/connect_tethering_operation.h" #include "chromeos/components/tether/connect_tethering_operation.h"
#include "chromeos/components/tether/host_scan_device_prioritizer.h"
#include "chromeos/components/tether/message_wrapper.h" #include "chromeos/components/tether/message_wrapper.h"
#include "chromeos/components/tether/proto/tether.pb.h" #include "chromeos/components/tether/proto/tether.pb.h"
#include "chromeos/components/tether/tether_host_response_recorder.h"
#include "components/proximity_auth/logging/logging.h" #include "components/proximity_auth/logging/logging.h"
namespace chromeos { namespace chromeos {
...@@ -22,12 +22,12 @@ std::unique_ptr<ConnectTetheringOperation> ...@@ -22,12 +22,12 @@ std::unique_ptr<ConnectTetheringOperation>
ConnectTetheringOperation::Factory::NewInstance( ConnectTetheringOperation::Factory::NewInstance(
const cryptauth::RemoteDevice& device_to_connect, const cryptauth::RemoteDevice& device_to_connect,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer) { TetherHostResponseRecorder* tether_host_response_recorder) {
if (!factory_instance_) { if (!factory_instance_) {
factory_instance_ = new Factory(); factory_instance_ = new Factory();
} }
return factory_instance_->BuildInstance(device_to_connect, connection_manager, return factory_instance_->BuildInstance(device_to_connect, connection_manager,
host_scan_device_prioritizer); tether_host_response_recorder);
} }
// static // static
...@@ -40,20 +40,20 @@ std::unique_ptr<ConnectTetheringOperation> ...@@ -40,20 +40,20 @@ std::unique_ptr<ConnectTetheringOperation>
ConnectTetheringOperation::Factory::BuildInstance( ConnectTetheringOperation::Factory::BuildInstance(
const cryptauth::RemoteDevice& device_to_connect, const cryptauth::RemoteDevice& device_to_connect,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer) { TetherHostResponseRecorder* tether_host_response_recorder) {
return base::MakeUnique<ConnectTetheringOperation>( return base::MakeUnique<ConnectTetheringOperation>(
device_to_connect, connection_manager, host_scan_device_prioritizer); device_to_connect, connection_manager, tether_host_response_recorder);
} }
ConnectTetheringOperation::ConnectTetheringOperation( ConnectTetheringOperation::ConnectTetheringOperation(
const cryptauth::RemoteDevice& device_to_connect, const cryptauth::RemoteDevice& device_to_connect,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer) TetherHostResponseRecorder* tether_host_response_recorder)
: MessageTransferOperation( : MessageTransferOperation(
std::vector<cryptauth::RemoteDevice>{device_to_connect}, std::vector<cryptauth::RemoteDevice>{device_to_connect},
connection_manager), connection_manager),
remote_device_(device_to_connect), remote_device_(device_to_connect),
host_scan_device_prioritizer_(host_scan_device_prioritizer), tether_host_response_recorder_(tether_host_response_recorder),
error_code_to_return_( error_code_to_return_(
ConnectTetheringResponse_ResponseCode:: ConnectTetheringResponse_ResponseCode::
ConnectTetheringResponse_ResponseCode_UNKNOWN_ERROR) {} ConnectTetheringResponse_ResponseCode_UNKNOWN_ERROR) {}
...@@ -101,7 +101,7 @@ void ConnectTetheringOperation::OnMessageReceived( ...@@ -101,7 +101,7 @@ void ConnectTetheringOperation::OnMessageReceived(
<< response->ssid() << "\", password: \"" << response->ssid() << "\", password: \""
<< response->password() << "\"}"; << response->password() << "\"}";
host_scan_device_prioritizer_->RecordSuccessfulConnectTetheringResponse( tether_host_response_recorder_->RecordSuccessfulConnectTetheringResponse(
remote_device); remote_device);
ssid_to_return_ = response->ssid(); ssid_to_return_ = response->ssid();
......
...@@ -18,8 +18,8 @@ namespace chromeos { ...@@ -18,8 +18,8 @@ namespace chromeos {
namespace tether { namespace tether {
class HostScanDevicePrioritizer;
class MessageWrapper; class MessageWrapper;
class TetherHostResponseRecorder;
// Operation used to request that a tether host share its Internet connection. // Operation used to request that a tether host share its Internet connection.
// Attempts a connection to the RemoteDevice passed to its constructor and // Attempts a connection to the RemoteDevice passed to its constructor and
...@@ -33,7 +33,7 @@ class ConnectTetheringOperation : public MessageTransferOperation { ...@@ -33,7 +33,7 @@ class ConnectTetheringOperation : public MessageTransferOperation {
static std::unique_ptr<ConnectTetheringOperation> NewInstance( static std::unique_ptr<ConnectTetheringOperation> NewInstance(
const cryptauth::RemoteDevice& device_to_connect, const cryptauth::RemoteDevice& device_to_connect,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer); TetherHostResponseRecorder* tether_host_response_recorder);
static void SetInstanceForTesting(Factory* factory); static void SetInstanceForTesting(Factory* factory);
...@@ -41,7 +41,7 @@ class ConnectTetheringOperation : public MessageTransferOperation { ...@@ -41,7 +41,7 @@ class ConnectTetheringOperation : public MessageTransferOperation {
virtual std::unique_ptr<ConnectTetheringOperation> BuildInstance( virtual std::unique_ptr<ConnectTetheringOperation> BuildInstance(
const cryptauth::RemoteDevice& devices_to_connect, const cryptauth::RemoteDevice& devices_to_connect,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer); TetherHostResponseRecorder* tether_host_response_recorder);
private: private:
static Factory* factory_instance_; static Factory* factory_instance_;
...@@ -61,7 +61,7 @@ class ConnectTetheringOperation : public MessageTransferOperation { ...@@ -61,7 +61,7 @@ class ConnectTetheringOperation : public MessageTransferOperation {
ConnectTetheringOperation( ConnectTetheringOperation(
const cryptauth::RemoteDevice& device_to_connect, const cryptauth::RemoteDevice& device_to_connect,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer); TetherHostResponseRecorder* tether_host_response_recorder);
~ConnectTetheringOperation() override; ~ConnectTetheringOperation() override;
void AddObserver(Observer* observer); void AddObserver(Observer* observer);
...@@ -84,7 +84,7 @@ class ConnectTetheringOperation : public MessageTransferOperation { ...@@ -84,7 +84,7 @@ class ConnectTetheringOperation : public MessageTransferOperation {
friend class ConnectTetheringOperationTest; friend class ConnectTetheringOperationTest;
cryptauth::RemoteDevice remote_device_; cryptauth::RemoteDevice remote_device_;
HostScanDevicePrioritizer* host_scan_device_prioritizer_; TetherHostResponseRecorder* tether_host_response_recorder_;
// These values are saved in OnMessageReceived() and returned in // These values are saved in OnMessageReceived() and returned in
// OnOperationFinished(). // OnOperationFinished().
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include "chromeos/components/tether/ble_constants.h" #include "chromeos/components/tether/ble_constants.h"
#include "chromeos/components/tether/fake_ble_connection_manager.h" #include "chromeos/components/tether/fake_ble_connection_manager.h"
#include "chromeos/components/tether/message_wrapper.h" #include "chromeos/components/tether/message_wrapper.h"
#include "chromeos/components/tether/mock_host_scan_device_prioritizer.h" #include "chromeos/components/tether/mock_tether_host_response_recorder.h"
#include "chromeos/components/tether/proto/tether.pb.h" #include "chromeos/components/tether/proto/tether.pb.h"
#include "components/cryptauth/remote_device_test_util.h" #include "components/cryptauth/remote_device_test_util.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
...@@ -111,13 +111,13 @@ class ConnectTetheringOperationTest : public testing::Test { ...@@ -111,13 +111,13 @@ class ConnectTetheringOperationTest : public testing::Test {
void SetUp() override { void SetUp() override {
fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>();
mock_host_scan_device_prioritizer_ = mock_tether_host_response_recorder_ =
base::MakeUnique<StrictMock<MockHostScanDevicePrioritizer>>(); base::MakeUnique<StrictMock<MockTetherHostResponseRecorder>>();
test_observer_ = base::WrapUnique(new TestObserver()); test_observer_ = base::WrapUnique(new TestObserver());
operation_ = base::WrapUnique(new ConnectTetheringOperation( operation_ = base::WrapUnique(new ConnectTetheringOperation(
test_device_, fake_ble_connection_manager_.get(), test_device_, fake_ble_connection_manager_.get(),
mock_host_scan_device_prioritizer_.get())); mock_tether_host_response_recorder_.get()));
operation_->AddObserver(test_observer_.get()); operation_->AddObserver(test_observer_.get());
operation_->Initialize(); operation_->Initialize();
} }
...@@ -170,8 +170,8 @@ class ConnectTetheringOperationTest : public testing::Test { ...@@ -170,8 +170,8 @@ class ConnectTetheringOperationTest : public testing::Test {
const cryptauth::RemoteDevice test_device_; const cryptauth::RemoteDevice test_device_;
std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_;
std::unique_ptr<StrictMock<MockHostScanDevicePrioritizer>> std::unique_ptr<StrictMock<MockTetherHostResponseRecorder>>
mock_host_scan_device_prioritizer_; mock_tether_host_response_recorder_;
std::unique_ptr<TestObserver> test_observer_; std::unique_ptr<TestObserver> test_observer_;
std::unique_ptr<ConnectTetheringOperation> operation_; std::unique_ptr<ConnectTetheringOperation> operation_;
...@@ -180,7 +180,7 @@ class ConnectTetheringOperationTest : public testing::Test { ...@@ -180,7 +180,7 @@ class ConnectTetheringOperationTest : public testing::Test {
}; };
TEST_F(ConnectTetheringOperationTest, TestOperation_SuccessButInvalidResponse) { TEST_F(ConnectTetheringOperationTest, TestOperation_SuccessButInvalidResponse) {
EXPECT_CALL(*mock_host_scan_device_prioritizer_, EXPECT_CALL(*mock_tether_host_response_recorder_,
RecordSuccessfulConnectTetheringResponse(_)) RecordSuccessfulConnectTetheringResponse(_))
.Times(0); .Times(0);
...@@ -192,7 +192,7 @@ TEST_F(ConnectTetheringOperationTest, TestOperation_SuccessButInvalidResponse) { ...@@ -192,7 +192,7 @@ TEST_F(ConnectTetheringOperationTest, TestOperation_SuccessButInvalidResponse) {
} }
TEST_F(ConnectTetheringOperationTest, TestOperation_SuccessWithValidResponse) { TEST_F(ConnectTetheringOperationTest, TestOperation_SuccessWithValidResponse) {
EXPECT_CALL(*mock_host_scan_device_prioritizer_, EXPECT_CALL(*mock_tether_host_response_recorder_,
RecordSuccessfulConnectTetheringResponse(test_device_)); RecordSuccessfulConnectTetheringResponse(test_device_));
SimulateDeviceAuthenticationAndVerifyMessageSent(); SimulateDeviceAuthenticationAndVerifyMessageSent();
...@@ -203,7 +203,7 @@ TEST_F(ConnectTetheringOperationTest, TestOperation_SuccessWithValidResponse) { ...@@ -203,7 +203,7 @@ TEST_F(ConnectTetheringOperationTest, TestOperation_SuccessWithValidResponse) {
} }
TEST_F(ConnectTetheringOperationTest, TestOperation_UnknownError) { TEST_F(ConnectTetheringOperationTest, TestOperation_UnknownError) {
EXPECT_CALL(*mock_host_scan_device_prioritizer_, EXPECT_CALL(*mock_tether_host_response_recorder_,
RecordSuccessfulConnectTetheringResponse(_)) RecordSuccessfulConnectTetheringResponse(_))
.Times(0); .Times(0);
...@@ -215,7 +215,7 @@ TEST_F(ConnectTetheringOperationTest, TestOperation_UnknownError) { ...@@ -215,7 +215,7 @@ TEST_F(ConnectTetheringOperationTest, TestOperation_UnknownError) {
} }
TEST_F(ConnectTetheringOperationTest, TestOperation_ProvisioningFailed) { TEST_F(ConnectTetheringOperationTest, TestOperation_ProvisioningFailed) {
EXPECT_CALL(*mock_host_scan_device_prioritizer_, EXPECT_CALL(*mock_tether_host_response_recorder_,
RecordSuccessfulConnectTetheringResponse(_)) RecordSuccessfulConnectTetheringResponse(_))
.Times(0); .Times(0);
...@@ -227,7 +227,7 @@ TEST_F(ConnectTetheringOperationTest, TestOperation_ProvisioningFailed) { ...@@ -227,7 +227,7 @@ TEST_F(ConnectTetheringOperationTest, TestOperation_ProvisioningFailed) {
} }
TEST_F(ConnectTetheringOperationTest, TestCannotConnect) { TEST_F(ConnectTetheringOperationTest, TestCannotConnect) {
EXPECT_CALL(*mock_host_scan_device_prioritizer_, EXPECT_CALL(*mock_tether_host_response_recorder_,
RecordSuccessfulConnectTetheringResponse(_)) RecordSuccessfulConnectTetheringResponse(_))
.Times(0); .Times(0);
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/values.h" #include "base/values.h"
#include "chromeos/components/tether/pref_names.h" #include "chromeos/components/tether/pref_names.h"
#include "chromeos/components/tether/tether_host_response_recorder.h"
#include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
...@@ -14,97 +15,43 @@ namespace chromeos { ...@@ -14,97 +15,43 @@ namespace chromeos {
namespace tether { namespace tether {
// static HostScanDevicePrioritizer::HostScanDevicePrioritizer(
void HostScanDevicePrioritizer::RegisterPrefs(PrefRegistrySimple* registry) { TetherHostResponseRecorder* tether_host_response_recorder)
registry->RegisterListPref(prefs::kMostRecentTetherAvailablilityResponderIds); : tether_host_response_recorder_(tether_host_response_recorder) {}
registry->RegisterStringPref(prefs::kMostRecentConnectTetheringResponderId,
"");
}
HostScanDevicePrioritizer::HostScanDevicePrioritizer(PrefService* pref_service)
: pref_service_(pref_service) {}
HostScanDevicePrioritizer::~HostScanDevicePrioritizer() {} HostScanDevicePrioritizer::~HostScanDevicePrioritizer() {}
void HostScanDevicePrioritizer::RecordSuccessfulTetherAvailabilityResponse(
const cryptauth::RemoteDevice& remote_device) {
std::string device_id = remote_device.GetDeviceId();
const base::ListValue* ids =
pref_service_->GetList(prefs::kMostRecentTetherAvailablilityResponderIds);
// Create a mutable copy of the stored IDs, or create one if it has yet to be
// stored.
std::unique_ptr<base::ListValue> updated_ids =
ids ? ids->CreateDeepCopy() : base::MakeUnique<base::ListValue>();
// Remove the device ID if it was already present in the list.
std::unique_ptr<base::Value> device_id_value =
base::MakeUnique<base::Value>(device_id);
updated_ids->Remove(*device_id_value, nullptr);
// Add the device ID to the front of the queue.
updated_ids->Insert(0, std::move(device_id_value));
// Store the updated list back in |pref_service_|.
pref_service_->Set(prefs::kMostRecentTetherAvailablilityResponderIds,
*updated_ids);
}
void HostScanDevicePrioritizer::RecordSuccessfulConnectTetheringResponse(
const cryptauth::RemoteDevice& remote_device) {
pref_service_->Set(prefs::kMostRecentConnectTetheringResponderId,
base::Value(remote_device.GetDeviceId()));
}
void HostScanDevicePrioritizer::SortByHostScanOrder( void HostScanDevicePrioritizer::SortByHostScanOrder(
std::vector<cryptauth::RemoteDevice>* remote_devices) const { std::vector<cryptauth::RemoteDevice>* remote_devices) const {
// Fetch the stored IDs associated with the devices which most recently sent // First, fetch the hosts which have previously responded.
// TetherAvailabilityResponses. std::vector<std::string> prioritized_ids =
const base::ListValue* tether_availability_ids = tether_host_response_recorder_->GetPreviouslyAvailableHostIds();
pref_service_->GetList(prefs::kMostRecentTetherAvailablilityResponderIds);
std::vector<std::string> previously_connectable_host_ids =
// Create a mutable copy of the stored IDs, or create one if it has yet to be tether_host_response_recorder_->GetPreviouslyConnectedHostIds();
// stored. if (!previously_connectable_host_ids.empty()) {
std::unique_ptr<base::ListValue> prioritized_ids = // If there is a most-recently connectable host, insert it at the front of
tether_availability_ids ? tether_availability_ids->CreateDeepCopy() // the list.
: base::MakeUnique<base::ListValue>(); prioritized_ids.insert(prioritized_ids.begin(),
previously_connectable_host_ids[0]);
// Now, fetch the ID associated with the device which most recently sent a
// ConnectTetheringRequest.
std::string connect_tethering_id =
pref_service_->GetString(prefs::kMostRecentConnectTetheringResponderId);
// If an ID exists, insert it at the front of |prioritized_ids|.
if (!connect_tethering_id.empty()) {
prioritized_ids->Insert(
0, base::MakeUnique<base::Value>(connect_tethering_id));
} }
// Iterate from the last stored ID to the first stored ID. This ensures that // Iterate from the last stored ID to the first stored ID. This ensures that
// the items at the front of the list end up in the front of the prioritized // the items at the front of the list end up in the front of the prioritized
// |remote_devices| vector. // |remote_devices| vector.
for (size_t i = prioritized_ids->GetSize(); i-- > 0;) { for (auto prioritized_it = prioritized_ids.rbegin();
base::Value* stored_id_value; prioritized_it != prioritized_ids.rend(); ++prioritized_it) {
if (!prioritized_ids->Get(i, &stored_id_value)) {
continue;
}
std::string stored_id;
if (!stored_id_value->GetAsString(&stored_id)) {
continue;
}
// Iterate through |remote_devices| to see if a device exists with a // Iterate through |remote_devices| to see if a device exists with a
// device ID of |stored_id|. If one exists, remove it from its previous // device ID of |stored_id|. If one exists, remove it from its previous
// position in the list and add it at the front instead. // position in the list and add it at the front instead.
for (auto it = remote_devices->begin(); it != remote_devices->end(); ++it) { for (auto remote_devices_it = remote_devices->begin();
if (it->GetDeviceId() != stored_id) { remote_devices_it != remote_devices->end(); ++remote_devices_it) {
if (remote_devices_it->GetDeviceId() != *prioritized_it) {
continue; continue;
} }
cryptauth::RemoteDevice device_to_move = *it; cryptauth::RemoteDevice device_to_move = *remote_devices_it;
remote_devices->erase(it); remote_devices->erase(remote_devices_it);
remote_devices->insert(remote_devices->begin(), device_to_move); remote_devices->insert(remote_devices->begin(), device_to_move);
break; break;
} }
......
...@@ -8,13 +8,12 @@ ...@@ -8,13 +8,12 @@
#include "base/macros.h" #include "base/macros.h"
#include "components/cryptauth/remote_device.h" #include "components/cryptauth/remote_device.h"
class PrefService;
class PrefRegistrySimple;
namespace chromeos { namespace chromeos {
namespace tether { namespace tether {
class TetherHostResponseRecorder;
// Prioritizes the order of devices when performing a host scan. To optimize for // Prioritizes the order of devices when performing a host scan. To optimize for
// the most common tethering operations, this class uses the following rules: // the most common tethering operations, this class uses the following rules:
// * The device which has most recently sent a successful // * The device which has most recently sent a successful
...@@ -25,32 +24,16 @@ namespace tether { ...@@ -25,32 +24,16 @@ namespace tether {
// * All other devices are left in the order they are passed. // * All other devices are left in the order they are passed.
class HostScanDevicePrioritizer { class HostScanDevicePrioritizer {
public: public:
// Note: The PrefService* passed here must be created using the same registry HostScanDevicePrioritizer(
// passed to RegisterPrefs(). TetherHostResponseRecorder* tether_host_response_recorder);
HostScanDevicePrioritizer(PrefService* pref_service);
virtual ~HostScanDevicePrioritizer(); virtual ~HostScanDevicePrioritizer();
// Registers the prefs used by this class to |registry|. Must be called before
// this class is utilized.
static void RegisterPrefs(PrefRegistrySimple* registry);
// Records a TetherAvailabilityResponse. This function should be called each
// time that a response is received from a potential host, even if a
// connection is not started.
virtual void RecordSuccessfulTetherAvailabilityResponse(
const cryptauth::RemoteDevice& remote_device);
// Records a ConnectTetheringResponse. This function should be called each
// time that a response is received from a host.
virtual void RecordSuccessfulConnectTetheringResponse(
const cryptauth::RemoteDevice& remote_device);
// Prioritizes |remote_devices| using the rules described above. // Prioritizes |remote_devices| using the rules described above.
virtual void SortByHostScanOrder( virtual void SortByHostScanOrder(
std::vector<cryptauth::RemoteDevice>* remote_devices) const; std::vector<cryptauth::RemoteDevice>* remote_devices) const;
private: private:
PrefService* pref_service_; TetherHostResponseRecorder* tether_host_response_recorder_;
DISALLOW_COPY_AND_ASSIGN(HostScanDevicePrioritizer); DISALLOW_COPY_AND_ASSIGN(HostScanDevicePrioritizer);
}; };
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "chromeos/components/tether/host_scan_device_prioritizer.h" #include "chromeos/components/tether/host_scan_device_prioritizer.h"
#include "chromeos/components/tether/tether_host_response_recorder.h"
#include "components/cryptauth/remote_device_test_util.h" #include "components/cryptauth/remote_device_test_util.h"
#include "components/prefs/testing_pref_service.h" #include "components/prefs/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -19,15 +20,19 @@ class HostScanDevicePrioritizerTest : public testing::Test { ...@@ -19,15 +20,19 @@ class HostScanDevicePrioritizerTest : public testing::Test {
void SetUp() override { void SetUp() override {
pref_service_ = base::MakeUnique<TestingPrefServiceSimple>(); pref_service_ = base::MakeUnique<TestingPrefServiceSimple>();
HostScanDevicePrioritizer::RegisterPrefs(pref_service_->registry()); TetherHostResponseRecorder::RegisterPrefs(pref_service_->registry());
prioritizer_ = recorder_ =
base::MakeUnique<HostScanDevicePrioritizer>(pref_service_.get()); base::MakeUnique<TetherHostResponseRecorder>(pref_service_.get());
prioritizer_ = base::MakeUnique<HostScanDevicePrioritizer>(recorder_.get());
} }
const std::vector<cryptauth::RemoteDevice> test_devices_; const std::vector<cryptauth::RemoteDevice> test_devices_;
std::unique_ptr<TestingPrefServiceSimple> pref_service_; std::unique_ptr<TestingPrefServiceSimple> pref_service_;
std::unique_ptr<TetherHostResponseRecorder> recorder_;
std::unique_ptr<HostScanDevicePrioritizer> prioritizer_; std::unique_ptr<HostScanDevicePrioritizer> prioritizer_;
private: private:
...@@ -36,11 +41,11 @@ class HostScanDevicePrioritizerTest : public testing::Test { ...@@ -36,11 +41,11 @@ class HostScanDevicePrioritizerTest : public testing::Test {
TEST_F(HostScanDevicePrioritizerTest, TestOnlyTetherAvailabilityResponses) { TEST_F(HostScanDevicePrioritizerTest, TestOnlyTetherAvailabilityResponses) {
// Receive TetherAvailabilityResponses from devices 0-4. // Receive TetherAvailabilityResponses from devices 0-4.
prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]); recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]);
prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]); recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]);
prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[2]); recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[2]);
prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[3]); recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[3]);
prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[4]); recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[4]);
// Do not receive a ConnectTetheringResponse. // Do not receive a ConnectTetheringResponse.
...@@ -60,14 +65,14 @@ TEST_F(HostScanDevicePrioritizerTest, TestOnlyTetherAvailabilityResponses) { ...@@ -60,14 +65,14 @@ TEST_F(HostScanDevicePrioritizerTest, TestOnlyTetherAvailabilityResponses) {
TEST_F(HostScanDevicePrioritizerTest, TestBothTypesOfResponses) { TEST_F(HostScanDevicePrioritizerTest, TestBothTypesOfResponses) {
// Receive TetherAvailabilityResponses from devices 0-4. // Receive TetherAvailabilityResponses from devices 0-4.
prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]); recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]);
prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]); recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]);
prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[2]); recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[2]);
prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[3]); recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[3]);
prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[4]); recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[4]);
// Receive ConnectTetheringResponse from device 0. // Receive ConnectTetheringResponse from device 0.
prioritizer_->RecordSuccessfulConnectTetheringResponse(test_devices_[0]); recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[0]);
std::vector<cryptauth::RemoteDevice> test_vector = std::vector<cryptauth::RemoteDevice> test_vector =
std::vector<cryptauth::RemoteDevice>{test_devices_[6], test_devices_[5], std::vector<cryptauth::RemoteDevice>{test_devices_[6], test_devices_[5],
...@@ -85,14 +90,14 @@ TEST_F(HostScanDevicePrioritizerTest, TestBothTypesOfResponses) { ...@@ -85,14 +90,14 @@ TEST_F(HostScanDevicePrioritizerTest, TestBothTypesOfResponses) {
TEST_F(HostScanDevicePrioritizerTest, TestBothTypesOfResponses_DifferentOrder) { TEST_F(HostScanDevicePrioritizerTest, TestBothTypesOfResponses_DifferentOrder) {
// Receive different order. // Receive different order.
prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]); recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]);
prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[2]); recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[2]);
prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]); recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]);
prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[4]); recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[4]);
prioritizer_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[3]); recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[3]);
// Receive ConnectTetheringResponse from device 1. // Receive ConnectTetheringResponse from device 1.
prioritizer_->RecordSuccessfulConnectTetheringResponse(test_devices_[1]); recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[1]);
std::vector<cryptauth::RemoteDevice> test_vector = std::vector<cryptauth::RemoteDevice> test_vector =
std::vector<cryptauth::RemoteDevice>{test_devices_[9], test_devices_[8], std::vector<cryptauth::RemoteDevice>{test_devices_[9], test_devices_[8],
......
...@@ -82,7 +82,13 @@ class HostScanSchedulerTest : public testing::Test { ...@@ -82,7 +82,13 @@ class HostScanSchedulerTest : public testing::Test {
class FakeHostScanner : public HostScanner { class FakeHostScanner : public HostScanner {
public: public:
FakeHostScanner() FakeHostScanner()
: HostScanner(nullptr, nullptr, nullptr, nullptr, nullptr, nullptr), : HostScanner(nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr),
num_scans_started_(0) {} num_scans_started_(0) {}
~FakeHostScanner() override {} ~FakeHostScanner() override {}
......
...@@ -36,12 +36,14 @@ HostScanner::HostScanner( ...@@ -36,12 +36,14 @@ HostScanner::HostScanner(
TetherHostFetcher* tether_host_fetcher, TetherHostFetcher* tether_host_fetcher,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer, HostScanDevicePrioritizer* host_scan_device_prioritizer,
TetherHostResponseRecorder* tether_host_response_recorder,
NetworkStateHandler* network_state_handler, NetworkStateHandler* network_state_handler,
NotificationPresenter* notification_presenter, NotificationPresenter* notification_presenter,
DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map) DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map)
: tether_host_fetcher_(tether_host_fetcher), : tether_host_fetcher_(tether_host_fetcher),
connection_manager_(connection_manager), connection_manager_(connection_manager),
host_scan_device_prioritizer_(host_scan_device_prioritizer), host_scan_device_prioritizer_(host_scan_device_prioritizer),
tether_host_response_recorder_(tether_host_response_recorder),
network_state_handler_(network_state_handler), network_state_handler_(network_state_handler),
notification_presenter_(notification_presenter), notification_presenter_(notification_presenter),
device_id_tether_network_guid_map_(device_id_tether_network_guid_map), device_id_tether_network_guid_map_(device_id_tether_network_guid_map),
...@@ -75,7 +77,8 @@ void HostScanner::OnTetherHostsFetched( ...@@ -75,7 +77,8 @@ void HostScanner::OnTetherHostsFetched(
is_fetching_hosts_ = false; is_fetching_hosts_ = false;
host_scanner_operation_ = HostScannerOperation::Factory::NewInstance( host_scanner_operation_ = HostScannerOperation::Factory::NewInstance(
tether_hosts, connection_manager_, host_scan_device_prioritizer_); tether_hosts, connection_manager_, host_scan_device_prioritizer_,
tether_host_response_recorder_);
host_scanner_operation_->AddObserver(this); host_scanner_operation_->AddObserver(this);
host_scanner_operation_->Initialize(); host_scanner_operation_->Initialize();
} }
...@@ -115,6 +118,7 @@ void HostScanner::OnTetherAvailabilityResponse( ...@@ -115,6 +118,7 @@ void HostScanner::OnTetherAvailabilityResponse(
status.connection_strength()) status.connection_strength())
: 100; : 100;
// TODO(khorimoto): Pass a HasConnectedToHost parameter to this function.
network_state_handler_->AddTetherNetworkState( network_state_handler_->AddTetherNetworkState(
device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId(
remote_device.GetDeviceId()), remote_device.GetDeviceId()),
......
...@@ -21,6 +21,7 @@ class BleConnectionManager; ...@@ -21,6 +21,7 @@ class BleConnectionManager;
class DeviceIdTetherNetworkGuidMap; class DeviceIdTetherNetworkGuidMap;
class HostScanDevicePrioritizer; class HostScanDevicePrioritizer;
class TetherHostFetcher; class TetherHostFetcher;
class TetherHostResponseRecorder;
// Scans for nearby tether hosts. // Scans for nearby tether hosts.
// TODO(khorimoto): Add some sort of "staleness" timeout which removes scan // TODO(khorimoto): Add some sort of "staleness" timeout which removes scan
...@@ -31,6 +32,7 @@ class HostScanner : public HostScannerOperation::Observer { ...@@ -31,6 +32,7 @@ class HostScanner : public HostScannerOperation::Observer {
HostScanner(TetherHostFetcher* tether_host_fetcher, HostScanner(TetherHostFetcher* tether_host_fetcher,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer, HostScanDevicePrioritizer* host_scan_device_prioritizer,
TetherHostResponseRecorder* tether_host_response_recorder,
NetworkStateHandler* network_state_handler, NetworkStateHandler* network_state_handler,
NotificationPresenter* notification_presenter, NotificationPresenter* notification_presenter,
DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map); DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map);
...@@ -62,6 +64,7 @@ class HostScanner : public HostScannerOperation::Observer { ...@@ -62,6 +64,7 @@ class HostScanner : public HostScannerOperation::Observer {
TetherHostFetcher* tether_host_fetcher_; TetherHostFetcher* tether_host_fetcher_;
BleConnectionManager* connection_manager_; BleConnectionManager* connection_manager_;
HostScanDevicePrioritizer* host_scan_device_prioritizer_; HostScanDevicePrioritizer* host_scan_device_prioritizer_;
TetherHostResponseRecorder* tether_host_response_recorder_;
NetworkStateHandler* network_state_handler_; NetworkStateHandler* network_state_handler_;
NotificationPresenter* notification_presenter_; NotificationPresenter* notification_presenter_;
DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map_; DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map_;
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "chromeos/components/tether/host_scan_device_prioritizer.h" #include "chromeos/components/tether/host_scan_device_prioritizer.h"
#include "chromeos/components/tether/message_wrapper.h" #include "chromeos/components/tether/message_wrapper.h"
#include "chromeos/components/tether/proto/tether.pb.h" #include "chromeos/components/tether/proto/tether.pb.h"
#include "chromeos/components/tether/tether_host_response_recorder.h"
#include "components/proximity_auth/logging/logging.h" #include "components/proximity_auth/logging/logging.h"
namespace chromeos { namespace chromeos {
...@@ -64,12 +65,14 @@ std::unique_ptr<HostScannerOperation> ...@@ -64,12 +65,14 @@ std::unique_ptr<HostScannerOperation>
HostScannerOperation::Factory::NewInstance( HostScannerOperation::Factory::NewInstance(
const std::vector<cryptauth::RemoteDevice>& devices_to_connect, const std::vector<cryptauth::RemoteDevice>& devices_to_connect,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer) { HostScanDevicePrioritizer* host_scan_device_prioritizer,
TetherHostResponseRecorder* tether_host_response_recorder) {
if (!factory_instance_) { if (!factory_instance_) {
factory_instance_ = new Factory(); factory_instance_ = new Factory();
} }
return factory_instance_->BuildInstance( return factory_instance_->BuildInstance(
devices_to_connect, connection_manager, host_scan_device_prioritizer); devices_to_connect, connection_manager, host_scan_device_prioritizer,
tether_host_response_recorder);
} }
// static // static
...@@ -81,9 +84,11 @@ std::unique_ptr<HostScannerOperation> ...@@ -81,9 +84,11 @@ std::unique_ptr<HostScannerOperation>
HostScannerOperation::Factory::BuildInstance( HostScannerOperation::Factory::BuildInstance(
const std::vector<cryptauth::RemoteDevice>& devices_to_connect, const std::vector<cryptauth::RemoteDevice>& devices_to_connect,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer) { HostScanDevicePrioritizer* host_scan_device_prioritizer,
TetherHostResponseRecorder* tether_host_response_recorder) {
return base::MakeUnique<HostScannerOperation>( return base::MakeUnique<HostScannerOperation>(
devices_to_connect, connection_manager, host_scan_device_prioritizer); devices_to_connect, connection_manager, host_scan_device_prioritizer,
tether_host_response_recorder);
} }
HostScannerOperation::ScannedDeviceInfo::ScannedDeviceInfo( HostScannerOperation::ScannedDeviceInfo::ScannedDeviceInfo(
...@@ -107,11 +112,12 @@ bool operator==(const HostScannerOperation::ScannedDeviceInfo& first, ...@@ -107,11 +112,12 @@ bool operator==(const HostScannerOperation::ScannedDeviceInfo& first,
HostScannerOperation::HostScannerOperation( HostScannerOperation::HostScannerOperation(
const std::vector<cryptauth::RemoteDevice>& devices_to_connect, const std::vector<cryptauth::RemoteDevice>& devices_to_connect,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer) HostScanDevicePrioritizer* host_scan_device_prioritizer,
TetherHostResponseRecorder* tether_host_response_recorder)
: MessageTransferOperation( : MessageTransferOperation(
PrioritizeDevices(devices_to_connect, host_scan_device_prioritizer), PrioritizeDevices(devices_to_connect, host_scan_device_prioritizer),
connection_manager), connection_manager),
host_scan_device_prioritizer_(host_scan_device_prioritizer) {} tether_host_response_recorder_(tether_host_response_recorder) {}
HostScannerOperation::~HostScannerOperation() {} HostScannerOperation::~HostScannerOperation() {}
...@@ -166,7 +172,7 @@ void HostScannerOperation::OnMessageReceived( ...@@ -166,7 +172,7 @@ void HostScannerOperation::OnMessageReceived(
<< "indicates that tethering is available. set_up_required = " << "indicates that tethering is available. set_up_required = "
<< set_up_required; << set_up_required;
host_scan_device_prioritizer_->RecordSuccessfulTetherAvailabilityResponse( tether_host_response_recorder_->RecordSuccessfulTetherAvailabilityResponse(
remote_device); remote_device);
scanned_device_list_so_far_.push_back(ScannedDeviceInfo( scanned_device_list_so_far_.push_back(ScannedDeviceInfo(
......
...@@ -20,6 +20,7 @@ namespace tether { ...@@ -20,6 +20,7 @@ namespace tether {
class HostScanDevicePrioritizer; class HostScanDevicePrioritizer;
class MessageWrapper; class MessageWrapper;
class TetherHostResponseRecorder;
// Operation used to perform a host scan. Attempts to connect to each of the // Operation used to perform a host scan. Attempts to connect to each of the
// devices passed and sends a TetherAvailabilityRequest to each connected device // devices passed and sends a TetherAvailabilityRequest to each connected device
...@@ -35,7 +36,8 @@ class HostScannerOperation : public MessageTransferOperation { ...@@ -35,7 +36,8 @@ class HostScannerOperation : public MessageTransferOperation {
static std::unique_ptr<HostScannerOperation> NewInstance( static std::unique_ptr<HostScannerOperation> NewInstance(
const std::vector<cryptauth::RemoteDevice>& devices_to_connect, const std::vector<cryptauth::RemoteDevice>& devices_to_connect,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer); HostScanDevicePrioritizer* host_scan_device_prioritizer,
TetherHostResponseRecorder* tether_host_response_recorder);
static void SetInstanceForTesting(Factory* factory); static void SetInstanceForTesting(Factory* factory);
...@@ -43,7 +45,8 @@ class HostScannerOperation : public MessageTransferOperation { ...@@ -43,7 +45,8 @@ class HostScannerOperation : public MessageTransferOperation {
virtual std::unique_ptr<HostScannerOperation> BuildInstance( virtual std::unique_ptr<HostScannerOperation> BuildInstance(
const std::vector<cryptauth::RemoteDevice>& devices_to_connect, const std::vector<cryptauth::RemoteDevice>& devices_to_connect,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer); HostScanDevicePrioritizer* host_scan_device_prioritizer,
TetherHostResponseRecorder* tether_host_response_recorder);
private: private:
static Factory* factory_instance_; static Factory* factory_instance_;
...@@ -77,7 +80,8 @@ class HostScannerOperation : public MessageTransferOperation { ...@@ -77,7 +80,8 @@ class HostScannerOperation : public MessageTransferOperation {
HostScannerOperation( HostScannerOperation(
const std::vector<cryptauth::RemoteDevice>& devices_to_connect, const std::vector<cryptauth::RemoteDevice>& devices_to_connect,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer); HostScanDevicePrioritizer* host_scan_device_prioritizer,
TetherHostResponseRecorder* tether_host_response_recorder);
~HostScannerOperation() override; ~HostScannerOperation() override;
void AddObserver(Observer* observer); void AddObserver(Observer* observer);
...@@ -100,7 +104,7 @@ class HostScannerOperation : public MessageTransferOperation { ...@@ -100,7 +104,7 @@ class HostScannerOperation : public MessageTransferOperation {
private: private:
friend class HostScannerOperationTest; friend class HostScannerOperationTest;
HostScanDevicePrioritizer* host_scan_device_prioritizer_; TetherHostResponseRecorder* tether_host_response_recorder_;
base::ObserverList<Observer> observer_list_; base::ObserverList<Observer> observer_list_;
DISALLOW_COPY_AND_ASSIGN(HostScannerOperation); DISALLOW_COPY_AND_ASSIGN(HostScannerOperation);
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "chromeos/components/tether/fake_ble_connection_manager.h" #include "chromeos/components/tether/fake_ble_connection_manager.h"
#include "chromeos/components/tether/message_wrapper.h" #include "chromeos/components/tether/message_wrapper.h"
#include "chromeos/components/tether/mock_host_scan_device_prioritizer.h" #include "chromeos/components/tether/mock_host_scan_device_prioritizer.h"
#include "chromeos/components/tether/mock_tether_host_response_recorder.h"
#include "chromeos/components/tether/proto/tether.pb.h" #include "chromeos/components/tether/proto/tether.pb.h"
#include "components/cryptauth/remote_device_test_util.h" #include "components/cryptauth/remote_device_test_util.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -117,6 +118,8 @@ class HostScannerOperationTest : public testing::Test { ...@@ -117,6 +118,8 @@ class HostScannerOperationTest : public testing::Test {
fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>();
test_host_scan_device_prioritizer_ = test_host_scan_device_prioritizer_ =
base::MakeUnique<StrictMock<TestHostScanDevicePrioritizer>>(); base::MakeUnique<StrictMock<TestHostScanDevicePrioritizer>>();
mock_tether_host_response_recorder_ =
base::MakeUnique<StrictMock<MockTetherHostResponseRecorder>>();
test_observer_ = base::WrapUnique(new TestObserver()); test_observer_ = base::WrapUnique(new TestObserver());
} }
...@@ -124,7 +127,8 @@ class HostScannerOperationTest : public testing::Test { ...@@ -124,7 +127,8 @@ class HostScannerOperationTest : public testing::Test {
const std::vector<cryptauth::RemoteDevice>& remote_devices) { const std::vector<cryptauth::RemoteDevice>& remote_devices) {
operation_ = base::WrapUnique(new HostScannerOperation( operation_ = base::WrapUnique(new HostScannerOperation(
remote_devices, fake_ble_connection_manager_.get(), remote_devices, fake_ble_connection_manager_.get(),
test_host_scan_device_prioritizer_.get())); test_host_scan_device_prioritizer_.get(),
mock_tether_host_response_recorder_.get()));
operation_->AddObserver(test_observer_.get()); operation_->AddObserver(test_observer_.get());
// Verify that the devices have been correctly prioritized. // Verify that the devices have been correctly prioritized.
...@@ -210,6 +214,8 @@ class HostScannerOperationTest : public testing::Test { ...@@ -210,6 +214,8 @@ class HostScannerOperationTest : public testing::Test {
std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_;
std::unique_ptr<StrictMock<TestHostScanDevicePrioritizer>> std::unique_ptr<StrictMock<TestHostScanDevicePrioritizer>>
test_host_scan_device_prioritizer_; test_host_scan_device_prioritizer_;
std::unique_ptr<StrictMock<MockTetherHostResponseRecorder>>
mock_tether_host_response_recorder_;
std::unique_ptr<TestObserver> test_observer_; std::unique_ptr<TestObserver> test_observer_;
std::unique_ptr<HostScannerOperation> operation_; std::unique_ptr<HostScannerOperation> operation_;
...@@ -223,7 +229,7 @@ TEST_F(HostScannerOperationTest, TestDevicesArePrioritizedDuringConstruction) { ...@@ -223,7 +229,7 @@ TEST_F(HostScannerOperationTest, TestDevicesArePrioritizedDuringConstruction) {
} }
TEST_F(HostScannerOperationTest, TestOperation_OneDevice_UnknownError) { TEST_F(HostScannerOperationTest, TestOperation_OneDevice_UnknownError) {
EXPECT_CALL(*test_host_scan_device_prioritizer_, EXPECT_CALL(*mock_tether_host_response_recorder_,
RecordSuccessfulTetherAvailabilityResponse(_)) RecordSuccessfulTetherAvailabilityResponse(_))
.Times(0); .Times(0);
...@@ -233,7 +239,7 @@ TEST_F(HostScannerOperationTest, TestOperation_OneDevice_UnknownError) { ...@@ -233,7 +239,7 @@ TEST_F(HostScannerOperationTest, TestOperation_OneDevice_UnknownError) {
} }
TEST_F(HostScannerOperationTest, TestOperation_OneDevice_TetherAvailable) { TEST_F(HostScannerOperationTest, TestOperation_OneDevice_TetherAvailable) {
EXPECT_CALL(*test_host_scan_device_prioritizer_, EXPECT_CALL(*mock_tether_host_response_recorder_,
RecordSuccessfulTetherAvailabilityResponse(test_devices_[0])); RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]));
TestOperationWithOneDevice( TestOperationWithOneDevice(
...@@ -242,7 +248,7 @@ TEST_F(HostScannerOperationTest, TestOperation_OneDevice_TetherAvailable) { ...@@ -242,7 +248,7 @@ TEST_F(HostScannerOperationTest, TestOperation_OneDevice_TetherAvailable) {
} }
TEST_F(HostScannerOperationTest, TestOperation_OneDevice_SetupNeeded) { TEST_F(HostScannerOperationTest, TestOperation_OneDevice_SetupNeeded) {
EXPECT_CALL(*test_host_scan_device_prioritizer_, EXPECT_CALL(*mock_tether_host_response_recorder_,
RecordSuccessfulTetherAvailabilityResponse(test_devices_[0])); RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]));
TestOperationWithOneDevice( TestOperationWithOneDevice(
...@@ -251,7 +257,7 @@ TEST_F(HostScannerOperationTest, TestOperation_OneDevice_SetupNeeded) { ...@@ -251,7 +257,7 @@ TEST_F(HostScannerOperationTest, TestOperation_OneDevice_SetupNeeded) {
} }
TEST_F(HostScannerOperationTest, TestOperation_OneDevice_NoReception) { TEST_F(HostScannerOperationTest, TestOperation_OneDevice_NoReception) {
EXPECT_CALL(*test_host_scan_device_prioritizer_, EXPECT_CALL(*mock_tether_host_response_recorder_,
RecordSuccessfulTetherAvailabilityResponse(_)) RecordSuccessfulTetherAvailabilityResponse(_))
.Times(0); .Times(0);
...@@ -261,7 +267,7 @@ TEST_F(HostScannerOperationTest, TestOperation_OneDevice_NoReception) { ...@@ -261,7 +267,7 @@ TEST_F(HostScannerOperationTest, TestOperation_OneDevice_NoReception) {
} }
TEST_F(HostScannerOperationTest, TestOperation_OneDevice_NoSimCard) { TEST_F(HostScannerOperationTest, TestOperation_OneDevice_NoSimCard) {
EXPECT_CALL(*test_host_scan_device_prioritizer_, EXPECT_CALL(*mock_tether_host_response_recorder_,
RecordSuccessfulTetherAvailabilityResponse(_)) RecordSuccessfulTetherAvailabilityResponse(_))
.Times(0); .Times(0);
...@@ -272,7 +278,7 @@ TEST_F(HostScannerOperationTest, TestOperation_OneDevice_NoSimCard) { ...@@ -272,7 +278,7 @@ TEST_F(HostScannerOperationTest, TestOperation_OneDevice_NoSimCard) {
TEST_F(HostScannerOperationTest, TEST_F(HostScannerOperationTest,
TestOperation_OneDevice_NotificationsDisabled) { TestOperation_OneDevice_NotificationsDisabled) {
EXPECT_CALL(*test_host_scan_device_prioritizer_, EXPECT_CALL(*mock_tether_host_response_recorder_,
RecordSuccessfulTetherAvailabilityResponse(_)) RecordSuccessfulTetherAvailabilityResponse(_))
.Times(0); .Times(0);
...@@ -282,17 +288,17 @@ TEST_F(HostScannerOperationTest, ...@@ -282,17 +288,17 @@ TEST_F(HostScannerOperationTest,
} }
TEST_F(HostScannerOperationTest, TestMultipleDevices) { TEST_F(HostScannerOperationTest, TestMultipleDevices) {
EXPECT_CALL(*test_host_scan_device_prioritizer_, EXPECT_CALL(*mock_tether_host_response_recorder_,
RecordSuccessfulTetherAvailabilityResponse(test_devices_[0])); RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]));
EXPECT_CALL(*test_host_scan_device_prioritizer_, EXPECT_CALL(*mock_tether_host_response_recorder_,
RecordSuccessfulTetherAvailabilityResponse(test_devices_[1])) RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]))
.Times(0); .Times(0);
EXPECT_CALL(*test_host_scan_device_prioritizer_, EXPECT_CALL(*mock_tether_host_response_recorder_,
RecordSuccessfulTetherAvailabilityResponse(test_devices_[2])); RecordSuccessfulTetherAvailabilityResponse(test_devices_[2]));
EXPECT_CALL(*test_host_scan_device_prioritizer_, EXPECT_CALL(*mock_tether_host_response_recorder_,
RecordSuccessfulTetherAvailabilityResponse(test_devices_[3])) RecordSuccessfulTetherAvailabilityResponse(test_devices_[3]))
.Times(0); .Times(0);
EXPECT_CALL(*test_host_scan_device_prioritizer_, EXPECT_CALL(*mock_tether_host_response_recorder_,
RecordSuccessfulTetherAvailabilityResponse(test_devices_[4])) RecordSuccessfulTetherAvailabilityResponse(test_devices_[4]))
.Times(0); .Times(0);
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "chromeos/components/tether/fake_tether_host_fetcher.h" #include "chromeos/components/tether/fake_tether_host_fetcher.h"
#include "chromeos/components/tether/host_scan_device_prioritizer.h" #include "chromeos/components/tether/host_scan_device_prioritizer.h"
#include "chromeos/components/tether/host_scanner.h" #include "chromeos/components/tether/host_scanner.h"
#include "chromeos/components/tether/mock_tether_host_response_recorder.h"
#include "chromeos/dbus/dbus_thread_manager.h" #include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/network/network_state.h" #include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h" #include "chromeos/network/network_state_handler.h"
...@@ -46,10 +47,12 @@ class FakeHostScannerOperation : public HostScannerOperation { ...@@ -46,10 +47,12 @@ class FakeHostScannerOperation : public HostScannerOperation {
FakeHostScannerOperation( FakeHostScannerOperation(
const std::vector<cryptauth::RemoteDevice>& devices_to_connect, const std::vector<cryptauth::RemoteDevice>& devices_to_connect,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer) HostScanDevicePrioritizer* host_scan_device_prioritizer,
TetherHostResponseRecorder* tether_host_response_recorder)
: HostScannerOperation(devices_to_connect, : HostScannerOperation(devices_to_connect,
connection_manager, connection_manager,
host_scan_device_prioritizer) {} host_scan_device_prioritizer,
tether_host_response_recorder) {}
~FakeHostScannerOperation() override {} ~FakeHostScannerOperation() override {}
...@@ -78,10 +81,12 @@ class FakeHostScannerOperationFactory : public HostScannerOperation::Factory { ...@@ -78,10 +81,12 @@ class FakeHostScannerOperationFactory : public HostScannerOperation::Factory {
std::unique_ptr<HostScannerOperation> BuildInstance( std::unique_ptr<HostScannerOperation> BuildInstance(
const std::vector<cryptauth::RemoteDevice>& devices_to_connect, const std::vector<cryptauth::RemoteDevice>& devices_to_connect,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer) override { HostScanDevicePrioritizer* host_scan_device_prioritizer,
TetherHostResponseRecorder* tether_host_response_recorder) override {
EXPECT_EQ(expected_devices_, devices_to_connect); EXPECT_EQ(expected_devices_, devices_to_connect);
FakeHostScannerOperation* operation = new FakeHostScannerOperation( FakeHostScannerOperation* operation = new FakeHostScannerOperation(
devices_to_connect, connection_manager, host_scan_device_prioritizer); devices_to_connect, connection_manager, host_scan_device_prioritizer,
tether_host_response_recorder);
created_operations_.push_back(operation); created_operations_.push_back(operation);
return base::WrapUnique(operation); return base::WrapUnique(operation);
} }
...@@ -149,6 +154,8 @@ class HostScannerTest : public NetworkStateTest { ...@@ -149,6 +154,8 @@ class HostScannerTest : public NetworkStateTest {
fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>();
fake_host_scan_device_prioritizer_ = fake_host_scan_device_prioritizer_ =
base::MakeUnique<FakeHostScanDevicePrioritizer>(); base::MakeUnique<FakeHostScanDevicePrioritizer>();
mock_tether_host_response_recorder_ =
base::MakeUnique<MockTetherHostResponseRecorder>();
fake_notification_presenter_ = fake_notification_presenter_ =
base::MakeUnique<FakeNotificationPresenter>(); base::MakeUnique<FakeNotificationPresenter>();
device_id_tether_network_guid_map_ = device_id_tether_network_guid_map_ =
...@@ -161,7 +168,8 @@ class HostScannerTest : public NetworkStateTest { ...@@ -161,7 +168,8 @@ class HostScannerTest : public NetworkStateTest {
host_scanner_ = base::WrapUnique(new HostScanner( host_scanner_ = base::WrapUnique(new HostScanner(
fake_tether_host_fetcher_.get(), fake_ble_connection_manager_.get(), fake_tether_host_fetcher_.get(), fake_ble_connection_manager_.get(),
fake_host_scan_device_prioritizer_.get(), network_state_handler(), fake_host_scan_device_prioritizer_.get(),
mock_tether_host_response_recorder_.get(), network_state_handler(),
fake_notification_presenter_.get(), fake_notification_presenter_.get(),
device_id_tether_network_guid_map_.get())); device_id_tether_network_guid_map_.get()));
} }
...@@ -221,6 +229,8 @@ class HostScannerTest : public NetworkStateTest { ...@@ -221,6 +229,8 @@ class HostScannerTest : public NetworkStateTest {
std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_; std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_;
std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_;
std::unique_ptr<HostScanDevicePrioritizer> fake_host_scan_device_prioritizer_; std::unique_ptr<HostScanDevicePrioritizer> fake_host_scan_device_prioritizer_;
std::unique_ptr<MockTetherHostResponseRecorder>
mock_tether_host_response_recorder_;
std::unique_ptr<FakeNotificationPresenter> fake_notification_presenter_; std::unique_ptr<FakeNotificationPresenter> fake_notification_presenter_;
// TODO(hansberry): Use a fake for this when a real mapping scheme is created. // TODO(hansberry): Use a fake for this when a real mapping scheme is created.
std::unique_ptr<DeviceIdTetherNetworkGuidMap> std::unique_ptr<DeviceIdTetherNetworkGuidMap>
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "chromeos/components/tether/tether_connector.h" #include "chromeos/components/tether/tether_connector.h"
#include "chromeos/components/tether/tether_device_state_manager.h" #include "chromeos/components/tether/tether_device_state_manager.h"
#include "chromeos/components/tether/tether_host_fetcher.h" #include "chromeos/components/tether/tether_host_fetcher.h"
#include "chromeos/components/tether/tether_host_response_recorder.h"
#include "chromeos/components/tether/tether_network_disconnection_handler.h" #include "chromeos/components/tether/tether_network_disconnection_handler.h"
#include "chromeos/components/tether/wifi_hotspot_connector.h" #include "chromeos/components/tether/wifi_hotspot_connector.h"
#include "chromeos/network/managed_network_configuration_handler.h" #include "chromeos/network/managed_network_configuration_handler.h"
...@@ -90,7 +91,7 @@ void Initializer::Shutdown() { ...@@ -90,7 +91,7 @@ void Initializer::Shutdown() {
// static // static
void Initializer::RegisterProfilePrefs(PrefRegistrySimple* registry) { void Initializer::RegisterProfilePrefs(PrefRegistrySimple* registry) {
ActiveHost::RegisterPrefs(registry); ActiveHost::RegisterPrefs(registry);
HostScanDevicePrioritizer::RegisterPrefs(registry); TetherHostResponseRecorder::RegisterPrefs(registry);
} }
Initializer::Initializer( Initializer::Initializer(
...@@ -179,8 +180,10 @@ void Initializer::OnBluetoothAdapterAdvertisingIntervalSet( ...@@ -179,8 +180,10 @@ void Initializer::OnBluetoothAdapterAdvertisingIntervalSet(
cryptauth_service_, adapter, local_device_data_provider_.get(), cryptauth_service_, adapter, local_device_data_provider_.get(),
remote_beacon_seed_fetcher_.get(), remote_beacon_seed_fetcher_.get(),
cryptauth::BluetoothThrottlerImpl::GetInstance()); cryptauth::BluetoothThrottlerImpl::GetInstance());
host_scan_device_prioritizer_ = tether_host_response_recorder_ =
base::MakeUnique<HostScanDevicePrioritizer>(pref_service_); base::MakeUnique<TetherHostResponseRecorder>(pref_service_);
host_scan_device_prioritizer_ = base::MakeUnique<HostScanDevicePrioritizer>(
tether_host_response_recorder_.get());
wifi_hotspot_connector_ = base::MakeUnique<WifiHotspotConnector>( wifi_hotspot_connector_ = base::MakeUnique<WifiHotspotConnector>(
network_state_handler_, network_connect_); network_state_handler_, network_connect_);
active_host_ = active_host_ =
...@@ -194,7 +197,7 @@ void Initializer::OnBluetoothAdapterAdvertisingIntervalSet( ...@@ -194,7 +197,7 @@ void Initializer::OnBluetoothAdapterAdvertisingIntervalSet(
network_connection_handler_, network_state_handler_, network_connection_handler_, network_state_handler_,
wifi_hotspot_connector_.get(), active_host_.get(), wifi_hotspot_connector_.get(), active_host_.get(),
tether_host_fetcher_.get(), ble_connection_manager_.get(), tether_host_fetcher_.get(), ble_connection_manager_.get(),
host_scan_device_prioritizer_.get(), tether_host_response_recorder_.get(),
device_id_tether_network_guid_map_.get()); device_id_tether_network_guid_map_.get());
network_configuration_remover_ = network_configuration_remover_ =
base::MakeUnique<NetworkConfigurationRemover>( base::MakeUnique<NetworkConfigurationRemover>(
...@@ -205,8 +208,9 @@ void Initializer::OnBluetoothAdapterAdvertisingIntervalSet( ...@@ -205,8 +208,9 @@ void Initializer::OnBluetoothAdapterAdvertisingIntervalSet(
network_configuration_remover_.get()); network_configuration_remover_.get());
host_scanner_ = base::MakeUnique<HostScanner>( host_scanner_ = base::MakeUnique<HostScanner>(
tether_host_fetcher_.get(), ble_connection_manager_.get(), tether_host_fetcher_.get(), ble_connection_manager_.get(),
host_scan_device_prioritizer_.get(), network_state_handler_, host_scan_device_prioritizer_.get(), tether_host_response_recorder_.get(),
notification_presenter_.get(), device_id_tether_network_guid_map_.get()); network_state_handler_, notification_presenter_.get(),
device_id_tether_network_guid_map_.get());
// TODO(khorimoto): Hook up HostScanScheduler. Currently, we simply start a // TODO(khorimoto): Hook up HostScanScheduler. Currently, we simply start a
// new scan once the user logs in. // new scan once the user logs in.
......
...@@ -44,6 +44,7 @@ class NotificationPresenter; ...@@ -44,6 +44,7 @@ class NotificationPresenter;
class TetherConnector; class TetherConnector;
class TetherDeviceStateManager; class TetherDeviceStateManager;
class TetherHostFetcher; class TetherHostFetcher;
class TetherHostResponseRecorder;
class TetherNetworkDisconnectionHandler; class TetherNetworkDisconnectionHandler;
class WifiHotspotConnector; class WifiHotspotConnector;
...@@ -112,6 +113,7 @@ class Initializer : public OAuth2TokenService::Observer { ...@@ -112,6 +113,7 @@ class Initializer : public OAuth2TokenService::Observer {
std::unique_ptr<cryptauth::RemoteBeaconSeedFetcher> std::unique_ptr<cryptauth::RemoteBeaconSeedFetcher>
remote_beacon_seed_fetcher_; remote_beacon_seed_fetcher_;
std::unique_ptr<BleConnectionManager> ble_connection_manager_; std::unique_ptr<BleConnectionManager> ble_connection_manager_;
std::unique_ptr<TetherHostResponseRecorder> tether_host_response_recorder_;
std::unique_ptr<HostScanDevicePrioritizer> host_scan_device_prioritizer_; std::unique_ptr<HostScanDevicePrioritizer> host_scan_device_prioritizer_;
std::unique_ptr<WifiHotspotConnector> wifi_hotspot_connector_; std::unique_ptr<WifiHotspotConnector> wifi_hotspot_connector_;
std::unique_ptr<ActiveHost> active_host_; std::unique_ptr<ActiveHost> active_host_;
......
...@@ -22,10 +22,6 @@ class MockHostScanDevicePrioritizer : public HostScanDevicePrioritizer { ...@@ -22,10 +22,6 @@ class MockHostScanDevicePrioritizer : public HostScanDevicePrioritizer {
MockHostScanDevicePrioritizer(); MockHostScanDevicePrioritizer();
~MockHostScanDevicePrioritizer() override; ~MockHostScanDevicePrioritizer() override;
MOCK_METHOD1(RecordSuccessfulTetherAvailabilityResponse,
void(const cryptauth::RemoteDevice&));
MOCK_METHOD1(RecordSuccessfulConnectTetheringResponse,
void(const cryptauth::RemoteDevice&));
MOCK_CONST_METHOD1(SortByHostScanOrder, MOCK_CONST_METHOD1(SortByHostScanOrder,
void(std::vector<cryptauth::RemoteDevice>*)); void(std::vector<cryptauth::RemoteDevice>*));
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/tether/mock_tether_host_response_recorder.h"
namespace chromeos {
namespace tether {
MockTetherHostResponseRecorder::MockTetherHostResponseRecorder()
: TetherHostResponseRecorder(nullptr) {}
MockTetherHostResponseRecorder::~MockTetherHostResponseRecorder() {}
} // namespace tether
} // namespace chromeos
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_COMPONENTS_TETHER_MOCK_TETHER_HOST_RESPONSE_RECORDER_H_
#define CHROMEOS_COMPONENTS_TETHER_MOCK_TETHER_HOST_RESPONSE_RECORDER_H_
#include <vector>
#include "base/macros.h"
#include "chromeos/components/tether/tether_host_response_recorder.h"
#include "components/cryptauth/remote_device.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace chromeos {
namespace tether {
// Test double for TetherHostResponseRecorder.
class MockTetherHostResponseRecorder : public TetherHostResponseRecorder {
public:
MockTetherHostResponseRecorder();
~MockTetherHostResponseRecorder() override;
MOCK_METHOD1(RecordSuccessfulTetherAvailabilityResponse,
void(const cryptauth::RemoteDevice&));
MOCK_METHOD1(RecordSuccessfulConnectTetheringResponse,
void(const cryptauth::RemoteDevice&));
MOCK_CONST_METHOD0(GetPreviouslyAvailableHostIds, std::vector<std::string>());
MOCK_CONST_METHOD0(GetPreviouslyConnectedHostIds, std::vector<std::string>());
private:
DISALLOW_COPY_AND_ASSIGN(MockTetherHostResponseRecorder);
};
} // namespace tether
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_TETHER_MOCK_TETHER_HOST_RESPONSE_RECORDER_H_
...@@ -13,7 +13,7 @@ namespace prefs { ...@@ -13,7 +13,7 @@ namespace prefs {
const char kMostRecentTetherAvailablilityResponderIds[] = const char kMostRecentTetherAvailablilityResponderIds[] =
"tether.most_recent_tether_availability_responder_ids"; "tether.most_recent_tether_availability_responder_ids";
const char kMostRecentConnectTetheringResponderId[] = const char kMostRecentConnectTetheringResponderIds[] =
"tether.most_recent_connect_tethering_responder_ids"; "tether.most_recent_connect_tethering_responder_ids";
const char kActiveHostStatus[] = "tether.active_host_status"; const char kActiveHostStatus[] = "tether.active_host_status";
......
...@@ -17,10 +17,11 @@ namespace prefs { ...@@ -17,10 +17,11 @@ namespace prefs {
// ListValue, with the most recent response residing at the start of the list. // ListValue, with the most recent response residing at the start of the list.
extern const char kMostRecentTetherAvailablilityResponderIds[]; extern const char kMostRecentTetherAvailablilityResponderIds[];
// Preference name for the preference which stores the ID corresponding to // Preference name for the preference which stores IDs corresponding to devices
// the device which has most recently replied to a ConnectTetheringRequest with // which have most recently replied to a ConnectTetheringResponse with a
// a response code indicating that its hotspot has started up successfully. // response code indicating that tethering is available. The value stored is a
extern const char kMostRecentConnectTetheringResponderId[]; // ListValue, with the most recent response residing at the start of the list.
extern const char kMostRecentConnectTetheringResponderIds[];
// The status of the active host. The value stored for this key is the integer // The status of the active host. The value stored for this key is the integer
// version of an ActiveHost::ActiveHostStatus enumeration value. // version of an ActiveHost::ActiveHostStatus enumeration value.
......
...@@ -24,7 +24,7 @@ TetherConnector::TetherConnector( ...@@ -24,7 +24,7 @@ TetherConnector::TetherConnector(
ActiveHost* active_host, ActiveHost* active_host,
TetherHostFetcher* tether_host_fetcher, TetherHostFetcher* tether_host_fetcher,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer, TetherHostResponseRecorder* tether_host_response_recorder,
DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map) DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map)
: network_connection_handler_(network_connection_handler), : network_connection_handler_(network_connection_handler),
network_state_handler_(network_state_handler), network_state_handler_(network_state_handler),
...@@ -32,7 +32,7 @@ TetherConnector::TetherConnector( ...@@ -32,7 +32,7 @@ TetherConnector::TetherConnector(
active_host_(active_host), active_host_(active_host),
tether_host_fetcher_(tether_host_fetcher), tether_host_fetcher_(tether_host_fetcher),
connection_manager_(connection_manager), connection_manager_(connection_manager),
host_scan_device_prioritizer_(host_scan_device_prioritizer), tether_host_response_recorder_(tether_host_response_recorder),
device_id_tether_network_guid_map_(device_id_tether_network_guid_map), device_id_tether_network_guid_map_(device_id_tether_network_guid_map),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
network_connection_handler_->SetTetherDelegate(this); network_connection_handler_->SetTetherDelegate(this);
...@@ -175,7 +175,7 @@ void TetherConnector::OnTetherHostToConnectFetched( ...@@ -175,7 +175,7 @@ void TetherConnector::OnTetherHostToConnectFetched(
connect_tethering_operation_ = connect_tethering_operation_ =
ConnectTetheringOperation::Factory::NewInstance( ConnectTetheringOperation::Factory::NewInstance(
*tether_host_to_connect, connection_manager_, *tether_host_to_connect, connection_manager_,
host_scan_device_prioritizer_); tether_host_response_recorder_);
connect_tethering_operation_->AddObserver(this); connect_tethering_operation_->AddObserver(this);
connect_tethering_operation_->Initialize(); connect_tethering_operation_->Initialize();
} }
......
...@@ -19,8 +19,8 @@ namespace tether { ...@@ -19,8 +19,8 @@ namespace tether {
class ActiveHost; class ActiveHost;
class BleConnectionManager; class BleConnectionManager;
class DeviceIdTetherNetworkGuidMap; class DeviceIdTetherNetworkGuidMap;
class HostScanDevicePrioritizer;
class TetherHostFetcher; class TetherHostFetcher;
class TetherHostResponseRecorder;
class WifiHotspotConnector; class WifiHotspotConnector;
// Connects to a tether network. When the user initiates a connection via the // Connects to a tether network. When the user initiates a connection via the
...@@ -38,7 +38,7 @@ class TetherConnector : public NetworkConnectionHandler::TetherDelegate, ...@@ -38,7 +38,7 @@ class TetherConnector : public NetworkConnectionHandler::TetherDelegate,
ActiveHost* active_host, ActiveHost* active_host,
TetherHostFetcher* tether_host_fetcher, TetherHostFetcher* tether_host_fetcher,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer, TetherHostResponseRecorder* tether_host_response_recorder,
DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map); DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map);
~TetherConnector() override; ~TetherConnector() override;
...@@ -76,7 +76,7 @@ class TetherConnector : public NetworkConnectionHandler::TetherDelegate, ...@@ -76,7 +76,7 @@ class TetherConnector : public NetworkConnectionHandler::TetherDelegate,
ActiveHost* active_host_; ActiveHost* active_host_;
TetherHostFetcher* tether_host_fetcher_; TetherHostFetcher* tether_host_fetcher_;
BleConnectionManager* connection_manager_; BleConnectionManager* connection_manager_;
HostScanDevicePrioritizer* host_scan_device_prioritizer_; TetherHostResponseRecorder* tether_host_response_recorder_;
DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map_; DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map_;
std::string device_id_pending_connection_; std::string device_id_pending_connection_;
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#include "chromeos/components/tether/fake_ble_connection_manager.h" #include "chromeos/components/tether/fake_ble_connection_manager.h"
#include "chromeos/components/tether/fake_tether_host_fetcher.h" #include "chromeos/components/tether/fake_tether_host_fetcher.h"
#include "chromeos/components/tether/fake_wifi_hotspot_connector.h" #include "chromeos/components/tether/fake_wifi_hotspot_connector.h"
#include "chromeos/components/tether/mock_host_scan_device_prioritizer.h" #include "chromeos/components/tether/mock_tether_host_response_recorder.h"
#include "chromeos/components/tether/tether_connector.h" #include "chromeos/components/tether/tether_connector.h"
#include "chromeos/dbus/dbus_thread_manager.h" #include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/network/network_connection_handler.h" #include "chromeos/network/network_connection_handler.h"
...@@ -67,10 +67,10 @@ class FakeConnectTetheringOperation : public ConnectTetheringOperation { ...@@ -67,10 +67,10 @@ class FakeConnectTetheringOperation : public ConnectTetheringOperation {
FakeConnectTetheringOperation( FakeConnectTetheringOperation(
const cryptauth::RemoteDevice& device_to_connect, const cryptauth::RemoteDevice& device_to_connect,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer) TetherHostResponseRecorder* tether_host_response_recorder)
: ConnectTetheringOperation(device_to_connect, : ConnectTetheringOperation(device_to_connect,
connection_manager, connection_manager,
host_scan_device_prioritizer) {} tether_host_response_recorder) {}
~FakeConnectTetheringOperation() override {} ~FakeConnectTetheringOperation() override {}
...@@ -104,10 +104,10 @@ class FakeConnectTetheringOperationFactory ...@@ -104,10 +104,10 @@ class FakeConnectTetheringOperationFactory
std::unique_ptr<ConnectTetheringOperation> BuildInstance( std::unique_ptr<ConnectTetheringOperation> BuildInstance(
const cryptauth::RemoteDevice& device_to_connect, const cryptauth::RemoteDevice& device_to_connect,
BleConnectionManager* connection_manager, BleConnectionManager* connection_manager,
HostScanDevicePrioritizer* host_scan_device_prioritizer) override { TetherHostResponseRecorder* tether_host_response_recorder) override {
FakeConnectTetheringOperation* operation = FakeConnectTetheringOperation* operation =
new FakeConnectTetheringOperation(device_to_connect, connection_manager, new FakeConnectTetheringOperation(device_to_connect, connection_manager,
host_scan_device_prioritizer); tether_host_response_recorder);
created_operations_.push_back(operation); created_operations_.push_back(operation);
return base::WrapUnique(operation); return base::WrapUnique(operation);
} }
...@@ -143,8 +143,8 @@ class TetherConnectorTest : public NetworkStateTest { ...@@ -143,8 +143,8 @@ class TetherConnectorTest : public NetworkStateTest {
fake_tether_host_fetcher_ = base::MakeUnique<FakeTetherHostFetcher>( fake_tether_host_fetcher_ = base::MakeUnique<FakeTetherHostFetcher>(
test_devices_, false /* synchronously_reply_with_results */); test_devices_, false /* synchronously_reply_with_results */);
fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>();
mock_host_scan_device_prioritizer_ = mock_tether_host_response_recorder_ =
base::MakeUnique<MockHostScanDevicePrioritizer>(); base::MakeUnique<MockTetherHostResponseRecorder>();
device_id_tether_network_guid_map_ = device_id_tether_network_guid_map_ =
base::MakeUnique<DeviceIdTetherNetworkGuidMap>(); base::MakeUnique<DeviceIdTetherNetworkGuidMap>();
...@@ -154,7 +154,7 @@ class TetherConnectorTest : public NetworkStateTest { ...@@ -154,7 +154,7 @@ class TetherConnectorTest : public NetworkStateTest {
test_network_connection_handler_.get(), network_state_handler(), test_network_connection_handler_.get(), network_state_handler(),
fake_wifi_hotspot_connector_.get(), fake_active_host_.get(), fake_wifi_hotspot_connector_.get(), fake_active_host_.get(),
fake_tether_host_fetcher_.get(), fake_ble_connection_manager_.get(), fake_tether_host_fetcher_.get(), fake_ble_connection_manager_.get(),
mock_host_scan_device_prioritizer_.get(), mock_tether_host_response_recorder_.get(),
device_id_tether_network_guid_map_.get())); device_id_tether_network_guid_map_.get()));
SetUpTetherNetworks(); SetUpTetherNetworks();
...@@ -240,8 +240,8 @@ class TetherConnectorTest : public NetworkStateTest { ...@@ -240,8 +240,8 @@ class TetherConnectorTest : public NetworkStateTest {
std::unique_ptr<FakeActiveHost> fake_active_host_; std::unique_ptr<FakeActiveHost> fake_active_host_;
std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_; std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_;
std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_;
std::unique_ptr<MockHostScanDevicePrioritizer> std::unique_ptr<MockTetherHostResponseRecorder>
mock_host_scan_device_prioritizer_; mock_tether_host_response_recorder_;
// TODO(hansberry): Use a fake for this when a real mapping scheme is created. // TODO(hansberry): Use a fake for this when a real mapping scheme is created.
std::unique_ptr<DeviceIdTetherNetworkGuidMap> std::unique_ptr<DeviceIdTetherNetworkGuidMap>
device_id_tether_network_guid_map_; device_id_tether_network_guid_map_;
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/tether/tether_host_response_recorder.h"
#include "base/memory/ptr_util.h"
#include "base/values.h"
#include "chromeos/components/tether/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
namespace chromeos {
namespace tether {
// static
void TetherHostResponseRecorder::RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterListPref(prefs::kMostRecentTetherAvailablilityResponderIds);
registry->RegisterListPref(prefs::kMostRecentConnectTetheringResponderIds);
}
TetherHostResponseRecorder::TetherHostResponseRecorder(
PrefService* pref_service)
: pref_service_(pref_service) {}
TetherHostResponseRecorder::~TetherHostResponseRecorder() {}
void TetherHostResponseRecorder::RecordSuccessfulTetherAvailabilityResponse(
const cryptauth::RemoteDevice& remote_device) {
AddRecentResponse(remote_device.GetDeviceId(),
prefs::kMostRecentTetherAvailablilityResponderIds);
}
std::vector<std::string>
TetherHostResponseRecorder::GetPreviouslyAvailableHostIds() const {
return GetDeviceIdsForPref(prefs::kMostRecentTetherAvailablilityResponderIds);
}
void TetherHostResponseRecorder::RecordSuccessfulConnectTetheringResponse(
const cryptauth::RemoteDevice& remote_device) {
AddRecentResponse(remote_device.GetDeviceId(),
prefs::kMostRecentConnectTetheringResponderIds);
}
std::vector<std::string>
TetherHostResponseRecorder::GetPreviouslyConnectedHostIds() const {
return GetDeviceIdsForPref(prefs::kMostRecentConnectTetheringResponderIds);
}
void TetherHostResponseRecorder::AddRecentResponse(
const std::string& device_id,
const std::string& pref_name) {
const base::ListValue* ids = pref_service_->GetList(pref_name);
// Create a mutable copy of the stored IDs, or create one if it has yet to be
// stored.
std::unique_ptr<base::ListValue> updated_ids =
ids ? ids->CreateDeepCopy() : base::MakeUnique<base::ListValue>();
// Remove the device ID if it was already present in the list.
std::unique_ptr<base::Value> device_id_value =
base::MakeUnique<base::Value>(device_id);
updated_ids->Remove(*device_id_value, nullptr);
// Add the device ID to the front of the queue.
updated_ids->Insert(0, std::move(device_id_value));
// Store the updated list back in |pref_service_|.
pref_service_->Set(pref_name, *updated_ids);
}
std::vector<std::string> TetherHostResponseRecorder::GetDeviceIdsForPref(
const std::string& pref_name) const {
std::vector<std::string> device_ids;
const base::ListValue* ids = pref_service_->GetList(pref_name);
if (!ids)
return device_ids;
for (auto it = ids->begin(); it != ids->end(); ++it) {
std::string device_id;
bool success = it->GetAsString(&device_id);
if (success)
device_ids.push_back(device_id);
}
return device_ids;
}
} // namespace tether
} // namespace chromeos
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_COMPONENTS_TETHER_TETHER_HOST_RESPONSE_RECORDER_H_
#define CHROMEOS_COMPONENTS_TETHER_TETHER_HOST_RESPONSE_RECORDER_H_
#include <string>
#include <vector>
#include "base/macros.h"
#include "components/cryptauth/remote_device.h"
class PrefService;
class PrefRegistrySimple;
namespace chromeos {
namespace tether {
// Records responses from tether hosts in user prefs, which persists these
// responses between reboots of the device. When a TetherAvailabilityResponse or
// ConnectTetheringResponse is received, it should be recorded using this class.
// Responses can be retrieved at a later time via getter methods.
class TetherHostResponseRecorder {
public:
// Note: The PrefService* passed here must be created using the same registry
// passed to RegisterPrefs().
TetherHostResponseRecorder(PrefService* pref_service);
virtual ~TetherHostResponseRecorder();
// Registers the prefs used by this class to |registry|. Must be called before
// this class is utilized.
static void RegisterPrefs(PrefRegistrySimple* registry);
// Records a TetherAvailabilityResponse. This function should be called each
// time that a response is received from a potential host, even if a
// connection is not started.
virtual void RecordSuccessfulTetherAvailabilityResponse(
const cryptauth::RemoteDevice& remote_device);
// Gets device IDs corresponding to hosts which have sent
// TetherAvailabilityResponses with a response code indicating that tethering
// is available. The list is sorted; the IDs of the devices which have
// responded most recently are at the front of the list and the IDs of the
// devices which have responded least recently are at the end of the list.
virtual std::vector<std::string> GetPreviouslyAvailableHostIds() const;
// Records a ConnectTetheringResponse. This function should be called each
// time that a response is received from a host.
virtual void RecordSuccessfulConnectTetheringResponse(
const cryptauth::RemoteDevice& remote_device);
// Gets device IDs corresponding to hosts which have sent
// ConnectTetheringResponses with a response code indicating that they have
// successfully turned on their Wi-Fi hotspots. The list is sorted; the IDs of
// the devices which have responded most recently are at the front of the list
// and the IDs of the devices which have responded least recently are at the
// end of the list.
virtual std::vector<std::string> GetPreviouslyConnectedHostIds() const;
private:
void AddRecentResponse(const std::string& device_id,
const std::string& pref_name);
std::vector<std::string> GetDeviceIdsForPref(
const std::string& pref_name) const;
PrefService* pref_service_;
DISALLOW_COPY_AND_ASSIGN(TetherHostResponseRecorder);
};
} // namespace tether
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_TETHER_TETHER_HOST_RESPONSE_RECORDER_H_
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/tether/tether_host_response_recorder.h"
#include "components/cryptauth/remote_device_test_util.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace tether {
class TetherHostResponseRecorderTest : public testing::Test {
protected:
TetherHostResponseRecorderTest()
: test_devices_(cryptauth::GenerateTestRemoteDevices(10)) {}
void SetUp() override {
pref_service_ = base::MakeUnique<TestingPrefServiceSimple>();
TetherHostResponseRecorder::RegisterPrefs(pref_service_->registry());
recorder_ =
base::MakeUnique<TetherHostResponseRecorder>(pref_service_.get());
}
const std::vector<cryptauth::RemoteDevice> test_devices_;
std::unique_ptr<TestingPrefServiceSimple> pref_service_;
std::unique_ptr<TetherHostResponseRecorder> recorder_;
private:
DISALLOW_COPY_AND_ASSIGN(TetherHostResponseRecorderTest);
};
TEST_F(TetherHostResponseRecorderTest, TestTetherAvailabilityResponses) {
// Receive TetherAvailabilityResponses from devices in the following order:
// 0, 2, 4, 6, 8, 1, 3, 5, 7, 9
recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]);
recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[2]);
recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[4]);
recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[6]);
recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[8]);
recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]);
recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[3]);
recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[5]);
recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[7]);
recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[9]);
// The order, from most recent to least recent, should be:
// 9, 7, 5, 3, 1, 8, 6, 4, 2, 0
EXPECT_EQ(
(std::vector<std::string>{
test_devices_[9].GetDeviceId(), test_devices_[7].GetDeviceId(),
test_devices_[5].GetDeviceId(), test_devices_[3].GetDeviceId(),
test_devices_[1].GetDeviceId(), test_devices_[8].GetDeviceId(),
test_devices_[6].GetDeviceId(), test_devices_[4].GetDeviceId(),
test_devices_[2].GetDeviceId(), test_devices_[0].GetDeviceId()}),
recorder_->GetPreviouslyAvailableHostIds());
}
TEST_F(TetherHostResponseRecorderTest, TestConnectTetheringResponses) {
// Receive TetherAvailabilityResponses from devices in the following order:
// 0, 2, 4, 6, 8, 1, 3, 5, 7, 9
recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[0]);
recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[2]);
recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[4]);
recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[6]);
recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[8]);
recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[1]);
recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[3]);
recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[5]);
recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[7]);
recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[9]);
// The order, from most recent to least recent, should be:
// 9, 7, 5, 3, 1, 8, 6, 4, 2, 0
EXPECT_EQ(
(std::vector<std::string>{
test_devices_[9].GetDeviceId(), test_devices_[7].GetDeviceId(),
test_devices_[5].GetDeviceId(), test_devices_[3].GetDeviceId(),
test_devices_[1].GetDeviceId(), test_devices_[8].GetDeviceId(),
test_devices_[6].GetDeviceId(), test_devices_[4].GetDeviceId(),
test_devices_[2].GetDeviceId(), test_devices_[0].GetDeviceId()}),
recorder_->GetPreviouslyConnectedHostIds());
}
TEST_F(TetherHostResponseRecorderTest, TestBothResponseTypes) {
// Receive TetherAvailabilityResponses from devices 0, 1, and 2.
recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]);
recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]);
recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[2]);
// Receive a ConnectTetheringResponse from device 2.
recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[2]);
// Receive TetherAvailabilityResponses from devices 0, 1, and 3.
recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[0]);
recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[1]);
recorder_->RecordSuccessfulTetherAvailabilityResponse(test_devices_[3]);
// Receive a ConnectTetheringResponse from device 0.
recorder_->RecordSuccessfulConnectTetheringResponse(test_devices_[0]);
// The order for TetherAvailabilityResponses, from most recent to least
// recent, should be:
// 3, 1, 0, 2
EXPECT_EQ(
(std::vector<std::string>{
test_devices_[3].GetDeviceId(), test_devices_[1].GetDeviceId(),
test_devices_[0].GetDeviceId(), test_devices_[2].GetDeviceId()}),
recorder_->GetPreviouslyAvailableHostIds());
// The order for ConnectTetheringResponses, from most recent to least
// recent, should be:
// 0, 2
EXPECT_EQ((std::vector<std::string>{test_devices_[0].GetDeviceId(),
test_devices_[2].GetDeviceId()}),
recorder_->GetPreviouslyConnectedHostIds());
}
} // namespace tether
} // namespace cryptauth
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