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/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
| 15 #include "chrome/browser/policy/cloud/cloud_policy_core.h" |
15 #include "chrome/browser/policy/cloud/cloud_policy_store.h" | 16 #include "chrome/browser/policy/cloud/cloud_policy_store.h" |
16 #include "sync/notifier/invalidation_handler.h" | 17 #include "sync/notifier/invalidation_handler.h" |
17 | 18 |
18 class Profile; | |
19 | |
20 namespace base { | 19 namespace base { |
21 class SequencedTaskRunner; | 20 class SequencedTaskRunner; |
22 } | 21 } |
23 | 22 |
24 namespace invalidation { | 23 namespace invalidation { |
25 class InvalidationService; | 24 class InvalidationService; |
26 } | 25 } |
27 | 26 |
28 namespace policy { | 27 namespace policy { |
29 | 28 |
30 class CloudPolicyInvalidationHandler; | |
31 | |
32 // Listens for and provides policy invalidations. | 29 // Listens for and provides policy invalidations. |
33 class CloudPolicyInvalidator : public syncer::InvalidationHandler, | 30 class CloudPolicyInvalidator : public syncer::InvalidationHandler, |
| 31 public CloudPolicyCore::Observer, |
34 public CloudPolicyStore::Observer { | 32 public CloudPolicyStore::Observer { |
35 public: | 33 public: |
36 // The number of minutes to delay a policy refresh after receiving an | 34 // The number of minutes to delay a policy refresh after receiving an |
37 // invalidation with no payload. | 35 // invalidation with no payload. |
38 static const int kMissingPayloadDelay; | 36 static const int kMissingPayloadDelay; |
39 | 37 |
40 // The default, min and max values for max_fetch_delay_. | 38 // The default, min and max values for max_fetch_delay_. |
41 static const int kMaxFetchDelayDefault; | 39 static const int kMaxFetchDelayDefault; |
42 static const int kMaxFetchDelayMin; | 40 static const int kMaxFetchDelayMin; |
43 static const int kMaxFetchDelayMax; | 41 static const int kMaxFetchDelayMax; |
44 | 42 |
45 // |invalidation_handler| handles invalidations provided by this object and | 43 // |core| is the cloud policy core which connects the various policy objects. |
46 // must remain valid until Shutdown is called. | 44 // It 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 | 45 // |task_runner| is used for scheduling delayed tasks. It must post tasks to |
50 // the main policy thread. | 46 // the main policy thread. |
51 CloudPolicyInvalidator( | 47 CloudPolicyInvalidator( |
52 CloudPolicyInvalidationHandler* invalidation_handler, | 48 CloudPolicyCore* core, |
53 CloudPolicyStore* store, | |
54 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | 49 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
55 virtual ~CloudPolicyInvalidator(); | 50 virtual ~CloudPolicyInvalidator(); |
56 | 51 |
57 // Initializes the invalidator with the given profile. The invalidator uses | 52 // Initializes the invalidator. No invalidations will be generated before this |
58 // the profile to get a reference to the profile's invalidation service if | 53 // method is called. This method must only be called once. |
59 // needed. Both the profile and the invalidation service must remain valid | 54 // |invalidation_service| is the invalidation service to use and must remain |
60 // until Shutdown is called. An Initialize method must only be called once. | 55 // valid until Shutdown is called. |
61 void InitializeWithProfile(Profile* profile); | 56 void Initialize(invalidation::InvalidationService* invalidation_service); |
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 InitializeWithService( | |
67 invalidation::InvalidationService* invalidation_service); | |
68 | 57 |
69 // Shuts down and disables invalidations. It must be called before the object | 58 // Shuts down and disables invalidations. It must be called before the object |
70 // is destroyed. | 59 // is destroyed. |
71 void Shutdown(); | 60 void Shutdown(); |
72 | 61 |
73 // Whether the invalidator currently has the ability to receive invalidations. | 62 // Whether the invalidator currently has the ability to receive invalidations. |
74 bool invalidations_enabled() { | 63 bool invalidations_enabled() { |
75 return invalidations_enabled_; | 64 return invalidations_enabled_; |
76 } | 65 } |
77 | 66 |
78 // syncer::InvalidationHandler: | 67 // syncer::InvalidationHandler: |
79 virtual void OnInvalidatorStateChange( | 68 virtual void OnInvalidatorStateChange( |
80 syncer::InvalidatorState state) OVERRIDE; | 69 syncer::InvalidatorState state) OVERRIDE; |
81 virtual void OnIncomingInvalidation( | 70 virtual void OnIncomingInvalidation( |
82 const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE; | 71 const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE; |
83 | 72 |
| 73 // CloudPolicyCore::Observer: |
| 74 virtual void OnCoreConnected(CloudPolicyCore* core) OVERRIDE; |
| 75 virtual void OnRefreshSchedulerStarted(CloudPolicyCore* core) OVERRIDE; |
| 76 virtual void OnCoreDisconnecting(CloudPolicyCore* core) OVERRIDE; |
| 77 |
84 // CloudPolicyStore::Observer: | 78 // CloudPolicyStore::Observer: |
85 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE; | 79 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE; |
86 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE; | 80 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE; |
87 | 81 |
88 protected: | 82 // Expose the number of times the invalidator has refreshed the policy. For |
89 // Allows subclasses to create a weak pointer to the object. The pointer | 83 // testing only. |
90 // should only be used to call one of the Initialize methods, as after the | 84 int policy_refresh_count() { |
91 // object is initialized weak pointers may be invalidated at any time. | 85 return policy_refresh_count_; |
92 base::WeakPtr<CloudPolicyInvalidator> GetWeakPtr(); | 86 } |
93 | 87 |
94 private: | 88 private: |
95 // Initialize the invalidator. | |
96 void Initialize(); | |
97 | |
98 // Returns whether an Initialize method has been called. | |
99 bool IsInitialized(); | |
100 | |
101 // Handle an invalidation to the policy. | 89 // Handle an invalidation to the policy. |
102 void HandleInvalidation(const syncer::Invalidation& invalidation); | 90 void HandleInvalidation(const syncer::Invalidation& invalidation); |
103 | 91 |
104 // Update object registration with the invalidation service based on the | 92 // Update object registration with the invalidation service based on the |
105 // given policy data. | 93 // given policy data. |
106 void UpdateRegistration(const enterprise_management::PolicyData* policy); | 94 void UpdateRegistration(const enterprise_management::PolicyData* policy); |
107 | 95 |
108 // Registers the given object with the invalidation service. | 96 // Registers the given object with the invalidation service. |
109 void Register(int64 timestamp, const invalidation::ObjectId& object_id); | 97 void Register(int64 timestamp, const invalidation::ObjectId& object_id); |
110 | 98 |
111 // Unregisters the current object with the invalidation service. | 99 // Unregisters the current object with the invalidation service. |
112 void Unregister(); | 100 void Unregister(); |
113 | 101 |
114 // Update |max_fetch_delay_| based on the given policy map. | 102 // Update |max_fetch_delay_| based on the given policy map. |
115 void UpdateMaxFetchDelay(const PolicyMap& policy_map); | 103 void UpdateMaxFetchDelay(const PolicyMap& policy_map); |
116 void set_max_fetch_delay(int delay); | 104 void set_max_fetch_delay(int delay); |
117 | 105 |
118 // Updates invalidations_enabled_ and calls the invalidation handler if the | 106 // Updates invalidations_enabled_ and calls the invalidation handler if the |
119 // value changed. | 107 // value changed. |
120 void UpdateInvalidationsEnabled(); | 108 void UpdateInvalidationsEnabled(); |
121 | 109 |
122 // Run the invalidate callback on the invalidation handler. is_missing_payload | 110 // Refresh the policy. |
123 // is set to true if the callback is being invoked in response to an | 111 // |is_missing_payload| is set to true if the callback is being invoked in |
124 // invalidation with a missing payload. | 112 // response to an invalidation with a missing payload. |
125 void RunInvalidateCallback(bool is_missing_payload); | 113 void RefreshPolicy(bool is_missing_payload); |
126 | 114 |
127 // Acknowledge the latest invalidation. | 115 // Acknowledge the latest invalidation. |
128 void AcknowledgeInvalidation(); | 116 void AcknowledgeInvalidation(); |
129 | 117 |
130 // Get the kMetricPolicyRefresh histogram metric which should be incremented | 118 // Get the kMetricPolicyRefresh histogram metric which should be incremented |
131 // when a policy is stored. | 119 // when a policy is stored. |
132 int GetPolicyRefreshMetric(); | 120 int GetPolicyRefreshMetric(); |
133 | 121 |
134 // The handler for invalidations provded by this object. | 122 // The state of the object. |
135 CloudPolicyInvalidationHandler* invalidation_handler_; | 123 enum State { |
| 124 UNINITIALIZED, |
| 125 STOPPED, |
| 126 STARTED, |
| 127 SHUT_DOWN |
| 128 }; |
| 129 State state_; |
136 | 130 |
137 // The cloud policy store. | 131 // The cloud policy core. |
138 CloudPolicyStore* store_; | 132 CloudPolicyCore* core_; |
139 | 133 |
140 // Schedules delayed tasks. | 134 // Schedules delayed tasks. |
141 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 135 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
142 | 136 |
143 // The profile which will be used to get a reference to an invalidation | |
144 // service. | |
145 Profile* profile_; | |
146 | |
147 // The invalidation service. | 137 // The invalidation service. |
148 invalidation::InvalidationService* invalidation_service_; | 138 invalidation::InvalidationService* invalidation_service_; |
149 | 139 |
150 // Whether the invalidator currently has the ability to receive invalidations. | 140 // Whether the invalidator currently has the ability to receive invalidations. |
151 // This is true if the invalidation service is enabled and the invalidator | 141 // This is true if the invalidation service is enabled and the invalidator |
152 // has registered for a policy object. | 142 // has registered for a policy object. |
153 bool invalidations_enabled_; | 143 bool invalidations_enabled_; |
154 | 144 |
155 // Whether the invalidation service is currently enabled. | 145 // Whether the invalidation service is currently enabled. |
156 bool invalidation_service_enabled_; | 146 bool invalidation_service_enabled_; |
(...skipping 27 matching lines...) Expand all Loading... |
184 base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory_; | 174 base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory_; |
185 | 175 |
186 // The maximum random delay, in ms, between receiving an invalidation and | 176 // The maximum random delay, in ms, between receiving an invalidation and |
187 // fetching the new policy. | 177 // fetching the new policy. |
188 int max_fetch_delay_; | 178 int max_fetch_delay_; |
189 | 179 |
190 // A thread checker to make sure that callbacks are invoked on the correct | 180 // A thread checker to make sure that callbacks are invoked on the correct |
191 // thread. | 181 // thread. |
192 base::ThreadChecker thread_checker_; | 182 base::ThreadChecker thread_checker_; |
193 | 183 |
| 184 // Policy refresh counter used for testing. |
| 185 int policy_refresh_count_; |
| 186 |
194 DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator); | 187 DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator); |
195 }; | 188 }; |
196 | 189 |
197 // Handles invalidations to cloud policy objects. | |
198 class CloudPolicyInvalidationHandler { | |
199 public: | |
200 virtual ~CloudPolicyInvalidationHandler() {} | |
201 | |
202 // This method is called when the current invalidation info should be set | |
203 // on the cloud policy client. | |
204 virtual void SetInvalidationInfo( | |
205 int64 version, | |
206 const std::string& payload) = 0; | |
207 | |
208 // This method is called when the policy should be refreshed due to an | |
209 // invalidation. A policy fetch should be scheduled in the near future. | |
210 virtual void InvalidatePolicy() = 0; | |
211 | |
212 // This method is called when the invalidator determines that the ability to | |
213 // receive policy invalidations becomes enabled or disabled. The invalidator | |
214 // starts in a disabled state, so the first call to this method is always when | |
215 // the invalidator becomes enabled. | |
216 virtual void OnInvalidatorStateChanged(bool invalidations_enabled) = 0; | |
217 }; | |
218 | |
219 } // namespace policy | 190 } // namespace policy |
220 | 191 |
221 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ | 192 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ |
OLD | NEW |