OLD | NEW |
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 #include "content/browser/background_sync/background_sync_manager.h" | 5 #include "content/browser/background_sync/background_sync_manager.h" |
6 | 6 |
7 #include "base/barrier_closure.h" | 7 #include "base/barrier_closure.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
12 #include "content/browser/background_sync/background_sync_metrics.h" | 12 #include "content/browser/background_sync/background_sync_metrics.h" |
13 #include "content/browser/background_sync/background_sync_network_observer.h" | 13 #include "content/browser/background_sync/background_sync_network_observer.h" |
14 #include "content/browser/background_sync/background_sync_power_observer.h" | 14 #include "content/browser/background_sync/background_sync_power_observer.h" |
| 15 #include "content/browser/background_sync/background_sync_registration_handle.h" |
15 #include "content/browser/background_sync/background_sync_registration_options.h
" | 16 #include "content/browser/background_sync/background_sync_registration_options.h
" |
16 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 17 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
17 #include "content/browser/service_worker/service_worker_storage.h" | 18 #include "content/browser/service_worker/service_worker_storage.h" |
18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
19 | 20 |
20 #if defined(OS_ANDROID) | 21 #if defined(OS_ANDROID) |
21 #include "content/browser/android/background_sync_launcher_android.h" | 22 #include "content/browser/android/background_sync_launcher_android.h" |
22 #endif | 23 #endif |
23 | 24 |
| 25 namespace content { |
| 26 |
| 27 class BackgroundSyncManager::RefCountedRegistration |
| 28 : public base::RefCounted<RefCountedRegistration> { |
| 29 public: |
| 30 BackgroundSyncRegistration* value() { return ®istration_; } |
| 31 const BackgroundSyncRegistration* value() const { return ®istration_; } |
| 32 |
| 33 private: |
| 34 friend class base::RefCounted<RefCountedRegistration>; |
| 35 ~RefCountedRegistration() = default; |
| 36 |
| 37 BackgroundSyncRegistration registration_; |
| 38 }; |
| 39 |
24 namespace { | 40 namespace { |
| 41 |
25 const char kBackgroundSyncUserDataKey[] = "BackgroundSyncUserData"; | 42 const char kBackgroundSyncUserDataKey[] = "BackgroundSyncUserData"; |
| 43 |
| 44 void PostErrorResponse( |
| 45 BackgroundSyncStatus status, |
| 46 const BackgroundSyncManager::StatusAndRegistrationCallback& callback) { |
| 47 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 48 FROM_HERE, |
| 49 base::Bind( |
| 50 callback, status, |
| 51 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>().Pass()))); |
26 } | 52 } |
27 | 53 |
28 namespace content { | 54 } // namespace |
29 | 55 |
30 BackgroundSyncManager::BackgroundSyncRegistrations:: | 56 BackgroundSyncManager::BackgroundSyncRegistrations:: |
31 BackgroundSyncRegistrations() | 57 BackgroundSyncRegistrations() |
32 : next_id(BackgroundSyncRegistration::kInitialId) { | 58 : next_id(BackgroundSyncRegistration::kInitialId) { |
33 } | 59 } |
34 | 60 |
35 BackgroundSyncManager::BackgroundSyncRegistrations:: | 61 BackgroundSyncManager::BackgroundSyncRegistrations:: |
36 ~BackgroundSyncRegistrations() { | 62 ~BackgroundSyncRegistrations() { |
37 } | 63 } |
38 | 64 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = | 107 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
82 AreOptionConditionsMet(options) | 108 AreOptionConditionsMet(options) |
83 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE | 109 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
84 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; | 110 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
85 | 111 |
86 if (disabled_) { | 112 if (disabled_) { |
87 BackgroundSyncMetrics::CountRegister( | 113 BackgroundSyncMetrics::CountRegister( |
88 options.periodicity, registration_could_fire, | 114 options.periodicity, registration_could_fire, |
89 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | 115 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
90 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 116 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
91 base::ThreadTaskRunnerHandle::Get()->PostTask( | 117 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
92 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | |
93 BackgroundSyncRegistration())); | |
94 return; | 118 return; |
95 } | 119 } |
96 | 120 |
97 op_scheduler_.ScheduleOperation(base::Bind( | 121 op_scheduler_.ScheduleOperation(base::Bind( |
98 &BackgroundSyncManager::RegisterImpl, weak_ptr_factory_.GetWeakPtr(), | 122 &BackgroundSyncManager::RegisterImpl, weak_ptr_factory_.GetWeakPtr(), |
99 sw_registration_id, options, requested_from_service_worker, | 123 sw_registration_id, options, requested_from_service_worker, |
100 MakeStatusAndRegistrationCompletion(callback))); | 124 MakeStatusAndRegistrationCompletion(callback))); |
101 } | 125 } |
102 | 126 |
103 void BackgroundSyncManager::Unregister( | |
104 int64 sw_registration_id, | |
105 const std::string& sync_registration_tag, | |
106 SyncPeriodicity periodicity, | |
107 BackgroundSyncRegistration::RegistrationId sync_registration_id, | |
108 const StatusCallback& callback) { | |
109 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
110 | |
111 if (disabled_) { | |
112 BackgroundSyncMetrics::CountUnregister( | |
113 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | |
114 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
115 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | |
116 return; | |
117 } | |
118 | |
119 RegistrationKey registration_key(sync_registration_tag, periodicity); | |
120 | |
121 op_scheduler_.ScheduleOperation(base::Bind( | |
122 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), | |
123 sw_registration_id, registration_key, sync_registration_id, periodicity, | |
124 MakeStatusCompletion(callback))); | |
125 } | |
126 | |
127 void BackgroundSyncManager::GetRegistration( | 127 void BackgroundSyncManager::GetRegistration( |
128 int64 sw_registration_id, | 128 int64 sw_registration_id, |
129 const std::string& sync_registration_tag, | 129 const std::string& sync_registration_tag, |
130 SyncPeriodicity periodicity, | 130 SyncPeriodicity periodicity, |
131 const StatusAndRegistrationCallback& callback) { | 131 const StatusAndRegistrationCallback& callback) { |
132 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 132 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
133 | 133 |
134 if (disabled_) { | 134 if (disabled_) { |
135 base::ThreadTaskRunnerHandle::Get()->PostTask( | 135 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
136 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | |
137 BackgroundSyncRegistration())); | |
138 return; | 136 return; |
139 } | 137 } |
140 | 138 |
141 RegistrationKey registration_key(sync_registration_tag, periodicity); | 139 RegistrationKey registration_key(sync_registration_tag, periodicity); |
142 | 140 |
143 op_scheduler_.ScheduleOperation(base::Bind( | 141 op_scheduler_.ScheduleOperation(base::Bind( |
144 &BackgroundSyncManager::GetRegistrationImpl, | 142 &BackgroundSyncManager::GetRegistrationImpl, |
145 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, registration_key, | 143 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, registration_key, |
146 MakeStatusAndRegistrationCompletion(callback))); | 144 MakeStatusAndRegistrationCompletion(callback))); |
147 } | 145 } |
148 | 146 |
149 void BackgroundSyncManager::GetRegistrations( | 147 void BackgroundSyncManager::GetRegistrations( |
150 int64 sw_registration_id, | 148 int64 sw_registration_id, |
151 SyncPeriodicity periodicity, | 149 SyncPeriodicity periodicity, |
152 const StatusAndRegistrationsCallback& callback) { | 150 const StatusAndRegistrationsCallback& callback) { |
153 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 151 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
154 | 152 |
155 if (disabled_) { | 153 if (disabled_) { |
156 base::ThreadTaskRunnerHandle::Get()->PostTask( | 154 base::ThreadTaskRunnerHandle::Get()->PostTask( |
157 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 155 FROM_HERE, |
158 std::vector<BackgroundSyncRegistration>())); | 156 base::Bind( |
| 157 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
| 158 base::Passed( |
| 159 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>>() |
| 160 .Pass()))); |
159 return; | 161 return; |
160 } | 162 } |
161 | 163 |
162 op_scheduler_.ScheduleOperation( | 164 op_scheduler_.ScheduleOperation( |
163 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl, | 165 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl, |
164 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 166 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
165 periodicity, MakeStatusAndRegistrationsCompletion(callback))); | 167 periodicity, MakeStatusAndRegistrationsCompletion(callback))); |
166 } | 168 } |
167 | 169 |
| 170 // Given a HandleId |handle_id|, return a new handle for the same |
| 171 // registration. |
| 172 scoped_ptr<BackgroundSyncRegistrationHandle> |
| 173 BackgroundSyncManager::DuplicateRegistrationHandle( |
| 174 BackgroundSyncRegistrationHandle::HandleId handle_id) { |
| 175 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 176 |
| 177 scoped_refptr<RefCountedRegistration>* ref_registration = |
| 178 registration_handle_ids_.Lookup(handle_id); |
| 179 if (!ref_registration) |
| 180 return scoped_ptr<BackgroundSyncRegistrationHandle>(); |
| 181 return CreateRegistrationHandle(ref_registration->get()); |
| 182 } |
| 183 |
168 void BackgroundSyncManager::OnRegistrationDeleted(int64 registration_id, | 184 void BackgroundSyncManager::OnRegistrationDeleted(int64 registration_id, |
169 const GURL& pattern) { | 185 const GURL& pattern) { |
170 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 186 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
171 | 187 |
172 // Operations already in the queue will either fail when they write to storage | 188 // Operations already in the queue will either fail when they write to storage |
173 // or return stale results based on registrations loaded in memory. This is | 189 // or return stale results based on registrations loaded in memory. This is |
174 // inconsequential since the service worker is gone. | 190 // inconsequential since the service worker is gone. |
175 op_scheduler_.ScheduleOperation(base::Bind( | 191 op_scheduler_.ScheduleOperation(base::Bind( |
176 &BackgroundSyncManager::OnRegistrationDeletedImpl, | 192 &BackgroundSyncManager::OnRegistrationDeletedImpl, |
177 weak_ptr_factory_.GetWeakPtr(), registration_id, MakeEmptyCompletion())); | 193 weak_ptr_factory_.GetWeakPtr(), registration_id, MakeEmptyCompletion())); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 LOG(ERROR) << "BackgroundSync failed to init due to backend failure."; | 255 LOG(ERROR) << "BackgroundSync failed to init due to backend failure."; |
240 DisableAndClearManager(base::Bind(callback)); | 256 DisableAndClearManager(base::Bind(callback)); |
241 return; | 257 return; |
242 } | 258 } |
243 | 259 |
244 bool corruption_detected = false; | 260 bool corruption_detected = false; |
245 for (const std::pair<int64, std::string>& data : user_data) { | 261 for (const std::pair<int64, std::string>& data : user_data) { |
246 BackgroundSyncRegistrationsProto registrations_proto; | 262 BackgroundSyncRegistrationsProto registrations_proto; |
247 if (registrations_proto.ParseFromString(data.second)) { | 263 if (registrations_proto.ParseFromString(data.second)) { |
248 BackgroundSyncRegistrations* registrations = | 264 BackgroundSyncRegistrations* registrations = |
249 &sw_to_registrations_map_[data.first]; | 265 &active_registrations_[data.first]; |
250 registrations->next_id = registrations_proto.next_registration_id(); | 266 registrations->next_id = registrations_proto.next_registration_id(); |
251 registrations->origin = GURL(registrations_proto.origin()); | 267 registrations->origin = GURL(registrations_proto.origin()); |
252 | 268 |
253 for (int i = 0, max = registrations_proto.registration_size(); i < max; | 269 for (int i = 0, max = registrations_proto.registration_size(); i < max; |
254 ++i) { | 270 ++i) { |
255 const BackgroundSyncRegistrationProto& registration_proto = | 271 const BackgroundSyncRegistrationProto& registration_proto = |
256 registrations_proto.registration(i); | 272 registrations_proto.registration(i); |
257 | 273 |
258 if (registration_proto.id() >= registrations->next_id) { | 274 if (registration_proto.id() >= registrations->next_id) { |
259 corruption_detected = true; | 275 corruption_detected = true; |
260 break; | 276 break; |
261 } | 277 } |
262 | 278 |
263 RegistrationKey registration_key(registration_proto.tag(), | 279 RegistrationKey registration_key(registration_proto.tag(), |
264 registration_proto.periodicity()); | 280 registration_proto.periodicity()); |
265 BackgroundSyncRegistration* registration = | 281 |
266 ®istrations->registration_map[registration_key]; | 282 scoped_refptr<RefCountedRegistration> ref_registration( |
| 283 new RefCountedRegistration()); |
| 284 registrations->registration_map[registration_key] = ref_registration; |
| 285 BackgroundSyncRegistration* registration = ref_registration->value(); |
267 | 286 |
268 BackgroundSyncRegistrationOptions* options = registration->options(); | 287 BackgroundSyncRegistrationOptions* options = registration->options(); |
269 options->tag = registration_proto.tag(); | 288 options->tag = registration_proto.tag(); |
270 options->periodicity = registration_proto.periodicity(); | 289 options->periodicity = registration_proto.periodicity(); |
271 options->min_period = registration_proto.min_period(); | 290 options->min_period = registration_proto.min_period(); |
272 options->network_state = registration_proto.network_state(); | 291 options->network_state = registration_proto.network_state(); |
273 options->power_state = registration_proto.power_state(); | 292 options->power_state = registration_proto.power_state(); |
274 | 293 |
275 registration->set_id(registration_proto.id()); | 294 registration->set_id(registration_proto.id()); |
276 registration->set_sync_state(registration_proto.sync_state()); | 295 registration->set_sync_state(registration_proto.sync_state()); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = | 329 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
311 AreOptionConditionsMet(options) | 330 AreOptionConditionsMet(options) |
312 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE | 331 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
313 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; | 332 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
314 | 333 |
315 if (disabled_) { | 334 if (disabled_) { |
316 BackgroundSyncMetrics::CountRegister( | 335 BackgroundSyncMetrics::CountRegister( |
317 options.periodicity, registration_could_fire, | 336 options.periodicity, registration_could_fire, |
318 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | 337 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
319 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 338 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
320 base::ThreadTaskRunnerHandle::Get()->PostTask( | 339 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
321 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | |
322 BackgroundSyncRegistration())); | |
323 return; | 340 return; |
324 } | 341 } |
325 | 342 |
326 ServiceWorkerRegistration* sw_registration = | 343 ServiceWorkerRegistration* sw_registration = |
327 service_worker_context_->GetLiveRegistration(sw_registration_id); | 344 service_worker_context_->GetLiveRegistration(sw_registration_id); |
328 if (!sw_registration || !sw_registration->active_version()) { | 345 if (!sw_registration || !sw_registration->active_version()) { |
329 BackgroundSyncMetrics::CountRegister( | 346 BackgroundSyncMetrics::CountRegister( |
330 options.periodicity, registration_could_fire, | 347 options.periodicity, registration_could_fire, |
331 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | 348 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
332 BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); | 349 BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); |
333 base::ThreadTaskRunnerHandle::Get()->PostTask( | 350 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback); |
334 FROM_HERE, | |
335 base::Bind(callback, BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, | |
336 BackgroundSyncRegistration())); | |
337 return; | 351 return; |
338 } | 352 } |
339 | 353 |
340 if (requested_from_service_worker && | 354 if (requested_from_service_worker && |
341 !sw_registration->active_version()->HasWindowClients()) { | 355 !sw_registration->active_version()->HasWindowClients()) { |
342 base::ThreadTaskRunnerHandle::Get()->PostTask( | 356 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback); |
343 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_ALLOWED, | |
344 BackgroundSyncRegistration())); | |
345 return; | 357 return; |
346 } | 358 } |
347 | 359 |
348 BackgroundSyncRegistration* existing_registration = | 360 RefCountedRegistration* existing_registration_ref = |
349 LookupRegistration(sw_registration_id, RegistrationKey(options)); | 361 LookupActiveRegistration(sw_registration_id, RegistrationKey(options)); |
350 if (existing_registration && | 362 if (existing_registration_ref && |
351 existing_registration->options()->Equals(options)) { | 363 existing_registration_ref->value()->options()->Equals(options)) { |
| 364 BackgroundSyncRegistration* existing_registration = |
| 365 existing_registration_ref->value(); |
352 if (existing_registration->sync_state() == SYNC_STATE_FAILED) { | 366 if (existing_registration->sync_state() == SYNC_STATE_FAILED) { |
353 existing_registration->set_sync_state(SYNC_STATE_PENDING); | 367 existing_registration->set_sync_state(SYNC_STATE_PENDING); |
354 StoreRegistrations( | 368 StoreRegistrations( |
355 sw_registration_id, | 369 sw_registration_id, |
356 base::Bind(&BackgroundSyncManager::RegisterDidStore, | 370 base::Bind(&BackgroundSyncManager::RegisterDidStore, |
357 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 371 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
358 *existing_registration, callback)); | 372 make_scoped_refptr(existing_registration_ref), callback)); |
359 return; | 373 return; |
360 } | 374 } |
361 | 375 |
362 // Record the duplicated registration | 376 // Record the duplicated registration |
363 BackgroundSyncMetrics::CountRegister( | 377 BackgroundSyncMetrics::CountRegister( |
364 existing_registration->options()->periodicity, registration_could_fire, | 378 existing_registration->options()->periodicity, registration_could_fire, |
365 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE, | 379 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE, |
366 BACKGROUND_SYNC_STATUS_OK); | 380 BACKGROUND_SYNC_STATUS_OK); |
367 | 381 |
368 base::ThreadTaskRunnerHandle::Get()->PostTask( | 382 base::ThreadTaskRunnerHandle::Get()->PostTask( |
369 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, | 383 FROM_HERE, |
370 *existing_registration)); | 384 base::Bind( |
| 385 callback, BACKGROUND_SYNC_STATUS_OK, |
| 386 base::Passed( |
| 387 CreateRegistrationHandle(existing_registration_ref).Pass()))); |
371 return; | 388 return; |
372 } | 389 } |
373 | 390 |
374 BackgroundSyncRegistration new_registration; | 391 scoped_refptr<RefCountedRegistration> new_ref_registration( |
375 *new_registration.options() = options; | 392 new RefCountedRegistration()); |
| 393 BackgroundSyncRegistration* new_registration = new_ref_registration->value(); |
| 394 |
| 395 *new_registration->options() = options; |
376 | 396 |
377 BackgroundSyncRegistrations* registrations = | 397 BackgroundSyncRegistrations* registrations = |
378 &sw_to_registrations_map_[sw_registration_id]; | 398 &active_registrations_[sw_registration_id]; |
379 new_registration.set_id(registrations->next_id++); | 399 new_registration->set_id(registrations->next_id++); |
380 | 400 |
381 AddRegistrationToMap(sw_registration_id, | 401 AddActiveRegistration(sw_registration_id, |
382 sw_registration->pattern().GetOrigin(), | 402 sw_registration->pattern().GetOrigin(), |
383 new_registration); | 403 new_ref_registration); |
384 | 404 |
385 StoreRegistrations( | 405 StoreRegistrations( |
386 sw_registration_id, | 406 sw_registration_id, |
387 base::Bind(&BackgroundSyncManager::RegisterDidStore, | 407 base::Bind(&BackgroundSyncManager::RegisterDidStore, |
388 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 408 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
389 new_registration, callback)); | 409 new_ref_registration, callback)); |
390 } | 410 } |
391 | 411 |
392 void BackgroundSyncManager::DisableAndClearManager( | 412 void BackgroundSyncManager::DisableAndClearManager( |
393 const base::Closure& callback) { | 413 const base::Closure& callback) { |
394 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 414 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
395 | 415 |
396 if (disabled_) { | 416 if (disabled_) { |
397 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 417 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
398 base::Bind(callback)); | 418 base::Bind(callback)); |
399 return; | 419 return; |
400 } | 420 } |
401 | 421 |
402 disabled_ = true; | 422 disabled_ = true; |
403 sw_to_registrations_map_.clear(); | 423 active_registrations_.clear(); |
404 | 424 |
405 // Delete all backend entries. The memory representation of registered syncs | 425 // Delete all backend entries. The memory representation of registered syncs |
406 // may be out of sync with storage (e.g., due to corruption detection on | 426 // may be out of sync with storage (e.g., due to corruption detection on |
407 // loading from storage), so reload the registrations from storage again. | 427 // loading from storage), so reload the registrations from storage again. |
408 GetDataFromBackend( | 428 GetDataFromBackend( |
409 kBackgroundSyncUserDataKey, | 429 kBackgroundSyncUserDataKey, |
410 base::Bind(&BackgroundSyncManager::DisableAndClearDidGetRegistrations, | 430 base::Bind(&BackgroundSyncManager::DisableAndClearDidGetRegistrations, |
411 weak_ptr_factory_.GetWeakPtr(), callback)); | 431 weak_ptr_factory_.GetWeakPtr(), callback)); |
412 } | 432 } |
413 | 433 |
(...skipping 23 matching lines...) Expand all Loading... |
437 void BackgroundSyncManager::DisableAndClearManagerClearedOne( | 457 void BackgroundSyncManager::DisableAndClearManagerClearedOne( |
438 const base::Closure& barrier_closure, | 458 const base::Closure& barrier_closure, |
439 ServiceWorkerStatusCode status) { | 459 ServiceWorkerStatusCode status) { |
440 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 460 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
441 | 461 |
442 // The status doesn't matter at this point, there is nothing else to be done. | 462 // The status doesn't matter at this point, there is nothing else to be done. |
443 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 463 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
444 base::Bind(barrier_closure)); | 464 base::Bind(barrier_closure)); |
445 } | 465 } |
446 | 466 |
447 BackgroundSyncRegistration* BackgroundSyncManager::LookupRegistration( | 467 BackgroundSyncManager::RefCountedRegistration* |
| 468 BackgroundSyncManager::LookupActiveRegistration( |
448 int64 sw_registration_id, | 469 int64 sw_registration_id, |
449 const RegistrationKey& registration_key) { | 470 const RegistrationKey& registration_key) { |
450 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 471 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
451 | 472 |
452 SWIdToRegistrationsMap::iterator it = | 473 SWIdToRegistrationsMap::iterator it = |
453 sw_to_registrations_map_.find(sw_registration_id); | 474 active_registrations_.find(sw_registration_id); |
454 if (it == sw_to_registrations_map_.end()) | 475 if (it == active_registrations_.end()) |
455 return nullptr; | 476 return nullptr; |
456 | 477 |
457 BackgroundSyncRegistrations& registrations = it->second; | 478 BackgroundSyncRegistrations& registrations = it->second; |
458 DCHECK_LE(BackgroundSyncRegistration::kInitialId, registrations.next_id); | 479 DCHECK_LE(BackgroundSyncRegistration::kInitialId, registrations.next_id); |
459 DCHECK(!registrations.origin.is_empty()); | 480 DCHECK(!registrations.origin.is_empty()); |
460 | 481 |
461 auto key_and_registration_iter = | 482 auto key_and_registration_iter = |
462 registrations.registration_map.find(registration_key); | 483 registrations.registration_map.find(registration_key); |
463 if (key_and_registration_iter == registrations.registration_map.end()) | 484 if (key_and_registration_iter == registrations.registration_map.end()) |
464 return nullptr; | 485 return nullptr; |
465 | 486 |
466 return &key_and_registration_iter->second; | 487 return key_and_registration_iter->second.get(); |
467 } | 488 } |
468 | 489 |
469 void BackgroundSyncManager::StoreRegistrations( | 490 void BackgroundSyncManager::StoreRegistrations( |
470 int64 sw_registration_id, | 491 int64 sw_registration_id, |
471 const ServiceWorkerStorage::StatusCallback& callback) { | 492 const ServiceWorkerStorage::StatusCallback& callback) { |
472 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 493 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
473 | 494 |
474 // Serialize the data. | 495 // Serialize the data. |
475 const BackgroundSyncRegistrations& registrations = | 496 const BackgroundSyncRegistrations& registrations = |
476 sw_to_registrations_map_[sw_registration_id]; | 497 active_registrations_[sw_registration_id]; |
477 BackgroundSyncRegistrationsProto registrations_proto; | 498 BackgroundSyncRegistrationsProto registrations_proto; |
478 registrations_proto.set_next_registration_id(registrations.next_id); | 499 registrations_proto.set_next_registration_id(registrations.next_id); |
479 registrations_proto.set_origin(registrations.origin.spec()); | 500 registrations_proto.set_origin(registrations.origin.spec()); |
480 | 501 |
481 for (const auto& key_and_registration : registrations.registration_map) { | 502 for (const auto& key_and_registration : registrations.registration_map) { |
482 const BackgroundSyncRegistration& registration = | 503 const BackgroundSyncRegistration& registration = |
483 key_and_registration.second; | 504 *key_and_registration.second->value(); |
484 BackgroundSyncRegistrationProto* registration_proto = | 505 BackgroundSyncRegistrationProto* registration_proto = |
485 registrations_proto.add_registration(); | 506 registrations_proto.add_registration(); |
486 registration_proto->set_id(registration.id()); | 507 registration_proto->set_id(registration.id()); |
487 registration_proto->set_sync_state(registration.sync_state()); | 508 registration_proto->set_sync_state(registration.sync_state()); |
488 registration_proto->set_tag(registration.options()->tag); | 509 registration_proto->set_tag(registration.options()->tag); |
489 registration_proto->set_periodicity(registration.options()->periodicity); | 510 registration_proto->set_periodicity(registration.options()->periodicity); |
490 registration_proto->set_min_period(registration.options()->min_period); | 511 registration_proto->set_min_period(registration.options()->min_period); |
491 registration_proto->set_network_state( | 512 registration_proto->set_network_state( |
492 registration.options()->network_state); | 513 registration.options()->network_state); |
493 registration_proto->set_power_state(registration.options()->power_state); | 514 registration_proto->set_power_state(registration.options()->power_state); |
494 } | 515 } |
495 std::string serialized; | 516 std::string serialized; |
496 bool success = registrations_proto.SerializeToString(&serialized); | 517 bool success = registrations_proto.SerializeToString(&serialized); |
497 DCHECK(success); | 518 DCHECK(success); |
498 | 519 |
499 StoreDataInBackend(sw_registration_id, registrations.origin, | 520 StoreDataInBackend(sw_registration_id, registrations.origin, |
500 kBackgroundSyncUserDataKey, serialized, callback); | 521 kBackgroundSyncUserDataKey, serialized, callback); |
501 } | 522 } |
502 | 523 |
503 void BackgroundSyncManager::RegisterDidStore( | 524 void BackgroundSyncManager::RegisterDidStore( |
504 int64 sw_registration_id, | 525 int64 sw_registration_id, |
505 const BackgroundSyncRegistration& new_registration, | 526 const scoped_refptr<RefCountedRegistration>& new_registration_ref, |
506 const StatusAndRegistrationCallback& callback, | 527 const StatusAndRegistrationCallback& callback, |
507 ServiceWorkerStatusCode status) { | 528 ServiceWorkerStatusCode status) { |
508 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 529 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
509 | 530 |
| 531 const BackgroundSyncRegistration* new_registration = |
| 532 new_registration_ref->value(); |
| 533 |
510 // For UMA, determine here whether the sync could fire immediately | 534 // For UMA, determine here whether the sync could fire immediately |
511 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = | 535 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
512 AreOptionConditionsMet(*new_registration.options()) | 536 AreOptionConditionsMet(*new_registration->options()) |
513 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE | 537 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
514 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; | 538 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
515 | 539 |
516 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 540 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
517 // The service worker registration is gone. | 541 // The service worker registration is gone. |
518 BackgroundSyncMetrics::CountRegister( | 542 BackgroundSyncMetrics::CountRegister( |
519 new_registration.options()->periodicity, registration_could_fire, | 543 new_registration->options()->periodicity, registration_could_fire, |
520 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | 544 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
521 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 545 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
522 sw_to_registrations_map_.erase(sw_registration_id); | 546 active_registrations_.erase(sw_registration_id); |
523 base::ThreadTaskRunnerHandle::Get()->PostTask( | 547 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
524 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | |
525 BackgroundSyncRegistration())); | |
526 return; | 548 return; |
527 } | 549 } |
528 | 550 |
529 if (status != SERVICE_WORKER_OK) { | 551 if (status != SERVICE_WORKER_OK) { |
530 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | 552 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
531 "failure."; | 553 "failure."; |
532 BackgroundSyncMetrics::CountRegister( | 554 BackgroundSyncMetrics::CountRegister( |
533 new_registration.options()->periodicity, registration_could_fire, | 555 new_registration->options()->periodicity, registration_could_fire, |
534 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | 556 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
535 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 557 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
536 DisableAndClearManager(base::Bind(callback, | 558 DisableAndClearManager(base::Bind( |
537 BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 559 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
538 BackgroundSyncRegistration())); | 560 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>().Pass()))); |
539 return; | 561 return; |
540 } | 562 } |
541 | 563 |
542 BackgroundSyncMetrics::CountRegister( | 564 BackgroundSyncMetrics::CountRegister( |
543 new_registration.options()->periodicity, registration_could_fire, | 565 new_registration->options()->periodicity, registration_could_fire, |
544 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, | 566 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE, |
545 BACKGROUND_SYNC_STATUS_OK); | 567 BACKGROUND_SYNC_STATUS_OK); |
546 FireReadyEvents(); | 568 FireReadyEvents(); |
547 base::ThreadTaskRunnerHandle::Get()->PostTask( | 569 base::ThreadTaskRunnerHandle::Get()->PostTask( |
548 FROM_HERE, | 570 FROM_HERE, |
549 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, new_registration)); | 571 base::Bind( |
| 572 callback, BACKGROUND_SYNC_STATUS_OK, |
| 573 base::Passed( |
| 574 CreateRegistrationHandle(new_registration_ref.get()).Pass()))); |
550 } | 575 } |
551 | 576 |
552 void BackgroundSyncManager::RemoveRegistrationFromMap( | 577 void BackgroundSyncManager::RemoveActiveRegistration( |
553 int64 sw_registration_id, | 578 int64 sw_registration_id, |
554 const RegistrationKey& registration_key) { | 579 const RegistrationKey& registration_key) { |
555 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 580 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
556 DCHECK(LookupRegistration(sw_registration_id, registration_key)); | 581 DCHECK(LookupActiveRegistration(sw_registration_id, registration_key)); |
557 | 582 |
558 BackgroundSyncRegistrations* registrations = | 583 BackgroundSyncRegistrations* registrations = |
559 &sw_to_registrations_map_[sw_registration_id]; | 584 &active_registrations_[sw_registration_id]; |
560 | 585 |
561 registrations->registration_map.erase(registration_key); | 586 registrations->registration_map.erase(registration_key); |
562 } | 587 } |
563 | 588 |
564 void BackgroundSyncManager::AddRegistrationToMap( | 589 void BackgroundSyncManager::AddActiveRegistration( |
565 int64 sw_registration_id, | 590 int64 sw_registration_id, |
566 const GURL& origin, | 591 const GURL& origin, |
567 const BackgroundSyncRegistration& sync_registration) { | 592 const scoped_refptr<RefCountedRegistration>& sync_registration) { |
568 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 593 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
569 DCHECK(sync_registration.IsValid()); | 594 DCHECK(sync_registration->value()->IsValid()); |
570 | 595 |
571 BackgroundSyncRegistrations* registrations = | 596 BackgroundSyncRegistrations* registrations = |
572 &sw_to_registrations_map_[sw_registration_id]; | 597 &active_registrations_[sw_registration_id]; |
573 registrations->origin = origin; | 598 registrations->origin = origin; |
574 | 599 |
575 RegistrationKey registration_key(sync_registration); | 600 RegistrationKey registration_key(*sync_registration->value()); |
576 registrations->registration_map[registration_key] = sync_registration; | 601 registrations->registration_map[registration_key] = sync_registration; |
577 } | 602 } |
578 | 603 |
579 void BackgroundSyncManager::StoreDataInBackend( | 604 void BackgroundSyncManager::StoreDataInBackend( |
580 int64 sw_registration_id, | 605 int64 sw_registration_id, |
581 const GURL& origin, | 606 const GURL& origin, |
582 const std::string& backend_key, | 607 const std::string& backend_key, |
583 const std::string& data, | 608 const std::string& data, |
584 const ServiceWorkerStorage::StatusCallback& callback) { | 609 const ServiceWorkerStorage::StatusCallback& callback) { |
585 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 610 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
586 | 611 |
587 service_worker_context_->StoreRegistrationUserData( | 612 service_worker_context_->StoreRegistrationUserData( |
588 sw_registration_id, origin, backend_key, data, callback); | 613 sw_registration_id, origin, backend_key, data, callback); |
589 } | 614 } |
590 | 615 |
591 void BackgroundSyncManager::GetDataFromBackend( | 616 void BackgroundSyncManager::GetDataFromBackend( |
592 const std::string& backend_key, | 617 const std::string& backend_key, |
593 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& | 618 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& |
594 callback) { | 619 callback) { |
595 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 620 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
596 | 621 |
597 service_worker_context_->GetUserDataForAllRegistrations(backend_key, | 622 service_worker_context_->GetUserDataForAllRegistrations(backend_key, |
598 callback); | 623 callback); |
599 } | 624 } |
600 | 625 |
601 void BackgroundSyncManager::FireOneShotSync( | 626 void BackgroundSyncManager::FireOneShotSync( |
602 const BackgroundSyncRegistration& registration, | 627 BackgroundSyncRegistrationHandle::HandleId handle_id, |
603 const scoped_refptr<ServiceWorkerVersion>& active_version, | 628 const scoped_refptr<ServiceWorkerVersion>& active_version, |
604 const ServiceWorkerVersion::StatusCallback& callback) { | 629 const ServiceWorkerVersion::StatusCallback& callback) { |
605 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 630 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 631 DCHECK(active_version); |
606 | 632 |
607 active_version->DispatchSyncEvent( | 633 // The ServiceWorkerVersion doesn't know when the client (javascript) is done |
608 mojo::ConvertTo<SyncRegistrationPtr>(registration), callback); | 634 // with the registration so don't give it a BackgroundSyncRegistrationHandle. |
| 635 // Once the render process gets the handle_id it can create its own handle |
| 636 // (with a new unique handle id). |
| 637 active_version->DispatchSyncEvent(handle_id, callback); |
| 638 } |
| 639 |
| 640 scoped_ptr<BackgroundSyncRegistrationHandle> |
| 641 BackgroundSyncManager::CreateRegistrationHandle( |
| 642 const scoped_refptr<RefCountedRegistration>& registration) { |
| 643 scoped_refptr<RefCountedRegistration>* ptr = |
| 644 new scoped_refptr<RefCountedRegistration>(registration); |
| 645 |
| 646 // Registration handles have unique handle ids. The handle id maps to an |
| 647 // internal RefCountedRegistration (which has the persistent registration id) |
| 648 // via |
| 649 // registration_reference_ids_. |
| 650 BackgroundSyncRegistrationHandle::HandleId handle_id = |
| 651 registration_handle_ids_.Add(ptr); |
| 652 |
| 653 return make_scoped_ptr(new BackgroundSyncRegistrationHandle( |
| 654 weak_ptr_factory_.GetWeakPtr(), handle_id)); |
| 655 } |
| 656 |
| 657 BackgroundSyncRegistration* BackgroundSyncManager::GetRegistrationForHandle( |
| 658 BackgroundSyncRegistrationHandle::HandleId handle_id) const { |
| 659 scoped_refptr<RefCountedRegistration>* ref_registration = |
| 660 registration_handle_ids_.Lookup(handle_id); |
| 661 if (!ref_registration) |
| 662 return nullptr; |
| 663 return (*ref_registration)->value(); |
| 664 } |
| 665 |
| 666 void BackgroundSyncManager::ReleaseRegistrationHandle( |
| 667 BackgroundSyncRegistrationHandle::HandleId handle_id) { |
| 668 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 669 DCHECK(registration_handle_ids_.Lookup(handle_id)); |
| 670 registration_handle_ids_.Remove(handle_id); |
| 671 } |
| 672 |
| 673 void BackgroundSyncManager::Unregister( |
| 674 int64 sw_registration_id, |
| 675 SyncPeriodicity periodicity, |
| 676 BackgroundSyncRegistrationHandle::HandleId handle_id, |
| 677 const StatusCallback& callback) { |
| 678 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 679 |
| 680 if (disabled_) { |
| 681 BackgroundSyncMetrics::CountUnregister( |
| 682 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
| 683 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 684 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
| 685 return; |
| 686 } |
| 687 |
| 688 BackgroundSyncRegistration* registration = |
| 689 GetRegistrationForHandle(handle_id); |
| 690 DCHECK(registration); |
| 691 |
| 692 op_scheduler_.ScheduleOperation(base::Bind( |
| 693 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), |
| 694 sw_registration_id, RegistrationKey(*registration), registration->id(), |
| 695 periodicity, MakeStatusCompletion(callback))); |
609 } | 696 } |
610 | 697 |
611 void BackgroundSyncManager::UnregisterImpl( | 698 void BackgroundSyncManager::UnregisterImpl( |
612 int64 sw_registration_id, | 699 int64 sw_registration_id, |
613 const RegistrationKey& registration_key, | 700 const RegistrationKey& registration_key, |
614 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 701 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
615 SyncPeriodicity periodicity, | 702 SyncPeriodicity periodicity, |
616 const StatusCallback& callback) { | 703 const StatusCallback& callback) { |
617 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 704 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
618 | 705 |
619 if (disabled_) { | 706 if (disabled_) { |
620 BackgroundSyncMetrics::CountUnregister( | 707 BackgroundSyncMetrics::CountUnregister( |
621 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 708 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
622 base::ThreadTaskRunnerHandle::Get()->PostTask( | 709 base::ThreadTaskRunnerHandle::Get()->PostTask( |
623 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | 710 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
624 return; | 711 return; |
625 } | 712 } |
626 | 713 |
627 const BackgroundSyncRegistration* existing_registration = | 714 const RefCountedRegistration* existing_registration = |
628 LookupRegistration(sw_registration_id, registration_key); | 715 LookupActiveRegistration(sw_registration_id, registration_key); |
| 716 |
629 if (!existing_registration || | 717 if (!existing_registration || |
630 existing_registration->id() != sync_registration_id) { | 718 existing_registration->value()->id() != sync_registration_id) { |
631 BackgroundSyncMetrics::CountUnregister(periodicity, | 719 BackgroundSyncMetrics::CountUnregister(periodicity, |
632 BACKGROUND_SYNC_STATUS_NOT_FOUND); | 720 BACKGROUND_SYNC_STATUS_NOT_FOUND); |
633 base::ThreadTaskRunnerHandle::Get()->PostTask( | 721 base::ThreadTaskRunnerHandle::Get()->PostTask( |
634 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND)); | 722 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND)); |
635 return; | 723 return; |
636 } | 724 } |
637 | 725 |
638 RemoveRegistrationFromMap(sw_registration_id, registration_key); | 726 RemoveActiveRegistration(sw_registration_id, registration_key); |
639 | 727 |
640 StoreRegistrations(sw_registration_id, | 728 StoreRegistrations(sw_registration_id, |
641 base::Bind(&BackgroundSyncManager::UnregisterDidStore, | 729 base::Bind(&BackgroundSyncManager::UnregisterDidStore, |
642 weak_ptr_factory_.GetWeakPtr(), | 730 weak_ptr_factory_.GetWeakPtr(), |
643 sw_registration_id, periodicity, callback)); | 731 sw_registration_id, periodicity, callback)); |
644 } | 732 } |
645 | 733 |
646 void BackgroundSyncManager::UnregisterDidStore(int64 sw_registration_id, | 734 void BackgroundSyncManager::UnregisterDidStore(int64 sw_registration_id, |
647 SyncPeriodicity periodicity, | 735 SyncPeriodicity periodicity, |
648 const StatusCallback& callback, | 736 const StatusCallback& callback, |
649 ServiceWorkerStatusCode status) { | 737 ServiceWorkerStatusCode status) { |
650 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 738 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
651 | 739 |
652 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 740 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
653 // ServiceWorker was unregistered. | 741 // ServiceWorker was unregistered. |
654 BackgroundSyncMetrics::CountUnregister( | 742 BackgroundSyncMetrics::CountUnregister( |
655 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 743 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
656 sw_to_registrations_map_.erase(sw_registration_id); | 744 active_registrations_.erase(sw_registration_id); |
657 base::ThreadTaskRunnerHandle::Get()->PostTask( | 745 base::ThreadTaskRunnerHandle::Get()->PostTask( |
658 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | 746 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
659 return; | 747 return; |
660 } | 748 } |
661 | 749 |
662 if (status != SERVICE_WORKER_OK) { | 750 if (status != SERVICE_WORKER_OK) { |
663 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; | 751 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; |
664 BackgroundSyncMetrics::CountUnregister( | 752 BackgroundSyncMetrics::CountUnregister( |
665 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 753 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
666 DisableAndClearManager( | 754 DisableAndClearManager( |
667 base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | 755 base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
668 return; | 756 return; |
669 } | 757 } |
670 | 758 |
671 BackgroundSyncMetrics::CountUnregister(periodicity, | 759 BackgroundSyncMetrics::CountUnregister(periodicity, |
672 BACKGROUND_SYNC_STATUS_OK); | 760 BACKGROUND_SYNC_STATUS_OK); |
673 base::ThreadTaskRunnerHandle::Get()->PostTask( | 761 base::ThreadTaskRunnerHandle::Get()->PostTask( |
674 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK)); | 762 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK)); |
675 } | 763 } |
676 | 764 |
677 void BackgroundSyncManager::GetRegistrationImpl( | 765 void BackgroundSyncManager::GetRegistrationImpl( |
678 int64 sw_registration_id, | 766 int64 sw_registration_id, |
679 const RegistrationKey& registration_key, | 767 const RegistrationKey& registration_key, |
680 const StatusAndRegistrationCallback& callback) { | 768 const StatusAndRegistrationCallback& callback) { |
681 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 769 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
682 | 770 |
683 if (disabled_) { | 771 if (disabled_) { |
684 base::ThreadTaskRunnerHandle::Get()->PostTask( | 772 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
685 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | |
686 BackgroundSyncRegistration())); | |
687 return; | 773 return; |
688 } | 774 } |
689 | 775 |
690 const BackgroundSyncRegistration* out_registration = | 776 RefCountedRegistration* registration = |
691 LookupRegistration(sw_registration_id, registration_key); | 777 LookupActiveRegistration(sw_registration_id, registration_key); |
692 if (!out_registration) { | 778 if (!registration) { |
693 base::ThreadTaskRunnerHandle::Get()->PostTask( | 779 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_FOUND, callback); |
694 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND, | |
695 BackgroundSyncRegistration())); | |
696 return; | 780 return; |
697 } | 781 } |
698 | 782 |
699 base::ThreadTaskRunnerHandle::Get()->PostTask( | 783 base::ThreadTaskRunnerHandle::Get()->PostTask( |
700 FROM_HERE, | 784 FROM_HERE, |
701 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, *out_registration)); | 785 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, |
| 786 base::Passed(CreateRegistrationHandle(registration).Pass()))); |
702 } | 787 } |
703 | 788 |
704 void BackgroundSyncManager::GetRegistrationsImpl( | 789 void BackgroundSyncManager::GetRegistrationsImpl( |
705 int64 sw_registration_id, | 790 int64 sw_registration_id, |
706 SyncPeriodicity periodicity, | 791 SyncPeriodicity periodicity, |
707 const StatusAndRegistrationsCallback& callback) { | 792 const StatusAndRegistrationsCallback& callback) { |
708 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 793 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
709 | 794 |
710 std::vector<BackgroundSyncRegistration> out_registrations; | 795 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> out_registrations( |
| 796 new ScopedVector<BackgroundSyncRegistrationHandle>()); |
711 | 797 |
712 if (disabled_) { | 798 if (disabled_) { |
713 base::ThreadTaskRunnerHandle::Get()->PostTask( | 799 base::ThreadTaskRunnerHandle::Get()->PostTask( |
714 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 800 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
715 out_registrations)); | 801 base::Passed(out_registrations.Pass()))); |
716 return; | 802 return; |
717 } | 803 } |
718 | 804 |
719 SWIdToRegistrationsMap::iterator it = | 805 SWIdToRegistrationsMap::iterator it = |
720 sw_to_registrations_map_.find(sw_registration_id); | 806 active_registrations_.find(sw_registration_id); |
721 | 807 |
722 if (it != sw_to_registrations_map_.end()) { | 808 if (it != active_registrations_.end()) { |
723 const BackgroundSyncRegistrations& registrations = it->second; | 809 const BackgroundSyncRegistrations& registrations = it->second; |
724 for (const auto& tag_and_registration : registrations.registration_map) { | 810 for (const auto& tag_and_registration : registrations.registration_map) { |
725 const BackgroundSyncRegistration& registration = | 811 RefCountedRegistration* registration = tag_and_registration.second.get(); |
726 tag_and_registration.second; | 812 if (registration->value()->options()->periodicity == periodicity) { |
727 if (registration.options()->periodicity == periodicity) | 813 out_registrations->push_back( |
728 out_registrations.push_back(registration); | 814 CreateRegistrationHandle(registration).release()); |
| 815 } |
729 } | 816 } |
730 } | 817 } |
731 | 818 |
732 base::ThreadTaskRunnerHandle::Get()->PostTask( | 819 base::ThreadTaskRunnerHandle::Get()->PostTask( |
733 FROM_HERE, | 820 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, |
734 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, out_registrations)); | 821 base::Passed(out_registrations.Pass()))); |
735 } | 822 } |
736 | 823 |
737 bool BackgroundSyncManager::AreOptionConditionsMet( | 824 bool BackgroundSyncManager::AreOptionConditionsMet( |
738 const BackgroundSyncRegistrationOptions& options) { | 825 const BackgroundSyncRegistrationOptions& options) { |
739 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 826 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
740 return network_observer_->NetworkSufficient(options.network_state) && | 827 return network_observer_->NetworkSufficient(options.network_state) && |
741 power_observer_->PowerSufficient(options.power_state); | 828 power_observer_->PowerSufficient(options.power_state); |
742 } | 829 } |
743 | 830 |
744 bool BackgroundSyncManager::IsRegistrationReadyToFire( | 831 bool BackgroundSyncManager::IsRegistrationReadyToFire( |
745 const BackgroundSyncRegistration& registration) { | 832 const BackgroundSyncRegistration& registration) { |
746 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 833 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
747 | 834 |
748 // TODO(jkarlin): Add support for firing periodic registrations. | 835 // TODO(jkarlin): Add support for firing periodic registrations. |
749 if (registration.options()->periodicity == SYNC_PERIODIC) | 836 if (registration.options()->periodicity == SYNC_PERIODIC) |
750 return false; | 837 return false; |
751 | 838 |
752 if (registration.sync_state() != SYNC_STATE_PENDING) | 839 if (registration.sync_state() != SYNC_STATE_PENDING) |
753 return false; | 840 return false; |
754 | 841 |
755 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); | 842 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); |
756 | 843 |
757 return AreOptionConditionsMet(*registration.options()); | 844 return AreOptionConditionsMet(*registration.options()); |
758 } | 845 } |
759 | 846 |
760 void BackgroundSyncManager::SchedulePendingRegistrations() { | 847 void BackgroundSyncManager::SchedulePendingRegistrations() { |
761 #if defined(OS_ANDROID) | 848 #if defined(OS_ANDROID) |
762 bool keep_browser_alive_for_one_shot = false; | 849 bool keep_browser_alive_for_one_shot = false; |
763 | 850 |
764 for (const auto& sw_id_and_registrations : sw_to_registrations_map_) { | 851 for (const auto& sw_id_and_registrations : active_registrations_) { |
765 for (const auto& key_and_registration : | 852 for (const auto& key_and_registration : |
766 sw_id_and_registrations.second.registration_map) { | 853 sw_id_and_registrations.second.registration_map) { |
767 const BackgroundSyncRegistration& registration = | 854 const BackgroundSyncRegistration& registration = |
768 key_and_registration.second; | 855 *key_and_registration.second->value(); |
769 if (registration.sync_state() == SYNC_STATE_PENDING) { | 856 if (registration.sync_state() == SYNC_STATE_PENDING) { |
770 if (registration.options()->periodicity == SYNC_ONE_SHOT) { | 857 if (registration.options()->periodicity == SYNC_ONE_SHOT) { |
771 keep_browser_alive_for_one_shot = true; | 858 keep_browser_alive_for_one_shot = true; |
772 } else { | 859 } else { |
773 // TODO(jkarlin): Support keeping the browser alive for periodic | 860 // TODO(jkarlin): Support keeping the browser alive for periodic |
774 // syncs. | 861 // syncs. |
775 } | 862 } |
776 } | 863 } |
777 } | 864 } |
778 } | 865 } |
(...skipping 25 matching lines...) Expand all Loading... |
804 | 891 |
805 if (disabled_) { | 892 if (disabled_) { |
806 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 893 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
807 base::Bind(callback)); | 894 base::Bind(callback)); |
808 return; | 895 return; |
809 } | 896 } |
810 | 897 |
811 // Find the registrations that are ready to run. | 898 // Find the registrations that are ready to run. |
812 std::vector<std::pair<int64, RegistrationKey>> sw_id_and_keys_to_fire; | 899 std::vector<std::pair<int64, RegistrationKey>> sw_id_and_keys_to_fire; |
813 | 900 |
814 for (auto& sw_id_and_registrations : sw_to_registrations_map_) { | 901 for (auto& sw_id_and_registrations : active_registrations_) { |
815 const int64 service_worker_id = sw_id_and_registrations.first; | 902 const int64 service_worker_id = sw_id_and_registrations.first; |
816 for (auto& key_and_registration : | 903 for (auto& key_and_registration : |
817 sw_id_and_registrations.second.registration_map) { | 904 sw_id_and_registrations.second.registration_map) { |
818 BackgroundSyncRegistration* registration = &key_and_registration.second; | 905 BackgroundSyncRegistration* registration = |
| 906 key_and_registration.second->value(); |
819 if (IsRegistrationReadyToFire(*registration)) { | 907 if (IsRegistrationReadyToFire(*registration)) { |
820 sw_id_and_keys_to_fire.push_back( | 908 sw_id_and_keys_to_fire.push_back( |
821 std::make_pair(service_worker_id, key_and_registration.first)); | 909 std::make_pair(service_worker_id, key_and_registration.first)); |
822 // The state change is not saved to persistent storage because | 910 // The state change is not saved to persistent storage because |
823 // if the sync event is killed mid-sync then it should return to | 911 // if the sync event is killed mid-sync then it should return to |
824 // SYNC_STATE_PENDING. | 912 // SYNC_STATE_PENDING. |
825 registration->set_sync_state(SYNC_STATE_FIRING); | 913 registration->set_sync_state(SYNC_STATE_FIRING); |
826 } | 914 } |
827 } | 915 } |
828 } | 916 } |
(...skipping 12 matching lines...) Expand all Loading... |
841 sw_id_and_keys_to_fire.size(), base::Bind(callback)); | 929 sw_id_and_keys_to_fire.size(), base::Bind(callback)); |
842 | 930 |
843 // Record the total time taken after all events have run to completion. | 931 // Record the total time taken after all events have run to completion. |
844 base::Closure events_completed_barrier_closure = | 932 base::Closure events_completed_barrier_closure = |
845 base::BarrierClosure(sw_id_and_keys_to_fire.size(), | 933 base::BarrierClosure(sw_id_and_keys_to_fire.size(), |
846 base::Bind(&OnAllSyncEventsCompleted, start_time, | 934 base::Bind(&OnAllSyncEventsCompleted, start_time, |
847 sw_id_and_keys_to_fire.size())); | 935 sw_id_and_keys_to_fire.size())); |
848 | 936 |
849 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) { | 937 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) { |
850 int64 service_worker_id = sw_id_and_key.first; | 938 int64 service_worker_id = sw_id_and_key.first; |
851 const BackgroundSyncRegistration* registration = | 939 const RefCountedRegistration* registration = |
852 LookupRegistration(service_worker_id, sw_id_and_key.second); | 940 LookupActiveRegistration(service_worker_id, sw_id_and_key.second); |
| 941 DCHECK(registration); |
853 | 942 |
854 service_worker_context_->FindRegistrationForId( | 943 service_worker_context_->FindRegistrationForId( |
855 service_worker_id, sw_to_registrations_map_[service_worker_id].origin, | 944 service_worker_id, active_registrations_[service_worker_id].origin, |
856 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration, | 945 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration, |
857 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second, | 946 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second, |
858 registration->id(), events_fired_barrier_closure, | 947 registration->value()->id(), events_fired_barrier_closure, |
859 events_completed_barrier_closure)); | 948 events_completed_barrier_closure)); |
860 } | 949 } |
861 } | 950 } |
862 | 951 |
863 SchedulePendingRegistrations(); | 952 SchedulePendingRegistrations(); |
864 } | 953 } |
865 | 954 |
866 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( | 955 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( |
867 const RegistrationKey& registration_key, | 956 const RegistrationKey& registration_key, |
868 BackgroundSyncRegistration::RegistrationId registration_id, | 957 BackgroundSyncRegistration::RegistrationId registration_id, |
869 const base::Closure& event_fired_callback, | 958 const base::Closure& event_fired_callback, |
870 const base::Closure& event_completed_callback, | 959 const base::Closure& event_completed_callback, |
871 ServiceWorkerStatusCode service_worker_status, | 960 ServiceWorkerStatusCode service_worker_status, |
872 const scoped_refptr<ServiceWorkerRegistration>& | 961 const scoped_refptr<ServiceWorkerRegistration>& |
873 service_worker_registration) { | 962 service_worker_registration) { |
874 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 963 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
875 if (service_worker_status != SERVICE_WORKER_OK) { | 964 if (service_worker_status != SERVICE_WORKER_OK) { |
876 base::ThreadTaskRunnerHandle::Get()->PostTask( | 965 base::ThreadTaskRunnerHandle::Get()->PostTask( |
877 FROM_HERE, base::Bind(event_fired_callback)); | 966 FROM_HERE, base::Bind(event_fired_callback)); |
878 base::ThreadTaskRunnerHandle::Get()->PostTask( | 967 base::ThreadTaskRunnerHandle::Get()->PostTask( |
879 FROM_HERE, base::Bind(event_completed_callback)); | 968 FROM_HERE, base::Bind(event_completed_callback)); |
880 return; | 969 return; |
881 } | 970 } |
882 | 971 |
883 BackgroundSyncRegistration* registration = | 972 RefCountedRegistration* registration = LookupActiveRegistration( |
884 LookupRegistration(service_worker_registration->id(), registration_key); | 973 service_worker_registration->id(), registration_key); |
| 974 DCHECK(registration); |
885 | 975 |
| 976 // Create a handle and keep it until the sync event completes. The client can |
| 977 // acquire its own handle for longer-term use. |
| 978 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = |
| 979 CreateRegistrationHandle(registration); |
| 980 |
| 981 BackgroundSyncRegistrationHandle::HandleId handle_id = |
| 982 registration_handle->handle_id(); |
886 FireOneShotSync( | 983 FireOneShotSync( |
887 *registration, service_worker_registration->active_version(), | 984 handle_id, service_worker_registration->active_version(), |
888 base::Bind(&BackgroundSyncManager::EventComplete, | 985 base::Bind( |
889 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, | 986 &BackgroundSyncManager::EventComplete, weak_ptr_factory_.GetWeakPtr(), |
890 service_worker_registration->id(), registration_key, | 987 service_worker_registration, service_worker_registration->id(), |
891 registration_id, event_completed_callback)); | 988 base::Passed(registration_handle.Pass()), event_completed_callback)); |
892 | 989 |
893 base::ThreadTaskRunnerHandle::Get()->PostTask( | 990 base::ThreadTaskRunnerHandle::Get()->PostTask( |
894 FROM_HERE, base::Bind(event_fired_callback)); | 991 FROM_HERE, base::Bind(event_fired_callback)); |
895 } | 992 } |
896 | 993 |
897 // |service_worker_registration| is just to keep the registration alive | 994 // |service_worker_registration| is just to keep the registration alive |
898 // while the event is firing. | 995 // while the event is firing. |
899 void BackgroundSyncManager::EventComplete( | 996 void BackgroundSyncManager::EventComplete( |
900 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, | 997 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, |
901 int64 service_worker_id, | 998 int64 service_worker_id, |
902 const RegistrationKey& key, | 999 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, |
903 BackgroundSyncRegistration::RegistrationId sync_registration_id, | |
904 const base::Closure& callback, | 1000 const base::Closure& callback, |
905 ServiceWorkerStatusCode status_code) { | 1001 ServiceWorkerStatusCode status_code) { |
906 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1002 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
907 if (disabled_) | 1003 if (disabled_) |
908 return; | 1004 return; |
909 | 1005 |
910 op_scheduler_.ScheduleOperation(base::Bind( | 1006 op_scheduler_.ScheduleOperation(base::Bind( |
911 &BackgroundSyncManager::EventCompleteImpl, weak_ptr_factory_.GetWeakPtr(), | 1007 &BackgroundSyncManager::EventCompleteImpl, weak_ptr_factory_.GetWeakPtr(), |
912 service_worker_id, key, sync_registration_id, status_code, | 1008 service_worker_id, base::Passed(registration_handle.Pass()), status_code, |
913 MakeClosureCompletion(callback))); | 1009 MakeClosureCompletion(callback))); |
914 } | 1010 } |
915 | 1011 |
916 void BackgroundSyncManager::EventCompleteImpl( | 1012 void BackgroundSyncManager::EventCompleteImpl( |
917 int64 service_worker_id, | 1013 int64 service_worker_id, |
918 const RegistrationKey& key, | 1014 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, |
919 BackgroundSyncRegistration::RegistrationId sync_registration_id, | |
920 ServiceWorkerStatusCode status_code, | 1015 ServiceWorkerStatusCode status_code, |
921 const base::Closure& callback) { | 1016 const base::Closure& callback) { |
922 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1017 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
923 | 1018 |
924 if (disabled_) { | 1019 if (disabled_) { |
925 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1020 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
926 base::Bind(callback)); | 1021 base::Bind(callback)); |
927 return; | 1022 return; |
928 } | 1023 } |
929 | 1024 |
930 BackgroundSyncRegistration* registration = | 1025 BackgroundSyncRegistration* registration = |
931 LookupRegistration(service_worker_id, key); | 1026 registration_handle->registration(); |
932 if (!registration || registration->id() != sync_registration_id) { | 1027 DCHECK(registration); |
933 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
934 base::Bind(callback)); | |
935 return; | |
936 } | |
937 | 1028 |
938 // The event ran to completion, we should count it, no matter what happens | 1029 // The event ran to completion, we should count it, no matter what happens |
939 // from here. | 1030 // from here. |
940 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, | 1031 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, |
941 status_code == SERVICE_WORKER_OK); | 1032 status_code == SERVICE_WORKER_OK); |
942 | 1033 |
943 if (registration->options()->periodicity == SYNC_ONE_SHOT) { | 1034 if (registration->options()->periodicity == SYNC_ONE_SHOT) { |
944 if (status_code != SERVICE_WORKER_OK) { | 1035 if (status_code != SERVICE_WORKER_OK) { |
945 // TODO(jkarlin) Fire the sync event on the next page load controlled by | 1036 // TODO(jkarlin) Fire the sync event on the next page load controlled by |
946 // this registration. (crbug.com/479665) | 1037 // this registration. (crbug.com/479665) |
947 registration->set_sync_state(SYNC_STATE_FAILED); | 1038 registration->set_sync_state(SYNC_STATE_FAILED); |
948 } else { | 1039 } else { |
949 registration = nullptr; | 1040 RegistrationKey key(*registration); |
950 RemoveRegistrationFromMap(service_worker_id, key); | 1041 // Remove the registration if it's still active. |
| 1042 RefCountedRegistration* active_registration = |
| 1043 LookupActiveRegistration(service_worker_id, key); |
| 1044 if (active_registration && |
| 1045 active_registration->value()->id() == registration->id()) |
| 1046 RemoveActiveRegistration(service_worker_id, key); |
951 } | 1047 } |
952 } else { | 1048 } else { |
953 // TODO(jkarlin): Add support for running periodic syncs. (crbug.com/479674) | 1049 // TODO(jkarlin): Add support for running periodic syncs. (crbug.com/479674) |
954 NOTREACHED(); | 1050 NOTREACHED(); |
955 } | 1051 } |
956 | 1052 |
957 StoreRegistrations( | 1053 StoreRegistrations( |
958 service_worker_id, | 1054 service_worker_id, |
959 base::Bind(&BackgroundSyncManager::EventCompleteDidStore, | 1055 base::Bind(&BackgroundSyncManager::EventCompleteDidStore, |
960 weak_ptr_factory_.GetWeakPtr(), service_worker_id, callback)); | 1056 weak_ptr_factory_.GetWeakPtr(), service_worker_id, callback)); |
961 } | 1057 } |
962 | 1058 |
963 void BackgroundSyncManager::EventCompleteDidStore( | 1059 void BackgroundSyncManager::EventCompleteDidStore( |
964 int64 service_worker_id, | 1060 int64 service_worker_id, |
965 const base::Closure& callback, | 1061 const base::Closure& callback, |
966 ServiceWorkerStatusCode status_code) { | 1062 ServiceWorkerStatusCode status_code) { |
967 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1063 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
968 | 1064 |
969 if (status_code == SERVICE_WORKER_ERROR_NOT_FOUND) { | 1065 if (status_code == SERVICE_WORKER_ERROR_NOT_FOUND) { |
970 // The registration is gone. | 1066 // The registration is gone. |
971 sw_to_registrations_map_.erase(service_worker_id); | 1067 active_registrations_.erase(service_worker_id); |
972 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1068 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
973 base::Bind(callback)); | 1069 base::Bind(callback)); |
974 return; | 1070 return; |
975 } | 1071 } |
976 | 1072 |
977 if (status_code != SERVICE_WORKER_OK) { | 1073 if (status_code != SERVICE_WORKER_OK) { |
978 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | 1074 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
979 "failure."; | 1075 "failure."; |
980 DisableAndClearManager(base::Bind(callback)); | 1076 DisableAndClearManager(base::Bind(callback)); |
981 return; | 1077 return; |
(...skipping 12 matching lines...) Expand all Loading... |
994 base::TimeTicks::Now() - start_time, number_of_batched_sync_events); | 1090 base::TimeTicks::Now() - start_time, number_of_batched_sync_events); |
995 } | 1091 } |
996 | 1092 |
997 void BackgroundSyncManager::OnRegistrationDeletedImpl( | 1093 void BackgroundSyncManager::OnRegistrationDeletedImpl( |
998 int64 registration_id, | 1094 int64 registration_id, |
999 const base::Closure& callback) { | 1095 const base::Closure& callback) { |
1000 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1096 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1001 | 1097 |
1002 // The backend (ServiceWorkerStorage) will delete the data, so just delete the | 1098 // The backend (ServiceWorkerStorage) will delete the data, so just delete the |
1003 // memory representation here. | 1099 // memory representation here. |
1004 sw_to_registrations_map_.erase(registration_id); | 1100 active_registrations_.erase(registration_id); |
1005 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1101 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
1006 base::Bind(callback)); | 1102 base::Bind(callback)); |
1007 } | 1103 } |
1008 | 1104 |
1009 void BackgroundSyncManager::OnStorageWipedImpl(const base::Closure& callback) { | 1105 void BackgroundSyncManager::OnStorageWipedImpl(const base::Closure& callback) { |
1010 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1106 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1011 | 1107 |
1012 sw_to_registrations_map_.clear(); | 1108 active_registrations_.clear(); |
1013 disabled_ = false; | 1109 disabled_ = false; |
1014 InitImpl(callback); | 1110 InitImpl(callback); |
1015 } | 1111 } |
1016 | 1112 |
1017 void BackgroundSyncManager::OnNetworkChanged() { | 1113 void BackgroundSyncManager::OnNetworkChanged() { |
1018 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1114 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1019 | 1115 |
1020 FireReadyEvents(); | 1116 FireReadyEvents(); |
1021 } | 1117 } |
1022 | 1118 |
1023 void BackgroundSyncManager::OnPowerChanged() { | 1119 void BackgroundSyncManager::OnPowerChanged() { |
1024 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1120 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1025 | 1121 |
1026 FireReadyEvents(); | 1122 FireReadyEvents(); |
1027 } | 1123 } |
1028 | 1124 |
| 1125 // TODO(jkarlin): Figure out how to pass scoped_ptrs with this. |
1029 template <typename CallbackT, typename... Params> | 1126 template <typename CallbackT, typename... Params> |
1030 void BackgroundSyncManager::CompleteOperationCallback(const CallbackT& callback, | 1127 void BackgroundSyncManager::CompleteOperationCallback(const CallbackT& callback, |
1031 Params... parameters) { | 1128 Params... parameters) { |
1032 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1129 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1033 | 1130 |
1034 callback.Run(parameters...); | 1131 callback.Run(parameters...); |
1035 op_scheduler_.CompleteOperationAndRunNext(); | 1132 op_scheduler_.CompleteOperationAndRunNext(); |
1036 } | 1133 } |
1037 | 1134 |
| 1135 void BackgroundSyncManager::CompleteStatusAndRegistrationCallback( |
| 1136 StatusAndRegistrationCallback callback, |
| 1137 BackgroundSyncStatus status, |
| 1138 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle) { |
| 1139 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1140 |
| 1141 callback.Run(status, registration_handle.Pass()); |
| 1142 op_scheduler_.CompleteOperationAndRunNext(); |
| 1143 } |
| 1144 |
| 1145 void BackgroundSyncManager::CompleteStatusAndRegistrationsCallback( |
| 1146 StatusAndRegistrationsCallback callback, |
| 1147 BackgroundSyncStatus status, |
| 1148 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> |
| 1149 registration_handles) { |
| 1150 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1151 |
| 1152 callback.Run(status, registration_handles.Pass()); |
| 1153 op_scheduler_.CompleteOperationAndRunNext(); |
| 1154 } |
| 1155 |
1038 base::Closure BackgroundSyncManager::MakeEmptyCompletion() { | 1156 base::Closure BackgroundSyncManager::MakeEmptyCompletion() { |
1039 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1157 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1040 | 1158 |
1041 return MakeClosureCompletion(base::Bind(base::DoNothing)); | 1159 return MakeClosureCompletion(base::Bind(base::DoNothing)); |
1042 } | 1160 } |
1043 | 1161 |
1044 base::Closure BackgroundSyncManager::MakeClosureCompletion( | 1162 base::Closure BackgroundSyncManager::MakeClosureCompletion( |
1045 const base::Closure& callback) { | 1163 const base::Closure& callback) { |
1046 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1164 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1047 | 1165 |
1048 return base::Bind( | 1166 return base::Bind( |
1049 &BackgroundSyncManager::CompleteOperationCallback<base::Closure>, | 1167 &BackgroundSyncManager::CompleteOperationCallback<base::Closure>, |
1050 weak_ptr_factory_.GetWeakPtr(), callback); | 1168 weak_ptr_factory_.GetWeakPtr(), callback); |
1051 } | 1169 } |
1052 | 1170 |
1053 BackgroundSyncManager::StatusAndRegistrationCallback | 1171 BackgroundSyncManager::StatusAndRegistrationCallback |
1054 BackgroundSyncManager::MakeStatusAndRegistrationCompletion( | 1172 BackgroundSyncManager::MakeStatusAndRegistrationCompletion( |
1055 const StatusAndRegistrationCallback& callback) { | 1173 const StatusAndRegistrationCallback& callback) { |
1056 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1174 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1057 | 1175 |
1058 return base::Bind(&BackgroundSyncManager::CompleteOperationCallback< | 1176 return base::Bind( |
1059 StatusAndRegistrationCallback, BackgroundSyncStatus, | 1177 &BackgroundSyncManager::CompleteStatusAndRegistrationCallback, |
1060 const BackgroundSyncRegistration&>, | 1178 weak_ptr_factory_.GetWeakPtr(), callback); |
1061 weak_ptr_factory_.GetWeakPtr(), callback); | |
1062 } | 1179 } |
1063 | 1180 |
1064 BackgroundSyncManager::StatusAndRegistrationsCallback | 1181 BackgroundSyncManager::StatusAndRegistrationsCallback |
1065 BackgroundSyncManager::MakeStatusAndRegistrationsCompletion( | 1182 BackgroundSyncManager::MakeStatusAndRegistrationsCompletion( |
1066 const StatusAndRegistrationsCallback& callback) { | 1183 const StatusAndRegistrationsCallback& callback) { |
1067 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1184 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1068 | 1185 |
1069 return base::Bind(&BackgroundSyncManager::CompleteOperationCallback< | 1186 return base::Bind( |
1070 StatusAndRegistrationsCallback, BackgroundSyncStatus, | 1187 &BackgroundSyncManager::CompleteStatusAndRegistrationsCallback, |
1071 const std::vector<BackgroundSyncRegistration>&>, | 1188 weak_ptr_factory_.GetWeakPtr(), callback); |
1072 weak_ptr_factory_.GetWeakPtr(), callback); | |
1073 } | 1189 } |
1074 | 1190 |
1075 BackgroundSyncManager::StatusCallback | 1191 BackgroundSyncManager::StatusCallback |
1076 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1192 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
1077 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1193 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1078 | 1194 |
1079 return base::Bind( | 1195 return base::Bind( |
1080 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1196 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
1081 BackgroundSyncStatus>, | 1197 BackgroundSyncStatus>, |
1082 weak_ptr_factory_.GetWeakPtr(), callback); | 1198 weak_ptr_factory_.GetWeakPtr(), callback); |
1083 } | 1199 } |
1084 | 1200 |
1085 } // namespace content | 1201 } // namespace content |
OLD | NEW |