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/weak_ptr.h"
14 #include "base/threading/thread_checker.h"
15 #include "chrome/browser/policy/cloud/cloud_policy_core.h"
16 #include "chrome/browser/policy/cloud/cloud_policy_store.h"
17 #include "sync/notifier/invalidation_handler.h"
20 class SequencedTaskRunner;
23 namespace invalidation {
24 class InvalidationService;
29 // Listens for and provides policy invalidations.
30 class CloudPolicyInvalidator : public syncer::InvalidationHandler,
31 public CloudPolicyCore::Observer,
32 public CloudPolicyStore::Observer {
34 // The number of minutes to delay a policy refresh after receiving an
35 // invalidation with no payload.
36 static const int kMissingPayloadDelay;
38 // The default, min and max values for max_fetch_delay_.
39 static const int kMaxFetchDelayDefault;
40 static const int kMaxFetchDelayMin;
41 static const int kMaxFetchDelayMax;
43 // |core| is the cloud policy core which connects the various policy objects.
44 // It must remain valid until Shutdown is called.
45 // |task_runner| is used for scheduling delayed tasks. It must post tasks to
46 // the main policy thread.
47 CloudPolicyInvalidator(
48 CloudPolicyCore* core,
49 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
50 virtual ~CloudPolicyInvalidator();
52 // Initializes the invalidator. No invalidations will be generated before this
53 // method is called. This method must only be called once.
54 // |invalidation_service| is the invalidation service to use and must remain
55 // valid until Shutdown is called.
56 void Initialize(invalidation::InvalidationService* invalidation_service);
58 // Shuts down and disables invalidations. It must be called before the object
62 // Whether the invalidator currently has the ability to receive invalidations.
63 bool invalidations_enabled() {
64 return invalidations_enabled_;
67 // syncer::InvalidationHandler:
68 virtual void OnInvalidatorStateChange(
69 syncer::InvalidatorState state) OVERRIDE;
70 virtual void OnIncomingInvalidation(
71 const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE;
73 // CloudPolicyCore::Observer:
74 virtual void OnCoreConnected(CloudPolicyCore* core) OVERRIDE;
75 virtual void OnRefreshSchedulerStarted(CloudPolicyCore* core) OVERRIDE;
76 virtual void OnCoreDisconnecting(CloudPolicyCore* core) OVERRIDE;
78 // CloudPolicyStore::Observer:
79 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE;
80 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE;
83 // Handle an invalidation to the policy.
84 void HandleInvalidation(const syncer::Invalidation& invalidation);
86 // Update object registration with the invalidation service based on the
88 void UpdateRegistration(const enterprise_management::PolicyData* policy);
90 // Registers the given object with the invalidation service.
91 void Register(int64 timestamp, const invalidation::ObjectId& object_id);
93 // Unregisters the current object with the invalidation service.
96 // Update |max_fetch_delay_| based on the given policy map.
97 void UpdateMaxFetchDelay(const PolicyMap& policy_map);
98 void set_max_fetch_delay(int delay);
100 // Updates invalidations_enabled_ and calls the invalidation handler if the
102 void UpdateInvalidationsEnabled();
104 // Refresh the policy.
105 // |is_missing_payload| is set to true if the callback is being invoked in
106 // response to an invalidation with a missing payload.
107 void RefreshPolicy(bool is_missing_payload);
109 // Acknowledge the latest invalidation.
110 void AcknowledgeInvalidation();
112 // Determines if the given policy is different from the policy passed in the
114 bool IsPolicyChanged(const enterprise_management::PolicyData* policy);
116 // Get the kMetricPolicyRefresh histogram metric which should be incremented
117 // when a policy is stored.
118 int GetPolicyRefreshMetric(bool policy_changed);
120 // The state of the object.
129 // The cloud policy core.
130 CloudPolicyCore* core_;
132 // Schedules delayed tasks.
133 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
135 // The invalidation service.
136 invalidation::InvalidationService* invalidation_service_;
138 // Whether the invalidator currently has the ability to receive invalidations.
139 // This is true if the invalidation service is enabled and the invalidator
140 // has registered for a policy object.
141 bool invalidations_enabled_;
143 // Whether the invalidation service is currently enabled.
144 bool invalidation_service_enabled_;
146 // The timestamp of the PolicyData at which this object registered for policy
147 // invalidations. Set to zero if the object has not registered yet.
148 int64 registered_timestamp_;
150 // The object id representing the policy in the invalidation service.
151 invalidation::ObjectId object_id_;
153 // Whether the policy is current invalid. This is set to true when an
154 // invalidation is received and reset when the policy fetched due to the
155 // invalidation is stored.
158 // The version of the latest invalidation received. This is compared to
159 // the invalidation version of policy stored to determine when the
160 // invalidated policy is up-to-date.
161 int64 invalidation_version_;
163 // The number of invalidations with unknown version received. Since such
164 // invalidations do not provide a version number, this count is used to set
165 // invalidation_version_ when such invalidations occur.
166 int unknown_version_invalidation_count_;
168 // The acknowledgment handle for the current invalidation.
169 syncer::AckHandle ack_handle_;
171 // WeakPtrFactory used to create callbacks to this object.
172 base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory_;
174 // The maximum random delay, in ms, between receiving an invalidation and
175 // fetching the new policy.
176 int max_fetch_delay_;
178 // The hash value of the current policy. This is used to determine if a new
179 // policy is different from the current one.
180 uint32 policy_hash_value_;
182 // A thread checker to make sure that callbacks are invoked on the correct
184 base::ThreadChecker thread_checker_;
186 DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator);
189 } // namespace policy
191 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_