Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/location.h" | |
| 10 #include "base/metrics/histogram.h" | |
| 11 #include "base/rand_util.h" | |
| 12 #include "base/sequenced_task_runner.h" | |
| 13 #include "base/strings/string_number_conversions.h" | |
| 14 #include "base/time/time.h" | |
| 15 #include "base/values.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" | |
| 19 #include "chrome/common/chrome_switches.h" | |
| 20 #include "policy/policy_constants.h" | |
| 21 #include "sync/notifier/object_id_invalidation_map.h" | |
| 22 | |
| 23 namespace policy { | |
| 24 | |
| 25 const int CloudPolicyInvalidator::kMissingPayloadDelay = 5; | |
| 26 const int CloudPolicyInvalidator::kMaxFetchDelayDefault = 5000; | |
| 27 const int CloudPolicyInvalidator::kMaxFetchDelayMin = 1000; | |
| 28 const int CloudPolicyInvalidator::kMaxFetchDelayMax = 300000; | |
| 29 | |
| 30 CloudPolicyInvalidator::CloudPolicyInvalidator( | |
| 31 CloudPolicyInvalidationHandler* invalidation_handler, | |
| 32 CloudPolicyStore* store, | |
| 33 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | |
| 34 : invalidation_handler_(invalidation_handler), | |
| 35 store_(store), | |
| 36 task_runner_(task_runner), | |
| 37 profile_(NULL), | |
| 38 invalidation_service_(NULL), | |
| 39 invalidations_enabled_(false), | |
| 40 invalidation_service_enabled_(false), | |
| 41 registered_timestamp_(0), | |
| 42 invalid_(false), | |
| 43 invalidation_version_(0), | |
| 44 unknown_version_invalidation_count_(0), | |
| 45 ack_handle_(syncer::AckHandle::InvalidAckHandle()), | |
| 46 weak_factory_(this), | |
| 47 max_fetch_delay_(kMaxFetchDelayDefault) { | |
| 48 DCHECK(invalidation_handler); | |
| 49 DCHECK(store); | |
| 50 DCHECK(task_runner.get()); | |
| 51 DCHECK(!IsInitialized()); | |
| 52 } | |
| 53 | |
| 54 CloudPolicyInvalidator::~CloudPolicyInvalidator() {} | |
| 55 | |
| 56 void CloudPolicyInvalidator::InitializeWithProfile(Profile* profile) { | |
| 57 DCHECK(!IsInitialized()); | |
| 58 DCHECK(profile); | |
| 59 profile_ = profile; | |
| 60 Initialize(); | |
| 61 } | |
| 62 | |
| 63 void CloudPolicyInvalidator::InitializeWithService( | |
| 64 invalidation::InvalidationService* invalidation_service) { | |
| 65 DCHECK(!IsInitialized()); | |
| 66 DCHECK(invalidation_service); | |
| 67 invalidation_service_ = invalidation_service; | |
| 68 Initialize(); | |
| 69 } | |
| 70 | |
| 71 void CloudPolicyInvalidator::Shutdown() { | |
| 72 if (IsInitialized()) { | |
| 73 if (registered_timestamp_) | |
| 74 invalidation_service_->UnregisterInvalidationHandler(this); | |
| 75 store_->RemoveObserver(this); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 void CloudPolicyInvalidator::OnInvalidatorStateChange( | |
| 80 syncer::InvalidatorState state) { | |
| 81 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 82 invalidation_service_enabled_ = state == syncer::INVALIDATIONS_ENABLED; | |
| 83 UpdateInvalidationsEnabled(); | |
| 84 } | |
| 85 | |
| 86 void CloudPolicyInvalidator::OnIncomingInvalidation( | |
| 87 const syncer::ObjectIdInvalidationMap& invalidation_map) { | |
| 88 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 89 const syncer::ObjectIdInvalidationMap::const_iterator invalidation = | |
| 90 invalidation_map.find(object_id_); | |
| 91 if (invalidation == invalidation_map.end()) { | |
| 92 NOTREACHED(); | |
| 93 return; | |
| 94 } | |
| 95 HandleInvalidation(invalidation->second); | |
| 96 } | |
| 97 | |
| 98 void CloudPolicyInvalidator::OnStoreLoaded(CloudPolicyStore* store) { | |
| 99 DCHECK(IsInitialized()); | |
| 100 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 101 if (registered_timestamp_) { | |
| 102 // Update the kMetricPolicyRefresh histogram. In some cases, this object can | |
| 103 // be constructed during an OnStoreLoaded callback, which causes | |
| 104 // OnStoreLoaded to be called twice at initialization time, so make sure | |
| 105 // that the timestamp does not match the timestamp at which registration | |
| 106 // occurred. We only measure changes which occur after registration. | |
| 107 if (!store->policy() || !store->policy()->has_timestamp() || | |
| 108 store->policy()->timestamp() != registered_timestamp_) { | |
| 109 UMA_HISTOGRAM_ENUMERATION( | |
| 110 kMetricPolicyRefresh, | |
| 111 GetPolicyRefreshMetric(), | |
| 112 kMetricPolicyRefreshSize); | |
| 113 } | |
| 114 | |
| 115 // If the policy was invalid and the version stored matches the latest | |
| 116 // invalidation version, acknowledge the latest invalidation. | |
| 117 if (invalid_ && store->invalidation_version() == invalidation_version_) | |
| 118 AcknowledgeInvalidation(); | |
| 119 } | |
| 120 | |
| 121 UpdateRegistration(store->policy()); | |
| 122 UpdateMaxFetchDelay(store->policy_map()); | |
| 123 } | |
| 124 | |
| 125 void CloudPolicyInvalidator::OnStoreError(CloudPolicyStore* store) {} | |
| 126 | |
| 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( | |
| 143 const syncer::Invalidation& invalidation) { | |
| 144 // The invalidation service may send an invalidation more than once if there | |
| 145 // is a delay in acknowledging it. Duplicate invalidations are ignored. | |
| 146 if (invalid_ && ack_handle_.Equals(invalidation.ack_handle)) | |
| 147 return; | |
| 148 | |
| 149 // If there is still a pending invalidation, acknowledge it, since we only | |
| 150 // care about the latest invalidation. | |
| 151 if (invalid_) | |
| 152 AcknowledgeInvalidation(); | |
| 153 | |
| 154 // Update invalidation state. | |
| 155 invalid_ = true; | |
| 156 ack_handle_ = invalidation.ack_handle; | |
| 157 invalidation_version_ = invalidation.version; | |
| 158 | |
| 159 // When an invalidation with unknown version is received, use negative | |
| 160 // numbers based on the number of such invalidations received. This | |
| 161 // ensures that the version numbers do not collide with "real" versions | |
| 162 // (which are positive) or previous invalidations with unknown version. | |
| 163 if (invalidation_version_ == syncer::Invalidation::kUnknownVersion) | |
| 164 invalidation_version_ = -(++unknown_version_invalidation_count_); | |
| 165 | |
| 166 // In order to prevent the cloud policy server from becoming overwhelmed when | |
| 167 // a policy with many users is modified, delay for a random period of time | |
| 168 // before fetching the policy. Delay for at least 20ms so that if multiple | |
| 169 // invalidations are received in quick succession, only one fetch will be | |
| 170 // performed. | |
| 171 base::TimeDelta delay = base::TimeDelta::FromMilliseconds( | |
| 172 base::RandInt(20, max_fetch_delay_)); | |
| 173 | |
| 174 // If there is a payload, the invalidate callback can run at any time, so set | |
| 175 // the version and payload on the client immediately. Otherwise, the callback | |
| 176 // must only run after at least kMissingPayloadDelay minutes. | |
| 177 const std::string& payload = invalidation.payload; | |
| 178 if (!invalidation.payload.empty()) | |
| 179 invalidation_handler_->SetInvalidationInfo(invalidation_version_, payload); | |
| 180 else | |
| 181 delay += base::TimeDelta::FromMinutes(kMissingPayloadDelay); | |
| 182 | |
| 183 // Schedule the invalidate callback to run. | |
| 184 task_runner_->PostDelayedTask( | |
| 185 FROM_HERE, | |
| 186 base::Bind( | |
| 187 &CloudPolicyInvalidator::RunInvalidateCallback, | |
| 188 weak_factory_.GetWeakPtr(), | |
| 189 payload.empty() /* is_missing_payload */), | |
| 190 delay); | |
| 191 | |
| 192 // Update the kMetricPolicyInvalidations histogram. | |
| 193 UMA_HISTOGRAM_ENUMERATION( | |
|
Ilya Sherman
2013/07/30 01:23:16
nit: An UMA_HISTOGRAM_BOOLEAN would probably serve
Steve Condie
2013/07/30 06:12:31
Done.
| |
| 194 kMetricPolicyInvalidations, | |
| 195 payload.empty() ? | |
| 196 kMetricPolicyInvalidationsNoPayload : | |
| 197 kMetricPolicyInvalidationsPayload, | |
| 198 kMetricPolicyInvalidationsSize); | |
| 199 } | |
| 200 | |
| 201 void CloudPolicyInvalidator::UpdateRegistration( | |
| 202 const enterprise_management::PolicyData* policy) { | |
| 203 // Create the ObjectId based on the policy data. | |
| 204 // If the policy does not specify an the ObjectId, then unregister. | |
| 205 if (!policy || | |
| 206 !policy->has_timestamp() || | |
| 207 !policy->has_invalidation_source() || | |
| 208 !policy->has_invalidation_name()) { | |
| 209 Unregister(); | |
| 210 return; | |
| 211 } | |
| 212 invalidation::ObjectId object_id( | |
| 213 policy->invalidation_source(), | |
| 214 policy->invalidation_name()); | |
| 215 | |
| 216 // If the policy object id in the policy data is different from the currently | |
| 217 // registered object id, update the object registration. | |
| 218 if (!registered_timestamp_ || !(object_id == object_id_)) | |
| 219 Register(policy->timestamp(), object_id); | |
| 220 } | |
| 221 | |
| 222 void CloudPolicyInvalidator::Register( | |
| 223 int64 timestamp, | |
| 224 const invalidation::ObjectId& object_id) { | |
| 225 // Get the invalidation service from the profile if needed. | |
| 226 if (!invalidation_service_) { | |
| 227 DCHECK(profile_); | |
| 228 invalidation_service_ = | |
| 229 invalidation::InvalidationServiceFactory::GetForProfile(profile_); | |
| 230 if (!invalidation_service_) | |
| 231 return; | |
| 232 } | |
| 233 | |
| 234 // Register this handler with the invalidation service if needed. | |
| 235 if (!registered_timestamp_) { | |
| 236 OnInvalidatorStateChange(invalidation_service_->GetInvalidatorState()); | |
| 237 invalidation_service_->RegisterInvalidationHandler(this); | |
| 238 } | |
| 239 | |
| 240 // Update internal state. | |
| 241 if (invalid_) | |
| 242 AcknowledgeInvalidation(); | |
| 243 registered_timestamp_ = timestamp; | |
| 244 object_id_ = object_id; | |
| 245 UpdateInvalidationsEnabled(); | |
| 246 | |
| 247 // Update registration with the invalidation service. | |
| 248 syncer::ObjectIdSet ids; | |
| 249 ids.insert(object_id); | |
| 250 invalidation_service_->UpdateRegisteredInvalidationIds(this, ids); | |
| 251 } | |
| 252 | |
| 253 void CloudPolicyInvalidator::Unregister() { | |
| 254 if (registered_timestamp_) { | |
| 255 if (invalid_) | |
| 256 AcknowledgeInvalidation(); | |
| 257 invalidation_service_->UpdateRegisteredInvalidationIds( | |
| 258 this, | |
| 259 syncer::ObjectIdSet()); | |
| 260 invalidation_service_->UnregisterInvalidationHandler(this); | |
| 261 registered_timestamp_ = 0; | |
| 262 UpdateInvalidationsEnabled(); | |
| 263 } | |
| 264 } | |
| 265 | |
| 266 void CloudPolicyInvalidator::UpdateMaxFetchDelay(const PolicyMap& policy_map) { | |
| 267 int delay; | |
| 268 | |
| 269 // Try reading the delay from the policy. | |
| 270 const base::Value* delay_policy_value = | |
| 271 policy_map.GetValue(key::kMaxInvalidationFetchDelay); | |
| 272 if (delay_policy_value && delay_policy_value->GetAsInteger(&delay)) { | |
| 273 set_max_fetch_delay(delay); | |
| 274 return; | |
| 275 } | |
| 276 | |
| 277 // Try reading the delay from the command line switch. | |
| 278 std::string delay_string = | |
| 279 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 280 switches::kCloudPolicyInvalidationDelay); | |
| 281 if (base::StringToInt(delay_string, &delay)) { | |
| 282 set_max_fetch_delay(delay); | |
| 283 return; | |
| 284 } | |
| 285 | |
| 286 set_max_fetch_delay(kMaxFetchDelayDefault); | |
| 287 } | |
| 288 | |
| 289 void CloudPolicyInvalidator::set_max_fetch_delay(int delay) { | |
| 290 if (delay < kMaxFetchDelayMin) | |
| 291 max_fetch_delay_ = kMaxFetchDelayMin; | |
| 292 else if (delay > kMaxFetchDelayMax) | |
| 293 max_fetch_delay_ = kMaxFetchDelayMax; | |
| 294 else | |
| 295 max_fetch_delay_ = delay; | |
| 296 } | |
| 297 | |
| 298 void CloudPolicyInvalidator::UpdateInvalidationsEnabled() { | |
| 299 bool invalidations_enabled = | |
| 300 invalidation_service_enabled_ && registered_timestamp_; | |
| 301 if (invalidations_enabled_ != invalidations_enabled) { | |
| 302 invalidations_enabled_ = invalidations_enabled; | |
| 303 invalidation_handler_->OnInvalidatorStateChanged(invalidations_enabled); | |
| 304 } | |
| 305 } | |
| 306 | |
| 307 void CloudPolicyInvalidator::RunInvalidateCallback(bool is_missing_payload) { | |
| 308 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 309 // In the missing payload case, the invalidation version has not been set on | |
| 310 // the client yet, so set it now that the required time has elapsed. | |
| 311 if (is_missing_payload) { | |
| 312 invalidation_handler_->SetInvalidationInfo( | |
| 313 invalidation_version_, | |
| 314 std::string()); | |
| 315 } | |
| 316 invalidation_handler_->InvalidatePolicy(); | |
| 317 } | |
| 318 | |
| 319 void CloudPolicyInvalidator::AcknowledgeInvalidation() { | |
| 320 DCHECK(invalid_); | |
| 321 invalid_ = false; | |
| 322 invalidation_handler_->SetInvalidationInfo(0, std::string()); | |
| 323 invalidation_service_->AcknowledgeInvalidation(object_id_, ack_handle_); | |
| 324 // Cancel any scheduled invalidate callbacks. | |
| 325 weak_factory_.InvalidateWeakPtrs(); | |
| 326 } | |
| 327 | |
| 328 int CloudPolicyInvalidator::GetPolicyRefreshMetric() { | |
| 329 if (store_->policy_changed()) { | |
| 330 if (invalid_) | |
| 331 return kMetricPolicyRefreshInvalidatedChanged; | |
| 332 if (invalidations_enabled_) | |
| 333 return kMetricPolicyRefreshChanged; | |
| 334 return kMetricPolicyRefreshChangedNoInvalidations; | |
| 335 } | |
| 336 if (invalid_) | |
| 337 return kMetricPolicyRefreshInvalidatedUnchanged; | |
| 338 return kMetricPolicyRefreshUnchanged; | |
| 339 } | |
| 340 | |
| 341 } // namespace policy | |
| OLD | NEW |