Chromium Code Reviews| Index: chrome/browser/policy/configuration_policy_reader.cc |
| diff --git a/chrome/browser/policy/configuration_policy_reader.cc b/chrome/browser/policy/configuration_policy_reader.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8d62e3064025388d53619c27814ac14a2c2e628b |
| --- /dev/null |
| +++ b/chrome/browser/policy/configuration_policy_reader.cc |
| @@ -0,0 +1,266 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/policy/configuration_policy_reader.h" |
| + |
| +#include <map> |
| +#include <vector> |
| +#include <set> |
|
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
alphabetize
simo
2011/08/10 14:28:19
Done.
|
| + |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/policy/browser_policy_connector.h" |
| +#include "chrome/browser/policy/configuration_policy_pref_store.h" |
| +#include "chrome/browser/policy/configuration_policy_store_interface.h" |
| + |
| +namespace policy { |
| + |
|
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
explain what the class is good for.
simo
2011/08/10 14:28:19
Done.
|
| +class ConfigurationPolicyStatusKeeper |
| + : public ConfigurationPolicyStoreInterface { |
| + |
|
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
no newline here
simo
2011/08/10 14:28:19
Done.
|
| + public: |
|
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
indentation
simo
2011/08/10 14:28:19
Done.
|
| + explicit ConfigurationPolicyStatusKeeper( |
|
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
explicit only for one-argument constructors.
simo
2011/08/10 14:28:19
Done.
|
| + ConfigurationPolicyProvider* provider, |
| + bool managed); |
| + virtual ~ConfigurationPolicyStatusKeeper(); |
| + |
| + // ConfigurationPolicyStoreInterface methods. |
| + virtual void Apply(ConfigurationPolicyType policy, base::Value* value); |
| + |
| + // Returns a pointer to a DictionaryValue containing the status information |
| + // of the policy |policy|. The caller acquires ownership of the returned |
| + // value. Returns NULL if no such policy is stored in this keeper. |
| + DictionaryValue* GetPolicyStatus(ConfigurationPolicyType policy) const; |
| + |
| + private: |
| + typedef std::map<ConfigurationPolicyType, string16> PolicyNameMapType; |
| + typedef ConfigurationPolicyProvider::PolicyDefinitionList |
| + PolicyDefinitionList; |
| + |
| + // Mapping from ConfigurationPolicyType to PolicyStatusInfo*. |
| + PolicyStatusMap policy_map_; |
| + |
| + // Mapping from ConfigurationPolicyType to policy names. |
| + PolicyNameMapType policy_name_map_; |
| + |
| + // The level of the policies stored in this keeper (mandatory or |
| + // recommended). |
| + PolicyStatusInfo::PolicyLevel policy_level_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyStatusKeeper); |
| +}; |
| + |
| +// ConfigurationPolicyStatusKeeper |
| +ConfigurationPolicyStatusKeeper::ConfigurationPolicyStatusKeeper( |
| + ConfigurationPolicyProvider* provider, |
| + bool managed) { |
| + |
|
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
no newline here.
simo
2011/08/10 14:28:19
Done.
|
| + const PolicyDefinitionList* policy_definition_list = |
| + ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(); |
| + |
| + // Create a mapping from ConfigurationPolicyType's to actual policy names. |
| + const PolicyDefinitionList::Entry* entry = policy_definition_list->begin; |
| + for ( ; entry != policy_definition_list->end; ++entry) |
| + policy_name_map_[entry->policy_type] = ASCIIToUTF16(entry->name); |
| + |
| + policy_level_ = managed ? |
| + PolicyStatusInfo::MANDATORY : PolicyStatusInfo::RECOMMENDED; |
| + |
| + if (!provider->Provide(this)) |
| + LOG(WARNING) << "Failed to get policy from provider."; |
| +} |
| + |
| +ConfigurationPolicyStatusKeeper::~ConfigurationPolicyStatusKeeper() { |
| + policy_map_.Clear(); |
| + policy_name_map_.clear(); |
| +} |
| + |
| +void ConfigurationPolicyStatusKeeper::Apply( |
| + ConfigurationPolicyType policy, base::Value* value) { |
| + |
| + PolicyNameMapType::const_iterator entry = policy_name_map_.find(policy); |
| + |
| + // This should never happen. |
| + if (entry == policy_name_map_.end()) { |
| + LOG(WARNING) << "Received unknown policy from provider."; |
| + return; |
| + } |
| + |
| + // TODO(simo) actually determine whether the policy is a user or a device one |
| + // and whether the policy could be enforced or not once this information |
| + // is available. |
| + PolicyStatusInfo* info = new PolicyStatusInfo(entry->second, |
| + PolicyStatusInfo::USER, |
| + policy_level_, |
| + value, |
| + PolicyStatusInfo::ENFORCED, |
| + string16()); |
| + policy_map_.Set(policy, info); |
| +} |
| + |
| +DictionaryValue* ConfigurationPolicyStatusKeeper::GetPolicyStatus( |
| + ConfigurationPolicyType policy) const { |
| + const PolicyStatusInfo* info = policy_map_.Get(policy); |
| + return info ? info->GetDictionaryValue() : NULL; |
| +} |
| + |
| +// ConfigurationPolicyReader |
| +ConfigurationPolicyReader::~ConfigurationPolicyReader() { |
| +} |
| + |
| +bool ConfigurationPolicyReader::IsInitializationComplete() const { |
| + return initialization_complete_; |
| +} |
| + |
| +void ConfigurationPolicyReader::OnUpdatePolicy() { |
| + Refresh(); |
| +} |
| + |
| +void ConfigurationPolicyReader::OnProviderGoingAway() { |
| + provider_ = NULL; |
| +} |
| + |
| +// static |
| +ConfigurationPolicyReader* |
| + ConfigurationPolicyReader::CreateManagedPlatformPolicyReader(bool managed) { |
| + BrowserPolicyConnector* connector = |
| + g_browser_process->browser_policy_connector(); |
| + return new ConfigurationPolicyReader( |
| + connector->GetManagedPlatformProvider(), managed); |
| +} |
| + |
| +// static |
| +ConfigurationPolicyReader* |
| + ConfigurationPolicyReader::CreateManagedCloudPolicyReader(bool managed) { |
| + BrowserPolicyConnector* connector = |
| + g_browser_process->browser_policy_connector(); |
| + return new ConfigurationPolicyReader( |
| + connector->GetManagedCloudProvider(), managed); |
| +} |
| + |
| +// static |
| +ConfigurationPolicyReader* |
| + ConfigurationPolicyReader::CreateRecommendedPlatformPolicyReader( |
| + bool managed) { |
| + BrowserPolicyConnector* connector = |
| + g_browser_process->browser_policy_connector(); |
| + return new ConfigurationPolicyReader( |
| + connector->GetRecommendedPlatformProvider(), managed); |
| +} |
| + |
| +// static |
| +ConfigurationPolicyReader* |
| + ConfigurationPolicyReader::CreateRecommendedCloudPolicyReader( |
| + bool managed) { |
| + BrowserPolicyConnector* connector = |
| + g_browser_process->browser_policy_connector(); |
| + return new ConfigurationPolicyReader( |
| + connector->GetRecommendedCloudProvider(), managed); |
| +} |
| + |
| +DictionaryValue* ConfigurationPolicyReader::GetPolicyStatus( |
| + ConfigurationPolicyType policy) const { |
| + return policy_keeper_.get()->GetPolicyStatus(policy); |
| +} |
| + |
| +ConfigurationPolicyReader::ConfigurationPolicyReader( |
| + ConfigurationPolicyProvider* provider, |
| + bool managed) |
| + : provider_(provider), |
| + initialization_complete_(false), |
| + managed_policies_(managed) { |
| + if (provider_) { |
| + // Read initial policy. |
| + policy_keeper_.reset( |
| + new ConfigurationPolicyStatusKeeper(provider, managed)); |
| + registrar_.Init(provider_, this); |
| + initialization_complete_ = provider->IsInitializationComplete(); |
| + } else { |
| + initialization_complete_ = true; |
| + } |
| +} |
| + |
| +void ConfigurationPolicyReader::Refresh() { |
| + if (!provider_) |
| + return; |
| + |
| + // Read policy state into a new keeper and swap out old keeper. |
| + scoped_ptr<ConfigurationPolicyStatusKeeper> new_keeper( |
| + new ConfigurationPolicyStatusKeeper(provider_, managed_policies_)); |
| + policy_keeper_.reset(new_keeper.release()); |
| + |
| + // TODO(simo) create an observer class which PolicyStatus can implement so |
| + // that it can be notified of changes. |
| +} |
| + |
| +// PolicyStatus |
| +PolicyStatus::PolicyStatus() { |
| + managed_platform_.reset( |
| + ConfigurationPolicyReader::CreateManagedPlatformPolicyReader(true)); |
| + managed_cloud_.reset( |
| + ConfigurationPolicyReader::CreateManagedCloudPolicyReader(true)); |
| + recommended_platform_.reset( |
| + ConfigurationPolicyReader::CreateRecommendedPlatformPolicyReader(false)); |
| + recommended_cloud_.reset( |
| + ConfigurationPolicyReader::CreateRecommendedCloudPolicyReader(false)); |
| +} |
| + |
| +PolicyStatus::~PolicyStatus() { |
| +} |
| + |
| +ListValue* PolicyStatus::GetPolicyStatusList() const { |
| + ListValue* result = new ListValue(); |
| + std::vector<PolicyStatusInfo*> unsent_policies; |
| + |
| + const PolicyDefinitionList* supported_policies = |
| + ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(); |
| + const PolicyDefinitionList::Entry* policy = supported_policies->begin; |
| + for ( ; policy != supported_policies->end; ++policy) { |
| + if (!AddPolicyFromReaders(policy->policy_type, result)) { |
| + PolicyStatusInfo* info = new PolicyStatusInfo( |
| + ASCIIToUTF16(policy->name), |
| + PolicyStatusInfo::SOURCE_TYPE_UNDEFINED, |
| + PolicyStatusInfo::LEVEL_UNDEFINED, |
| + Value::CreateNullValue(), |
| + PolicyStatusInfo::STATUS_UNDEFINED, |
| + string16()); |
| + unsent_policies.push_back(info); |
| + } |
| + } |
| + |
| + // Add policies that weren't actually sent from providers to list. |
| + std::vector<PolicyStatusInfo*>::const_iterator info = unsent_policies.begin(); |
| + for ( ; info != unsent_policies.end(); ++info) |
| + result->Append((*info)->GetDictionaryValue()); |
| + |
|
simo
2011/08/09 21:03:02
I am leaking the PolicyStatusInfo pointers here, w
|
| + return result; |
| +} |
| + |
| +bool PolicyStatus::AddPolicyFromReaders( |
| + ConfigurationPolicyType policy, ListValue* list) const { |
| + |
|
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
no newline
simo
2011/08/10 14:28:19
Done.
|
| + DictionaryValue* mp_policy = managed_platform_.get()->GetPolicyStatus(policy); |
|
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
you are leaking all these DictionaryValue pointers
simo
2011/08/09 21:03:02
I thought I saw in base/values.h that the pointers
|
| + DictionaryValue* mc_policy = |
| + managed_cloud_.get()->GetPolicyStatus(policy); |
| + DictionaryValue* rp_policy = |
| + recommended_platform_.get()->GetPolicyStatus(policy); |
| + DictionaryValue* rc_policy = |
| + recommended_cloud_.get()->GetPolicyStatus(policy); |
| + |
| + bool added_policy = mp_policy || mc_policy || rp_policy || rc_policy; |
| + |
| + if (mp_policy) |
| + list->Append(mp_policy); |
| + if (mc_policy) |
| + list->Append(mc_policy); |
| + if (rp_policy) |
| + list->Append(rp_policy); |
| + if (rc_policy) |
| + list->Append(rc_policy); |
| + |
| + return added_policy; |
| +} |
| + |
| +} // policy |
|
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
namespace policy
simo
2011/08/10 14:28:19
Done.
|
| + |
|
Mattias Nissler (ping if slow)
2011/08/09 13:20:40
remove trailing newline
simo
2011/08/10 14:28:19
Done.
|