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

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_store.h" 15 #include "chrome/browser/policy/cloud/cloud_policy_store.h"
16 #include "sync/notifier/invalidation_handler.h" 16 #include "sync/notifier/invalidation_handler.h"
17 17
18 class Profile;
19
20 namespace base { 18 namespace base {
21 class SequencedTaskRunner; 19 class SequencedTaskRunner;
22 } 20 }
23 21
24 namespace invalidation { 22 namespace invalidation {
25 class InvalidationService; 23 class InvalidationService;
26 } 24 }
27 25
28 namespace policy { 26 namespace policy {
29 27
30 class CloudPolicyInvalidationHandler; 28 class CloudPolicyInvalidationHandler;
31 29
32 // Listens for and provides policy invalidations. 30 // Listens for and provides policy invalidations.
33 class CloudPolicyInvalidator : public syncer::InvalidationHandler, 31 class CloudPolicyInvalidator : public syncer::InvalidationHandler,
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
43 // A callback used to get a pointer to an invalidation service.
44 typedef base::Callback<invalidation::InvalidationService*()>
45 GetInvalidationService;
46
47 // |get_invalidation_service| is a callback which gets a pointer to the
48 // invalidation service. The pointer returned is not owned by this object and
49 // must remain valid until Shutdown is called. A callback is passed instead of
50 // of the raw pointer so that the InvalidationService can be lazily
51 // initialized. This prevents attempting to access the service before it is
52 // ready or if it is not needed.
Mattias Nissler (ping if slow) 2013/08/29 16:02:30 Can you tell me more about the not-ready case? It
Steve Condie 2013/08/30 00:16:35 The requirements to turn on the invalidator are 1)
Mattias Nissler (ping if slow) 2013/09/02 11:54:52 Why?
45 // |invalidation_handler| handles invalidations provided by this object and 53 // |invalidation_handler| handles invalidations provided by this object and
46 // must remain valid until Shutdown is called. 54 // must remain valid until Shutdown is called.
47 // |store| is cloud policy store. It must remain valid until Shutdown is 55 // |store| is cloud policy store. It must remain valid until Shutdown is
48 // called. 56 // called.
49 // |task_runner| is used for scheduling delayed tasks. It must post tasks to 57 // |task_runner| is used for scheduling delayed tasks. It must post tasks to
50 // the main policy thread. 58 // the main policy thread.
51 CloudPolicyInvalidator( 59 CloudPolicyInvalidator(
60 const GetInvalidationService& get_invalidation_service,
52 CloudPolicyInvalidationHandler* invalidation_handler, 61 CloudPolicyInvalidationHandler* invalidation_handler,
53 CloudPolicyStore* store, 62 CloudPolicyStore* store,
54 const scoped_refptr<base::SequencedTaskRunner>& task_runner); 63 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
55 virtual ~CloudPolicyInvalidator(); 64 virtual ~CloudPolicyInvalidator();
56 65
57 // Initializes the invalidator with the given profile. The invalidator uses 66 // Starts the invalidator. This method should be called after the invalidation
58 // the profile to get a reference to the profile's invalidation service if 67 // handler is ready to receive invalidations and the invalidation service is
59 // needed. Both the profile and the invalidation service must remain valid 68 // ready for use. This method must only be called when the object is in the
60 // until Shutdown is called. An Initialize method must only be called once. 69 // stopped state (the initial state) and moves the object into the started
61 void InitializeWithProfile(Profile* profile); 70 // state.
71 void Start();
62 72
63 // Initializes the invalidator with the invalidation service. It must remain 73 // Stops the invalidator. This method should be only called when the cloud
64 // valid until Shutdown is called. An Initialize method must only be called 74 // policy is being discarded because the user explicitly signed out; it should
65 // once. 75 // not be called for a normal session shutdown. This method must only be
66 void InitializeWithService( 76 // called when the object is in the started state and moves the object into
67 invalidation::InvalidationService* invalidation_service); 77 // the stopped state.
78 void Stop();
68 79
69 // Shuts down and disables invalidations. It must be called before the object 80 // Shuts down the invalidator. This method must be called before the object
70 // is destroyed. 81 // is destroyed. It can be called when the object is in either the started or
82 // stopped state and moves the object into the shut-down state.
71 void Shutdown(); 83 void Shutdown();
72 84
73 // Whether the invalidator currently has the ability to receive invalidations. 85 // Whether the invalidator currently has the ability to receive invalidations.
74 bool invalidations_enabled() { 86 bool invalidations_enabled() {
75 return invalidations_enabled_; 87 return invalidations_enabled_;
76 } 88 }
77 89
78 // syncer::InvalidationHandler: 90 // syncer::InvalidationHandler:
79 virtual void OnInvalidatorStateChange( 91 virtual void OnInvalidatorStateChange(
80 syncer::InvalidatorState state) OVERRIDE; 92 syncer::InvalidatorState state) OVERRIDE;
81 virtual void OnIncomingInvalidation( 93 virtual void OnIncomingInvalidation(
82 const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE; 94 const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE;
83 95
84 // CloudPolicyStore::Observer: 96 // CloudPolicyStore::Observer:
85 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE; 97 virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE;
86 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE; 98 virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE;
87 99
88 protected:
89 // Allows subclasses to create a weak pointer to the object. The pointer
90 // should only be used to call one of the Initialize methods, as after the
91 // object is initialized weak pointers may be invalidated at any time.
92 base::WeakPtr<CloudPolicyInvalidator> GetWeakPtr();
93
94 private: 100 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. 101 // Handle an invalidation to the policy.
102 void HandleInvalidation(const syncer::Invalidation& invalidation); 102 void HandleInvalidation(const syncer::Invalidation& invalidation);
103 103
104 // Update object registration with the invalidation service based on the 104 // Update object registration with the invalidation service based on the
105 // given policy data. 105 // given policy data.
106 void UpdateRegistration(const enterprise_management::PolicyData* policy); 106 void UpdateRegistration(const enterprise_management::PolicyData* policy);
107 107
108 // Registers the given object with the invalidation service. 108 // Registers the given object with the invalidation service.
109 void Register(int64 timestamp, const invalidation::ObjectId& object_id); 109 void Register(int64 timestamp, const invalidation::ObjectId& object_id);
110 110
(...skipping 13 matching lines...) Expand all
124 // invalidation with a missing payload. 124 // invalidation with a missing payload.
125 void RunInvalidateCallback(bool is_missing_payload); 125 void RunInvalidateCallback(bool is_missing_payload);
126 126
127 // Acknowledge the latest invalidation. 127 // Acknowledge the latest invalidation.
128 void AcknowledgeInvalidation(); 128 void AcknowledgeInvalidation();
129 129
130 // Get the kMetricPolicyRefresh histogram metric which should be incremented 130 // Get the kMetricPolicyRefresh histogram metric which should be incremented
131 // when a policy is stored. 131 // when a policy is stored.
132 int GetPolicyRefreshMetric(); 132 int GetPolicyRefreshMetric();
133 133
134 // The handler for invalidations provded by this object. 134 // The state of the object.
135 enum State {
136 STOPPED,
137 STARTED,
138 SHUT_DOWN
139 };
140 State state_;
141
142 // The callback invoked to lazily retrieve the invalidation service.
143 GetInvalidationService get_invalidation_service_;
144
145 // The handler for invalidations provided by this object.
135 CloudPolicyInvalidationHandler* invalidation_handler_; 146 CloudPolicyInvalidationHandler* invalidation_handler_;
136 147
137 // The cloud policy store. 148 // The cloud policy store.
138 CloudPolicyStore* store_; 149 CloudPolicyStore* store_;
139 150
140 // Schedules delayed tasks. 151 // Schedules delayed tasks.
141 const scoped_refptr<base::SequencedTaskRunner> task_runner_; 152 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
142 153
143 // The profile which will be used to get a reference to an invalidation
144 // service.
145 Profile* profile_;
146
147 // The invalidation service. 154 // The invalidation service.
148 invalidation::InvalidationService* invalidation_service_; 155 invalidation::InvalidationService* invalidation_service_;
149 156
150 // Whether the invalidator currently has the ability to receive invalidations. 157 // Whether the invalidator currently has the ability to receive invalidations.
151 // This is true if the invalidation service is enabled and the invalidator 158 // This is true if the invalidation service is enabled and the invalidator
152 // has registered for a policy object. 159 // has registered for a policy object.
153 bool invalidations_enabled_; 160 bool invalidations_enabled_;
154 161
155 // Whether the invalidation service is currently enabled. 162 // Whether the invalidation service is currently enabled.
156 bool invalidation_service_enabled_; 163 bool invalidation_service_enabled_;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 // This method is called when the invalidator determines that the ability to 219 // This method is called when the invalidator determines that the ability to
213 // receive policy invalidations becomes enabled or disabled. The invalidator 220 // 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 221 // starts in a disabled state, so the first call to this method is always when
215 // the invalidator becomes enabled. 222 // the invalidator becomes enabled.
216 virtual void OnInvalidatorStateChanged(bool invalidations_enabled) = 0; 223 virtual void OnInvalidatorStateChanged(bool invalidations_enabled) = 0;
217 }; 224 };
218 225
219 } // namespace policy 226 } // namespace policy
220 227
221 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_ 228 #endif // CHROME_BROWSER_POLICY_CLOUD_CLOUD_POLICY_INVALIDATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698