| 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" | |
| 18 #include "chrome/browser/policy/cloud/enterprise_metrics.h" | 17 #include "chrome/browser/policy/cloud/enterprise_metrics.h" |
| 19 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 20 #include "policy/policy_constants.h" | 19 #include "policy/policy_constants.h" |
| 21 #include "sync/notifier/object_id_invalidation_map.h" | 20 #include "sync/notifier/object_id_invalidation_map.h" |
| 22 | 21 |
| 23 namespace policy { | 22 namespace policy { |
| 24 | 23 |
| 25 const int CloudPolicyInvalidator::kMissingPayloadDelay = 5; | 24 const int CloudPolicyInvalidator::kMissingPayloadDelay = 5; |
| 26 const int CloudPolicyInvalidator::kMaxFetchDelayDefault = 5000; | 25 const int CloudPolicyInvalidator::kMaxFetchDelayDefault = 5000; |
| 27 const int CloudPolicyInvalidator::kMaxFetchDelayMin = 1000; | 26 const int CloudPolicyInvalidator::kMaxFetchDelayMin = 1000; |
| 28 const int CloudPolicyInvalidator::kMaxFetchDelayMax = 300000; | 27 const int CloudPolicyInvalidator::kMaxFetchDelayMax = 300000; |
| 29 | 28 |
| 30 CloudPolicyInvalidator::CloudPolicyInvalidator( | 29 CloudPolicyInvalidator::CloudPolicyInvalidator( |
| 30 const GetInvalidationService& get_invalidation_service, |
| 31 CloudPolicyInvalidationHandler* invalidation_handler, | 31 CloudPolicyInvalidationHandler* invalidation_handler, |
| 32 CloudPolicyStore* store, | 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_(STOPPED), |
| 35 get_invalidation_service_(get_invalidation_service), |
| 36 invalidation_handler_(invalidation_handler), |
| 35 store_(store), | 37 store_(store), |
| 36 task_runner_(task_runner), | 38 task_runner_(task_runner), |
| 37 profile_(NULL), | |
| 38 invalidation_service_(NULL), | 39 invalidation_service_(NULL), |
| 39 invalidations_enabled_(false), | 40 invalidations_enabled_(false), |
| 40 invalidation_service_enabled_(false), | 41 invalidation_service_enabled_(false), |
| 41 registered_timestamp_(0), | 42 registered_timestamp_(0), |
| 42 invalid_(false), | 43 invalid_(false), |
| 43 invalidation_version_(0), | 44 invalidation_version_(0), |
| 44 unknown_version_invalidation_count_(0), | 45 unknown_version_invalidation_count_(0), |
| 45 ack_handle_(syncer::AckHandle::InvalidAckHandle()), | 46 ack_handle_(syncer::AckHandle::InvalidAckHandle()), |
| 46 weak_factory_(this), | 47 weak_factory_(this), |
| 47 max_fetch_delay_(kMaxFetchDelayDefault) { | 48 max_fetch_delay_(kMaxFetchDelayDefault) { |
| 48 DCHECK(invalidation_handler); | 49 DCHECK(invalidation_handler); |
| 49 DCHECK(store); | 50 DCHECK(store); |
| 50 DCHECK(task_runner.get()); | 51 DCHECK(task_runner.get()); |
| 51 DCHECK(!IsInitialized()); | |
| 52 } | 52 } |
| 53 | 53 |
| 54 CloudPolicyInvalidator::~CloudPolicyInvalidator() {} | 54 CloudPolicyInvalidator::~CloudPolicyInvalidator() { |
| 55 | 55 DCHECK(state_ == SHUT_DOWN); |
| 56 void CloudPolicyInvalidator::InitializeWithProfile(Profile* profile) { | |
| 57 DCHECK(!IsInitialized()); | |
| 58 DCHECK(profile); | |
| 59 profile_ = profile; | |
| 60 Initialize(); | |
| 61 } | 56 } |
| 62 | 57 |
| 63 void CloudPolicyInvalidator::InitializeWithService( | 58 void CloudPolicyInvalidator::Start() { |
| 64 invalidation::InvalidationService* invalidation_service) { | 59 DCHECK(state_ == STOPPED); |
| 65 DCHECK(!IsInitialized()); | 60 state_ = STARTED; |
| 66 DCHECK(invalidation_service); | 61 OnStoreLoaded(store_); |
| 67 invalidation_service_ = invalidation_service; | 62 store_->AddObserver(this); |
| 68 Initialize(); | 63 } |
| 64 |
| 65 void CloudPolicyInvalidator::Stop() { |
| 66 DCHECK(state_ == STARTED); |
| 67 Unregister(); |
| 68 store_->RemoveObserver(this); |
| 69 state_ = STOPPED; |
| 69 } | 70 } |
| 70 | 71 |
| 71 void CloudPolicyInvalidator::Shutdown() { | 72 void CloudPolicyInvalidator::Shutdown() { |
| 72 if (IsInitialized()) { | 73 if (state_ == STARTED) { |
| 73 if (registered_timestamp_) | 74 if (registered_timestamp_) |
| 74 invalidation_service_->UnregisterInvalidationHandler(this); | 75 invalidation_service_->UnregisterInvalidationHandler(this); |
| 75 store_->RemoveObserver(this); | 76 store_->RemoveObserver(this); |
| 76 } | 77 } |
| 78 weak_factory_.InvalidateWeakPtrs(); |
| 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 |
| 98 void CloudPolicyInvalidator::OnStoreLoaded(CloudPolicyStore* store) { | 103 void CloudPolicyInvalidator::OnStoreLoaded(CloudPolicyStore* store) { |
| 99 DCHECK(IsInitialized()); | 104 DCHECK(state_ == STARTED); |
| 100 DCHECK(thread_checker_.CalledOnValidThread()); | 105 DCHECK(thread_checker_.CalledOnValidThread()); |
| 101 if (registered_timestamp_) { | 106 if (registered_timestamp_) { |
| 102 // Update the kMetricPolicyRefresh histogram. In some cases, this object can | 107 // Update the kMetricPolicyRefresh histogram. In some cases, this object can |
| 103 // be constructed during an OnStoreLoaded callback, which causes | 108 // be constructed during an OnStoreLoaded callback, which causes |
| 104 // OnStoreLoaded to be called twice at initialization time, so make sure | 109 // OnStoreLoaded to be called twice at initialization time, so make sure |
| 105 // that the timestamp does not match the timestamp at which registration | 110 // that the timestamp does not match the timestamp at which registration |
| 106 // occurred. We only measure changes which occur after registration. | 111 // occurred. We only measure changes which occur after registration. |
| 107 if (!store->policy() || !store->policy()->has_timestamp() || | 112 if (!store->policy() || !store->policy()->has_timestamp() || |
| 108 store->policy()->timestamp() != registered_timestamp_) { | 113 store->policy()->timestamp() != registered_timestamp_) { |
| 109 UMA_HISTOGRAM_ENUMERATION( | 114 UMA_HISTOGRAM_ENUMERATION( |
| 110 kMetricPolicyRefresh, | 115 kMetricPolicyRefresh, |
| 111 GetPolicyRefreshMetric(), | 116 GetPolicyRefreshMetric(), |
| 112 METRIC_POLICY_REFRESH_SIZE); | 117 METRIC_POLICY_REFRESH_SIZE); |
| 113 } | 118 } |
| 114 | 119 |
| 115 // If the policy was invalid and the version stored matches the latest | 120 // If the policy was invalid and the version stored matches the latest |
| 116 // invalidation version, acknowledge the latest invalidation. | 121 // invalidation version, acknowledge the latest invalidation. |
| 117 if (invalid_ && store->invalidation_version() == invalidation_version_) | 122 if (invalid_ && store->invalidation_version() == invalidation_version_) |
| 118 AcknowledgeInvalidation(); | 123 AcknowledgeInvalidation(); |
| 119 } | 124 } |
| 120 | 125 |
| 121 UpdateRegistration(store->policy()); | 126 UpdateRegistration(store->policy()); |
| 122 UpdateMaxFetchDelay(store->policy_map()); | 127 UpdateMaxFetchDelay(store->policy_map()); |
| 123 } | 128 } |
| 124 | 129 |
| 125 void CloudPolicyInvalidator::OnStoreError(CloudPolicyStore* store) {} | 130 void CloudPolicyInvalidator::OnStoreError(CloudPolicyStore* store) {} |
| 126 | 131 |
| 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( | 132 void CloudPolicyInvalidator::HandleInvalidation( |
| 143 const syncer::Invalidation& invalidation) { | 133 const syncer::Invalidation& invalidation) { |
| 144 // The invalidation service may send an invalidation more than once if there | 134 // The invalidation service may send an invalidation more than once if there |
| 145 // is a delay in acknowledging it. Duplicate invalidations are ignored. | 135 // is a delay in acknowledging it. Duplicate invalidations are ignored. |
| 146 if (invalid_ && ack_handle_.Equals(invalidation.ack_handle)) | 136 if (invalid_ && ack_handle_.Equals(invalidation.ack_handle)) |
| 147 return; | 137 return; |
| 148 | 138 |
| 149 // If there is still a pending invalidation, acknowledge it, since we only | 139 // If there is still a pending invalidation, acknowledge it, since we only |
| 150 // care about the latest invalidation. | 140 // care about the latest invalidation. |
| 151 if (invalid_) | 141 if (invalid_) |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 | 200 |
| 211 // If the policy object id in the policy data is different from the currently | 201 // If the policy object id in the policy data is different from the currently |
| 212 // registered object id, update the object registration. | 202 // registered object id, update the object registration. |
| 213 if (!registered_timestamp_ || !(object_id == object_id_)) | 203 if (!registered_timestamp_ || !(object_id == object_id_)) |
| 214 Register(policy->timestamp(), object_id); | 204 Register(policy->timestamp(), object_id); |
| 215 } | 205 } |
| 216 | 206 |
| 217 void CloudPolicyInvalidator::Register( | 207 void CloudPolicyInvalidator::Register( |
| 218 int64 timestamp, | 208 int64 timestamp, |
| 219 const invalidation::ObjectId& object_id) { | 209 const invalidation::ObjectId& object_id) { |
| 220 // Get the invalidation service from the profile if needed. | 210 // Get the invalidation service if needed. |
| 221 if (!invalidation_service_) { | 211 if (!invalidation_service_) { |
| 222 DCHECK(profile_); | 212 invalidation_service_ = get_invalidation_service_.Run(); |
| 223 invalidation_service_ = | |
| 224 invalidation::InvalidationServiceFactory::GetForProfile(profile_); | |
| 225 if (!invalidation_service_) | 213 if (!invalidation_service_) |
| 226 return; | 214 return; |
| 227 } | 215 } |
| 228 | 216 |
| 229 // Register this handler with the invalidation service if needed. | 217 // Register this handler with the invalidation service if needed. |
| 230 if (!registered_timestamp_) { | 218 if (!registered_timestamp_) { |
| 231 OnInvalidatorStateChange(invalidation_service_->GetInvalidatorState()); | 219 OnInvalidatorStateChange(invalidation_service_->GetInvalidatorState()); |
| 232 invalidation_service_->RegisterInvalidationHandler(this); | 220 invalidation_service_->RegisterInvalidationHandler(this); |
| 233 } | 221 } |
| 234 | 222 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 if (invalidations_enabled_) | 315 if (invalidations_enabled_) |
| 328 return METRIC_POLICY_REFRESH_CHANGED; | 316 return METRIC_POLICY_REFRESH_CHANGED; |
| 329 return METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS; | 317 return METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS; |
| 330 } | 318 } |
| 331 if (invalid_) | 319 if (invalid_) |
| 332 return METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED; | 320 return METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED; |
| 333 return METRIC_POLICY_REFRESH_UNCHANGED; | 321 return METRIC_POLICY_REFRESH_UNCHANGED; |
| 334 } | 322 } |
| 335 | 323 |
| 336 } // namespace policy | 324 } // namespace policy |
| OLD | NEW |