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) { | |
|
Mattias Nissler (ping if slow)
2013/08/28 14:20:40
Why do we need two initializers here? We'd prefera
| |
| 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 METRIC_POLICY_REFRESH_SIZE); | |
| 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_BOOLEAN(kMetricPolicyInvalidations, !payload.empty()); | |
| 194 } | |
| 195 | |
| 196 void CloudPolicyInvalidator::UpdateRegistration( | |
| 197 const enterprise_management::PolicyData* policy) { | |
| 198 // Create the ObjectId based on the policy data. | |
| 199 // If the policy does not specify an the ObjectId, then unregister. | |
| 200 if (!policy || | |
| 201 !policy->has_timestamp() || | |
| 202 !policy->has_invalidation_source() || | |
| 203 !policy->has_invalidation_name()) { | |
| 204 Unregister(); | |
| 205 return; | |
| 206 } | |
| 207 invalidation::ObjectId object_id( | |
| 208 policy->invalidation_source(), | |
| 209 policy->invalidation_name()); | |
| 210 | |
| 211 // If the policy object id in the policy data is different from the currently | |
| 212 // registered object id, update the object registration. | |
| 213 if (!registered_timestamp_ || !(object_id == object_id_)) | |
| 214 Register(policy->timestamp(), object_id); | |
| 215 } | |
| 216 | |
| 217 void CloudPolicyInvalidator::Register( | |
| 218 int64 timestamp, | |
| 219 const invalidation::ObjectId& object_id) { | |
| 220 // Get the invalidation service from the profile if needed. | |
| 221 if (!invalidation_service_) { | |
| 222 DCHECK(profile_); | |
| 223 invalidation_service_ = | |
| 224 invalidation::InvalidationServiceFactory::GetForProfile(profile_); | |
| 225 if (!invalidation_service_) | |
| 226 return; | |
| 227 } | |
| 228 | |
| 229 // Register this handler with the invalidation service if needed. | |
| 230 if (!registered_timestamp_) { | |
| 231 OnInvalidatorStateChange(invalidation_service_->GetInvalidatorState()); | |
| 232 invalidation_service_->RegisterInvalidationHandler(this); | |
| 233 } | |
| 234 | |
| 235 // Update internal state. | |
| 236 if (invalid_) | |
| 237 AcknowledgeInvalidation(); | |
| 238 registered_timestamp_ = timestamp; | |
| 239 object_id_ = object_id; | |
| 240 UpdateInvalidationsEnabled(); | |
| 241 | |
| 242 // Update registration with the invalidation service. | |
| 243 syncer::ObjectIdSet ids; | |
| 244 ids.insert(object_id); | |
| 245 invalidation_service_->UpdateRegisteredInvalidationIds(this, ids); | |
| 246 } | |
| 247 | |
| 248 void CloudPolicyInvalidator::Unregister() { | |
| 249 if (registered_timestamp_) { | |
| 250 if (invalid_) | |
| 251 AcknowledgeInvalidation(); | |
| 252 invalidation_service_->UpdateRegisteredInvalidationIds( | |
| 253 this, | |
| 254 syncer::ObjectIdSet()); | |
| 255 invalidation_service_->UnregisterInvalidationHandler(this); | |
| 256 registered_timestamp_ = 0; | |
| 257 UpdateInvalidationsEnabled(); | |
| 258 } | |
| 259 } | |
| 260 | |
| 261 void CloudPolicyInvalidator::UpdateMaxFetchDelay(const PolicyMap& policy_map) { | |
| 262 int delay; | |
| 263 | |
| 264 // Try reading the delay from the policy. | |
| 265 const base::Value* delay_policy_value = | |
| 266 policy_map.GetValue(key::kMaxInvalidationFetchDelay); | |
| 267 if (delay_policy_value && delay_policy_value->GetAsInteger(&delay)) { | |
| 268 set_max_fetch_delay(delay); | |
| 269 return; | |
| 270 } | |
| 271 | |
| 272 // Try reading the delay from the command line switch. | |
| 273 std::string delay_string = | |
| 274 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 275 switches::kCloudPolicyInvalidationDelay); | |
| 276 if (base::StringToInt(delay_string, &delay)) { | |
| 277 set_max_fetch_delay(delay); | |
| 278 return; | |
| 279 } | |
| 280 | |
| 281 set_max_fetch_delay(kMaxFetchDelayDefault); | |
| 282 } | |
| 283 | |
| 284 void CloudPolicyInvalidator::set_max_fetch_delay(int delay) { | |
| 285 if (delay < kMaxFetchDelayMin) | |
| 286 max_fetch_delay_ = kMaxFetchDelayMin; | |
| 287 else if (delay > kMaxFetchDelayMax) | |
| 288 max_fetch_delay_ = kMaxFetchDelayMax; | |
| 289 else | |
| 290 max_fetch_delay_ = delay; | |
| 291 } | |
| 292 | |
| 293 void CloudPolicyInvalidator::UpdateInvalidationsEnabled() { | |
| 294 bool invalidations_enabled = | |
| 295 invalidation_service_enabled_ && registered_timestamp_; | |
| 296 if (invalidations_enabled_ != invalidations_enabled) { | |
| 297 invalidations_enabled_ = invalidations_enabled; | |
| 298 invalidation_handler_->OnInvalidatorStateChanged(invalidations_enabled); | |
| 299 } | |
| 300 } | |
| 301 | |
| 302 void CloudPolicyInvalidator::RunInvalidateCallback(bool is_missing_payload) { | |
| 303 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 304 // In the missing payload case, the invalidation version has not been set on | |
| 305 // the client yet, so set it now that the required time has elapsed. | |
| 306 if (is_missing_payload) { | |
| 307 invalidation_handler_->SetInvalidationInfo( | |
| 308 invalidation_version_, | |
| 309 std::string()); | |
| 310 } | |
| 311 invalidation_handler_->InvalidatePolicy(); | |
| 312 } | |
| 313 | |
| 314 void CloudPolicyInvalidator::AcknowledgeInvalidation() { | |
| 315 DCHECK(invalid_); | |
| 316 invalid_ = false; | |
| 317 invalidation_handler_->SetInvalidationInfo(0, std::string()); | |
| 318 invalidation_service_->AcknowledgeInvalidation(object_id_, ack_handle_); | |
| 319 // Cancel any scheduled invalidate callbacks. | |
| 320 weak_factory_.InvalidateWeakPtrs(); | |
| 321 } | |
| 322 | |
| 323 int CloudPolicyInvalidator::GetPolicyRefreshMetric() { | |
| 324 if (store_->policy_changed()) { | |
| 325 if (invalid_) | |
| 326 return METRIC_POLICY_REFRESH_INVALIDATED_CHANGED; | |
| 327 if (invalidations_enabled_) | |
| 328 return METRIC_POLICY_REFRESH_CHANGED; | |
| 329 return METRIC_POLICY_REFRESH_CHANGED_NO_INVALIDATIONS; | |
| 330 } | |
| 331 if (invalid_) | |
| 332 return METRIC_POLICY_REFRESH_INVALIDATED_UNCHANGED; | |
| 333 return METRIC_POLICY_REFRESH_UNCHANGED; | |
| 334 } | |
| 335 | |
| 336 } // namespace policy | |
| OLD | NEW |