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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/barrier_closure.h" | 9 #include "base/barrier_closure.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
156 } | 156 } |
157 | 157 |
158 BackgroundSyncManager::~BackgroundSyncManager() { | 158 BackgroundSyncManager::~BackgroundSyncManager() { |
159 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 159 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
160 | 160 |
161 service_worker_context_->RemoveObserver(this); | 161 service_worker_context_->RemoveObserver(this); |
162 } | 162 } |
163 | 163 |
164 BackgroundSyncManager::RegistrationKey::RegistrationKey( | 164 BackgroundSyncManager::RegistrationKey::RegistrationKey( |
165 const BackgroundSyncRegistration& registration) | 165 const BackgroundSyncRegistration& registration) |
166 : RegistrationKey(registration.options()->tag, | 166 : RegistrationKey(registration.options()->tag) {} |
167 registration.options()->periodicity) { | |
168 } | |
169 | 167 |
170 BackgroundSyncManager::RegistrationKey::RegistrationKey( | 168 BackgroundSyncManager::RegistrationKey::RegistrationKey( |
171 const BackgroundSyncRegistrationOptions& options) | 169 const BackgroundSyncRegistrationOptions& options) |
172 : RegistrationKey(options.tag, options.periodicity) { | 170 : RegistrationKey(options.tag) {} |
173 } | |
174 | 171 |
175 BackgroundSyncManager::RegistrationKey::RegistrationKey( | 172 BackgroundSyncManager::RegistrationKey::RegistrationKey(const std::string& tag) |
176 const std::string& tag, | 173 : value_("o_" + tag) {} |
iclelland
2016/01/22 19:20:41
Should we add a comment here to explain why we don
jkarlin
2016/01/22 19:45:46
Good idea. Done.
| |
177 SyncPeriodicity periodicity) | |
178 : value_(periodicity == SYNC_ONE_SHOT ? "o_" + tag : "p_" + tag) { | |
179 } | |
180 | 174 |
181 void BackgroundSyncManager::Register( | 175 void BackgroundSyncManager::Register( |
182 int64_t sw_registration_id, | 176 int64_t sw_registration_id, |
183 const BackgroundSyncRegistrationOptions& options, | 177 const BackgroundSyncRegistrationOptions& options, |
184 bool requested_from_service_worker, | 178 bool requested_from_service_worker, |
185 const StatusAndRegistrationCallback& callback) { | 179 const StatusAndRegistrationCallback& callback) { |
186 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 180 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
187 | 181 |
188 if (disabled_) { | 182 if (disabled_) { |
189 BackgroundSyncMetrics::CountRegisterFailure( | 183 BackgroundSyncMetrics::CountRegisterFailure( |
190 options.periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 184 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
191 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); | 185 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
192 return; | 186 return; |
193 } | 187 } |
194 | 188 |
195 if (requested_from_service_worker) { | 189 if (requested_from_service_worker) { |
196 op_scheduler_.ScheduleOperation( | 190 op_scheduler_.ScheduleOperation( |
197 base::Bind(&BackgroundSyncManager::RegisterCheckIfHasMainFrame, | 191 base::Bind(&BackgroundSyncManager::RegisterCheckIfHasMainFrame, |
198 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, | 192 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, |
199 MakeStatusAndRegistrationCompletion(callback))); | 193 MakeStatusAndRegistrationCompletion(callback))); |
200 return; | 194 return; |
201 } | 195 } |
202 | 196 |
203 op_scheduler_.ScheduleOperation( | 197 op_scheduler_.ScheduleOperation( |
204 base::Bind(&BackgroundSyncManager::RegisterImpl, | 198 base::Bind(&BackgroundSyncManager::RegisterImpl, |
205 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, | 199 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, |
206 MakeStatusAndRegistrationCompletion(callback))); | 200 MakeStatusAndRegistrationCompletion(callback))); |
207 } | 201 } |
208 | 202 |
209 void BackgroundSyncManager::GetRegistration( | 203 void BackgroundSyncManager::GetRegistration( |
210 int64_t sw_registration_id, | 204 int64_t sw_registration_id, |
211 const std::string& sync_registration_tag, | 205 const std::string& sync_registration_tag, |
212 SyncPeriodicity periodicity, | |
213 const StatusAndRegistrationCallback& callback) { | 206 const StatusAndRegistrationCallback& callback) { |
214 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 207 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
215 | 208 |
216 if (disabled_) { | 209 if (disabled_) { |
217 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); | 210 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
218 return; | 211 return; |
219 } | 212 } |
220 | 213 |
221 RegistrationKey registration_key(sync_registration_tag, periodicity); | 214 RegistrationKey registration_key(sync_registration_tag); |
222 | 215 |
223 op_scheduler_.ScheduleOperation(base::Bind( | 216 op_scheduler_.ScheduleOperation(base::Bind( |
224 &BackgroundSyncManager::GetRegistrationImpl, | 217 &BackgroundSyncManager::GetRegistrationImpl, |
225 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, registration_key, | 218 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, registration_key, |
226 MakeStatusAndRegistrationCompletion(callback))); | 219 MakeStatusAndRegistrationCompletion(callback))); |
227 } | 220 } |
228 | 221 |
229 void BackgroundSyncManager::GetRegistrations( | 222 void BackgroundSyncManager::GetRegistrations( |
230 int64_t sw_registration_id, | 223 int64_t sw_registration_id, |
231 SyncPeriodicity periodicity, | |
232 const StatusAndRegistrationsCallback& callback) { | 224 const StatusAndRegistrationsCallback& callback) { |
233 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 225 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
234 | 226 |
235 if (disabled_) { | 227 if (disabled_) { |
236 base::ThreadTaskRunnerHandle::Get()->PostTask( | 228 base::ThreadTaskRunnerHandle::Get()->PostTask( |
237 FROM_HERE, | 229 FROM_HERE, |
238 base::Bind( | 230 base::Bind( |
239 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 231 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
240 base::Passed( | 232 base::Passed( |
241 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>>( | 233 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>>( |
242 new ScopedVector<BackgroundSyncRegistrationHandle>())))); | 234 new ScopedVector<BackgroundSyncRegistrationHandle>())))); |
243 return; | 235 return; |
244 } | 236 } |
245 | 237 |
246 op_scheduler_.ScheduleOperation( | 238 op_scheduler_.ScheduleOperation( |
247 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl, | 239 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl, |
248 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 240 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
249 periodicity, MakeStatusAndRegistrationsCompletion(callback))); | 241 MakeStatusAndRegistrationsCompletion(callback))); |
250 } | 242 } |
251 | 243 |
252 // Given a HandleId |handle_id|, return a new handle for the same | 244 // Given a HandleId |handle_id|, return a new handle for the same |
253 // registration. | 245 // registration. |
254 scoped_ptr<BackgroundSyncRegistrationHandle> | 246 scoped_ptr<BackgroundSyncRegistrationHandle> |
255 BackgroundSyncManager::DuplicateRegistrationHandle( | 247 BackgroundSyncManager::DuplicateRegistrationHandle( |
256 BackgroundSyncRegistrationHandle::HandleId handle_id) { | 248 BackgroundSyncRegistrationHandle::HandleId handle_id) { |
257 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 249 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
258 | 250 |
259 scoped_refptr<RefCountedRegistration>* ref_registration = | 251 scoped_refptr<RefCountedRegistration>* ref_registration = |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
390 for (int i = 0, max = registrations_proto.registration_size(); i < max; | 382 for (int i = 0, max = registrations_proto.registration_size(); i < max; |
391 ++i) { | 383 ++i) { |
392 const BackgroundSyncRegistrationProto& registration_proto = | 384 const BackgroundSyncRegistrationProto& registration_proto = |
393 registrations_proto.registration(i); | 385 registrations_proto.registration(i); |
394 | 386 |
395 if (registration_proto.id() >= registrations->next_id) { | 387 if (registration_proto.id() >= registrations->next_id) { |
396 corruption_detected = true; | 388 corruption_detected = true; |
397 break; | 389 break; |
398 } | 390 } |
399 | 391 |
400 RegistrationKey registration_key(registration_proto.tag(), | 392 RegistrationKey registration_key(registration_proto.tag()); |
401 registration_proto.periodicity()); | |
402 | 393 |
403 scoped_refptr<RefCountedRegistration> ref_registration( | 394 scoped_refptr<RefCountedRegistration> ref_registration( |
404 new RefCountedRegistration()); | 395 new RefCountedRegistration()); |
405 registrations->registration_map[registration_key] = ref_registration; | 396 registrations->registration_map[registration_key] = ref_registration; |
406 BackgroundSyncRegistration* registration = ref_registration->value(); | 397 BackgroundSyncRegistration* registration = ref_registration->value(); |
407 | 398 |
408 BackgroundSyncRegistrationOptions* options = registration->options(); | 399 BackgroundSyncRegistrationOptions* options = registration->options(); |
409 options->tag = registration_proto.tag(); | 400 options->tag = registration_proto.tag(); |
410 options->periodicity = registration_proto.periodicity(); | |
411 options->min_period = registration_proto.min_period(); | |
412 options->network_state = registration_proto.network_state(); | 401 options->network_state = registration_proto.network_state(); |
413 | 402 |
414 registration->set_id(registration_proto.id()); | 403 registration->set_id(registration_proto.id()); |
415 registration->set_num_attempts(registration_proto.num_attempts()); | 404 registration->set_num_attempts(registration_proto.num_attempts()); |
416 registration->set_delay_until( | 405 registration->set_delay_until( |
417 base::Time::FromInternalValue(registration_proto.delay_until())); | 406 base::Time::FromInternalValue(registration_proto.delay_until())); |
418 } | 407 } |
419 } | 408 } |
420 | 409 |
421 if (corruption_detected) | 410 if (corruption_detected) |
(...skipping 15 matching lines...) Expand all Loading... | |
437 void BackgroundSyncManager::RegisterCheckIfHasMainFrame( | 426 void BackgroundSyncManager::RegisterCheckIfHasMainFrame( |
438 int64_t sw_registration_id, | 427 int64_t sw_registration_id, |
439 const BackgroundSyncRegistrationOptions& options, | 428 const BackgroundSyncRegistrationOptions& options, |
440 const StatusAndRegistrationCallback& callback) { | 429 const StatusAndRegistrationCallback& callback) { |
441 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 430 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
442 | 431 |
443 ServiceWorkerRegistration* sw_registration = | 432 ServiceWorkerRegistration* sw_registration = |
444 service_worker_context_->GetLiveRegistration(sw_registration_id); | 433 service_worker_context_->GetLiveRegistration(sw_registration_id); |
445 if (!sw_registration || !sw_registration->active_version()) { | 434 if (!sw_registration || !sw_registration->active_version()) { |
446 BackgroundSyncMetrics::CountRegisterFailure( | 435 BackgroundSyncMetrics::CountRegisterFailure( |
447 options.periodicity, BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); | 436 BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); |
448 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback); | 437 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback); |
449 return; | 438 return; |
450 } | 439 } |
451 | 440 |
452 HasMainFrameProviderHost( | 441 HasMainFrameProviderHost( |
453 sw_registration->pattern().GetOrigin(), | 442 sw_registration->pattern().GetOrigin(), |
454 base::Bind(&BackgroundSyncManager::RegisterDidCheckIfMainFrame, | 443 base::Bind(&BackgroundSyncManager::RegisterDidCheckIfMainFrame, |
455 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, | 444 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options, |
456 callback)); | 445 callback)); |
457 } | 446 } |
458 | 447 |
459 void BackgroundSyncManager::RegisterDidCheckIfMainFrame( | 448 void BackgroundSyncManager::RegisterDidCheckIfMainFrame( |
460 int64_t sw_registration_id, | 449 int64_t sw_registration_id, |
461 const BackgroundSyncRegistrationOptions& options, | 450 const BackgroundSyncRegistrationOptions& options, |
462 const StatusAndRegistrationCallback& callback, | 451 const StatusAndRegistrationCallback& callback, |
463 bool has_main_frame_client) { | 452 bool has_main_frame_client) { |
464 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 453 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
465 | 454 |
466 if (!has_main_frame_client) { | 455 if (!has_main_frame_client) { |
467 BackgroundSyncMetrics::CountRegisterFailure( | 456 BackgroundSyncMetrics::CountRegisterFailure( |
468 options.periodicity, BACKGROUND_SYNC_STATUS_NOT_ALLOWED); | 457 BACKGROUND_SYNC_STATUS_NOT_ALLOWED); |
469 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback); | 458 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback); |
470 return; | 459 return; |
471 } | 460 } |
472 RegisterImpl(sw_registration_id, options, callback); | 461 RegisterImpl(sw_registration_id, options, callback); |
473 } | 462 } |
474 | 463 |
475 void BackgroundSyncManager::RegisterImpl( | 464 void BackgroundSyncManager::RegisterImpl( |
476 int64_t sw_registration_id, | 465 int64_t sw_registration_id, |
477 const BackgroundSyncRegistrationOptions& options, | 466 const BackgroundSyncRegistrationOptions& options, |
478 const StatusAndRegistrationCallback& callback) { | 467 const StatusAndRegistrationCallback& callback) { |
479 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 468 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
480 | 469 |
481 if (disabled_) { | 470 if (disabled_) { |
482 BackgroundSyncMetrics::CountRegisterFailure( | 471 BackgroundSyncMetrics::CountRegisterFailure( |
483 options.periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 472 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
484 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); | 473 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
485 return; | 474 return; |
486 } | 475 } |
487 | 476 |
488 if (options.tag.length() > kMaxTagLength) { | 477 if (options.tag.length() > kMaxTagLength) { |
489 BackgroundSyncMetrics::CountRegisterFailure( | 478 BackgroundSyncMetrics::CountRegisterFailure( |
490 options.periodicity, BACKGROUND_SYNC_STATUS_NOT_ALLOWED); | 479 BACKGROUND_SYNC_STATUS_NOT_ALLOWED); |
491 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback); | 480 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback); |
492 return; | 481 return; |
493 } | 482 } |
494 | 483 |
495 ServiceWorkerRegistration* sw_registration = | 484 ServiceWorkerRegistration* sw_registration = |
496 service_worker_context_->GetLiveRegistration(sw_registration_id); | 485 service_worker_context_->GetLiveRegistration(sw_registration_id); |
497 if (!sw_registration || !sw_registration->active_version()) { | 486 if (!sw_registration || !sw_registration->active_version()) { |
498 BackgroundSyncMetrics::CountRegisterFailure( | 487 BackgroundSyncMetrics::CountRegisterFailure( |
499 options.periodicity, BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); | 488 BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); |
500 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback); | 489 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback); |
501 return; | 490 return; |
502 } | 491 } |
503 | 492 |
504 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 493 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
505 base::Bind(&NotifyBackgroundSyncRegisteredOnUIThread, | 494 base::Bind(&NotifyBackgroundSyncRegisteredOnUIThread, |
506 service_worker_context_, | 495 service_worker_context_, |
507 sw_registration->pattern().GetOrigin())); | 496 sw_registration->pattern().GetOrigin())); |
508 | 497 |
509 RefCountedRegistration* existing_registration_ref = | 498 RefCountedRegistration* existing_registration_ref = |
510 LookupActiveRegistration(sw_registration_id, RegistrationKey(options)); | 499 LookupActiveRegistration(sw_registration_id, RegistrationKey(options)); |
511 if (existing_registration_ref) { | 500 if (existing_registration_ref) { |
512 if (existing_registration_ref->value()->options()->Equals(options)) { | 501 if (existing_registration_ref->value()->options()->Equals(options)) { |
513 BackgroundSyncRegistration* existing_registration = | 502 BackgroundSyncRegistration* existing_registration = |
514 existing_registration_ref->value(); | 503 existing_registration_ref->value(); |
515 | 504 |
516 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = | 505 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
517 AreOptionConditionsMet(options) | 506 AreOptionConditionsMet(options) |
518 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE | 507 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
519 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; | 508 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
520 BackgroundSyncMetrics::CountRegisterSuccess( | 509 BackgroundSyncMetrics::CountRegisterSuccess( |
521 existing_registration->options()->periodicity, | |
522 registration_could_fire, | 510 registration_could_fire, |
523 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE); | 511 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE); |
524 | 512 |
525 if (existing_registration->IsFiring()) { | 513 if (existing_registration->IsFiring()) { |
526 existing_registration->set_sync_state( | 514 existing_registration->set_sync_state( |
527 BackgroundSyncState::REREGISTERED_WHILE_FIRING); | 515 BackgroundSyncState::REREGISTERED_WHILE_FIRING); |
528 } | 516 } |
529 | 517 |
530 base::ThreadTaskRunnerHandle::Get()->PostTask( | 518 base::ThreadTaskRunnerHandle::Get()->PostTask( |
531 FROM_HERE, | 519 FROM_HERE, |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
659 registrations_proto.set_next_registration_id(registrations.next_id); | 647 registrations_proto.set_next_registration_id(registrations.next_id); |
660 registrations_proto.set_origin(registrations.origin.spec()); | 648 registrations_proto.set_origin(registrations.origin.spec()); |
661 | 649 |
662 for (const auto& key_and_registration : registrations.registration_map) { | 650 for (const auto& key_and_registration : registrations.registration_map) { |
663 const BackgroundSyncRegistration& registration = | 651 const BackgroundSyncRegistration& registration = |
664 *key_and_registration.second->value(); | 652 *key_and_registration.second->value(); |
665 BackgroundSyncRegistrationProto* registration_proto = | 653 BackgroundSyncRegistrationProto* registration_proto = |
666 registrations_proto.add_registration(); | 654 registrations_proto.add_registration(); |
667 registration_proto->set_id(registration.id()); | 655 registration_proto->set_id(registration.id()); |
668 registration_proto->set_tag(registration.options()->tag); | 656 registration_proto->set_tag(registration.options()->tag); |
669 registration_proto->set_periodicity(registration.options()->periodicity); | |
670 registration_proto->set_min_period(registration.options()->min_period); | |
671 registration_proto->set_network_state( | 657 registration_proto->set_network_state( |
672 registration.options()->network_state); | 658 registration.options()->network_state); |
673 registration_proto->set_num_attempts(registration.num_attempts()); | 659 registration_proto->set_num_attempts(registration.num_attempts()); |
674 registration_proto->set_delay_until( | 660 registration_proto->set_delay_until( |
675 registration.delay_until().ToInternalValue()); | 661 registration.delay_until().ToInternalValue()); |
676 } | 662 } |
677 std::string serialized; | 663 std::string serialized; |
678 bool success = registrations_proto.SerializeToString(&serialized); | 664 bool success = registrations_proto.SerializeToString(&serialized); |
679 DCHECK(success); | 665 DCHECK(success); |
680 | 666 |
681 StoreDataInBackend(sw_registration_id, registrations.origin, | 667 StoreDataInBackend(sw_registration_id, registrations.origin, |
682 kBackgroundSyncUserDataKey, serialized, callback); | 668 kBackgroundSyncUserDataKey, serialized, callback); |
683 } | 669 } |
684 | 670 |
685 void BackgroundSyncManager::RegisterDidStore( | 671 void BackgroundSyncManager::RegisterDidStore( |
686 int64_t sw_registration_id, | 672 int64_t sw_registration_id, |
687 const scoped_refptr<RefCountedRegistration>& new_registration_ref, | 673 const scoped_refptr<RefCountedRegistration>& new_registration_ref, |
688 const StatusAndRegistrationCallback& callback, | 674 const StatusAndRegistrationCallback& callback, |
689 ServiceWorkerStatusCode status) { | 675 ServiceWorkerStatusCode status) { |
690 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 676 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
691 | 677 |
692 const BackgroundSyncRegistration* new_registration = | 678 const BackgroundSyncRegistration* new_registration = |
693 new_registration_ref->value(); | 679 new_registration_ref->value(); |
694 | 680 |
695 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 681 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
696 // The service worker registration is gone. | 682 // The service worker registration is gone. |
697 BackgroundSyncMetrics::CountRegisterFailure( | 683 BackgroundSyncMetrics::CountRegisterFailure( |
698 new_registration->options()->periodicity, | |
699 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 684 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
700 active_registrations_.erase(sw_registration_id); | 685 active_registrations_.erase(sw_registration_id); |
701 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); | 686 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
702 return; | 687 return; |
703 } | 688 } |
704 | 689 |
705 if (status != SERVICE_WORKER_OK) { | 690 if (status != SERVICE_WORKER_OK) { |
706 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | 691 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
707 "failure."; | 692 "failure."; |
708 BackgroundSyncMetrics::CountRegisterFailure( | 693 BackgroundSyncMetrics::CountRegisterFailure( |
709 new_registration->options()->periodicity, | |
710 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 694 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
711 DisableAndClearManager(base::Bind( | 695 DisableAndClearManager(base::Bind( |
712 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 696 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
713 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>()))); | 697 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>()))); |
714 return; | 698 return; |
715 } | 699 } |
716 | 700 |
717 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = | 701 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
718 AreOptionConditionsMet(*new_registration->options()) | 702 AreOptionConditionsMet(*new_registration->options()) |
719 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE | 703 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
720 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; | 704 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
721 BackgroundSyncMetrics::CountRegisterSuccess( | 705 BackgroundSyncMetrics::CountRegisterSuccess( |
722 new_registration->options()->periodicity, registration_could_fire, | 706 registration_could_fire, |
723 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE); | 707 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE); |
724 | 708 |
725 FireReadyEvents(); | 709 FireReadyEvents(); |
726 base::ThreadTaskRunnerHandle::Get()->PostTask( | 710 base::ThreadTaskRunnerHandle::Get()->PostTask( |
727 FROM_HERE, | 711 FROM_HERE, |
728 base::Bind( | 712 base::Bind( |
729 callback, BACKGROUND_SYNC_STATUS_OK, | 713 callback, BACKGROUND_SYNC_STATUS_OK, |
730 base::Passed(CreateRegistrationHandle(new_registration_ref.get())))); | 714 base::Passed(CreateRegistrationHandle(new_registration_ref.get())))); |
731 } | 715 } |
732 | 716 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
863 BackgroundSyncRegistrationHandle::HandleId handle_id, | 847 BackgroundSyncRegistrationHandle::HandleId handle_id, |
864 const StatusCallback& callback) { | 848 const StatusCallback& callback) { |
865 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 849 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
866 | 850 |
867 BackgroundSyncRegistration* registration = | 851 BackgroundSyncRegistration* registration = |
868 GetRegistrationForHandle(handle_id); | 852 GetRegistrationForHandle(handle_id); |
869 DCHECK(registration); | 853 DCHECK(registration); |
870 | 854 |
871 if (disabled_) { | 855 if (disabled_) { |
872 BackgroundSyncMetrics::CountUnregister( | 856 BackgroundSyncMetrics::CountUnregister( |
873 registration->options()->periodicity, | |
874 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 857 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
875 base::ThreadTaskRunnerHandle::Get()->PostTask( | 858 base::ThreadTaskRunnerHandle::Get()->PostTask( |
876 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | 859 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
877 return; | 860 return; |
878 } | 861 } |
879 | 862 |
880 op_scheduler_.ScheduleOperation(base::Bind( | 863 op_scheduler_.ScheduleOperation(base::Bind( |
881 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), | 864 &BackgroundSyncManager::UnregisterImpl, weak_ptr_factory_.GetWeakPtr(), |
882 sw_registration_id, RegistrationKey(*registration), registration->id(), | 865 sw_registration_id, RegistrationKey(*registration), registration->id(), |
883 registration->options()->periodicity, MakeStatusCompletion(callback))); | 866 MakeStatusCompletion(callback))); |
884 } | 867 } |
885 | 868 |
886 void BackgroundSyncManager::UnregisterImpl( | 869 void BackgroundSyncManager::UnregisterImpl( |
887 int64_t sw_registration_id, | 870 int64_t sw_registration_id, |
888 const RegistrationKey& registration_key, | 871 const RegistrationKey& registration_key, |
889 BackgroundSyncRegistration::RegistrationId sync_registration_id, | 872 BackgroundSyncRegistration::RegistrationId sync_registration_id, |
890 SyncPeriodicity periodicity, | |
891 const StatusCallback& callback) { | 873 const StatusCallback& callback) { |
892 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 874 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
893 | 875 |
894 if (disabled_) { | 876 if (disabled_) { |
895 BackgroundSyncMetrics::CountUnregister( | 877 BackgroundSyncMetrics::CountUnregister( |
896 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 878 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
897 base::ThreadTaskRunnerHandle::Get()->PostTask( | 879 base::ThreadTaskRunnerHandle::Get()->PostTask( |
898 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | 880 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
899 return; | 881 return; |
900 } | 882 } |
901 | 883 |
902 RefCountedRegistration* existing_registration = | 884 RefCountedRegistration* existing_registration = |
903 LookupActiveRegistration(sw_registration_id, registration_key); | 885 LookupActiveRegistration(sw_registration_id, registration_key); |
904 | 886 |
905 if (!existing_registration || | 887 if (!existing_registration || |
906 existing_registration->value()->id() != sync_registration_id) { | 888 existing_registration->value()->id() != sync_registration_id) { |
907 BackgroundSyncMetrics::CountUnregister(periodicity, | 889 BackgroundSyncMetrics::CountUnregister(BACKGROUND_SYNC_STATUS_NOT_FOUND); |
908 BACKGROUND_SYNC_STATUS_NOT_FOUND); | |
909 base::ThreadTaskRunnerHandle::Get()->PostTask( | 890 base::ThreadTaskRunnerHandle::Get()->PostTask( |
910 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND)); | 891 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND)); |
911 return; | 892 return; |
912 } | 893 } |
913 | 894 |
914 existing_registration->value()->SetUnregisteredState(); | 895 existing_registration->value()->SetUnregisteredState(); |
915 | 896 |
916 RemoveActiveRegistration(sw_registration_id, registration_key); | 897 RemoveActiveRegistration(sw_registration_id, registration_key); |
917 | 898 |
918 StoreRegistrations(sw_registration_id, | 899 StoreRegistrations( |
919 base::Bind(&BackgroundSyncManager::UnregisterDidStore, | 900 sw_registration_id, |
920 weak_ptr_factory_.GetWeakPtr(), | 901 base::Bind(&BackgroundSyncManager::UnregisterDidStore, |
921 sw_registration_id, periodicity, callback)); | 902 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, callback)); |
922 } | 903 } |
923 | 904 |
924 void BackgroundSyncManager::UnregisterDidStore(int64_t sw_registration_id, | 905 void BackgroundSyncManager::UnregisterDidStore(int64_t sw_registration_id, |
925 SyncPeriodicity periodicity, | |
926 const StatusCallback& callback, | 906 const StatusCallback& callback, |
927 ServiceWorkerStatusCode status) { | 907 ServiceWorkerStatusCode status) { |
928 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 908 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
929 | 909 |
930 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 910 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
931 // ServiceWorker was unregistered. | 911 // ServiceWorker was unregistered. |
932 BackgroundSyncMetrics::CountUnregister( | 912 BackgroundSyncMetrics::CountUnregister( |
933 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 913 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
934 active_registrations_.erase(sw_registration_id); | 914 active_registrations_.erase(sw_registration_id); |
935 base::ThreadTaskRunnerHandle::Get()->PostTask( | 915 base::ThreadTaskRunnerHandle::Get()->PostTask( |
936 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | 916 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
937 return; | 917 return; |
938 } | 918 } |
939 | 919 |
940 if (status != SERVICE_WORKER_OK) { | 920 if (status != SERVICE_WORKER_OK) { |
941 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; | 921 LOG(ERROR) << "BackgroundSync failed to unregister due to backend failure."; |
942 BackgroundSyncMetrics::CountUnregister( | 922 BackgroundSyncMetrics::CountUnregister( |
943 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 923 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
944 DisableAndClearManager( | 924 DisableAndClearManager( |
945 base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | 925 base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
946 return; | 926 return; |
947 } | 927 } |
948 | 928 |
949 BackgroundSyncMetrics::CountUnregister(periodicity, | 929 BackgroundSyncMetrics::CountUnregister(BACKGROUND_SYNC_STATUS_OK); |
950 BACKGROUND_SYNC_STATUS_OK); | |
951 base::ThreadTaskRunnerHandle::Get()->PostTask( | 930 base::ThreadTaskRunnerHandle::Get()->PostTask( |
952 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK)); | 931 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK)); |
953 } | 932 } |
954 | 933 |
955 void BackgroundSyncManager::NotifyWhenFinished( | 934 void BackgroundSyncManager::NotifyWhenFinished( |
956 BackgroundSyncRegistrationHandle::HandleId handle_id, | 935 BackgroundSyncRegistrationHandle::HandleId handle_id, |
957 const StatusAndStateCallback& callback) { | 936 const StatusAndStateCallback& callback) { |
958 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 937 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
959 | 938 |
960 if (disabled_) { | 939 if (disabled_) { |
961 base::ThreadTaskRunnerHandle::Get()->PostTask( | 940 base::ThreadTaskRunnerHandle::Get()->PostTask( |
962 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 941 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
963 BackgroundSyncState::FAILED)); | 942 BackgroundSyncState::FAILED)); |
964 return; | 943 return; |
965 } | 944 } |
966 | 945 |
967 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = | 946 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = |
968 DuplicateRegistrationHandle(handle_id); | 947 DuplicateRegistrationHandle(handle_id); |
969 | 948 |
970 op_scheduler_.ScheduleOperation( | 949 op_scheduler_.ScheduleOperation( |
971 base::Bind(&BackgroundSyncManager::NotifyWhenFinishedImpl, | 950 base::Bind(&BackgroundSyncManager::NotifyWhenFinishedImpl, |
972 weak_ptr_factory_.GetWeakPtr(), | 951 weak_ptr_factory_.GetWeakPtr(), |
973 base::Passed(std::move(registration_handle)), callback)); | 952 base::Passed(std::move(registration_handle)), callback)); |
974 } | 953 } |
975 | 954 |
976 void BackgroundSyncManager::NotifyWhenFinishedImpl( | 955 void BackgroundSyncManager::NotifyWhenFinishedImpl( |
977 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, | 956 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, |
978 const StatusAndStateCallback& callback) { | 957 const StatusAndStateCallback& callback) { |
979 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 958 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
980 DCHECK_EQ(SYNC_ONE_SHOT, registration_handle->options()->periodicity); | |
981 | 959 |
982 if (disabled_) { | 960 if (disabled_) { |
983 base::ThreadTaskRunnerHandle::Get()->PostTask( | 961 base::ThreadTaskRunnerHandle::Get()->PostTask( |
984 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 962 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
985 BackgroundSyncState::FAILED)); | 963 BackgroundSyncState::FAILED)); |
986 return; | 964 return; |
987 } | 965 } |
988 | 966 |
989 if (!registration_handle->registration()->HasCompleted()) { | 967 if (!registration_handle->registration()->HasCompleted()) { |
990 registration_handle->registration()->AddFinishedCallback( | 968 registration_handle->registration()->AddFinishedCallback( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1027 } | 1005 } |
1028 | 1006 |
1029 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1007 base::ThreadTaskRunnerHandle::Get()->PostTask( |
1030 FROM_HERE, | 1008 FROM_HERE, |
1031 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, | 1009 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, |
1032 base::Passed(CreateRegistrationHandle(registration)))); | 1010 base::Passed(CreateRegistrationHandle(registration)))); |
1033 } | 1011 } |
1034 | 1012 |
1035 void BackgroundSyncManager::GetRegistrationsImpl( | 1013 void BackgroundSyncManager::GetRegistrationsImpl( |
1036 int64_t sw_registration_id, | 1014 int64_t sw_registration_id, |
1037 SyncPeriodicity periodicity, | |
1038 const StatusAndRegistrationsCallback& callback) { | 1015 const StatusAndRegistrationsCallback& callback) { |
1039 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1016 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1040 | 1017 |
1041 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> out_registrations( | 1018 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> out_registrations( |
1042 new ScopedVector<BackgroundSyncRegistrationHandle>()); | 1019 new ScopedVector<BackgroundSyncRegistrationHandle>()); |
1043 | 1020 |
1044 if (disabled_) { | 1021 if (disabled_) { |
1045 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1022 base::ThreadTaskRunnerHandle::Get()->PostTask( |
1046 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 1023 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
1047 base::Passed(std::move(out_registrations)))); | 1024 base::Passed(std::move(out_registrations)))); |
1048 return; | 1025 return; |
1049 } | 1026 } |
1050 | 1027 |
1051 SWIdToRegistrationsMap::iterator it = | 1028 SWIdToRegistrationsMap::iterator it = |
1052 active_registrations_.find(sw_registration_id); | 1029 active_registrations_.find(sw_registration_id); |
1053 | 1030 |
1054 if (it != active_registrations_.end()) { | 1031 if (it != active_registrations_.end()) { |
1055 const BackgroundSyncRegistrations& registrations = it->second; | 1032 const BackgroundSyncRegistrations& registrations = it->second; |
1056 for (const auto& tag_and_registration : registrations.registration_map) { | 1033 for (const auto& tag_and_registration : registrations.registration_map) { |
1057 RefCountedRegistration* registration = tag_and_registration.second.get(); | 1034 RefCountedRegistration* registration = tag_and_registration.second.get(); |
1058 if (registration->value()->options()->periodicity == periodicity) { | 1035 out_registrations->push_back( |
1059 out_registrations->push_back( | 1036 CreateRegistrationHandle(registration).release()); |
1060 CreateRegistrationHandle(registration).release()); | |
1061 } | |
1062 } | 1037 } |
1063 } | 1038 } |
1064 | 1039 |
1065 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1040 base::ThreadTaskRunnerHandle::Get()->PostTask( |
1066 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, | 1041 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, |
1067 base::Passed(std::move(out_registrations)))); | 1042 base::Passed(std::move(out_registrations)))); |
1068 } | 1043 } |
1069 | 1044 |
1070 bool BackgroundSyncManager::AreOptionConditionsMet( | 1045 bool BackgroundSyncManager::AreOptionConditionsMet( |
1071 const BackgroundSyncRegistrationOptions& options) { | 1046 const BackgroundSyncRegistrationOptions& options) { |
1072 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1047 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1073 return network_observer_->NetworkSufficient(options.network_state); | 1048 return network_observer_->NetworkSufficient(options.network_state); |
1074 } | 1049 } |
1075 | 1050 |
1076 bool BackgroundSyncManager::IsRegistrationReadyToFire( | 1051 bool BackgroundSyncManager::IsRegistrationReadyToFire( |
1077 const BackgroundSyncRegistration& registration) { | 1052 const BackgroundSyncRegistration& registration) { |
1078 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1053 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1079 | 1054 |
1080 // TODO(jkarlin): Add support for firing periodic registrations. | |
1081 if (registration.options()->periodicity == SYNC_PERIODIC) | |
1082 return false; | |
1083 | |
1084 if (registration.sync_state() != BackgroundSyncState::PENDING) | 1055 if (registration.sync_state() != BackgroundSyncState::PENDING) |
1085 return false; | 1056 return false; |
1086 | 1057 |
1087 if (clock_->Now() < registration.delay_until()) | 1058 if (clock_->Now() < registration.delay_until()) |
1088 return false; | 1059 return false; |
1089 | 1060 |
1090 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); | |
1091 | |
1092 return AreOptionConditionsMet(*registration.options()); | 1061 return AreOptionConditionsMet(*registration.options()); |
1093 } | 1062 } |
1094 | 1063 |
1095 void BackgroundSyncManager::RunInBackgroundIfNecessary() { | 1064 void BackgroundSyncManager::RunInBackgroundIfNecessary() { |
1096 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1065 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1097 base::TimeDelta soonest_wakeup_delta = base::TimeDelta::Max(); | 1066 base::TimeDelta soonest_wakeup_delta = base::TimeDelta::Max(); |
1098 | 1067 |
1099 for (const auto& sw_id_and_registrations : active_registrations_) { | 1068 for (const auto& sw_id_and_registrations : active_registrations_) { |
1100 for (const auto& key_and_registration : | 1069 for (const auto& key_and_registration : |
1101 sw_id_and_registrations.second.registration_map) { | 1070 sw_id_and_registrations.second.registration_map) { |
1102 const BackgroundSyncRegistration& registration = | 1071 const BackgroundSyncRegistration& registration = |
1103 *key_and_registration.second->value(); | 1072 *key_and_registration.second->value(); |
1104 if (registration.sync_state() == BackgroundSyncState::PENDING) { | 1073 if (registration.sync_state() == BackgroundSyncState::PENDING) { |
1105 if (registration.options()->periodicity == SYNC_ONE_SHOT) { | 1074 if (clock_->Now() >= registration.delay_until()) { |
1106 if (clock_->Now() >= registration.delay_until()) { | 1075 soonest_wakeup_delta = base::TimeDelta(); |
1107 soonest_wakeup_delta = base::TimeDelta(); | |
1108 } else { | |
1109 base::TimeDelta delay_delta = | |
1110 registration.delay_until() - clock_->Now(); | |
1111 if (delay_delta < soonest_wakeup_delta) | |
1112 soonest_wakeup_delta = delay_delta; | |
1113 } | |
1114 } else { | 1076 } else { |
1115 // TODO(jkarlin): Support keeping the browser alive for periodic | 1077 base::TimeDelta delay_delta = |
1116 // syncs. | 1078 registration.delay_until() - clock_->Now(); |
1079 if (delay_delta < soonest_wakeup_delta) | |
1080 soonest_wakeup_delta = delay_delta; | |
1117 } | 1081 } |
1118 } | 1082 } |
1119 } | 1083 } |
1120 } | 1084 } |
1121 | 1085 |
1122 // If the browser is closed while firing events, the browser needs a task to | 1086 // If the browser is closed while firing events, the browser needs a task to |
1123 // wake it back up and try again. | 1087 // wake it back up and try again. |
1124 if (num_firing_registrations_ > 0 && | 1088 if (num_firing_registrations_ > 0 && |
1125 soonest_wakeup_delta > parameters_->min_sync_recovery_time) { | 1089 soonest_wakeup_delta > parameters_->min_sync_recovery_time) { |
1126 soonest_wakeup_delta = parameters_->min_sync_recovery_time; | 1090 soonest_wakeup_delta = parameters_->min_sync_recovery_time; |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1253 registration_handle->handle_id(); | 1217 registration_handle->handle_id(); |
1254 | 1218 |
1255 BackgroundSyncEventLastChance last_chance = | 1219 BackgroundSyncEventLastChance last_chance = |
1256 registration->value()->num_attempts() == | 1220 registration->value()->num_attempts() == |
1257 parameters_->max_sync_attempts - 1 | 1221 parameters_->max_sync_attempts - 1 |
1258 ? BackgroundSyncEventLastChance::IS_LAST_CHANCE | 1222 ? BackgroundSyncEventLastChance::IS_LAST_CHANCE |
1259 : BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE; | 1223 : BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE; |
1260 | 1224 |
1261 HasMainFrameProviderHost( | 1225 HasMainFrameProviderHost( |
1262 service_worker_registration->pattern().GetOrigin(), | 1226 service_worker_registration->pattern().GetOrigin(), |
1263 base::Bind(&BackgroundSyncMetrics::RecordEventStarted, | 1227 base::Bind(&BackgroundSyncMetrics::RecordEventStarted)); |
1264 registration->value()->options()->periodicity)); | |
1265 | 1228 |
1266 FireOneShotSync( | 1229 FireOneShotSync( |
1267 handle_id, service_worker_registration->active_version(), last_chance, | 1230 handle_id, service_worker_registration->active_version(), last_chance, |
1268 base::Bind(&BackgroundSyncManager::EventComplete, | 1231 base::Bind(&BackgroundSyncManager::EventComplete, |
1269 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, | 1232 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, |
1270 service_worker_registration->id(), | 1233 service_worker_registration->id(), |
1271 base::Passed(std::move(registration_handle)), | 1234 base::Passed(std::move(registration_handle)), |
1272 event_completed_callback)); | 1235 event_completed_callback)); |
1273 | 1236 |
1274 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1237 base::ThreadTaskRunnerHandle::Get()->PostTask( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1319 num_firing_registrations_ -= 1; | 1282 num_firing_registrations_ -= 1; |
1320 | 1283 |
1321 // The event ran to completion, we should count it, no matter what happens | 1284 // The event ran to completion, we should count it, no matter what happens |
1322 // from here. | 1285 // from here. |
1323 ServiceWorkerRegistration* sw_registration = | 1286 ServiceWorkerRegistration* sw_registration = |
1324 service_worker_context_->GetLiveRegistration(service_worker_id); | 1287 service_worker_context_->GetLiveRegistration(service_worker_id); |
1325 if (sw_registration) { | 1288 if (sw_registration) { |
1326 HasMainFrameProviderHost( | 1289 HasMainFrameProviderHost( |
1327 sw_registration->pattern().GetOrigin(), | 1290 sw_registration->pattern().GetOrigin(), |
1328 base::Bind(&BackgroundSyncMetrics::RecordEventResult, | 1291 base::Bind(&BackgroundSyncMetrics::RecordEventResult, |
1329 registration->options()->periodicity, | |
1330 status_code == SERVICE_WORKER_OK)); | 1292 status_code == SERVICE_WORKER_OK)); |
1331 } | 1293 } |
1332 | 1294 |
1333 if (registration->options()->periodicity == SYNC_ONE_SHOT) { | 1295 if (registration->sync_state() == |
1296 BackgroundSyncState::REREGISTERED_WHILE_FIRING) { | |
1297 registration->set_sync_state(BackgroundSyncState::PENDING); | |
1298 registration->set_num_attempts(0); | |
1299 } else if (status_code != SERVICE_WORKER_OK) { // Sync failed | |
1300 bool can_retry = | |
1301 registration->num_attempts() < parameters_->max_sync_attempts; | |
1334 if (registration->sync_state() == | 1302 if (registration->sync_state() == |
1335 BackgroundSyncState::REREGISTERED_WHILE_FIRING) { | 1303 BackgroundSyncState::UNREGISTERED_WHILE_FIRING) { |
1304 registration->set_sync_state(can_retry ? BackgroundSyncState::UNREGISTERED | |
1305 : BackgroundSyncState::FAILED); | |
1306 registration->RunFinishedCallbacks(); | |
1307 } else if (can_retry) { | |
1336 registration->set_sync_state(BackgroundSyncState::PENDING); | 1308 registration->set_sync_state(BackgroundSyncState::PENDING); |
1337 registration->set_num_attempts(0); | 1309 registration->set_delay_until(clock_->Now() + |
1338 } else if (status_code != SERVICE_WORKER_OK) { // Sync failed | 1310 parameters_->initial_retry_delay * |
1339 bool can_retry = | 1311 pow(parameters_->retry_delay_factor, |
1340 registration->num_attempts() < parameters_->max_sync_attempts; | 1312 registration->num_attempts() - 1)); |
1341 if (registration->sync_state() == | 1313 } else { |
1342 BackgroundSyncState::UNREGISTERED_WHILE_FIRING) { | 1314 registration->set_sync_state(BackgroundSyncState::FAILED); |
1343 registration->set_sync_state(can_retry | |
1344 ? BackgroundSyncState::UNREGISTERED | |
1345 : BackgroundSyncState::FAILED); | |
1346 registration->RunFinishedCallbacks(); | |
1347 } else if (can_retry) { | |
1348 registration->set_sync_state(BackgroundSyncState::PENDING); | |
1349 registration->set_delay_until( | |
1350 clock_->Now() + | |
1351 parameters_->initial_retry_delay * | |
1352 pow(parameters_->retry_delay_factor, | |
1353 registration->num_attempts() - 1)); | |
1354 } else { | |
1355 registration->set_sync_state(BackgroundSyncState::FAILED); | |
1356 registration->RunFinishedCallbacks(); | |
1357 } | |
1358 } else { // Sync succeeded | |
1359 registration->set_sync_state(BackgroundSyncState::SUCCESS); | |
1360 registration->RunFinishedCallbacks(); | 1315 registration->RunFinishedCallbacks(); |
1361 } | 1316 } |
1317 } else { // Sync succeeded | |
1318 registration->set_sync_state(BackgroundSyncState::SUCCESS); | |
1319 registration->RunFinishedCallbacks(); | |
1320 } | |
1362 | 1321 |
1363 if (registration->HasCompleted()) { | 1322 if (registration->HasCompleted()) { |
1364 RegistrationKey key(*registration); | 1323 RegistrationKey key(*registration); |
1365 RefCountedRegistration* active_registration = | 1324 RefCountedRegistration* active_registration = |
1366 LookupActiveRegistration(service_worker_id, key); | 1325 LookupActiveRegistration(service_worker_id, key); |
1367 if (active_registration && | 1326 if (active_registration && |
1368 active_registration->value()->id() == registration->id()) { | 1327 active_registration->value()->id() == registration->id()) { |
1369 RemoveActiveRegistration(service_worker_id, key); | 1328 RemoveActiveRegistration(service_worker_id, key); |
1370 } | |
1371 } | 1329 } |
1372 } else { // !SYNC_ONE_SHOT | |
1373 // TODO(jkarlin): Add support for running periodic syncs. (crbug.com/479674) | |
1374 NOTREACHED(); | |
1375 } | 1330 } |
1376 | 1331 |
1377 if (disabled_) { | 1332 if (disabled_) { |
1378 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1333 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
1379 base::Bind(callback)); | 1334 base::Bind(callback)); |
1380 return; | 1335 return; |
1381 } | 1336 } |
1382 | 1337 |
1383 StoreRegistrations( | 1338 StoreRegistrations( |
1384 service_worker_id, | 1339 service_worker_id, |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1528 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1483 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
1529 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1484 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1530 | 1485 |
1531 return base::Bind( | 1486 return base::Bind( |
1532 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1487 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
1533 BackgroundSyncStatus>, | 1488 BackgroundSyncStatus>, |
1534 weak_ptr_factory_.GetWeakPtr(), callback); | 1489 weak_ptr_factory_.GetWeakPtr(), callback); |
1535 } | 1490 } |
1536 | 1491 |
1537 } // namespace content | 1492 } // namespace content |
OLD | NEW |