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