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/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/threading/thread_checker.h"
16 #include "components/policy/core/common/cloud/cloud_policy_core.h"
17 #include "components/policy/core/common/cloud/cloud_policy_store.h"
18 #include "google/cacheinvalidation/include/types.h"
19 #include "sync/internal_api/public/base/invalidation.h"
20 #include "sync/notifier/invalidation_handler.h"
23 class SequencedTaskRunner
;
26 namespace invalidation
{
27 class InvalidationService
;
32 // Listens for and provides policy invalidations.
33 class CloudPolicyInvalidator
: public syncer::InvalidationHandler
,
34 public CloudPolicyCore::Observer
,
35 public CloudPolicyStore::Observer
{
37 // The number of minutes to delay a policy refresh after receiving an
38 // invalidation with no payload.
39 static const int kMissingPayloadDelay
;
41 // The default, min and max values for max_fetch_delay_.
42 static const int kMaxFetchDelayDefault
;
43 static const int kMaxFetchDelayMin
;
44 static const int kMaxFetchDelayMax
;
46 // |core| is the cloud policy core which connects the various policy objects.
47 // It must remain valid until Shutdown is called.
48 // |task_runner| is used for scheduling delayed tasks. It must post tasks to
49 // the main policy thread.
50 CloudPolicyInvalidator(
51 CloudPolicyCore
* core
,
52 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
);
53 virtual ~CloudPolicyInvalidator();
55 // Initializes the invalidator. No invalidations will be generated before this
56 // method is called. This method must only be called once.
57 // |invalidation_service| is the invalidation service to use and must remain
58 // valid until Shutdown is called.
59 void Initialize(invalidation::InvalidationService
* invalidation_service
);
61 // Shuts down and disables invalidations. It must be called before the object
65 // Whether the invalidator currently has the ability to receive invalidations.
66 bool invalidations_enabled() {
67 return invalidations_enabled_
;
70 // syncer::InvalidationHandler:
71 virtual void OnInvalidatorStateChange(
72 syncer::InvalidatorState state
) OVERRIDE
;
73 virtual void OnIncomingInvalidation(
74 const syncer::ObjectIdInvalidationMap
& invalidation_map
) OVERRIDE
;
76 // CloudPolicyCore::Observer:
77 virtual void OnCoreConnected(CloudPolicyCore
* core
) OVERRIDE
;
78 virtual void OnRefreshSchedulerStarted(CloudPolicyCore
* core
) OVERRIDE
;
79 virtual void OnCoreDisconnecting(CloudPolicyCore
* core
) OVERRIDE
;
81 // CloudPolicyStore::Observer:
82 virtual void OnStoreLoaded(CloudPolicyStore
* store
) OVERRIDE
;
83 virtual void OnStoreError(CloudPolicyStore
* store
) OVERRIDE
;
86 // Handle an invalidation to the policy.
87 void HandleInvalidation(const syncer::Invalidation
& invalidation
);
89 // Update object registration with the invalidation service based on the
91 void UpdateRegistration(const enterprise_management::PolicyData
* policy
);
93 // Registers the given object with the invalidation service.
94 void Register(int64 timestamp
, const invalidation::ObjectId
& object_id
);
96 // Unregisters the current object with the invalidation service.
99 // Update |max_fetch_delay_| based on the given policy map.
100 void UpdateMaxFetchDelay(const PolicyMap
& policy_map
);
101 void set_max_fetch_delay(int delay
);
103 // Updates invalidations_enabled_ and calls the invalidation handler if the
105 void UpdateInvalidationsEnabled();
107 // Refresh the policy.
108 // |is_missing_payload| is set to true if the callback is being invoked in
109 // response to an invalidation with a missing payload.
110 void RefreshPolicy(bool is_missing_payload
);
112 // Acknowledge the latest invalidation.
113 void AcknowledgeInvalidation();
115 // Determines if the given policy is different from the policy passed in the
117 bool IsPolicyChanged(const enterprise_management::PolicyData
* policy
);
119 // Get the kMetricPolicyRefresh histogram metric which should be incremented
120 // when a policy is stored.
121 int GetPolicyRefreshMetric(bool policy_changed
);
123 // The state of the object.
132 // The cloud policy core.
133 CloudPolicyCore
* core_
;
135 // Schedules delayed tasks.
136 const scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
138 // The invalidation service.
139 invalidation::InvalidationService
* invalidation_service_
;
141 // Whether the invalidator currently has the ability to receive invalidations.
142 // This is true if the invalidation service is enabled and the invalidator
143 // has registered for a policy object.
144 bool invalidations_enabled_
;
146 // Whether the invalidation service is currently enabled.
147 bool invalidation_service_enabled_
;
149 // The timestamp of the PolicyData at which this object registered for policy
150 // invalidations. Set to zero if the object has not registered yet.
151 int64 registered_timestamp_
;
153 // The object id representing the policy in the invalidation service.
154 invalidation::ObjectId object_id_
;
156 // Whether the policy is current invalid. This is set to true when an
157 // invalidation is received and reset when the policy fetched due to the
158 // invalidation is stored.
161 // The version of the latest invalidation received. This is compared to
162 // the invalidation version of policy stored to determine when the
163 // invalidated policy is up-to-date.
164 int64 invalidation_version_
;
166 // The number of invalidations with unknown version received. Since such
167 // invalidations do not provide a version number, this count is used to set
168 // invalidation_version_ when such invalidations occur.
169 int unknown_version_invalidation_count_
;
171 // The most up to date invalidation.
172 scoped_ptr
<syncer::Invalidation
> invalidation_
;
174 // WeakPtrFactory used to create callbacks to this object.
175 base::WeakPtrFactory
<CloudPolicyInvalidator
> weak_factory_
;
177 // The maximum random delay, in ms, between receiving an invalidation and
178 // fetching the new policy.
179 int max_fetch_delay_
;
181 // The hash value of the current policy. This is used to determine if a new
182 // policy is different from the current one.
183 uint32 policy_hash_value_
;
185 // A thread checker to make sure that callbacks are invoked on the correct
187 base::ThreadChecker thread_checker_
;
189 DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator
);
192 } // namespace policy
194 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_