| 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_cache_base.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/metrics/histogram.h" | |
| 11 #include "chrome/browser/policy/enterprise_metrics.h" | |
| 12 #include "chrome/browser/policy/policy_notifier.h" | |
| 13 | |
| 14 namespace em = enterprise_management; | |
| 15 | |
| 16 namespace policy { | |
| 17 | |
| 18 CloudPolicyCacheBase::CloudPolicyCacheBase() | |
| 19 : notifier_(NULL), | |
| 20 initialization_complete_(false), | |
| 21 is_unmanaged_(false), | |
| 22 machine_id_missing_(false) { | |
| 23 public_key_version_.version = 0; | |
| 24 public_key_version_.valid = false; | |
| 25 } | |
| 26 | |
| 27 CloudPolicyCacheBase::~CloudPolicyCacheBase() {} | |
| 28 | |
| 29 void CloudPolicyCacheBase::SetFetchingDone() { | |
| 30 // NotifyObservers only fires notifications if the cache is ready. | |
| 31 NotifyObservers(); | |
| 32 } | |
| 33 | |
| 34 void CloudPolicyCacheBase::AddObserver(Observer* observer) { | |
| 35 observer_list_.AddObserver(observer); | |
| 36 } | |
| 37 | |
| 38 void CloudPolicyCacheBase::RemoveObserver(Observer* observer) { | |
| 39 observer_list_.RemoveObserver(observer); | |
| 40 } | |
| 41 | |
| 42 void CloudPolicyCacheBase::Reset() { | |
| 43 last_policy_refresh_time_ = base::Time(); | |
| 44 is_unmanaged_ = false; | |
| 45 policies_.Clear(); | |
| 46 public_key_version_.version = 0; | |
| 47 public_key_version_.valid = false; | |
| 48 InformNotifier(CloudPolicySubsystem::UNENROLLED, | |
| 49 CloudPolicySubsystem::NO_DETAILS); | |
| 50 } | |
| 51 | |
| 52 bool CloudPolicyCacheBase::IsReady() { | |
| 53 return initialization_complete_; | |
| 54 } | |
| 55 | |
| 56 bool CloudPolicyCacheBase::GetPublicKeyVersion(int* version) { | |
| 57 if (public_key_version_.valid) | |
| 58 *version = public_key_version_.version; | |
| 59 | |
| 60 return public_key_version_.valid; | |
| 61 } | |
| 62 | |
| 63 bool CloudPolicyCacheBase::SetPolicyInternal( | |
| 64 const em::PolicyFetchResponse& policy, | |
| 65 base::Time* timestamp, | |
| 66 bool check_for_timestamp_validity) { | |
| 67 DCHECK(CalledOnValidThread()); | |
| 68 is_unmanaged_ = false; | |
| 69 PolicyMap policies; | |
| 70 base::Time temp_timestamp; | |
| 71 PublicKeyVersion temp_public_key_version; | |
| 72 bool ok = DecodePolicyResponse(policy, &policies, | |
| 73 &temp_timestamp, &temp_public_key_version); | |
| 74 if (!ok) { | |
| 75 LOG(WARNING) << "Decoding policy data failed."; | |
| 76 UMA_HISTOGRAM_ENUMERATION(kMetricPolicy, kMetricPolicyFetchInvalidPolicy, | |
| 77 kMetricPolicySize); | |
| 78 return false; | |
| 79 } | |
| 80 if (timestamp) { | |
| 81 *timestamp = temp_timestamp; | |
| 82 } | |
| 83 if (check_for_timestamp_validity && | |
| 84 temp_timestamp > base::Time::NowFromSystemTime()) { | |
| 85 LOG(WARNING) << "Rejected policy data, file is from the future."; | |
| 86 UMA_HISTOGRAM_ENUMERATION(kMetricPolicy, | |
| 87 kMetricPolicyFetchTimestampInFuture, | |
| 88 kMetricPolicySize); | |
| 89 return false; | |
| 90 } | |
| 91 public_key_version_.version = temp_public_key_version.version; | |
| 92 public_key_version_.valid = temp_public_key_version.valid; | |
| 93 | |
| 94 if (policies_.Equals(policies)) { | |
| 95 UMA_HISTOGRAM_ENUMERATION(kMetricPolicy, kMetricPolicyFetchNotModified, | |
| 96 kMetricPolicySize); | |
| 97 } | |
| 98 | |
| 99 policies_.Swap(&policies); | |
| 100 | |
| 101 InformNotifier(CloudPolicySubsystem::SUCCESS, | |
| 102 CloudPolicySubsystem::NO_DETAILS); | |
| 103 return true; | |
| 104 } | |
| 105 | |
| 106 void CloudPolicyCacheBase::SetUnmanagedInternal(const base::Time& timestamp) { | |
| 107 is_unmanaged_ = true; | |
| 108 public_key_version_.valid = false; | |
| 109 policies_.Clear(); | |
| 110 last_policy_refresh_time_ = timestamp; | |
| 111 } | |
| 112 | |
| 113 void CloudPolicyCacheBase::SetReady() { | |
| 114 initialization_complete_ = true; | |
| 115 NotifyObservers(); | |
| 116 } | |
| 117 | |
| 118 bool CloudPolicyCacheBase::DecodePolicyResponse( | |
| 119 const em::PolicyFetchResponse& policy_response, | |
| 120 PolicyMap* policies, | |
| 121 base::Time* timestamp, | |
| 122 PublicKeyVersion* public_key_version) { | |
| 123 std::string data = policy_response.policy_data(); | |
| 124 em::PolicyData policy_data; | |
| 125 if (!policy_data.ParseFromString(data)) { | |
| 126 LOG(WARNING) << "Failed to parse PolicyData protobuf."; | |
| 127 return false; | |
| 128 } | |
| 129 if (timestamp) { | |
| 130 *timestamp = base::Time::UnixEpoch() + | |
| 131 base::TimeDelta::FromMilliseconds(policy_data.timestamp()); | |
| 132 } | |
| 133 if (public_key_version) { | |
| 134 public_key_version->valid = policy_data.has_public_key_version(); | |
| 135 if (public_key_version->valid) | |
| 136 public_key_version->version = policy_data.public_key_version(); | |
| 137 } | |
| 138 machine_id_missing_ = policy_data.valid_serial_number_missing(); | |
| 139 | |
| 140 return DecodePolicyData(policy_data, policies); | |
| 141 } | |
| 142 | |
| 143 void CloudPolicyCacheBase::NotifyObservers() { | |
| 144 if (IsReady()) | |
| 145 FOR_EACH_OBSERVER(Observer, observer_list_, OnCacheUpdate(this)); | |
| 146 } | |
| 147 | |
| 148 void CloudPolicyCacheBase::InformNotifier( | |
| 149 CloudPolicySubsystem::PolicySubsystemState state, | |
| 150 CloudPolicySubsystem::ErrorDetails error_details) { | |
| 151 // TODO(jkummerow): To obsolete this NULL-check, make all uses of | |
| 152 // UserPolicyCache explicitly set a notifier using |set_policy_notifier()|. | |
| 153 if (notifier_) | |
| 154 notifier_->Inform(state, error_details, PolicyNotifier::POLICY_CACHE); | |
| 155 } | |
| 156 | |
| 157 } // namespace policy | |
| OLD | NEW |