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_policy_refresh_scheduler.h" | 5 #include "chrome/browser/policy/cloud_policy_refresh_scheduler.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/task_runner.h" | 9 #include "base/task_runner.h" |
| 10 #include "chrome/browser/policy/cloud_policy_constants.h" | 10 #include "chrome/browser/policy/cloud_policy_constants.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" | 11 #include "chrome/browser/prefs/pref_service.h" |
|
bartfab (slow)
2012/12/04 12:25:18
Nit: No longer needed.
Mattias Nissler (ping if slow)
2012/12/04 13:20:40
Done.
| |
| 12 #include "chrome/common/chrome_notification_types.h" | 12 #include "chrome/common/chrome_notification_types.h" |
| 13 #include "content/public/browser/notification_details.h" | 13 #include "content/public/browser/notification_details.h" |
| 14 | 14 |
| 15 namespace policy { | 15 namespace policy { |
| 16 | 16 |
| 17 const int64 CloudPolicyRefreshScheduler::kDefaultRefreshDelayMs = | |
| 18 3 * 60 * 60 * 1000; // 3 hours. | |
| 17 const int64 CloudPolicyRefreshScheduler::kUnmanagedRefreshDelayMs = | 19 const int64 CloudPolicyRefreshScheduler::kUnmanagedRefreshDelayMs = |
| 18 24 * 60 * 60 * 1000; // 1 day. | 20 24 * 60 * 60 * 1000; // 1 day. |
| 19 const int64 CloudPolicyRefreshScheduler::kInitialErrorRetryDelayMs = | 21 const int64 CloudPolicyRefreshScheduler::kInitialErrorRetryDelayMs = |
| 20 5 * 60 * 1000; // 5 minutes. | 22 5 * 60 * 1000; // 5 minutes. |
| 21 const int64 CloudPolicyRefreshScheduler::kRefreshDelayMinMs = | 23 const int64 CloudPolicyRefreshScheduler::kRefreshDelayMinMs = |
| 22 30 * 60 * 1000; // 30 minutes. | 24 30 * 60 * 1000; // 30 minutes. |
| 23 const int64 CloudPolicyRefreshScheduler::kRefreshDelayMaxMs = | 25 const int64 CloudPolicyRefreshScheduler::kRefreshDelayMaxMs = |
| 24 24 * 60 * 60 * 1000; // 1 day. | 26 24 * 60 * 60 * 1000; // 1 day. |
| 25 | 27 |
| 26 CloudPolicyRefreshScheduler::CloudPolicyRefreshScheduler( | 28 CloudPolicyRefreshScheduler::CloudPolicyRefreshScheduler( |
| 27 CloudPolicyClient* client, | 29 CloudPolicyClient* client, |
| 28 CloudPolicyStore* store, | 30 CloudPolicyStore* store, |
| 29 PrefService* prefs, | |
| 30 const std::string& refresh_pref, | |
| 31 const scoped_refptr<base::TaskRunner>& task_runner) | 31 const scoped_refptr<base::TaskRunner>& task_runner) |
| 32 : client_(client), | 32 : client_(client), |
| 33 store_(store), | 33 store_(store), |
| 34 task_runner_(task_runner), | 34 task_runner_(task_runner), |
| 35 error_retry_delay_ms_(kInitialErrorRetryDelayMs) { | 35 error_retry_delay_ms_(kInitialErrorRetryDelayMs), |
| 36 refresh_delay_ms_(kDefaultRefreshDelayMs) { | |
| 36 client_->AddObserver(this); | 37 client_->AddObserver(this); |
| 37 store_->AddObserver(this); | 38 store_->AddObserver(this); |
| 38 net::NetworkChangeNotifier::AddIPAddressObserver(this); | 39 net::NetworkChangeNotifier::AddIPAddressObserver(this); |
| 39 | 40 |
| 40 refresh_delay_.Init( | |
| 41 refresh_pref.c_str(), prefs, | |
| 42 base::Bind(&CloudPolicyRefreshScheduler::ScheduleRefresh, | |
| 43 base::Unretained(this))); | |
| 44 | |
| 45 UpdateLastRefreshFromPolicy(); | 41 UpdateLastRefreshFromPolicy(); |
| 46 ScheduleRefresh(); | 42 ScheduleRefresh(); |
| 47 } | 43 } |
| 48 | 44 |
| 49 CloudPolicyRefreshScheduler::~CloudPolicyRefreshScheduler() { | 45 CloudPolicyRefreshScheduler::~CloudPolicyRefreshScheduler() { |
| 50 store_->RemoveObserver(this); | 46 store_->RemoveObserver(this); |
| 51 client_->RemoveObserver(this); | 47 client_->RemoveObserver(this); |
| 52 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 48 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 53 } | 49 } |
| 54 | 50 |
| 51 void CloudPolicyRefreshScheduler::SetRefreshDelay(int64 refresh_delay) { | |
| 52 refresh_delay_ms_ = refresh_delay; | |
|
bartfab (slow)
2012/12/04 12:25:18
Why not clamp when setting the value here instead
Mattias Nissler (ping if slow)
2012/12/04 13:20:40
Doesn't matter really, but I guess what you propos
| |
| 53 ScheduleRefresh(); | |
| 54 } | |
| 55 | |
| 55 void CloudPolicyRefreshScheduler::OnPolicyFetched(CloudPolicyClient* client) { | 56 void CloudPolicyRefreshScheduler::OnPolicyFetched(CloudPolicyClient* client) { |
| 56 error_retry_delay_ms_ = kInitialErrorRetryDelayMs; | 57 error_retry_delay_ms_ = kInitialErrorRetryDelayMs; |
| 57 | 58 |
| 58 // Schedule the next refresh. | 59 // Schedule the next refresh. |
| 59 last_refresh_ = base::Time::NowFromSystemTime(); | 60 last_refresh_ = base::Time::NowFromSystemTime(); |
| 60 ScheduleRefresh(); | 61 ScheduleRefresh(); |
| 61 } | 62 } |
| 62 | 63 |
| 63 void CloudPolicyRefreshScheduler::OnRegistrationStateChanged( | 64 void CloudPolicyRefreshScheduler::OnRegistrationStateChanged( |
| 64 CloudPolicyClient* client) { | 65 CloudPolicyClient* client) { |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 base::TimeDelta delay = | 197 base::TimeDelta delay = |
| 197 std::max((last_refresh_ + delta) - base::Time::NowFromSystemTime(), | 198 std::max((last_refresh_ + delta) - base::Time::NowFromSystemTime(), |
| 198 base::TimeDelta()); | 199 base::TimeDelta()); |
| 199 refresh_callback_.Reset( | 200 refresh_callback_.Reset( |
| 200 base::Bind(&CloudPolicyRefreshScheduler::PerformRefresh, | 201 base::Bind(&CloudPolicyRefreshScheduler::PerformRefresh, |
| 201 base::Unretained(this))); | 202 base::Unretained(this))); |
| 202 task_runner_->PostDelayedTask(FROM_HERE, refresh_callback_.callback(), delay); | 203 task_runner_->PostDelayedTask(FROM_HERE, refresh_callback_.callback(), delay); |
| 203 } | 204 } |
| 204 | 205 |
| 205 int64 CloudPolicyRefreshScheduler::GetRefreshDelay() { | 206 int64 CloudPolicyRefreshScheduler::GetRefreshDelay() { |
| 206 return std::min(std::max<int64>(refresh_delay_.GetValue(), | 207 return std::min(std::max(refresh_delay_ms_, kRefreshDelayMinMs), |
| 207 kRefreshDelayMinMs), | |
| 208 kRefreshDelayMaxMs); | 208 kRefreshDelayMaxMs); |
| 209 } | 209 } |
| 210 | 210 |
| 211 } // namespace policy | 211 } // namespace policy |
| OLD | NEW |