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/logging.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/threading/thread_checker.h"
18 #include "components/invalidation/public/invalidation.h"
19 #include "components/invalidation/public/invalidation_handler.h"
20 #include "components/policy/core/common/cloud/cloud_policy_core.h"
21 #include "components/policy/core/common/cloud/cloud_policy_store.h"
22 #include "google/cacheinvalidation/include/types.h"
23 #include "policy/proto/device_management_backend.pb.h"
27 class SequencedTaskRunner
;
30 namespace invalidation
{
31 class InvalidationService
;
36 // Listens for and provides policy invalidations.
37 class CloudPolicyInvalidator
: public syncer::InvalidationHandler
,
38 public CloudPolicyCore::Observer
,
39 public CloudPolicyStore::Observer
{
41 // The number of minutes to delay a policy refresh after receiving an
42 // invalidation with no payload.
43 static const int kMissingPayloadDelay
;
45 // The default, min and max values for max_fetch_delay_.
46 static const int kMaxFetchDelayDefault
;
47 static const int kMaxFetchDelayMin
;
48 static const int kMaxFetchDelayMax
;
50 // The grace period, in seconds, to allow for invalidations to be received
51 // once the invalidation service starts up.
52 static const int kInvalidationGracePeriod
;
54 // Time, in seconds, for which unknown version invalidations are ignored after
56 static const int kUnknownVersionIgnorePeriod
;
58 // The max tolerated discrepancy, in seconds, between policy timestamps and
59 // invalidation timestamps when determining if an invalidation is expired.
60 static const int kMaxInvalidationTimeDelta
;
62 // |type| indicates the policy type that this invalidator is responsible for.
63 // |core| is the cloud policy core which connects the various policy objects.
64 // It must remain valid until Shutdown is called.
65 // |task_runner| is used for scheduling delayed tasks. It must post tasks to
66 // the main policy thread.
67 // |clock| is used to get the current time.
68 // |highest_handled_invalidation_version| is the highest invalidation version
69 // that was handled already before this invalidator was created.
70 CloudPolicyInvalidator(
71 enterprise_management::DeviceRegisterRequest::Type type
,
72 CloudPolicyCore
* core
,
73 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
,
74 scoped_ptr
<base::Clock
> clock
,
75 int64 highest_handled_invalidation_version
);
76 ~CloudPolicyInvalidator() override
;
78 // Initializes the invalidator. No invalidations will be generated before this
79 // method is called. This method must only be called once.
80 // |invalidation_service| is the invalidation service to use and must remain
81 // valid until Shutdown is called.
82 void Initialize(invalidation::InvalidationService
* invalidation_service
);
84 // Shuts down and disables invalidations. It must be called before the object
88 // Whether the invalidator currently has the ability to receive invalidations.
89 bool invalidations_enabled() {
90 return invalidations_enabled_
;
93 // The highest invalidation version that was handled already.
94 int64
highest_handled_invalidation_version() const {
95 return highest_handled_invalidation_version_
;
98 invalidation::InvalidationService
* invalidation_service_for_test() const {
99 return invalidation_service_
;
102 // syncer::InvalidationHandler:
103 void OnInvalidatorStateChange(syncer::InvalidatorState state
) override
;
104 void OnIncomingInvalidation(
105 const syncer::ObjectIdInvalidationMap
& invalidation_map
) override
;
106 std::string
GetOwnerName() const override
;
108 // CloudPolicyCore::Observer:
109 void OnCoreConnected(CloudPolicyCore
* core
) override
;
110 void OnRefreshSchedulerStarted(CloudPolicyCore
* core
) override
;
111 void OnCoreDisconnecting(CloudPolicyCore
* core
) override
;
113 // CloudPolicyStore::Observer:
114 void OnStoreLoaded(CloudPolicyStore
* store
) override
;
115 void OnStoreError(CloudPolicyStore
* store
) override
;
118 // Handle an invalidation to the policy.
119 void HandleInvalidation(const syncer::Invalidation
& invalidation
);
121 // Update object registration with the invalidation service based on the
122 // given policy data.
123 void UpdateRegistration(const enterprise_management::PolicyData
* policy
);
125 // Registers the given object with the invalidation service.
126 void Register(const invalidation::ObjectId
& object_id
);
128 // Unregisters the current object with the invalidation service.
131 // Update |max_fetch_delay_| based on the given policy map.
132 void UpdateMaxFetchDelay(const PolicyMap
& policy_map
);
133 void set_max_fetch_delay(int delay
);
135 // Updates invalidations_enabled_ and calls the invalidation handler if the
137 void UpdateInvalidationsEnabled();
139 // Refresh the policy.
140 // |is_missing_payload| is set to true if the callback is being invoked in
141 // response to an invalidation with a missing payload.
142 void RefreshPolicy(bool is_missing_payload
);
144 // Acknowledge the latest invalidation.
145 void AcknowledgeInvalidation();
147 // Determines if the given policy is different from the policy passed in the
149 bool IsPolicyChanged(const enterprise_management::PolicyData
* policy
);
151 // Determine if an invalidation has expired.
152 // |version| is the version of the invalidation, or zero for unknown.
153 bool IsInvalidationExpired(int64 version
);
155 // Get the kMetricPolicyRefresh histogram metric which should be incremented
156 // when a policy is stored.
157 int GetPolicyRefreshMetric(bool policy_changed
);
159 // Get the kMetricPolicyInvalidations histogram metric which should be
160 // incremented when an invalidation is received.
161 int GetInvalidationMetric(bool is_missing_payload
, bool is_expired
);
163 // Determine if invalidations have been enabled longer than the grace period.
164 bool GetInvalidationsEnabled();
166 // The state of the object.
175 // The policy type this invalidator is responsible for.
176 const enterprise_management::DeviceRegisterRequest::Type type_
;
178 // The cloud policy core.
179 CloudPolicyCore
* core_
;
181 // Schedules delayed tasks.
182 const scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
185 scoped_ptr
<base::Clock
> clock_
;
187 // The invalidation service.
188 invalidation::InvalidationService
* invalidation_service_
;
190 // Whether the invalidator currently has the ability to receive invalidations.
191 // This is true if the invalidation service is enabled and the invalidator
192 // has registered for a policy object.
193 bool invalidations_enabled_
;
195 // The time that invalidations became enabled.
196 base::Time invalidations_enabled_time_
;
198 // Whether the invalidation service is currently enabled.
199 bool invalidation_service_enabled_
;
201 // Whether this object has registered for policy invalidations.
204 // The object id representing the policy in the invalidation service.
205 invalidation::ObjectId object_id_
;
207 // Whether the policy is current invalid. This is set to true when an
208 // invalidation is received and reset when the policy fetched due to the
209 // invalidation is stored.
212 // The version of the latest invalidation received. This is compared to
213 // the invalidation version of policy stored to determine when the
214 // invalidated policy is up-to-date.
215 int64 invalidation_version_
;
217 // The number of invalidations with unknown version received. Since such
218 // invalidations do not provide a version number, this count is used to set
219 // invalidation_version_ when such invalidations occur.
220 int unknown_version_invalidation_count_
;
222 // The highest invalidation version that was handled already.
223 int64 highest_handled_invalidation_version_
;
225 // The most up to date invalidation.
226 scoped_ptr
<syncer::Invalidation
> invalidation_
;
228 // The maximum random delay, in ms, between receiving an invalidation and
229 // fetching the new policy.
230 int max_fetch_delay_
;
232 // The hash value of the current policy. This is used to determine if a new
233 // policy is different from the current one.
234 uint32 policy_hash_value_
;
236 // A thread checker to make sure that callbacks are invoked on the correct
238 base::ThreadChecker thread_checker_
;
240 // WeakPtrFactory used to create callbacks to this object.
241 base::WeakPtrFactory
<CloudPolicyInvalidator
> weak_factory_
;
243 DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator
);
246 } // namespace policy
248 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_