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

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

Issue 1763123002: [BackgroundSync] Remove BackgroundSyncRegistrationHandle (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments from PS7 Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h" 13 #include "base/thread_task_runner_handle.h"
14 #include "base/time/default_clock.h" 14 #include "base/time/default_clock.h"
15 #include "build/build_config.h" 15 #include "build/build_config.h"
16 #include "content/browser/background_sync/background_sync_metrics.h" 16 #include "content/browser/background_sync/background_sync_metrics.h"
17 #include "content/browser/background_sync/background_sync_network_observer.h" 17 #include "content/browser/background_sync/background_sync_network_observer.h"
18 #include "content/browser/background_sync/background_sync_registration_handle.h"
19 #include "content/browser/background_sync/background_sync_registration_options.h " 18 #include "content/browser/background_sync/background_sync_registration_options.h "
20 #include "content/browser/service_worker/service_worker_context_wrapper.h" 19 #include "content/browser/service_worker/service_worker_context_wrapper.h"
21 #include "content/browser/service_worker/service_worker_storage.h" 20 #include "content/browser/service_worker/service_worker_storage.h"
22 #include "content/browser/storage_partition_impl.h" 21 #include "content/browser/storage_partition_impl.h"
23 #include "content/common/service_worker/service_worker_type_converters.h" 22 #include "content/common/service_worker/service_worker_type_converters.h"
24 #include "content/public/browser/background_sync_controller.h" 23 #include "content/public/browser/background_sync_controller.h"
25 #include "content/public/browser/browser_context.h" 24 #include "content/public/browser/browser_context.h"
26 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
27 #include "content/public/common/background_sync.mojom.h" 26 #include "content/public/common/background_sync.mojom.h"
28 27
29 #if defined(OS_ANDROID) 28 #if defined(OS_ANDROID)
30 #include "content/browser/android/background_sync_network_observer_android.h" 29 #include "content/browser/android/background_sync_network_observer_android.h"
31 #endif 30 #endif
32 31
33 namespace content { 32 namespace content {
34 33
35 class BackgroundSyncManager::RefCountedRegistration
36 : public base::RefCounted<RefCountedRegistration> {
37 public:
38 BackgroundSyncRegistration* value() { return &registration_; }
39 const BackgroundSyncRegistration* value() const { return &registration_; }
40
41 private:
42 friend class base::RefCounted<RefCountedRegistration>;
43 ~RefCountedRegistration() = default;
44
45 BackgroundSyncRegistration registration_;
46 };
47
48 namespace { 34 namespace {
49 35
50 // The key used to index the background sync data in ServiceWorkerStorage. 36 // The key used to index the background sync data in ServiceWorkerStorage.
51 const char kBackgroundSyncUserDataKey[] = "BackgroundSyncUserData"; 37 const char kBackgroundSyncUserDataKey[] = "BackgroundSyncUserData";
52 38
53 void PostErrorResponse( 39 void PostErrorResponse(
54 BackgroundSyncStatus status, 40 BackgroundSyncStatus status,
55 const BackgroundSyncManager::StatusAndRegistrationCallback& callback) { 41 const BackgroundSyncManager::StatusAndRegistrationCallback& callback) {
56 base::ThreadTaskRunnerHandle::Get()->PostTask( 42 base::ThreadTaskRunnerHandle::Get()->PostTask(
57 FROM_HERE, 43 FROM_HERE,
58 base::Bind(callback, status, 44 base::Bind(callback, status,
59 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>()))); 45 base::Passed(scoped_ptr<BackgroundSyncRegistration>())));
60 } 46 }
61 47
62 // Returns nullptr if the controller cannot be accessed for any reason. 48 // Returns nullptr if the controller cannot be accessed for any reason.
63 BackgroundSyncController* GetBackgroundSyncControllerOnUIThread( 49 BackgroundSyncController* GetBackgroundSyncControllerOnUIThread(
64 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) { 50 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) {
65 DCHECK_CURRENTLY_ON(BrowserThread::UI); 51 DCHECK_CURRENTLY_ON(BrowserThread::UI);
66 52
67 if (!service_worker_context) 53 if (!service_worker_context)
68 return nullptr; 54 return nullptr;
69 StoragePartitionImpl* storage_partition_impl = 55 StoragePartitionImpl* storage_partition_impl =
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 void BackgroundSyncManager::GetRegistrations( 195 void BackgroundSyncManager::GetRegistrations(
210 int64_t sw_registration_id, 196 int64_t sw_registration_id,
211 const StatusAndRegistrationsCallback& callback) { 197 const StatusAndRegistrationsCallback& callback) {
212 DCHECK_CURRENTLY_ON(BrowserThread::IO); 198 DCHECK_CURRENTLY_ON(BrowserThread::IO);
213 199
214 if (disabled_) { 200 if (disabled_) {
215 base::ThreadTaskRunnerHandle::Get()->PostTask( 201 base::ThreadTaskRunnerHandle::Get()->PostTask(
216 FROM_HERE, 202 FROM_HERE,
217 base::Bind( 203 base::Bind(
218 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, 204 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
219 base::Passed( 205 base::Passed(scoped_ptr<ScopedVector<BackgroundSyncRegistration>>(
220 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>>( 206 new ScopedVector<BackgroundSyncRegistration>()))));
221 new ScopedVector<BackgroundSyncRegistrationHandle>()))));
222 return; 207 return;
223 } 208 }
224 209
225 op_scheduler_.ScheduleOperation( 210 op_scheduler_.ScheduleOperation(
226 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl, 211 base::Bind(&BackgroundSyncManager::GetRegistrationsImpl,
227 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, 212 weak_ptr_factory_.GetWeakPtr(), sw_registration_id,
228 MakeStatusAndRegistrationsCompletion(callback))); 213 MakeStatusAndRegistrationsCompletion(callback)));
229 } 214 }
230 215
231 // Given a HandleId |handle_id|, return a new handle for the same
232 // registration.
233 scoped_ptr<BackgroundSyncRegistrationHandle>
234 BackgroundSyncManager::DuplicateRegistrationHandle(
235 BackgroundSyncRegistrationHandle::HandleId handle_id) {
236 DCHECK_CURRENTLY_ON(BrowserThread::IO);
237
238 scoped_refptr<RefCountedRegistration>* ref_registration =
239 registration_handle_ids_.Lookup(handle_id);
240 if (!ref_registration)
241 return scoped_ptr<BackgroundSyncRegistrationHandle>();
242 return CreateRegistrationHandle(ref_registration->get());
243 }
244
245 void BackgroundSyncManager::OnRegistrationDeleted(int64_t sw_registration_id, 216 void BackgroundSyncManager::OnRegistrationDeleted(int64_t sw_registration_id,
246 const GURL& pattern) { 217 const GURL& pattern) {
247 DCHECK_CURRENTLY_ON(BrowserThread::IO); 218 DCHECK_CURRENTLY_ON(BrowserThread::IO);
248 219
249 // Operations already in the queue will either fail when they write to storage 220 // Operations already in the queue will either fail when they write to storage
250 // or return stale results based on registrations loaded in memory. This is 221 // or return stale results based on registrations loaded in memory. This is
251 // inconsequential since the service worker is gone. 222 // inconsequential since the service worker is gone.
252 op_scheduler_.ScheduleOperation( 223 op_scheduler_.ScheduleOperation(
253 base::Bind(&BackgroundSyncManager::OnRegistrationDeletedImpl, 224 base::Bind(&BackgroundSyncManager::OnRegistrationDeletedImpl,
254 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, 225 weak_ptr_factory_.GetWeakPtr(), sw_registration_id,
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 const BackgroundSyncRegistrationProto& registration_proto = 342 const BackgroundSyncRegistrationProto& registration_proto =
372 registrations_proto.registration(i); 343 registrations_proto.registration(i);
373 344
374 if (registration_proto.id() >= registrations->next_id) { 345 if (registration_proto.id() >= registrations->next_id) {
375 corruption_detected = true; 346 corruption_detected = true;
376 break; 347 break;
377 } 348 }
378 349
379 RegistrationKey registration_key(registration_proto.tag()); 350 RegistrationKey registration_key(registration_proto.tag());
380 351
381 scoped_refptr<RefCountedRegistration> ref_registration( 352 BackgroundSyncRegistration* registration =
382 new RefCountedRegistration()); 353 &registrations->registration_map[registration_key];
383 registrations->registration_map[registration_key] = ref_registration;
384 BackgroundSyncRegistration* registration = ref_registration->value();
385 354
386 BackgroundSyncRegistrationOptions* options = registration->options(); 355 BackgroundSyncRegistrationOptions* options = registration->options();
387 options->tag = registration_proto.tag(); 356 options->tag = registration_proto.tag();
388 options->network_state = registration_proto.network_state(); 357 options->network_state = registration_proto.network_state();
389 358
390 registration->set_id(registration_proto.id()); 359 registration->set_id(registration_proto.id());
391 registration->set_num_attempts(registration_proto.num_attempts()); 360 registration->set_num_attempts(registration_proto.num_attempts());
392 registration->set_delay_until( 361 registration->set_delay_until(
393 base::Time::FromInternalValue(registration_proto.delay_until())); 362 base::Time::FromInternalValue(registration_proto.delay_until()));
394 } 363 }
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); 444 BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER);
476 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback); 445 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback);
477 return; 446 return;
478 } 447 }
479 448
480 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 449 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
481 base::Bind(&NotifyBackgroundSyncRegisteredOnUIThread, 450 base::Bind(&NotifyBackgroundSyncRegisteredOnUIThread,
482 service_worker_context_, 451 service_worker_context_,
483 sw_registration->pattern().GetOrigin())); 452 sw_registration->pattern().GetOrigin()));
484 453
485 RefCountedRegistration* existing_registration_ref = 454 BackgroundSyncRegistration* existing_registration =
486 LookupActiveRegistration(sw_registration_id, RegistrationKey(options)); 455 LookupActiveRegistration(sw_registration_id, RegistrationKey(options));
487 if (existing_registration_ref) { 456 if (existing_registration) {
488 DCHECK(existing_registration_ref->value()->options()->Equals(options)); 457 DCHECK(existing_registration->options()->Equals(options));
489 BackgroundSyncRegistration* existing_registration =
490 existing_registration_ref->value();
491 458
492 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = 459 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire =
493 AreOptionConditionsMet(options) 460 AreOptionConditionsMet(options)
494 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE 461 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE
495 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; 462 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE;
496 BackgroundSyncMetrics::CountRegisterSuccess( 463 BackgroundSyncMetrics::CountRegisterSuccess(
497 registration_could_fire, 464 registration_could_fire,
498 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE); 465 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE);
499 466
500 if (existing_registration->IsFiring()) { 467 if (existing_registration->IsFiring()) {
501 existing_registration->set_sync_state( 468 existing_registration->set_sync_state(
502 BackgroundSyncState::REREGISTERED_WHILE_FIRING); 469 BackgroundSyncState::REREGISTERED_WHILE_FIRING);
503 } 470 }
504 471
505 base::ThreadTaskRunnerHandle::Get()->PostTask( 472 base::ThreadTaskRunnerHandle::Get()->PostTask(
506 FROM_HERE, 473 FROM_HERE,
507 base::Bind( 474 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK,
508 callback, BACKGROUND_SYNC_STATUS_OK, 475 base::Passed(make_scoped_ptr(new BackgroundSyncRegistration(
509 base::Passed(CreateRegistrationHandle(existing_registration_ref)))); 476 *existing_registration)))));
510 return; 477 return;
511 } 478 }
512 479
513 scoped_refptr<RefCountedRegistration> new_ref_registration( 480 BackgroundSyncRegistration new_registration;
514 new RefCountedRegistration());
515 BackgroundSyncRegistration* new_registration = new_ref_registration->value();
516 481
517 *new_registration->options() = options; 482 *new_registration.options() = options;
518 483
519 BackgroundSyncRegistrations* registrations = 484 BackgroundSyncRegistrations* registrations =
520 &active_registrations_[sw_registration_id]; 485 &active_registrations_[sw_registration_id];
521 new_registration->set_id(registrations->next_id++); 486 new_registration.set_id(registrations->next_id++);
522 487
523 AddActiveRegistration(sw_registration_id, 488 AddActiveRegistration(sw_registration_id,
524 sw_registration->pattern().GetOrigin(), 489 sw_registration->pattern().GetOrigin(),
525 new_ref_registration); 490 new_registration);
526 491
527 StoreRegistrations( 492 StoreRegistrations(
528 sw_registration_id, 493 sw_registration_id,
529 base::Bind(&BackgroundSyncManager::RegisterDidStore, 494 base::Bind(&BackgroundSyncManager::RegisterDidStore,
530 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, 495 weak_ptr_factory_.GetWeakPtr(), sw_registration_id,
531 new_ref_registration, callback)); 496 new_registration, callback));
532 } 497 }
533 498
534 void BackgroundSyncManager::DisableAndClearManager( 499 void BackgroundSyncManager::DisableAndClearManager(
535 const base::Closure& callback) { 500 const base::Closure& callback) {
536 DCHECK_CURRENTLY_ON(BrowserThread::IO); 501 DCHECK_CURRENTLY_ON(BrowserThread::IO);
537 502
538 if (disabled_) { 503 if (disabled_) {
539 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 504 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
540 base::Bind(callback)); 505 base::Bind(callback));
541 return; 506 return;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
580 void BackgroundSyncManager::DisableAndClearManagerClearedOne( 545 void BackgroundSyncManager::DisableAndClearManagerClearedOne(
581 const base::Closure& barrier_closure, 546 const base::Closure& barrier_closure,
582 ServiceWorkerStatusCode status) { 547 ServiceWorkerStatusCode status) {
583 DCHECK_CURRENTLY_ON(BrowserThread::IO); 548 DCHECK_CURRENTLY_ON(BrowserThread::IO);
584 549
585 // The status doesn't matter at this point, there is nothing else to be done. 550 // The status doesn't matter at this point, there is nothing else to be done.
586 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 551 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
587 base::Bind(barrier_closure)); 552 base::Bind(barrier_closure));
588 } 553 }
589 554
590 BackgroundSyncManager::RefCountedRegistration* 555 BackgroundSyncRegistration* BackgroundSyncManager::LookupActiveRegistration(
591 BackgroundSyncManager::LookupActiveRegistration(
592 int64_t sw_registration_id, 556 int64_t sw_registration_id,
593 const RegistrationKey& registration_key) { 557 const RegistrationKey& registration_key) {
594 DCHECK_CURRENTLY_ON(BrowserThread::IO); 558 DCHECK_CURRENTLY_ON(BrowserThread::IO);
595 559
596 SWIdToRegistrationsMap::iterator it = 560 SWIdToRegistrationsMap::iterator it =
597 active_registrations_.find(sw_registration_id); 561 active_registrations_.find(sw_registration_id);
598 if (it == active_registrations_.end()) 562 if (it == active_registrations_.end())
599 return nullptr; 563 return nullptr;
600 564
601 BackgroundSyncRegistrations& registrations = it->second; 565 BackgroundSyncRegistrations& registrations = it->second;
602 DCHECK_LE(BackgroundSyncRegistration::kInitialId, registrations.next_id); 566 DCHECK_LE(BackgroundSyncRegistration::kInitialId, registrations.next_id);
603 DCHECK(!registrations.origin.is_empty()); 567 DCHECK(!registrations.origin.is_empty());
604 568
605 auto key_and_registration_iter = 569 auto key_and_registration_iter =
606 registrations.registration_map.find(registration_key); 570 registrations.registration_map.find(registration_key);
607 if (key_and_registration_iter == registrations.registration_map.end()) 571 if (key_and_registration_iter == registrations.registration_map.end())
608 return nullptr; 572 return nullptr;
609 573
610 return key_and_registration_iter->second.get(); 574 return &key_and_registration_iter->second;
611 } 575 }
612 576
613 void BackgroundSyncManager::StoreRegistrations( 577 void BackgroundSyncManager::StoreRegistrations(
614 int64_t sw_registration_id, 578 int64_t sw_registration_id,
615 const ServiceWorkerStorage::StatusCallback& callback) { 579 const ServiceWorkerStorage::StatusCallback& callback) {
616 DCHECK_CURRENTLY_ON(BrowserThread::IO); 580 DCHECK_CURRENTLY_ON(BrowserThread::IO);
617 581
618 // Serialize the data. 582 // Serialize the data.
619 const BackgroundSyncRegistrations& registrations = 583 const BackgroundSyncRegistrations& registrations =
620 active_registrations_[sw_registration_id]; 584 active_registrations_[sw_registration_id];
621 BackgroundSyncRegistrationsProto registrations_proto; 585 BackgroundSyncRegistrationsProto registrations_proto;
622 registrations_proto.set_next_registration_id(registrations.next_id); 586 registrations_proto.set_next_registration_id(registrations.next_id);
623 registrations_proto.set_origin(registrations.origin.spec()); 587 registrations_proto.set_origin(registrations.origin.spec());
624 588
625 for (const auto& key_and_registration : registrations.registration_map) { 589 for (const auto& key_and_registration : registrations.registration_map) {
626 const BackgroundSyncRegistration& registration = 590 const BackgroundSyncRegistration& registration =
627 *key_and_registration.second->value(); 591 key_and_registration.second;
628 BackgroundSyncRegistrationProto* registration_proto = 592 BackgroundSyncRegistrationProto* registration_proto =
629 registrations_proto.add_registration(); 593 registrations_proto.add_registration();
630 registration_proto->set_id(registration.id()); 594 registration_proto->set_id(registration.id());
631 registration_proto->set_tag(registration.options()->tag); 595 registration_proto->set_tag(registration.options()->tag);
632 registration_proto->set_network_state( 596 registration_proto->set_network_state(
633 registration.options()->network_state); 597 registration.options()->network_state);
634 registration_proto->set_num_attempts(registration.num_attempts()); 598 registration_proto->set_num_attempts(registration.num_attempts());
635 registration_proto->set_delay_until( 599 registration_proto->set_delay_until(
636 registration.delay_until().ToInternalValue()); 600 registration.delay_until().ToInternalValue());
637 } 601 }
638 std::string serialized; 602 std::string serialized;
639 bool success = registrations_proto.SerializeToString(&serialized); 603 bool success = registrations_proto.SerializeToString(&serialized);
640 DCHECK(success); 604 DCHECK(success);
641 605
642 StoreDataInBackend(sw_registration_id, registrations.origin, 606 StoreDataInBackend(sw_registration_id, registrations.origin,
643 kBackgroundSyncUserDataKey, serialized, callback); 607 kBackgroundSyncUserDataKey, serialized, callback);
644 } 608 }
645 609
646 void BackgroundSyncManager::RegisterDidStore( 610 void BackgroundSyncManager::RegisterDidStore(
647 int64_t sw_registration_id, 611 int64_t sw_registration_id,
648 const scoped_refptr<RefCountedRegistration>& new_registration_ref, 612 const BackgroundSyncRegistration& new_registration,
649 const StatusAndRegistrationCallback& callback, 613 const StatusAndRegistrationCallback& callback,
650 ServiceWorkerStatusCode status) { 614 ServiceWorkerStatusCode status) {
651 DCHECK_CURRENTLY_ON(BrowserThread::IO); 615 DCHECK_CURRENTLY_ON(BrowserThread::IO);
652 616
653 const BackgroundSyncRegistration* new_registration =
654 new_registration_ref->value();
655
656 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { 617 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) {
657 // The service worker registration is gone. 618 // The service worker registration is gone.
658 BackgroundSyncMetrics::CountRegisterFailure( 619 BackgroundSyncMetrics::CountRegisterFailure(
659 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); 620 BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
660 active_registrations_.erase(sw_registration_id); 621 active_registrations_.erase(sw_registration_id);
661 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); 622 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback);
662 return; 623 return;
663 } 624 }
664 625
665 if (status != SERVICE_WORKER_OK) { 626 if (status != SERVICE_WORKER_OK) {
666 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " 627 LOG(ERROR) << "BackgroundSync failed to store registration due to backend "
667 "failure."; 628 "failure.";
668 BackgroundSyncMetrics::CountRegisterFailure( 629 BackgroundSyncMetrics::CountRegisterFailure(
669 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); 630 BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
670 DisableAndClearManager(base::Bind( 631 DisableAndClearManager(
671 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, 632 base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
672 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>()))); 633 base::Passed(scoped_ptr<BackgroundSyncRegistration>())));
673 return; 634 return;
674 } 635 }
675 636
676 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = 637 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire =
677 AreOptionConditionsMet(*new_registration->options()) 638 AreOptionConditionsMet(*new_registration.options())
678 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE 639 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE
679 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; 640 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE;
680 BackgroundSyncMetrics::CountRegisterSuccess( 641 BackgroundSyncMetrics::CountRegisterSuccess(
681 registration_could_fire, 642 registration_could_fire,
682 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE); 643 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE);
683 644
684 FireReadyEvents(); 645 FireReadyEvents();
646
685 base::ThreadTaskRunnerHandle::Get()->PostTask( 647 base::ThreadTaskRunnerHandle::Get()->PostTask(
686 FROM_HERE, 648 FROM_HERE,
687 base::Bind( 649 base::Bind(callback, BACKGROUND_SYNC_STATUS_OK,
688 callback, BACKGROUND_SYNC_STATUS_OK, 650 base::Passed(make_scoped_ptr(
689 base::Passed(CreateRegistrationHandle(new_registration_ref.get())))); 651 new BackgroundSyncRegistration(new_registration)))));
690 } 652 }
691 653
692 void BackgroundSyncManager::RemoveActiveRegistration( 654 void BackgroundSyncManager::RemoveActiveRegistration(
693 int64_t sw_registration_id, 655 int64_t sw_registration_id,
694 const RegistrationKey& registration_key) { 656 const RegistrationKey& registration_key) {
695 DCHECK_CURRENTLY_ON(BrowserThread::IO); 657 DCHECK_CURRENTLY_ON(BrowserThread::IO);
696 DCHECK(LookupActiveRegistration(sw_registration_id, registration_key)); 658 DCHECK(LookupActiveRegistration(sw_registration_id, registration_key));
697 659
698 BackgroundSyncRegistrations* registrations = 660 BackgroundSyncRegistrations* registrations =
699 &active_registrations_[sw_registration_id]; 661 &active_registrations_[sw_registration_id];
700 662
701 registrations->registration_map.erase(registration_key); 663 registrations->registration_map.erase(registration_key);
702 } 664 }
703 665
704 void BackgroundSyncManager::AddActiveRegistration( 666 void BackgroundSyncManager::AddActiveRegistration(
705 int64_t sw_registration_id, 667 int64_t sw_registration_id,
706 const GURL& origin, 668 const GURL& origin,
707 const scoped_refptr<RefCountedRegistration>& sync_registration) { 669 const BackgroundSyncRegistration& sync_registration) {
708 DCHECK_CURRENTLY_ON(BrowserThread::IO); 670 DCHECK_CURRENTLY_ON(BrowserThread::IO);
709 DCHECK(sync_registration->value()->IsValid()); 671 DCHECK(sync_registration.IsValid());
710 672
711 BackgroundSyncRegistrations* registrations = 673 BackgroundSyncRegistrations* registrations =
712 &active_registrations_[sw_registration_id]; 674 &active_registrations_[sw_registration_id];
713 registrations->origin = origin; 675 registrations->origin = origin;
714 676
715 RegistrationKey registration_key(*sync_registration->value()); 677 RegistrationKey registration_key(sync_registration);
716 registrations->registration_map[registration_key] = sync_registration; 678 registrations->registration_map[registration_key] = sync_registration;
717 } 679 }
718 680
719 void BackgroundSyncManager::StoreDataInBackend( 681 void BackgroundSyncManager::StoreDataInBackend(
720 int64_t sw_registration_id, 682 int64_t sw_registration_id,
721 const GURL& origin, 683 const GURL& origin,
722 const std::string& backend_key, 684 const std::string& backend_key,
723 const std::string& data, 685 const std::string& data,
724 const ServiceWorkerStorage::StatusCallback& callback) { 686 const ServiceWorkerStorage::StatusCallback& callback) {
725 DCHECK_CURRENTLY_ON(BrowserThread::IO); 687 DCHECK_CURRENTLY_ON(BrowserThread::IO);
726 688
727 service_worker_context_->StoreRegistrationUserData( 689 service_worker_context_->StoreRegistrationUserData(
728 sw_registration_id, origin, backend_key, data, callback); 690 sw_registration_id, origin, backend_key, data, callback);
729 } 691 }
730 692
731 void BackgroundSyncManager::GetDataFromBackend( 693 void BackgroundSyncManager::GetDataFromBackend(
732 const std::string& backend_key, 694 const std::string& backend_key,
733 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& 695 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback&
734 callback) { 696 callback) {
735 DCHECK_CURRENTLY_ON(BrowserThread::IO); 697 DCHECK_CURRENTLY_ON(BrowserThread::IO);
736 698
737 service_worker_context_->GetUserDataForAllRegistrations(backend_key, 699 service_worker_context_->GetUserDataForAllRegistrations(backend_key,
738 callback); 700 callback);
739 } 701 }
740 702
741 void BackgroundSyncManager::DispatchSyncEvent( 703 void BackgroundSyncManager::DispatchSyncEvent(
742 BackgroundSyncRegistrationHandle::HandleId handle_id, 704 const std::string& tag,
743 const scoped_refptr<ServiceWorkerVersion>& active_version, 705 const scoped_refptr<ServiceWorkerVersion>& active_version,
744 BackgroundSyncEventLastChance last_chance, 706 BackgroundSyncEventLastChance last_chance,
745 const ServiceWorkerVersion::StatusCallback& callback) { 707 const ServiceWorkerVersion::StatusCallback& callback) {
746 DCHECK_CURRENTLY_ON(BrowserThread::IO); 708 DCHECK_CURRENTLY_ON(BrowserThread::IO);
747 DCHECK(active_version); 709 DCHECK(active_version);
748 710
749 if (active_version->running_status() != ServiceWorkerVersion::RUNNING) { 711 if (active_version->running_status() != ServiceWorkerVersion::RUNNING) {
750 active_version->RunAfterStartWorker( 712 active_version->RunAfterStartWorker(
751 base::Bind(&BackgroundSyncManager::DispatchSyncEvent, 713 base::Bind(&BackgroundSyncManager::DispatchSyncEvent,
752 weak_ptr_factory_.GetWeakPtr(), handle_id, active_version, 714 weak_ptr_factory_.GetWeakPtr(), tag, active_version,
753 last_chance, callback), 715 last_chance, callback),
754 callback); 716 callback);
755 return; 717 return;
756 } 718 }
757 719
758 int request_id = active_version->StartRequestWithCustomTimeout( 720 int request_id = active_version->StartRequestWithCustomTimeout(
759 ServiceWorkerMetrics::EventType::SYNC, callback, 721 ServiceWorkerMetrics::EventType::SYNC, callback,
760 parameters_->max_sync_event_duration, 722 parameters_->max_sync_event_duration,
761 ServiceWorkerVersion::CONTINUE_ON_TIMEOUT); 723 ServiceWorkerVersion::CONTINUE_ON_TIMEOUT);
762 base::WeakPtr<BackgroundSyncServiceClient> client = 724 base::WeakPtr<BackgroundSyncServiceClient> client =
763 active_version->GetMojoServiceForRequest<BackgroundSyncServiceClient>( 725 active_version->GetMojoServiceForRequest<BackgroundSyncServiceClient>(
764 request_id); 726 request_id);
765 727
766 // The ServiceWorkerVersion doesn't know when the client (javascript) is done
767 // with the registration so don't give it a BackgroundSyncRegistrationHandle.
768 // Once the render process gets the handle_id it can create its own handle
769 // (with a new unique handle id).
770 client->Sync( 728 client->Sync(
771 handle_id, last_chance, 729 tag, last_chance,
772 base::Bind(&OnSyncEventFinished, active_version, request_id, callback)); 730 base::Bind(&OnSyncEventFinished, active_version, request_id, callback));
773 } 731 }
774 732
775 void BackgroundSyncManager::ScheduleDelayedTask(const base::Closure& callback, 733 void BackgroundSyncManager::ScheduleDelayedTask(const base::Closure& callback,
776 base::TimeDelta delay) { 734 base::TimeDelta delay) {
777 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, callback, 735 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, callback,
778 delay); 736 delay);
779 } 737 }
780 738
781 void BackgroundSyncManager::HasMainFrameProviderHost( 739 void BackgroundSyncManager::HasMainFrameProviderHost(
782 const GURL& origin, 740 const GURL& origin,
783 const BoolCallback& callback) { 741 const BoolCallback& callback) {
784 service_worker_context_->HasMainFrameProviderHost(origin, callback); 742 service_worker_context_->HasMainFrameProviderHost(origin, callback);
785 } 743 }
786 744
787 scoped_ptr<BackgroundSyncRegistrationHandle>
788 BackgroundSyncManager::CreateRegistrationHandle(
789 const scoped_refptr<RefCountedRegistration>& registration) {
790 scoped_refptr<RefCountedRegistration>* ptr =
791 new scoped_refptr<RefCountedRegistration>(registration);
792
793 // Registration handles have unique handle ids. The handle id maps to an
794 // internal RefCountedRegistration (which has the persistent registration id)
795 // via
796 // registration_reference_ids_.
797 BackgroundSyncRegistrationHandle::HandleId handle_id =
798 registration_handle_ids_.Add(ptr);
799
800 return make_scoped_ptr(new BackgroundSyncRegistrationHandle(
801 weak_ptr_factory_.GetWeakPtr(), handle_id));
802 }
803
804 BackgroundSyncRegistration* BackgroundSyncManager::GetRegistrationForHandle(
805 BackgroundSyncRegistrationHandle::HandleId handle_id) const {
806 scoped_refptr<RefCountedRegistration>* ref_registration =
807 registration_handle_ids_.Lookup(handle_id);
808 if (!ref_registration)
809 return nullptr;
810 return (*ref_registration)->value();
811 }
812
813 void BackgroundSyncManager::ReleaseRegistrationHandle(
814 BackgroundSyncRegistrationHandle::HandleId handle_id) {
815 DCHECK_CURRENTLY_ON(BrowserThread::IO);
816 DCHECK(registration_handle_ids_.Lookup(handle_id));
817 registration_handle_ids_.Remove(handle_id);
818 }
819
820 void BackgroundSyncManager::GetRegistrationsImpl( 745 void BackgroundSyncManager::GetRegistrationsImpl(
821 int64_t sw_registration_id, 746 int64_t sw_registration_id,
822 const StatusAndRegistrationsCallback& callback) { 747 const StatusAndRegistrationsCallback& callback) {
823 DCHECK_CURRENTLY_ON(BrowserThread::IO); 748 DCHECK_CURRENTLY_ON(BrowserThread::IO);
824 749
825 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> out_registrations( 750 scoped_ptr<ScopedVector<BackgroundSyncRegistration>> out_registrations(
826 new ScopedVector<BackgroundSyncRegistrationHandle>()); 751 new ScopedVector<BackgroundSyncRegistration>());
827 752
828 if (disabled_) { 753 if (disabled_) {
829 base::ThreadTaskRunnerHandle::Get()->PostTask( 754 base::ThreadTaskRunnerHandle::Get()->PostTask(
830 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, 755 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
831 base::Passed(std::move(out_registrations)))); 756 base::Passed(std::move(out_registrations))));
832 return; 757 return;
833 } 758 }
834 759
835 SWIdToRegistrationsMap::iterator it = 760 SWIdToRegistrationsMap::iterator it =
836 active_registrations_.find(sw_registration_id); 761 active_registrations_.find(sw_registration_id);
837 762
838 if (it != active_registrations_.end()) { 763 if (it != active_registrations_.end()) {
839 const BackgroundSyncRegistrations& registrations = it->second; 764 const BackgroundSyncRegistrations& registrations = it->second;
840 for (const auto& tag_and_registration : registrations.registration_map) { 765 for (const auto& tag_and_registration : registrations.registration_map) {
841 RefCountedRegistration* registration = tag_and_registration.second.get(); 766 const BackgroundSyncRegistration& registration =
842 out_registrations->push_back( 767 tag_and_registration.second;
843 CreateRegistrationHandle(registration).release()); 768 BackgroundSyncRegistration* out_registration =
769 new BackgroundSyncRegistration(registration);
770 out_registrations->push_back(out_registration);
844 } 771 }
845 } 772 }
846 773
847 base::ThreadTaskRunnerHandle::Get()->PostTask( 774 base::ThreadTaskRunnerHandle::Get()->PostTask(
848 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, 775 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK,
849 base::Passed(std::move(out_registrations)))); 776 base::Passed(std::move(out_registrations))));
850 } 777 }
851 778
852 bool BackgroundSyncManager::AreOptionConditionsMet( 779 bool BackgroundSyncManager::AreOptionConditionsMet(
853 const BackgroundSyncRegistrationOptions& options) { 780 const BackgroundSyncRegistrationOptions& options) {
(...skipping 15 matching lines...) Expand all
869 } 796 }
870 797
871 void BackgroundSyncManager::RunInBackgroundIfNecessary() { 798 void BackgroundSyncManager::RunInBackgroundIfNecessary() {
872 DCHECK_CURRENTLY_ON(BrowserThread::IO); 799 DCHECK_CURRENTLY_ON(BrowserThread::IO);
873 base::TimeDelta soonest_wakeup_delta = base::TimeDelta::Max(); 800 base::TimeDelta soonest_wakeup_delta = base::TimeDelta::Max();
874 801
875 for (const auto& sw_id_and_registrations : active_registrations_) { 802 for (const auto& sw_id_and_registrations : active_registrations_) {
876 for (const auto& key_and_registration : 803 for (const auto& key_and_registration :
877 sw_id_and_registrations.second.registration_map) { 804 sw_id_and_registrations.second.registration_map) {
878 const BackgroundSyncRegistration& registration = 805 const BackgroundSyncRegistration& registration =
879 *key_and_registration.second->value(); 806 key_and_registration.second;
880 if (registration.sync_state() == BackgroundSyncState::PENDING) { 807 if (registration.sync_state() == BackgroundSyncState::PENDING) {
881 if (clock_->Now() >= registration.delay_until()) { 808 if (clock_->Now() >= registration.delay_until()) {
882 soonest_wakeup_delta = base::TimeDelta(); 809 soonest_wakeup_delta = base::TimeDelta();
883 } else { 810 } else {
884 base::TimeDelta delay_delta = 811 base::TimeDelta delay_delta =
885 registration.delay_until() - clock_->Now(); 812 registration.delay_until() - clock_->Now();
886 if (delay_delta < soonest_wakeup_delta) 813 if (delay_delta < soonest_wakeup_delta)
887 soonest_wakeup_delta = delay_delta; 814 soonest_wakeup_delta = delay_delta;
888 } 815 }
889 } 816 }
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
935 return; 862 return;
936 } 863 }
937 864
938 // Find the registrations that are ready to run. 865 // Find the registrations that are ready to run.
939 std::vector<std::pair<int64_t, RegistrationKey>> sw_id_and_keys_to_fire; 866 std::vector<std::pair<int64_t, RegistrationKey>> sw_id_and_keys_to_fire;
940 867
941 for (auto& sw_id_and_registrations : active_registrations_) { 868 for (auto& sw_id_and_registrations : active_registrations_) {
942 const int64_t service_worker_id = sw_id_and_registrations.first; 869 const int64_t service_worker_id = sw_id_and_registrations.first;
943 for (auto& key_and_registration : 870 for (auto& key_and_registration :
944 sw_id_and_registrations.second.registration_map) { 871 sw_id_and_registrations.second.registration_map) {
945 BackgroundSyncRegistration* registration = 872 BackgroundSyncRegistration* registration = &key_and_registration.second;
946 key_and_registration.second->value();
947 if (IsRegistrationReadyToFire(*registration)) { 873 if (IsRegistrationReadyToFire(*registration)) {
948 sw_id_and_keys_to_fire.push_back( 874 sw_id_and_keys_to_fire.push_back(
949 std::make_pair(service_worker_id, key_and_registration.first)); 875 std::make_pair(service_worker_id, key_and_registration.first));
950 // The state change is not saved to persistent storage because 876 // The state change is not saved to persistent storage because
951 // if the sync event is killed mid-sync then it should return to 877 // if the sync event is killed mid-sync then it should return to
952 // SYNC_STATE_PENDING. 878 // SYNC_STATE_PENDING.
953 registration->set_sync_state(BackgroundSyncState::FIRING); 879 registration->set_sync_state(BackgroundSyncState::FIRING);
954 } 880 }
955 } 881 }
956 } 882 }
(...skipping 15 matching lines...) Expand all
972 weak_ptr_factory_.GetWeakPtr(), callback)); 898 weak_ptr_factory_.GetWeakPtr(), callback));
973 899
974 // Record the total time taken after all events have run to completion. 900 // Record the total time taken after all events have run to completion.
975 base::Closure events_completed_barrier_closure = 901 base::Closure events_completed_barrier_closure =
976 base::BarrierClosure(sw_id_and_keys_to_fire.size(), 902 base::BarrierClosure(sw_id_and_keys_to_fire.size(),
977 base::Bind(&OnAllSyncEventsCompleted, start_time, 903 base::Bind(&OnAllSyncEventsCompleted, start_time,
978 sw_id_and_keys_to_fire.size())); 904 sw_id_and_keys_to_fire.size()));
979 905
980 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) { 906 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) {
981 int64_t service_worker_id = sw_id_and_key.first; 907 int64_t service_worker_id = sw_id_and_key.first;
982 const RefCountedRegistration* registration = 908 const BackgroundSyncRegistration* registration =
983 LookupActiveRegistration(service_worker_id, sw_id_and_key.second); 909 LookupActiveRegistration(service_worker_id, sw_id_and_key.second);
984 DCHECK(registration); 910 DCHECK(registration);
985 911
986 service_worker_context_->FindReadyRegistrationForId( 912 service_worker_context_->FindReadyRegistrationForId(
987 service_worker_id, active_registrations_[service_worker_id].origin, 913 service_worker_id, active_registrations_[service_worker_id].origin,
988 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration, 914 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration,
989 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second, 915 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second,
990 registration->value()->id(), events_fired_barrier_closure, 916 registration->id(), events_fired_barrier_closure,
991 events_completed_barrier_closure)); 917 events_completed_barrier_closure));
992 } 918 }
993 } 919 }
994 920
995 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( 921 void BackgroundSyncManager::FireReadyEventsDidFindRegistration(
996 const RegistrationKey& registration_key, 922 const RegistrationKey& registration_key,
997 BackgroundSyncRegistration::RegistrationId registration_id, 923 BackgroundSyncRegistration::RegistrationId registration_id,
998 const base::Closure& event_fired_callback, 924 const base::Closure& event_fired_callback,
999 const base::Closure& event_completed_callback, 925 const base::Closure& event_completed_callback,
1000 ServiceWorkerStatusCode service_worker_status, 926 ServiceWorkerStatusCode service_worker_status,
1001 const scoped_refptr<ServiceWorkerRegistration>& 927 const scoped_refptr<ServiceWorkerRegistration>&
1002 service_worker_registration) { 928 service_worker_registration) {
1003 DCHECK_CURRENTLY_ON(BrowserThread::IO); 929 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1004 if (service_worker_status != SERVICE_WORKER_OK) { 930 if (service_worker_status != SERVICE_WORKER_OK) {
1005 base::ThreadTaskRunnerHandle::Get()->PostTask( 931 base::ThreadTaskRunnerHandle::Get()->PostTask(
1006 FROM_HERE, base::Bind(event_fired_callback)); 932 FROM_HERE, base::Bind(event_fired_callback));
1007 base::ThreadTaskRunnerHandle::Get()->PostTask( 933 base::ThreadTaskRunnerHandle::Get()->PostTask(
1008 FROM_HERE, base::Bind(event_completed_callback)); 934 FROM_HERE, base::Bind(event_completed_callback));
1009 return; 935 return;
1010 } 936 }
1011 937
1012 RefCountedRegistration* registration = LookupActiveRegistration( 938 BackgroundSyncRegistration* registration = LookupActiveRegistration(
1013 service_worker_registration->id(), registration_key); 939 service_worker_registration->id(), registration_key);
1014 DCHECK(registration); 940 DCHECK(registration);
1015 941
1016 // Create a handle and keep it until the sync event completes. The client can
1017 // acquire its own handle for longer-term use.
1018 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle =
1019 CreateRegistrationHandle(registration);
1020
1021 num_firing_registrations_ += 1; 942 num_firing_registrations_ += 1;
1022 943
1023 BackgroundSyncRegistrationHandle::HandleId handle_id =
1024 registration_handle->handle_id();
1025
1026 BackgroundSyncEventLastChance last_chance = 944 BackgroundSyncEventLastChance last_chance =
1027 registration->value()->num_attempts() == 945 registration->num_attempts() == parameters_->max_sync_attempts - 1
1028 parameters_->max_sync_attempts - 1
1029 ? BackgroundSyncEventLastChance::IS_LAST_CHANCE 946 ? BackgroundSyncEventLastChance::IS_LAST_CHANCE
1030 : BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE; 947 : BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE;
1031 948
1032 HasMainFrameProviderHost( 949 HasMainFrameProviderHost(
1033 service_worker_registration->pattern().GetOrigin(), 950 service_worker_registration->pattern().GetOrigin(),
1034 base::Bind(&BackgroundSyncMetrics::RecordEventStarted)); 951 base::Bind(&BackgroundSyncMetrics::RecordEventStarted));
1035 952
1036 DispatchSyncEvent( 953 DispatchSyncEvent(
1037 handle_id, service_worker_registration->active_version(), last_chance, 954 registration->options()->tag,
955 service_worker_registration->active_version(), last_chance,
1038 base::Bind(&BackgroundSyncManager::EventComplete, 956 base::Bind(&BackgroundSyncManager::EventComplete,
1039 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, 957 weak_ptr_factory_.GetWeakPtr(), service_worker_registration,
1040 service_worker_registration->id(), 958 service_worker_registration->id(), registration_key,
1041 base::Passed(std::move(registration_handle)),
1042 event_completed_callback)); 959 event_completed_callback));
1043 960
1044 base::ThreadTaskRunnerHandle::Get()->PostTask( 961 base::ThreadTaskRunnerHandle::Get()->PostTask(
1045 FROM_HERE, base::Bind(event_fired_callback)); 962 FROM_HERE, base::Bind(event_fired_callback));
1046 } 963 }
1047 964
1048 void BackgroundSyncManager::FireReadyEventsAllEventsFiring( 965 void BackgroundSyncManager::FireReadyEventsAllEventsFiring(
1049 const base::Closure& callback) { 966 const base::Closure& callback) {
1050 DCHECK_CURRENTLY_ON(BrowserThread::IO); 967 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1051 968
1052 RunInBackgroundIfNecessary(); 969 RunInBackgroundIfNecessary();
1053 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 970 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
1054 base::Bind(callback)); 971 base::Bind(callback));
1055 } 972 }
1056 973
1057 // |service_worker_registration| is just to keep the registration alive 974 // |service_worker_registration| is just to keep the registration alive
1058 // while the event is firing. 975 // while the event is firing.
1059 void BackgroundSyncManager::EventComplete( 976 void BackgroundSyncManager::EventComplete(
1060 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, 977 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration,
1061 int64_t service_worker_id, 978 int64_t service_worker_id,
1062 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, 979 const RegistrationKey& registration_key,
1063 const base::Closure& callback, 980 const base::Closure& callback,
1064 ServiceWorkerStatusCode status_code) { 981 ServiceWorkerStatusCode status_code) {
1065 DCHECK_CURRENTLY_ON(BrowserThread::IO); 982 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1066 983
1067 if (disabled_) { 984 if (disabled_) {
1068 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 985 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
1069 base::Bind(callback)); 986 base::Bind(callback));
1070 return; 987 return;
1071 } 988 }
1072 989
1073 op_scheduler_.ScheduleOperation(base::Bind( 990 op_scheduler_.ScheduleOperation(base::Bind(
1074 &BackgroundSyncManager::EventCompleteImpl, weak_ptr_factory_.GetWeakPtr(), 991 &BackgroundSyncManager::EventCompleteImpl, weak_ptr_factory_.GetWeakPtr(),
1075 service_worker_id, base::Passed(std::move(registration_handle)), 992 service_worker_id, registration_key, status_code,
1076 status_code, MakeClosureCompletion(callback))); 993 MakeClosureCompletion(callback)));
1077 } 994 }
1078 995
1079 void BackgroundSyncManager::EventCompleteImpl( 996 void BackgroundSyncManager::EventCompleteImpl(
1080 int64_t service_worker_id, 997 int64_t service_worker_id,
1081 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, 998 const RegistrationKey& registration_key,
1082 ServiceWorkerStatusCode status_code, 999 ServiceWorkerStatusCode status_code,
1083 const base::Closure& callback) { 1000 const base::Closure& callback) {
1084 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1001 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1085 1002
1086 if (disabled_) { 1003 if (disabled_) {
1087 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 1004 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
1088 base::Bind(callback)); 1005 base::Bind(callback));
1089 return; 1006 return;
1090 } 1007 }
1091 1008
1009 num_firing_registrations_ -= 1;
1010
1092 BackgroundSyncRegistration* registration = 1011 BackgroundSyncRegistration* registration =
1093 registration_handle->registration(); 1012 LookupActiveRegistration(service_worker_id, registration_key);
1094 DCHECK(registration); 1013 if (!registration) {
1014 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
1015 base::Bind(callback));
1016 return;
1017 }
1018
1095 DCHECK_NE(BackgroundSyncState::PENDING, registration->sync_state()); 1019 DCHECK_NE(BackgroundSyncState::PENDING, registration->sync_state());
1096 1020
1097 registration->set_num_attempts(registration->num_attempts() + 1); 1021 registration->set_num_attempts(registration->num_attempts() + 1);
1098 1022
1099 num_firing_registrations_ -= 1;
1100
1101 // The event ran to completion, we should count it, no matter what happens 1023 // The event ran to completion, we should count it, no matter what happens
1102 // from here. 1024 // from here.
1103 ServiceWorkerRegistration* sw_registration = 1025 ServiceWorkerRegistration* sw_registration =
1104 service_worker_context_->GetLiveRegistration(service_worker_id); 1026 service_worker_context_->GetLiveRegistration(service_worker_id);
1105 if (sw_registration) { 1027 if (sw_registration) {
1106 HasMainFrameProviderHost( 1028 HasMainFrameProviderHost(
1107 sw_registration->pattern().GetOrigin(), 1029 sw_registration->pattern().GetOrigin(),
1108 base::Bind(&BackgroundSyncMetrics::RecordEventResult, 1030 base::Bind(&BackgroundSyncMetrics::RecordEventResult,
1109 status_code == SERVICE_WORKER_OK)); 1031 status_code == SERVICE_WORKER_OK));
1110 } 1032 }
(...skipping 12 matching lines...) Expand all
1123 registration->set_sync_state(BackgroundSyncState::PENDING); 1045 registration->set_sync_state(BackgroundSyncState::PENDING);
1124 registration->set_delay_until(clock_->Now() + 1046 registration->set_delay_until(clock_->Now() +
1125 parameters_->initial_retry_delay * 1047 parameters_->initial_retry_delay *
1126 pow(parameters_->retry_delay_factor, 1048 pow(parameters_->retry_delay_factor,
1127 registration->num_attempts() - 1)); 1049 registration->num_attempts() - 1));
1128 registration_completed = false; 1050 registration_completed = false;
1129 } 1051 }
1130 1052
1131 if (registration_completed) { 1053 if (registration_completed) {
1132 RegistrationKey key(*registration); 1054 RegistrationKey key(*registration);
1133 RefCountedRegistration* active_registration = 1055 BackgroundSyncRegistration* active_registration =
1134 LookupActiveRegistration(service_worker_id, key); 1056 LookupActiveRegistration(service_worker_id, key);
1135 if (active_registration && 1057 if (active_registration &&
1136 active_registration->value()->id() == registration->id()) { 1058 active_registration->id() == registration->id()) {
1137 RemoveActiveRegistration(service_worker_id, key); 1059 RemoveActiveRegistration(service_worker_id, key);
1138 } 1060 }
1139 } 1061 }
1140 1062
1141 StoreRegistrations( 1063 StoreRegistrations(
1142 service_worker_id, 1064 service_worker_id,
1143 base::Bind(&BackgroundSyncManager::EventCompleteDidStore, 1065 base::Bind(&BackgroundSyncManager::EventCompleteDidStore,
1144 weak_ptr_factory_.GetWeakPtr(), service_worker_id, callback)); 1066 weak_ptr_factory_.GetWeakPtr(), service_worker_id, callback));
1145 } 1067 }
1146 1068
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1222 Params... parameters) { 1144 Params... parameters) {
1223 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1145 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1224 1146
1225 callback.Run(parameters...); 1147 callback.Run(parameters...);
1226 op_scheduler_.CompleteOperationAndRunNext(); 1148 op_scheduler_.CompleteOperationAndRunNext();
1227 } 1149 }
1228 1150
1229 void BackgroundSyncManager::CompleteStatusAndRegistrationCallback( 1151 void BackgroundSyncManager::CompleteStatusAndRegistrationCallback(
1230 StatusAndRegistrationCallback callback, 1152 StatusAndRegistrationCallback callback,
1231 BackgroundSyncStatus status, 1153 BackgroundSyncStatus status,
1232 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle) { 1154 scoped_ptr<BackgroundSyncRegistration> registration) {
1233 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1155 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1234 1156
1235 callback.Run(status, std::move(registration_handle)); 1157 callback.Run(status, std::move(registration));
1236 op_scheduler_.CompleteOperationAndRunNext(); 1158 op_scheduler_.CompleteOperationAndRunNext();
1237 } 1159 }
1238 1160
1239 void BackgroundSyncManager::CompleteStatusAndRegistrationsCallback( 1161 void BackgroundSyncManager::CompleteStatusAndRegistrationsCallback(
1240 StatusAndRegistrationsCallback callback, 1162 StatusAndRegistrationsCallback callback,
1241 BackgroundSyncStatus status, 1163 BackgroundSyncStatus status,
1242 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> 1164 scoped_ptr<ScopedVector<BackgroundSyncRegistration>> registrations) {
1243 registration_handles) {
1244 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1165 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1245 1166
1246 callback.Run(status, std::move(registration_handles)); 1167 callback.Run(status, std::move(registrations));
1247 op_scheduler_.CompleteOperationAndRunNext(); 1168 op_scheduler_.CompleteOperationAndRunNext();
1248 } 1169 }
1249 1170
1250 base::Closure BackgroundSyncManager::MakeEmptyCompletion() { 1171 base::Closure BackgroundSyncManager::MakeEmptyCompletion() {
1251 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1172 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1252 1173
1253 return MakeClosureCompletion(base::Bind(base::DoNothing)); 1174 return MakeClosureCompletion(base::Bind(base::DoNothing));
1254 } 1175 }
1255 1176
1256 base::Closure BackgroundSyncManager::MakeClosureCompletion( 1177 base::Closure BackgroundSyncManager::MakeClosureCompletion(
(...skipping 29 matching lines...) Expand all
1286 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { 1207 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) {
1287 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1208 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1288 1209
1289 return base::Bind( 1210 return base::Bind(
1290 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, 1211 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback,
1291 BackgroundSyncStatus>, 1212 BackgroundSyncStatus>,
1292 weak_ptr_factory_.GetWeakPtr(), callback); 1213 weak_ptr_factory_.GetWeakPtr(), callback);
1293 } 1214 }
1294 1215
1295 } // namespace content 1216 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698