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|, |store| and |prefs| 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 // CloudPolicyClient::Observer: 66 virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE; 67 virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE; 68 virtual void OnClientError(CloudPolicyClient* client) OVERRIDE; 69 70 // CloudPolicyStore::Observer: 71 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE; 72 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE; 73 74 // net::NetworkChangeNotifier::IPAddressObserver: 75 virtual void OnIPAddressChanged() OVERRIDE; 76 77 private: 78 // Initializes |last_refresh_| to the policy timestamp from |store_| in case 79 // there is policy present that indicates this client is not managed. This 80 // results in policy fetches only to occur after the entire unmanaged refresh 81 // delay expires, even over restarts. For managed clients, we want to trigger 82 // a refresh on every restart. 83 void UpdateLastRefreshFromPolicy(); 84 85 // Schedules a refresh to be performed immediately. 86 void RefreshNow(); 87 88 // Evaluates when the next refresh is pending and updates the callback to 89 // execute that refresh at the appropriate time. 90 void ScheduleRefresh(); 91 92 // Triggers a policy refresh. 93 void PerformRefresh(); 94 95 // Schedules a policy refresh to happen after |delta_ms| milliseconds, 96 // relative to |last_refresh_|. 97 void RefreshAfter(int delta_ms); 98 99 // Sets the |wait_for_invalidations_timeout_callback_| and schedules it. 100 void WaitForInvalidationService(); 101 102 // Callback for |wait_for_invalidations_timeout_callback_|. 103 void OnWaitForInvalidationServiceTimeout(); 104 105 // Returns true if the refresh scheduler is currently waiting for the 106 // availability of the invalidations service. 107 bool WaitingForInvalidationService() const; 108 109 CloudPolicyClient* client_; 110 CloudPolicyStore* store_; 111 112 // For scheduling delayed tasks. 113 const scoped_refptr<base::SequencedTaskRunner> task_runner_; 114 115 // The delayed refresh callback. 116 base::CancelableClosure refresh_callback_; 117 118 // The last time a refresh callback completed. 119 base::Time last_refresh_; 120 121 // Error retry delay in milliseconds. 122 int64 error_retry_delay_ms_; 123 124 // The refresh delay. 125 int64 refresh_delay_ms_; 126 127 // Used to limit the rate at which refreshes are scheduled. 128 RateLimiter rate_limiter_; 129 130 // Whether the invalidations service is available and receiving notifications 131 // of policy updates. 132 bool invalidations_available_; 133 134 // The refresh scheduler waits some seconds for the invalidations service 135 // before starting to issue refresh requests. If the invalidations service 136 // doesn't become available during this time then the refresh scheduler will 137 // use the polling refresh rate. 138 base::CancelableClosure wait_for_invalidations_timeout_callback_; 139 140 // Used to measure how long it took for the invalidations service to report 141 // its initial status. 142 base::Time creation_time_; 143 144 DISALLOW_COPY_AND_ASSIGN(CloudPolicyRefreshScheduler); 145 }; 146 147 } // namespace policy 148 149 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_REFRESH_SCHEDULER_H_ 150