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

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

Issue 23592017: Fix policy invalidator lifecycle bugs for Android. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 3 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
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 // |get_invalidation_service| is a callback which gets a pointer to the
60 // until Shutdown is called. An Initialize method must only be called once. 55 // invalidation service. It must be callable until Shutdown is called and the
61 void InitializeWithProfile(Profile* profile); 56 // pointer returned must also remain valid during that time. A callback is
62 57 // passed instead of of the raw pointer so that the InvalidationService can be
63 // Initializes the invalidator with the invalidation service. It must remain 58 // lazily initialized.
64 // valid until Shutdown is called. An Initialize method must only be called 59 typedef base::Callback<invalidation::InvalidationService*()>
65 // once. 60 GetInvalidationService;
66 void InitializeWithService( 61 void Initialize(const GetInvalidationService& get_invalidation_service);
Mattias Nissler (ping if slow) 2013/09/02 11:54:52 I still don't see why we can't pass the Invalidati
Steve Condie 2013/09/03 04:41:56 Creating and registering with the InvalidationServ
Mattias Nissler (ping if slow) 2013/09/03 14:46:36 Do you have a stack trace? This likely indicates a
Steve Condie 2013/09/04 18:00:38 Added a TODO to investigate a solution on the Inva
67 invalidation::InvalidationService* invalidation_service);
68 62
69 // Shuts down and disables invalidations. It must be called before the object 63 // Shuts down and disables invalidations. It must be called before the object
70 // is destroyed. 64 // is destroyed.
71 void Shutdown(); 65 void Shutdown();
72 66
73 // Whether the invalidator currently has the ability to receive invalidations. 67 // Whether the invalidator currently has the ability to receive invalidations.
74 bool invalidations_enabled() { 68 bool invalidations_enabled() {
75 return invalidations_enabled_; 69 return invalidations_enabled_;
76 } 70 }
77 71
78 // syncer::InvalidationHandler: 72 // syncer::InvalidationHandler:
79 virtual void OnInvalidatorStateChange( 73 virtual void OnInvalidatorStateChange(
80 syncer::InvalidatorState state) OVERRIDE; 74 syncer::InvalidatorState state) OVERRIDE;
81 virtual void OnIncomingInvalidation( 75 virtual void OnIncomingInvalidation(
82 const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE; 76 const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE;
83 77
78 // CloudPolicyCore::Observer:
79 virtual void OnCoreConnected(CloudPolicyCore* core) OVERRIDE;
80 virtual void OnRefreshSchedulerStarted(CloudPolicyCore* core) OVERRIDE;
81 virtual void OnCoreDisconnected(CloudPolicyCore* core) OVERRIDE;
82
84 // CloudPolicyStore::Observer: 83 // CloudPolicyStore::Observer:
85 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE; 84 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE;
86 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE; 85 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE;
87 86
88 protected: 87 // 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 88 // testing only.
90 // should only be used to call one of the Initialize methods, as after the 89 int policy_refresh_count() {
91 // object is initialized weak pointers may be invalidated at any time. 90 return policy_refresh_count_;
92 base::WeakPtr<CloudPolicyInvalidator> GetWeakPtr(); 91 }
92
93 // Expose the number of times the invalidator has notified the refresh
94 // scheduler of a state change. For testing only.
95 int invalidations_enabled_count() {
96 return invalidations_enabled_count_;
97 }
98 int invalidations_disabled_count() {
99 return invalidations_disabled_count_;
100 }
93 101
94 private: 102 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. 103 // Handle an invalidation to the policy.
102 void HandleInvalidation(const syncer::Invalidation& invalidation); 104 void HandleInvalidation(const syncer::Invalidation& invalidation);
103 105
104 // Update object registration with the invalidation service based on the 106 // Update object registration with the invalidation service based on the
105 // given policy data. 107 // given policy data.
106 void UpdateRegistration(const enterprise_management::PolicyData* policy); 108 void UpdateRegistration(const enterprise_management::PolicyData* policy);
107 109
108 // Registers the given object with the invalidation service. 110 // Registers the given object with the invalidation service.
109 void Register(int64 timestamp, const invalidation::ObjectId& object_id); 111 void Register(int64 timestamp, const invalidation::ObjectId& object_id);
110 112
111 // Unregisters the current object with the invalidation service. 113 // Unregisters the current object with the invalidation service.
112 void Unregister(); 114 void Unregister();
113 115
114 // Update |max_fetch_delay_| based on the given policy map. 116 // Update |max_fetch_delay_| based on the given policy map.
115 void UpdateMaxFetchDelay(const PolicyMap& policy_map); 117 void UpdateMaxFetchDelay(const PolicyMap& policy_map);
116 void set_max_fetch_delay(int delay); 118 void set_max_fetch_delay(int delay);
117 119
118 // Updates invalidations_enabled_ and calls the invalidation handler if the 120 // Updates invalidations_enabled_ and calls the invalidation handler if the
119 // value changed. 121 // value changed.
120 void UpdateInvalidationsEnabled(); 122 void UpdateInvalidationsEnabled();
121 123
122 // Run the invalidate callback on the invalidation handler. is_missing_payload 124 // Sets the current invalidation info on the cloud policy client.
123 // is set to true if the callback is being invoked in response to an 125 void SetInvalidationInfo(int64 invalidation_version,
124 // invalidation with a missing payload. 126 const std::string& payload);
125 void RunInvalidateCallback(bool is_missing_payload); 127
128 // Refresh the policy.
129 // |is_missing_payload| is set to true if the callback is being invoked in
130 // response to an invalidation with a missing payload.
131 void RefreshPolicy(bool is_missing_payload);
126 132
127 // Acknowledge the latest invalidation. 133 // Acknowledge the latest invalidation.
128 void AcknowledgeInvalidation(); 134 void AcknowledgeInvalidation();
129 135
130 // Get the kMetricPolicyRefresh histogram metric which should be incremented 136 // Get the kMetricPolicyRefresh histogram metric which should be incremented
131 // when a policy is stored. 137 // when a policy is stored.
132 int GetPolicyRefreshMetric(); 138 int GetPolicyRefreshMetric();
133 139
134 // The handler for invalidations provded by this object. 140 // The state of the object.
135 CloudPolicyInvalidationHandler* invalidation_handler_; 141 enum State {
142 UNINITIALIZED,
143 STOPPED,
144 STARTED,
145 SHUT_DOWN
146 };
147 State state_;
136 148
137 // The cloud policy store. 149 // The cloud policy core.
138 CloudPolicyStore* store_; 150 CloudPolicyCore* core_;
139 151
140 // Schedules delayed tasks. 152 // Schedules delayed tasks.
141 const scoped_refptr<base::SequencedTaskRunner> task_runner_; 153 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
142 154
143 // The profile which will be used to get a reference to an invalidation 155 // The callback invoked to lazily retrieve the invalidation service.
144 // service. 156 GetInvalidationService get_invalidation_service_;
145 Profile* profile_;
146 157
147 // The invalidation service. 158 // The invalidation service.
148 invalidation::InvalidationService* invalidation_service_; 159 invalidation::InvalidationService* invalidation_service_;
149 160
150 // Whether the invalidator currently has the ability to receive invalidations. 161 // Whether the invalidator currently has the ability to receive invalidations.
151 // This is true if the invalidation service is enabled and the invalidator 162 // This is true if the invalidation service is enabled and the invalidator
152 // has registered for a policy object. 163 // has registered for a policy object.
153 bool invalidations_enabled_; 164 bool invalidations_enabled_;
154 165
155 // Whether the invalidation service is currently enabled. 166 // Whether the invalidation service is currently enabled.
(...skipping 28 matching lines...) Expand all
184 base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory_; 195 base::WeakPtrFactory<CloudPolicyInvalidator> weak_factory_;
185 196
186 // The maximum random delay, in ms, between receiving an invalidation and 197 // The maximum random delay, in ms, between receiving an invalidation and
187 // fetching the new policy. 198 // fetching the new policy.
188 int max_fetch_delay_; 199 int max_fetch_delay_;
189 200
190 // 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
191 // thread. 202 // thread.
192 base::ThreadChecker thread_checker_; 203 base::ThreadChecker thread_checker_;
193 204
205 // Counters used for testing.
206 int policy_refresh_count_;
207 int invalidations_enabled_count_;
208 int invalidations_disabled_count_;
209
194 DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator); 210 DISALLOW_COPY_AND_ASSIGN(CloudPolicyInvalidator);
195 }; 211 };
196 212
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 213 } // namespace policy
220 214
221 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ 215 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698