Chromium Code Reviews| 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" |
| 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 ®istration_; } | |
| 39 const BackgroundSyncRegistration* value() const { return ®istration_; } | |
| 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 Loading... | |
| 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 Loading... | |
| 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 ®istrations->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 Loading... | |
| 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 |
| 472 scoped_ptr<BackgroundSyncRegistration> out_registration( | |
| 473 new BackgroundSyncRegistration(*existing_registration)); | |
| 474 | |
| 505 base::ThreadTaskRunnerHandle::Get()->PostTask( | 475 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 506 FROM_HERE, | 476 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, |
| 507 base::Bind( | 477 base::Passed(std::move(out_registration)))); |
| 508 callback, BACKGROUND_SYNC_STATUS_OK, | |
| 509 base::Passed(CreateRegistrationHandle(existing_registration_ref)))); | |
| 510 return; | 478 return; |
| 511 } | 479 } |
| 512 | 480 |
| 513 scoped_refptr<RefCountedRegistration> new_ref_registration( | 481 BackgroundSyncRegistration new_registration; |
| 514 new RefCountedRegistration()); | |
| 515 BackgroundSyncRegistration* new_registration = new_ref_registration->value(); | |
| 516 | 482 |
| 517 *new_registration->options() = options; | 483 *new_registration.options() = options; |
| 518 | 484 |
| 519 BackgroundSyncRegistrations* registrations = | 485 BackgroundSyncRegistrations* registrations = |
| 520 &active_registrations_[sw_registration_id]; | 486 &active_registrations_[sw_registration_id]; |
| 521 new_registration->set_id(registrations->next_id++); | 487 new_registration.set_id(registrations->next_id++); |
| 522 | 488 |
| 523 AddActiveRegistration(sw_registration_id, | 489 AddActiveRegistration(sw_registration_id, |
| 524 sw_registration->pattern().GetOrigin(), | 490 sw_registration->pattern().GetOrigin(), |
| 525 new_ref_registration); | 491 new_registration); |
| 526 | 492 |
| 527 StoreRegistrations( | 493 StoreRegistrations( |
| 528 sw_registration_id, | 494 sw_registration_id, |
| 529 base::Bind(&BackgroundSyncManager::RegisterDidStore, | 495 base::Bind(&BackgroundSyncManager::RegisterDidStore, |
| 530 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 496 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
| 531 new_ref_registration, callback)); | 497 new_registration, callback)); |
| 532 } | 498 } |
| 533 | 499 |
| 534 void BackgroundSyncManager::DisableAndClearManager( | 500 void BackgroundSyncManager::DisableAndClearManager( |
| 535 const base::Closure& callback) { | 501 const base::Closure& callback) { |
| 536 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 502 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 537 | 503 |
| 538 if (disabled_) { | 504 if (disabled_) { |
| 539 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 505 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 540 base::Bind(callback)); | 506 base::Bind(callback)); |
| 541 return; | 507 return; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 580 void BackgroundSyncManager::DisableAndClearManagerClearedOne( | 546 void BackgroundSyncManager::DisableAndClearManagerClearedOne( |
| 581 const base::Closure& barrier_closure, | 547 const base::Closure& barrier_closure, |
| 582 ServiceWorkerStatusCode status) { | 548 ServiceWorkerStatusCode status) { |
| 583 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 549 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 584 | 550 |
| 585 // The status doesn't matter at this point, there is nothing else to be done. | 551 // The status doesn't matter at this point, there is nothing else to be done. |
| 586 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 552 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 587 base::Bind(barrier_closure)); | 553 base::Bind(barrier_closure)); |
| 588 } | 554 } |
| 589 | 555 |
| 590 BackgroundSyncManager::RefCountedRegistration* | 556 BackgroundSyncRegistration* BackgroundSyncManager::LookupActiveRegistration( |
| 591 BackgroundSyncManager::LookupActiveRegistration( | |
| 592 int64_t sw_registration_id, | 557 int64_t sw_registration_id, |
| 593 const RegistrationKey& registration_key) { | 558 const RegistrationKey& registration_key) { |
| 594 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 559 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 595 | 560 |
| 596 SWIdToRegistrationsMap::iterator it = | 561 SWIdToRegistrationsMap::iterator it = |
| 597 active_registrations_.find(sw_registration_id); | 562 active_registrations_.find(sw_registration_id); |
| 598 if (it == active_registrations_.end()) | 563 if (it == active_registrations_.end()) |
| 599 return nullptr; | 564 return nullptr; |
| 600 | 565 |
| 601 BackgroundSyncRegistrations& registrations = it->second; | 566 BackgroundSyncRegistrations& registrations = it->second; |
| 602 DCHECK_LE(BackgroundSyncRegistration::kInitialId, registrations.next_id); | 567 DCHECK_LE(BackgroundSyncRegistration::kInitialId, registrations.next_id); |
| 603 DCHECK(!registrations.origin.is_empty()); | 568 DCHECK(!registrations.origin.is_empty()); |
| 604 | 569 |
| 605 auto key_and_registration_iter = | 570 auto key_and_registration_iter = |
| 606 registrations.registration_map.find(registration_key); | 571 registrations.registration_map.find(registration_key); |
| 607 if (key_and_registration_iter == registrations.registration_map.end()) | 572 if (key_and_registration_iter == registrations.registration_map.end()) |
| 608 return nullptr; | 573 return nullptr; |
| 609 | 574 |
| 610 return key_and_registration_iter->second.get(); | 575 return &key_and_registration_iter->second; |
| 611 } | 576 } |
| 612 | 577 |
| 613 void BackgroundSyncManager::StoreRegistrations( | 578 void BackgroundSyncManager::StoreRegistrations( |
| 614 int64_t sw_registration_id, | 579 int64_t sw_registration_id, |
| 615 const ServiceWorkerStorage::StatusCallback& callback) { | 580 const ServiceWorkerStorage::StatusCallback& callback) { |
| 616 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 581 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 617 | 582 |
| 618 // Serialize the data. | 583 // Serialize the data. |
| 619 const BackgroundSyncRegistrations& registrations = | 584 const BackgroundSyncRegistrations& registrations = |
| 620 active_registrations_[sw_registration_id]; | 585 active_registrations_[sw_registration_id]; |
| 621 BackgroundSyncRegistrationsProto registrations_proto; | 586 BackgroundSyncRegistrationsProto registrations_proto; |
| 622 registrations_proto.set_next_registration_id(registrations.next_id); | 587 registrations_proto.set_next_registration_id(registrations.next_id); |
| 623 registrations_proto.set_origin(registrations.origin.spec()); | 588 registrations_proto.set_origin(registrations.origin.spec()); |
| 624 | 589 |
| 625 for (const auto& key_and_registration : registrations.registration_map) { | 590 for (const auto& key_and_registration : registrations.registration_map) { |
| 626 const BackgroundSyncRegistration& registration = | 591 const BackgroundSyncRegistration& registration = |
| 627 *key_and_registration.second->value(); | 592 key_and_registration.second; |
| 628 BackgroundSyncRegistrationProto* registration_proto = | 593 BackgroundSyncRegistrationProto* registration_proto = |
| 629 registrations_proto.add_registration(); | 594 registrations_proto.add_registration(); |
| 630 registration_proto->set_id(registration.id()); | 595 registration_proto->set_id(registration.id()); |
| 631 registration_proto->set_tag(registration.options()->tag); | 596 registration_proto->set_tag(registration.options()->tag); |
| 632 registration_proto->set_network_state( | 597 registration_proto->set_network_state( |
| 633 registration.options()->network_state); | 598 registration.options()->network_state); |
| 634 registration_proto->set_num_attempts(registration.num_attempts()); | 599 registration_proto->set_num_attempts(registration.num_attempts()); |
| 635 registration_proto->set_delay_until( | 600 registration_proto->set_delay_until( |
| 636 registration.delay_until().ToInternalValue()); | 601 registration.delay_until().ToInternalValue()); |
| 637 } | 602 } |
| 638 std::string serialized; | 603 std::string serialized; |
| 639 bool success = registrations_proto.SerializeToString(&serialized); | 604 bool success = registrations_proto.SerializeToString(&serialized); |
| 640 DCHECK(success); | 605 DCHECK(success); |
| 641 | 606 |
| 642 StoreDataInBackend(sw_registration_id, registrations.origin, | 607 StoreDataInBackend(sw_registration_id, registrations.origin, |
| 643 kBackgroundSyncUserDataKey, serialized, callback); | 608 kBackgroundSyncUserDataKey, serialized, callback); |
| 644 } | 609 } |
| 645 | 610 |
| 646 void BackgroundSyncManager::RegisterDidStore( | 611 void BackgroundSyncManager::RegisterDidStore( |
| 647 int64_t sw_registration_id, | 612 int64_t sw_registration_id, |
| 648 const scoped_refptr<RefCountedRegistration>& new_registration_ref, | 613 const BackgroundSyncRegistration& new_registration, |
| 649 const StatusAndRegistrationCallback& callback, | 614 const StatusAndRegistrationCallback& callback, |
| 650 ServiceWorkerStatusCode status) { | 615 ServiceWorkerStatusCode status) { |
| 651 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 616 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 652 | 617 |
| 653 const BackgroundSyncRegistration* new_registration = | |
| 654 new_registration_ref->value(); | |
| 655 | |
| 656 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { | 618 if (status == SERVICE_WORKER_ERROR_NOT_FOUND) { |
| 657 // The service worker registration is gone. | 619 // The service worker registration is gone. |
| 658 BackgroundSyncMetrics::CountRegisterFailure( | 620 BackgroundSyncMetrics::CountRegisterFailure( |
| 659 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 621 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
| 660 active_registrations_.erase(sw_registration_id); | 622 active_registrations_.erase(sw_registration_id); |
| 661 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); | 623 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
| 662 return; | 624 return; |
| 663 } | 625 } |
| 664 | 626 |
| 665 if (status != SERVICE_WORKER_OK) { | 627 if (status != SERVICE_WORKER_OK) { |
| 666 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " | 628 LOG(ERROR) << "BackgroundSync failed to store registration due to backend " |
| 667 "failure."; | 629 "failure."; |
| 668 BackgroundSyncMetrics::CountRegisterFailure( | 630 BackgroundSyncMetrics::CountRegisterFailure( |
| 669 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 631 BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
| 670 DisableAndClearManager(base::Bind( | 632 DisableAndClearManager( |
| 671 callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 633 base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
| 672 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>()))); | 634 base::Passed(scoped_ptr<BackgroundSyncRegistration>()))); |
| 673 return; | 635 return; |
| 674 } | 636 } |
| 675 | 637 |
| 676 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = | 638 BackgroundSyncMetrics::RegistrationCouldFire registration_could_fire = |
| 677 AreOptionConditionsMet(*new_registration->options()) | 639 AreOptionConditionsMet(*new_registration.options()) |
| 678 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE | 640 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
| 679 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; | 641 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
| 680 BackgroundSyncMetrics::CountRegisterSuccess( | 642 BackgroundSyncMetrics::CountRegisterSuccess( |
| 681 registration_could_fire, | 643 registration_could_fire, |
| 682 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE); | 644 BackgroundSyncMetrics::REGISTRATION_IS_NOT_DUPLICATE); |
| 683 | 645 |
| 684 FireReadyEvents(); | 646 FireReadyEvents(); |
| 647 | |
| 648 scoped_ptr<BackgroundSyncRegistration> out_registration( | |
| 649 new BackgroundSyncRegistration(new_registration)); | |
| 650 | |
| 685 base::ThreadTaskRunnerHandle::Get()->PostTask( | 651 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 686 FROM_HERE, | 652 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, |
| 687 base::Bind( | 653 base::Passed(std::move(out_registration)))); |
| 688 callback, BACKGROUND_SYNC_STATUS_OK, | |
| 689 base::Passed(CreateRegistrationHandle(new_registration_ref.get())))); | |
| 690 } | 654 } |
| 691 | 655 |
| 692 void BackgroundSyncManager::RemoveActiveRegistration( | 656 void BackgroundSyncManager::RemoveActiveRegistration( |
| 693 int64_t sw_registration_id, | 657 int64_t sw_registration_id, |
| 694 const RegistrationKey& registration_key) { | 658 const RegistrationKey& registration_key) { |
| 695 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 659 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 696 DCHECK(LookupActiveRegistration(sw_registration_id, registration_key)); | 660 DCHECK(LookupActiveRegistration(sw_registration_id, registration_key)); |
| 697 | 661 |
| 698 BackgroundSyncRegistrations* registrations = | 662 BackgroundSyncRegistrations* registrations = |
| 699 &active_registrations_[sw_registration_id]; | 663 &active_registrations_[sw_registration_id]; |
| 700 | 664 |
| 701 registrations->registration_map.erase(registration_key); | 665 registrations->registration_map.erase(registration_key); |
| 702 } | 666 } |
| 703 | 667 |
| 704 void BackgroundSyncManager::AddActiveRegistration( | 668 void BackgroundSyncManager::AddActiveRegistration( |
| 705 int64_t sw_registration_id, | 669 int64_t sw_registration_id, |
| 706 const GURL& origin, | 670 const GURL& origin, |
| 707 const scoped_refptr<RefCountedRegistration>& sync_registration) { | 671 const BackgroundSyncRegistration& sync_registration) { |
| 708 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 672 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 709 DCHECK(sync_registration->value()->IsValid()); | 673 DCHECK(sync_registration.IsValid()); |
| 710 | 674 |
| 711 BackgroundSyncRegistrations* registrations = | 675 BackgroundSyncRegistrations* registrations = |
| 712 &active_registrations_[sw_registration_id]; | 676 &active_registrations_[sw_registration_id]; |
| 713 registrations->origin = origin; | 677 registrations->origin = origin; |
| 714 | 678 |
| 715 RegistrationKey registration_key(*sync_registration->value()); | 679 RegistrationKey registration_key(sync_registration); |
| 716 registrations->registration_map[registration_key] = sync_registration; | 680 registrations->registration_map[registration_key] = sync_registration; |
| 717 } | 681 } |
| 718 | 682 |
| 719 void BackgroundSyncManager::StoreDataInBackend( | 683 void BackgroundSyncManager::StoreDataInBackend( |
| 720 int64_t sw_registration_id, | 684 int64_t sw_registration_id, |
| 721 const GURL& origin, | 685 const GURL& origin, |
| 722 const std::string& backend_key, | 686 const std::string& backend_key, |
| 723 const std::string& data, | 687 const std::string& data, |
| 724 const ServiceWorkerStorage::StatusCallback& callback) { | 688 const ServiceWorkerStorage::StatusCallback& callback) { |
| 725 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 689 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 726 | 690 |
| 727 service_worker_context_->StoreRegistrationUserData( | 691 service_worker_context_->StoreRegistrationUserData( |
| 728 sw_registration_id, origin, backend_key, data, callback); | 692 sw_registration_id, origin, backend_key, data, callback); |
| 729 } | 693 } |
| 730 | 694 |
| 731 void BackgroundSyncManager::GetDataFromBackend( | 695 void BackgroundSyncManager::GetDataFromBackend( |
| 732 const std::string& backend_key, | 696 const std::string& backend_key, |
| 733 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& | 697 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback& |
| 734 callback) { | 698 callback) { |
| 735 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 699 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 736 | 700 |
| 737 service_worker_context_->GetUserDataForAllRegistrations(backend_key, | 701 service_worker_context_->GetUserDataForAllRegistrations(backend_key, |
| 738 callback); | 702 callback); |
| 739 } | 703 } |
| 740 | 704 |
| 741 void BackgroundSyncManager::DispatchSyncEvent( | 705 void BackgroundSyncManager::DispatchSyncEvent( |
| 742 BackgroundSyncRegistrationHandle::HandleId handle_id, | 706 const std::string& tag, |
| 743 const scoped_refptr<ServiceWorkerVersion>& active_version, | 707 const scoped_refptr<ServiceWorkerVersion>& active_version, |
| 744 BackgroundSyncEventLastChance last_chance, | 708 BackgroundSyncEventLastChance last_chance, |
| 745 const ServiceWorkerVersion::StatusCallback& callback) { | 709 const ServiceWorkerVersion::StatusCallback& callback) { |
| 746 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 710 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 747 DCHECK(active_version); | 711 DCHECK(active_version); |
| 748 | 712 |
| 749 if (active_version->running_status() != ServiceWorkerVersion::RUNNING) { | 713 if (active_version->running_status() != ServiceWorkerVersion::RUNNING) { |
| 750 active_version->RunAfterStartWorker( | 714 active_version->RunAfterStartWorker( |
| 751 base::Bind(&BackgroundSyncManager::DispatchSyncEvent, | 715 base::Bind(&BackgroundSyncManager::DispatchSyncEvent, |
| 752 weak_ptr_factory_.GetWeakPtr(), handle_id, active_version, | 716 weak_ptr_factory_.GetWeakPtr(), tag, active_version, |
| 753 last_chance, callback), | 717 last_chance, callback), |
| 754 callback); | 718 callback); |
| 755 return; | 719 return; |
| 756 } | 720 } |
| 757 | 721 |
| 758 int request_id = active_version->StartRequestWithCustomTimeout( | 722 int request_id = active_version->StartRequestWithCustomTimeout( |
| 759 ServiceWorkerMetrics::EventType::SYNC, callback, | 723 ServiceWorkerMetrics::EventType::SYNC, callback, |
| 760 parameters_->max_sync_event_duration, | 724 parameters_->max_sync_event_duration, |
| 761 ServiceWorkerVersion::CONTINUE_ON_TIMEOUT); | 725 ServiceWorkerVersion::CONTINUE_ON_TIMEOUT); |
| 762 base::WeakPtr<BackgroundSyncServiceClient> client = | 726 base::WeakPtr<BackgroundSyncServiceClient> client = |
| 763 active_version->GetMojoServiceForRequest<BackgroundSyncServiceClient>( | 727 active_version->GetMojoServiceForRequest<BackgroundSyncServiceClient>( |
| 764 request_id); | 728 request_id); |
| 765 | 729 |
| 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( | 730 client->Sync( |
| 771 handle_id, last_chance, | 731 tag, last_chance, |
| 772 base::Bind(&OnSyncEventFinished, active_version, request_id, callback)); | 732 base::Bind(&OnSyncEventFinished, active_version, request_id, callback)); |
| 773 } | 733 } |
| 774 | 734 |
| 775 void BackgroundSyncManager::ScheduleDelayedTask(const base::Closure& callback, | 735 void BackgroundSyncManager::ScheduleDelayedTask(const base::Closure& callback, |
| 776 base::TimeDelta delay) { | 736 base::TimeDelta delay) { |
| 777 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, callback, | 737 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, callback, |
| 778 delay); | 738 delay); |
| 779 } | 739 } |
| 780 | 740 |
| 781 void BackgroundSyncManager::HasMainFrameProviderHost( | 741 void BackgroundSyncManager::HasMainFrameProviderHost( |
| 782 const GURL& origin, | 742 const GURL& origin, |
| 783 const BoolCallback& callback) { | 743 const BoolCallback& callback) { |
| 784 service_worker_context_->HasMainFrameProviderHost(origin, callback); | 744 service_worker_context_->HasMainFrameProviderHost(origin, callback); |
| 785 } | 745 } |
| 786 | 746 |
| 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( | 747 void BackgroundSyncManager::GetRegistrationsImpl( |
| 821 int64_t sw_registration_id, | 748 int64_t sw_registration_id, |
| 822 const StatusAndRegistrationsCallback& callback) { | 749 const StatusAndRegistrationsCallback& callback) { |
| 823 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 750 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 824 | 751 |
| 825 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> out_registrations( | 752 scoped_ptr<ScopedVector<BackgroundSyncRegistration>> out_registrations( |
| 826 new ScopedVector<BackgroundSyncRegistrationHandle>()); | 753 new ScopedVector<BackgroundSyncRegistration>()); |
| 827 | 754 |
| 828 if (disabled_) { | 755 if (disabled_) { |
| 829 base::ThreadTaskRunnerHandle::Get()->PostTask( | 756 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 830 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 757 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
| 831 base::Passed(std::move(out_registrations)))); | 758 base::Passed(std::move(out_registrations)))); |
| 832 return; | 759 return; |
| 833 } | 760 } |
| 834 | 761 |
| 835 SWIdToRegistrationsMap::iterator it = | 762 SWIdToRegistrationsMap::iterator it = |
| 836 active_registrations_.find(sw_registration_id); | 763 active_registrations_.find(sw_registration_id); |
| 837 | 764 |
| 838 if (it != active_registrations_.end()) { | 765 if (it != active_registrations_.end()) { |
| 839 const BackgroundSyncRegistrations& registrations = it->second; | 766 const BackgroundSyncRegistrations& registrations = it->second; |
| 840 for (const auto& tag_and_registration : registrations.registration_map) { | 767 for (const auto& tag_and_registration : registrations.registration_map) { |
| 841 RefCountedRegistration* registration = tag_and_registration.second.get(); | 768 const BackgroundSyncRegistration& registration = |
| 842 out_registrations->push_back( | 769 tag_and_registration.second; |
| 843 CreateRegistrationHandle(registration).release()); | 770 BackgroundSyncRegistration* out_registration = |
| 771 new BackgroundSyncRegistration(registration); | |
|
iclelland
2016/03/08 16:48:14
These are allocated here, but never released in th
jkarlin
2016/03/08 19:37:36
That weak_clear had to go, thanks!
| |
| 772 out_registrations->push_back(out_registration); | |
| 844 } | 773 } |
| 845 } | 774 } |
| 846 | 775 |
| 847 base::ThreadTaskRunnerHandle::Get()->PostTask( | 776 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 848 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, | 777 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, |
| 849 base::Passed(std::move(out_registrations)))); | 778 base::Passed(std::move(out_registrations)))); |
| 850 } | 779 } |
| 851 | 780 |
| 852 bool BackgroundSyncManager::AreOptionConditionsMet( | 781 bool BackgroundSyncManager::AreOptionConditionsMet( |
| 853 const BackgroundSyncRegistrationOptions& options) { | 782 const BackgroundSyncRegistrationOptions& options) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 869 } | 798 } |
| 870 | 799 |
| 871 void BackgroundSyncManager::RunInBackgroundIfNecessary() { | 800 void BackgroundSyncManager::RunInBackgroundIfNecessary() { |
| 872 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 801 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 873 base::TimeDelta soonest_wakeup_delta = base::TimeDelta::Max(); | 802 base::TimeDelta soonest_wakeup_delta = base::TimeDelta::Max(); |
| 874 | 803 |
| 875 for (const auto& sw_id_and_registrations : active_registrations_) { | 804 for (const auto& sw_id_and_registrations : active_registrations_) { |
| 876 for (const auto& key_and_registration : | 805 for (const auto& key_and_registration : |
| 877 sw_id_and_registrations.second.registration_map) { | 806 sw_id_and_registrations.second.registration_map) { |
| 878 const BackgroundSyncRegistration& registration = | 807 const BackgroundSyncRegistration& registration = |
| 879 *key_and_registration.second->value(); | 808 key_and_registration.second; |
| 880 if (registration.sync_state() == BackgroundSyncState::PENDING) { | 809 if (registration.sync_state() == BackgroundSyncState::PENDING) { |
| 881 if (clock_->Now() >= registration.delay_until()) { | 810 if (clock_->Now() >= registration.delay_until()) { |
| 882 soonest_wakeup_delta = base::TimeDelta(); | 811 soonest_wakeup_delta = base::TimeDelta(); |
| 883 } else { | 812 } else { |
| 884 base::TimeDelta delay_delta = | 813 base::TimeDelta delay_delta = |
| 885 registration.delay_until() - clock_->Now(); | 814 registration.delay_until() - clock_->Now(); |
| 886 if (delay_delta < soonest_wakeup_delta) | 815 if (delay_delta < soonest_wakeup_delta) |
| 887 soonest_wakeup_delta = delay_delta; | 816 soonest_wakeup_delta = delay_delta; |
| 888 } | 817 } |
| 889 } | 818 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 935 return; | 864 return; |
| 936 } | 865 } |
| 937 | 866 |
| 938 // Find the registrations that are ready to run. | 867 // Find the registrations that are ready to run. |
| 939 std::vector<std::pair<int64_t, RegistrationKey>> sw_id_and_keys_to_fire; | 868 std::vector<std::pair<int64_t, RegistrationKey>> sw_id_and_keys_to_fire; |
| 940 | 869 |
| 941 for (auto& sw_id_and_registrations : active_registrations_) { | 870 for (auto& sw_id_and_registrations : active_registrations_) { |
| 942 const int64_t service_worker_id = sw_id_and_registrations.first; | 871 const int64_t service_worker_id = sw_id_and_registrations.first; |
| 943 for (auto& key_and_registration : | 872 for (auto& key_and_registration : |
| 944 sw_id_and_registrations.second.registration_map) { | 873 sw_id_and_registrations.second.registration_map) { |
| 945 BackgroundSyncRegistration* registration = | 874 BackgroundSyncRegistration* registration = &key_and_registration.second; |
| 946 key_and_registration.second->value(); | |
| 947 if (IsRegistrationReadyToFire(*registration)) { | 875 if (IsRegistrationReadyToFire(*registration)) { |
| 948 sw_id_and_keys_to_fire.push_back( | 876 sw_id_and_keys_to_fire.push_back( |
| 949 std::make_pair(service_worker_id, key_and_registration.first)); | 877 std::make_pair(service_worker_id, key_and_registration.first)); |
| 950 // The state change is not saved to persistent storage because | 878 // The state change is not saved to persistent storage because |
| 951 // if the sync event is killed mid-sync then it should return to | 879 // if the sync event is killed mid-sync then it should return to |
| 952 // SYNC_STATE_PENDING. | 880 // SYNC_STATE_PENDING. |
| 953 registration->set_sync_state(BackgroundSyncState::FIRING); | 881 registration->set_sync_state(BackgroundSyncState::FIRING); |
| 954 } | 882 } |
| 955 } | 883 } |
| 956 } | 884 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 972 weak_ptr_factory_.GetWeakPtr(), callback)); | 900 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 973 | 901 |
| 974 // Record the total time taken after all events have run to completion. | 902 // Record the total time taken after all events have run to completion. |
| 975 base::Closure events_completed_barrier_closure = | 903 base::Closure events_completed_barrier_closure = |
| 976 base::BarrierClosure(sw_id_and_keys_to_fire.size(), | 904 base::BarrierClosure(sw_id_and_keys_to_fire.size(), |
| 977 base::Bind(&OnAllSyncEventsCompleted, start_time, | 905 base::Bind(&OnAllSyncEventsCompleted, start_time, |
| 978 sw_id_and_keys_to_fire.size())); | 906 sw_id_and_keys_to_fire.size())); |
| 979 | 907 |
| 980 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) { | 908 for (const auto& sw_id_and_key : sw_id_and_keys_to_fire) { |
| 981 int64_t service_worker_id = sw_id_and_key.first; | 909 int64_t service_worker_id = sw_id_and_key.first; |
| 982 const RefCountedRegistration* registration = | 910 const BackgroundSyncRegistration* registration = |
| 983 LookupActiveRegistration(service_worker_id, sw_id_and_key.second); | 911 LookupActiveRegistration(service_worker_id, sw_id_and_key.second); |
| 984 DCHECK(registration); | 912 DCHECK(registration); |
| 985 | 913 |
| 986 service_worker_context_->FindReadyRegistrationForId( | 914 service_worker_context_->FindReadyRegistrationForId( |
| 987 service_worker_id, active_registrations_[service_worker_id].origin, | 915 service_worker_id, active_registrations_[service_worker_id].origin, |
| 988 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration, | 916 base::Bind(&BackgroundSyncManager::FireReadyEventsDidFindRegistration, |
| 989 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second, | 917 weak_ptr_factory_.GetWeakPtr(), sw_id_and_key.second, |
| 990 registration->value()->id(), events_fired_barrier_closure, | 918 registration->id(), events_fired_barrier_closure, |
| 991 events_completed_barrier_closure)); | 919 events_completed_barrier_closure)); |
| 992 } | 920 } |
| 993 } | 921 } |
| 994 | 922 |
| 995 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( | 923 void BackgroundSyncManager::FireReadyEventsDidFindRegistration( |
| 996 const RegistrationKey& registration_key, | 924 const RegistrationKey& registration_key, |
| 997 BackgroundSyncRegistration::RegistrationId registration_id, | 925 BackgroundSyncRegistration::RegistrationId registration_id, |
| 998 const base::Closure& event_fired_callback, | 926 const base::Closure& event_fired_callback, |
| 999 const base::Closure& event_completed_callback, | 927 const base::Closure& event_completed_callback, |
| 1000 ServiceWorkerStatusCode service_worker_status, | 928 ServiceWorkerStatusCode service_worker_status, |
| 1001 const scoped_refptr<ServiceWorkerRegistration>& | 929 const scoped_refptr<ServiceWorkerRegistration>& |
| 1002 service_worker_registration) { | 930 service_worker_registration) { |
| 1003 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 931 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1004 if (service_worker_status != SERVICE_WORKER_OK) { | 932 if (service_worker_status != SERVICE_WORKER_OK) { |
| 1005 base::ThreadTaskRunnerHandle::Get()->PostTask( | 933 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 1006 FROM_HERE, base::Bind(event_fired_callback)); | 934 FROM_HERE, base::Bind(event_fired_callback)); |
| 1007 base::ThreadTaskRunnerHandle::Get()->PostTask( | 935 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 1008 FROM_HERE, base::Bind(event_completed_callback)); | 936 FROM_HERE, base::Bind(event_completed_callback)); |
| 1009 return; | 937 return; |
| 1010 } | 938 } |
| 1011 | 939 |
| 1012 RefCountedRegistration* registration = LookupActiveRegistration( | 940 BackgroundSyncRegistration* registration = LookupActiveRegistration( |
| 1013 service_worker_registration->id(), registration_key); | 941 service_worker_registration->id(), registration_key); |
| 1014 DCHECK(registration); | 942 DCHECK(registration); |
| 1015 | 943 |
| 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; | 944 num_firing_registrations_ += 1; |
| 1022 | 945 |
| 1023 BackgroundSyncRegistrationHandle::HandleId handle_id = | |
| 1024 registration_handle->handle_id(); | |
| 1025 | |
| 1026 BackgroundSyncEventLastChance last_chance = | 946 BackgroundSyncEventLastChance last_chance = |
| 1027 registration->value()->num_attempts() == | 947 registration->num_attempts() == parameters_->max_sync_attempts - 1 |
| 1028 parameters_->max_sync_attempts - 1 | |
| 1029 ? BackgroundSyncEventLastChance::IS_LAST_CHANCE | 948 ? BackgroundSyncEventLastChance::IS_LAST_CHANCE |
| 1030 : BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE; | 949 : BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE; |
| 1031 | 950 |
| 1032 HasMainFrameProviderHost( | 951 HasMainFrameProviderHost( |
| 1033 service_worker_registration->pattern().GetOrigin(), | 952 service_worker_registration->pattern().GetOrigin(), |
| 1034 base::Bind(&BackgroundSyncMetrics::RecordEventStarted)); | 953 base::Bind(&BackgroundSyncMetrics::RecordEventStarted)); |
| 1035 | 954 |
| 1036 DispatchSyncEvent( | 955 DispatchSyncEvent( |
| 1037 handle_id, service_worker_registration->active_version(), last_chance, | 956 registration->options()->tag, |
| 957 service_worker_registration->active_version(), last_chance, | |
| 1038 base::Bind(&BackgroundSyncManager::EventComplete, | 958 base::Bind(&BackgroundSyncManager::EventComplete, |
| 1039 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, | 959 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, |
| 1040 service_worker_registration->id(), | 960 service_worker_registration->id(), registration_key, |
| 1041 base::Passed(std::move(registration_handle)), | |
| 1042 event_completed_callback)); | 961 event_completed_callback)); |
| 1043 | 962 |
| 1044 base::ThreadTaskRunnerHandle::Get()->PostTask( | 963 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 1045 FROM_HERE, base::Bind(event_fired_callback)); | 964 FROM_HERE, base::Bind(event_fired_callback)); |
| 1046 } | 965 } |
| 1047 | 966 |
| 1048 void BackgroundSyncManager::FireReadyEventsAllEventsFiring( | 967 void BackgroundSyncManager::FireReadyEventsAllEventsFiring( |
| 1049 const base::Closure& callback) { | 968 const base::Closure& callback) { |
| 1050 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 969 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1051 | 970 |
| 1052 RunInBackgroundIfNecessary(); | 971 RunInBackgroundIfNecessary(); |
| 1053 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 972 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 1054 base::Bind(callback)); | 973 base::Bind(callback)); |
| 1055 } | 974 } |
| 1056 | 975 |
| 1057 // |service_worker_registration| is just to keep the registration alive | 976 // |service_worker_registration| is just to keep the registration alive |
| 1058 // while the event is firing. | 977 // while the event is firing. |
| 1059 void BackgroundSyncManager::EventComplete( | 978 void BackgroundSyncManager::EventComplete( |
| 1060 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, | 979 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, |
| 1061 int64_t service_worker_id, | 980 int64_t service_worker_id, |
| 1062 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, | 981 const RegistrationKey& registration_key, |
| 1063 const base::Closure& callback, | 982 const base::Closure& callback, |
| 1064 ServiceWorkerStatusCode status_code) { | 983 ServiceWorkerStatusCode status_code) { |
| 1065 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 984 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1066 | 985 |
| 1067 if (disabled_) { | 986 if (disabled_) { |
| 1068 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 987 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 1069 base::Bind(callback)); | 988 base::Bind(callback)); |
| 1070 return; | 989 return; |
| 1071 } | 990 } |
| 1072 | 991 |
| 1073 op_scheduler_.ScheduleOperation(base::Bind( | 992 op_scheduler_.ScheduleOperation(base::Bind( |
| 1074 &BackgroundSyncManager::EventCompleteImpl, weak_ptr_factory_.GetWeakPtr(), | 993 &BackgroundSyncManager::EventCompleteImpl, weak_ptr_factory_.GetWeakPtr(), |
| 1075 service_worker_id, base::Passed(std::move(registration_handle)), | 994 service_worker_id, registration_key, status_code, |
| 1076 status_code, MakeClosureCompletion(callback))); | 995 MakeClosureCompletion(callback))); |
| 1077 } | 996 } |
| 1078 | 997 |
| 1079 void BackgroundSyncManager::EventCompleteImpl( | 998 void BackgroundSyncManager::EventCompleteImpl( |
| 1080 int64_t service_worker_id, | 999 int64_t service_worker_id, |
| 1081 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, | 1000 const RegistrationKey& registration_key, |
| 1082 ServiceWorkerStatusCode status_code, | 1001 ServiceWorkerStatusCode status_code, |
| 1083 const base::Closure& callback) { | 1002 const base::Closure& callback) { |
| 1084 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1003 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1085 | 1004 |
| 1086 if (disabled_) { | 1005 if (disabled_) { |
| 1087 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1006 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 1088 base::Bind(callback)); | 1007 base::Bind(callback)); |
| 1089 return; | 1008 return; |
| 1090 } | 1009 } |
| 1091 | 1010 |
| 1092 BackgroundSyncRegistration* registration = | 1011 BackgroundSyncRegistration* registration = |
| 1093 registration_handle->registration(); | 1012 LookupActiveRegistration(service_worker_id, registration_key); |
| 1013 if (!registration) { | |
|
iclelland
2016/03/08 16:48:14
Is this a failure case?
jkarlin
2016/03/08 19:37:36
This is what happens if the ServiceWorkerRegistrat
| |
| 1014 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 1015 base::Bind(callback)); | |
| 1016 return; | |
| 1017 } | |
| 1018 | |
| 1094 DCHECK(registration); | 1019 DCHECK(registration); |
|
iclelland
2016/03/08 16:48:13
No longer necessary
jkarlin
2016/03/08 19:37:36
Done.
| |
| 1095 DCHECK_NE(BackgroundSyncState::PENDING, registration->sync_state()); | 1020 DCHECK_NE(BackgroundSyncState::PENDING, registration->sync_state()); |
| 1096 | 1021 |
| 1097 registration->set_num_attempts(registration->num_attempts() + 1); | 1022 registration->set_num_attempts(registration->num_attempts() + 1); |
| 1098 | 1023 |
| 1099 num_firing_registrations_ -= 1; | 1024 num_firing_registrations_ -= 1; |
|
iclelland
2016/03/08 16:48:14
Since we're not guarding this with the DCHECK any
jkarlin
2016/03/08 19:37:36
Good catch, thanks!
| |
| 1100 | 1025 |
| 1101 // The event ran to completion, we should count it, no matter what happens | 1026 // The event ran to completion, we should count it, no matter what happens |
| 1102 // from here. | 1027 // from here. |
| 1103 ServiceWorkerRegistration* sw_registration = | 1028 ServiceWorkerRegistration* sw_registration = |
| 1104 service_worker_context_->GetLiveRegistration(service_worker_id); | 1029 service_worker_context_->GetLiveRegistration(service_worker_id); |
| 1105 if (sw_registration) { | 1030 if (sw_registration) { |
| 1106 HasMainFrameProviderHost( | 1031 HasMainFrameProviderHost( |
| 1107 sw_registration->pattern().GetOrigin(), | 1032 sw_registration->pattern().GetOrigin(), |
| 1108 base::Bind(&BackgroundSyncMetrics::RecordEventResult, | 1033 base::Bind(&BackgroundSyncMetrics::RecordEventResult, |
| 1109 status_code == SERVICE_WORKER_OK)); | 1034 status_code == SERVICE_WORKER_OK)); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 1123 registration->set_sync_state(BackgroundSyncState::PENDING); | 1048 registration->set_sync_state(BackgroundSyncState::PENDING); |
| 1124 registration->set_delay_until(clock_->Now() + | 1049 registration->set_delay_until(clock_->Now() + |
| 1125 parameters_->initial_retry_delay * | 1050 parameters_->initial_retry_delay * |
| 1126 pow(parameters_->retry_delay_factor, | 1051 pow(parameters_->retry_delay_factor, |
| 1127 registration->num_attempts() - 1)); | 1052 registration->num_attempts() - 1)); |
| 1128 registration_completed = false; | 1053 registration_completed = false; |
| 1129 } | 1054 } |
| 1130 | 1055 |
| 1131 if (registration_completed) { | 1056 if (registration_completed) { |
| 1132 RegistrationKey key(*registration); | 1057 RegistrationKey key(*registration); |
| 1133 RefCountedRegistration* active_registration = | 1058 BackgroundSyncRegistration* active_registration = |
| 1134 LookupActiveRegistration(service_worker_id, key); | 1059 LookupActiveRegistration(service_worker_id, key); |
| 1135 if (active_registration && | 1060 if (active_registration && |
| 1136 active_registration->value()->id() == registration->id()) { | 1061 active_registration->id() == registration->id()) { |
| 1137 RemoveActiveRegistration(service_worker_id, key); | 1062 RemoveActiveRegistration(service_worker_id, key); |
| 1138 } | 1063 } |
| 1139 } | 1064 } |
| 1140 | 1065 |
| 1141 StoreRegistrations( | 1066 StoreRegistrations( |
| 1142 service_worker_id, | 1067 service_worker_id, |
| 1143 base::Bind(&BackgroundSyncManager::EventCompleteDidStore, | 1068 base::Bind(&BackgroundSyncManager::EventCompleteDidStore, |
| 1144 weak_ptr_factory_.GetWeakPtr(), service_worker_id, callback)); | 1069 weak_ptr_factory_.GetWeakPtr(), service_worker_id, callback)); |
| 1145 } | 1070 } |
| 1146 | 1071 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1222 Params... parameters) { | 1147 Params... parameters) { |
| 1223 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1148 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1224 | 1149 |
| 1225 callback.Run(parameters...); | 1150 callback.Run(parameters...); |
| 1226 op_scheduler_.CompleteOperationAndRunNext(); | 1151 op_scheduler_.CompleteOperationAndRunNext(); |
| 1227 } | 1152 } |
| 1228 | 1153 |
| 1229 void BackgroundSyncManager::CompleteStatusAndRegistrationCallback( | 1154 void BackgroundSyncManager::CompleteStatusAndRegistrationCallback( |
| 1230 StatusAndRegistrationCallback callback, | 1155 StatusAndRegistrationCallback callback, |
| 1231 BackgroundSyncStatus status, | 1156 BackgroundSyncStatus status, |
| 1232 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle) { | 1157 scoped_ptr<BackgroundSyncRegistration> registration) { |
| 1233 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1158 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1234 | 1159 |
| 1235 callback.Run(status, std::move(registration_handle)); | 1160 callback.Run(status, std::move(registration)); |
| 1236 op_scheduler_.CompleteOperationAndRunNext(); | 1161 op_scheduler_.CompleteOperationAndRunNext(); |
| 1237 } | 1162 } |
| 1238 | 1163 |
| 1239 void BackgroundSyncManager::CompleteStatusAndRegistrationsCallback( | 1164 void BackgroundSyncManager::CompleteStatusAndRegistrationsCallback( |
| 1240 StatusAndRegistrationsCallback callback, | 1165 StatusAndRegistrationsCallback callback, |
| 1241 BackgroundSyncStatus status, | 1166 BackgroundSyncStatus status, |
| 1242 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> | 1167 scoped_ptr<ScopedVector<BackgroundSyncRegistration>> registration) { |
| 1243 registration_handles) { | |
| 1244 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1168 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1245 | 1169 |
| 1246 callback.Run(status, std::move(registration_handles)); | 1170 callback.Run(status, std::move(registration)); |
| 1247 op_scheduler_.CompleteOperationAndRunNext(); | 1171 op_scheduler_.CompleteOperationAndRunNext(); |
| 1248 } | 1172 } |
| 1249 | 1173 |
| 1250 base::Closure BackgroundSyncManager::MakeEmptyCompletion() { | 1174 base::Closure BackgroundSyncManager::MakeEmptyCompletion() { |
| 1251 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1175 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1252 | 1176 |
| 1253 return MakeClosureCompletion(base::Bind(base::DoNothing)); | 1177 return MakeClosureCompletion(base::Bind(base::DoNothing)); |
| 1254 } | 1178 } |
| 1255 | 1179 |
| 1256 base::Closure BackgroundSyncManager::MakeClosureCompletion( | 1180 base::Closure BackgroundSyncManager::MakeClosureCompletion( |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 1286 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1210 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
| 1287 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1211 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1288 | 1212 |
| 1289 return base::Bind( | 1213 return base::Bind( |
| 1290 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1214 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
| 1291 BackgroundSyncStatus>, | 1215 BackgroundSyncStatus>, |
| 1292 weak_ptr_factory_.GetWeakPtr(), callback); | 1216 weak_ptr_factory_.GetWeakPtr(), callback); |
| 1293 } | 1217 } |
| 1294 | 1218 |
| 1295 } // namespace content | 1219 } // namespace content |
| OLD | NEW |