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

Side by Side Diff: content/browser/background_sync/background_sync_manager.h

Issue 1048053002: [BackgroundSync] Handle storage failure (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@background_sync
Patch Set: Address comments from PS5 Created 5 years, 8 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
« no previous file with comments | « no previous file | content/browser/background_sync/background_sync_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ 5 #ifndef CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_
6 #define CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ 6 #define CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_
7 7
8 #include <list> 8 #include <list>
9 #include <map> 9 #include <map>
10 10
11 #include "base/callback_forward.h" 11 #include "base/callback_forward.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "content/browser/cache_storage/cache_storage_scheduler.h" 14 #include "content/browser/cache_storage/cache_storage_scheduler.h"
15 #include "content/browser/service_worker/service_worker_context_observer.h"
15 #include "content/browser/service_worker/service_worker_storage.h" 16 #include "content/browser/service_worker/service_worker_storage.h"
16 #include "content/common/content_export.h" 17 #include "content/common/content_export.h"
17 #include "content/common/service_worker/service_worker_status_code.h" 18 #include "content/common/service_worker/service_worker_status_code.h"
18 #include "url/gurl.h" 19 #include "url/gurl.h"
19 20
20 namespace content { 21 namespace content {
21 22
22 class ServiceWorkerContextWrapper; 23 class ServiceWorkerContextWrapper;
23 24
24 // BackgroundSyncManager manages and stores the set of background sync 25 // BackgroundSyncManager manages and stores the set of background sync
25 // registrations across all registered service workers for a profile. 26 // registrations across all registered service workers for a profile.
26 // Registrations are stored along with their associated Service Worker 27 // Registrations are stored along with their associated Service Worker
27 // registration in ServiceWorkerStorage. If the ServiceWorker is unregistered, 28 // registration in ServiceWorkerStorage. If the ServiceWorker is unregistered,
28 // the sync registrations are removed. This class expects to be run on the IO 29 // the sync registrations are removed. This class expects to be run on the IO
29 // thread. The asynchronous methods are executed sequentially. 30 // thread. The asynchronous methods are executed sequentially.
30 31
31 // TODO(jkarlin): Check permissions when registering, scheduling, and firing 32 // TODO(jkarlin): Check permissions when registering, scheduling, and firing
32 // background sync. In the meantime, --enable-service-worker-sync is required to 33 // background sync. In the meantime, --enable-service-worker-sync is required to
33 // fire a sync event. 34 // fire a sync event.
34 // TODO(jkarlin): Unregister syncs when permission is revoked. 35 // TODO(jkarlin): Unregister syncs when permission is revoked.
35 // TODO(jkarlin): Create a background sync scheduler to actually run the 36 // TODO(jkarlin): Create a background sync scheduler to actually run the
36 // registered events. 37 // registered events.
37 // TODO(jkarlin): Keep the browser alive if "Let Google Chrome Run in the 38 // TODO(jkarlin): Keep the browser alive if "Let Google Chrome Run in the
38 // Background" is true and a sync is registered. 39 // Background" is true and a sync is registered.
39 // TODO(jkarlin): Unregister syncs when storage for an origin is cleared. 40 class CONTENT_EXPORT BackgroundSyncManager
40 // TODO(jkarlin): Detect and handle a corrupt or broken backend. 41 : NON_EXPORTED_BASE(public ServiceWorkerContextObserver) {
41 class CONTENT_EXPORT BackgroundSyncManager {
42 public: 42 public:
43 enum ErrorType { 43 enum ErrorType {
44 ERROR_TYPE_OK = 0, 44 ERROR_TYPE_OK = 0,
45 ERROR_TYPE_STORAGE, 45 ERROR_TYPE_STORAGE,
46 ERROR_TYPE_NOT_FOUND 46 ERROR_TYPE_NOT_FOUND
47 }; 47 };
48 48
49 // TODO(jkarlin): Remove this and use the struct from IPC messages once it 49 // TODO(jkarlin): Remove this and use the struct from IPC messages once it
50 // lands. 50 // lands.
51 struct CONTENT_EXPORT BackgroundSyncRegistration { 51 struct CONTENT_EXPORT BackgroundSyncRegistration {
(...skipping 29 matching lines...) Expand all
81 NameToRegistrationMap name_to_registration_map; 81 NameToRegistrationMap name_to_registration_map;
82 BackgroundSyncRegistration::RegistrationId next_id; 82 BackgroundSyncRegistration::RegistrationId next_id;
83 }; 83 };
84 84
85 using StatusCallback = base::Callback<void(ErrorType)>; 85 using StatusCallback = base::Callback<void(ErrorType)>;
86 using StatusAndRegistrationCallback = 86 using StatusAndRegistrationCallback =
87 base::Callback<void(ErrorType, const BackgroundSyncRegistration&)>; 87 base::Callback<void(ErrorType, const BackgroundSyncRegistration&)>;
88 88
89 static scoped_ptr<BackgroundSyncManager> Create( 89 static scoped_ptr<BackgroundSyncManager> Create(
90 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); 90 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context);
91 virtual ~BackgroundSyncManager(); 91 ~BackgroundSyncManager() override;
92 92
93 // Stores the given background sync registration and adds it to the scheduling 93 // Stores the given background sync registration and adds it to the scheduling
94 // queue. Overwrites any existing registration with the same name but 94 // queue. Overwrites any existing registration with the same name but
95 // different parameters (other than the id). Calls |callback| with ErrorTypeOK 95 // different parameters (other than the id). Calls |callback| with ErrorTypeOK
96 // and the accepted registration on success. The accepted registration will 96 // and the accepted registration on success. The accepted registration will
97 // have a unique id. It may also have altered parameters if the user or UA 97 // have a unique id. It may also have altered parameters if the user or UA
98 // chose different parameters than those supplied. 98 // chose different parameters than those supplied.
99 void Register(const GURL& origin, 99 void Register(const GURL& origin,
100 int64 sw_registration_id, 100 int64 sw_registration_id,
101 const BackgroundSyncRegistration& sync_registration, 101 const BackgroundSyncRegistration& sync_registration,
(...skipping 12 matching lines...) Expand all
114 const StatusCallback& callback); 114 const StatusCallback& callback);
115 115
116 // Finds the background sync registration associated with 116 // Finds the background sync registration associated with
117 // |sw_registration_id|. Calls |callback| with ErrorTypeNotFound if it doesn't 117 // |sw_registration_id|. Calls |callback| with ErrorTypeNotFound if it doesn't
118 // exist. Calls |callback| with ErrorTypeOK on success. 118 // exist. Calls |callback| with ErrorTypeOK on success.
119 void GetRegistration(const GURL& origin, 119 void GetRegistration(const GURL& origin,
120 int64 sw_registration_id, 120 int64 sw_registration_id,
121 const std::string sync_registration_name, 121 const std::string sync_registration_name,
122 const StatusAndRegistrationCallback& callback); 122 const StatusAndRegistrationCallback& callback);
123 123
124 // ServiceWorkerContextObserver overrides.
125 void OnRegistrationDeleted(int64 registration_id,
126 const GURL& pattern) override;
127 void OnStorageWiped() override;
128
124 protected: 129 protected:
125 explicit BackgroundSyncManager( 130 explicit BackgroundSyncManager(
126 const scoped_refptr<ServiceWorkerContextWrapper>& context); 131 const scoped_refptr<ServiceWorkerContextWrapper>& context);
127 132
128 // Init must be called before any public member function. Only call it once. 133 // Init must be called before any public member function. Only call it once.
129 void Init(); 134 void Init();
130 135
131 // The following methods are virtual for testing. 136 // The following methods are virtual for testing.
132 virtual void StoreDataInBackend( 137 virtual void StoreDataInBackend(
133 int64 sw_registration_id, 138 int64 sw_registration_id,
134 const GURL& origin, 139 const GURL& origin,
135 const std::string& key, 140 const std::string& key,
136 const std::string& data, 141 const std::string& data,
137 const ServiceWorkerStorage::StatusCallback& callback); 142 const ServiceWorkerStorage::StatusCallback& callback);
138 virtual void GetDataFromBackend( 143 virtual void GetDataFromBackend(
139 const std::string& key, 144 const std::string& key,
140 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& 145 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback&
141 callback); 146 callback);
142 147
143 private: 148 private:
144 using PermissionStatusCallback = base::Callback<void(bool)>; 149 using PermissionStatusCallback = base::Callback<void(bool)>;
145 using SWIdToRegistrationsMap = std::map<int64, BackgroundSyncRegistrations>; 150 using SWIdToRegistrationsMap = std::map<int64, BackgroundSyncRegistrations>;
146 151
152 // Disable the manager. Already queued operations will abort once they start
153 // to run (in their impl methods). Future operations will not queue. Any
154 // registrations are cleared from memory and the backend (if it's still
155 // functioning). The manager will reenable itself once it receives the
156 // OnStorageWiped message or on browser restart.
157 void DisableAndClearManager(const base::Closure& callback);
158 void DisableAndClearDidGetRegistrations(
159 const base::Closure& callback,
160 const std::vector<std::pair<int64, std::string>>& user_data,
161 ServiceWorkerStatusCode status);
162 void DisableAndClearManagerClearedOne(const base::Closure& barrier_closure,
163 ServiceWorkerStatusCode status);
164
147 // Returns the existing registration in |existing_registration| if it is not 165 // Returns the existing registration in |existing_registration| if it is not
148 // null. 166 // null.
149 bool LookupRegistration(int64 sw_registration_id, 167 bool LookupRegistration(int64 sw_registration_id,
150 const std::string& sync_registration_name, 168 const std::string& sync_registration_name,
151 BackgroundSyncRegistration* existing_registration); 169 BackgroundSyncRegistration* existing_registration);
152 170
153 // Store all registrations for a given |sw_registration_id|. 171 // Store all registrations for a given |sw_registration_id|.
154 void StoreRegistrations(const GURL& origin, 172 void StoreRegistrations(const GURL& origin,
155 int64 sw_registration_id, 173 int64 sw_registration_id,
156 const ServiceWorkerStorage::StatusCallback& callback); 174 const ServiceWorkerStorage::StatusCallback& callback);
157 175
158 // If the registration is in the map, removes it and returns the removed 176 // If the registration is in the map, removes it and returns the removed
159 // registration in |old_registration|. |old_registration| may be null. 177 // registration in |old_registration|. |old_registration| may be null.
160 void RemoveRegistrationFromMap(int64 sw_registration_id, 178 void RemoveRegistrationFromMap(int64 sw_registration_id,
161 const std::string& sync_registration_name, 179 const std::string& sync_registration_name,
162 BackgroundSyncRegistration* old_registration); 180 BackgroundSyncRegistration* old_registration);
163 181
164 void AddRegistrationToMap( 182 void AddRegistrationToMap(
165 int64 sw_registration_id, 183 int64 sw_registration_id,
166 const BackgroundSyncRegistration& sync_registration); 184 const BackgroundSyncRegistration& sync_registration);
167 185
168 void InitImpl(); 186 void InitImpl(const base::Closure& callback);
169 void InitDidGetDataFromBackend( 187 void InitDidGetDataFromBackend(
188 const base::Closure& callback,
170 const std::vector<std::pair<int64, std::string>>& user_data, 189 const std::vector<std::pair<int64, std::string>>& user_data,
171 ServiceWorkerStatusCode status); 190 ServiceWorkerStatusCode status);
172 191
173 // Register callbacks 192 // Register callbacks
174 void RegisterImpl(const GURL& origin, 193 void RegisterImpl(const GURL& origin,
175 int64 sw_registration_id, 194 int64 sw_registration_id,
176 const BackgroundSyncRegistration& sync_registration, 195 const BackgroundSyncRegistration& sync_registration,
177 const StatusAndRegistrationCallback& callback); 196 const StatusAndRegistrationCallback& callback);
178 void RegisterDidStore(int64 sw_registration_id, 197 void RegisterDidStore(int64 sw_registration_id,
179 const BackgroundSyncRegistration& sync_registration, 198 const BackgroundSyncRegistration& sync_registration,
180 const BackgroundSyncRegistration& previous_registration,
181 const StatusAndRegistrationCallback& callback, 199 const StatusAndRegistrationCallback& callback,
182 ServiceWorkerStatusCode status); 200 ServiceWorkerStatusCode status);
183 201
184 // Unregister callbacks 202 // Unregister callbacks
185 void UnregisterImpl( 203 void UnregisterImpl(
186 const GURL& origin, 204 const GURL& origin,
187 int64 sw_registration_id, 205 int64 sw_registration_id,
188 const std::string& sync_registration_name, 206 const std::string& sync_registration_name,
189 BackgroundSyncRegistration::RegistrationId sync_registration_id, 207 BackgroundSyncRegistration::RegistrationId sync_registration_id,
190 const StatusCallback& callback); 208 const StatusCallback& callback);
191 void UnregisterDidStore( 209 void UnregisterDidStore(
192 int64 sw_registration_id, 210 int64 sw_registration_id,
193 const BackgroundSyncRegistration& old_sync_registration, 211 const BackgroundSyncRegistration& old_sync_registration,
194 const StatusCallback& callback, 212 const StatusCallback& callback,
195 ServiceWorkerStatusCode status); 213 ServiceWorkerStatusCode status);
196 214
197 // GetRegistration callbacks 215 // GetRegistration callbacks
198 void GetRegistrationImpl(const GURL& origin, 216 void GetRegistrationImpl(const GURL& origin,
199 int64 sw_registration_id, 217 int64 sw_registration_id,
200 const std::string sync_registration_name, 218 const std::string sync_registration_name,
201 const StatusAndRegistrationCallback& callback); 219 const StatusAndRegistrationCallback& callback);
202 220
221 // OnRegistrationDeleted callbacks
222 void OnRegistrationDeletedImpl(int64 registration_id,
223 const base::Closure& callback);
224
225 // OnStorageWiped callbacks
226 void OnStorageWipedImpl(const base::Closure& callback);
227
203 // Operation Scheduling callbacks 228 // Operation Scheduling callbacks
204 void PendingStatusAndRegistrationCallback( 229 void PendingStatusAndRegistrationCallback(
205 const StatusAndRegistrationCallback& callback, 230 const StatusAndRegistrationCallback& callback,
206 ErrorType error, 231 ErrorType error,
207 const BackgroundSyncRegistration& sync_registration); 232 const BackgroundSyncRegistration& sync_registration);
208 void PendingStatusCallback(const StatusCallback& callback, ErrorType error); 233 void PendingStatusCallback(const StatusCallback& callback, ErrorType error);
234 void PendingClosure(const base::Closure& closure);
235
236 StatusAndRegistrationCallback MakeStatusAndRegistrationCompletion(
237 const StatusAndRegistrationCallback& callback);
238 BackgroundSyncManager::StatusCallback MakeStatusCompletion(
239 const StatusCallback& callback);
240 base::Closure MakeEmptyCompletion();
209 241
210 SWIdToRegistrationsMap sw_to_registrations_map_; 242 SWIdToRegistrationsMap sw_to_registrations_map_;
211 CacheStorageScheduler op_scheduler_; 243 CacheStorageScheduler op_scheduler_;
212 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; 244 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
245 bool disabled_;
213 246
214 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_; 247 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_;
215 248
216 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager); 249 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager);
217 }; 250 };
218 251
219 } // namespace content 252 } // namespace content
220 253
221 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_ 254 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_
OLDNEW
« no previous file with comments | « no previous file | content/browser/background_sync/background_sync_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698