OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/background_sync/background_sync_manager.h" | 5 #include "content/browser/background_sync/background_sync_manager.h" |
6 | 6 |
7 #include "base/barrier_closure.h" | 7 #include "base/barrier_closure.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
(...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
696 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 696 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
697 | 697 |
698 if (disabled_) { | 698 if (disabled_) { |
699 BackgroundSyncMetrics::CountUnregister( | 699 BackgroundSyncMetrics::CountUnregister( |
700 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); | 700 periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); |
701 base::ThreadTaskRunnerHandle::Get()->PostTask( | 701 base::ThreadTaskRunnerHandle::Get()->PostTask( |
702 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | 702 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
703 return; | 703 return; |
704 } | 704 } |
705 | 705 |
706 const RefCountedRegistration* existing_registration = | 706 RefCountedRegistration* existing_registration = |
707 LookupActiveRegistration(sw_registration_id, registration_key); | 707 LookupActiveRegistration(sw_registration_id, registration_key); |
708 | 708 |
709 if (!existing_registration || | 709 if (!existing_registration || |
710 existing_registration->value()->id() != sync_registration_id) { | 710 existing_registration->value()->id() != sync_registration_id) { |
711 BackgroundSyncMetrics::CountUnregister(periodicity, | 711 BackgroundSyncMetrics::CountUnregister(periodicity, |
712 BACKGROUND_SYNC_STATUS_NOT_FOUND); | 712 BACKGROUND_SYNC_STATUS_NOT_FOUND); |
713 base::ThreadTaskRunnerHandle::Get()->PostTask( | 713 base::ThreadTaskRunnerHandle::Get()->PostTask( |
714 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND)); | 714 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_NOT_FOUND)); |
715 return; | 715 return; |
716 } | 716 } |
717 | 717 |
| 718 DCHECK(!existing_registration->value()->HasCompleted()); |
| 719 |
| 720 bool firing = existing_registration->value()->sync_state() == |
| 721 BACKGROUND_SYNC_STATE_FIRING || |
| 722 existing_registration->value()->sync_state() == |
| 723 BACKGROUND_SYNC_STATE_UNREGISTERED_WHILE_FIRING; |
| 724 |
| 725 existing_registration->value()->set_sync_state( |
| 726 firing ? BACKGROUND_SYNC_STATE_UNREGISTERED_WHILE_FIRING |
| 727 : BACKGROUND_SYNC_STATE_UNREGISTERED); |
| 728 |
| 729 if (!firing) { |
| 730 // If the registration is currently firing then wait to run |
| 731 // RunDoneCallbacks until after it has finished as it might |
| 732 // change state to SUCCESS first. |
| 733 existing_registration->value()->RunDoneCallbacks(); |
| 734 } |
| 735 |
718 RemoveActiveRegistration(sw_registration_id, registration_key); | 736 RemoveActiveRegistration(sw_registration_id, registration_key); |
719 | 737 |
720 StoreRegistrations(sw_registration_id, | 738 StoreRegistrations(sw_registration_id, |
721 base::Bind(&BackgroundSyncManager::UnregisterDidStore, | 739 base::Bind(&BackgroundSyncManager::UnregisterDidStore, |
722 weak_ptr_factory_.GetWeakPtr(), | 740 weak_ptr_factory_.GetWeakPtr(), |
723 sw_registration_id, periodicity, callback)); | 741 sw_registration_id, periodicity, callback)); |
724 } | 742 } |
725 | 743 |
726 void BackgroundSyncManager::UnregisterDidStore(int64 sw_registration_id, | 744 void BackgroundSyncManager::UnregisterDidStore(int64 sw_registration_id, |
727 SyncPeriodicity periodicity, | 745 SyncPeriodicity periodicity, |
(...skipping 19 matching lines...) Expand all Loading... |
747 base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | 765 base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
748 return; | 766 return; |
749 } | 767 } |
750 | 768 |
751 BackgroundSyncMetrics::CountUnregister(periodicity, | 769 BackgroundSyncMetrics::CountUnregister(periodicity, |
752 BACKGROUND_SYNC_STATUS_OK); | 770 BACKGROUND_SYNC_STATUS_OK); |
753 base::ThreadTaskRunnerHandle::Get()->PostTask( | 771 base::ThreadTaskRunnerHandle::Get()->PostTask( |
754 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK)); | 772 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK)); |
755 } | 773 } |
756 | 774 |
| 775 void BackgroundSyncManager::NotifyWhenDone( |
| 776 BackgroundSyncRegistrationHandle::HandleId handle_id, |
| 777 const StatusAndStateCallback& callback) { |
| 778 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 779 |
| 780 if (disabled_) { |
| 781 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 782 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
| 783 BACKGROUND_SYNC_STATE_FAILED)); |
| 784 return; |
| 785 } |
| 786 |
| 787 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = |
| 788 DuplicateRegistrationHandle(handle_id); |
| 789 |
| 790 op_scheduler_.ScheduleOperation( |
| 791 base::Bind(&BackgroundSyncManager::NotifyWhenDoneImpl, |
| 792 weak_ptr_factory_.GetWeakPtr(), |
| 793 base::Passed(registration_handle.Pass()), callback)); |
| 794 } |
| 795 |
| 796 void BackgroundSyncManager::NotifyWhenDoneImpl( |
| 797 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, |
| 798 const StatusAndStateCallback& callback) { |
| 799 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 800 |
| 801 if (disabled_) { |
| 802 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 803 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
| 804 BACKGROUND_SYNC_STATE_FAILED)); |
| 805 return; |
| 806 } |
| 807 |
| 808 if (!registration_handle->registration()->HasCompleted()) { |
| 809 registration_handle->registration()->AddDoneCallback( |
| 810 base::Bind(&BackgroundSyncManager::NotifyWhenDoneDidFinish, |
| 811 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 812 op_scheduler_.CompleteOperationAndRunNext(); |
| 813 return; |
| 814 } |
| 815 |
| 816 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 817 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, |
| 818 registration_handle->sync_state())); |
| 819 op_scheduler_.CompleteOperationAndRunNext(); |
| 820 } |
| 821 |
| 822 void BackgroundSyncManager::NotifyWhenDoneDidFinish( |
| 823 const StatusAndStateCallback& callback, |
| 824 BackgroundSyncState sync_state) { |
| 825 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 826 |
| 827 if (disabled_) { |
| 828 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 829 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
| 830 BACKGROUND_SYNC_STATE_FAILED)); |
| 831 return; |
| 832 } |
| 833 |
| 834 callback.Run(BACKGROUND_SYNC_STATUS_OK, sync_state); |
| 835 } |
| 836 |
757 void BackgroundSyncManager::GetRegistrationImpl( | 837 void BackgroundSyncManager::GetRegistrationImpl( |
758 int64 sw_registration_id, | 838 int64 sw_registration_id, |
759 const RegistrationKey& registration_key, | 839 const RegistrationKey& registration_key, |
760 const StatusAndRegistrationCallback& callback) { | 840 const StatusAndRegistrationCallback& callback) { |
761 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 841 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
762 | 842 |
763 if (disabled_) { | 843 if (disabled_) { |
764 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); | 844 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
765 return; | 845 return; |
766 } | 846 } |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1018 registration_handle->registration(); | 1098 registration_handle->registration(); |
1019 DCHECK(registration); | 1099 DCHECK(registration); |
1020 | 1100 |
1021 // The event ran to completion, we should count it, no matter what happens | 1101 // The event ran to completion, we should count it, no matter what happens |
1022 // from here. | 1102 // from here. |
1023 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, | 1103 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, |
1024 status_code == SERVICE_WORKER_OK); | 1104 status_code == SERVICE_WORKER_OK); |
1025 | 1105 |
1026 if (registration->options()->periodicity == SYNC_ONE_SHOT) { | 1106 if (registration->options()->periodicity == SYNC_ONE_SHOT) { |
1027 if (status_code != SERVICE_WORKER_OK) { | 1107 if (status_code != SERVICE_WORKER_OK) { |
1028 // TODO(jkarlin) Fire the sync event on the next page load controlled by | 1108 // TODO(jkarlin): Insert retry logic here. Be sure to check if the state |
| 1109 // is UNREGISTERED_WHILE_FIRING first. If so then set the state to failed |
| 1110 // if it was already out of retry attempts otherwise keep the state as |
| 1111 // unregistered. Then call RunDoneCallbacks(); (crbug.com/501838) |
| 1112 // TODO(jkarlin): Fire the sync event on the next page load controlled |
| 1113 // by |
1029 // this registration. (crbug.com/479665) | 1114 // this registration. (crbug.com/479665) |
1030 registration->set_sync_state(BACKGROUND_SYNC_STATE_FAILED); | 1115 registration->set_sync_state(BACKGROUND_SYNC_STATE_FAILED); |
| 1116 registration->RunDoneCallbacks(); |
1031 } else { | 1117 } else { |
| 1118 registration->set_sync_state(BACKGROUND_SYNC_STATE_SUCCESS); |
| 1119 registration->RunDoneCallbacks(); |
| 1120 |
1032 RegistrationKey key(*registration); | 1121 RegistrationKey key(*registration); |
1033 // Remove the registration if it's still active. | 1122 // Remove the registration if it's still active. |
1034 RefCountedRegistration* active_registration = | 1123 RefCountedRegistration* active_registration = |
1035 LookupActiveRegistration(service_worker_id, key); | 1124 LookupActiveRegistration(service_worker_id, key); |
1036 if (active_registration && | 1125 if (active_registration && |
1037 active_registration->value()->id() == registration->id()) { | 1126 active_registration->value()->id() == registration->id()) { |
1038 RemoveActiveRegistration(service_worker_id, key); | 1127 RemoveActiveRegistration(service_worker_id, key); |
1039 } | 1128 } |
1040 } | 1129 } |
1041 } else { | 1130 } else { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1185 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1274 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
1186 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1275 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1187 | 1276 |
1188 return base::Bind( | 1277 return base::Bind( |
1189 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1278 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
1190 BackgroundSyncStatus>, | 1279 BackgroundSyncStatus>, |
1191 weak_ptr_factory_.GetWeakPtr(), callback); | 1280 weak_ptr_factory_.GetWeakPtr(), callback); |
1192 } | 1281 } |
1193 | 1282 |
1194 } // namespace content | 1283 } // namespace content |
OLD | NEW |