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/memory/ref_counted.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/threading/thread_checker.h" |
| 14 #include "chrome/browser/policy/cloud/cloud_policy_store.h" |
| 15 #include "sync/notifier/invalidation_handler.h" |
| 16 |
| 17 namespace base { |
| 18 class SequencedTaskRunner; |
| 19 } |
| 20 |
| 21 namespace invalidation { |
| 22 class InvalidationService; |
| 23 } |
| 24 |
| 25 namespace policy { |
| 26 |
| 27 class CloudPolicyInvalidationHandler; |
| 28 |
| 29 // Listens for and provides policy invalidations. |
| 30 class CloudPolicyInvalidator : public syncer::InvalidationHandler, |
| 31 public CloudPolicyStore::Observer { |
| 32 public: |
| 33 // The number of minutes to delay a policy refresh after receiving an |
| 34 // invalidation with no payload. |
| 35 static const int kMissingPayloadDelay; |
| 36 |
| 37 // The default, min and max values for max_fetch_delay_. |
| 38 static const int kMaxFetchDelayDefault; |
| 39 static const int kMaxFetchDelayMin; |
| 40 static const int kMaxFetchDelayMax; |
| 41 |
| 42 // |invalidation_service| is the invalidation service which generates the |
| 43 // invalidations and must remain valid until Shutdown is called. |
| 44 // |store| is cloud policy store. It must remain valid until Shutdown is |
| 45 // called. |
| 46 // |task_runner| is used for scheduling delayed tasks. It must post tasks to |
| 47 // the main policy thread. |
| 48 // |invalidation_handler| handles invalidations provided by this object and |
| 49 // must remain valid until Shutdown is called. |
| 50 CloudPolicyInvalidator( |
| 51 invalidation::InvalidationService* invalidation_service, |
| 52 CloudPolicyStore* store, |
| 53 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 54 CloudPolicyInvalidationHandler* invalidation_handler); |
| 55 |
| 56 // Shuts down and disables invalidations. It must be called before the object |
| 57 // is destroyed. |
| 58 void Shutdown(); |
| 59 |
| 60 // Whether the invalidator currently has the ability to receive invalidations. |
| 61 bool invalidations_enabled() { |
| 62 return invalidations_enabled_; |
| 63 } |
| 64 |
| 65 // syncer::InvalidationHandler: |
| 66 virtual void OnInvalidatorStateChange( |
| 67 syncer::InvalidatorState state) OVERRIDE; |
| 68 virtual void OnIncomingInvalidation( |
| 69 const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE; |
| 70 |
| 71 // CloudPolicyStore::Observer: |
| 72 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE; |
| 73 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE; |
| 74 |
| 75 private: |
| 76 // Handle an invalidation to the policy. |
| 77 void HandleInvalidation(const syncer::Invalidation& invalidation); |
| 78 |
| 79 // Update object registration with the invalidation service based on the |
| 80 // given policy data. |
| 81 void UpdateRegistration(const enterprise_management::PolicyData* policy); |
| 82 |
| 83 // Unregisters the current object with the invalidation service. |
| 84 void Unregister(); |
| 85 |
| 86 // Update |max_fetch_delay_| based on the given policy map. |
| 87 void UpdateMaxFetchDelay(const PolicyMap& policy_map); |
| 88 void set_max_fetch_delay(int delay); |
| 89 |
| 90 // Updates invalidations_enabled_ and calls the invalidation handler if the |
| 91 // value changed. |
| 92 void UpdateInvalidationsEnabled(); |
| 93 |
| 94 // Run the invalidate callback on the invalidation handler. is_missing_payload |
| 95 // is set to true if the callback is being invoked in response to an |
| 96 // invalidation with a missing payload. |
| 97 void RunInvalidateCallback(bool is_missing_payload); |
| 98 |
| 99 // Acknowledge the latest invalidation. |
| 100 void AcknowledgeInvalidation(); |
| 101 |
| 102 // Get the kMetricPolicyRefresh histogram metric which should be incremented |
| 103 // when a policy is stored. |
| 104 int GetPolicyRefreshMetric(); |
| 105 |
| 106 // The invalidation service. |
| 107 invalidation::InvalidationService* invalidation_service_; |
| 108 |
| 109 // The cloud policy store. |
| 110 CloudPolicyStore* store_; |
| 111 |
| 112 // Schedules delayed tasks. |
| 113 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 114 |
| 115 // The handler for invalidations provded by this object. |
| 116 CloudPolicyInvalidationHandler* invalidation_handler_; |
| 117 |
| 118 // Whether the invalidator currently has the ability to receive invalidations. |
| 119 // This is true if the invalidation service is enabled and the invalidator |
| 120 // has registered for a policy object. |
| 121 bool invalidations_enabled_; |
| 122 |
| 123 // Whether the invalidation service is currently enabled. |
| 124 bool invalidation_service_enabled_; |
| 125 |
| 126 // The timestamp of the PolicyData at which this object registered for policy |
| 127 // invalidations. Set to zero if the object has not registered yet. |
| 128 int64 registered_timestamp_; |
| 129 |
| 130 // The object id representing the policy in the invalidation service. |
| 131 invalidation::ObjectId object_id_; |
| 132 |
| 133 // Whether the policy is current invalid. This is set to true when an |
| 134 // invalidation is received and reset when the policy fetched due to the |
| 135 // invalidation is stored. |
| 136 bool invalid_; |
| 137 |
| 138 // The version of the latest invalidation received. This is compared to |
| 139 // the invalidation version of policy stored to determine when the |
| 140 // invalidated policy is up-to-date. |
| 141 int64 invalidation_version_; |
| 142 |
| 143 // The number of invalidations with unknown version received. Since such |
| 144 // invalidations do not provide a version number, this count is used to set |
| 145 // invalidation_version_ when such invalidations occur. |
| 146 int unknown_version_invalidation_count_; |
| 147 |
| 148 // The acknowledgment handle for the current invalidation. |
| 149 syncer::AckHandle ack_handle_; |
| 150 |
| 151 // WeakPtrFactory used to create callbacks to this object. |
| 152 base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory_; |
| 153 |
| 154 // The maximum random delay, in ms, between receiving an invalidation and |
| 155 // fetching the new policy. |
| 156 int max_fetch_delay_; |
| 157 |
| 158 // A thread checker to make sure that callbacks are invoked on the correct |
| 159 // thread. |
| 160 base::ThreadChecker thread_checker_; |
| 161 |
| 162 DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator); |
| 163 }; |
| 164 |
| 165 // Handles invalidations to cloud policy objects. |
| 166 class CloudPolicyInvalidationHandler { |
| 167 public: |
| 168 // This method is called when the current invalidation info should be set |
| 169 // on the cloud policy client. |
| 170 virtual void SetInvalidationInfo( |
| 171 int64 version, |
| 172 const std::string& payload) = 0; |
| 173 |
| 174 // This method is called when the policy should be refreshed due to an |
| 175 // invalidation. A policy fetch should be scheduled in the near future. |
| 176 virtual void InvalidatePolicy() = 0; |
| 177 |
| 178 // This method is called when the invalidator determines that the ability to |
| 179 // receive policy invalidations becomes enabled or disabled. The invalidator |
| 180 // starts in a disabled state, so the first call to this method is always when |
| 181 // the invalidator becomes enabled. |
| 182 virtual void OnInvalidatorStateChanged(bool invalidations_enabled) = 0; |
| 183 }; |
| 184 |
| 185 // Caches the latest invalidation received and replays it to a |
| 186 // CloudPolicyInvalidationHandler. The intent is for this class to be used by |
| 187 // CloudPolicyInvalidationHandler objects that may not be ready to respond |
| 188 // to invalidations when the policy invalidator starts. The object can pass |
| 189 // through the calls to an instance of this class. When the object becomes ready |
| 190 // to handle invalidations, it calls Replay to handle the latest invalidation. |
| 191 class CloudPolicyInvalidationReplayer { |
| 192 public: |
| 193 CloudPolicyInvalidationReplayer(); |
| 194 |
| 195 // Methods corresponding to CloudPolicyInvalidationHandler methods. |
| 196 void SetInvalidationInfo(int64 version, const std::string& payload); |
| 197 void InvalidatePolicy(); |
| 198 void OnInvalidatorStateChanged(bool invalidations_enabled); |
| 199 |
| 200 // Replays the latest invalidation (if any) on the given |
| 201 // CloudPolicyInvalidationHandler. This method must only be called once. |
| 202 void Replay(CloudPolicyInvalidationHandler* handler); |
| 203 |
| 204 private: |
| 205 int64 invalidation_version_; |
| 206 std::string invalidation_payload_; |
| 207 bool invalidate_policy_called_; |
| 208 bool invalidations_enabled_; |
| 209 }; |
| 210 |
| 211 } // namespace policy |
| 212 |
| 213 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ |
OLD | NEW |