Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_refresh_scheduler.h" | 5 #include "chrome/browser/policy/cloud/cloud_policy_refresh_scheduler.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 const int64 CloudPolicyRefreshScheduler::kDefaultRefreshDelayMs = | 28 const int64 CloudPolicyRefreshScheduler::kDefaultRefreshDelayMs = |
| 29 3 * 60 * 60 * 1000; // 3 hours. | 29 3 * 60 * 60 * 1000; // 3 hours. |
| 30 const int64 CloudPolicyRefreshScheduler::kUnmanagedRefreshDelayMs = | 30 const int64 CloudPolicyRefreshScheduler::kUnmanagedRefreshDelayMs = |
| 31 24 * 60 * 60 * 1000; // 1 day. | 31 24 * 60 * 60 * 1000; // 1 day. |
| 32 const int64 CloudPolicyRefreshScheduler::kInitialErrorRetryDelayMs = | 32 const int64 CloudPolicyRefreshScheduler::kInitialErrorRetryDelayMs = |
| 33 5 * 60 * 1000; // 5 minutes. | 33 5 * 60 * 1000; // 5 minutes. |
| 34 const int64 CloudPolicyRefreshScheduler::kRefreshDelayMinMs = | 34 const int64 CloudPolicyRefreshScheduler::kRefreshDelayMinMs = |
| 35 30 * 60 * 1000; // 30 minutes. | 35 30 * 60 * 1000; // 30 minutes. |
| 36 const int64 CloudPolicyRefreshScheduler::kRefreshDelayMaxMs = | 36 const int64 CloudPolicyRefreshScheduler::kRefreshDelayMaxMs = |
| 37 24 * 60 * 60 * 1000; // 1 day. | 37 24 * 60 * 60 * 1000; // 1 day. |
| 38 const int64 CloudPolicyRefreshScheduler::kFirstRefreshDelayMs = 4000; | |
| 38 | 39 |
| 39 CloudPolicyRefreshScheduler::CloudPolicyRefreshScheduler( | 40 CloudPolicyRefreshScheduler::CloudPolicyRefreshScheduler( |
| 40 CloudPolicyClient* client, | 41 CloudPolicyClient* client, |
| 41 CloudPolicyStore* store, | 42 CloudPolicyStore* store, |
| 42 const scoped_refptr<base::SequencedTaskRunner>& task_runner) | 43 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 44 bool invalidations_enabled) | |
| 43 : client_(client), | 45 : client_(client), |
| 44 store_(store), | 46 store_(store), |
| 45 task_runner_(task_runner), | 47 task_runner_(task_runner), |
| 46 error_retry_delay_ms_(kInitialErrorRetryDelayMs), | 48 error_retry_delay_ms_(kInitialErrorRetryDelayMs), |
| 47 refresh_delay_ms_(kDefaultRefreshDelayMs), | 49 refresh_delay_ms_(kDefaultRefreshDelayMs), |
| 48 rate_limiter_(kMaxRefreshesPerHour, | 50 rate_limiter_(kMaxRefreshesPerHour, |
| 49 base::TimeDelta::FromHours(1), | 51 base::TimeDelta::FromHours(1), |
| 50 base::Bind(&CloudPolicyRefreshScheduler::RefreshNow, | 52 base::Bind(&CloudPolicyRefreshScheduler::RefreshNow, |
| 51 base::Unretained(this)), | 53 base::Unretained(this)), |
| 52 task_runner_, | 54 task_runner_, |
| 53 scoped_ptr<base::TickClock>(new base::DefaultTickClock())) { | 55 scoped_ptr<base::TickClock>(new base::DefaultTickClock())), |
| 56 first_refresh_(base::Time::NowFromSystemTime() + | |
| 57 base::TimeDelta::FromMilliseconds(kFirstRefreshDelayMs)) { | |
| 54 client_->AddObserver(this); | 58 client_->AddObserver(this); |
| 55 store_->AddObserver(this); | 59 store_->AddObserver(this); |
| 56 net::NetworkChangeNotifier::AddIPAddressObserver(this); | 60 net::NetworkChangeNotifier::AddIPAddressObserver(this); |
| 57 | 61 |
| 62 // Disable first refresh delay if invalidations are not enabled. | |
| 63 if (!invalidations_enabled) | |
| 64 first_refresh_ = base::Time(); | |
| 58 UpdateLastRefreshFromPolicy(); | 65 UpdateLastRefreshFromPolicy(); |
| 59 ScheduleRefresh(); | 66 ScheduleRefresh(); |
| 60 } | 67 } |
| 61 | 68 |
| 62 CloudPolicyRefreshScheduler::~CloudPolicyRefreshScheduler() { | 69 CloudPolicyRefreshScheduler::~CloudPolicyRefreshScheduler() { |
| 63 store_->RemoveObserver(this); | 70 store_->RemoveObserver(this); |
| 64 client_->RemoveObserver(this); | 71 client_->RemoveObserver(this); |
| 65 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 72 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 66 } | 73 } |
| 67 | 74 |
| 68 void CloudPolicyRefreshScheduler::SetRefreshDelay(int64 refresh_delay) { | 75 void CloudPolicyRefreshScheduler::SetRefreshDelay(int64 refresh_delay) { |
| 69 refresh_delay_ms_ = std::min(std::max(refresh_delay, kRefreshDelayMinMs), | 76 refresh_delay_ms_ = std::min(std::max(refresh_delay, kRefreshDelayMinMs), |
| 70 kRefreshDelayMaxMs); | 77 kRefreshDelayMaxMs); |
| 71 ScheduleRefresh(); | 78 ScheduleRefresh(); |
| 72 } | 79 } |
| 73 | 80 |
| 74 void CloudPolicyRefreshScheduler::RefreshSoon() { | 81 void CloudPolicyRefreshScheduler::RefreshSoon() { |
| 75 rate_limiter_.PostRequest(); | 82 rate_limiter_.PostRequest(); |
| 76 } | 83 } |
| 77 | 84 |
| 85 void CloudPolicyRefreshScheduler::RefreshForInvalidation() { | |
| 86 // Remove the first refresh time restriction, as the intent is to prevent | |
| 87 // refreshes on login from occurring before invalidations can be processed. | |
| 88 first_refresh_ = base::Time(); | |
| 89 RefreshNow(); | |
| 90 } | |
| 91 | |
| 78 void CloudPolicyRefreshScheduler::OnPolicyFetched(CloudPolicyClient* client) { | 92 void CloudPolicyRefreshScheduler::OnPolicyFetched(CloudPolicyClient* client) { |
| 79 error_retry_delay_ms_ = kInitialErrorRetryDelayMs; | 93 error_retry_delay_ms_ = kInitialErrorRetryDelayMs; |
| 80 | 94 |
| 81 // Schedule the next refresh. | 95 // Schedule the next refresh. |
| 82 last_refresh_ = base::Time::NowFromSystemTime(); | 96 last_refresh_ = base::Time::NowFromSystemTime(); |
| 83 ScheduleRefresh(); | 97 ScheduleRefresh(); |
| 84 } | 98 } |
| 85 | 99 |
| 86 void CloudPolicyRefreshScheduler::OnRegistrationStateChanged( | 100 void CloudPolicyRefreshScheduler::OnRegistrationStateChanged( |
| 87 CloudPolicyClient* client) { | 101 CloudPolicyClient* client) { |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 | 226 |
| 213 // This should never happen, as the registration change should have been | 227 // This should never happen, as the registration change should have been |
| 214 // handled via OnRegistrationStateChanged(). | 228 // handled via OnRegistrationStateChanged(). |
| 215 NOTREACHED(); | 229 NOTREACHED(); |
| 216 } | 230 } |
| 217 | 231 |
| 218 void CloudPolicyRefreshScheduler::RefreshAfter(int delta_ms) { | 232 void CloudPolicyRefreshScheduler::RefreshAfter(int delta_ms) { |
| 219 base::TimeDelta delta(base::TimeDelta::FromMilliseconds(delta_ms)); | 233 base::TimeDelta delta(base::TimeDelta::FromMilliseconds(delta_ms)); |
| 220 refresh_callback_.Cancel(); | 234 refresh_callback_.Cancel(); |
| 221 | 235 |
| 236 // Make sure the refresh does not occur before the first refresh time. | |
|
rlarocque
2013/07/23 17:43:08
Did cloud policy refresh within minutes of restart
Joao da Silva
2013/07/23 20:44:47
On the desktop, yes (on Android we will delay it d
Steve Condie
2013/07/24 01:42:04
As we discussed I reverted all my changes to Cloud
| |
| 237 base::Time now = base::Time::NowFromSystemTime(); | |
| 238 base::TimeDelta min_delay; | |
| 239 if (now < first_refresh_) | |
| 240 min_delay = first_refresh_ - now; | |
| 241 | |
| 222 // Schedule the callback. | 242 // Schedule the callback. |
| 223 base::TimeDelta delay = | 243 base::TimeDelta delay = std::max((last_refresh_ + delta) - now, min_delay); |
| 224 std::max((last_refresh_ + delta) - base::Time::NowFromSystemTime(), | |
| 225 base::TimeDelta()); | |
| 226 refresh_callback_.Reset( | 244 refresh_callback_.Reset( |
| 227 base::Bind(&CloudPolicyRefreshScheduler::PerformRefresh, | 245 base::Bind(&CloudPolicyRefreshScheduler::PerformRefresh, |
| 228 base::Unretained(this))); | 246 base::Unretained(this))); |
| 229 task_runner_->PostDelayedTask(FROM_HERE, refresh_callback_.callback(), delay); | 247 task_runner_->PostDelayedTask(FROM_HERE, refresh_callback_.callback(), delay); |
| 230 } | 248 } |
| 231 | 249 |
| 232 } // namespace policy | 250 } // namespace policy |
| OLD | NEW |