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