| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/policy/cloud_policy_provider_impl.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "chrome/browser/policy/browser_policy_connector.h" | |
| 10 #include "chrome/browser/policy/configuration_policy_pref_store.h" | |
| 11 | |
| 12 namespace policy { | |
| 13 | |
| 14 CloudPolicyProviderImpl::CloudPolicyProviderImpl( | |
| 15 BrowserPolicyConnector* browser_policy_connector, | |
| 16 const PolicyDefinitionList* policy_list, | |
| 17 PolicyLevel level) | |
| 18 : CloudPolicyProvider(policy_list), | |
| 19 browser_policy_connector_(browser_policy_connector), | |
| 20 level_(level), | |
| 21 initialization_complete_(true) {} | |
| 22 | |
| 23 CloudPolicyProviderImpl::~CloudPolicyProviderImpl() { | |
| 24 for (ListType::iterator i = caches_.begin(); i != caches_.end(); ++i) | |
| 25 (*i)->RemoveObserver(this); | |
| 26 } | |
| 27 | |
| 28 bool CloudPolicyProviderImpl::ProvideInternal(PolicyMap* result) { | |
| 29 result->CopyFrom(combined_); | |
| 30 return true; | |
| 31 } | |
| 32 | |
| 33 bool CloudPolicyProviderImpl::IsInitializationComplete() const { | |
| 34 return initialization_complete_; | |
| 35 } | |
| 36 | |
| 37 void CloudPolicyProviderImpl::RefreshPolicies() { | |
| 38 pending_update_caches_ = caches_; | |
| 39 if (pending_update_caches_.empty()) | |
| 40 NotifyPolicyUpdated(); | |
| 41 else | |
| 42 browser_policy_connector_->FetchCloudPolicy(); | |
| 43 } | |
| 44 | |
| 45 void CloudPolicyProviderImpl::OnCacheUpdate(CloudPolicyCacheBase* cache) { | |
| 46 RemoveCache(cache, &pending_update_caches_); | |
| 47 RecombineCachesAndTriggerUpdate(); | |
| 48 } | |
| 49 | |
| 50 void CloudPolicyProviderImpl::OnCacheGoingAway(CloudPolicyCacheBase* cache) { | |
| 51 cache->RemoveObserver(this); | |
| 52 RemoveCache(cache, &caches_); | |
| 53 RemoveCache(cache, &pending_update_caches_); | |
| 54 RecombineCachesAndTriggerUpdate(); | |
| 55 } | |
| 56 | |
| 57 void CloudPolicyProviderImpl::AppendCache(CloudPolicyCacheBase* cache) { | |
| 58 initialization_complete_ &= cache->IsReady(); | |
| 59 cache->AddObserver(this); | |
| 60 caches_.push_back(cache); | |
| 61 RecombineCachesAndTriggerUpdate(); | |
| 62 } | |
| 63 | |
| 64 void CloudPolicyProviderImpl::PrependCache(CloudPolicyCacheBase* cache) { | |
| 65 initialization_complete_ &= cache->IsReady(); | |
| 66 cache->AddObserver(this); | |
| 67 caches_.insert(caches_.begin(), cache); | |
| 68 RecombineCachesAndTriggerUpdate(); | |
| 69 } | |
| 70 | |
| 71 void CloudPolicyProviderImpl::RecombineCachesAndTriggerUpdate() { | |
| 72 // Re-check whether all caches are ready. | |
| 73 if (!initialization_complete_) { | |
| 74 bool all_caches_ready = true; | |
| 75 for (ListType::const_iterator i = caches_.begin(); | |
| 76 i != caches_.end(); ++i) { | |
| 77 if (!(*i)->IsReady()) { | |
| 78 all_caches_ready = false; | |
| 79 break; | |
| 80 } | |
| 81 } | |
| 82 if (all_caches_ready) | |
| 83 initialization_complete_ = true; | |
| 84 } | |
| 85 | |
| 86 // Reconstruct the merged policy map. | |
| 87 PolicyMap newly_combined; | |
| 88 PolicyMap cache_policies; | |
| 89 for (ListType::iterator i = caches_.begin(); i != caches_.end(); ++i) { | |
| 90 if (!(*i)->IsReady()) | |
| 91 continue; | |
| 92 cache_policies.CopyFrom(*(*i)->policy()); | |
| 93 FixDeprecatedPolicies(&cache_policies); | |
| 94 newly_combined.MergeFrom(cache_policies); | |
| 95 } | |
| 96 | |
| 97 newly_combined.FilterLevel(level_); | |
| 98 combined_.Swap(&newly_combined); | |
| 99 | |
| 100 // Trigger a notification. | |
| 101 if (pending_update_caches_.empty()) | |
| 102 NotifyPolicyUpdated(); | |
| 103 } | |
| 104 | |
| 105 // static | |
| 106 void CloudPolicyProviderImpl::RemoveCache(CloudPolicyCacheBase* cache, | |
| 107 ListType* caches) { | |
| 108 ListType::iterator it = std::find(caches->begin(), caches->end(), cache); | |
| 109 if (it != caches->end()) | |
| 110 caches->erase(it); | |
| 111 } | |
| 112 | |
| 113 } // namespace policy | |
| OLD | NEW |