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   // Sets the |wait_for_invalidations_timeout_callback_| and schedules it.
    107   void WaitForInvalidationService();
    108 
    109   // Callback for |wait_for_invalidations_timeout_callback_|.
    110   void OnWaitForInvalidationServiceTimeout();
    111 
    112   // Returns true if the refresh scheduler is currently waiting for the
    113   // availability of the invalidations service.
    114   bool WaitingForInvalidationService() const;
    115 
    116   CloudPolicyClient* client_;
    117   CloudPolicyStore* store_;
    118 
    119   // For scheduling delayed tasks.
    120   const scoped_refptr<base::SequencedTaskRunner> task_runner_;
    121 
    122   // The delayed refresh callback.
    123   base::CancelableClosure refresh_callback_;
    124 
    125   // The last time a refresh callback completed.
    126   base::Time last_refresh_;
    127 
    128   // Error retry delay in milliseconds.
    129   int64 error_retry_delay_ms_;
    130 
    131   // The refresh delay.
    132   int64 refresh_delay_ms_;
    133 
    134   // Used to limit the rate at which refreshes are scheduled.
    135   RateLimiter rate_limiter_;
    136 
    137   // Whether the invalidations service is available and receiving notifications
    138   // of policy updates.
    139   bool invalidations_available_;
    140 
    141   // The refresh scheduler waits some seconds for the invalidations service
    142   // before starting to issue refresh requests. If the invalidations service
    143   // doesn't become available during this time then the refresh scheduler will
    144   // use the polling refresh rate.
    145   base::CancelableClosure wait_for_invalidations_timeout_callback_;
    146 
    147   // Used to measure how long it took for the invalidations service to report
    148   // its initial status.
    149   base::Time creation_time_;
    150 
    151   DISALLOW_COPY_AND_ASSIGN(CloudPolicyRefreshScheduler);
    152 };
    153 
    154 }  // namespace policy
    155 
    156 #endif  // COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_REFRESH_SCHEDULER_H_
    157