OLD | NEW |
---|---|
(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 "base/basictypes.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/memory/weak_ptr.h" | |
11 #include "base/threading/thread_checker.h" | |
12 #include "chrome/browser/policy/cloud/cloud_policy_store.h" | |
13 #include "sync/notifier/invalidation_handler.h" | |
14 | |
15 namespace base { | |
16 class SequencedTaskRunner; | |
17 } | |
18 | |
19 namespace invalidation { | |
20 class InvalidationService; | |
21 } | |
22 | |
23 namespace policy { | |
24 | |
25 class CloudPolicyClient; | |
26 class CloudPolicyInvalidationHandler; | |
27 | |
28 // Listens for and provides policy invalidations. | |
29 class CloudPolicyInvalidator : public syncer::InvalidationHandler, | |
30 public CloudPolicyStore::Observer { | |
31 public: | |
32 // The number of minutes to delay a policy refresh after receiving an | |
33 // invalidation with no payload. | |
34 static const int kMissingPayloadDelay; | |
35 | |
36 // The default, min and max values for max_fetch_delay_. | |
37 static const int kMaxFetchDelayDefault; | |
38 static const int kMaxFetchDelayMin; | |
39 static const int kMaxFetchDelayMax; | |
40 | |
41 // |invalidation_service| is the invalidation service which generates the | |
42 // invalidations and must remain valid during the lifetime of this object. | |
43 // |client| is cloud policy client. It must remain valid during the lifetime | |
44 // of this object. | |
45 // |store| is cloud policy store. It must remain valid during the lifetime | |
46 // of this object. | |
47 // |task_runner| is used for scheduling delayed tasks. It must post tasks to | |
48 // the main policy thread. | |
49 // |invalidation_handler| handles invalidations provided by this object and | |
50 // must remain valid during the lifetime of this object. | |
51 CloudPolicyInvalidator( | |
52 invalidation::InvalidationService* invalidation_service, | |
53 CloudPolicyClient* client, | |
54 CloudPolicyStore* store, | |
55 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | |
56 CloudPolicyInvalidationHandler* invalidation_handler); | |
57 virtual ~CloudPolicyInvalidator(); | |
58 | |
59 // Unregisters the policy object from the invalidation service. This method | |
60 // should only be called when policy is no longer applied; it should not be | |
61 // called for normal browser shutdown. This object should be destroyed | |
62 // immediately after calling this method. | |
63 void Unregister(); | |
64 | |
65 // Whether the invalidator currently has the ability to receive invalidations. | |
66 bool invalidations_enabled() { | |
67 return invalidations_enabled_; | |
68 } | |
69 | |
70 // syncer::InvalidationHandler: | |
71 virtual void OnInvalidatorStateChange( | |
72 syncer::InvalidatorState state) OVERRIDE; | |
73 virtual void OnIncomingInvalidation( | |
74 const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE; | |
75 | |
76 // CloudPolicyStore::Observer: | |
77 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE; | |
78 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE; | |
79 | |
80 private: | |
81 // Handle an invalidation to the policy. | |
82 void HandleInvalidation(const syncer::Invalidation& invalidation); | |
83 | |
84 // Update object registration with the invalidation service based on the | |
85 // given policy data. | |
86 void UpdateRegistration(const enterprise_management::PolicyData* policy); | |
87 | |
88 // Update |max_fetch_delay_| based on the given policy map. | |
89 void UpdateMaxFetchDelay(const PolicyMap& policy_map); | |
90 void set_max_fetch_delay(int delay); | |
91 | |
92 // Updates invalidations_enabled_ and calls the invalidation handler if the | |
93 // value changed. | |
94 void UpdateInvalidationsEnabled(); | |
95 | |
96 // Run the invalidate callback on the invalidation handler. is_missing_payload | |
97 // is set to true if the callback is being invoked in response to an | |
98 // invalidation with a missing payload. | |
99 void RunInvalidateCallback(bool is_missing_payload); | |
100 | |
101 // Acknowledge the latest invalidation. | |
102 void AcknowledgeInvalidation(); | |
103 | |
104 // Get the kMetricPolicyRefresh histogram metric which should be incremented | |
105 // when a policy is stored. | |
106 int GetPolicyRefreshMetric(); | |
107 | |
108 // The invalidation service. | |
109 invalidation::InvalidationService* invalidation_service_; | |
110 | |
111 // The cloud policy client. | |
112 CloudPolicyClient* client_; | |
113 | |
114 // The cloud policy store. | |
115 CloudPolicyStore* store_; | |
116 | |
117 // Schedules delayed tasks. | |
118 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
119 | |
120 // The handler for invalidations provded by this object. | |
121 CloudPolicyInvalidationHandler* invalidation_handler_; | |
122 | |
123 // Whether the invalidator currently has the ability to receive invalidations. | |
124 // This is true if the invalidation service is enabled and the invalidator | |
125 // has registered for a policy object. | |
126 bool invalidations_enabled_; | |
127 | |
128 // Whether the invalidation service is currently enabled. | |
129 bool invalidation_service_enabled_; | |
130 | |
131 // The timestamp of the PolicyData at which this object registered for policy | |
132 // invalidations. Set to zero if the object has not registered yet. | |
133 int64 registered_timestamp_; | |
134 | |
135 // The object id representing the policy in the invalidation service. | |
136 invalidation::ObjectId object_id_; | |
137 | |
138 // Whether the policy is current invalid. This is set to true when an | |
139 // invalidation is received and reset when the policy fetched due to the | |
140 // invalidation is stored. | |
141 bool invalid_; | |
142 | |
143 // The version of the latest invalidation received. This is compared to | |
144 // the invalidation version of policy stored to determine when the | |
145 // invalidated policy is up-to-date. | |
146 int64 invalidation_version_; | |
147 | |
148 // The number of invalidations with unknown version received. Since such | |
149 // invalidations do not provide a version number, this count is used to set | |
150 // invalidation_version_ when such invalidations occur. | |
151 int unknown_version_invalidation_count_; | |
152 | |
153 // The acknowledgment handle for the current invalidation. | |
154 syncer::AckHandle ack_handle_; | |
155 | |
156 // WeakPtrFactory used to create callbacks to this object. | |
157 base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory_; | |
158 | |
159 // The maximum random delay, in ms, between receiving an invalidation and | |
160 // fetching the new policy. | |
161 int max_fetch_delay_; | |
162 | |
163 // A thread checker to make sure that callbacks are invoked on the correct | |
164 // thread. | |
165 base::ThreadChecker thread_checker_; | |
166 | |
167 DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator); | |
168 }; | |
169 | |
170 // Handles invalidations to cloud policy objects. | |
171 class CloudPolicyInvalidationHandler { | |
172 public: | |
Joao da Silva
2013/07/25 18:03:53
virtual ~CloudPolicyInvalidatorHandler() {}
Steve Condie
2013/07/26 01:39:25
Done.
| |
173 // This method is called when the policy should be refreshed due to an | |
174 // invalidation. A policy fetch should be scheduled in the near future. | |
175 virtual void InvalidatePolicy() = 0; | |
176 | |
177 // This method is called when the invalidator determines that the ability to | |
178 // receive policy invalidations becomes enabled or disabled. The invalidator | |
179 // starts in a disabled state, so the first call to this method is always when | |
180 // the invalidator becomes enabled. | |
181 virtual void OnInvalidatorStateChanged(bool invalidations_enabled) = 0; | |
182 }; | |
183 | |
184 } // namespace policy | |
185 | |
186 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ | |
OLD | NEW |