OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_policy_controller.h" | 5 #include "chrome/browser/policy/cloud_policy_controller.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 // Default value for the policy refresh rate. | 59 // Default value for the policy refresh rate. |
60 static const int kPolicyRefreshRateInMilliseconds = | 60 static const int kPolicyRefreshRateInMilliseconds = |
61 3 * 60 * 60 * 1000; // 3 hours. | 61 3 * 60 * 60 * 1000; // 3 hours. |
62 | 62 |
63 CloudPolicyController::CloudPolicyController( | 63 CloudPolicyController::CloudPolicyController( |
64 DeviceManagementService* service, | 64 DeviceManagementService* service, |
65 CloudPolicyCacheBase* cache, | 65 CloudPolicyCacheBase* cache, |
66 DeviceTokenFetcher* token_fetcher, | 66 DeviceTokenFetcher* token_fetcher, |
67 CloudPolicyIdentityStrategy* identity_strategy, | 67 CloudPolicyIdentityStrategy* identity_strategy, |
68 PolicyNotifier* notifier) | 68 PolicyNotifier* notifier) |
69 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 69 : stop_after_token_fetch_(false), |
| 70 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
70 Initialize(service, | 71 Initialize(service, |
71 cache, | 72 cache, |
72 token_fetcher, | 73 token_fetcher, |
73 identity_strategy, | 74 identity_strategy, |
74 notifier, | 75 notifier, |
75 kPolicyRefreshRateInMilliseconds, | 76 kPolicyRefreshRateInMilliseconds, |
76 kPolicyRefreshDeviationFactorPercent, | 77 kPolicyRefreshDeviationFactorPercent, |
77 kPolicyRefreshDeviationMaxInMilliseconds, | 78 kPolicyRefreshDeviationMaxInMilliseconds, |
78 kPolicyRefreshErrorDelayInMilliseconds); | 79 kPolicyRefreshErrorDelayInMilliseconds); |
79 } | 80 } |
80 | 81 |
81 CloudPolicyController::~CloudPolicyController() { | 82 CloudPolicyController::~CloudPolicyController() { |
82 token_fetcher_->RemoveObserver(this); | 83 token_fetcher_->RemoveObserver(this); |
83 identity_strategy_->RemoveObserver(this); | 84 identity_strategy_->RemoveObserver(this); |
84 CancelDelayedWork(); | 85 CancelDelayedWork(); |
85 } | 86 } |
86 | 87 |
87 void CloudPolicyController::SetRefreshRate(int64 refresh_rate_milliseconds) { | 88 void CloudPolicyController::SetRefreshRate(int64 refresh_rate_milliseconds) { |
88 policy_refresh_rate_ms_ = refresh_rate_milliseconds; | 89 policy_refresh_rate_ms_ = refresh_rate_milliseconds; |
89 | 90 |
90 // Reschedule the refresh task if necessary. | 91 // Reschedule the refresh task if necessary. |
91 if (state_ == STATE_POLICY_VALID) | 92 if (state_ == STATE_POLICY_VALID) |
92 SetState(STATE_POLICY_VALID); | 93 SetState(STATE_POLICY_VALID); |
93 } | 94 } |
94 | 95 |
| 96 void CloudPolicyController::StopAfterTokenFetch(bool enable) { |
| 97 stop_after_token_fetch_ = enable; |
| 98 } |
| 99 |
95 void CloudPolicyController::Retry() { | 100 void CloudPolicyController::Retry() { |
96 CancelDelayedWork(); | 101 CancelDelayedWork(); |
97 DoWork(); | 102 DoWork(); |
98 } | 103 } |
99 | 104 |
100 void CloudPolicyController::StopAutoRetry() { | 105 void CloudPolicyController::StopAutoRetry() { |
101 CancelDelayedWork(); | 106 CancelDelayedWork(); |
102 backend_.reset(); | 107 backend_.reset(); |
103 } | 108 } |
104 | 109 |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 void CloudPolicyController::SetState( | 304 void CloudPolicyController::SetState( |
300 CloudPolicyController::ControllerState new_state) { | 305 CloudPolicyController::ControllerState new_state) { |
301 state_ = new_state; | 306 state_ = new_state; |
302 backend_.reset(); // Discard any pending requests. | 307 backend_.reset(); // Discard any pending requests. |
303 | 308 |
304 base::Time now(base::Time::NowFromSystemTime()); | 309 base::Time now(base::Time::NowFromSystemTime()); |
305 base::Time refresh_at; | 310 base::Time refresh_at; |
306 base::Time last_refresh(cache_->last_policy_refresh_time()); | 311 base::Time last_refresh(cache_->last_policy_refresh_time()); |
307 if (last_refresh.is_null()) | 312 if (last_refresh.is_null()) |
308 last_refresh = now; | 313 last_refresh = now; |
309 | 314 LOG(ERROR) << "$$$ state : " << state_; |
310 // Determine when to take the next step. | 315 // Determine when to take the next step. |
311 bool inform_notifier_done = false; | 316 bool inform_notifier_done = false; |
312 switch (state_) { | 317 switch (state_) { |
313 case STATE_TOKEN_UNMANAGED: | 318 case STATE_TOKEN_UNMANAGED: |
314 notifier_->Inform(CloudPolicySubsystem::UNMANAGED, | 319 notifier_->Inform(CloudPolicySubsystem::UNMANAGED, |
315 CloudPolicySubsystem::NO_DETAILS, | 320 CloudPolicySubsystem::NO_DETAILS, |
316 PolicyNotifier::POLICY_CONTROLLER); | 321 PolicyNotifier::POLICY_CONTROLLER); |
317 break; | 322 break; |
318 case STATE_TOKEN_UNAVAILABLE: | 323 case STATE_TOKEN_UNAVAILABLE: |
319 // The controller is not yet initialized and needs to immediately fetch | 324 // The controller is not yet initialized and needs to immediately fetch |
320 // token and policy if present. | 325 // token and policy if present. |
| 326 refresh_at = now; |
| 327 break; |
321 case STATE_TOKEN_VALID: | 328 case STATE_TOKEN_VALID: |
322 // Immediately try to fetch the token on initialization or policy after a | 329 // Immediately try to fetch the token on initialization or policy after a |
323 // token update. Subsequent retries will respect the back-off strategy. | 330 // token update. Subsequent retries will respect the back-off strategy. |
324 refresh_at = now; | 331 if (!stop_after_token_fetch_) { |
325 // |notifier_| isn't informed about anything at this point, we wait for | 332 refresh_at = now; |
326 // the result of the next action first. | 333 LOG(ERROR) << "$$$ Will fetch!"; |
| 334 } else { |
| 335 LOG(ERROR) << "$$$ Will notify!"; |
| 336 stop_after_token_fetch_ = false; |
| 337 notifier_->Inform(CloudPolicySubsystem::TOKEN_FETCHED, |
| 338 CloudPolicySubsystem::NO_DETAILS, |
| 339 PolicyNotifier::POLICY_CONTROLLER); |
| 340 } |
327 break; | 341 break; |
328 case STATE_POLICY_VALID: | 342 case STATE_POLICY_VALID: |
329 // Delay is only reset if the policy fetch operation was successful. This | 343 // Delay is only reset if the policy fetch operation was successful. This |
330 // will ensure the server won't get overloaded with retries in case of | 344 // will ensure the server won't get overloaded with retries in case of |
331 // a bug on either side. | 345 // a bug on either side. |
332 effective_policy_refresh_error_delay_ms_ = policy_refresh_error_delay_ms_; | 346 effective_policy_refresh_error_delay_ms_ = policy_refresh_error_delay_ms_; |
333 refresh_at = | 347 refresh_at = |
334 last_refresh + base::TimeDelta::FromMilliseconds(GetRefreshDelay()); | 348 last_refresh + base::TimeDelta::FromMilliseconds(GetRefreshDelay()); |
335 notifier_->Inform(CloudPolicySubsystem::SUCCESS, | 349 notifier_->Inform(CloudPolicySubsystem::SUCCESS, |
336 CloudPolicySubsystem::NO_DETAILS, | 350 CloudPolicySubsystem::NO_DETAILS, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 } | 389 } |
376 | 390 |
377 int64 CloudPolicyController::GetRefreshDelay() { | 391 int64 CloudPolicyController::GetRefreshDelay() { |
378 int64 deviation = (policy_refresh_deviation_factor_percent_ * | 392 int64 deviation = (policy_refresh_deviation_factor_percent_ * |
379 policy_refresh_rate_ms_) / 100; | 393 policy_refresh_rate_ms_) / 100; |
380 deviation = std::min(deviation, policy_refresh_deviation_max_ms_); | 394 deviation = std::min(deviation, policy_refresh_deviation_max_ms_); |
381 return policy_refresh_rate_ms_ - base::RandGenerator(deviation + 1); | 395 return policy_refresh_rate_ms_ - base::RandGenerator(deviation + 1); |
382 } | 396 } |
383 | 397 |
384 } // namespace policy | 398 } // namespace policy |
OLD | NEW |