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/policy_export.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 POLICY_EXPORT 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.
     55   void RefreshSoon();
     56 
     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
     61   // lower.
     62   void SetInvalidationServiceAvailability(bool is_available);
     63 
     64   // Whether the invalidations service is available and receiving notifications
     65   // of policy updates.
     66   bool invalidations_available() {
     67     return invalidations_available_;
     68   }
     69 
     70   // CloudPolicyClient::Observer:
     71   virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE;
     72   virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE;
     73   virtual void OnClientError(CloudPolicyClient* client) OVERRIDE;
     74 
     75   // CloudPolicyStore::Observer:
     76   virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE;
     77   virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE;
     78 
     79   // net::NetworkChangeNotifier::IPAddressObserver:
     80   virtual void OnIPAddressChanged() OVERRIDE;
     81 
     82  private:
     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();
     89 
     90   // Schedules a refresh to be performed immediately.
     91   void RefreshNow();
     92 
     93   // Evaluates when the next refresh is pending and updates the callback to
     94   // execute that refresh at the appropriate time.
     95   void ScheduleRefresh();
     96 
     97   // Triggers a policy refresh.
     98   void PerformRefresh();
     99 
    100   // Schedules a policy refresh to happen after |delta_ms| milliseconds,
    101   // relative to |last_refresh_|.
    102   void RefreshAfter(int delta_ms);
    103 
    104   CloudPolicyClient* client_;
    105   CloudPolicyStore* store_;
    106 
    107   // For scheduling delayed tasks.
    108   const scoped_refptr<base::SequencedTaskRunner> task_runner_;
    109 
    110   // The delayed refresh callback.
    111   base::CancelableClosure refresh_callback_;
    112 
    113   // The last time a refresh callback completed.
    114   base::Time last_refresh_;
    115 
    116   // Error retry delay in milliseconds.
    117   int64 error_retry_delay_ms_;
    118 
    119   // The refresh delay.
    120   int64 refresh_delay_ms_;
    121 
    122   // Whether the invalidations service is available and receiving notifications
    123   // of policy updates.
    124   bool invalidations_available_;
    125 
    126   // Used to measure how long it took for the invalidations service to report
    127   // its initial status.
    128   base::Time creation_time_;
    129 
    130   DISALLOW_COPY_AND_ASSIGN(CloudPolicyRefreshScheduler);
    131 };
    132 
    133 }  // namespace policy
    134 
    135 #endif  // COMPONENTS_POLICY_CORE_COMMON_CLOUD_CLOUD_POLICY_REFRESH_SCHEDULER_H_
    136