| 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" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 DCHECK(state_ == STARTED); | 84 DCHECK(state_ == STARTED); |
| 85 DCHECK(thread_checker_.CalledOnValidThread()); | 85 DCHECK(thread_checker_.CalledOnValidThread()); |
| 86 invalidation_service_enabled_ = state == syncer::INVALIDATIONS_ENABLED; | 86 invalidation_service_enabled_ = state == syncer::INVALIDATIONS_ENABLED; |
| 87 UpdateInvalidationsEnabled(); | 87 UpdateInvalidationsEnabled(); |
| 88 } | 88 } |
| 89 | 89 |
| 90 void CloudPolicyInvalidator::OnIncomingInvalidation( | 90 void CloudPolicyInvalidator::OnIncomingInvalidation( |
| 91 const syncer::ObjectIdInvalidationMap& invalidation_map) { | 91 const syncer::ObjectIdInvalidationMap& invalidation_map) { |
| 92 DCHECK(state_ == STARTED); | 92 DCHECK(state_ == STARTED); |
| 93 DCHECK(thread_checker_.CalledOnValidThread()); | 93 DCHECK(thread_checker_.CalledOnValidThread()); |
| 94 const syncer::ObjectIdInvalidationMap::const_iterator invalidation = | 94 const syncer::OrderedInvalidationList& list = |
| 95 invalidation_map.find(object_id_); | 95 invalidation_map.ForObject(object_id_); |
| 96 if (invalidation == invalidation_map.end()) { | 96 if (list.IsEmpty()) { |
| 97 NOTREACHED(); | 97 NOTREACHED(); |
| 98 return; | 98 return; |
| 99 } | 99 } |
| 100 HandleInvalidation(invalidation->second); | 100 HandleInvalidation(list.back()); |
| 101 } | 101 } |
| 102 | 102 |
| 103 void CloudPolicyInvalidator::OnCoreConnected(CloudPolicyCore* core) {} | 103 void CloudPolicyInvalidator::OnCoreConnected(CloudPolicyCore* core) {} |
| 104 | 104 |
| 105 void CloudPolicyInvalidator::OnRefreshSchedulerStarted(CloudPolicyCore* core) { | 105 void CloudPolicyInvalidator::OnRefreshSchedulerStarted(CloudPolicyCore* core) { |
| 106 DCHECK(state_ == STOPPED); | 106 DCHECK(state_ == STOPPED); |
| 107 DCHECK(thread_checker_.CalledOnValidThread()); | 107 DCHECK(thread_checker_.CalledOnValidThread()); |
| 108 state_ = STARTED; | 108 state_ = STARTED; |
| 109 OnStoreLoaded(core_->store()); | 109 OnStoreLoaded(core_->store()); |
| 110 core_->store()->AddObserver(this); | 110 core_->store()->AddObserver(this); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 UpdateRegistration(store->policy()); | 146 UpdateRegistration(store->policy()); |
| 147 UpdateMaxFetchDelay(store->policy_map()); | 147 UpdateMaxFetchDelay(store->policy_map()); |
| 148 } | 148 } |
| 149 | 149 |
| 150 void CloudPolicyInvalidator::OnStoreError(CloudPolicyStore* store) {} | 150 void CloudPolicyInvalidator::OnStoreError(CloudPolicyStore* store) {} |
| 151 | 151 |
| 152 void CloudPolicyInvalidator::HandleInvalidation( | 152 void CloudPolicyInvalidator::HandleInvalidation( |
| 153 const syncer::Invalidation& invalidation) { | 153 const syncer::Invalidation& invalidation) { |
| 154 // 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 |
| 155 // is a delay in acknowledging it. Duplicate invalidations are ignored. | 155 // is a delay in acknowledging it. Duplicate invalidations are ignored. |
| 156 if (invalid_ && ack_handle_.Equals(invalidation.ack_handle)) | 156 if (invalid_ && ack_handle_.Equals(invalidation.GetAckHandle())) |
| 157 return; | 157 return; |
| 158 | 158 |
| 159 // 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 |
| 160 // care about the latest invalidation. | 160 // care about the latest invalidation. |
| 161 if (invalid_) | 161 if (invalid_) |
| 162 AcknowledgeInvalidation(); | 162 AcknowledgeInvalidation(); |
| 163 | 163 |
| 164 // Update invalidation state. | 164 // Update invalidation state. |
| 165 invalid_ = true; | 165 invalid_ = true; |
| 166 ack_handle_ = invalidation.ack_handle; | 166 ack_handle_ = invalidation.GetAckHandle(); |
| 167 invalidation_version_ = invalidation.version; | |
| 168 | 167 |
| 169 // When an invalidation with unknown version is received, use negative | 168 // When an invalidation with unknown version is received, use negative |
| 170 // numbers based on the number of such invalidations received. This | 169 // numbers based on the number of such invalidations received. This |
| 171 // ensures that the version numbers do not collide with "real" versions | 170 // ensures that the version numbers do not collide with "real" versions |
| 172 // (which are positive) or previous invalidations with unknown version. | 171 // (which are positive) or previous invalidations with unknown version. |
| 173 if (invalidation_version_ == syncer::Invalidation::kUnknownVersion) | 172 if (invalidation.IsUnknownVersion()) { |
| 174 invalidation_version_ = -(++unknown_version_invalidation_count_); | 173 invalidation_version_ = -(++unknown_version_invalidation_count_); |
| 174 } else { |
| 175 invalidation_version_ = invalidation.GetVersion(); |
| 176 } |
| 175 | 177 |
| 176 // In order to prevent the cloud policy server from becoming overwhelmed when | 178 // In order to prevent the cloud policy server from becoming overwhelmed when |
| 177 // a policy with many users is modified, delay for a random period of time | 179 // a policy with many users is modified, delay for a random period of time |
| 178 // before fetching the policy. Delay for at least 20ms so that if multiple | 180 // before fetching the policy. Delay for at least 20ms so that if multiple |
| 179 // invalidations are received in quick succession, only one fetch will be | 181 // invalidations are received in quick succession, only one fetch will be |
| 180 // performed. | 182 // performed. |
| 181 base::TimeDelta delay = base::TimeDelta::FromMilliseconds( | 183 base::TimeDelta delay = base::TimeDelta::FromMilliseconds( |
| 182 base::RandInt(20, max_fetch_delay_)); | 184 base::RandInt(20, max_fetch_delay_)); |
| 183 | 185 |
| 184 // If there is a payload, the policy can be refreshed at any time, so set | 186 std::string payload; |
| 185 // the version and payload on the client immediately. Otherwise, the refresh | 187 if (!invalidation.IsUnknownVersion()) |
| 188 payload = invalidation.GetPayload(); |
| 189 |
| 190 // If there is a payload, the invalidate callback can run at any time, so set |
| 191 // the version and payload on the client immediately. Otherwise, the callback |
| 186 // must only run after at least kMissingPayloadDelay minutes. | 192 // must only run after at least kMissingPayloadDelay minutes. |
| 187 const std::string& payload = invalidation.payload; | 193 if (!payload.empty()) |
| 188 if (!invalidation.payload.empty()) | |
| 189 core_->client()->SetInvalidationInfo(invalidation_version_, payload); | 194 core_->client()->SetInvalidationInfo(invalidation_version_, payload); |
| 190 else | 195 else |
| 191 delay += base::TimeDelta::FromMinutes(kMissingPayloadDelay); | 196 delay += base::TimeDelta::FromMinutes(kMissingPayloadDelay); |
| 192 | 197 |
| 193 // Schedule the policy to be refreshed. | 198 // Schedule the policy to be refreshed. |
| 194 task_runner_->PostDelayedTask( | 199 task_runner_->PostDelayedTask( |
| 195 FROM_HERE, | 200 FROM_HERE, |
| 196 base::Bind( | 201 base::Bind( |
| 197 &CloudPolicyInvalidator::RefreshPolicy, | 202 &CloudPolicyInvalidator::RefreshPolicy, |
| 198 weak_factory_.GetWeakPtr(), | 203 weak_factory_.GetWeakPtr(), |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 if (invalidations_enabled_) | 332 if (invalidations_enabled_) |
| 328 return METRIC_POLICY_REFRESH_CHANGED; | 333 return METRIC_POLICY_REFRESH_CHANGED; |
| 329 return METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS; | 334 return METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS; |
| 330 } | 335 } |
| 331 if (invalid_) | 336 if (invalid_) |
| 332 return METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED; | 337 return METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED; |
| 333 return METRIC_POLICY_REFRESH_UNCHANGED; | 338 return METRIC_POLICY_REFRESH_UNCHANGED; |
| 334 } | 339 } |
| 335 | 340 |
| 336 } // namespace policy | 341 } // namespace policy |
| OLD | NEW |