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 DCHECK_EQ(SYNC_ONE_SHOT, registration_handle->options()->periodicity); |
| 801 |
| 802 if (disabled_) { |
| 803 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 804 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
| 805 BACKGROUND_SYNC_STATE_FAILED)); |
| 806 return; |
| 807 } |
| 808 |
| 809 if (!registration_handle->registration()->HasCompleted()) { |
| 810 registration_handle->registration()->AddDoneCallback( |
| 811 base::Bind(&BackgroundSyncManager::NotifyWhenDoneDidFinish, |
| 812 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 813 op_scheduler_.CompleteOperationAndRunNext(); |
| 814 return; |
| 815 } |
| 816 |
| 817 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 818 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, |
| 819 registration_handle->sync_state())); |
| 820 op_scheduler_.CompleteOperationAndRunNext(); |
| 821 } |
| 822 |
| 823 void BackgroundSyncManager::NotifyWhenDoneDidFinish( |
| 824 const StatusAndStateCallback& callback, |
| 825 BackgroundSyncState sync_state) { |
| 826 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 827 |
| 828 if (disabled_) { |
| 829 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 830 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
| 831 BACKGROUND_SYNC_STATE_FAILED)); |
| 832 return; |
| 833 } |
| 834 |
| 835 callback.Run(BACKGROUND_SYNC_STATUS_OK, sync_state); |
| 836 } |
| 837 |
757 void BackgroundSyncManager::GetRegistrationImpl( | 838 void BackgroundSyncManager::GetRegistrationImpl( |
758 int64 sw_registration_id, | 839 int64 sw_registration_id, |
759 const RegistrationKey& registration_key, | 840 const RegistrationKey& registration_key, |
760 const StatusAndRegistrationCallback& callback) { | 841 const StatusAndRegistrationCallback& callback) { |
761 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 842 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
762 | 843 |
763 if (disabled_) { | 844 if (disabled_) { |
764 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); | 845 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
765 return; | 846 return; |
766 } | 847 } |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1018 registration_handle->registration(); | 1099 registration_handle->registration(); |
1019 DCHECK(registration); | 1100 DCHECK(registration); |
1020 | 1101 |
1021 // The event ran to completion, we should count it, no matter what happens | 1102 // The event ran to completion, we should count it, no matter what happens |
1022 // from here. | 1103 // from here. |
1023 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, | 1104 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, |
1024 status_code == SERVICE_WORKER_OK); | 1105 status_code == SERVICE_WORKER_OK); |
1025 | 1106 |
1026 if (registration->options()->periodicity == SYNC_ONE_SHOT) { | 1107 if (registration->options()->periodicity == SYNC_ONE_SHOT) { |
1027 if (status_code != SERVICE_WORKER_OK) { | 1108 if (status_code != SERVICE_WORKER_OK) { |
1028 // TODO(jkarlin) Fire the sync event on the next page load controlled by | 1109 // TODO(jkarlin): Insert retry logic here. Be sure to check if the state |
| 1110 // is UNREGISTERED_WHILE_FIRING first. If so then set the state to failed |
| 1111 // if it was already out of retry attempts otherwise keep the state as |
| 1112 // unregistered. Then call RunDoneCallbacks(); (crbug.com/501838) |
| 1113 // TODO(jkarlin): Fire the sync event on the next page load controlled |
| 1114 // by |
1029 // this registration. (crbug.com/479665) | 1115 // this registration. (crbug.com/479665) |
1030 registration->set_sync_state(BACKGROUND_SYNC_STATE_FAILED); | 1116 registration->set_sync_state(BACKGROUND_SYNC_STATE_FAILED); |
| 1117 registration->RunDoneCallbacks(); |
1031 } else { | 1118 } else { |
| 1119 registration->set_sync_state(BACKGROUND_SYNC_STATE_SUCCESS); |
| 1120 registration->RunDoneCallbacks(); |
| 1121 |
1032 RegistrationKey key(*registration); | 1122 RegistrationKey key(*registration); |
1033 // Remove the registration if it's still active. | 1123 // Remove the registration if it's still active. |
1034 RefCountedRegistration* active_registration = | 1124 RefCountedRegistration* active_registration = |
1035 LookupActiveRegistration(service_worker_id, key); | 1125 LookupActiveRegistration(service_worker_id, key); |
1036 if (active_registration && | 1126 if (active_registration && |
1037 active_registration->value()->id() == registration->id()) { | 1127 active_registration->value()->id() == registration->id()) { |
1038 RemoveActiveRegistration(service_worker_id, key); | 1128 RemoveActiveRegistration(service_worker_id, key); |
1039 } | 1129 } |
1040 } | 1130 } |
1041 } else { | 1131 } else { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1185 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1275 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
1186 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1276 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1187 | 1277 |
1188 return base::Bind( | 1278 return base::Bind( |
1189 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1279 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
1190 BackgroundSyncStatus>, | 1280 BackgroundSyncStatus>, |
1191 weak_ptr_factory_.GetWeakPtr(), callback); | 1281 weak_ptr_factory_.GetWeakPtr(), callback); |
1192 } | 1282 } |
1193 | 1283 |
1194 } // namespace content | 1284 } // namespace content |
OLD | NEW |