Refactored ConfigurationPolicyPrefStore to not depend on chrome/.

Some of the chrome/browser/policy code will be moved to a new layered component.
This change prepares the ConfigurationPolicyPrefStore class for the move, by
removing its dependency on g_browser_process and BrowserThread.

BUG=271392

Review URL: https://chromiumcodereview.appspot.com/22812002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@217084 0039d316-1c4b-4281-b951-d872f2087c98
parent ef686fc3
......@@ -80,11 +80,6 @@ specific_include_rules = {
"+chrome/common/pref_names.h",
],
r"configuration_policy_pref_store\.cc": [
"+chrome/browser/browser_process.h",
"+content/public/browser/browser_thread.h",
],
r"configuration_policy_pref_store_unittest\.cc": [
"+chrome/browser/prefs/incognito_mode_prefs.h",
"+chrome/browser/prefs/proxy_config_dictionary.h",
......
......@@ -9,18 +9,14 @@
#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/prefs/pref_value_map.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/policy/browser_policy_connector.h"
#include "chrome/browser/policy/configuration_policy_handler_list.h"
#include "chrome/browser/policy/policy_error_map.h"
#include "content/public/browser/browser_thread.h"
#include "policy/policy_constants.h"
using content::BrowserThread;
namespace policy {
namespace {
......@@ -40,8 +36,10 @@ void LogErrors(PolicyErrorMap* errors) {
ConfigurationPolicyPrefStore::ConfigurationPolicyPrefStore(
PolicyService* service,
const ConfigurationPolicyHandlerList* handler_list,
PolicyLevel level)
: policy_service_(service),
handler_list_(handler_list),
level_(level) {
// Read initial policy.
prefs_.reset(CreatePreferencesFromPolicies());
......@@ -93,22 +91,6 @@ void ConfigurationPolicyPrefStore::OnPolicyServiceInitialized(
}
}
// static
ConfigurationPolicyPrefStore*
ConfigurationPolicyPrefStore::CreateMandatoryPolicyPrefStore(
PolicyService* policy_service) {
return new ConfigurationPolicyPrefStore(policy_service,
POLICY_LEVEL_MANDATORY);
}
// static
ConfigurationPolicyPrefStore*
ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore(
PolicyService* policy_service) {
return new ConfigurationPolicyPrefStore(policy_service,
POLICY_LEVEL_RECOMMENDED);
}
ConfigurationPolicyPrefStore::~ConfigurationPolicyPrefStore() {
policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, this);
}
......@@ -137,18 +119,14 @@ PrefValueMap* ConfigurationPolicyPrefStore::CreatePreferencesFromPolicies() {
scoped_ptr<PolicyErrorMap> errors(new PolicyErrorMap);
const ConfigurationPolicyHandlerList* handler_list =
g_browser_process->browser_policy_connector()->GetHandlerList();
handler_list->ApplyPolicySettings(filtered_policies,
handler_list_->ApplyPolicySettings(filtered_policies,
prefs.get(),
errors.get());
// Retrieve and log the errors once the UI loop is ready. This is only an
// issue during startup.
BrowserThread::PostTask(BrowserThread::UI,
FROM_HERE,
base::Bind(&LogErrors,
base::Owned(errors.release())));
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(&LogErrors, base::Owned(errors.release())));
return prefs.release();
}
......
......@@ -14,11 +14,14 @@
#include "base/values.h"
#include "chrome/browser/policy/policy_map.h"
#include "chrome/browser/policy/policy_service.h"
#include "chrome/browser/policy/policy_types.h"
class PrefValueMap;
namespace policy {
class ConfigurationPolicyHandlerList;
// An implementation of PrefStore that bridges policy settings as read from the
// PolicyService to preferences. Converts POLICY_DOMAIN_CHROME policies a given
// PolicyLevel to their corresponding preferences.
......@@ -26,9 +29,12 @@ class ConfigurationPolicyPrefStore
: public PrefStore,
public PolicyService::Observer {
public:
// Does not take ownership of |service|. Only policies of the given |level|
// will be mapped.
ConfigurationPolicyPrefStore(PolicyService* service, PolicyLevel level);
// Does not take ownership of |service| nor |handler_list|, which must outlive
// the store. Only policies of the given |level| will be mapped.
ConfigurationPolicyPrefStore(
PolicyService* service,
const ConfigurationPolicyHandlerList* handler_list,
PolicyLevel level);
// PrefStore methods:
virtual void AddObserver(PrefStore::Observer* observer) OVERRIDE;
......@@ -44,16 +50,6 @@ class ConfigurationPolicyPrefStore
const PolicyMap& current) OVERRIDE;
virtual void OnPolicyServiceInitialized(PolicyDomain domain) OVERRIDE;
// Creates a ConfigurationPolicyPrefStore that only provides policies that
// have POLICY_LEVEL_MANDATORY level.
static ConfigurationPolicyPrefStore* CreateMandatoryPolicyPrefStore(
PolicyService* policy_service);
// Creates a ConfigurationPolicyPrefStore that only provides policies that
// have POLICY_LEVEL_RECOMMENDED level.
static ConfigurationPolicyPrefStore* CreateRecommendedPolicyPrefStore(
PolicyService* policy_service);
private:
virtual ~ConfigurationPolicyPrefStore();
......@@ -68,6 +64,10 @@ class ConfigurationPolicyPrefStore
// The PolicyService from which policy settings are read.
PolicyService* policy_service_;
// The policy handlers used to convert policies into their corresponding
// preferences.
const ConfigurationPolicyHandlerList* handler_list_;
// The policy level that this PrefStore uses.
PolicyLevel level_;
......
......@@ -11,6 +11,7 @@
#include "base/prefs/pref_store_observer_mock.h"
#include "base/run_loop.h"
#include "chrome/browser/policy/configuration_policy_handler.h"
#include "chrome/browser/policy/configuration_policy_handler_list.h"
#include "chrome/browser/policy/configuration_policy_pref_store.h"
#include "chrome/browser/policy/external_data_fetcher.h"
#include "chrome/browser/policy/mock_configuration_policy_provider.h"
......@@ -55,6 +56,7 @@ class ConfigurationPolicyPrefStoreTest : public testing::Test {
providers.push_back(&provider_);
policy_service_.reset(new PolicyServiceImpl(providers));
store_ = new ConfigurationPolicyPrefStore(policy_service_.get(),
&handler_list_,
POLICY_LEVEL_MANDATORY);
}
......@@ -68,6 +70,7 @@ class ConfigurationPolicyPrefStoreTest : public testing::Test {
loop.RunUntilIdle();
}
ConfigurationPolicyHandlerList handler_list_;
MockConfigurationPolicyProvider provider_;
scoped_ptr<PolicyServiceImpl> policy_service_;
scoped_refptr<ConfigurationPolicyPrefStore> store_;
......
......@@ -15,7 +15,7 @@
#include "base/prefs/pref_registry.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/pref_value_store.h"
#include "chrome/browser/policy/configuration_policy_pref_store.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/prefs/command_line_pref_store.h"
#include "chrome/browser/prefs/pref_model_associator.h"
#include "chrome/browser/prefs/pref_service_syncable_builder.h"
......@@ -26,6 +26,12 @@
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#if defined(ENABLE_CONFIGURATION_POLICY)
#include "chrome/browser/policy/browser_policy_connector.h"
#include "chrome/browser/policy/configuration_policy_pref_store.h"
#include "chrome/browser/policy/policy_types.h"
#endif
using content::BrowserContext;
using content::BrowserThread;
......@@ -75,12 +81,14 @@ void PrepareBuilder(
#if defined(ENABLE_CONFIGURATION_POLICY)
using policy::ConfigurationPolicyPrefStore;
builder->WithManagedPrefs(
ConfigurationPolicyPrefStore::CreateMandatoryPolicyPrefStore(
policy_service));
builder->WithRecommendedPrefs(
ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore(
policy_service));
builder->WithManagedPrefs(new ConfigurationPolicyPrefStore(
policy_service,
g_browser_process->browser_policy_connector()->GetHandlerList(),
policy::POLICY_LEVEL_MANDATORY));
builder->WithRecommendedPrefs(new ConfigurationPolicyPrefStore(
policy_service,
g_browser_process->browser_policy_connector()->GetHandlerList(),
policy::POLICY_LEVEL_RECOMMENDED));
#endif // ENABLE_CONFIGURATION_POLICY
builder->WithAsync(async);
......
......@@ -8,12 +8,18 @@
#include "base/prefs/default_pref_store.h"
#include "base/prefs/pref_notifier_impl.h"
#include "base/prefs/pref_value_store.h"
#include "chrome/browser/policy/configuration_policy_pref_store.h"
#include "chrome/browser/policy/policy_service.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/prefs/command_line_pref_store.h"
#include "chrome/browser/prefs/pref_service_syncable.h"
#include "components/user_prefs/pref_registry_syncable.h"
#if defined(ENABLE_CONFIGURATION_POLICY)
#include "chrome/browser/policy/browser_policy_connector.h"
#include "chrome/browser/policy/configuration_policy_pref_store.h"
#include "chrome/browser/policy/policy_service.h"
#include "chrome/browser/policy/policy_types.h"
#endif
PrefServiceSyncableBuilder::PrefServiceSyncableBuilder() {
}
......@@ -24,14 +30,18 @@ PrefServiceSyncableBuilder::~PrefServiceSyncableBuilder() {
PrefServiceSyncableBuilder& PrefServiceSyncableBuilder::WithManagedPolicies(
policy::PolicyService* service) {
WithManagedPrefs(new policy::ConfigurationPolicyPrefStore(
service, policy::POLICY_LEVEL_MANDATORY));
service,
g_browser_process->browser_policy_connector()->GetHandlerList(),
policy::POLICY_LEVEL_MANDATORY));
return *this;
}
PrefServiceSyncableBuilder& PrefServiceSyncableBuilder::WithRecommendedPolicies(
policy::PolicyService* service) {
WithRecommendedPrefs(new policy::ConfigurationPolicyPrefStore(
service, policy::POLICY_LEVEL_RECOMMENDED));
service,
g_browser_process->browser_policy_connector()->GetHandlerList(),
policy::POLICY_LEVEL_RECOMMENDED));
return *this;
}
#endif
......
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