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_CLOUD_POLICY_REFRESH_SCHEDULER_H_ | |
6 #define CHROME_BROWSER_POLICY_CLOUD_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/time.h" | |
12 #include "chrome/browser/policy/cloud/cloud_policy_client.h" | |
13 #include "chrome/browser/policy/cloud/cloud_policy_store.h" | |
14 #include "chrome/browser/policy/cloud/rate_limiter.h" | |
15 #include "net/base/network_change_notifier.h" | |
16 | |
17 namespace base { | |
18 class SequencedTaskRunner; | |
19 } | |
20 | |
21 namespace policy { | |
22 | |
23 // Observes CloudPolicyClient and CloudPolicyStore to trigger periodic policy | |
24 // fetches and issue retries on error conditions. | |
25 class CloudPolicyRefreshScheduler | |
26 : public CloudPolicyClient::Observer, | |
27 public CloudPolicyStore::Observer, | |
28 public net::NetworkChangeNotifier::IPAddressObserver { | |
29 public: | |
30 // Refresh constants. | |
31 static const int64 kDefaultRefreshDelayMs; | |
32 static const int64 kUnmanagedRefreshDelayMs; | |
33 static const int64 kWithInvalidationsRefreshDelayMs; | |
34 static const int64 kInitialErrorRetryDelayMs; | |
35 | |
36 // Refresh delay bounds. | |
37 static const int64 kRefreshDelayMinMs; | |
38 static const int64 kRefreshDelayMaxMs; | |
39 | |
40 // |client| and |store| pointers must stay valid throughout the | |
41 // lifetime of CloudPolicyRefreshScheduler. | |
42 CloudPolicyRefreshScheduler( | |
43 CloudPolicyClient* client, | |
44 CloudPolicyStore* store, | |
45 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | |
46 virtual ~CloudPolicyRefreshScheduler(); | |
47 | |
48 base::Time last_refresh() const { return last_refresh_; } | |
49 int64 refresh_delay() const { return refresh_delay_ms_; } | |
50 | |
51 // Sets the refresh delay to |refresh_delay| (subject to min/max clamping). | |
52 void SetRefreshDelay(int64 refresh_delay); | |
53 | |
54 // Requests a policy refresh to be performed soon. This may apply throttling, | |
55 // and the request may not be immediately sent. | |
56 void RefreshSoon(); | |
57 | |
58 // The refresh scheduler starts by assuming that invalidations are not | |
59 // available. This call can be used to signal whether the invalidations | |
60 // service is available or not, and can be called multiple times. | |
61 // When the invalidations service is available then the refresh rate is much | |
62 // lower. | |
63 void SetInvalidationServiceAvailability(bool is_available); | |
64 | |
65 // Whether the invalidations service is available and receiving notifications | |
66 // of policy updates. | |
67 bool invalidations_available() { | |
68 return invalidations_available_; | |
69 } | |
70 | |
71 // CloudPolicyClient::Observer: | |
72 virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE; | |
73 virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE; | |
74 virtual void OnClientError(CloudPolicyClient* client) OVERRIDE; | |
75 | |
76 // CloudPolicyStore::Observer: | |
77 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE; | |
78 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE; | |
79 | |
80 // net::NetworkChangeNotifier::IPAddressObserver: | |
81 virtual void OnIPAddressChanged() OVERRIDE; | |
82 | |
83 private: | |
84 // Initializes |last_refresh_| to the policy timestamp from |store_| in case | |
85 // there is policy present that indicates this client is not managed. This | |
86 // results in policy fetches only to occur after the entire unmanaged refresh | |
87 // delay expires, even over restarts. For managed clients, we want to trigger | |
88 // a refresh on every restart. | |
89 void UpdateLastRefreshFromPolicy(); | |
90 | |
91 // Schedules a refresh to be performed immediately. | |
92 void RefreshNow(); | |
93 | |
94 // Evaluates when the next refresh is pending and updates the callback to | |
95 // execute that refresh at the appropriate time. | |
96 void ScheduleRefresh(); | |
97 | |
98 // Triggers a policy refresh. | |
99 void PerformRefresh(); | |
100 | |
101 // Schedules a policy refresh to happen after |delta_ms| milliseconds, | |
102 // relative to |last_refresh_|. | |
103 void RefreshAfter(int delta_ms); | |
104 | |
105 // Sets the |wait_for_invalidations_timeout_callback_| and schedules it. | |
106 void WaitForInvalidationService(); | |
107 | |
108 // Callback for |wait_for_invalidations_timeout_callback_|. | |
109 void OnWaitForInvalidationServiceTimeout(); | |
110 | |
111 // Returns true if the refresh scheduler is currently waiting for the | |
112 // availability of the invalidations service. | |
113 bool WaitingForInvalidationService() const; | |
114 | |
115 CloudPolicyClient* client_; | |
116 CloudPolicyStore* store_; | |
117 | |
118 // For scheduling delayed tasks. | |
119 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
120 | |
121 // The delayed refresh callback. | |
122 base::CancelableClosure refresh_callback_; | |
123 | |
124 // The last time a refresh callback completed. | |
125 base::Time last_refresh_; | |
126 | |
127 // Error retry delay in milliseconds. | |
128 int64 error_retry_delay_ms_; | |
129 | |
130 // The refresh delay. | |
131 int64 refresh_delay_ms_; | |
132 | |
133 // Used to limit the rate at which refreshes are scheduled. | |
134 RateLimiter rate_limiter_; | |
135 | |
136 // Whether the invalidations service is available and receiving notifications | |
137 // of policy updates. | |
138 bool invalidations_available_; | |
139 | |
140 // The refresh scheduler waits some seconds for the invalidations service | |
141 // before starting to issue refresh requests. If the invalidations service | |
142 // doesn't become available during this time then the refresh scheduler will | |
143 // use the polling refresh rate. | |
144 base::CancelableClosure wait_for_invalidations_timeout_callback_; | |
145 | |
146 // Used to measure how long it took for the invalidations service to report | |
147 // its initial status. | |
148 base::Time creation_time_; | |
149 | |
150 DISALLOW_COPY_AND_ASSIGN(CloudPolicyRefreshScheduler); | |
151 }; | |
152 | |
153 } // namespace policy | |
154 | |
155 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_REFRESH_SCHEDULER_H_ | |
OLD | NEW |