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 "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 bool firing = existing_registration->value()->sync_state() == | |
| 719 BACKGROUND_SYNC_STATE_FIRING; | |
| 720 | |
| 721 existing_registration->value()->set_sync_state( | |
| 722 BACKGROUND_SYNC_STATE_UNREGISTERED); | |
| 723 | |
| 724 if (!firing) { | |
| 725 // If the registration is currently firing then wait to run | |
| 726 // RunDoneCallbacks until after it has finished as it might | |
| 727 // change state to SUCCESS first. | |
| 728 existing_registration->value()->RunDoneCallbacks(); | |
| 729 } | |
| 730 | |
| 718 RemoveActiveRegistration(sw_registration_id, registration_key); | 731 RemoveActiveRegistration(sw_registration_id, registration_key); |
| 719 | 732 |
| 720 StoreRegistrations(sw_registration_id, | 733 StoreRegistrations(sw_registration_id, |
| 721 base::Bind(&BackgroundSyncManager::UnregisterDidStore, | 734 base::Bind(&BackgroundSyncManager::UnregisterDidStore, |
| 722 weak_ptr_factory_.GetWeakPtr(), | 735 weak_ptr_factory_.GetWeakPtr(), |
| 723 sw_registration_id, periodicity, callback)); | 736 sw_registration_id, periodicity, callback)); |
| 724 } | 737 } |
| 725 | 738 |
| 726 void BackgroundSyncManager::UnregisterDidStore(int64 sw_registration_id, | 739 void BackgroundSyncManager::UnregisterDidStore(int64 sw_registration_id, |
| 727 SyncPeriodicity periodicity, | 740 SyncPeriodicity periodicity, |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 747 base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); | 760 base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR)); |
| 748 return; | 761 return; |
| 749 } | 762 } |
| 750 | 763 |
| 751 BackgroundSyncMetrics::CountUnregister(periodicity, | 764 BackgroundSyncMetrics::CountUnregister(periodicity, |
| 752 BACKGROUND_SYNC_STATUS_OK); | 765 BACKGROUND_SYNC_STATUS_OK); |
| 753 base::ThreadTaskRunnerHandle::Get()->PostTask( | 766 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 754 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK)); | 767 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK)); |
| 755 } | 768 } |
| 756 | 769 |
| 770 void BackgroundSyncManager::NotifyWhenDone( | |
| 771 BackgroundSyncRegistrationHandle::HandleId handle_id, | |
| 772 const StatusAndStateCallback& callback) { | |
| 773 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 774 | |
| 775 if (disabled_) { | |
| 776 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 777 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | |
| 778 BACKGROUND_SYNC_STATE_FAILED)); | |
| 779 return; | |
| 780 } | |
| 781 | |
| 782 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = | |
| 783 DuplicateRegistrationHandle(handle_id); | |
| 784 | |
| 785 op_scheduler_.ScheduleOperation( | |
| 786 base::Bind(&BackgroundSyncManager::NotifyWhenDoneImpl, | |
| 787 weak_ptr_factory_.GetWeakPtr(), | |
| 788 base::Passed(registration_handle.Pass()), callback)); | |
| 789 } | |
| 790 | |
| 791 void BackgroundSyncManager::NotifyWhenDoneImpl( | |
| 792 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, | |
| 793 const StatusAndStateCallback& callback) { | |
| 794 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 795 | |
| 796 if (disabled_) { | |
| 797 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 798 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | |
| 799 BACKGROUND_SYNC_STATE_FAILED)); | |
| 800 return; | |
| 801 } | |
| 802 | |
| 803 if (registration_handle->sync_state() == BACKGROUND_SYNC_STATE_PENDING || | |
| 804 registration_handle->sync_state() == BACKGROUND_SYNC_STATE_FIRING) { | |
| 805 registration_handle->registration()->AddDoneCallback( | |
| 806 base::Bind(&BackgroundSyncManager::NotifyWhenDoneDidFinish, | |
| 807 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 808 op_scheduler_.CompleteOperationAndRunNext(); | |
| 809 return; | |
| 810 } | |
| 811 | |
| 812 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 813 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, | |
| 814 registration_handle->sync_state())); | |
| 815 op_scheduler_.CompleteOperationAndRunNext(); | |
| 816 } | |
| 817 | |
| 818 void BackgroundSyncManager::NotifyWhenDoneDidFinish( | |
| 819 const StatusAndStateCallback& callback, | |
| 820 BackgroundSyncState sync_state) { | |
| 821 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 822 | |
| 823 if (disabled_) { | |
| 824 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 825 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | |
| 826 BACKGROUND_SYNC_STATE_FAILED)); | |
| 827 return; | |
| 828 } | |
| 829 | |
| 830 callback.Run(BACKGROUND_SYNC_STATUS_OK, sync_state); | |
| 831 } | |
| 832 | |
| 757 void BackgroundSyncManager::GetRegistrationImpl( | 833 void BackgroundSyncManager::GetRegistrationImpl( |
| 758 int64 sw_registration_id, | 834 int64 sw_registration_id, |
| 759 const RegistrationKey& registration_key, | 835 const RegistrationKey& registration_key, |
| 760 const StatusAndRegistrationCallback& callback) { | 836 const StatusAndRegistrationCallback& callback) { |
| 761 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 837 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 762 | 838 |
| 763 if (disabled_) { | 839 if (disabled_) { |
| 764 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); | 840 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); |
| 765 return; | 841 return; |
| 766 } | 842 } |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 838 | 914 |
| 839 void BackgroundSyncManager::SchedulePendingRegistrations() { | 915 void BackgroundSyncManager::SchedulePendingRegistrations() { |
| 840 #if defined(OS_ANDROID) | 916 #if defined(OS_ANDROID) |
| 841 bool keep_browser_alive_for_one_shot = false; | 917 bool keep_browser_alive_for_one_shot = false; |
| 842 | 918 |
| 843 for (const auto& sw_id_and_registrations : active_registrations_) { | 919 for (const auto& sw_id_and_registrations : active_registrations_) { |
| 844 for (const auto& key_and_registration : | 920 for (const auto& key_and_registration : |
| 845 sw_id_and_registrations.second.registration_map) { | 921 sw_id_and_registrations.second.registration_map) { |
| 846 const BackgroundSyncRegistration& registration = | 922 const BackgroundSyncRegistration& registration = |
| 847 *key_and_registration.second->value(); | 923 *key_and_registration.second->value(); |
| 848 if (registration.sync_state() == SYNC_STATE_PENDING) { | 924 if (registration.sync_state() == BACKGROUND_SYNC_STATE_PENDING) { |
| 849 if (registration.options()->periodicity == SYNC_ONE_SHOT) { | 925 if (registration.options()->periodicity == SYNC_ONE_SHOT) { |
| 850 keep_browser_alive_for_one_shot = true; | 926 keep_browser_alive_for_one_shot = true; |
| 851 } else { | 927 } else { |
| 852 // TODO(jkarlin): Support keeping the browser alive for periodic | 928 // TODO(jkarlin): Support keeping the browser alive for periodic |
| 853 // syncs. | 929 // syncs. |
| 854 } | 930 } |
| 855 } | 931 } |
| 856 } | 932 } |
| 857 } | 933 } |
| 858 | 934 |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1018 registration_handle->registration(); | 1094 registration_handle->registration(); |
| 1019 DCHECK(registration); | 1095 DCHECK(registration); |
| 1020 | 1096 |
| 1021 // The event ran to completion, we should count it, no matter what happens | 1097 // The event ran to completion, we should count it, no matter what happens |
| 1022 // from here. | 1098 // from here. |
| 1023 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, | 1099 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, |
| 1024 status_code == SERVICE_WORKER_OK); | 1100 status_code == SERVICE_WORKER_OK); |
| 1025 | 1101 |
| 1026 if (registration->options()->periodicity == SYNC_ONE_SHOT) { | 1102 if (registration->options()->periodicity == SYNC_ONE_SHOT) { |
| 1027 if (status_code != SERVICE_WORKER_OK) { | 1103 if (status_code != SERVICE_WORKER_OK) { |
| 1028 // TODO(jkarlin) Fire the sync event on the next page load controlled by | 1104 // TODO(jkarlin): Insert retry logic here. Be sure to check if the state |
| 1105 // is unregistered first. If unregistered then set the state to failed | |
| 1106 // if it was already out of retry attempts otherwise keep the state as | |
|
iclelland
2015/09/17 13:48:09
Add link to bug? crbug.com/501838
jkarlin
2015/09/17 17:40:24
Done.
| |
| 1107 // unregistered. Then call RunDoneCallbacks(); | |
| 1108 // TODO(jkarlin): Fire the sync event on the next page load controlled | |
| 1109 // by | |
| 1029 // this registration. (crbug.com/479665) | 1110 // this registration. (crbug.com/479665) |
| 1030 registration->set_sync_state(BACKGROUND_SYNC_STATE_FAILED); | 1111 registration->set_sync_state(BACKGROUND_SYNC_STATE_FAILED); |
| 1112 registration->RunDoneCallbacks(); | |
| 1031 } else { | 1113 } else { |
| 1114 registration->set_sync_state(BACKGROUND_SYNC_STATE_SUCCESS); | |
| 1115 registration->RunDoneCallbacks(); | |
| 1116 | |
| 1032 RegistrationKey key(*registration); | 1117 RegistrationKey key(*registration); |
| 1033 // Remove the registration if it's still active. | 1118 // Remove the registration if it's still active. |
| 1034 RefCountedRegistration* active_registration = | 1119 RefCountedRegistration* active_registration = |
| 1035 LookupActiveRegistration(service_worker_id, key); | 1120 LookupActiveRegistration(service_worker_id, key); |
| 1036 if (active_registration && | 1121 if (active_registration && |
| 1037 active_registration->value()->id() == registration->id()) { | 1122 active_registration->value()->id() == registration->id()) { |
| 1038 RemoveActiveRegistration(service_worker_id, key); | 1123 RemoveActiveRegistration(service_worker_id, key); |
| 1039 } | 1124 } |
| 1040 } | 1125 } |
| 1041 } else { | 1126 } else { |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1185 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1270 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
| 1186 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1271 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1187 | 1272 |
| 1188 return base::Bind( | 1273 return base::Bind( |
| 1189 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1274 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
| 1190 BackgroundSyncStatus>, | 1275 BackgroundSyncStatus>, |
| 1191 weak_ptr_factory_.GetWeakPtr(), callback); | 1276 weak_ptr_factory_.GetWeakPtr(), callback); |
| 1192 } | 1277 } |
| 1193 | 1278 |
| 1194 } // namespace content | 1279 } // namespace content |
| OLD | NEW |