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