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/hash.h" | 9 #include "base/hash.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/time/clock.h" |
15 #include "base/time/time.h" | 16 #include "base/time/time.h" |
16 #include "base/values.h" | 17 #include "base/values.h" |
17 #include "chrome/browser/invalidation/invalidation_service.h" | 18 #include "chrome/browser/invalidation/invalidation_service.h" |
18 #include "components/policy/core/common/cloud/cloud_policy_client.h" | 19 #include "components/policy/core/common/cloud/cloud_policy_client.h" |
19 #include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h" | 20 #include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h" |
20 #include "components/policy/core/common/cloud/enterprise_metrics.h" | 21 #include "components/policy/core/common/cloud/enterprise_metrics.h" |
21 #include "components/policy/core/common/policy_switches.h" | 22 #include "components/policy/core/common/policy_switches.h" |
22 #include "policy/policy_constants.h" | 23 #include "policy/policy_constants.h" |
23 #include "sync/notifier/object_id_invalidation_map.h" | 24 #include "sync/notifier/object_id_invalidation_map.h" |
24 | 25 |
25 namespace policy { | 26 namespace policy { |
26 | 27 |
27 const int CloudPolicyInvalidator::kMissingPayloadDelay = 5; | 28 const int CloudPolicyInvalidator::kMissingPayloadDelay = 5; |
28 const int CloudPolicyInvalidator::kMaxFetchDelayDefault = 120000; | 29 const int CloudPolicyInvalidator::kMaxFetchDelayDefault = 120000; |
29 const int CloudPolicyInvalidator::kMaxFetchDelayMin = 1000; | 30 const int CloudPolicyInvalidator::kMaxFetchDelayMin = 1000; |
30 const int CloudPolicyInvalidator::kMaxFetchDelayMax = 300000; | 31 const int CloudPolicyInvalidator::kMaxFetchDelayMax = 300000; |
| 32 const int CloudPolicyInvalidator::kInvalidationGracePeriod = 10; |
31 | 33 |
32 CloudPolicyInvalidator::CloudPolicyInvalidator( | 34 CloudPolicyInvalidator::CloudPolicyInvalidator( |
33 CloudPolicyCore* core, | 35 CloudPolicyCore* core, |
34 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 36 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 37 scoped_ptr<base::Clock> clock) |
35 : state_(UNINITIALIZED), | 38 : state_(UNINITIALIZED), |
36 core_(core), | 39 core_(core), |
37 task_runner_(task_runner), | 40 task_runner_(task_runner), |
| 41 clock_(clock.Pass()), |
38 invalidation_service_(NULL), | 42 invalidation_service_(NULL), |
39 invalidations_enabled_(false), | 43 invalidations_enabled_(false), |
40 invalidation_service_enabled_(false), | 44 invalidation_service_enabled_(false), |
41 registered_timestamp_(0), | 45 is_registered_(false), |
42 invalid_(false), | 46 invalid_(false), |
43 invalidation_version_(0), | 47 invalidation_version_(0), |
44 unknown_version_invalidation_count_(0), | 48 unknown_version_invalidation_count_(0), |
45 weak_factory_(this), | 49 weak_factory_(this), |
46 max_fetch_delay_(kMaxFetchDelayDefault), | 50 max_fetch_delay_(kMaxFetchDelayDefault), |
47 policy_hash_value_(0) { | 51 policy_hash_value_(0) { |
48 DCHECK(core); | 52 DCHECK(core); |
49 DCHECK(task_runner.get()); | 53 DCHECK(task_runner.get()); |
50 } | 54 } |
51 | 55 |
(...skipping 10 matching lines...) Expand all Loading... |
62 state_ = STOPPED; | 66 state_ = STOPPED; |
63 core_->AddObserver(this); | 67 core_->AddObserver(this); |
64 if (core_->refresh_scheduler()) | 68 if (core_->refresh_scheduler()) |
65 OnRefreshSchedulerStarted(core_); | 69 OnRefreshSchedulerStarted(core_); |
66 } | 70 } |
67 | 71 |
68 void CloudPolicyInvalidator::Shutdown() { | 72 void CloudPolicyInvalidator::Shutdown() { |
69 DCHECK(state_ != SHUT_DOWN); | 73 DCHECK(state_ != SHUT_DOWN); |
70 DCHECK(thread_checker_.CalledOnValidThread()); | 74 DCHECK(thread_checker_.CalledOnValidThread()); |
71 if (state_ == STARTED) { | 75 if (state_ == STARTED) { |
72 if (registered_timestamp_) | 76 if (is_registered_) |
73 invalidation_service_->UnregisterInvalidationHandler(this); | 77 invalidation_service_->UnregisterInvalidationHandler(this); |
74 core_->store()->RemoveObserver(this); | 78 core_->store()->RemoveObserver(this); |
75 weak_factory_.InvalidateWeakPtrs(); | 79 weak_factory_.InvalidateWeakPtrs(); |
76 } | 80 } |
77 if (state_ != UNINITIALIZED) | 81 if (state_ != UNINITIALIZED) |
78 core_->RemoveObserver(this); | 82 core_->RemoveObserver(this); |
79 state_ = SHUT_DOWN; | 83 state_ = SHUT_DOWN; |
80 } | 84 } |
81 | 85 |
82 void CloudPolicyInvalidator::OnInvalidatorStateChange( | 86 void CloudPolicyInvalidator::OnInvalidatorStateChange( |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 core_->store()->RemoveObserver(this); | 134 core_->store()->RemoveObserver(this); |
131 state_ = STOPPED; | 135 state_ = STOPPED; |
132 } | 136 } |
133 } | 137 } |
134 | 138 |
135 void CloudPolicyInvalidator::OnStoreLoaded(CloudPolicyStore* store) { | 139 void CloudPolicyInvalidator::OnStoreLoaded(CloudPolicyStore* store) { |
136 DCHECK(state_ == STARTED); | 140 DCHECK(state_ == STARTED); |
137 DCHECK(thread_checker_.CalledOnValidThread()); | 141 DCHECK(thread_checker_.CalledOnValidThread()); |
138 bool policy_changed = IsPolicyChanged(store->policy()); | 142 bool policy_changed = IsPolicyChanged(store->policy()); |
139 | 143 |
140 if (registered_timestamp_) { | 144 if (is_registered_) { |
141 // Update the kMetricPolicyRefresh histogram. In some cases, this object can | 145 // Update the kMetricPolicyRefresh histogram. |
142 // be constructed during an OnStoreLoaded callback, which causes | 146 UMA_HISTOGRAM_ENUMERATION( |
143 // OnStoreLoaded to be called twice at initialization time, so make sure | 147 kMetricPolicyRefresh, |
144 // that the timestamp does not match the timestamp at which registration | 148 GetPolicyRefreshMetric(policy_changed), |
145 // occurred. We only measure changes which occur after registration. | 149 METRIC_POLICY_REFRESH_SIZE); |
146 if (!store->policy() || !store->policy()->has_timestamp() || | |
147 store->policy()->timestamp() != registered_timestamp_) { | |
148 UMA_HISTOGRAM_ENUMERATION( | |
149 kMetricPolicyRefresh, | |
150 GetPolicyRefreshMetric(policy_changed), | |
151 METRIC_POLICY_REFRESH_SIZE); | |
152 } | |
153 | 150 |
154 // If the policy was invalid and the version stored matches the latest | 151 // If the policy was invalid and the version stored matches the latest |
155 // invalidation version, acknowledge the latest invalidation. | 152 // invalidation version, acknowledge the latest invalidation. |
156 if (invalid_ && store->invalidation_version() == invalidation_version_) | 153 if (invalid_ && store->invalidation_version() == invalidation_version_) |
157 AcknowledgeInvalidation(); | 154 AcknowledgeInvalidation(); |
158 } | 155 } |
159 | 156 |
160 UpdateRegistration(store->policy()); | 157 UpdateRegistration(store->policy()); |
161 UpdateMaxFetchDelay(store->policy_map()); | 158 UpdateMaxFetchDelay(store->policy_map()); |
162 } | 159 } |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 | 219 |
223 // Update the kMetricPolicyInvalidations histogram. | 220 // Update the kMetricPolicyInvalidations histogram. |
224 UMA_HISTOGRAM_BOOLEAN(kMetricPolicyInvalidations, !payload.empty()); | 221 UMA_HISTOGRAM_BOOLEAN(kMetricPolicyInvalidations, !payload.empty()); |
225 } | 222 } |
226 | 223 |
227 void CloudPolicyInvalidator::UpdateRegistration( | 224 void CloudPolicyInvalidator::UpdateRegistration( |
228 const enterprise_management::PolicyData* policy) { | 225 const enterprise_management::PolicyData* policy) { |
229 // Create the ObjectId based on the policy data. | 226 // Create the ObjectId based on the policy data. |
230 // If the policy does not specify an the ObjectId, then unregister. | 227 // If the policy does not specify an the ObjectId, then unregister. |
231 if (!policy || | 228 if (!policy || |
232 !policy->has_timestamp() || | |
233 !policy->has_invalidation_source() || | 229 !policy->has_invalidation_source() || |
234 !policy->has_invalidation_name()) { | 230 !policy->has_invalidation_name()) { |
235 Unregister(); | 231 Unregister(); |
236 return; | 232 return; |
237 } | 233 } |
238 invalidation::ObjectId object_id( | 234 invalidation::ObjectId object_id( |
239 policy->invalidation_source(), | 235 policy->invalidation_source(), |
240 policy->invalidation_name()); | 236 policy->invalidation_name()); |
241 | 237 |
242 // If the policy object id in the policy data is different from the currently | 238 // If the policy object id in the policy data is different from the currently |
243 // registered object id, update the object registration. | 239 // registered object id, update the object registration. |
244 if (!registered_timestamp_ || !(object_id == object_id_)) | 240 if (!is_registered_ || !(object_id == object_id_)) |
245 Register(policy->timestamp(), object_id); | 241 Register(object_id); |
246 } | 242 } |
247 | 243 |
248 void CloudPolicyInvalidator::Register( | 244 void CloudPolicyInvalidator::Register(const invalidation::ObjectId& object_id) { |
249 int64 timestamp, | |
250 const invalidation::ObjectId& object_id) { | |
251 // Register this handler with the invalidation service if needed. | 245 // Register this handler with the invalidation service if needed. |
252 if (!registered_timestamp_) { | 246 if (!is_registered_) { |
253 OnInvalidatorStateChange(invalidation_service_->GetInvalidatorState()); | 247 OnInvalidatorStateChange(invalidation_service_->GetInvalidatorState()); |
254 invalidation_service_->RegisterInvalidationHandler(this); | 248 invalidation_service_->RegisterInvalidationHandler(this); |
255 } | 249 } |
256 | 250 |
257 // Update internal state. | 251 // Update internal state. |
258 if (invalid_) | 252 if (invalid_) |
259 AcknowledgeInvalidation(); | 253 AcknowledgeInvalidation(); |
260 registered_timestamp_ = timestamp; | 254 is_registered_ = true; |
261 object_id_ = object_id; | 255 object_id_ = object_id; |
262 UpdateInvalidationsEnabled(); | 256 UpdateInvalidationsEnabled(); |
263 | 257 |
264 // Update registration with the invalidation service. | 258 // Update registration with the invalidation service. |
265 syncer::ObjectIdSet ids; | 259 syncer::ObjectIdSet ids; |
266 ids.insert(object_id); | 260 ids.insert(object_id); |
267 invalidation_service_->UpdateRegisteredInvalidationIds(this, ids); | 261 invalidation_service_->UpdateRegisteredInvalidationIds(this, ids); |
268 } | 262 } |
269 | 263 |
270 void CloudPolicyInvalidator::Unregister() { | 264 void CloudPolicyInvalidator::Unregister() { |
271 if (registered_timestamp_) { | 265 if (is_registered_) { |
272 if (invalid_) | 266 if (invalid_) |
273 AcknowledgeInvalidation(); | 267 AcknowledgeInvalidation(); |
274 invalidation_service_->UpdateRegisteredInvalidationIds( | 268 invalidation_service_->UpdateRegisteredInvalidationIds( |
275 this, | 269 this, |
276 syncer::ObjectIdSet()); | 270 syncer::ObjectIdSet()); |
277 invalidation_service_->UnregisterInvalidationHandler(this); | 271 invalidation_service_->UnregisterInvalidationHandler(this); |
278 registered_timestamp_ = 0; | 272 is_registered_ = false; |
279 UpdateInvalidationsEnabled(); | 273 UpdateInvalidationsEnabled(); |
280 } | 274 } |
281 } | 275 } |
282 | 276 |
283 void CloudPolicyInvalidator::UpdateMaxFetchDelay(const PolicyMap& policy_map) { | 277 void CloudPolicyInvalidator::UpdateMaxFetchDelay(const PolicyMap& policy_map) { |
284 int delay; | 278 int delay; |
285 | 279 |
286 // Try reading the delay from the policy. | 280 // Try reading the delay from the policy. |
287 const base::Value* delay_policy_value = | 281 const base::Value* delay_policy_value = |
288 policy_map.GetValue(key::kMaxInvalidationFetchDelay); | 282 policy_map.GetValue(key::kMaxInvalidationFetchDelay); |
(...skipping 17 matching lines...) Expand all Loading... |
306 void CloudPolicyInvalidator::set_max_fetch_delay(int delay) { | 300 void CloudPolicyInvalidator::set_max_fetch_delay(int delay) { |
307 if (delay < kMaxFetchDelayMin) | 301 if (delay < kMaxFetchDelayMin) |
308 max_fetch_delay_ = kMaxFetchDelayMin; | 302 max_fetch_delay_ = kMaxFetchDelayMin; |
309 else if (delay > kMaxFetchDelayMax) | 303 else if (delay > kMaxFetchDelayMax) |
310 max_fetch_delay_ = kMaxFetchDelayMax; | 304 max_fetch_delay_ = kMaxFetchDelayMax; |
311 else | 305 else |
312 max_fetch_delay_ = delay; | 306 max_fetch_delay_ = delay; |
313 } | 307 } |
314 | 308 |
315 void CloudPolicyInvalidator::UpdateInvalidationsEnabled() { | 309 void CloudPolicyInvalidator::UpdateInvalidationsEnabled() { |
316 bool invalidations_enabled = | 310 bool invalidations_enabled = invalidation_service_enabled_ && is_registered_; |
317 invalidation_service_enabled_ && registered_timestamp_; | |
318 if (invalidations_enabled_ != invalidations_enabled) { | 311 if (invalidations_enabled_ != invalidations_enabled) { |
319 invalidations_enabled_ = invalidations_enabled; | 312 invalidations_enabled_ = invalidations_enabled; |
| 313 if (invalidations_enabled) |
| 314 invalidations_enabled_time_ = clock_->Now(); |
320 core_->refresh_scheduler()->SetInvalidationServiceAvailability( | 315 core_->refresh_scheduler()->SetInvalidationServiceAvailability( |
321 invalidations_enabled); | 316 invalidations_enabled); |
322 } | 317 } |
323 } | 318 } |
324 | 319 |
325 void CloudPolicyInvalidator::RefreshPolicy(bool is_missing_payload) { | 320 void CloudPolicyInvalidator::RefreshPolicy(bool is_missing_payload) { |
326 DCHECK(thread_checker_.CalledOnValidThread()); | 321 DCHECK(thread_checker_.CalledOnValidThread()); |
327 // In the missing payload case, the invalidation version has not been set on | 322 // In the missing payload case, the invalidation version has not been set on |
328 // the client yet, so set it now that the required time has elapsed. | 323 // the client yet, so set it now that the required time has elapsed. |
329 if (is_missing_payload) | 324 if (is_missing_payload) |
(...skipping 20 matching lines...) Expand all Loading... |
350 new_hash_value = base::Hash(policy->policy_value()); | 345 new_hash_value = base::Hash(policy->policy_value()); |
351 bool changed = new_hash_value != policy_hash_value_; | 346 bool changed = new_hash_value != policy_hash_value_; |
352 policy_hash_value_ = new_hash_value; | 347 policy_hash_value_ = new_hash_value; |
353 return changed; | 348 return changed; |
354 } | 349 } |
355 | 350 |
356 int CloudPolicyInvalidator::GetPolicyRefreshMetric(bool policy_changed) { | 351 int CloudPolicyInvalidator::GetPolicyRefreshMetric(bool policy_changed) { |
357 if (policy_changed) { | 352 if (policy_changed) { |
358 if (invalid_) | 353 if (invalid_) |
359 return METRIC_POLICY_REFRESH_INVALIDATED_CHANGED; | 354 return METRIC_POLICY_REFRESH_INVALIDATED_CHANGED; |
360 if (invalidations_enabled_) | 355 if (GetInvalidationsEnabled()) |
361 return METRIC_POLICY_REFRESH_CHANGED; | 356 return METRIC_POLICY_REFRESH_CHANGED; |
362 return METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS; | 357 return METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS; |
363 } | 358 } |
364 if (invalid_) | 359 if (invalid_) |
365 return METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED; | 360 return METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED; |
366 return METRIC_POLICY_REFRESH_UNCHANGED; | 361 return METRIC_POLICY_REFRESH_UNCHANGED; |
367 } | 362 } |
368 | 363 |
| 364 bool CloudPolicyInvalidator::GetInvalidationsEnabled() { |
| 365 if (!invalidations_enabled_) |
| 366 return false; |
| 367 // If invalidations have been enabled for less than the grace period, then |
| 368 // consider invalidations to be disabled for metrics reporting. |
| 369 base::TimeDelta elapsed = clock_->Now() - invalidations_enabled_time_; |
| 370 return elapsed.InSeconds() >= kInvalidationGracePeriod; |
| 371 } |
| 372 |
369 } // namespace policy | 373 } // namespace policy |
OLD | NEW |