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