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 invalidation::InvalidationService
* invalidation_service_for_test() const {
98 return invalidation_service_
;
101 // syncer::InvalidationHandler:
102 void OnInvalidatorStateChange(syncer::InvalidatorState state
) override
;
103 void OnIncomingInvalidation(
104 const syncer::ObjectIdInvalidationMap
& invalidation_map
) override
;
105 std::string
GetOwnerName() const override
;
107 // CloudPolicyCore::Observer:
108 void OnCoreConnected(CloudPolicyCore
* core
) override
;
109 void OnRefreshSchedulerStarted(CloudPolicyCore
* core
) override
;
110 void OnCoreDisconnecting(CloudPolicyCore
* core
) override
;
112 // CloudPolicyStore::Observer:
113 void OnStoreLoaded(CloudPolicyStore
* store
) override
;
114 void OnStoreError(CloudPolicyStore
* store
) override
;
117 // Handle an invalidation to the policy.
118 void HandleInvalidation(const syncer::Invalidation
& invalidation
);
120 // Update object registration with the invalidation service based on the
121 // given policy data.
122 void UpdateRegistration(const enterprise_management::PolicyData
* policy
);
124 // Registers the given object with the invalidation service.
125 void Register(const invalidation::ObjectId
& object_id
);
127 // Unregisters the current object with the invalidation service.
130 // Update |max_fetch_delay_| based on the given policy map.
131 void UpdateMaxFetchDelay(const PolicyMap
& policy_map
);
132 void set_max_fetch_delay(int delay
);
134 // Updates invalidations_enabled_ and calls the invalidation handler if the
136 void UpdateInvalidationsEnabled();
138 // Refresh the policy.
139 // |is_missing_payload| is set to true if the callback is being invoked in
140 // response to an invalidation with a missing payload.
141 void RefreshPolicy(bool is_missing_payload
);
143 // Acknowledge the latest invalidation.
144 void AcknowledgeInvalidation();
146 // Determines if the given policy is different from the policy passed in the
148 bool IsPolicyChanged(const enterprise_management::PolicyData
* policy
);
150 // Determine if an invalidation has expired.
151 // |version| is the version of the invalidation, or zero for unknown.
152 bool IsInvalidationExpired(int64 version
);
154 // Get the kMetricPolicyRefresh histogram metric which should be incremented
155 // when a policy is stored.
156 int GetPolicyRefreshMetric(bool policy_changed
);
158 // Get the kMetricPolicyInvalidations histogram metric which should be
159 // incremented when an invalidation is received.
160 int GetInvalidationMetric(bool is_missing_payload
, bool is_expired
);
162 // Determine if invalidations have been enabled longer than the grace period.
163 bool GetInvalidationsEnabled();
165 // The state of the object.
174 // The policy type this invalidator is responsible for.
175 const enterprise_management::DeviceRegisterRequest::Type type_
;
177 // The cloud policy core.
178 CloudPolicyCore
* core_
;
180 // Schedules delayed tasks.
181 const scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
184 scoped_ptr
<base::Clock
> clock_
;
186 // The invalidation service.
187 invalidation::InvalidationService
* invalidation_service_
;
189 // Whether the invalidator currently has the ability to receive invalidations.
190 // This is true if the invalidation service is enabled and the invalidator
191 // has registered for a policy object.
192 bool invalidations_enabled_
;
194 // The time that invalidations became enabled.
195 base::Time invalidations_enabled_time_
;
197 // Whether the invalidation service is currently enabled.
198 bool invalidation_service_enabled_
;
200 // Whether this object has registered for policy invalidations.
203 // The object id representing the policy in the invalidation service.
204 invalidation::ObjectId object_id_
;
206 // Whether the policy is current invalid. This is set to true when an
207 // invalidation is received and reset when the policy fetched due to the
208 // invalidation is stored.
211 // The version of the latest invalidation received. This is compared to
212 // the invalidation version of policy stored to determine when the
213 // invalidated policy is up-to-date.
214 int64 invalidation_version_
;
216 // The number of invalidations with unknown version received. Since such
217 // invalidations do not provide a version number, this count is used to set
218 // invalidation_version_ when such invalidations occur.
219 int unknown_version_invalidation_count_
;
221 // The highest invalidation version that was handled already.
222 int64 highest_handled_invalidation_version_
;
224 // The most up to date invalidation.
225 scoped_ptr
<syncer::Invalidation
> invalidation_
;
227 // The maximum random delay, in ms, between receiving an invalidation and
228 // fetching the new policy.
229 int max_fetch_delay_
;
231 // The hash value of the current policy. This is used to determine if a new
232 // policy is different from the current one.
233 uint32 policy_hash_value_
;
235 // A thread checker to make sure that callbacks are invoked on the correct
237 base::ThreadChecker thread_checker_
;
239 // WeakPtrFactory used to create callbacks to this object.
240 base::WeakPtrFactory
<CloudPolicyInvalidator
> weak_factory_
;
242 DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator
);
245 } // namespace policy
247 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_