OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" | 5 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
12 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/browser/invalidation/invalidation_service.h" | 16 #include "chrome/browser/invalidation/invalidation_service.h" |
17 #include "chrome/browser/invalidation/invalidation_service_factory.h" | 17 #include "chrome/browser/policy/cloud/cloud_policy_client.h" |
| 18 #include "chrome/browser/policy/cloud/cloud_policy_refresh_scheduler.h" |
18 #include "chrome/browser/policy/cloud/enterprise_metrics.h" | 19 #include "chrome/browser/policy/cloud/enterprise_metrics.h" |
19 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
20 #include "policy/policy_constants.h" | 21 #include "policy/policy_constants.h" |
21 #include "sync/notifier/object_id_invalidation_map.h" | 22 #include "sync/notifier/object_id_invalidation_map.h" |
22 | 23 |
23 namespace policy { | 24 namespace policy { |
24 | 25 |
25 const int CloudPolicyInvalidator::kMissingPayloadDelay = 5; | 26 const int CloudPolicyInvalidator::kMissingPayloadDelay = 5; |
26 const int CloudPolicyInvalidator::kMaxFetchDelayDefault = 5000; | 27 const int CloudPolicyInvalidator::kMaxFetchDelayDefault = 5000; |
27 const int CloudPolicyInvalidator::kMaxFetchDelayMin = 1000; | 28 const int CloudPolicyInvalidator::kMaxFetchDelayMin = 1000; |
28 const int CloudPolicyInvalidator::kMaxFetchDelayMax = 300000; | 29 const int CloudPolicyInvalidator::kMaxFetchDelayMax = 300000; |
29 | 30 |
30 CloudPolicyInvalidator::CloudPolicyInvalidator( | 31 CloudPolicyInvalidator::CloudPolicyInvalidator( |
31 CloudPolicyInvalidationHandler* invalidation_handler, | 32 CloudPolicyCore* core, |
32 CloudPolicyStore* store, | |
33 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 33 const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
34 : invalidation_handler_(invalidation_handler), | 34 : state_(UNINITIALIZED), |
35 store_(store), | 35 core_(core), |
36 task_runner_(task_runner), | 36 task_runner_(task_runner), |
37 profile_(NULL), | |
38 invalidation_service_(NULL), | 37 invalidation_service_(NULL), |
39 invalidations_enabled_(false), | 38 invalidations_enabled_(false), |
40 invalidation_service_enabled_(false), | 39 invalidation_service_enabled_(false), |
41 registered_timestamp_(0), | 40 registered_timestamp_(0), |
42 invalid_(false), | 41 invalid_(false), |
43 invalidation_version_(0), | 42 invalidation_version_(0), |
44 unknown_version_invalidation_count_(0), | 43 unknown_version_invalidation_count_(0), |
45 ack_handle_(syncer::AckHandle::InvalidAckHandle()), | 44 ack_handle_(syncer::AckHandle::InvalidAckHandle()), |
46 weak_factory_(this), | 45 weak_factory_(this), |
47 max_fetch_delay_(kMaxFetchDelayDefault) { | 46 max_fetch_delay_(kMaxFetchDelayDefault), |
48 DCHECK(invalidation_handler); | 47 policy_refresh_count_(0) { |
49 DCHECK(store); | 48 DCHECK(core); |
50 DCHECK(task_runner.get()); | 49 DCHECK(task_runner.get()); |
51 DCHECK(!IsInitialized()); | |
52 } | 50 } |
53 | 51 |
54 CloudPolicyInvalidator::~CloudPolicyInvalidator() {} | 52 CloudPolicyInvalidator::~CloudPolicyInvalidator() { |
55 | 53 DCHECK(state_ == SHUT_DOWN); |
56 void CloudPolicyInvalidator::InitializeWithProfile(Profile* profile) { | |
57 DCHECK(!IsInitialized()); | |
58 DCHECK(profile); | |
59 profile_ = profile; | |
60 Initialize(); | |
61 } | 54 } |
62 | 55 |
63 void CloudPolicyInvalidator::InitializeWithService( | 56 void CloudPolicyInvalidator::Initialize( |
64 invalidation::InvalidationService* invalidation_service) { | 57 invalidation::InvalidationService* invalidation_service) { |
65 DCHECK(!IsInitialized()); | 58 DCHECK(state_ == UNINITIALIZED); |
| 59 DCHECK(thread_checker_.CalledOnValidThread()); |
66 DCHECK(invalidation_service); | 60 DCHECK(invalidation_service); |
67 invalidation_service_ = invalidation_service; | 61 invalidation_service_ = invalidation_service; |
68 Initialize(); | 62 state_ = STOPPED; |
| 63 core_->AddObserver(this); |
| 64 if (core_->refresh_scheduler()) |
| 65 OnRefreshSchedulerStarted(core_); |
69 } | 66 } |
70 | 67 |
71 void CloudPolicyInvalidator::Shutdown() { | 68 void CloudPolicyInvalidator::Shutdown() { |
72 if (IsInitialized()) { | 69 DCHECK(state_ != SHUT_DOWN); |
| 70 DCHECK(thread_checker_.CalledOnValidThread()); |
| 71 if (state_ == STARTED) { |
73 if (registered_timestamp_) | 72 if (registered_timestamp_) |
74 invalidation_service_->UnregisterInvalidationHandler(this); | 73 invalidation_service_->UnregisterInvalidationHandler(this); |
75 store_->RemoveObserver(this); | 74 core_->store()->RemoveObserver(this); |
| 75 weak_factory_.InvalidateWeakPtrs(); |
76 } | 76 } |
| 77 if (state_ != UNINITIALIZED) |
| 78 core_->RemoveObserver(this); |
| 79 state_ = SHUT_DOWN; |
77 } | 80 } |
78 | 81 |
79 void CloudPolicyInvalidator::OnInvalidatorStateChange( | 82 void CloudPolicyInvalidator::OnInvalidatorStateChange( |
80 syncer::InvalidatorState state) { | 83 syncer::InvalidatorState state) { |
| 84 DCHECK(state_ == STARTED); |
81 DCHECK(thread_checker_.CalledOnValidThread()); | 85 DCHECK(thread_checker_.CalledOnValidThread()); |
82 invalidation_service_enabled_ = state == syncer::INVALIDATIONS_ENABLED; | 86 invalidation_service_enabled_ = state == syncer::INVALIDATIONS_ENABLED; |
83 UpdateInvalidationsEnabled(); | 87 UpdateInvalidationsEnabled(); |
84 } | 88 } |
85 | 89 |
86 void CloudPolicyInvalidator::OnIncomingInvalidation( | 90 void CloudPolicyInvalidator::OnIncomingInvalidation( |
87 const syncer::ObjectIdInvalidationMap& invalidation_map) { | 91 const syncer::ObjectIdInvalidationMap& invalidation_map) { |
| 92 DCHECK(state_ == STARTED); |
88 DCHECK(thread_checker_.CalledOnValidThread()); | 93 DCHECK(thread_checker_.CalledOnValidThread()); |
89 const syncer::ObjectIdInvalidationMap::const_iterator invalidation = | 94 const syncer::ObjectIdInvalidationMap::const_iterator invalidation = |
90 invalidation_map.find(object_id_); | 95 invalidation_map.find(object_id_); |
91 if (invalidation == invalidation_map.end()) { | 96 if (invalidation == invalidation_map.end()) { |
92 NOTREACHED(); | 97 NOTREACHED(); |
93 return; | 98 return; |
94 } | 99 } |
95 HandleInvalidation(invalidation->second); | 100 HandleInvalidation(invalidation->second); |
96 } | 101 } |
97 | 102 |
| 103 void CloudPolicyInvalidator::OnCoreConnected(CloudPolicyCore* core) {} |
| 104 |
| 105 void CloudPolicyInvalidator::OnRefreshSchedulerStarted(CloudPolicyCore* core) { |
| 106 DCHECK(state_ == STOPPED); |
| 107 DCHECK(thread_checker_.CalledOnValidThread()); |
| 108 state_ = STARTED; |
| 109 OnStoreLoaded(core_->store()); |
| 110 core_->store()->AddObserver(this); |
| 111 } |
| 112 |
| 113 void CloudPolicyInvalidator::OnCoreDisconnecting(CloudPolicyCore* core) { |
| 114 DCHECK(state_ == STARTED || state_ == STOPPED); |
| 115 DCHECK(thread_checker_.CalledOnValidThread()); |
| 116 if (state_ == STARTED) { |
| 117 Unregister(); |
| 118 core_->store()->RemoveObserver(this); |
| 119 state_ = STOPPED; |
| 120 } |
| 121 } |
| 122 |
98 void CloudPolicyInvalidator::OnStoreLoaded(CloudPolicyStore* store) { | 123 void CloudPolicyInvalidator::OnStoreLoaded(CloudPolicyStore* store) { |
99 DCHECK(IsInitialized()); | 124 DCHECK(state_ == STARTED); |
100 DCHECK(thread_checker_.CalledOnValidThread()); | 125 DCHECK(thread_checker_.CalledOnValidThread()); |
101 if (registered_timestamp_) { | 126 if (registered_timestamp_) { |
102 // Update the kMetricPolicyRefresh histogram. In some cases, this object can | 127 // Update the kMetricPolicyRefresh histogram. In some cases, this object can |
103 // be constructed during an OnStoreLoaded callback, which causes | 128 // be constructed during an OnStoreLoaded callback, which causes |
104 // OnStoreLoaded to be called twice at initialization time, so make sure | 129 // OnStoreLoaded to be called twice at initialization time, so make sure |
105 // that the timestamp does not match the timestamp at which registration | 130 // that the timestamp does not match the timestamp at which registration |
106 // occurred. We only measure changes which occur after registration. | 131 // occurred. We only measure changes which occur after registration. |
107 if (!store->policy() || !store->policy()->has_timestamp() || | 132 if (!store->policy() || !store->policy()->has_timestamp() || |
108 store->policy()->timestamp() != registered_timestamp_) { | 133 store->policy()->timestamp() != registered_timestamp_) { |
109 UMA_HISTOGRAM_ENUMERATION( | 134 UMA_HISTOGRAM_ENUMERATION( |
110 kMetricPolicyRefresh, | 135 kMetricPolicyRefresh, |
111 GetPolicyRefreshMetric(), | 136 GetPolicyRefreshMetric(), |
112 METRIC_POLICY_REFRESH_SIZE); | 137 METRIC_POLICY_REFRESH_SIZE); |
113 } | 138 } |
114 | 139 |
115 // If the policy was invalid and the version stored matches the latest | 140 // If the policy was invalid and the version stored matches the latest |
116 // invalidation version, acknowledge the latest invalidation. | 141 // invalidation version, acknowledge the latest invalidation. |
117 if (invalid_ && store->invalidation_version() == invalidation_version_) | 142 if (invalid_ && store->invalidation_version() == invalidation_version_) |
118 AcknowledgeInvalidation(); | 143 AcknowledgeInvalidation(); |
119 } | 144 } |
120 | 145 |
121 UpdateRegistration(store->policy()); | 146 UpdateRegistration(store->policy()); |
122 UpdateMaxFetchDelay(store->policy_map()); | 147 UpdateMaxFetchDelay(store->policy_map()); |
123 } | 148 } |
124 | 149 |
125 void CloudPolicyInvalidator::OnStoreError(CloudPolicyStore* store) {} | 150 void CloudPolicyInvalidator::OnStoreError(CloudPolicyStore* store) {} |
126 | 151 |
127 base::WeakPtr<CloudPolicyInvalidator> CloudPolicyInvalidator::GetWeakPtr() { | |
128 DCHECK(!IsInitialized()); | |
129 return weak_factory_.GetWeakPtr(); | |
130 } | |
131 | |
132 void CloudPolicyInvalidator::Initialize() { | |
133 OnStoreLoaded(store_); | |
134 store_->AddObserver(this); | |
135 } | |
136 | |
137 bool CloudPolicyInvalidator::IsInitialized() { | |
138 // Could have been initialized with a profile or invalidation service. | |
139 return profile_ || invalidation_service_; | |
140 } | |
141 | |
142 void CloudPolicyInvalidator::HandleInvalidation( | 152 void CloudPolicyInvalidator::HandleInvalidation( |
143 const syncer::Invalidation& invalidation) { | 153 const syncer::Invalidation& invalidation) { |
144 // The invalidation service may send an invalidation more than once if there | 154 // The invalidation service may send an invalidation more than once if there |
145 // is a delay in acknowledging it. Duplicate invalidations are ignored. | 155 // is a delay in acknowledging it. Duplicate invalidations are ignored. |
146 if (invalid_ && ack_handle_.Equals(invalidation.ack_handle)) | 156 if (invalid_ && ack_handle_.Equals(invalidation.ack_handle)) |
147 return; | 157 return; |
148 | 158 |
149 // If there is still a pending invalidation, acknowledge it, since we only | 159 // If there is still a pending invalidation, acknowledge it, since we only |
150 // care about the latest invalidation. | 160 // care about the latest invalidation. |
151 if (invalid_) | 161 if (invalid_) |
(...skipping 12 matching lines...) Expand all Loading... |
164 invalidation_version_ = -(++unknown_version_invalidation_count_); | 174 invalidation_version_ = -(++unknown_version_invalidation_count_); |
165 | 175 |
166 // In order to prevent the cloud policy server from becoming overwhelmed when | 176 // In order to prevent the cloud policy server from becoming overwhelmed when |
167 // a policy with many users is modified, delay for a random period of time | 177 // a policy with many users is modified, delay for a random period of time |
168 // before fetching the policy. Delay for at least 20ms so that if multiple | 178 // before fetching the policy. Delay for at least 20ms so that if multiple |
169 // invalidations are received in quick succession, only one fetch will be | 179 // invalidations are received in quick succession, only one fetch will be |
170 // performed. | 180 // performed. |
171 base::TimeDelta delay = base::TimeDelta::FromMilliseconds( | 181 base::TimeDelta delay = base::TimeDelta::FromMilliseconds( |
172 base::RandInt(20, max_fetch_delay_)); | 182 base::RandInt(20, max_fetch_delay_)); |
173 | 183 |
174 // If there is a payload, the invalidate callback can run at any time, so set | 184 // If there is a payload, the policy can be refreshed at any time, so set |
175 // the version and payload on the client immediately. Otherwise, the callback | 185 // the version and payload on the client immediately. Otherwise, the refresh |
176 // must only run after at least kMissingPayloadDelay minutes. | 186 // must only run after at least kMissingPayloadDelay minutes. |
177 const std::string& payload = invalidation.payload; | 187 const std::string& payload = invalidation.payload; |
178 if (!invalidation.payload.empty()) | 188 if (!invalidation.payload.empty()) |
179 invalidation_handler_->SetInvalidationInfo(invalidation_version_, payload); | 189 core_->client()->SetInvalidationInfo(invalidation_version_, payload); |
180 else | 190 else |
181 delay += base::TimeDelta::FromMinutes(kMissingPayloadDelay); | 191 delay += base::TimeDelta::FromMinutes(kMissingPayloadDelay); |
182 | 192 |
183 // Schedule the invalidate callback to run. | 193 // Schedule the policy to be refreshed. |
184 task_runner_->PostDelayedTask( | 194 task_runner_->PostDelayedTask( |
185 FROM_HERE, | 195 FROM_HERE, |
186 base::Bind( | 196 base::Bind( |
187 &CloudPolicyInvalidator::RunInvalidateCallback, | 197 &CloudPolicyInvalidator::RefreshPolicy, |
188 weak_factory_.GetWeakPtr(), | 198 weak_factory_.GetWeakPtr(), |
189 payload.empty() /* is_missing_payload */), | 199 payload.empty() /* is_missing_payload */), |
190 delay); | 200 delay); |
191 | 201 |
192 // Update the kMetricPolicyInvalidations histogram. | 202 // Update the kMetricPolicyInvalidations histogram. |
193 UMA_HISTOGRAM_BOOLEAN(kMetricPolicyInvalidations, !payload.empty()); | 203 UMA_HISTOGRAM_BOOLEAN(kMetricPolicyInvalidations, !payload.empty()); |
194 } | 204 } |
195 | 205 |
196 void CloudPolicyInvalidator::UpdateRegistration( | 206 void CloudPolicyInvalidator::UpdateRegistration( |
197 const enterprise_management::PolicyData* policy) { | 207 const enterprise_management::PolicyData* policy) { |
(...skipping 12 matching lines...) Expand all Loading... |
210 | 220 |
211 // If the policy object id in the policy data is different from the currently | 221 // If the policy object id in the policy data is different from the currently |
212 // registered object id, update the object registration. | 222 // registered object id, update the object registration. |
213 if (!registered_timestamp_ || !(object_id == object_id_)) | 223 if (!registered_timestamp_ || !(object_id == object_id_)) |
214 Register(policy->timestamp(), object_id); | 224 Register(policy->timestamp(), object_id); |
215 } | 225 } |
216 | 226 |
217 void CloudPolicyInvalidator::Register( | 227 void CloudPolicyInvalidator::Register( |
218 int64 timestamp, | 228 int64 timestamp, |
219 const invalidation::ObjectId& object_id) { | 229 const invalidation::ObjectId& object_id) { |
220 // Get the invalidation service from the profile if needed. | |
221 if (!invalidation_service_) { | |
222 DCHECK(profile_); | |
223 invalidation_service_ = | |
224 invalidation::InvalidationServiceFactory::GetForProfile(profile_); | |
225 if (!invalidation_service_) | |
226 return; | |
227 } | |
228 | |
229 // Register this handler with the invalidation service if needed. | 230 // Register this handler with the invalidation service if needed. |
230 if (!registered_timestamp_) { | 231 if (!registered_timestamp_) { |
231 OnInvalidatorStateChange(invalidation_service_->GetInvalidatorState()); | 232 OnInvalidatorStateChange(invalidation_service_->GetInvalidatorState()); |
232 invalidation_service_->RegisterInvalidationHandler(this); | 233 invalidation_service_->RegisterInvalidationHandler(this); |
233 } | 234 } |
234 | 235 |
235 // Update internal state. | 236 // Update internal state. |
236 if (invalid_) | 237 if (invalid_) |
237 AcknowledgeInvalidation(); | 238 AcknowledgeInvalidation(); |
238 registered_timestamp_ = timestamp; | 239 registered_timestamp_ = timestamp; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 max_fetch_delay_ = kMaxFetchDelayMax; | 289 max_fetch_delay_ = kMaxFetchDelayMax; |
289 else | 290 else |
290 max_fetch_delay_ = delay; | 291 max_fetch_delay_ = delay; |
291 } | 292 } |
292 | 293 |
293 void CloudPolicyInvalidator::UpdateInvalidationsEnabled() { | 294 void CloudPolicyInvalidator::UpdateInvalidationsEnabled() { |
294 bool invalidations_enabled = | 295 bool invalidations_enabled = |
295 invalidation_service_enabled_ && registered_timestamp_; | 296 invalidation_service_enabled_ && registered_timestamp_; |
296 if (invalidations_enabled_ != invalidations_enabled) { | 297 if (invalidations_enabled_ != invalidations_enabled) { |
297 invalidations_enabled_ = invalidations_enabled; | 298 invalidations_enabled_ = invalidations_enabled; |
298 invalidation_handler_->OnInvalidatorStateChanged(invalidations_enabled); | 299 core_->refresh_scheduler()->SetInvalidationServiceAvailability( |
| 300 invalidations_enabled); |
299 } | 301 } |
300 } | 302 } |
301 | 303 |
302 void CloudPolicyInvalidator::RunInvalidateCallback(bool is_missing_payload) { | 304 void CloudPolicyInvalidator::RefreshPolicy(bool is_missing_payload) { |
303 DCHECK(thread_checker_.CalledOnValidThread()); | 305 DCHECK(thread_checker_.CalledOnValidThread()); |
304 // In the missing payload case, the invalidation version has not been set on | 306 // In the missing payload case, the invalidation version has not been set on |
305 // the client yet, so set it now that the required time has elapsed. | 307 // the client yet, so set it now that the required time has elapsed. |
306 if (is_missing_payload) { | 308 if (is_missing_payload) |
307 invalidation_handler_->SetInvalidationInfo( | 309 core_->client()->SetInvalidationInfo(invalidation_version_, std::string()); |
308 invalidation_version_, | 310 core_->refresh_scheduler()->RefreshSoon(); |
309 std::string()); | 311 policy_refresh_count_++; |
310 } | |
311 invalidation_handler_->InvalidatePolicy(); | |
312 } | 312 } |
313 | 313 |
314 void CloudPolicyInvalidator::AcknowledgeInvalidation() { | 314 void CloudPolicyInvalidator::AcknowledgeInvalidation() { |
315 DCHECK(invalid_); | 315 DCHECK(invalid_); |
316 invalid_ = false; | 316 invalid_ = false; |
317 invalidation_handler_->SetInvalidationInfo(0, std::string()); | 317 core_->client()->SetInvalidationInfo(0, std::string()); |
318 invalidation_service_->AcknowledgeInvalidation(object_id_, ack_handle_); | 318 invalidation_service_->AcknowledgeInvalidation(object_id_, ack_handle_); |
319 // Cancel any scheduled invalidate callbacks. | 319 // Cancel any scheduled policy refreshes. |
320 weak_factory_.InvalidateWeakPtrs(); | 320 weak_factory_.InvalidateWeakPtrs(); |
321 } | 321 } |
322 | 322 |
323 int CloudPolicyInvalidator::GetPolicyRefreshMetric() { | 323 int CloudPolicyInvalidator::GetPolicyRefreshMetric() { |
324 if (store_->policy_changed()) { | 324 if (core_->store()->policy_changed()) { |
325 if (invalid_) | 325 if (invalid_) |
326 return METRIC_POLICY_REFRESH_INVALIDATED_CHANGED; | 326 return METRIC_POLICY_REFRESH_INVALIDATED_CHANGED; |
327 if (invalidations_enabled_) | 327 if (invalidations_enabled_) |
328 return METRIC_POLICY_REFRESH_CHANGED; | 328 return METRIC_POLICY_REFRESH_CHANGED; |
329 return METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS; | 329 return METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS; |
330 } | 330 } |
331 if (invalid_) | 331 if (invalid_) |
332 return METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED; | 332 return METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED; |
333 return METRIC_POLICY_REFRESH_UNCHANGED; | 333 return METRIC_POLICY_REFRESH_UNCHANGED; |
334 } | 334 } |
335 | 335 |
336 } // namespace policy | 336 } // namespace policy |
OLD | NEW |