| 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" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 CloudPolicyClient* client, | 28 CloudPolicyClient* client, |
| 29 CloudPolicyStore* store, | 29 CloudPolicyStore* store, |
| 30 const scoped_refptr<base::TaskRunner>& task_runner) | 30 const scoped_refptr<base::TaskRunner>& task_runner) |
| 31 : client_(client), | 31 : client_(client), |
| 32 store_(store), | 32 store_(store), |
| 33 task_runner_(task_runner), | 33 task_runner_(task_runner), |
| 34 error_retry_delay_ms_(kInitialErrorRetryDelayMs), | 34 error_retry_delay_ms_(kInitialErrorRetryDelayMs), |
| 35 refresh_delay_ms_(kDefaultRefreshDelayMs) { | 35 refresh_delay_ms_(kDefaultRefreshDelayMs) { |
| 36 client_->AddObserver(this); | 36 client_->AddObserver(this); |
| 37 store_->AddObserver(this); | 37 store_->AddObserver(this); |
| 38 net::NetworkChangeNotifier::AddIPAddressObserver(this); | 38 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); |
| 39 | 39 |
| 40 UpdateLastRefreshFromPolicy(); | 40 UpdateLastRefreshFromPolicy(); |
| 41 ScheduleRefresh(); | 41 ScheduleRefresh(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 CloudPolicyRefreshScheduler::~CloudPolicyRefreshScheduler() { | 44 CloudPolicyRefreshScheduler::~CloudPolicyRefreshScheduler() { |
| 45 store_->RemoveObserver(this); | 45 store_->RemoveObserver(this); |
| 46 client_->RemoveObserver(this); | 46 client_->RemoveObserver(this); |
| 47 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 47 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
| 48 } | 48 } |
| 49 | 49 |
| 50 void CloudPolicyRefreshScheduler::SetRefreshDelay(int64 refresh_delay) { | 50 void CloudPolicyRefreshScheduler::SetRefreshDelay(int64 refresh_delay) { |
| 51 refresh_delay_ms_ = std::min(std::max(refresh_delay, kRefreshDelayMinMs), | 51 refresh_delay_ms_ = std::min(std::max(refresh_delay, kRefreshDelayMinMs), |
| 52 kRefreshDelayMaxMs); | 52 kRefreshDelayMaxMs); |
| 53 ScheduleRefresh(); | 53 ScheduleRefresh(); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void CloudPolicyRefreshScheduler::OnPolicyFetched(CloudPolicyClient* client) { | 56 void CloudPolicyRefreshScheduler::OnPolicyFetched(CloudPolicyClient* client) { |
| 57 error_retry_delay_ms_ = kInitialErrorRetryDelayMs; | 57 error_retry_delay_ms_ = kInitialErrorRetryDelayMs; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 ScheduleRefresh(); | 96 ScheduleRefresh(); |
| 97 } | 97 } |
| 98 | 98 |
| 99 void CloudPolicyRefreshScheduler::OnStoreError(CloudPolicyStore* store) { | 99 void CloudPolicyRefreshScheduler::OnStoreError(CloudPolicyStore* store) { |
| 100 // If |store_| fails, the is_managed bit that it provides may become stale. | 100 // If |store_| fails, the is_managed bit that it provides may become stale. |
| 101 // The best guess in that situation is to assume is_managed didn't change and | 101 // The best guess in that situation is to assume is_managed didn't change and |
| 102 // continue using the stale information. Thus, no specific response to a store | 102 // continue using the stale information. Thus, no specific response to a store |
| 103 // error is required. NB: Changes to is_managed fire OnStoreLoaded(). | 103 // error is required. NB: Changes to is_managed fire OnStoreLoaded(). |
| 104 } | 104 } |
| 105 | 105 |
| 106 void CloudPolicyRefreshScheduler::OnIPAddressChanged() { | 106 void CloudPolicyRefreshScheduler::OnNetworkChanged( |
| 107 if (client_->status() == DM_STATUS_REQUEST_FAILED) | 107 net::NetworkChangeNotifier::ConnectionType type) { |
| 108 if (type != net::NetworkChangeNotifier::CONNECTION_NONE && |
| 109 client_->status() == DM_STATUS_REQUEST_FAILED) |
| 108 RefreshAfter(0); | 110 RefreshAfter(0); |
| 109 } | 111 } |
| 110 | 112 |
| 111 void CloudPolicyRefreshScheduler::UpdateLastRefreshFromPolicy() { | 113 void CloudPolicyRefreshScheduler::UpdateLastRefreshFromPolicy() { |
| 112 if (!last_refresh_.is_null()) | 114 if (!last_refresh_.is_null()) |
| 113 return; | 115 return; |
| 114 | 116 |
| 115 // If the client has already fetched policy, assume that happened recently. If | 117 // If the client has already fetched policy, assume that happened recently. If |
| 116 // that assumption ever breaks, the proper thing to do probably is to move the | 118 // that assumption ever breaks, the proper thing to do probably is to move the |
| 117 // |last_refresh_| bookkeeping to CloudPolicyClient. | 119 // |last_refresh_| bookkeeping to CloudPolicyClient. |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 base::TimeDelta delay = | 199 base::TimeDelta delay = |
| 198 std::max((last_refresh_ + delta) - base::Time::NowFromSystemTime(), | 200 std::max((last_refresh_ + delta) - base::Time::NowFromSystemTime(), |
| 199 base::TimeDelta()); | 201 base::TimeDelta()); |
| 200 refresh_callback_.Reset( | 202 refresh_callback_.Reset( |
| 201 base::Bind(&CloudPolicyRefreshScheduler::PerformRefresh, | 203 base::Bind(&CloudPolicyRefreshScheduler::PerformRefresh, |
| 202 base::Unretained(this))); | 204 base::Unretained(this))); |
| 203 task_runner_->PostDelayedTask(FROM_HERE, refresh_callback_.callback(), delay); | 205 task_runner_->PostDelayedTask(FROM_HERE, refresh_callback_.callback(), delay); |
| 204 } | 206 } |
| 205 | 207 |
| 206 } // namespace policy | 208 } // namespace policy |
| OLD | NEW |