Commit f633990e authored by Robbie McElrath's avatar Robbie McElrath Committed by Commit Bot

Migrate CloudPolicyRefreshScheduler to NetworkConnectionTracker

This CL migrates CloudPolicyRefreshScheduler from NetworkChangeNotifier
to NetworkConnectionTracker, which works with the network service
enabled.

Bug: 883124
Cq-Include-Trybots: luci.chromium.try:linux_mojo
Change-Id: I455250e60d94347da2fcb8ecc752dbf958bc2abd
Reviewed-on: https://chromium-review.googlesource.com/c/1220241
Commit-Queue: Robbie McElrath <rmcelrath@chromium.org>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Reviewed-by: default avatarDan Erat <derat@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#595993}
parent e90cd145
......@@ -24,6 +24,7 @@
#include "components/policy/proto/chrome_device_policy.pb.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "google/cacheinvalidation/include/types.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -77,10 +78,9 @@ TEST(AffiliatedCloudPolicyInvalidatorTest, CreateUseDestroy) {
// Set up a CloudPolicyCore backed by a simple CloudPolicyStore that does no
// signature verification and stores policy in memory.
FakeCloudPolicyStore store;
CloudPolicyCore core(dm_protocol::kChromeDevicePolicyType,
std::string(),
&store,
base::ThreadTaskRunnerHandle::Get());
CloudPolicyCore core(dm_protocol::kChromeDevicePolicyType, std::string(),
&store, base::ThreadTaskRunnerHandle::Get(),
network::TestNetworkConnectionTracker::CreateGetter());
// Connect |core|. Expect it to send a registration request. Let the
// registration succeed.
......
......@@ -46,6 +46,7 @@
#include "components/policy/proto/device_management_backend.pb.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/network_service_instance.h"
#include "crypto/sha2.h"
#include "net/url_request/url_request_context_getter.h"
#include "url/gurl.h"
......@@ -110,10 +111,12 @@ DeviceCloudPolicyManagerChromeOS::DeviceCloudPolicyManagerChromeOS(
std::unique_ptr<DeviceCloudPolicyStoreChromeOS> store,
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
ServerBackedStateKeysBroker* state_keys_broker)
: CloudPolicyManager(dm_protocol::kChromeDevicePolicyType,
std::string(),
store.get(),
task_runner),
: CloudPolicyManager(
dm_protocol::kChromeDevicePolicyType,
std::string(),
store.get(),
task_runner,
base::BindRepeating(&content::GetNetworkConnectionTracker)),
device_store_(std::move(store)),
state_keys_broker_(state_keys_broker),
task_runner_(task_runner),
......
......@@ -41,6 +41,7 @@
#include "components/policy/policy_constants.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/network_service_instance.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "url/gurl.h"
......@@ -142,7 +143,8 @@ DeviceLocalAccountPolicyBroker::DeviceLocalAccountPolicyBroker(
core_(dm_protocol::kChromePublicAccountPolicyType,
store_->account_id(),
store_.get(),
task_runner),
task_runner,
base::BindRepeating(&content::GetNetworkConnectionTracker)),
policy_update_callback_(policy_update_callback),
resource_cache_task_runner_(resource_cache_task_runner) {
if (account.type != DeviceLocalAccount::TYPE_ARC_KIOSK_APP) {
......
......@@ -51,6 +51,7 @@
#include "components/user_manager/known_user.h"
#include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "net/url_request/url_request_context_getter.h"
......@@ -119,10 +120,12 @@ UserCloudPolicyManagerChromeOS::UserCloudPolicyManagerChromeOS(
base::OnceClosure fatal_error_callback,
const AccountId& account_id,
const scoped_refptr<base::SequencedTaskRunner>& task_runner)
: CloudPolicyManager(dm_protocol::kChromeUserPolicyType,
std::string(),
store.get(),
task_runner),
: CloudPolicyManager(
dm_protocol::kChromeUserPolicyType,
std::string(),
store.get(),
task_runner,
base::BindRepeating(content::GetNetworkConnectionTracker)),
profile_(profile),
store_(std::move(store)),
external_data_manager_(std::move(external_data_manager)),
......
......@@ -34,6 +34,7 @@
#include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
#include "components/policy/core/common/policy_types.h"
#include "components/policy/policy_constants.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -215,7 +216,8 @@ CloudPolicyInvalidatorTest::CloudPolicyInvalidatorTest()
: core_(dm_protocol::kChromeUserPolicyType,
std::string(),
&store_,
loop_.task_runner()),
loop_.task_runner(),
network::TestNetworkConnectionTracker::CreateGetter()),
client_(nullptr),
task_runner_(new base::TestSimpleTaskRunner()),
object_id_a_(135, "asdf"),
......
......@@ -40,6 +40,7 @@
#include "components/policy/core/common/cloud/mock_cloud_external_data_manager.h"
#include "components/policy/core/common/cloud/mock_device_management_service.h"
#include "components/policy/core/common/policy_switches.h"
#include "content/public/browser/network_service_instance.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/base/upload_data_stream.h"
#include "testing/gmock/include/gmock/gmock.h"
......@@ -356,7 +357,8 @@ class MachineLevelUserCloudPolicyManagerTest : public InProcessBrowserTest {
std::unique_ptr<MachineLevelUserCloudPolicyManager> manager =
std::make_unique<MachineLevelUserCloudPolicyManager>(
std::move(policy_store), nullptr, policy_dir,
base::ThreadTaskRunnerHandle::Get());
base::ThreadTaskRunnerHandle::Get(),
base::BindRepeating(&content::GetNetworkConnectionTracker));
manager->Init(&schema_registry);
manager->store()->RemoveObserver(&observer);
......
......@@ -20,6 +20,7 @@
#include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
#include "components/policy/core/common/cloud/user_cloud_policy_store.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/network_service_instance.h"
namespace policy {
......@@ -143,10 +144,11 @@ UserCloudPolicyManagerFactory::CreateManagerForOriginalBrowserContext(
context->GetPath().Append(kPolicy).Append(kComponentsDir);
std::unique_ptr<UserCloudPolicyManager> manager;
manager.reset(
new UserCloudPolicyManager(std::move(store), component_policy_cache_dir,
std::unique_ptr<CloudExternalDataManager>(),
base::ThreadTaskRunnerHandle::Get()));
manager.reset(new UserCloudPolicyManager(
std::move(store), component_policy_cache_dir,
std::unique_ptr<CloudExternalDataManager>(),
base::ThreadTaskRunnerHandle::Get(),
base::BindRepeating(&content::GetNetworkConnectionTracker)));
manager->Init(
SchemaRegistryServiceFactory::GetForContext(context)->registry());
manager_wrappers_[context] = new ManagerWrapper(manager.get());
......
......@@ -55,6 +55,7 @@
#include "net/base/net_errors.h"
#include "net/http/http_status_code.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -100,7 +101,8 @@ UserCloudPolicyManager* BuildCloudPolicyManager(
return new UserCloudPolicyManager(
std::unique_ptr<UserCloudPolicyStore>(store), base::FilePath(),
std::unique_ptr<CloudExternalDataManager>(),
base::ThreadTaskRunnerHandle::Get());
base::ThreadTaskRunnerHandle::Get(),
network::TestNetworkConnectionTracker::CreateGetter());
}
class UserPolicySigninServiceTest : public testing::Test {
......
......@@ -26,6 +26,7 @@
#include "components/policy/core/common/cloud/machine_level_user_cloud_policy_metrics.h"
#include "components/policy/core/common/cloud/machine_level_user_cloud_policy_store.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/common/content_switches.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
......@@ -111,7 +112,8 @@ MachineLevelUserCloudPolicyController::CreatePolicyManager() {
{base::MayBlock(), base::TaskPriority::BEST_EFFORT}));
return std::make_unique<MachineLevelUserCloudPolicyManager>(
std::move(policy_store), nullptr, policy_dir,
base::ThreadTaskRunnerHandle::Get());
base::ThreadTaskRunnerHandle::Get(),
base::BindRepeating(&content::GetNetworkConnectionTracker));
}
void MachineLevelUserCloudPolicyController::Init(
......
......@@ -25,6 +25,7 @@
#include "components/policy/policy_constants.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "components/user_manager/user.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -54,7 +55,8 @@ class ProfilePolicyConnectorTest : public testing::Test {
cloud_policy_store_.NotifyStoreLoaded();
const auto task_runner = scoped_task_environment_.GetMainThreadTaskRunner();
cloud_policy_manager_.reset(new CloudPolicyManager(
std::string(), std::string(), &cloud_policy_store_, task_runner));
std::string(), std::string(), &cloud_policy_store_, task_runner,
network::TestNetworkConnectionTracker::CreateGetter()));
cloud_policy_manager_->Init(&schema_registry_);
}
......
......@@ -29,11 +29,13 @@ CloudPolicyCore::CloudPolicyCore(
const std::string& policy_type,
const std::string& settings_entity_id,
CloudPolicyStore* store,
const scoped_refptr<base::SequencedTaskRunner>& task_runner)
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
network::NetworkConnectionTrackerGetter network_connection_tracker_getter)
: policy_type_(policy_type),
settings_entity_id_(settings_entity_id),
store_(store),
task_runner_(task_runner) {}
task_runner_(task_runner),
network_connection_tracker_getter_(network_connection_tracker_getter) {}
CloudPolicyCore::~CloudPolicyCore() {}
......@@ -81,7 +83,8 @@ void CloudPolicyCore::RefreshSoon() {
void CloudPolicyCore::StartRefreshScheduler() {
if (!refresh_scheduler_) {
refresh_scheduler_.reset(
new CloudPolicyRefreshScheduler(client_.get(), store_, task_runner_));
new CloudPolicyRefreshScheduler(client_.get(), store_, task_runner_,
network_connection_tracker_getter_));
UpdateRefreshDelayFromPref();
for (auto& observer : observers_)
observer.OnRefreshSchedulerStarted(this);
......
......@@ -13,6 +13,7 @@
#include "base/observer_list.h"
#include "components/policy/policy_export.h"
#include "components/prefs/pref_member.h"
#include "services/network/public/cpp/network_connection_tracker.h"
class PrefService;
......@@ -60,7 +61,9 @@ class POLICY_EXPORT CloudPolicyCore {
CloudPolicyCore(const std::string& policy_type,
const std::string& settings_entity_id,
CloudPolicyStore* store,
const scoped_refptr<base::SequencedTaskRunner>& task_runner);
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
network::NetworkConnectionTrackerGetter
network_connection_tracker_getter);
~CloudPolicyCore();
CloudPolicyClient* client() { return client_.get(); }
......@@ -124,6 +127,7 @@ class POLICY_EXPORT CloudPolicyCore {
std::string settings_entity_id_;
CloudPolicyStore* store_;
scoped_refptr<base::SequencedTaskRunner> task_runner_;
network::NetworkConnectionTrackerGetter network_connection_tracker_getter_;
std::unique_ptr<CloudPolicyClient> client_;
std::unique_ptr<CloudPolicyService> service_;
std::unique_ptr<CloudPolicyRefreshScheduler> refresh_scheduler_;
......
......@@ -13,6 +13,7 @@
#include "components/policy/core/common/policy_pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace policy {
......@@ -24,7 +25,8 @@ class CloudPolicyCoreTest : public testing::Test,
: core_(dm_protocol::kChromeUserPolicyType,
std::string(),
&store_,
loop_.task_runner()),
loop_.task_runner(),
network::TestNetworkConnectionTracker::CreateGetter()),
core_connected_callback_count_(0),
refresh_scheduler_started_callback_count_(0),
core_disconnecting_callback_count_(0),
......
......@@ -31,8 +31,13 @@ CloudPolicyManager::CloudPolicyManager(
const std::string& policy_type,
const std::string& settings_entity_id,
CloudPolicyStore* cloud_policy_store,
const scoped_refptr<base::SequencedTaskRunner>& task_runner)
: core_(policy_type, settings_entity_id, cloud_policy_store, task_runner),
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
network::NetworkConnectionTrackerGetter network_connection_tracker_getter)
: core_(policy_type,
settings_entity_id,
cloud_policy_store,
task_runner,
network_connection_tracker_getter),
waiting_for_policy_refresh_(false) {}
CloudPolicyManager::~CloudPolicyManager() {}
......
......@@ -17,6 +17,7 @@
#include "components/policy/core/common/configuration_policy_provider.h"
#include "components/policy/policy_export.h"
#include "components/prefs/pref_member.h"
#include "services/network/public/cpp/network_connection_tracker.h"
namespace base {
class FilePath;
......@@ -44,7 +45,9 @@ class POLICY_EXPORT CloudPolicyManager
const std::string& policy_type,
const std::string& settings_entity_id,
CloudPolicyStore* cloud_policy_store,
const scoped_refptr<base::SequencedTaskRunner>& task_runner);
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
network::NetworkConnectionTrackerGetter
network_connection_tracker_getter);
~CloudPolicyManager() override;
CloudPolicyCore* core() { return &core_; }
......
......@@ -20,6 +20,7 @@
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_types.h"
#include "components/policy/core/common/schema_registry.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -79,7 +80,8 @@ ConfigurationPolicyProvider* TestHarness::CreateProvider(
// Create and initialize the store.
store_.NotifyStoreLoaded();
ConfigurationPolicyProvider* provider = new CloudPolicyManager(
dm_protocol::kChromeUserPolicyType, std::string(), &store_, task_runner);
dm_protocol::kChromeUserPolicyType, std::string(), &store_, task_runner,
network::TestNetworkConnectionTracker::CreateGetter());
Mock::VerifyAndClearExpectations(&store_);
return provider;
}
......@@ -144,10 +146,12 @@ class TestCloudPolicyManager : public CloudPolicyManager {
TestCloudPolicyManager(
CloudPolicyStore* store,
const scoped_refptr<base::SequencedTaskRunner>& task_runner)
: CloudPolicyManager(dm_protocol::kChromeUserPolicyType,
std::string(),
store,
task_runner) {}
: CloudPolicyManager(
dm_protocol::kChromeUserPolicyType,
std::string(),
store,
task_runner,
network::TestNetworkConnectionTracker::CreateGetter()) {}
~TestCloudPolicyManager() override {}
// Publish the protected members for testing.
......
......@@ -57,17 +57,20 @@ const int64_t CloudPolicyRefreshScheduler::kRefreshDelayMaxMs =
CloudPolicyRefreshScheduler::CloudPolicyRefreshScheduler(
CloudPolicyClient* client,
CloudPolicyStore* store,
const scoped_refptr<base::SequencedTaskRunner>& task_runner)
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
network::NetworkConnectionTrackerGetter network_connection_tracker_getter)
: client_(client),
store_(store),
task_runner_(task_runner),
network_connection_tracker_(network_connection_tracker_getter.Run()),
error_retry_delay_ms_(kInitialErrorRetryDelayMs),
refresh_delay_ms_(kDefaultRefreshDelayMs),
invalidations_available_(false),
creation_time_(base::Time::NowFromSystemTime()) {
creation_time_(base::Time::NowFromSystemTime()),
weak_factory_(this) {
client_->AddObserver(this);
store_->AddObserver(this);
net::NetworkChangeNotifier::AddNetworkChangeObserver(this);
network_connection_tracker_->AddNetworkConnectionObserver(this);
UpdateLastRefreshFromPolicy();
ScheduleRefresh();
......@@ -76,7 +79,8 @@ CloudPolicyRefreshScheduler::CloudPolicyRefreshScheduler(
CloudPolicyRefreshScheduler::~CloudPolicyRefreshScheduler() {
store_->RemoveObserver(this);
client_->RemoveObserver(this);
net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
if (network_connection_tracker_)
network_connection_tracker_->RemoveNetworkConnectionObserver(this);
}
void CloudPolicyRefreshScheduler::SetDesiredRefreshDelay(
......@@ -177,9 +181,9 @@ void CloudPolicyRefreshScheduler::OnStoreError(CloudPolicyStore* store) {
// error is required. NB: Changes to is_managed fire OnStoreLoaded().
}
void CloudPolicyRefreshScheduler::OnNetworkChanged(
net::NetworkChangeNotifier::ConnectionType type) {
if (type == net::NetworkChangeNotifier::CONNECTION_NONE)
void CloudPolicyRefreshScheduler::OnConnectionChanged(
network::mojom::ConnectionType type) {
if (type == network::mojom::ConnectionType::CONNECTION_NONE)
return;
if (client_->status() == DM_STATUS_REQUEST_FAILED) {
......
......@@ -14,7 +14,7 @@
#include "components/policy/core/common/cloud/cloud_policy_client.h"
#include "components/policy/core/common/cloud/cloud_policy_store.h"
#include "components/policy/policy_export.h"
#include "net/base/network_change_notifier.h"
#include "services/network/public/cpp/network_connection_tracker.h"
namespace base {
class SequencedTaskRunner;
......@@ -27,7 +27,7 @@ namespace policy {
class POLICY_EXPORT CloudPolicyRefreshScheduler
: public CloudPolicyClient::Observer,
public CloudPolicyStore::Observer,
public net::NetworkChangeNotifier::NetworkChangeObserver {
public network::NetworkConnectionTracker::NetworkConnectionObserver {
public:
// Refresh constants.
static const int64_t kDefaultRefreshDelayMs;
......@@ -44,7 +44,9 @@ class POLICY_EXPORT CloudPolicyRefreshScheduler
CloudPolicyRefreshScheduler(
CloudPolicyClient* client,
CloudPolicyStore* store,
const scoped_refptr<base::SequencedTaskRunner>& task_runner);
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
network::NetworkConnectionTrackerGetter
network_connection_tracker_getter);
~CloudPolicyRefreshScheduler() override;
base::Time last_refresh() const { return last_refresh_; }
......@@ -82,10 +84,9 @@ class POLICY_EXPORT CloudPolicyRefreshScheduler
void OnStoreLoaded(CloudPolicyStore* store) override;
void OnStoreError(CloudPolicyStore* store) override;
// net::NetworkChangeNotifier::NetworkChangeObserver:
// network::NetworkConnectionTracker::NetworkConnectionObserver:
// Triggered also when the device wakes up.
void OnNetworkChanged(
net::NetworkChangeNotifier::ConnectionType type) override;
void OnConnectionChanged(network::mojom::ConnectionType type) override;
void set_last_refresh_for_testing(base::Time last_refresh);
......@@ -120,6 +121,9 @@ class POLICY_EXPORT CloudPolicyRefreshScheduler
// For scheduling delayed tasks.
const scoped_refptr<base::SequencedTaskRunner> task_runner_;
// For listening for network connection changes.
network::NetworkConnectionTracker* network_connection_tracker_;
// The delayed refresh callback.
base::CancelableClosure refresh_callback_;
......@@ -149,6 +153,8 @@ class POLICY_EXPORT CloudPolicyRefreshScheduler
// its initial status.
base::Time creation_time_;
base::WeakPtrFactory<CloudPolicyRefreshScheduler> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(CloudPolicyRefreshScheduler);
};
......
......@@ -18,6 +18,7 @@
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -38,8 +39,7 @@ const int64_t kInitialCacheAgeMinutes = 1;
class CloudPolicyRefreshSchedulerTest : public testing::Test {
protected:
CloudPolicyRefreshSchedulerTest()
: task_runner_(new base::TestSimpleTaskRunner()),
network_change_notifier_(net::NetworkChangeNotifier::CreateMock()) {}
: task_runner_(new base::TestSimpleTaskRunner()) {}
void SetUp() override {
client_.SetDMToken("token");
......@@ -59,15 +59,18 @@ class CloudPolicyRefreshSchedulerTest : public testing::Test {
CloudPolicyRefreshScheduler* CreateRefreshScheduler() {
EXPECT_EQ(0u, task_runner_->NumPendingTasks());
CloudPolicyRefreshScheduler* scheduler =
new CloudPolicyRefreshScheduler(&client_, &store_, task_runner_);
CloudPolicyRefreshScheduler* scheduler = new CloudPolicyRefreshScheduler(
&client_, &store_, task_runner_,
network::TestNetworkConnectionTracker::CreateGetter());
// Make sure the NetworkConnectionTracker has been set up.
base::RunLoop().RunUntilIdle();
scheduler->SetDesiredRefreshDelay(kPolicyRefreshRate);
return scheduler;
}
void NotifyNetworkChanged() {
net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
net::NetworkChangeNotifier::CONNECTION_WIFI);
void NotifyConnectionChanged() {
network::TestNetworkConnectionTracker::GetInstance()->SetConnectionType(
network::mojom::ConnectionType::CONNECTION_WIFI);
base::RunLoop().RunUntilIdle();
}
......@@ -166,7 +169,6 @@ class CloudPolicyRefreshSchedulerTest : public testing::Test {
MockCloudPolicyClient client_;
MockCloudPolicyStore store_;
scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier_;
// Base time for the refresh that the scheduler should be using.
base::Time last_update_;
......@@ -266,7 +268,9 @@ TEST_F(CloudPolicyRefreshSchedulerTest, RefreshSoonOverriding) {
TEST_F(CloudPolicyRefreshSchedulerTest, InvalidationsAvailable) {
std::unique_ptr<CloudPolicyRefreshScheduler> scheduler(
new CloudPolicyRefreshScheduler(&client_, &store_, task_runner_));
new CloudPolicyRefreshScheduler(
&client_, &store_, task_runner_,
network::TestNetworkConnectionTracker::CreateGetter()));
scheduler->SetDesiredRefreshDelay(kPolicyRefreshRate);
// The scheduler has scheduled refreshes at the initial refresh rate.
......@@ -295,7 +299,9 @@ TEST_F(CloudPolicyRefreshSchedulerTest, InvalidationsAvailable) {
TEST_F(CloudPolicyRefreshSchedulerTest, InvalidationsNotAvailable) {
std::unique_ptr<CloudPolicyRefreshScheduler> scheduler(
new CloudPolicyRefreshScheduler(&client_, &store_, task_runner_));
new CloudPolicyRefreshScheduler(
&client_, &store_, task_runner_,
network::TestNetworkConnectionTracker::CreateGetter()));
scheduler->SetDesiredRefreshDelay(kPolicyRefreshRate);
// Signal that invalidations are not available. The scheduler will not
......@@ -325,7 +331,9 @@ TEST_F(CloudPolicyRefreshSchedulerTest, InvalidationsNotAvailable) {
TEST_F(CloudPolicyRefreshSchedulerTest, InvalidationsOffAndOn) {
std::unique_ptr<CloudPolicyRefreshScheduler> scheduler(
new CloudPolicyRefreshScheduler(&client_, &store_, task_runner_));
new CloudPolicyRefreshScheduler(
&client_, &store_, task_runner_,
network::TestNetworkConnectionTracker::CreateGetter()));
scheduler->SetDesiredRefreshDelay(kPolicyRefreshRate);
scheduler->SetInvalidationServiceAvailability(true);
// Initial fetch.
......@@ -352,7 +360,9 @@ TEST_F(CloudPolicyRefreshSchedulerTest, InvalidationsOffAndOn) {
TEST_F(CloudPolicyRefreshSchedulerTest, InvalidationsDisconnected) {
std::unique_ptr<CloudPolicyRefreshScheduler> scheduler(
new CloudPolicyRefreshScheduler(&client_, &store_, task_runner_));
new CloudPolicyRefreshScheduler(
&client_, &store_, task_runner_,
network::TestNetworkConnectionTracker::CreateGetter()));
scheduler->SetDesiredRefreshDelay(kPolicyRefreshRate);
scheduler->SetInvalidationServiceAvailability(true);
// Initial fetch.
......@@ -375,7 +385,7 @@ TEST_F(CloudPolicyRefreshSchedulerTest, InvalidationsDisconnected) {
CheckTiming(kPolicyRefreshRate);
}
TEST_F(CloudPolicyRefreshSchedulerTest, OnNetworkChangedUnregistered) {
TEST_F(CloudPolicyRefreshSchedulerTest, OnConnectionChangedUnregistered) {
client_.SetDMToken(std::string());
std::unique_ptr<CloudPolicyRefreshScheduler> scheduler(
CreateRefreshScheduler());
......@@ -384,14 +394,16 @@ TEST_F(CloudPolicyRefreshSchedulerTest, OnNetworkChangedUnregistered) {
EXPECT_FALSE(task_runner_->HasPendingTask());
EmulateSleepThroughLastRefreshTime(scheduler.get());
scheduler->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_WIFI);
scheduler->OnConnectionChanged(
network::mojom::ConnectionType::CONNECTION_WIFI);
EXPECT_FALSE(task_runner_->HasPendingTask());
}
// TODO(igorcov): Before sleep in normal flow there's a task pending. When the
// device wakes up, OnNetworkChanged is called which should cancel the
// device wakes up, OnConnectionChanged is called which should cancel the
// pending task and queue a new task to run earlier. It is desirable to
// simulate that flow here.
TEST_F(CloudPolicyRefreshSchedulerTest, OnNetworkChangedAfterSleep) {
TEST_F(CloudPolicyRefreshSchedulerTest, OnConnectionChangedAfterSleep) {
std::unique_ptr<CloudPolicyRefreshScheduler> scheduler(
CreateRefreshScheduler());
......@@ -401,7 +413,8 @@ TEST_F(CloudPolicyRefreshSchedulerTest, OnNetworkChangedAfterSleep) {
EXPECT_FALSE(task_runner_->HasPendingTask());
EmulateSleepThroughLastRefreshTime(scheduler.get());
scheduler->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_WIFI);
scheduler->OnConnectionChanged(
network::mojom::ConnectionType::CONNECTION_WIFI);
EXPECT_TRUE(task_runner_->HasPendingTask());
task_runner_->ClearPendingTasks();
}
......@@ -464,9 +477,9 @@ TEST_F(CloudPolicyRefreshSchedulerSteadyStateTest, RefreshDelayChange) {
CheckTiming(CloudPolicyRefreshScheduler::kRefreshDelayMaxMs);
}
TEST_F(CloudPolicyRefreshSchedulerSteadyStateTest, OnNetworkChanged) {
TEST_F(CloudPolicyRefreshSchedulerSteadyStateTest, OnConnectionChanged) {
client_.SetStatus(DM_STATUS_REQUEST_FAILED);
NotifyNetworkChanged();
NotifyConnectionChanged();
EXPECT_EQ(GetLastDelay(), base::TimeDelta());
}
......
......@@ -32,6 +32,7 @@
#include "crypto/rsa_private_key.h"
#include "crypto/sha2.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -98,7 +99,8 @@ class ComponentCloudPolicyServiceTest : public testing::Test {
core_(dm_protocol::kChromeUserPolicyType,
std::string(),
&store_,
loop_.task_runner()) {
loop_.task_runner(),
network::TestNetworkConnectionTracker::CreateGetter()) {
builder_.SetDefaultSigningKey();
builder_.policy_data().set_policy_type(
dm_protocol::kChromeExtensionPolicyType);
......
......@@ -27,11 +27,13 @@ MachineLevelUserCloudPolicyManager::MachineLevelUserCloudPolicyManager(
std::unique_ptr<MachineLevelUserCloudPolicyStore> store,
std::unique_ptr<CloudExternalDataManager> external_data_manager,
const base::FilePath& policy_dir,
const scoped_refptr<base::SequencedTaskRunner>& task_runner)
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
network::NetworkConnectionTrackerGetter network_connection_tracker_getter)
: CloudPolicyManager(dm_protocol::kChromeMachineLevelUserCloudPolicyType,
std::string(),
store.get(),
task_runner),
task_runner,
network_connection_tracker_getter),
store_(std::move(store)),
external_data_manager_(std::move(external_data_manager)),
policy_dir_(policy_dir) {}
......
......@@ -9,6 +9,7 @@
#include "base/macros.h"
#include "components/policy/core/common/cloud/cloud_policy_manager.h"
#include "services/network/public/cpp/network_connection_tracker.h"
class PrefService;
......@@ -25,7 +26,9 @@ class POLICY_EXPORT MachineLevelUserCloudPolicyManager
std::unique_ptr<MachineLevelUserCloudPolicyStore> store,
std::unique_ptr<CloudExternalDataManager> external_data_manager,
const base::FilePath& policy_dir,
const scoped_refptr<base::SequencedTaskRunner>& task_runner);
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
network::NetworkConnectionTrackerGetter
network_connection_tracker_getter);
~MachineLevelUserCloudPolicyManager() override;
// Initializes the cloud connection. |local_state| must stay valid until this
......
......@@ -11,6 +11,7 @@
#include "base/sequenced_task_runner.h"
#include "components/policy/core/common/cloud/cloud_external_data_manager.h"
#include "components/policy/core/common/cloud/machine_level_user_cloud_policy_store.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -40,7 +41,8 @@ class MachineLevelUserCloudPolicyManagerTest : public ::testing::Test {
store_ = store.get();
manager_ = std::make_unique<MachineLevelUserCloudPolicyManager>(
std::move(store), std::unique_ptr<CloudExternalDataManager>(),
base::FilePath(), scoped_refptr<base::SequencedTaskRunner>());
base::FilePath(), scoped_refptr<base::SequencedTaskRunner>(),
network::TestNetworkConnectionTracker::CreateGetter());
}
SchemaRegistry schema_registry_;
......
......@@ -30,11 +30,13 @@ UserCloudPolicyManager::UserCloudPolicyManager(
std::unique_ptr<UserCloudPolicyStore> store,
const base::FilePath& component_policy_cache_path,
std::unique_ptr<CloudExternalDataManager> external_data_manager,
const scoped_refptr<base::SequencedTaskRunner>& task_runner)
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
network::NetworkConnectionTrackerGetter network_connection_tracker_getter)
: CloudPolicyManager(dm_protocol::kChromeUserPolicyType,
std::string(),
store.get(),
task_runner),
task_runner,
network_connection_tracker_getter),
store_(std::move(store)),
component_policy_cache_path_(component_policy_cache_path),
external_data_manager_(std::move(external_data_manager)) {}
......
......@@ -14,6 +14,7 @@
#include "base/memory/ref_counted.h"
#include "components/policy/core/common/cloud/cloud_policy_manager.h"
#include "components/policy/policy_export.h"
#include "services/network/public/cpp/network_connection_tracker.h"
class AccountId;
class PrefService;
......@@ -40,7 +41,9 @@ class POLICY_EXPORT UserCloudPolicyManager : public CloudPolicyManager {
std::unique_ptr<UserCloudPolicyStore> store,
const base::FilePath& component_policy_cache_path,
std::unique_ptr<CloudExternalDataManager> external_data_manager,
const scoped_refptr<base::SequencedTaskRunner>& task_runner);
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
network::NetworkConnectionTrackerGetter
network_connection_tracker_getter);
~UserCloudPolicyManager() override;
// ConfigurationPolicyProvider overrides:
......
......@@ -14,6 +14,7 @@
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_types.h"
#include "components/policy/core/common/schema_registry.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -53,7 +54,8 @@ class UserCloudPolicyManagerTest : public testing::Test {
const auto task_runner = scoped_task_environment_.GetMainThreadTaskRunner();
manager_.reset(new UserCloudPolicyManager(
std::unique_ptr<UserCloudPolicyStore>(store_), base::FilePath(),
std::unique_ptr<CloudExternalDataManager>(), task_runner));
std::unique_ptr<CloudExternalDataManager>(), task_runner,
network::TestNetworkConnectionTracker::CreateGetter()));
manager_->Init(&schema_registry_);
manager_->AddObserver(&observer_);
Mock::VerifyAndClearExpectations(store_);
......
......@@ -157,6 +157,13 @@ network::NetworkConnectionTracker* GetNetworkConnectionTracker() {
return g_network_connection_tracker;
}
void GetNetworkConnectionTrackerFromUIThread(
base::OnceCallback<void(network::NetworkConnectionTracker*)> callback) {
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE, {BrowserThread::UI, base::TaskPriority::BEST_EFFORT},
base::BindOnce(&GetNetworkConnectionTracker), std::move(callback));
}
void SetNetworkConnectionTrackerForTesting(
network::NetworkConnectionTracker* network_connection_tracker) {
if (g_network_connection_tracker != network_connection_tracker) {
......
......@@ -5,6 +5,7 @@
#ifndef CONTENT_PUBLIC_BROWSER_NETWORK_SERVICE_INSTANCE_H_
#define CONTENT_PUBLIC_BROWSER_NETWORK_SERVICE_INSTANCE_H_
#include "base/callback.h"
#include "content/common/content_export.h"
namespace network {
......@@ -52,6 +53,14 @@ CONTENT_EXPORT void FlushNetworkServiceInstanceForTesting();
// Must only be called on the UI thread.
CONTENT_EXPORT network::NetworkConnectionTracker* GetNetworkConnectionTracker();
// Asynchronously calls the given callback with a NetworkConnectionTracker that
// can be used to subscribe to network change events.
//
// This is a helper method for classes that can't easily call
// GetNetworkConnectionTracker from the UI thread.
CONTENT_EXPORT void GetNetworkConnectionTrackerFromUIThread(
base::OnceCallback<void(network::NetworkConnectionTracker*)> callback);
// Sets the NetworkConnectionTracker instance to use. For testing only.
// Must be called on the UI thread. Must be called before the first call to
// GetNetworkConnectionTracker.
......
......@@ -21,6 +21,12 @@
namespace network {
// Defines the type of a callback that will return a NetworkConnectionTracker
// instance.
class NetworkConnectionTracker;
using NetworkConnectionTrackerGetter =
base::RepeatingCallback<NetworkConnectionTracker*()>;
// This class subscribes to network change events from
// network::mojom::NetworkChangeManager and propogates these notifications to
// its NetworkConnectionObservers registered through
......
......@@ -4,14 +4,23 @@
#include "services/network/test/test_network_connection_tracker.h"
#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/threading/sequenced_task_runner_handle.h"
namespace network {
static TestNetworkConnectionTracker* g_test_network_connection_tracker_instance;
namespace {
NetworkConnectionTracker* GetNonTestInstance() {
return TestNetworkConnectionTracker::GetInstance();
}
} // namespace
// static
std::unique_ptr<TestNetworkConnectionTracker>
TestNetworkConnectionTracker::CreateInstance() {
......@@ -24,6 +33,11 @@ TestNetworkConnectionTracker* TestNetworkConnectionTracker::GetInstance() {
return g_test_network_connection_tracker_instance;
}
// static
NetworkConnectionTrackerGetter TestNetworkConnectionTracker::CreateGetter() {
return base::BindRepeating(&GetNonTestInstance);
}
TestNetworkConnectionTracker::TestNetworkConnectionTracker() {
if (g_test_network_connection_tracker_instance) {
LOG(WARNING) << "Creating more than one TestNetworkConnectionTracker";
......
......@@ -25,6 +25,10 @@ class TestNetworkConnectionTracker : public NetworkConnectionTracker {
// CreateInstance() must have been called before calling this.
static TestNetworkConnectionTracker* GetInstance();
// Creates a NetworkConnectionTrackerGetter that will return the active
// TestNetworkConnectionTracker instance when called.
static NetworkConnectionTrackerGetter CreateGetter();
~TestNetworkConnectionTracker() override;
bool GetConnectionType(network::mojom::ConnectionType* type,
......
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