OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_POLICY_CLOUD_POLICY_REFRESH_SCHEDULER_H_ | |
6 #define CHROME_BROWSER_POLICY_CLOUD_POLICY_REFRESH_SCHEDULER_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/cancelable_callback.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/time.h" | |
12 #include "chrome/browser/policy/cloud_policy_client.h" | |
13 #include "chrome/browser/policy/cloud_policy_store.h" | |
14 #include "net/base/network_change_notifier.h" | |
15 | |
16 namespace base { | |
17 class TaskRunner; | |
18 } | |
19 | |
20 namespace policy { | |
21 | |
22 // Observes CloudPolicyClient and CloudPolicyStore to trigger periodic policy | |
23 // fetches and issue retries on error conditions. | |
24 class CloudPolicyRefreshScheduler | |
25 : public CloudPolicyClient::Observer, | |
26 public CloudPolicyStore::Observer, | |
27 public net::NetworkChangeNotifier::IPAddressObserver { | |
28 public: | |
29 // Refresh constants. | |
30 static const int64 kDefaultRefreshDelayMs; | |
31 static const int64 kUnmanagedRefreshDelayMs; | |
32 static const int64 kInitialErrorRetryDelayMs; | |
33 | |
34 // Refresh delay bounds. | |
35 static const int64 kRefreshDelayMinMs; | |
36 static const int64 kRefreshDelayMaxMs; | |
37 | |
38 // |client|, |store| and |prefs| pointers must stay valid throughout the | |
39 // lifetime of CloudPolicyRefreshScheduler. | |
40 CloudPolicyRefreshScheduler( | |
41 CloudPolicyClient* client, | |
42 CloudPolicyStore* store, | |
43 const scoped_refptr<base::TaskRunner>& task_runner); | |
44 virtual ~CloudPolicyRefreshScheduler(); | |
45 | |
46 base::Time last_refresh() const { return last_refresh_; } | |
47 int64 refresh_delay() const { return refresh_delay_ms_; } | |
48 | |
49 // Sets the refresh delay to |refresh_delay| (subject to min/max clamping). | |
50 void SetRefreshDelay(int64 refresh_delay); | |
51 | |
52 // CloudPolicyClient::Observer: | |
53 virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE; | |
54 virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE; | |
55 virtual void OnClientError(CloudPolicyClient* client) OVERRIDE; | |
56 | |
57 // CloudPolicyStore::Observer: | |
58 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE; | |
59 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE; | |
60 | |
61 // net::NetworkChangeNotifier::IPAddressObserver: | |
62 virtual void OnIPAddressChanged() OVERRIDE; | |
63 | |
64 private: | |
65 // Initializes |last_refresh_| to the policy timestamp from |store_| in case | |
66 // there is policy present that indicates this client is not managed. This | |
67 // results in policy fetches only to occur after the entire unmanaged refresh | |
68 // delay expires, even over restarts. For managed clients, we want to trigger | |
69 // a refresh on every restart. | |
70 void UpdateLastRefreshFromPolicy(); | |
71 | |
72 // Evaluates when the next refresh is pending and updates the callback to | |
73 // execute that refresh at the appropriate time. | |
74 void ScheduleRefresh(); | |
75 | |
76 // Triggers a policy refresh. | |
77 void PerformRefresh(); | |
78 | |
79 // Schedules a policy refresh to happen after |delta_ms| milliseconds, | |
80 // relative to |last_refresh_|. | |
81 void RefreshAfter(int delta_ms); | |
82 | |
83 CloudPolicyClient* client_; | |
84 CloudPolicyStore* store_; | |
85 | |
86 // For scheduling delayed tasks. | |
87 const scoped_refptr<base::TaskRunner> task_runner_; | |
88 | |
89 // The delayed refresh callback. | |
90 base::CancelableClosure refresh_callback_; | |
91 | |
92 // The last time a refresh callback completed. | |
93 base::Time last_refresh_; | |
94 | |
95 // Error retry delay in milliseconds. | |
96 int64 error_retry_delay_ms_; | |
97 | |
98 // The refresh delay. | |
99 int64 refresh_delay_ms_; | |
100 | |
101 DISALLOW_COPY_AND_ASSIGN(CloudPolicyRefreshScheduler); | |
102 }; | |
103 | |
104 } // namespace policy | |
105 | |
106 #endif // CHROME_BROWSER_POLICY_CLOUD_POLICY_REFRESH_SCHEDULER_H_ | |
OLD | NEW |