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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_
6 #define CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/thread_checker.h"
17 #include "components/invalidation/invalidation.h"
18 #include "components/invalidation/invalidation_handler.h"
19 #include "components/policy/core/common/cloud/cloud_policy_core.h"
20 #include "components/policy/core/common/cloud/cloud_policy_store.h"
21 #include "google/cacheinvalidation/include/types.h"
22 #include "policy/proto/device_management_backend.pb.h"
26 class SequencedTaskRunner;
29 namespace invalidation {
30 class InvalidationService;
35 // Listens for and provides policy invalidations.
36 class CloudPolicyInvalidator : public syncer::InvalidationHandler,
37 public CloudPolicyCore::Observer,
38 public CloudPolicyStore::Observer {
40 // The number of minutes to delay a policy refresh after receiving an
41 // invalidation with no payload.
42 static const int kMissingPayloadDelay;
44 // The default, min and max values for max_fetch_delay_.
45 static const int kMaxFetchDelayDefault;
46 static const int kMaxFetchDelayMin;
47 static const int kMaxFetchDelayMax;
49 // The grace period, in seconds, to allow for invalidations to be received
50 // once the invalidation service starts up.
51 static const int kInvalidationGracePeriod;
53 // Time, in seconds, for which unknown version invalidations are ignored after
55 static const int kUnknownVersionIgnorePeriod;
57 // The max tolerated discrepancy, in seconds, between policy timestamps and
58 // invalidation timestamps when determining if an invalidation is expired.
59 static const int kMaxInvalidationTimeDelta;
61 // |type| indicates the policy type that this invalidator is responsible for.
62 // |core| is the cloud policy core which connects the various policy objects.
63 // It must remain valid until Shutdown is called.
64 // |task_runner| is used for scheduling delayed tasks. It must post tasks to
65 // the main policy thread.
66 // |clock| is used to get the current time.
67 // |highest_handled_invalidation_version| is the highest invalidation version
68 // that was handled already before this invalidator was created.
69 CloudPolicyInvalidator(
70 enterprise_management::DeviceRegisterRequest::Type type,
71 CloudPolicyCore* core,
72 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
73 scoped_ptr<base::Clock> clock,
74 int64 highest_handled_invalidation_version);
75 ~CloudPolicyInvalidator() override;
77 // Initializes the invalidator. No invalidations will be generated before this
78 // method is called. This method must only be called once.
79 // |invalidation_service| is the invalidation service to use and must remain
80 // valid until Shutdown is called.
81 void Initialize(invalidation::InvalidationService* invalidation_service);
83 // Shuts down and disables invalidations. It must be called before the object
87 // Whether the invalidator currently has the ability to receive invalidations.
88 bool invalidations_enabled() {
89 return invalidations_enabled_;
92 // The highest invalidation version that was handled already.
93 int64 highest_handled_invalidation_version() const {
94 return highest_handled_invalidation_version_;
97 // syncer::InvalidationHandler:
98 void OnInvalidatorStateChange(syncer::InvalidatorState state) override;
99 void OnIncomingInvalidation(
100 const syncer::ObjectIdInvalidationMap& invalidation_map) override;
101 std::string GetOwnerName() const override;
103 // CloudPolicyCore::Observer:
104 void OnCoreConnected(CloudPolicyCore* core) override;
105 void OnRefreshSchedulerStarted(CloudPolicyCore* core) override;
106 void OnCoreDisconnecting(CloudPolicyCore* core) override;
108 // CloudPolicyStore::Observer:
109 void OnStoreLoaded(CloudPolicyStore* store) override;
110 void OnStoreError(CloudPolicyStore* store) override;
113 // Handle an invalidation to the policy.
114 void HandleInvalidation(const syncer::Invalidation& invalidation);
116 // Update object registration with the invalidation service based on the
117 // given policy data.
118 void UpdateRegistration(const enterprise_management::PolicyData* policy);
120 // Registers the given object with the invalidation service.
121 void Register(const invalidation::ObjectId& object_id);
123 // Unregisters the current object with the invalidation service.
126 // Update |max_fetch_delay_| based on the given policy map.
127 void UpdateMaxFetchDelay(const PolicyMap& policy_map);
128 void set_max_fetch_delay(int delay);
130 // Updates invalidations_enabled_ and calls the invalidation handler if the
132 void UpdateInvalidationsEnabled();
134 // Refresh the policy.
135 // |is_missing_payload| is set to true if the callback is being invoked in
136 // response to an invalidation with a missing payload.
137 void RefreshPolicy(bool is_missing_payload);
139 // Acknowledge the latest invalidation.
140 void AcknowledgeInvalidation();
142 // Determines if the given policy is different from the policy passed in the
144 bool IsPolicyChanged(const enterprise_management::PolicyData* policy);
146 // Determine if an invalidation has expired.
147 // |version| is the version of the invalidation, or zero for unknown.
148 bool IsInvalidationExpired(int64 version);
150 // Get the kMetricPolicyRefresh histogram metric which should be incremented
151 // when a policy is stored.
152 int GetPolicyRefreshMetric(bool policy_changed);
154 // Get the kMetricPolicyInvalidations histogram metric which should be
155 // incremented when an invalidation is received.
156 int GetInvalidationMetric(bool is_missing_payload, bool is_expired);
158 // Determine if invalidations have been enabled longer than the grace period.
159 bool GetInvalidationsEnabled();
161 // The state of the object.
170 // The policy type this invalidator is responsible for.
171 const enterprise_management::DeviceRegisterRequest::Type type_;
173 // The cloud policy core.
174 CloudPolicyCore* core_;
176 // Schedules delayed tasks.
177 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
180 scoped_ptr<base::Clock> clock_;
182 // The invalidation service.
183 invalidation::InvalidationService* invalidation_service_;
185 // Whether the invalidator currently has the ability to receive invalidations.
186 // This is true if the invalidation service is enabled and the invalidator
187 // has registered for a policy object.
188 bool invalidations_enabled_;
190 // The time that invalidations became enabled.
191 base::Time invalidations_enabled_time_;
193 // Whether the invalidation service is currently enabled.
194 bool invalidation_service_enabled_;
196 // Whether this object has registered for policy invalidations.
199 // The object id representing the policy in the invalidation service.
200 invalidation::ObjectId object_id_;
202 // Whether the policy is current invalid. This is set to true when an
203 // invalidation is received and reset when the policy fetched due to the
204 // invalidation is stored.
207 // The version of the latest invalidation received. This is compared to
208 // the invalidation version of policy stored to determine when the
209 // invalidated policy is up-to-date.
210 int64 invalidation_version_;
212 // The number of invalidations with unknown version received. Since such
213 // invalidations do not provide a version number, this count is used to set
214 // invalidation_version_ when such invalidations occur.
215 int unknown_version_invalidation_count_;
217 // The highest invalidation version that was handled already.
218 int64 highest_handled_invalidation_version_;
220 // The most up to date invalidation.
221 scoped_ptr<syncer::Invalidation> invalidation_;
223 // The maximum random delay, in ms, between receiving an invalidation and
224 // fetching the new policy.
225 int max_fetch_delay_;
227 // The hash value of the current policy. This is used to determine if a new
228 // policy is different from the current one.
229 uint32 policy_hash_value_;
231 // A thread checker to make sure that callbacks are invoked on the correct
233 base::ThreadChecker thread_checker_;
235 // WeakPtrFactory used to create callbacks to this object.
236 base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory_;
238 DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator);
241 } // namespace policy
243 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_