OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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_INVALIDATOR_H_ |
| 6 #define CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/threading/thread_checker.h" |
| 15 #include "chrome/browser/policy/cloud/cloud_policy_store.h" |
| 16 #include "sync/notifier/invalidation_handler.h" |
| 17 |
| 18 class Profile; |
| 19 |
| 20 namespace base { |
| 21 class SequencedTaskRunner; |
| 22 } |
| 23 |
| 24 namespace invalidation { |
| 25 class InvalidationService; |
| 26 } |
| 27 |
| 28 namespace policy { |
| 29 |
| 30 class CloudPolicyInvalidationHandler; |
| 31 |
| 32 // Listens for and provides policy invalidations. |
| 33 class CloudPolicyInvalidator : public syncer::InvalidationHandler, |
| 34 public CloudPolicyStore::Observer { |
| 35 public: |
| 36 // The number of minutes to delay a policy refresh after receiving an |
| 37 // invalidation with no payload. |
| 38 static const int kMissingPayloadDelay; |
| 39 |
| 40 // The default, min and max values for max_fetch_delay_. |
| 41 static const int kMaxFetchDelayDefault; |
| 42 static const int kMaxFetchDelayMin; |
| 43 static const int kMaxFetchDelayMax; |
| 44 |
| 45 // |invalidation_handler| handles invalidations provided by this object and |
| 46 // 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 |
| 50 // the main policy thread. |
| 51 CloudPolicyInvalidator( |
| 52 CloudPolicyInvalidationHandler* invalidation_handler, |
| 53 CloudPolicyStore* store, |
| 54 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
| 55 virtual ~CloudPolicyInvalidator(); |
| 56 |
| 57 // Initializes the invalidator with the given profile. The invalidator uses |
| 58 // the profile to get a reference to the profile's invalidation service if |
| 59 // needed. Both the profile and the invalidation service must remain valid |
| 60 // until Shutdown is called. An Initialize method must only be called once. |
| 61 void InitializeWithProfile(Profile* profile); |
| 62 |
| 63 // Initializes the invalidator with the invalidation service. It must remain |
| 64 // valid until Shutdown is called. An Initialize method must only be called |
| 65 // once. |
| 66 void InitializeWithService( |
| 67 invalidation::InvalidationService* invalidation_service); |
| 68 |
| 69 // Shuts down and disables invalidations. It must be called before the object |
| 70 // is destroyed. |
| 71 void Shutdown(); |
| 72 |
| 73 // Whether the invalidator currently has the ability to receive invalidations. |
| 74 bool invalidations_enabled() { |
| 75 return invalidations_enabled_; |
| 76 } |
| 77 |
| 78 // syncer::InvalidationHandler: |
| 79 virtual void OnInvalidatorStateChange( |
| 80 syncer::InvalidatorState state) OVERRIDE; |
| 81 virtual void OnIncomingInvalidation( |
| 82 const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE; |
| 83 |
| 84 // CloudPolicyStore::Observer: |
| 85 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE; |
| 86 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE; |
| 87 |
| 88 protected: |
| 89 // Allows subclasses to create a weak pointer to the object. The pointer |
| 90 // should only be used to call one of the Initialize methods, as after the |
| 91 // object is initialized weak pointers may be invalidated at any time. |
| 92 base::WeakPtr<CloudPolicyInvalidator> GetWeakPtr(); |
| 93 |
| 94 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. |
| 102 void HandleInvalidation(const syncer::Invalidation& invalidation); |
| 103 |
| 104 // Update object registration with the invalidation service based on the |
| 105 // given policy data. |
| 106 void UpdateRegistration(const enterprise_management::PolicyData* policy); |
| 107 |
| 108 // Registers the given object with the invalidation service. |
| 109 void Register(int64 timestamp, const invalidation::ObjectId& object_id); |
| 110 |
| 111 // Unregisters the current object with the invalidation service. |
| 112 void Unregister(); |
| 113 |
| 114 // Update |max_fetch_delay_| based on the given policy map. |
| 115 void UpdateMaxFetchDelay(const PolicyMap& policy_map); |
| 116 void set_max_fetch_delay(int delay); |
| 117 |
| 118 // Updates invalidations_enabled_ and calls the invalidation handler if the |
| 119 // value changed. |
| 120 void UpdateInvalidationsEnabled(); |
| 121 |
| 122 // Run the invalidate callback on the invalidation handler. is_missing_payload |
| 123 // is set to true if the callback is being invoked in response to an |
| 124 // invalidation with a missing payload. |
| 125 void RunInvalidateCallback(bool is_missing_payload); |
| 126 |
| 127 // Acknowledge the latest invalidation. |
| 128 void AcknowledgeInvalidation(); |
| 129 |
| 130 // Get the kMetricPolicyRefresh histogram metric which should be incremented |
| 131 // when a policy is stored. |
| 132 int GetPolicyRefreshMetric(); |
| 133 |
| 134 // The handler for invalidations provded by this object. |
| 135 CloudPolicyInvalidationHandler* invalidation_handler_; |
| 136 |
| 137 // The cloud policy store. |
| 138 CloudPolicyStore* store_; |
| 139 |
| 140 // Schedules delayed tasks. |
| 141 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 142 |
| 143 // The profile which will be used to get a reference to an invalidation |
| 144 // service. |
| 145 Profile* profile_; |
| 146 |
| 147 // The invalidation service. |
| 148 invalidation::InvalidationService* invalidation_service_; |
| 149 |
| 150 // Whether the invalidator currently has the ability to receive invalidations. |
| 151 // This is true if the invalidation service is enabled and the invalidator |
| 152 // has registered for a policy object. |
| 153 bool invalidations_enabled_; |
| 154 |
| 155 // Whether the invalidation service is currently enabled. |
| 156 bool invalidation_service_enabled_; |
| 157 |
| 158 // The timestamp of the PolicyData at which this object registered for policy |
| 159 // invalidations. Set to zero if the object has not registered yet. |
| 160 int64 registered_timestamp_; |
| 161 |
| 162 // The object id representing the policy in the invalidation service. |
| 163 invalidation::ObjectId object_id_; |
| 164 |
| 165 // Whether the policy is current invalid. This is set to true when an |
| 166 // invalidation is received and reset when the policy fetched due to the |
| 167 // invalidation is stored. |
| 168 bool invalid_; |
| 169 |
| 170 // The version of the latest invalidation received. This is compared to |
| 171 // the invalidation version of policy stored to determine when the |
| 172 // invalidated policy is up-to-date. |
| 173 int64 invalidation_version_; |
| 174 |
| 175 // The number of invalidations with unknown version received. Since such |
| 176 // invalidations do not provide a version number, this count is used to set |
| 177 // invalidation_version_ when such invalidations occur. |
| 178 int unknown_version_invalidation_count_; |
| 179 |
| 180 // The acknowledgment handle for the current invalidation. |
| 181 syncer::AckHandle ack_handle_; |
| 182 |
| 183 // WeakPtrFactory used to create callbacks to this object. |
| 184 base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory_; |
| 185 |
| 186 // The maximum random delay, in ms, between receiving an invalidation and |
| 187 // fetching the new policy. |
| 188 int max_fetch_delay_; |
| 189 |
| 190 // A thread checker to make sure that callbacks are invoked on the correct |
| 191 // thread. |
| 192 base::ThreadChecker thread_checker_; |
| 193 |
| 194 DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator); |
| 195 }; |
| 196 |
| 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 |
| 220 |
| 221 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ |
OLD | NEW |