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

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

Issue 950343006: [BackgroundSync] Initial land of the BackgroundSyncManager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Mapped registrations, more tests, register overwrites existing, unique ids for registrations Created 5 years, 9 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_
6 #define CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_
7
8 #include <list>
9 #include <map>
10
11 #include "base/callback_forward.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "content/browser/service_worker/service_worker_cache_scheduler.h"
15 #include "content/browser/service_worker/service_worker_storage.h"
16 #include "content/common/content_export.h"
17 #include "content/common/service_worker/service_worker_status_code.h"
18 #include "url/gurl.h"
19
20 namespace content {
21
22 class ServiceWorkerContextWrapper;
23
24 // BackgroundSyncManager manages and stores the set of background sync
25 // registrations across all registered service workers for a profile.
26 // Registrations are stored along with their associated Service Worker
27 // registration in ServiceWorkerStorage. If the ServiceWorker is unregistered,
28 // the sync registrations are removed. This class expects to be run on the IO
29 // thread. The asynchronous methods are executed sequentially.
30
31 // TODO(jkarlin): Check permissions when registering, scheduling, and firing
32 // background sync. In the meantime, --enable-service-worker-sync is required to
33 // fire a sync event.
34 // TODO(jkarlin): Unregister syncs when permission is revoked.
35 // TODO(jkarlin): Create a background sync scheduler to actually run the
36 // registered events.
37 // TODO(jkarlin): Keep the browser alive if "Let Google Chrome Run in the
38 // 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 {
41 public:
42 enum ErrorType {
43 ERROR_TYPE_OK = 0,
44 ERROR_TYPE_STORAGE,
45 ERROR_TYPE_NOT_FOUND
46 };
47
48 // TODO(jkarlin): Remove this and use the struct from IPC messages once it
49 // lands.
50 struct BackgroundSyncRegistration {
51 using RegistrationId = int64;
52 static const int64 kInvalidRegistrationId = -1;
53
54 BackgroundSyncRegistration()
55 : BackgroundSyncRegistration(kInvalidRegistrationId, "") {}
56 explicit BackgroundSyncRegistration(const std::string& name)
57 : BackgroundSyncRegistration(kInvalidRegistrationId, name) {}
58 BackgroundSyncRegistration(int64 id, const std::string& name)
59 : id(id), min_period(0), name(name) {}
60
61 bool Equals(const BackgroundSyncRegistration& other) {
62 return this->name == other.name && this->min_period == other.min_period;
63 }
64
65 RegistrationId id;
66 int64 min_period;
67 std::string name;
68 };
69
70 struct BackgroundSyncRegistrations {
71 using NameToRegistrationMap =
72 std::map<std::string, BackgroundSyncRegistration>;
73 static const int64 kInitialCounterId = 0;
74
75 BackgroundSyncRegistrations();
76 explicit BackgroundSyncRegistrations(
77 BackgroundSyncRegistration::RegistrationId id_counter);
78 ~BackgroundSyncRegistrations();
79
80 NameToRegistrationMap name_to_registration_map;
81 BackgroundSyncRegistration::RegistrationId id_counter;
82 };
83
84 using StatusCallback = base::Callback<void(ErrorType)>;
85 using StatusAndRegistrationCallback =
86 base::Callback<void(ErrorType, const BackgroundSyncRegistration&)>;
87
88 static scoped_ptr<BackgroundSyncManager> Create(
89 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context);
90 virtual ~BackgroundSyncManager();
91
92 // Stores the given background sync registration and adds it to the scheduling
93 // queue. Overwrites any existing registration with the same name but
94 // different parameters (other than the id). Calls |callback| with ErrorTypeOK
95 // and the accepted registration on success. The accepted registration will
96 // have a unique id. It may also have altered parameters if the user or UA
97 // chose different parameters than those supplied.
98 void Register(const GURL& origin,
99 int64 sw_registration_id,
100 const BackgroundSyncRegistration& sync_registration,
101 const StatusAndRegistrationCallback& callback);
102
103 // Removes the given background sync registration from storage and the
104 // scheduling queue. Calls |callback| with ErrorTypeNotFound if not
105 // registered. Calls |callback| with ErrorTypeOK on success.
davidben 2015/03/25 16:01:10 Could you document the relation between |sync_regi
jkarlin 2015/03/25 19:24:18 Done.
106 void Unregister(
107 const GURL& origin,
108 int64 sw_registration_id,
109 const std::string& sync_registration_name,
110 BackgroundSyncRegistration::RegistrationId sync_registration_id,
111 const StatusCallback& callback);
112
113 // Finds the background sync registration associated with
114 // |sw_registration_id|. Calls |callback| with ErrorTypeNotFound if it doesn't
115 // exist. Calls |callback| with ErrorTypeOK on success.
116 void GetRegistration(const GURL& origin,
117 int64 sw_registration_id,
118 const std::string sync_registration_name,
119 const StatusAndRegistrationCallback& callback);
120
121 protected:
122 explicit BackgroundSyncManager(
123 const scoped_refptr<ServiceWorkerContextWrapper>& context);
124
125 // Init must be called before any public member function. Only call it once.
126 void Init();
127
128 // The following methods are virtual for testing.
129 virtual void StoreDataInBackend(
130 int64 sw_registration_id,
131 const GURL& origin,
132 const std::string& key,
133 const std::string& data,
134 const ServiceWorkerStorage::StatusCallback& callback);
135 virtual void GetDataFromBackend(
136 const std::string& key,
137 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback&
138 callback);
139
140 private:
141 using PermissionStatusCallback = base::Callback<void(bool)>;
142 using SWIdToRegistrationsMap = std::map<int64, BackgroundSyncRegistrations>;
143
144 // Returns the existing registration in |existing_registration| if it is not
145 // null.
146 bool HasRegistration(int64 sw_registration_id,
davidben 2015/03/25 16:01:10 Nit: LookupRegistration might be a better name for
jkarlin 2015/03/25 19:24:18 Done.
147 const std::string& sync_registration_name,
148 BackgroundSyncRegistration* existing_registration);
149
150 // Store all registrations for a given |sw_registration_id|.
151 void StoreRegistrations(const GURL& origin,
152 int64 sw_registration_id,
153 const ServiceWorkerStorage::StatusCallback& callback);
154
155 // If the registration is in the map, removes it and returns the removed
156 // registration in |old_registration|. |old_registration| may be null.
157 void RemoveRegistrationFromMap(int64 sw_registration_id,
158 const std::string& sync_registration_name,
159 BackgroundSyncRegistration* old_registration);
160
161 void AddRegistrationToMap(
162 int64 sw_registration_id,
163 const BackgroundSyncRegistration& sync_registration);
164
165 void InitImpl();
166 void InitDidGetDataFromBackend(
167 const std::vector<std::pair<int64, std::string>>& user_data,
168 ServiceWorkerStatusCode status);
169
170 // Register callbacks
171 void RegisterImpl(const GURL& origin,
172 int64 sw_registration_id,
173 const BackgroundSyncRegistration& sync_registration,
174 const StatusAndRegistrationCallback& callback);
175 void RegisterDidStore(int64 sw_registration_id,
176 const BackgroundSyncRegistration& sync_registration,
177 const BackgroundSyncRegistration& previous_registration,
178 const StatusAndRegistrationCallback& callback,
179 ServiceWorkerStatusCode status);
180
181 // Unregister callbacks
182 void UnregisterImpl(
183 const GURL& origin,
184 int64 sw_registration_id,
185 const std::string& sync_registration_name,
186 BackgroundSyncRegistration::RegistrationId sync_registration_id,
187 const StatusCallback& callback);
188 void UnregisterDidStore(
189 int64 sw_registration_id,
190 const BackgroundSyncRegistration& old_sync_registration,
191 const StatusCallback& callback,
192 ServiceWorkerStatusCode status);
193
194 // GetRegistration callbacks
195 void GetRegistrationImpl(const GURL& origin,
196 int64 sw_registration_id,
197 const std::string sync_registration_name,
198 const StatusAndRegistrationCallback& callback);
199
200 // Operation Scheduling callbacks
201 void PendingStatusAndRegistrationCallback(
202 const StatusAndRegistrationCallback& callback,
203 ErrorType error,
204 const BackgroundSyncRegistration& sync_registration);
205 void PendingStatusCallback(const StatusCallback& callback, ErrorType error);
206
207 SWIdToRegistrationsMap sw_to_registrations_map_;
208 ServiceWorkerCacheScheduler op_scheduler_;
209 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
210
211 base::WeakPtrFactory<BackgroundSyncManager> weak_ptr_factory_;
212
213 DISALLOW_COPY_AND_ASSIGN(BackgroundSyncManager);
214 };
215
216 } // namespace content
217
218 #endif // CONTENT_BROWSER_BACKGROUND_SYNC_BACKGROUND_SYNC_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698