Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: chrome/browser/policy/cloud/cloud_policy_invalidator.h

Issue 19733003: Implement cloud policy invalidations using the invalidation service framework. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 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.
4
5 #ifndef CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_
6 #define CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_
7
8 #include <string>
9
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_store.h"
16 #include "sync/notifier/invalidation_handler.h"
17
18 class Profile;
19
20 namespace base {
21 class SequencedTaskRunner;
22 }
23
24 namespace invalidation {
25 class InvalidationService;
26 }
27
28 namespace policy {
29
30 class CloudPolicyInvalidationHandler;
31
32 // Listens for and provides policy invalidations.
33 class CloudPolicyInvalidator : public syncer::InvalidationHandler,
34 public CloudPolicyStore::Observer {
35 public:
36 // The number of minutes to delay a policy refresh after receiving an
37 // invalidation with no payload.
38 static const int kMissingPayloadDelay;
39
40 // The default, min and max values for max_fetch_delay_.
41 static const int kMaxFetchDelayDefault;
42 static const int kMaxFetchDelayMin;
43 static const int kMaxFetchDelayMax;
44
45 // |invalidation_handler| handles invalidations provided by this object and
46 // must remain valid until Shutdown is called.
47 // |store| is cloud policy store. It must remain valid until Shutdown is
48 // called.
49 // |task_runner| is used for scheduling delayed tasks. It must post tasks to
50 // the main policy thread.
51 CloudPolicyInvalidator(
52 CloudPolicyInvalidationHandler* invalidation_handler,
53 CloudPolicyStore* store,
54 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
55 virtual ~CloudPolicyInvalidator();
56
57 // Initializes the invalidator with the given profile. The invalidator uses
58 // the profile to get a reference to the profile's invalidation service if
59 // needed. Both the profile and the invalidation service must remain valid
60 // until Shutdown is called. An Initialize method must only be called once.
61 void Initialize(Profile* profile);
62
63 // Initializes the invalidator with the invalidation service. It must remain
64 // valid until Shutdown is called. An Initialize method must only be called
65 // once.
66 void Initialize(invalidation::InvalidationService* invalidation_service);
67
68 // Shuts down and disables invalidations. It must be called before the object
69 // is destroyed.
70 void Shutdown();
71
72 // Whether the invalidator currently has the ability to receive invalidations.
73 bool invalidations_enabled() {
74 return invalidations_enabled_;
75 }
76
77 // syncer::InvalidationHandler:
78 virtual void OnInvalidatorStateChange(
79 syncer::InvalidatorState state) OVERRIDE;
80 virtual void OnIncomingInvalidation(
81 const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE;
82
83 // CloudPolicyStore::Observer:
84 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE;
85 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE;
86
87 private:
88 // Initialize the invalidator.
89 void Initialize();
90
91 // Returns whether an Initialize method has been called.
92 bool IsInitialized();
93
94 // Handle an invalidation to the policy.
95 void HandleInvalidation(const syncer::Invalidation& invalidation);
96
97 // Update object registration with the invalidation service based on the
98 // given policy data.
99 void UpdateRegistration(const enterprise_management::PolicyData* policy);
100
101 // Registers the given object with the invalidation service.
102 void Register(int64 timestamp, const invalidation::ObjectId& object_id);
103
104 // Unregisters the current object with the invalidation service.
105 void Unregister();
106
107 // Update |max_fetch_delay_| based on the given policy map.
108 void UpdateMaxFetchDelay(const PolicyMap& policy_map);
109 void set_max_fetch_delay(int delay);
110
111 // Updates invalidations_enabled_ and calls the invalidation handler if the
112 // value changed.
113 void UpdateInvalidationsEnabled();
114
115 // Run the invalidate callback on the invalidation handler. is_missing_payload
116 // is set to true if the callback is being invoked in response to an
117 // invalidation with a missing payload.
118 void RunInvalidateCallback(bool is_missing_payload);
119
120 // Acknowledge the latest invalidation.
121 void AcknowledgeInvalidation();
122
123 // Get the kMetricPolicyRefresh histogram metric which should be incremented
124 // when a policy is stored.
125 int GetPolicyRefreshMetric();
126
127 // The handler for invalidations provded by this object.
128 CloudPolicyInvalidationHandler* invalidation_handler_;
129
130 // The cloud policy store.
131 CloudPolicyStore* store_;
132
133 // Schedules delayed tasks.
134 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
135
136 // The profile which will be used to get a reference to an invalidation
137 // service.
138 Profile* profile_;
139
140 // The invalidation service.
141 invalidation::InvalidationService* invalidation_service_;
142
143 // Whether the invalidator currently has the ability to receive invalidations.
144 // This is true if the invalidation service is enabled and the invalidator
145 // has registered for a policy object.
146 bool invalidations_enabled_;
147
148 // Whether the invalidation service is currently enabled.
149 bool invalidation_service_enabled_;
150
151 // The timestamp of the PolicyData at which this object registered for policy
152 // invalidations. Set to zero if the object has not registered yet.
153 int64 registered_timestamp_;
154
155 // The object id representing the policy in the invalidation service.
156 invalidation::ObjectId object_id_;
157
158 // Whether the policy is current invalid. This is set to true when an
159 // invalidation is received and reset when the policy fetched due to the
160 // invalidation is stored.
161 bool invalid_;
162
163 // The version of the latest invalidation received. This is compared to
164 // the invalidation version of policy stored to determine when the
165 // invalidated policy is up-to-date.
166 int64 invalidation_version_;
167
168 // The number of invalidations with unknown version received. Since such
169 // invalidations do not provide a version number, this count is used to set
170 // invalidation_version_ when such invalidations occur.
171 int unknown_version_invalidation_count_;
172
173 // The acknowledgment handle for the current invalidation.
174 syncer::AckHandle ack_handle_;
175
176 // WeakPtrFactory used to create callbacks to this object.
177 base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory_;
178
179 // The maximum random delay, in ms, between receiving an invalidation and
180 // fetching the new policy.
181 int max_fetch_delay_;
182
183 // A thread checker to make sure that callbacks are invoked on the correct
184 // thread.
185 base::ThreadChecker thread_checker_;
186
187 DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator);
188 };
189
190 // Handles invalidations to cloud policy objects.
191 class CloudPolicyInvalidationHandler {
192 public:
193 virtual ~CloudPolicyInvalidationHandler() {}
194
195 // This method is called when the current invalidation info should be set
196 // on the cloud policy client.
197 virtual void SetInvalidationInfo(
198 int64 version,
199 const std::string& payload) = 0;
200
201 // This method is called when the policy should be refreshed due to an
202 // invalidation. A policy fetch should be scheduled in the near future.
203 virtual void InvalidatePolicy() = 0;
204
205 // This method is called when the invalidator determines that the ability to
206 // receive policy invalidations becomes enabled or disabled. The invalidator
207 // starts in a disabled state, so the first call to this method is always when
208 // the invalidator becomes enabled.
209 virtual void OnInvalidatorStateChanged(bool invalidations_enabled) = 0;
210 };
211
212 } // namespace policy
213
214 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698