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.
5 #ifndef COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_REFRESH_SCHEDULER_H_
6 #define COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_REFRESH_SCHEDULER_H_
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 "components/policy/core/common/cloud/cloud_policy_client.h"
13 #include "components/policy/core/common/cloud/cloud_policy_store.h"
14 #include "components/policy/policy_export.h"
15 #include "net/base/network_change_notifier.h"
18 class SequencedTaskRunner;
23 // Observes CloudPolicyClient and CloudPolicyStore to trigger periodic policy
24 // fetches and issue retries on error conditions.
25 class POLICY_EXPORT CloudPolicyRefreshScheduler
26 : public CloudPolicyClient::Observer,
27 public CloudPolicyStore::Observer,
28 public net::NetworkChangeNotifier::IPAddressObserver {
31 static const int64 kDefaultRefreshDelayMs;
32 static const int64 kUnmanagedRefreshDelayMs;
33 static const int64 kWithInvalidationsRefreshDelayMs;
34 static const int64 kInitialErrorRetryDelayMs;
36 // Refresh delay bounds.
37 static const int64 kRefreshDelayMinMs;
38 static const int64 kRefreshDelayMaxMs;
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();
48 base::Time last_refresh() const { return last_refresh_; }
49 int64 refresh_delay() const { return refresh_delay_ms_; }
51 // Sets the refresh delay to |refresh_delay| (subject to min/max clamping).
52 void SetRefreshDelay(int64 refresh_delay);
54 // Requests a policy refresh to be performed soon.
57 // The refresh scheduler starts by assuming that invalidations are not
58 // available. This call can be used to signal whether the invalidations
59 // service is available or not, and can be called multiple times.
60 // When the invalidations service is available then the refresh rate is much
62 void SetInvalidationServiceAvailability(bool is_available);
64 // Whether the invalidations service is available and receiving notifications
66 bool invalidations_available() {
67 return invalidations_available_;
70 // CloudPolicyClient::Observer:
71 virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE;
72 virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE;
73 virtual void OnClientError(CloudPolicyClient* client) OVERRIDE;
75 // CloudPolicyStore::Observer:
76 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE;
77 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE;
79 // net::NetworkChangeNotifier::IPAddressObserver:
80 virtual void OnIPAddressChanged() OVERRIDE;
83 // Initializes |last_refresh_| to the policy timestamp from |store_| in case
84 // there is policy present that indicates this client is not managed. This
85 // results in policy fetches only to occur after the entire unmanaged refresh
86 // delay expires, even over restarts. For managed clients, we want to trigger
87 // a refresh on every restart.
88 void UpdateLastRefreshFromPolicy();
90 // Schedules a refresh to be performed immediately.
93 // Evaluates when the next refresh is pending and updates the callback to
94 // execute that refresh at the appropriate time.
95 void ScheduleRefresh();
97 // Triggers a policy refresh.
98 void PerformRefresh();
100 // Schedules a policy refresh to happen after |delta_ms| milliseconds,
101 // relative to |last_refresh_|.
102 void RefreshAfter(int delta_ms);
104 CloudPolicyClient* client_;
105 CloudPolicyStore* store_;
107 // For scheduling delayed tasks.
108 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
110 // The delayed refresh callback.
111 base::CancelableClosure refresh_callback_;
113 // The last time a refresh callback completed.
114 base::Time last_refresh_;
116 // Error retry delay in milliseconds.
117 int64 error_retry_delay_ms_;
119 // The refresh delay.
120 int64 refresh_delay_ms_;
122 // Whether the invalidations service is available and receiving notifications
123 // of policy updates.
124 bool invalidations_available_;
126 // Used to measure how long it took for the invalidations service to report
127 // its initial status.
128 base::Time creation_time_;
130 DISALLOW_COPY_AND_ASSIGN(CloudPolicyRefreshScheduler);
133 } // namespace policy
135 #endif // COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_REFRESH_SCHEDULER_H_