OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ | 5 #ifndef CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ |
6 #define CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ | 6 #define CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
16 #include "components/policy/core/common/cloud/cloud_policy_core.h" | 16 #include "components/policy/core/common/cloud/cloud_policy_core.h" |
17 #include "components/policy/core/common/cloud/cloud_policy_store.h" | 17 #include "components/policy/core/common/cloud/cloud_policy_store.h" |
18 #include "google/cacheinvalidation/include/types.h" | 18 #include "google/cacheinvalidation/include/types.h" |
19 #include "sync/internal_api/public/base/invalidation.h" | 19 #include "sync/internal_api/public/base/invalidation.h" |
20 #include "sync/notifier/invalidation_handler.h" | 20 #include "sync/notifier/invalidation_handler.h" |
21 | 21 |
22 namespace base { | 22 namespace base { |
| 23 class Clock; |
23 class SequencedTaskRunner; | 24 class SequencedTaskRunner; |
24 } | 25 } |
25 | 26 |
26 namespace invalidation { | 27 namespace invalidation { |
27 class InvalidationService; | 28 class InvalidationService; |
28 } | 29 } |
29 | 30 |
30 namespace policy { | 31 namespace policy { |
31 | 32 |
32 // Listens for and provides policy invalidations. | 33 // Listens for and provides policy invalidations. |
33 class CloudPolicyInvalidator : public syncer::InvalidationHandler, | 34 class CloudPolicyInvalidator : public syncer::InvalidationHandler, |
34 public CloudPolicyCore::Observer, | 35 public CloudPolicyCore::Observer, |
35 public CloudPolicyStore::Observer { | 36 public CloudPolicyStore::Observer { |
36 public: | 37 public: |
37 // The number of minutes to delay a policy refresh after receiving an | 38 // The number of minutes to delay a policy refresh after receiving an |
38 // invalidation with no payload. | 39 // invalidation with no payload. |
39 static const int kMissingPayloadDelay; | 40 static const int kMissingPayloadDelay; |
40 | 41 |
41 // The default, min and max values for max_fetch_delay_. | 42 // The default, min and max values for max_fetch_delay_. |
42 static const int kMaxFetchDelayDefault; | 43 static const int kMaxFetchDelayDefault; |
43 static const int kMaxFetchDelayMin; | 44 static const int kMaxFetchDelayMin; |
44 static const int kMaxFetchDelayMax; | 45 static const int kMaxFetchDelayMax; |
45 | 46 |
| 47 // The grace period, in seconds, to allow for invalidations to be received |
| 48 // once the invalidation service starts up. |
| 49 static const int kInvalidationGracePeriod; |
| 50 |
46 // |core| is the cloud policy core which connects the various policy objects. | 51 // |core| is the cloud policy core which connects the various policy objects. |
47 // It must remain valid until Shutdown is called. | 52 // It must remain valid until Shutdown is called. |
48 // |task_runner| is used for scheduling delayed tasks. It must post tasks to | 53 // |task_runner| is used for scheduling delayed tasks. It must post tasks to |
49 // the main policy thread. | 54 // the main policy thread. |
| 55 // |clock| is used to get the current time. |
50 CloudPolicyInvalidator( | 56 CloudPolicyInvalidator( |
51 CloudPolicyCore* core, | 57 CloudPolicyCore* core, |
52 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | 58 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 59 scoped_ptr<base::Clock> clock); |
53 virtual ~CloudPolicyInvalidator(); | 60 virtual ~CloudPolicyInvalidator(); |
54 | 61 |
55 // Initializes the invalidator. No invalidations will be generated before this | 62 // Initializes the invalidator. No invalidations will be generated before this |
56 // method is called. This method must only be called once. | 63 // method is called. This method must only be called once. |
57 // |invalidation_service| is the invalidation service to use and must remain | 64 // |invalidation_service| is the invalidation service to use and must remain |
58 // valid until Shutdown is called. | 65 // valid until Shutdown is called. |
59 void Initialize(invalidation::InvalidationService* invalidation_service); | 66 void Initialize(invalidation::InvalidationService* invalidation_service); |
60 | 67 |
61 // Shuts down and disables invalidations. It must be called before the object | 68 // Shuts down and disables invalidations. It must be called before the object |
62 // is destroyed. | 69 // is destroyed. |
(...skipping 22 matching lines...) Expand all Loading... |
85 | 92 |
86 private: | 93 private: |
87 // Handle an invalidation to the policy. | 94 // Handle an invalidation to the policy. |
88 void HandleInvalidation(const syncer::Invalidation& invalidation); | 95 void HandleInvalidation(const syncer::Invalidation& invalidation); |
89 | 96 |
90 // Update object registration with the invalidation service based on the | 97 // Update object registration with the invalidation service based on the |
91 // given policy data. | 98 // given policy data. |
92 void UpdateRegistration(const enterprise_management::PolicyData* policy); | 99 void UpdateRegistration(const enterprise_management::PolicyData* policy); |
93 | 100 |
94 // Registers the given object with the invalidation service. | 101 // Registers the given object with the invalidation service. |
95 void Register(int64 timestamp, const invalidation::ObjectId& object_id); | 102 void Register(const invalidation::ObjectId& object_id); |
96 | 103 |
97 // Unregisters the current object with the invalidation service. | 104 // Unregisters the current object with the invalidation service. |
98 void Unregister(); | 105 void Unregister(); |
99 | 106 |
100 // Update |max_fetch_delay_| based on the given policy map. | 107 // Update |max_fetch_delay_| based on the given policy map. |
101 void UpdateMaxFetchDelay(const PolicyMap& policy_map); | 108 void UpdateMaxFetchDelay(const PolicyMap& policy_map); |
102 void set_max_fetch_delay(int delay); | 109 void set_max_fetch_delay(int delay); |
103 | 110 |
104 // Updates invalidations_enabled_ and calls the invalidation handler if the | 111 // Updates invalidations_enabled_ and calls the invalidation handler if the |
105 // value changed. | 112 // value changed. |
106 void UpdateInvalidationsEnabled(); | 113 void UpdateInvalidationsEnabled(); |
107 | 114 |
108 // Refresh the policy. | 115 // Refresh the policy. |
109 // |is_missing_payload| is set to true if the callback is being invoked in | 116 // |is_missing_payload| is set to true if the callback is being invoked in |
110 // response to an invalidation with a missing payload. | 117 // response to an invalidation with a missing payload. |
111 void RefreshPolicy(bool is_missing_payload); | 118 void RefreshPolicy(bool is_missing_payload); |
112 | 119 |
113 // Acknowledge the latest invalidation. | 120 // Acknowledge the latest invalidation. |
114 void AcknowledgeInvalidation(); | 121 void AcknowledgeInvalidation(); |
115 | 122 |
116 // Determines if the given policy is different from the policy passed in the | 123 // Determines if the given policy is different from the policy passed in the |
117 // previous call. | 124 // previous call. |
118 bool IsPolicyChanged(const enterprise_management::PolicyData* policy); | 125 bool IsPolicyChanged(const enterprise_management::PolicyData* policy); |
119 | 126 |
120 // Get the kMetricPolicyRefresh histogram metric which should be incremented | 127 // Get the kMetricPolicyRefresh histogram metric which should be incremented |
121 // when a policy is stored. | 128 // when a policy is stored. |
122 int GetPolicyRefreshMetric(bool policy_changed); | 129 int GetPolicyRefreshMetric(bool policy_changed); |
123 | 130 |
| 131 // Determine if invalidations have been enabled longer than the grace period. |
| 132 bool GetInvalidationsEnabled(); |
| 133 |
124 // The state of the object. | 134 // The state of the object. |
125 enum State { | 135 enum State { |
126 UNINITIALIZED, | 136 UNINITIALIZED, |
127 STOPPED, | 137 STOPPED, |
128 STARTED, | 138 STARTED, |
129 SHUT_DOWN | 139 SHUT_DOWN |
130 }; | 140 }; |
131 State state_; | 141 State state_; |
132 | 142 |
133 // The cloud policy core. | 143 // The cloud policy core. |
134 CloudPolicyCore* core_; | 144 CloudPolicyCore* core_; |
135 | 145 |
136 // Schedules delayed tasks. | 146 // Schedules delayed tasks. |
137 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 147 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
138 | 148 |
| 149 // The clock. |
| 150 scoped_ptr<base::Clock> clock_; |
| 151 |
139 // The invalidation service. | 152 // The invalidation service. |
140 invalidation::InvalidationService* invalidation_service_; | 153 invalidation::InvalidationService* invalidation_service_; |
141 | 154 |
142 // Whether the invalidator currently has the ability to receive invalidations. | 155 // Whether the invalidator currently has the ability to receive invalidations. |
143 // This is true if the invalidation service is enabled and the invalidator | 156 // This is true if the invalidation service is enabled and the invalidator |
144 // has registered for a policy object. | 157 // has registered for a policy object. |
145 bool invalidations_enabled_; | 158 bool invalidations_enabled_; |
146 | 159 |
| 160 // The time that invalidations became enabled. |
| 161 base::Time invalidations_enabled_time_; |
| 162 |
147 // Whether the invalidation service is currently enabled. | 163 // Whether the invalidation service is currently enabled. |
148 bool invalidation_service_enabled_; | 164 bool invalidation_service_enabled_; |
149 | 165 |
150 // The timestamp of the PolicyData at which this object registered for policy | 166 // Whether this object has registered for policy invalidations. |
151 // invalidations. Set to zero if the object has not registered yet. | 167 bool is_registered_; |
152 int64 registered_timestamp_; | |
153 | 168 |
154 // The object id representing the policy in the invalidation service. | 169 // The object id representing the policy in the invalidation service. |
155 invalidation::ObjectId object_id_; | 170 invalidation::ObjectId object_id_; |
156 | 171 |
157 // Whether the policy is current invalid. This is set to true when an | 172 // Whether the policy is current invalid. This is set to true when an |
158 // invalidation is received and reset when the policy fetched due to the | 173 // invalidation is received and reset when the policy fetched due to the |
159 // invalidation is stored. | 174 // invalidation is stored. |
160 bool invalid_; | 175 bool invalid_; |
161 | 176 |
162 // The version of the latest invalidation received. This is compared to | 177 // The version of the latest invalidation received. This is compared to |
(...skipping 23 matching lines...) Expand all Loading... |
186 // A thread checker to make sure that callbacks are invoked on the correct | 201 // A thread checker to make sure that callbacks are invoked on the correct |
187 // thread. | 202 // thread. |
188 base::ThreadChecker thread_checker_; | 203 base::ThreadChecker thread_checker_; |
189 | 204 |
190 DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator); | 205 DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator); |
191 }; | 206 }; |
192 | 207 |
193 } // namespace policy | 208 } // namespace policy |
194 | 209 |
195 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ | 210 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ |
OLD | NEW |