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