| OLD | NEW | 
|    1 // Copyright 2013 The Chromium Authors. All rights reserved. |    1 // Copyright 2013 The Chromium Authors. All rights reserved. | 
|    2 // Use of this source code is governed by a BSD-style license that can be |    2 // Use of this source code is governed by a BSD-style license that can be | 
|    3 // found in the LICENSE file. |    3 // found in the LICENSE file. | 
|    4  |    4  | 
|    5 #ifndef CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ |    5 #ifndef CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ | 
|    6 #define CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ |    6 #define CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ | 
|    7  |    7  | 
|    8 #include <string> |    8 #include <string> | 
|    9  |    9  | 
|   10 #include "base/basictypes.h" |   10 #include "base/basictypes.h" | 
|   11 #include "base/callback.h" |   11 #include "base/callback.h" | 
|   12 #include "base/memory/ref_counted.h" |   12 #include "base/memory/ref_counted.h" | 
|   13 #include "base/memory/weak_ptr.h" |   13 #include "base/memory/weak_ptr.h" | 
|   14 #include "base/threading/thread_checker.h" |   14 #include "base/threading/thread_checker.h" | 
 |   15 #include "chrome/browser/policy/cloud/cloud_policy_core.h" | 
|   15 #include "chrome/browser/policy/cloud/cloud_policy_store.h" |   16 #include "chrome/browser/policy/cloud/cloud_policy_store.h" | 
|   16 #include "sync/notifier/invalidation_handler.h" |   17 #include "sync/notifier/invalidation_handler.h" | 
|   17  |   18  | 
|   18 class Profile; |  | 
|   19  |  | 
|   20 namespace base { |   19 namespace base { | 
|   21 class SequencedTaskRunner; |   20 class SequencedTaskRunner; | 
|   22 } |   21 } | 
|   23  |   22  | 
|   24 namespace invalidation { |   23 namespace invalidation { | 
|   25 class InvalidationService; |   24 class InvalidationService; | 
|   26 } |   25 } | 
|   27  |   26  | 
|   28 namespace policy { |   27 namespace policy { | 
|   29  |   28  | 
|   30 class CloudPolicyInvalidationHandler; |  | 
|   31  |  | 
|   32 // Listens for and provides policy invalidations. |   29 // Listens for and provides policy invalidations. | 
|   33 class CloudPolicyInvalidator : public syncer::InvalidationHandler, |   30 class CloudPolicyInvalidator : public syncer::InvalidationHandler, | 
 |   31                                public CloudPolicyCore::Observer, | 
|   34                                public CloudPolicyStore::Observer { |   32                                public CloudPolicyStore::Observer { | 
|   35  public: |   33  public: | 
|   36   // The number of minutes to delay a policy refresh after receiving an |   34   // The number of minutes to delay a policy refresh after receiving an | 
|   37   // invalidation with no payload. |   35   // invalidation with no payload. | 
|   38   static const int kMissingPayloadDelay; |   36   static const int kMissingPayloadDelay; | 
|   39  |   37  | 
|   40   // The default, min and max values for max_fetch_delay_. |   38   // The default, min and max values for max_fetch_delay_. | 
|   41   static const int kMaxFetchDelayDefault; |   39   static const int kMaxFetchDelayDefault; | 
|   42   static const int kMaxFetchDelayMin; |   40   static const int kMaxFetchDelayMin; | 
|   43   static const int kMaxFetchDelayMax; |   41   static const int kMaxFetchDelayMax; | 
|   44  |   42  | 
|   45   // |invalidation_handler| handles invalidations provided by this object and |   43   // |core| is the cloud policy core which connects the various policy objects. | 
|   46   // must remain valid until Shutdown is called. |   44   // It must remain valid until Shutdown is called. | 
|   47   // |store| is cloud policy store. It must remain valid until Shutdown is |  | 
|   48   // called. |  | 
|   49   // |task_runner| is used for scheduling delayed tasks. It must post tasks to |   45   // |task_runner| is used for scheduling delayed tasks. It must post tasks to | 
|   50   // the main policy thread. |   46   // the main policy thread. | 
|   51   CloudPolicyInvalidator( |   47   CloudPolicyInvalidator( | 
|   52       CloudPolicyInvalidationHandler* invalidation_handler, |   48       CloudPolicyCore* core, | 
|   53       CloudPolicyStore* store, |  | 
|   54       const scoped_refptr<base::SequencedTaskRunner>& task_runner); |   49       const scoped_refptr<base::SequencedTaskRunner>& task_runner); | 
|   55   virtual ~CloudPolicyInvalidator(); |   50   virtual ~CloudPolicyInvalidator(); | 
|   56  |   51  | 
|   57   // Initializes the invalidator with the given profile. The invalidator uses |   52   // Initializes the invalidator. No invalidations will be generated before this | 
|   58   // the profile to get a reference to the profile's invalidation service if |   53   // method is called. This method must only be called once. | 
|   59   // needed. Both the profile and the invalidation service must remain valid |   54   // |get_invalidation_service| is a callback which gets a pointer to the | 
|   60   // until Shutdown is called. An Initialize method must only be called once. |   55   // invalidation service. It must be callable until Shutdown is called and the | 
|   61   void InitializeWithProfile(Profile* profile); |   56   // pointer returned must also remain valid during that time. A callback is | 
|   62  |   57   // passed instead of of the raw pointer so that the InvalidationService can be | 
|   63   // Initializes the invalidator with the invalidation service. It must remain |   58   // lazily initialized. | 
|   64   // valid until Shutdown is called. An Initialize method must only be called |   59   typedef base::Callback<invalidation::InvalidationService*()> | 
|   65   // once. |   60       GetInvalidationService; | 
|   66   void InitializeWithService( |   61   void Initialize(const GetInvalidationService& get_invalidation_service); | 
|   67       invalidation::InvalidationService* invalidation_service); |  | 
|   68  |   62  | 
|   69   // Shuts down and disables invalidations. It must be called before the object |   63   // Shuts down and disables invalidations. It must be called before the object | 
|   70   // is destroyed. |   64   // is destroyed. | 
|   71   void Shutdown(); |   65   void Shutdown(); | 
|   72  |   66  | 
|   73   // Whether the invalidator currently has the ability to receive invalidations. |   67   // Whether the invalidator currently has the ability to receive invalidations. | 
|   74   bool invalidations_enabled() { |   68   bool invalidations_enabled() { | 
|   75     return invalidations_enabled_; |   69     return invalidations_enabled_; | 
|   76   } |   70   } | 
|   77  |   71  | 
|   78   // syncer::InvalidationHandler: |   72   // syncer::InvalidationHandler: | 
|   79   virtual void OnInvalidatorStateChange( |   73   virtual void OnInvalidatorStateChange( | 
|   80       syncer::InvalidatorState state) OVERRIDE; |   74       syncer::InvalidatorState state) OVERRIDE; | 
|   81   virtual void OnIncomingInvalidation( |   75   virtual void OnIncomingInvalidation( | 
|   82       const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE; |   76       const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE; | 
|   83  |   77  | 
 |   78   // CloudPolicyCore::Observer: | 
 |   79   virtual void OnCoreConnected(CloudPolicyCore* core) OVERRIDE; | 
 |   80   virtual void OnRefreshSchedulerStarted(CloudPolicyCore* core) OVERRIDE; | 
 |   81   virtual void OnCoreDisconnecting(CloudPolicyCore* core) OVERRIDE; | 
 |   82  | 
|   84   // CloudPolicyStore::Observer: |   83   // CloudPolicyStore::Observer: | 
|   85   virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE; |   84   virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE; | 
|   86   virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE; |   85   virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE; | 
|   87  |   86  | 
|   88  protected: |   87   // Expose the number of times the invalidator has refreshed the policy. For | 
|   89   // Allows subclasses to create a weak pointer to the object. The pointer |   88   // testing only. | 
|   90   // should only be used to call one of the Initialize methods, as after the |   89   int policy_refresh_count() { | 
|   91   // object is initialized weak pointers may be invalidated at any time. |   90     return policy_refresh_count_; | 
|   92   base::WeakPtr<CloudPolicyInvalidator> GetWeakPtr(); |   91   } | 
|   93  |   92  | 
|   94  private: |   93  private: | 
|   95   // Initialize the invalidator. |  | 
|   96   void Initialize(); |  | 
|   97  |  | 
|   98   // Returns whether an Initialize method has been called. |  | 
|   99   bool IsInitialized(); |  | 
|  100  |  | 
|  101   // Handle an invalidation to the policy. |   94   // Handle an invalidation to the policy. | 
|  102   void HandleInvalidation(const syncer::Invalidation& invalidation); |   95   void HandleInvalidation(const syncer::Invalidation& invalidation); | 
|  103  |   96  | 
|  104   // Update object registration with the invalidation service based on the |   97   // Update object registration with the invalidation service based on the | 
|  105   // given policy data. |   98   // given policy data. | 
|  106   void UpdateRegistration(const enterprise_management::PolicyData* policy); |   99   void UpdateRegistration(const enterprise_management::PolicyData* policy); | 
|  107  |  100  | 
|  108   // Registers the given object with the invalidation service. |  101   // Registers the given object with the invalidation service. | 
|  109   void Register(int64 timestamp, const invalidation::ObjectId& object_id); |  102   void Register(int64 timestamp, const invalidation::ObjectId& object_id); | 
|  110  |  103  | 
|  111   // Unregisters the current object with the invalidation service. |  104   // Unregisters the current object with the invalidation service. | 
|  112   void Unregister(); |  105   void Unregister(); | 
|  113  |  106  | 
|  114   // Update |max_fetch_delay_| based on the given policy map. |  107   // Update |max_fetch_delay_| based on the given policy map. | 
|  115   void UpdateMaxFetchDelay(const PolicyMap& policy_map); |  108   void UpdateMaxFetchDelay(const PolicyMap& policy_map); | 
|  116   void set_max_fetch_delay(int delay); |  109   void set_max_fetch_delay(int delay); | 
|  117  |  110  | 
|  118   // Updates invalidations_enabled_ and calls the invalidation handler if the |  111   // Updates invalidations_enabled_ and calls the invalidation handler if the | 
|  119   // value changed. |  112   // value changed. | 
|  120   void UpdateInvalidationsEnabled(); |  113   void UpdateInvalidationsEnabled(); | 
|  121  |  114  | 
|  122   // Run the invalidate callback on the invalidation handler. is_missing_payload |  115   // Refresh the policy. | 
|  123   // is set to true if the callback is being invoked in response to an |  116   // |is_missing_payload| is set to true if the callback is being invoked in | 
|  124   // invalidation with a missing payload. |  117   // response to an invalidation with a missing payload. | 
|  125   void RunInvalidateCallback(bool is_missing_payload); |  118   void RefreshPolicy(bool is_missing_payload); | 
|  126  |  119  | 
|  127   // Acknowledge the latest invalidation. |  120   // Acknowledge the latest invalidation. | 
|  128   void AcknowledgeInvalidation(); |  121   void AcknowledgeInvalidation(); | 
|  129  |  122  | 
|  130   // Get the kMetricPolicyRefresh histogram metric which should be incremented |  123   // Get the kMetricPolicyRefresh histogram metric which should be incremented | 
|  131   // when a policy is stored. |  124   // when a policy is stored. | 
|  132   int GetPolicyRefreshMetric(); |  125   int GetPolicyRefreshMetric(); | 
|  133  |  126  | 
|  134   // The handler for invalidations provded by this object. |  127   // The state of the object. | 
|  135   CloudPolicyInvalidationHandler* invalidation_handler_; |  128   enum State { | 
 |  129     UNINITIALIZED, | 
 |  130     STOPPED, | 
 |  131     STARTED, | 
 |  132     SHUT_DOWN | 
 |  133   }; | 
 |  134   State state_; | 
|  136  |  135  | 
|  137   // The cloud policy store. |  136   // The cloud policy core. | 
|  138   CloudPolicyStore* store_; |  137   CloudPolicyCore* core_; | 
|  139  |  138  | 
|  140   // Schedules delayed tasks. |  139   // Schedules delayed tasks. | 
|  141   const scoped_refptr<base::SequencedTaskRunner> task_runner_; |  140   const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 
|  142  |  141  | 
|  143   // The profile which will be used to get a reference to an invalidation |  142   // The callback invoked to lazily retrieve the invalidation service. | 
|  144   // service. |  143   GetInvalidationService get_invalidation_service_; | 
|  145   Profile* profile_; |  | 
|  146  |  144  | 
|  147   // The invalidation service. |  145   // The invalidation service. | 
|  148   invalidation::InvalidationService* invalidation_service_; |  146   invalidation::InvalidationService* invalidation_service_; | 
|  149  |  147  | 
|  150   // Whether the invalidator currently has the ability to receive invalidations. |  148   // Whether the invalidator currently has the ability to receive invalidations. | 
|  151   // This is true if the invalidation service is enabled and the invalidator |  149   // This is true if the invalidation service is enabled and the invalidator | 
|  152   // has registered for a policy object. |  150   // has registered for a policy object. | 
|  153   bool invalidations_enabled_; |  151   bool invalidations_enabled_; | 
|  154  |  152  | 
|  155   // Whether the invalidation service is currently enabled. |  153   // Whether the invalidation service is currently enabled. | 
| (...skipping 28 matching lines...) Expand all  Loading... | 
|  184   base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory_; |  182   base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory_; | 
|  185  |  183  | 
|  186   // The maximum random delay, in ms, between receiving an invalidation and |  184   // The maximum random delay, in ms, between receiving an invalidation and | 
|  187   // fetching the new policy. |  185   // fetching the new policy. | 
|  188   int max_fetch_delay_; |  186   int max_fetch_delay_; | 
|  189  |  187  | 
|  190   // A thread checker to make sure that callbacks are invoked on the correct |  188   // A thread checker to make sure that callbacks are invoked on the correct | 
|  191   // thread. |  189   // thread. | 
|  192   base::ThreadChecker thread_checker_; |  190   base::ThreadChecker thread_checker_; | 
|  193  |  191  | 
 |  192   // Policy refresh counter used for testing. | 
 |  193   int policy_refresh_count_; | 
 |  194  | 
|  194   DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator); |  195   DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator); | 
|  195 }; |  196 }; | 
|  196  |  197  | 
|  197 // Handles invalidations to cloud policy objects. |  | 
|  198 class CloudPolicyInvalidationHandler { |  | 
|  199  public: |  | 
|  200   virtual ~CloudPolicyInvalidationHandler() {} |  | 
|  201  |  | 
|  202   // This method is called when the current invalidation info should be set |  | 
|  203   // on the cloud policy client. |  | 
|  204   virtual void SetInvalidationInfo( |  | 
|  205       int64 version, |  | 
|  206       const std::string& payload) = 0; |  | 
|  207  |  | 
|  208   // This method is called when the policy should be refreshed due to an |  | 
|  209   // invalidation. A policy fetch should be scheduled in the near future. |  | 
|  210   virtual void InvalidatePolicy() = 0; |  | 
|  211  |  | 
|  212   // This method is called when the invalidator determines that the ability to |  | 
|  213   // receive policy invalidations becomes enabled or disabled. The invalidator |  | 
|  214   // starts in a disabled state, so the first call to this method is always when |  | 
|  215   // the invalidator becomes enabled. |  | 
|  216   virtual void OnInvalidatorStateChanged(bool invalidations_enabled) = 0; |  | 
|  217 }; |  | 
|  218  |  | 
|  219 }  // namespace policy |  198 }  // namespace policy | 
|  220  |  199  | 
|  221 #endif  // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ |  200 #endif  // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ | 
| OLD | NEW |