Chromium Code Reviews| Index: chrome/browser/policy/cloud_policy_cache_base.cc |
| diff --git a/chrome/browser/policy/cloud_policy_cache_base.cc b/chrome/browser/policy/cloud_policy_cache_base.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ef429ad40424dff575c12ceda8baa4c4f9a85298 |
| --- /dev/null |
| +++ b/chrome/browser/policy/cloud_policy_cache_base.cc |
| @@ -0,0 +1,131 @@ |
| +// 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/cloud_policy_cache_base.h" |
| + |
| +#include <string> |
| + |
| +#include "base/logging.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/policy/configuration_policy_pref_store.h" |
| + |
| +namespace policy { |
| + |
| +// A thin ConfigurationPolicyProvider implementation sitting on top of |
| +// CloudPolicyCache for hooking up with ConfigurationPolicyPrefStore. |
| +class CloudPolicyCacheBase::CloudPolicyProvider |
| + : public ConfigurationPolicyProvider { |
| + public: |
| + CloudPolicyProvider(const PolicyDefinitionList* policy_list, |
| + CloudPolicyCacheBase* cache, |
| + CloudPolicyCacheBase::PolicyLevel level) |
| + : ConfigurationPolicyProvider(policy_list), |
| + cache_(cache), |
| + level_(level) {} |
| + virtual ~CloudPolicyProvider() {} |
| + |
| + virtual bool Provide(ConfigurationPolicyStoreInterface* store) { |
| + if (level_ == POLICY_LEVEL_MANDATORY) |
| + ApplyPolicyMap(&cache_->mandatory_policy_, store); |
| + else if (level_ == POLICY_LEVEL_RECOMMENDED) |
| + ApplyPolicyMap(&cache_->recommended_policy_, store); |
| + return true; |
| + } |
| + |
| + virtual bool IsInitializationComplete() const { |
| + return cache_->initialization_complete_; |
| + } |
| + |
| + virtual void AddObserver(ConfigurationPolicyProvider::Observer* observer) { |
| + cache_->observer_list_.AddObserver(observer); |
| + } |
| + virtual void RemoveObserver(ConfigurationPolicyProvider::Observer* observer) { |
| + cache_->observer_list_.RemoveObserver(observer); |
| + } |
| + |
| + private: |
| + // The underlying policy cache. |
| + CloudPolicyCacheBase* cache_; |
| + // Policy level this provider will handle. |
| + CloudPolicyCacheBase::PolicyLevel level_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(CloudPolicyProvider); |
| +}; |
| + |
| +CloudPolicyCacheBase::CloudPolicyCacheBase() |
| + : initialization_complete_(false), |
| + is_unmanaged_(false) { |
| + managed_policy_provider_.reset( |
| + new CloudPolicyProvider( |
| + ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), |
| + this, |
| + POLICY_LEVEL_MANDATORY)); |
| + recommended_policy_provider_.reset( |
| + new CloudPolicyProvider( |
| + ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), |
| + this, |
| + POLICY_LEVEL_RECOMMENDED)); |
| +} |
| + |
| +CloudPolicyCacheBase::~CloudPolicyCacheBase() { |
| + FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer, |
| + observer_list_, OnProviderGoingAway()); |
| +} |
| + |
| +bool CloudPolicyCacheBase::SetPolicyInternal( |
| + const em::PolicyFetchResponse& policy) { |
| + DCHECK(CalledOnValidThread()); |
| + bool initialization_was_not_complete = !initialization_complete_; |
| + is_unmanaged_ = false; |
| + base::Time timestamp; |
| + PolicyMap mandatory_policy; |
| + PolicyMap recommended_policy; |
| + bool ok = DecodePolicyResponse(policy, &mandatory_policy, &recommended_policy, |
| + ×tamp); |
| + if (!ok) |
| + return false; |
| + |
| + const bool new_policy_differs = |
| + !mandatory_policy_.Equals(mandatory_policy) || |
| + !recommended_policy_.Equals(recommended_policy); |
| + mandatory_policy_.Swap(&mandatory_policy); |
| + recommended_policy_.Swap(&recommended_policy); |
| + initialization_complete_ = true; |
| + last_policy_refresh_time_ = timestamp; |
|
Mattias Nissler (ping if slow)
2011/03/24 18:55:57
This does have the same bogus-timestamp bug we've
Jakob Kummerow
2011/03/28 13:53:53
In this patch set: yes; it's already fixed in my l
|
| + |
| + if (new_policy_differs || initialization_was_not_complete) { |
| + FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer, |
| + observer_list_, OnUpdatePolicy()); |
| + } |
| + return true; |
| +} |
| + |
| +ConfigurationPolicyProvider* CloudPolicyCacheBase::GetManagedPolicyProvider() { |
| + DCHECK(CalledOnValidThread()); |
| + return managed_policy_provider_.get(); |
| +} |
| + |
| +ConfigurationPolicyProvider* |
| + CloudPolicyCacheBase::GetRecommendedPolicyProvider() { |
| + DCHECK(CalledOnValidThread()); |
| + return recommended_policy_provider_.get(); |
| +} |
| + |
| +bool CloudPolicyCacheBase::DecodePolicyResponse( |
| + const em::PolicyFetchResponse& policy_response, |
| + PolicyMap* mandatory, |
| + PolicyMap* recommended, |
| + base::Time* timestamp) { |
| + std::string data = policy_response.policy_data(); |
| + em::PolicyData policy_data; |
| + if (!policy_data.ParseFromString(data)) { |
| + LOG(WARNING) << "Failed to parse PolicyData protobuf."; |
| + return false; |
| + } |
| + *timestamp = base::Time::UnixEpoch() + |
| + base::TimeDelta::FromMilliseconds(policy_data.timestamp()); |
| + return DecodePolicyData(policy_data, mandatory, recommended); |
| +} |
| + |
| +} // namespace policy |