OLD | NEW |
---|---|
(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 #include "content/browser/background_sync/background_sync_manager.h" | |
6 | |
7 #include "base/barrier_closure.h" | |
8 #include "base/bind.h" | |
9 #include "content/browser/background_sync/background_sync.pb.h" | |
10 #include "content/browser/service_worker/service_worker_context_wrapper.h" | |
11 #include "content/browser/service_worker/service_worker_storage.h" | |
12 #include "content/public/browser/browser_thread.h" | |
13 | |
14 namespace { | |
15 const char kBackgroundSyncUserDataKey[] = "BackgroundSyncUserData"; | |
16 } | |
17 | |
18 namespace content { | |
19 | |
20 BackgroundSyncManager::BackgroundSyncRegistrations:: | |
21 BackgroundSyncRegistrations() | |
22 : id_counter(kInitialCounterId) { | |
23 } | |
24 BackgroundSyncManager::BackgroundSyncRegistrations::BackgroundSyncRegistrations( | |
25 BackgroundSyncRegistration::RegistrationId id_counter) | |
26 : id_counter(id_counter) { | |
27 } | |
28 BackgroundSyncManager::BackgroundSyncRegistrations:: | |
29 ~BackgroundSyncRegistrations() { | |
30 } | |
31 | |
32 // static | |
33 scoped_ptr<BackgroundSyncManager> BackgroundSyncManager::Create( | |
34 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) { | |
35 BackgroundSyncManager* sync_manager = | |
36 new BackgroundSyncManager(service_worker_context); | |
37 sync_manager->Init(); | |
38 return make_scoped_ptr(sync_manager); | |
39 } | |
40 | |
41 BackgroundSyncManager::~BackgroundSyncManager() { | |
42 } | |
43 | |
44 void BackgroundSyncManager::Register( | |
45 const GURL& origin, | |
46 int64 sw_registration_id, | |
47 const BackgroundSyncRegistration& sync_registration, | |
48 const StatusAndRegistrationCallback& callback) { | |
49 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
50 DCHECK(sync_registration.id == | |
davidben
2015/03/25 16:01:09
Nit: DCHECK -> DCHECK_EQ
jkarlin
2015/03/25 19:24:18
Done.
| |
51 BackgroundSyncRegistration::kInvalidRegistrationId); | |
52 | |
53 StatusAndRegistrationCallback pending_callback = | |
54 base::Bind(&BackgroundSyncManager::PendingStatusAndRegistrationCallback, | |
55 weak_ptr_factory_.GetWeakPtr(), callback); | |
56 | |
57 op_scheduler_.ScheduleOperation(base::Bind( | |
58 &BackgroundSyncManager::RegisterImpl, weak_ptr_factory_.GetWeakPtr(), | |
59 origin, sw_registration_id, sync_registration, pending_callback)); | |
60 } | |
61 | |
62 void BackgroundSyncManager::Unregister( | |
63 const GURL& origin, | |
64 int64 sw_registration_id, | |
65 const std::string& sync_registration_name, | |
66 BackgroundSyncRegistration::RegistrationId sync_registration_id, | |
67 const StatusCallback& callback) { | |
68 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
69 | |
70 StatusCallback pending_callback = | |
71 base::Bind(&BackgroundSyncManager::PendingStatusCallback, | |
72 weak_ptr_factory_.GetWeakPtr(), callback); | |
73 | |
74 op_scheduler_.ScheduleOperation(base::Bind( | |
75 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), | |
76 origin, sw_registration_id, sync_registration_name, sync_registration_id, | |
77 pending_callback)); | |
78 } | |
79 | |
80 void BackgroundSyncManager::GetRegistration( | |
81 const GURL& origin, | |
82 int64 sw_registration_id, | |
83 const std::string sync_registration_name, | |
84 const StatusAndRegistrationCallback& callback) { | |
85 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
86 | |
87 StatusAndRegistrationCallback pending_callback = | |
88 base::Bind(&BackgroundSyncManager::PendingStatusAndRegistrationCallback, | |
89 weak_ptr_factory_.GetWeakPtr(), callback); | |
90 | |
91 op_scheduler_.ScheduleOperation( | |
92 base::Bind(&BackgroundSyncManager::GetRegistrationImpl, | |
93 weak_ptr_factory_.GetWeakPtr(), origin, sw_registration_id, | |
94 sync_registration_name, pending_callback)); | |
95 } | |
96 | |
97 BackgroundSyncManager::BackgroundSyncManager( | |
98 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) | |
99 : service_worker_context_(service_worker_context), weak_ptr_factory_(this) { | |
100 } | |
101 | |
102 void BackgroundSyncManager::Init() { | |
103 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
104 DCHECK(!op_scheduler_.ScheduledOperations()); | |
105 | |
106 op_scheduler_.ScheduleOperation(base::Bind(&BackgroundSyncManager::InitImpl, | |
107 weak_ptr_factory_.GetWeakPtr())); | |
108 } | |
109 | |
110 void BackgroundSyncManager::InitImpl() { | |
111 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
112 | |
113 GetDataFromBackend( | |
114 kBackgroundSyncUserDataKey, | |
115 base::Bind(&BackgroundSyncManager::InitDidGetDataFromBackend, | |
116 weak_ptr_factory_.GetWeakPtr())); | |
117 } | |
118 | |
119 void BackgroundSyncManager::InitDidGetDataFromBackend( | |
120 const std::vector<std::pair<int64, std::string>>& user_data, | |
121 ServiceWorkerStatusCode status) { | |
122 for (const std::pair<int64, std::string>& data : user_data) { | |
123 BackgroundSyncRegistrationsProto registrations_proto; | |
124 if (registrations_proto.ParseFromString(data.second)) { | |
125 sw_to_registrations_map_[data.first] = BackgroundSyncRegistrations( | |
126 registrations_proto.registration_id_counter()); | |
127 BackgroundSyncRegistrations* registrations = | |
128 &sw_to_registrations_map_[data.first]; | |
129 | |
130 for (int i = 0, max = registrations_proto.registration_size(); i < max; | |
131 ++i) { | |
132 const BackgroundSyncRegistrationProto& registration_proto = | |
133 registrations_proto.registration(i); | |
134 BackgroundSyncRegistration registration(registration_proto.id(), | |
135 registration_proto.name()); | |
davidben
2015/03/25 16:01:09
Optional: since this data comes from disk, is it w
jkarlin
2015/03/25 19:24:18
Done.
| |
136 if (registration_proto.has_min_period()) | |
137 registration.min_period = registration_proto.min_period(); | |
138 registrations->name_to_registration_map[registration_proto.name()] = | |
139 registration; | |
140 } | |
141 } | |
142 } | |
143 | |
144 // TODO(jkarlin): Call the scheduling algorithm here. | |
145 | |
146 op_scheduler_.CompleteOperationAndRunNext(); | |
147 } | |
148 | |
149 void BackgroundSyncManager::RegisterImpl( | |
150 const GURL& origin, | |
151 int64 sw_registration_id, | |
152 const BackgroundSyncRegistration& sync_registration, | |
153 const StatusAndRegistrationCallback& callback) { | |
154 BackgroundSyncRegistration existing_registration; | |
155 if (HasRegistration(sw_registration_id, sync_registration.name, | |
156 &existing_registration)) { | |
157 if (existing_registration.Equals(sync_registration)) { | |
davidben
2015/03/25 16:01:10
This check gives an interesting interaction with U
jkarlin
2015/03/25 19:24:18
Interesting. The race potential is there, sync fun
davidben
2015/03/25 19:42:03
Sounds good.
| |
158 base::MessageLoop::current()->PostTask( | |
159 FROM_HERE, | |
160 base::Bind(callback, ERROR_TYPE_OK, existing_registration)); | |
161 return; | |
162 } | |
163 } | |
164 | |
165 BackgroundSyncRegistration new_registration = sync_registration; | |
166 BackgroundSyncRegistrations* registrations = | |
167 &sw_to_registrations_map_[sw_registration_id]; | |
168 new_registration.id = registrations->id_counter++; | |
169 | |
170 AddRegistrationToMap(sw_registration_id, new_registration); | |
171 | |
172 StoreRegistrations( | |
173 origin, sw_registration_id, | |
174 base::Bind(&BackgroundSyncManager::RegisterDidStore, | |
175 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | |
176 new_registration, existing_registration, callback)); | |
177 } | |
178 | |
179 bool BackgroundSyncManager::HasRegistration( | |
180 int64 sw_registration_id, | |
181 const std::string& sync_registration_name, | |
182 BackgroundSyncRegistration* existing_registration) { | |
183 SWIdToRegistrationsMap::iterator it = | |
184 sw_to_registrations_map_.find(sw_registration_id); | |
185 if (it == sw_to_registrations_map_.end()) | |
186 return false; | |
187 | |
188 const BackgroundSyncRegistrations& registrations = it->second; | |
189 const BackgroundSyncRegistrations::NameToRegistrationMap::const_iterator | |
davidben
2015/03/25 16:01:09
Nit/optional: This type name is probably a prime c
jkarlin
2015/03/25 19:24:18
Done.
| |
190 reg_iter = | |
191 registrations.name_to_registration_map.find(sync_registration_name); | |
192 if (reg_iter == registrations.name_to_registration_map.end()) | |
193 return false; | |
194 | |
195 if (existing_registration) | |
196 *existing_registration = reg_iter->second; | |
197 | |
198 return true; | |
199 } | |
200 | |
201 void BackgroundSyncManager::StoreRegistrations( | |
202 const GURL& origin, | |
203 int64 sw_registration_id, | |
204 const ServiceWorkerStorage::StatusCallback& callback) { | |
205 // Serialize the data. | |
206 const BackgroundSyncRegistrations& registrations = | |
207 sw_to_registrations_map_[sw_registration_id]; | |
208 BackgroundSyncRegistrationsProto registrations_proto; | |
209 registrations_proto.set_registration_id_counter(registrations.id_counter); | |
210 | |
211 for (const auto& name_and_registration : | |
212 registrations.name_to_registration_map) { | |
213 const BackgroundSyncRegistration& registration = | |
214 name_and_registration.second; | |
215 BackgroundSyncRegistrationProto* registration_proto = | |
216 registrations_proto.add_registration(); | |
217 registration_proto->set_id(registration.id); | |
218 registration_proto->set_name(registration.name); | |
219 if (registration.min_period != 0) | |
220 registration_proto->set_min_period(registration.min_period); | |
221 } | |
222 std::string serialized; | |
223 bool success = registrations_proto.SerializeToString(&serialized); | |
224 DCHECK(success); | |
225 | |
226 StoreDataInBackend(sw_registration_id, origin, kBackgroundSyncUserDataKey, | |
227 serialized, callback); | |
228 } | |
229 | |
230 void BackgroundSyncManager::RegisterDidStore( | |
231 int64 sw_registration_id, | |
232 const BackgroundSyncRegistration& sync_registration, | |
davidben
2015/03/25 16:01:09
Nit: new_registration, to match the rename in the
jkarlin
2015/03/25 19:24:18
Done.
| |
233 const BackgroundSyncRegistration& previous_registration, | |
234 const StatusAndRegistrationCallback& callback, | |
235 ServiceWorkerStatusCode status) { | |
236 if (status != SERVICE_WORKER_OK) { | |
237 // Restore the previous state. | |
238 if (previous_registration.id != | |
239 BackgroundSyncRegistration::kInvalidRegistrationId) { | |
240 AddRegistrationToMap(sw_registration_id, previous_registration); | |
241 } else { | |
242 RemoveRegistrationFromMap(sw_registration_id, sync_registration.name, | |
243 nullptr); | |
244 } | |
245 base::MessageLoop::current()->PostTask( | |
246 FROM_HERE, | |
247 base::Bind(callback, ERROR_TYPE_STORAGE, BackgroundSyncRegistration())); | |
248 return; | |
249 } | |
250 | |
251 // TODO(jkarlin): Run the registration algorithm. | |
252 base::MessageLoop::current()->PostTask( | |
253 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, sync_registration)); | |
254 } | |
255 | |
256 void BackgroundSyncManager::RemoveRegistrationFromMap( | |
257 int64 sw_registration_id, | |
258 const std::string& sync_registration_name, | |
259 BackgroundSyncRegistration* old_registration) { | |
davidben
2015/03/25 16:01:09
This never writes the old entry to old_registratio
jkarlin
2015/03/25 19:24:18
!!!!
Done.
| |
260 DCHECK(HasRegistration(sw_registration_id, sync_registration_name, nullptr)); | |
261 | |
262 BackgroundSyncRegistrations* registrations = | |
263 &sw_to_registrations_map_[sw_registration_id]; | |
264 registrations->name_to_registration_map.erase(sync_registration_name); | |
265 } | |
266 | |
267 void BackgroundSyncManager::AddRegistrationToMap( | |
268 int64 sw_registration_id, | |
269 const BackgroundSyncRegistration& sync_registration) { | |
270 DCHECK(sw_registration_id != | |
davidben
2015/03/25 16:01:09
Nit: DCHECK_NE
jkarlin
2015/03/25 19:24:18
Done.
| |
271 BackgroundSyncRegistration::kInvalidRegistrationId); | |
272 | |
273 BackgroundSyncRegistrations* registrations = | |
274 &sw_to_registrations_map_[sw_registration_id]; | |
275 registrations->name_to_registration_map[sync_registration.name] = | |
276 sync_registration; | |
277 } | |
278 | |
279 void BackgroundSyncManager::StoreDataInBackend( | |
280 int64 sw_registration_id, | |
281 const GURL& origin, | |
282 const std::string& key, | |
283 const std::string& data, | |
284 const ServiceWorkerStorage::StatusCallback& callback) { | |
285 service_worker_context_->context()->storage()->StoreUserData( | |
286 sw_registration_id, origin, key, data, callback); | |
287 } | |
288 | |
289 void BackgroundSyncManager::GetDataFromBackend( | |
290 const std::string& key, | |
291 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& | |
292 callback) { | |
293 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
294 | |
295 service_worker_context_->context()->storage()->GetUserDataForAllRegistrations( | |
296 key, callback); | |
297 } | |
298 | |
299 void BackgroundSyncManager::UnregisterImpl( | |
300 const GURL& origin, | |
301 int64 sw_registration_id, | |
302 const std::string& sync_registration_name, | |
303 BackgroundSyncRegistration::RegistrationId sync_registration_id, | |
304 const StatusCallback& callback) { | |
305 BackgroundSyncRegistration existing_registration; | |
306 if (!HasRegistration(sw_registration_id, sync_registration_name, | |
307 &existing_registration) || | |
308 existing_registration.id != sync_registration_id) { | |
309 base::MessageLoop::current()->PostTask( | |
310 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND)); | |
311 return; | |
312 } | |
313 | |
314 BackgroundSyncRegistration old_sync_registration; | |
315 RemoveRegistrationFromMap(sw_registration_id, sync_registration_name, | |
316 &old_sync_registration); | |
317 | |
318 StoreRegistrations( | |
319 origin, sw_registration_id, | |
320 base::Bind(&BackgroundSyncManager::UnregisterDidStore, | |
321 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | |
322 old_sync_registration, callback)); | |
323 } | |
324 | |
325 void BackgroundSyncManager::UnregisterDidStore( | |
326 int64 sw_registration_id, | |
327 const BackgroundSyncRegistration& old_sync_registration, | |
328 const StatusCallback& callback, | |
329 ServiceWorkerStatusCode status) { | |
330 if (status != SERVICE_WORKER_OK) { | |
331 // Restore the previous state. | |
332 AddRegistrationToMap(sw_registration_id, old_sync_registration); | |
333 base::MessageLoop::current()->PostTask( | |
334 FROM_HERE, base::Bind(callback, ERROR_TYPE_STORAGE)); | |
335 return; | |
336 } | |
337 | |
338 // TODO(jkarlin): Run the registration algorithm. | |
339 base::MessageLoop::current()->PostTask(FROM_HERE, | |
340 base::Bind(callback, ERROR_TYPE_OK)); | |
341 } | |
342 | |
343 void BackgroundSyncManager::GetRegistrationImpl( | |
344 const GURL& origin, | |
345 int64 sw_registration_id, | |
346 const std::string sync_registration_name, | |
347 const StatusAndRegistrationCallback& callback) { | |
davidben
2015/03/25 16:01:09
I think this function can just call HasRegistratio
jkarlin
2015/03/25 19:24:18
Done.
| |
348 SWIdToRegistrationsMap::const_iterator it = | |
349 sw_to_registrations_map_.find(sw_registration_id); | |
350 | |
351 if (it == sw_to_registrations_map_.end()) { | |
352 base::MessageLoop::current()->PostTask( | |
353 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND, | |
354 BackgroundSyncRegistration())); | |
355 return; | |
356 } | |
357 | |
358 const BackgroundSyncRegistrations& registrations = it->second; | |
359 const BackgroundSyncRegistrations::NameToRegistrationMap::const_iterator | |
davidben
2015/03/25 16:01:10
Optional/nit: ditto about maybe using auto here.
jkarlin
2015/03/25 19:24:18
no longer needed
| |
360 reg_iter = | |
361 registrations.name_to_registration_map.find(sync_registration_name); | |
362 if (reg_iter == registrations.name_to_registration_map.end()) { | |
363 base::MessageLoop::current()->PostTask( | |
364 FROM_HERE, base::Bind(callback, ERROR_TYPE_NOT_FOUND, | |
365 BackgroundSyncRegistration())); | |
366 return; | |
367 } | |
368 | |
369 base::MessageLoop::current()->PostTask( | |
370 FROM_HERE, base::Bind(callback, ERROR_TYPE_OK, reg_iter->second)); | |
371 } | |
372 | |
373 void BackgroundSyncManager::PendingStatusAndRegistrationCallback( | |
374 const StatusAndRegistrationCallback& callback, | |
375 ErrorType error, | |
376 const BackgroundSyncRegistration& sync_registration) { | |
377 // The callback might delete this object, so hang onto a weak ptr to find out. | |
378 base::WeakPtr<BackgroundSyncManager> manager = weak_ptr_factory_.GetWeakPtr(); | |
379 callback.Run(error, sync_registration); | |
380 if (manager) | |
381 op_scheduler_.CompleteOperationAndRunNext(); | |
382 } | |
383 | |
384 void BackgroundSyncManager::PendingStatusCallback( | |
385 const StatusCallback& callback, | |
386 ErrorType error) { | |
387 // The callback might delete this object, so hang onto a weak ptr to find out. | |
388 base::WeakPtr<BackgroundSyncManager> manager = weak_ptr_factory_.GetWeakPtr(); | |
389 callback.Run(error); | |
390 if (manager) | |
391 op_scheduler_.CompleteOperationAndRunNext(); | |
392 } | |
393 | |
394 } // namespace content | |
OLD | NEW |