Commit 76da73ab authored by Scott Violet's avatar Scott Violet Committed by Commit Bot

cleanup how ConfigurationPolicyProviders are set

It's expected that the providers are available when the service is
committed, so it make it explicit.

BUG=none
TEST=none

Change-Id: Ibc2f9be6ecec9261e1a2b9ebef18b16557882398
Reviewed-on: https://chromium-review.googlesource.com/890722
Commit-Queue: Scott Violet <sky@chromium.org>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Cr-Commit-Position: refs/heads/master@{#533126}
parent d055eca7
...@@ -62,14 +62,17 @@ std::unique_ptr<policy::ConfigurationPolicyHandlerList> BuildHandlerList( ...@@ -62,14 +62,17 @@ std::unique_ptr<policy::ConfigurationPolicyHandlerList> BuildHandlerList(
} // namespace } // namespace
AwBrowserPolicyConnector::AwBrowserPolicyConnector() AwBrowserPolicyConnector::AwBrowserPolicyConnector()
: BrowserPolicyConnectorBase(base::Bind(&BuildHandlerList)) { : BrowserPolicyConnectorBase(base::Bind(&BuildHandlerList)) {}
AwBrowserPolicyConnector::~AwBrowserPolicyConnector() = default;
std::vector<std::unique_ptr<policy::ConfigurationPolicyProvider>>
AwBrowserPolicyConnector::CreatePolicyProviders() {
std::vector<std::unique_ptr<policy::ConfigurationPolicyProvider>> providers; std::vector<std::unique_ptr<policy::ConfigurationPolicyProvider>> providers;
providers.push_back( providers.push_back(
std::make_unique<policy::android::AndroidCombinedPolicyProvider>( std::make_unique<policy::android::AndroidCombinedPolicyProvider>(
GetSchemaRegistry())); GetSchemaRegistry()));
SetPolicyProviders(std::move(providers)); return providers;
} }
AwBrowserPolicyConnector::~AwBrowserPolicyConnector() {}
} // namespace android_webview } // namespace android_webview
...@@ -13,11 +13,16 @@ namespace android_webview { ...@@ -13,11 +13,16 @@ namespace android_webview {
// Sets up and keeps the browser-global policy objects such as the PolicyService // Sets up and keeps the browser-global policy objects such as the PolicyService
// and the platform-specific PolicyProvider. // and the platform-specific PolicyProvider.
class AwBrowserPolicyConnector : public policy::BrowserPolicyConnectorBase { class AwBrowserPolicyConnector : public policy::BrowserPolicyConnectorBase {
public: public:
AwBrowserPolicyConnector(); AwBrowserPolicyConnector();
~AwBrowserPolicyConnector() override; ~AwBrowserPolicyConnector() override;
private: protected:
// policy::BrowserPolicyConnectorBase:
std::vector<std::unique_ptr<policy::ConfigurationPolicyProvider>>
CreatePolicyProviders() override;
private:
DISALLOW_COPY_AND_ASSIGN(AwBrowserPolicyConnector); DISALLOW_COPY_AND_ASSIGN(AwBrowserPolicyConnector);
}; };
......
...@@ -653,7 +653,6 @@ BrowserProcessImpl::browser_policy_connector() { ...@@ -653,7 +653,6 @@ BrowserProcessImpl::browser_policy_connector() {
DCHECK(!browser_policy_connector_); DCHECK(!browser_policy_connector_);
browser_policy_connector_ = platform_part_->CreateBrowserPolicyConnector(); browser_policy_connector_ = platform_part_->CreateBrowserPolicyConnector();
created_browser_policy_connector_ = true; created_browser_policy_connector_ = true;
browser_policy_connector_->InitPolicyProviders();
} }
return browser_policy_connector_.get(); return browser_policy_connector_.get();
} }
......
...@@ -355,11 +355,13 @@ void BrowserPolicyConnectorChromeOS::OnDeviceCloudPolicyManagerDisconnected() { ...@@ -355,11 +355,13 @@ void BrowserPolicyConnectorChromeOS::OnDeviceCloudPolicyManagerDisconnected() {
RestartDeviceCloudPolicyInitializer(); RestartDeviceCloudPolicyInitializer();
} }
void BrowserPolicyConnectorChromeOS::BuildPolicyProviders( std::vector<std::unique_ptr<policy::ConfigurationPolicyProvider>>
std::vector<std::unique_ptr<ConfigurationPolicyProvider>>* providers) { BrowserPolicyConnectorChromeOS::CreatePolicyProviders() {
auto providers = ChromeBrowserPolicyConnector::CreatePolicyProviders();
for (auto& provider_ptr : providers_for_init_) for (auto& provider_ptr : providers_for_init_)
providers->push_back(std::move(provider_ptr)); providers.push_back(std::move(provider_ptr));
providers_for_init_.clear(); providers_for_init_.clear();
return providers;
} }
void BrowserPolicyConnectorChromeOS::SetTimezoneIfPolicyAvailable() { void BrowserPolicyConnectorChromeOS::SetTimezoneIfPolicyAvailable() {
......
...@@ -180,9 +180,8 @@ class BrowserPolicyConnectorChromeOS ...@@ -180,9 +180,8 @@ class BrowserPolicyConnectorChromeOS
protected: protected:
// ChromeBrowserPolicyConnector: // ChromeBrowserPolicyConnector:
void BuildPolicyProviders( std::vector<std::unique_ptr<policy::ConfigurationPolicyProvider>>
std::vector<std::unique_ptr<ConfigurationPolicyProvider>>* providers) CreatePolicyProviders() override;
override;
private: private:
// Set the timezone as soon as the policies are available. // Set the timezone as soon as the policies are available.
......
...@@ -334,8 +334,7 @@ void CloudExternalDataPolicyObserverTest::LogInAsDeviceLocalAccount( ...@@ -334,8 +334,7 @@ void CloudExternalDataPolicyObserverTest::LogInAsDeviceLocalAccount(
providers.push_back(device_local_account_policy_provider_.get()); providers.push_back(device_local_account_policy_provider_.get());
TestingProfile::Builder builder; TestingProfile::Builder builder;
std::unique_ptr<PolicyServiceImpl> policy_service = std::unique_ptr<PolicyServiceImpl> policy_service =
std::make_unique<PolicyServiceImpl>(); std::make_unique<PolicyServiceImpl>(std::move(providers));
policy_service->SetProviders(providers);
builder.SetPolicyService(std::move(policy_service)); builder.SetPolicyService(std::move(policy_service));
builder.SetPath(chromeos::ProfileHelper::Get()->GetProfilePathByUserIdHash( builder.SetPath(chromeos::ProfileHelper::Get()->GetProfilePathByUserIdHash(
chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting( chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting(
...@@ -370,8 +369,7 @@ void CloudExternalDataPolicyObserverTest::LogInAsRegularUser() { ...@@ -370,8 +369,7 @@ void CloudExternalDataPolicyObserverTest::LogInAsRegularUser() {
providers.push_back(&user_policy_provider_); providers.push_back(&user_policy_provider_);
TestingProfile::Builder builder; TestingProfile::Builder builder;
std::unique_ptr<PolicyServiceImpl> policy_service = std::unique_ptr<PolicyServiceImpl> policy_service =
std::make_unique<PolicyServiceImpl>(); std::make_unique<PolicyServiceImpl>(std::move(providers));
policy_service->SetProviders(providers);
builder.SetPolicyService(std::move(policy_service)); builder.SetPolicyService(std::move(policy_service));
builder.SetPath(chromeos::ProfileHelper::Get()->GetProfilePathByUserIdHash( builder.SetPath(chromeos::ProfileHelper::Get()->GetProfilePathByUserIdHash(
chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting( chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting(
......
...@@ -220,8 +220,7 @@ class NetworkConfigurationUpdaterTest : public testing::Test { ...@@ -220,8 +220,7 @@ class NetworkConfigurationUpdaterTest : public testing::Test {
provider_.Init(); provider_.Init();
PolicyServiceImpl::Providers providers; PolicyServiceImpl::Providers providers;
providers.push_back(&provider_); providers.push_back(&provider_);
policy_service_ = std::make_unique<PolicyServiceImpl>(); policy_service_ = std::make_unique<PolicyServiceImpl>(std::move(providers));
policy_service_->SetProviders(providers);
std::unique_ptr<base::DictionaryValue> fake_toplevel_onc = std::unique_ptr<base::DictionaryValue> fake_toplevel_onc =
chromeos::onc::ReadDictionaryFromJson(kFakeONC); chromeos::onc::ReadDictionaryFromJson(kFakeONC);
......
...@@ -71,18 +71,6 @@ void ChromeBrowserPolicyConnector::OnResourceBundleCreated() { ...@@ -71,18 +71,6 @@ void ChromeBrowserPolicyConnector::OnResourceBundleCreated() {
BrowserPolicyConnectorBase::OnResourceBundleCreated(); BrowserPolicyConnectorBase::OnResourceBundleCreated();
} }
void ChromeBrowserPolicyConnector::InitPolicyProviders() {
std::vector<std::unique_ptr<ConfigurationPolicyProvider>> providers;
std::unique_ptr<ConfigurationPolicyProvider> platform_provider =
CreatePlatformProvider();
if (platform_provider) {
platform_provider_ = platform_provider.get();
providers.push_back(std::move(platform_provider));
}
BuildPolicyProviders(&providers);
SetPolicyProviders(std::move(providers));
}
void ChromeBrowserPolicyConnector::Init( void ChromeBrowserPolicyConnector::Init(
PrefService* local_state, PrefService* local_state,
scoped_refptr<net::URLRequestContextGetter> request_context) { scoped_refptr<net::URLRequestContextGetter> request_context) {
...@@ -104,6 +92,19 @@ ChromeBrowserPolicyConnector::GetPlatformProvider() { ...@@ -104,6 +92,19 @@ ChromeBrowserPolicyConnector::GetPlatformProvider() {
return provider ? provider : platform_provider_; return provider ? provider : platform_provider_;
} }
std::vector<std::unique_ptr<policy::ConfigurationPolicyProvider>>
ChromeBrowserPolicyConnector::CreatePolicyProviders() {
auto providers = BrowserPolicyConnector::CreatePolicyProviders();
std::unique_ptr<ConfigurationPolicyProvider> platform_provider =
CreatePlatformProvider();
if (platform_provider) {
platform_provider_ = platform_provider.get();
// PlatformProvider should be before all other providers (highest priority).
providers.insert(providers.begin(), std::move(platform_provider));
}
return providers;
}
std::unique_ptr<ConfigurationPolicyProvider> std::unique_ptr<ConfigurationPolicyProvider>
ChromeBrowserPolicyConnector::CreatePlatformProvider() { ChromeBrowserPolicyConnector::CreatePlatformProvider() {
#if defined(OS_WIN) #if defined(OS_WIN)
...@@ -140,7 +141,4 @@ ChromeBrowserPolicyConnector::CreatePlatformProvider() { ...@@ -140,7 +141,4 @@ ChromeBrowserPolicyConnector::CreatePlatformProvider() {
#endif #endif
} }
void ChromeBrowserPolicyConnector::BuildPolicyProviders(
std::vector<std::unique_ptr<ConfigurationPolicyProvider>>* providers) {}
} // namespace policy } // namespace policy
...@@ -42,9 +42,6 @@ class ChromeBrowserPolicyConnector : public BrowserPolicyConnector { ...@@ -42,9 +42,6 @@ class ChromeBrowserPolicyConnector : public BrowserPolicyConnector {
// class to notify observers. // class to notify observers.
void OnResourceBundleCreated(); void OnResourceBundleCreated();
// TODO(sky): remove. Temporary until resolve ordering.
void InitPolicyProviders();
void Init( void Init(
PrefService* local_state, PrefService* local_state,
scoped_refptr<net::URLRequestContextGetter> request_context) override; scoped_refptr<net::URLRequestContextGetter> request_context) override;
...@@ -52,12 +49,9 @@ class ChromeBrowserPolicyConnector : public BrowserPolicyConnector { ...@@ -52,12 +49,9 @@ class ChromeBrowserPolicyConnector : public BrowserPolicyConnector {
ConfigurationPolicyProvider* GetPlatformProvider(); ConfigurationPolicyProvider* GetPlatformProvider();
protected: protected:
// Called from Init() to build the list of ConfigurationPolicyProviders that // BrowserPolicyConnector:
// is supplied to SetPolicyProviders(). This implementation does nothing std::vector<std::unique_ptr<policy::ConfigurationPolicyProvider>>
// and is provided for subclasses. NOTE: |providers| may already contain CreatePolicyProviders() override;
// some providers, generally subclasses should append.
virtual void BuildPolicyProviders(
std::vector<std::unique_ptr<ConfigurationPolicyProvider>>* providers);
private: private:
std::unique_ptr<ConfigurationPolicyProvider> CreatePlatformProvider(); std::unique_ptr<ConfigurationPolicyProvider> CreatePlatformProvider();
......
...@@ -104,10 +104,7 @@ void ProfilePolicyConnector::Init( ...@@ -104,10 +104,7 @@ void ProfilePolicyConnector::Init(
} }
#endif #endif
std::unique_ptr<PolicyServiceImpl> policy_service = policy_service_ = std::make_unique<PolicyServiceImpl>(policy_providers_);
std::make_unique<PolicyServiceImpl>();
policy_service->SetProviders(policy_providers_);
policy_service_ = std::move(policy_service);
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
if (is_primary_user_) { if (is_primary_user_) {
......
...@@ -154,8 +154,7 @@ ProfilePolicyConnectorFactory::CreateForBrowserContextInternal( ...@@ -154,8 +154,7 @@ ProfilePolicyConnectorFactory::CreateForBrowserContextInternal(
providers.push_back(test_providers_.front()); providers.push_back(test_providers_.front());
test_providers_.pop_front(); test_providers_.pop_front();
std::unique_ptr<PolicyServiceImpl> service = std::unique_ptr<PolicyServiceImpl> service =
std::make_unique<PolicyServiceImpl>(); std::make_unique<PolicyServiceImpl>(std::move(providers));
service->SetProviders(providers);
connector->InitForTesting(std::move(service)); connector->InitForTesting(std::move(service));
} }
......
...@@ -98,8 +98,7 @@ class ProxyPolicyTest : public testing::Test { ...@@ -98,8 +98,7 @@ class ProxyPolicyTest : public testing::Test {
PolicyServiceImpl::Providers providers; PolicyServiceImpl::Providers providers;
providers.push_back(&provider_); providers.push_back(&provider_);
policy_service_ = std::make_unique<PolicyServiceImpl>(); policy_service_ = std::make_unique<PolicyServiceImpl>(std::move(providers));
policy_service_->SetProviders(providers);
provider_.Init(); provider_.Init();
} }
......
...@@ -800,8 +800,7 @@ void TestingProfile::CreateProfilePolicyConnector() { ...@@ -800,8 +800,7 @@ void TestingProfile::CreateProfilePolicyConnector() {
if (!policy_service_) { if (!policy_service_) {
std::vector<policy::ConfigurationPolicyProvider*> providers; std::vector<policy::ConfigurationPolicyProvider*> providers;
std::unique_ptr<policy::PolicyServiceImpl> policy_service = std::unique_ptr<policy::PolicyServiceImpl> policy_service =
std::make_unique<policy::PolicyServiceImpl>(); std::make_unique<policy::PolicyServiceImpl>(std::move(providers));
policy_service->SetProviders(providers);
policy_service_ = std::move(policy_service); policy_service_ = std::move(policy_service);
} }
profile_policy_connector_.reset(new policy::ProfilePolicyConnector()); profile_policy_connector_.reset(new policy::ProfilePolicyConnector());
......
...@@ -26,8 +26,7 @@ ConfigurationPolicyProvider* g_testing_provider = nullptr; ...@@ -26,8 +26,7 @@ ConfigurationPolicyProvider* g_testing_provider = nullptr;
} // namespace } // namespace
BrowserPolicyConnectorBase::BrowserPolicyConnectorBase( BrowserPolicyConnectorBase::BrowserPolicyConnectorBase(
const HandlerListFactory& handler_list_factory) const HandlerListFactory& handler_list_factory) {
: is_initialized_(false) {
// GetPolicyService() must be ready after the constructor is done. // GetPolicyService() must be ready after the constructor is done.
// The connector is created very early during startup, when the browser // The connector is created very early during startup, when the browser
// threads aren't running yet; initialize components that need local_state, // threads aren't running yet; initialize components that need local_state,
...@@ -56,10 +55,8 @@ void BrowserPolicyConnectorBase::Shutdown() { ...@@ -56,10 +55,8 @@ void BrowserPolicyConnectorBase::Shutdown() {
is_initialized_ = false; is_initialized_ = false;
if (g_testing_provider) if (g_testing_provider)
g_testing_provider->Shutdown(); g_testing_provider->Shutdown();
if (policy_providers_) { for (const auto& provider : policy_providers_)
for (const auto& provider : *policy_providers_)
provider->Shutdown(); provider->Shutdown();
}
// Drop g_testing_provider so that tests executed with --single_process can // Drop g_testing_provider so that tests executed with --single_process can
// call SetPolicyProviderForTesting() again. It is still owned by the test. // call SetPolicyProviderForTesting() again. It is still owned by the test.
g_testing_provider = nullptr; g_testing_provider = nullptr;
...@@ -75,12 +72,23 @@ CombinedSchemaRegistry* BrowserPolicyConnectorBase::GetSchemaRegistry() { ...@@ -75,12 +72,23 @@ CombinedSchemaRegistry* BrowserPolicyConnectorBase::GetSchemaRegistry() {
} }
PolicyService* BrowserPolicyConnectorBase::GetPolicyService() { PolicyService* BrowserPolicyConnectorBase::GetPolicyService() {
if (!policy_service_) { if (policy_service_)
return policy_service_.get();
DCHECK(!is_initialized_);
is_initialized_ = true;
policy_providers_ = CreatePolicyProviders();
if (g_testing_provider)
g_testing_provider->Init(GetSchemaRegistry());
for (const auto& provider : policy_providers_)
provider->Init(GetSchemaRegistry());
g_created_policy_service = true; g_created_policy_service = true;
policy_service_ = std::make_unique<PolicyServiceImpl>(); policy_service_ =
if (policy_providers_ || g_testing_provider) std::make_unique<PolicyServiceImpl>(GetProvidersForPolicyService());
policy_service_->SetProviders(GetProvidersForPolicyService());
}
return policy_service_.get(); return policy_service_.get();
} }
...@@ -111,32 +119,6 @@ BrowserPolicyConnectorBase::GetPolicyProviderForTesting() { ...@@ -111,32 +119,6 @@ BrowserPolicyConnectorBase::GetPolicyProviderForTesting() {
return g_testing_provider; return g_testing_provider;
} }
void BrowserPolicyConnectorBase::SetPolicyProviders(
std::vector<std::unique_ptr<ConfigurationPolicyProvider>> providers) {
// SetPolicyProviders() should only called once.
DCHECK(!is_initialized_);
policy_providers_ = std::move(providers);
if (g_testing_provider)
g_testing_provider->Init(GetSchemaRegistry());
for (const auto& provider : *policy_providers_)
provider->Init(GetSchemaRegistry());
is_initialized_ = true;
if (policy_service_) {
if (!policy_service_->has_providers()) {
policy_service_->SetProviders(GetProvidersForPolicyService());
} else {
// GetPolicyService() triggers calling SetProviders() if
// |g_testing_provider| has been set. That's the only way that should
// result in ending up in this branch.
DCHECK(g_testing_provider);
}
}
}
std::vector<ConfigurationPolicyProvider*> std::vector<ConfigurationPolicyProvider*>
BrowserPolicyConnectorBase::GetProvidersForPolicyService() { BrowserPolicyConnectorBase::GetProvidersForPolicyService() {
std::vector<ConfigurationPolicyProvider*> providers; std::vector<ConfigurationPolicyProvider*> providers;
...@@ -144,12 +126,17 @@ BrowserPolicyConnectorBase::GetProvidersForPolicyService() { ...@@ -144,12 +126,17 @@ BrowserPolicyConnectorBase::GetProvidersForPolicyService() {
providers.push_back(g_testing_provider); providers.push_back(g_testing_provider);
return providers; return providers;
} }
providers.reserve(policy_providers_->size()); providers.reserve(policy_providers_.size());
for (const auto& policy : *policy_providers_) for (const auto& policy : policy_providers_)
providers.push_back(policy.get()); providers.push_back(policy.get());
return providers; return providers;
} }
std::vector<std::unique_ptr<ConfigurationPolicyProvider>>
BrowserPolicyConnectorBase::CreatePolicyProviders() {
return {};
}
void BrowserPolicyConnectorBase::OnResourceBundleCreated() { void BrowserPolicyConnectorBase::OnResourceBundleCreated() {
std::vector<base::OnceClosure> resource_bundle_callbacks; std::vector<base::OnceClosure> resource_bundle_callbacks;
std::swap(resource_bundle_callbacks, resource_bundle_callbacks_); std::swap(resource_bundle_callbacks, resource_bundle_callbacks_);
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#include "base/callback_forward.h" #include "base/callback_forward.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/optional.h"
#include "components/policy/core/browser/configuration_policy_handler_list.h" #include "components/policy/core/browser/configuration_policy_handler_list.h"
#include "components/policy/core/common/schema.h" #include "components/policy/core/common/schema.h"
#include "components/policy/core/common/schema_registry.h" #include "components/policy/core/common/schema_registry.h"
...@@ -73,10 +72,11 @@ class POLICY_EXPORT BrowserPolicyConnectorBase { ...@@ -73,10 +72,11 @@ class POLICY_EXPORT BrowserPolicyConnectorBase {
explicit BrowserPolicyConnectorBase( explicit BrowserPolicyConnectorBase(
const HandlerListFactory& handler_list_factory); const HandlerListFactory& handler_list_factory);
// Sets the set of providers, in decreasing order of priority. May only be // Called from GetPolicyService() to create the set of
// called once. // ConfigurationPolicyProviders that are used, in decreasing order of
void SetPolicyProviders( // priority.
std::vector<std::unique_ptr<ConfigurationPolicyProvider>> providers); virtual std::vector<std::unique_ptr<ConfigurationPolicyProvider>>
CreatePolicyProviders();
// Must be called when ui::ResourceBundle has been loaded, results in running // Must be called when ui::ResourceBundle has been loaded, results in running
// any callbacks scheduled in NotifyWhenResourceBundleReady(). // any callbacks scheduled in NotifyWhenResourceBundleReady().
...@@ -88,8 +88,10 @@ class POLICY_EXPORT BrowserPolicyConnectorBase { ...@@ -88,8 +88,10 @@ class POLICY_EXPORT BrowserPolicyConnectorBase {
// called. // called.
std::vector<ConfigurationPolicyProvider*> GetProvidersForPolicyService(); std::vector<ConfigurationPolicyProvider*> GetProvidersForPolicyService();
// Whether SetPolicyProviders() but not Shutdown() has been invoked. // Set to true when the PolicyService has been created, and false in
bool is_initialized_; // Shutdown(). Once created the PolicyService is destroyed in the destructor,
// not Shutdown().
bool is_initialized_ = false;
// Used to convert policies to preferences. The providers declared below // Used to convert policies to preferences. The providers declared below
// may trigger policy updates during shutdown, which will result in // may trigger policy updates during shutdown, which will result in
...@@ -105,8 +107,7 @@ class POLICY_EXPORT BrowserPolicyConnectorBase { ...@@ -105,8 +107,7 @@ class POLICY_EXPORT BrowserPolicyConnectorBase {
CombinedSchemaRegistry schema_registry_; CombinedSchemaRegistry schema_registry_;
// The browser-global policy providers, in decreasing order of priority. // The browser-global policy providers, in decreasing order of priority.
base::Optional<std::vector<std::unique_ptr<ConfigurationPolicyProvider>>> std::vector<std::unique_ptr<ConfigurationPolicyProvider>> policy_providers_;
policy_providers_;
// Must be deleted before all the policy providers. // Must be deleted before all the policy providers.
std::unique_ptr<PolicyServiceImpl> policy_service_; std::unique_ptr<PolicyServiceImpl> policy_service_;
......
...@@ -30,8 +30,7 @@ ConfigurationPolicyPrefStoreTest::ConfigurationPolicyPrefStoreTest() ...@@ -30,8 +30,7 @@ ConfigurationPolicyPrefStoreTest::ConfigurationPolicyPrefStoreTest()
.WillRepeatedly(Return(false)); .WillRepeatedly(Return(false));
provider_.Init(); provider_.Init();
providers_.push_back(&provider_); providers_.push_back(&provider_);
policy_service_ = std::make_unique<PolicyServiceImpl>(); policy_service_ = std::make_unique<PolicyServiceImpl>(providers_);
policy_service_->SetProviders(providers_);
store_ = new ConfigurationPolicyPrefStore( store_ = new ConfigurationPolicyPrefStore(
nullptr, policy_service_.get(), &handler_list_, POLICY_LEVEL_MANDATORY); nullptr, policy_service_.get(), &handler_list_, POLICY_LEVEL_MANDATORY);
} }
......
...@@ -32,8 +32,7 @@ class ProxyPolicyHandlerTest ...@@ -32,8 +32,7 @@ class ProxyPolicyHandlerTest
// preprocessor. The previous store must be nulled out first so that it // preprocessor. The previous store must be nulled out first so that it
// removes itself from the service's observer list. // removes itself from the service's observer list.
store_ = nullptr; store_ = nullptr;
policy_service_ = std::make_unique<PolicyServiceImpl>(); policy_service_ = std::make_unique<PolicyServiceImpl>(providers_);
policy_service_->SetProviders(providers_);
store_ = new ConfigurationPolicyPrefStore( store_ = new ConfigurationPolicyPrefStore(
nullptr, policy_service_.get(), &handler_list_, POLICY_LEVEL_MANDATORY); nullptr, policy_service_.get(), &handler_list_, POLICY_LEVEL_MANDATORY);
} }
......
...@@ -72,25 +72,12 @@ void RemapProxyPolicies(PolicyMap* policies) { ...@@ -72,25 +72,12 @@ void RemapProxyPolicies(PolicyMap* policies) {
} // namespace } // namespace
PolicyServiceImpl::PolicyServiceImpl() : update_task_ptr_factory_(this) { PolicyServiceImpl::PolicyServiceImpl(Providers providers)
for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) : update_task_ptr_factory_(this) {
initialization_complete_[domain] = false;
}
PolicyServiceImpl::~PolicyServiceImpl() {
DCHECK(thread_checker_.CalledOnValidThread());
if (providers_) {
for (auto* provider : *providers_)
provider->RemoveObserver(this);
}
}
void PolicyServiceImpl::SetProviders(Providers providers) {
DCHECK(!providers_);
providers_ = std::move(providers); providers_ = std::move(providers);
for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain)
initialization_complete_[domain] = true; initialization_complete_[domain] = true;
for (auto* provider : *providers_) { for (auto* provider : providers_) {
provider->AddObserver(this); provider->AddObserver(this);
for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) { for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) {
initialization_complete_[domain] &= initialization_complete_[domain] &=
...@@ -102,6 +89,12 @@ void PolicyServiceImpl::SetProviders(Providers providers) { ...@@ -102,6 +89,12 @@ void PolicyServiceImpl::SetProviders(Providers providers) {
MergeAndTriggerUpdates(); MergeAndTriggerUpdates();
} }
PolicyServiceImpl::~PolicyServiceImpl() {
DCHECK(thread_checker_.CalledOnValidThread());
for (auto* provider : providers_)
provider->RemoveObserver(this);
}
void PolicyServiceImpl::AddObserver(PolicyDomain domain, void PolicyServiceImpl::AddObserver(PolicyDomain domain,
PolicyService::Observer* observer) { PolicyService::Observer* observer) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
...@@ -143,7 +136,7 @@ void PolicyServiceImpl::RefreshPolicies(const base::Closure& callback) { ...@@ -143,7 +136,7 @@ void PolicyServiceImpl::RefreshPolicies(const base::Closure& callback) {
if (!callback.is_null()) if (!callback.is_null())
refresh_callbacks_.push_back(callback); refresh_callbacks_.push_back(callback);
if (!providers_ || providers_->empty()) { if (providers_.empty()) {
// Refresh is immediately complete if there are no providers. See the note // Refresh is immediately complete if there are no providers. See the note
// on OnUpdatePolicy() about why this is a posted task. // on OnUpdatePolicy() about why this is a posted task.
update_task_ptr_factory_.InvalidateWeakPtrs(); update_task_ptr_factory_.InvalidateWeakPtrs();
...@@ -153,15 +146,15 @@ void PolicyServiceImpl::RefreshPolicies(const base::Closure& callback) { ...@@ -153,15 +146,15 @@ void PolicyServiceImpl::RefreshPolicies(const base::Closure& callback) {
} else { } else {
// Some providers might invoke OnUpdatePolicy synchronously while handling // Some providers might invoke OnUpdatePolicy synchronously while handling
// RefreshPolicies. Mark all as pending before refreshing. // RefreshPolicies. Mark all as pending before refreshing.
for (auto* provider : *providers_) for (auto* provider : providers_)
refresh_pending_.insert(provider); refresh_pending_.insert(provider);
for (auto* provider : *providers_) for (auto* provider : providers_)
provider->RefreshPolicies(); provider->RefreshPolicies();
} }
} }
void PolicyServiceImpl::OnUpdatePolicy(ConfigurationPolicyProvider* provider) { void PolicyServiceImpl::OnUpdatePolicy(ConfigurationPolicyProvider* provider) {
DCHECK_EQ(1, std::count(providers_->begin(), providers_->end(), provider)); DCHECK_EQ(1, std::count(providers_.begin(), providers_.end(), provider));
refresh_pending_.erase(provider); refresh_pending_.erase(provider);
// Note: a policy change may trigger further policy changes in some providers. // Note: a policy change may trigger further policy changes in some providers.
...@@ -194,14 +187,12 @@ void PolicyServiceImpl::MergeAndTriggerUpdates() { ...@@ -194,14 +187,12 @@ void PolicyServiceImpl::MergeAndTriggerUpdates() {
// Merge from each provider in their order of priority. // Merge from each provider in their order of priority.
const PolicyNamespace chrome_namespace(POLICY_DOMAIN_CHROME, std::string()); const PolicyNamespace chrome_namespace(POLICY_DOMAIN_CHROME, std::string());
PolicyBundle bundle; PolicyBundle bundle;
if (providers_) { for (auto* provider : providers_) {
for (auto* provider : *providers_) {
PolicyBundle provided_bundle; PolicyBundle provided_bundle;
provided_bundle.CopyFrom(provider->policies()); provided_bundle.CopyFrom(provider->policies());
RemapProxyPolicies(&provided_bundle.Get(chrome_namespace)); RemapProxyPolicies(&provided_bundle.Get(chrome_namespace));
bundle.MergeFrom(provided_bundle); bundle.MergeFrom(provided_bundle);
} }
}
// Swap first, so that observers that call GetPolicies() see the current // Swap first, so that observers that call GetPolicies() see the current
// values. // values.
...@@ -247,9 +238,6 @@ void PolicyServiceImpl::MergeAndTriggerUpdates() { ...@@ -247,9 +238,6 @@ void PolicyServiceImpl::MergeAndTriggerUpdates() {
void PolicyServiceImpl::CheckInitializationComplete() { void PolicyServiceImpl::CheckInitializationComplete() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
if (!providers_)
return;
// Check if all the providers just became initialized for each domain; if so, // Check if all the providers just became initialized for each domain; if so,
// notify that domain's observers. // notify that domain's observers.
for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) { for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) {
...@@ -259,7 +247,7 @@ void PolicyServiceImpl::CheckInitializationComplete() { ...@@ -259,7 +247,7 @@ void PolicyServiceImpl::CheckInitializationComplete() {
PolicyDomain policy_domain = static_cast<PolicyDomain>(domain); PolicyDomain policy_domain = static_cast<PolicyDomain>(domain);
bool all_complete = true; bool all_complete = true;
for (auto* provider : *providers_) { for (auto* provider : providers_) {
if (!provider->IsInitializationComplete(policy_domain)) { if (!provider->IsInitializationComplete(policy_domain)) {
all_complete = false; all_complete = false;
break; break;
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/observer_list.h" #include "base/observer_list.h"
#include "base/optional.h"
#include "base/threading/thread_checker.h" #include "base/threading/thread_checker.h"
#include "components/policy/core/common/configuration_policy_provider.h" #include "components/policy/core/common/configuration_policy_provider.h"
#include "components/policy/core/common/policy_bundle.h" #include "components/policy/core/common/policy_bundle.h"
...@@ -32,18 +31,11 @@ class POLICY_EXPORT PolicyServiceImpl ...@@ -32,18 +31,11 @@ class POLICY_EXPORT PolicyServiceImpl
public: public:
using Providers = std::vector<ConfigurationPolicyProvider*>; using Providers = std::vector<ConfigurationPolicyProvider*>;
// Creates a new PolicyServiceImpl, it is expected SetProviders() is called // Creates a new PolicyServiceImpl with the list of
// once to complete initialization. // ConfigurationPolicyProviders, in order of decreasing priority.
PolicyServiceImpl(); explicit PolicyServiceImpl(Providers providers);
~PolicyServiceImpl() override; ~PolicyServiceImpl() override;
// Sets the providers; see description of constructor for details.
void SetProviders(Providers providers);
// Returns true if SetProviders() was called.
bool has_providers() const { return providers_.has_value(); }
// PolicyService overrides: // PolicyService overrides:
void AddObserver(PolicyDomain domain, void AddObserver(PolicyDomain domain,
PolicyService::Observer* observer) override; PolicyService::Observer* observer) override;
...@@ -76,8 +68,8 @@ class POLICY_EXPORT PolicyServiceImpl ...@@ -76,8 +68,8 @@ class POLICY_EXPORT PolicyServiceImpl
// Invokes all the refresh callbacks if there are no more refreshes pending. // Invokes all the refresh callbacks if there are no more refreshes pending.
void CheckRefreshComplete(); void CheckRefreshComplete();
// The providers set via SetProviders(), in order of decreasing priority. // The providers, in order of decreasing priority.
base::Optional<Providers> providers_; Providers providers_;
// Maps each policy namespace to its current policies. // Maps each policy namespace to its current policies.
PolicyBundle policy_bundle_; PolicyBundle policy_bundle_;
......
...@@ -120,8 +120,7 @@ class PolicyServiceTest : public testing::Test { ...@@ -120,8 +120,7 @@ class PolicyServiceTest : public testing::Test {
providers.push_back(&provider0_); providers.push_back(&provider0_);
providers.push_back(&provider1_); providers.push_back(&provider1_);
providers.push_back(&provider2_); providers.push_back(&provider2_);
policy_service_ = std::make_unique<PolicyServiceImpl>(); policy_service_ = std::make_unique<PolicyServiceImpl>(std::move(providers));
policy_service_->SetProviders(providers);
} }
void TearDown() override { void TearDown() override {
...@@ -561,8 +560,7 @@ TEST_F(PolicyServiceTest, IsInitializationComplete) { ...@@ -561,8 +560,7 @@ TEST_F(PolicyServiceTest, IsInitializationComplete) {
providers.push_back(&provider0_); providers.push_back(&provider0_);
providers.push_back(&provider1_); providers.push_back(&provider1_);
providers.push_back(&provider2_); providers.push_back(&provider2_);
policy_service_ = std::make_unique<PolicyServiceImpl>(); policy_service_ = std::make_unique<PolicyServiceImpl>(std::move(providers));
policy_service_->SetProviders(providers);
EXPECT_FALSE(policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); EXPECT_FALSE(policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
EXPECT_FALSE( EXPECT_FALSE(
policy_service_->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS)); policy_service_->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS));
......
...@@ -376,8 +376,7 @@ std::unique_ptr<PolicyWatcher> PolicyWatcher::CreateFromPolicyLoader( ...@@ -376,8 +376,7 @@ std::unique_ptr<PolicyWatcher> PolicyWatcher::CreateFromPolicyLoader(
policy::PolicyServiceImpl::Providers providers; policy::PolicyServiceImpl::Providers providers;
providers.push_back(policy_provider.get()); providers.push_back(policy_provider.get());
std::unique_ptr<policy::PolicyServiceImpl> policy_service = std::unique_ptr<policy::PolicyServiceImpl> policy_service =
std::make_unique<policy::PolicyServiceImpl>(); std::make_unique<policy::PolicyServiceImpl>(std::move(providers));
policy_service->SetProviders(providers);
policy::PolicyService* borrowed_policy_service = policy_service.get(); policy::PolicyService* borrowed_policy_service = policy_service.get();
return base::WrapUnique(new PolicyWatcher( return base::WrapUnique(new PolicyWatcher(
......
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