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" |
(...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 AreOptionConditionsMet(options) | 506 AreOptionConditionsMet(options) |
507 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE | 507 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
508 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; | 508 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
509 BackgroundSyncMetrics::CountRegisterSuccess( | 509 BackgroundSyncMetrics::CountRegisterSuccess( |
510 existing_registration->options()->periodicity, | 510 existing_registration->options()->periodicity, |
511 registration_could_fire, | 511 registration_could_fire, |
512 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE); | 512 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE); |
513 | 513 |
514 if (existing_registration->IsFiring()) { | 514 if (existing_registration->IsFiring()) { |
515 existing_registration->set_sync_state( | 515 existing_registration->set_sync_state( |
516 BACKGROUND_SYNC_STATE_REREGISTERED_WHILE_FIRING); | 516 BackgroundSyncState::REREGISTERED_WHILE_FIRING); |
517 } | 517 } |
518 | 518 |
519 base::ThreadTaskRunnerHandle::Get()->PostTask( | 519 base::ThreadTaskRunnerHandle::Get()->PostTask( |
520 FROM_HERE, | 520 FROM_HERE, |
521 base::Bind( | 521 base::Bind( |
522 callback, BACKGROUND_SYNC_STATUS_OK, | 522 callback, BACKGROUND_SYNC_STATUS_OK, |
523 base::Passed(CreateRegistrationHandle(existing_registration_ref)))); | 523 base::Passed(CreateRegistrationHandle(existing_registration_ref)))); |
524 return; | 524 return; |
525 } else { | 525 } else { |
526 existing_registration_ref->value()->SetUnregisteredState(); | 526 existing_registration_ref->value()->SetUnregisteredState(); |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
925 } | 925 } |
926 | 926 |
927 void BackgroundSyncManager::NotifyWhenFinished( | 927 void BackgroundSyncManager::NotifyWhenFinished( |
928 BackgroundSyncRegistrationHandle::HandleId handle_id, | 928 BackgroundSyncRegistrationHandle::HandleId handle_id, |
929 const StatusAndStateCallback& callback) { | 929 const StatusAndStateCallback& callback) { |
930 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 930 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
931 | 931 |
932 if (disabled_) { | 932 if (disabled_) { |
933 base::ThreadTaskRunnerHandle::Get()->PostTask( | 933 base::ThreadTaskRunnerHandle::Get()->PostTask( |
934 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 934 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
935 BACKGROUND_SYNC_STATE_FAILED)); | 935 BackgroundSyncState::FAILED)); |
936 return; | 936 return; |
937 } | 937 } |
938 | 938 |
939 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = | 939 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = |
940 DuplicateRegistrationHandle(handle_id); | 940 DuplicateRegistrationHandle(handle_id); |
941 | 941 |
942 op_scheduler_.ScheduleOperation( | 942 op_scheduler_.ScheduleOperation( |
943 base::Bind(&BackgroundSyncManager::NotifyWhenFinishedImpl, | 943 base::Bind(&BackgroundSyncManager::NotifyWhenFinishedImpl, |
944 weak_ptr_factory_.GetWeakPtr(), | 944 weak_ptr_factory_.GetWeakPtr(), |
945 base::Passed(std::move(registration_handle)), callback)); | 945 base::Passed(std::move(registration_handle)), callback)); |
946 } | 946 } |
947 | 947 |
948 void BackgroundSyncManager::NotifyWhenFinishedImpl( | 948 void BackgroundSyncManager::NotifyWhenFinishedImpl( |
949 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, | 949 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, |
950 const StatusAndStateCallback& callback) { | 950 const StatusAndStateCallback& callback) { |
951 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 951 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
952 DCHECK_EQ(SYNC_ONE_SHOT, registration_handle->options()->periodicity); | 952 DCHECK_EQ(SYNC_ONE_SHOT, registration_handle->options()->periodicity); |
953 | 953 |
954 if (disabled_) { | 954 if (disabled_) { |
955 base::ThreadTaskRunnerHandle::Get()->PostTask( | 955 base::ThreadTaskRunnerHandle::Get()->PostTask( |
956 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 956 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
957 BACKGROUND_SYNC_STATE_FAILED)); | 957 BackgroundSyncState::FAILED)); |
958 return; | 958 return; |
959 } | 959 } |
960 | 960 |
961 if (!registration_handle->registration()->HasCompleted()) { | 961 if (!registration_handle->registration()->HasCompleted()) { |
962 registration_handle->registration()->AddFinishedCallback( | 962 registration_handle->registration()->AddFinishedCallback( |
963 base::Bind(&BackgroundSyncManager::NotifyWhenFinishedInvokeCallback, | 963 base::Bind(&BackgroundSyncManager::NotifyWhenFinishedInvokeCallback, |
964 weak_ptr_factory_.GetWeakPtr(), callback)); | 964 weak_ptr_factory_.GetWeakPtr(), callback)); |
965 op_scheduler_.CompleteOperationAndRunNext(); | 965 op_scheduler_.CompleteOperationAndRunNext(); |
966 return; | 966 return; |
967 } | 967 } |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1047 } | 1047 } |
1048 | 1048 |
1049 bool BackgroundSyncManager::IsRegistrationReadyToFire( | 1049 bool BackgroundSyncManager::IsRegistrationReadyToFire( |
1050 const BackgroundSyncRegistration& registration) { | 1050 const BackgroundSyncRegistration& registration) { |
1051 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1051 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1052 | 1052 |
1053 // TODO(jkarlin): Add support for firing periodic registrations. | 1053 // TODO(jkarlin): Add support for firing periodic registrations. |
1054 if (registration.options()->periodicity == SYNC_PERIODIC) | 1054 if (registration.options()->periodicity == SYNC_PERIODIC) |
1055 return false; | 1055 return false; |
1056 | 1056 |
1057 if (registration.sync_state() != BACKGROUND_SYNC_STATE_PENDING) | 1057 if (registration.sync_state() != BackgroundSyncState::PENDING) |
1058 return false; | 1058 return false; |
1059 | 1059 |
1060 if (clock_->Now() < registration.delay_until()) | 1060 if (clock_->Now() < registration.delay_until()) |
1061 return false; | 1061 return false; |
1062 | 1062 |
1063 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); | 1063 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); |
1064 | 1064 |
1065 return AreOptionConditionsMet(*registration.options()); | 1065 return AreOptionConditionsMet(*registration.options()); |
1066 } | 1066 } |
1067 | 1067 |
1068 void BackgroundSyncManager::RunInBackgroundIfNecessary() { | 1068 void BackgroundSyncManager::RunInBackgroundIfNecessary() { |
1069 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1069 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1070 base::TimeDelta soonest_wakeup_delta = base::TimeDelta::Max(); | 1070 base::TimeDelta soonest_wakeup_delta = base::TimeDelta::Max(); |
1071 | 1071 |
1072 for (const auto& sw_id_and_registrations : active_registrations_) { | 1072 for (const auto& sw_id_and_registrations : active_registrations_) { |
1073 for (const auto& key_and_registration : | 1073 for (const auto& key_and_registration : |
1074 sw_id_and_registrations.second.registration_map) { | 1074 sw_id_and_registrations.second.registration_map) { |
1075 const BackgroundSyncRegistration& registration = | 1075 const BackgroundSyncRegistration& registration = |
1076 *key_and_registration.second->value(); | 1076 *key_and_registration.second->value(); |
1077 if (registration.sync_state() == BACKGROUND_SYNC_STATE_PENDING) { | 1077 if (registration.sync_state() == BackgroundSyncState::PENDING) { |
1078 if (registration.options()->periodicity == SYNC_ONE_SHOT) { | 1078 if (registration.options()->periodicity == SYNC_ONE_SHOT) { |
1079 if (clock_->Now() >= registration.delay_until()) { | 1079 if (clock_->Now() >= registration.delay_until()) { |
1080 soonest_wakeup_delta = base::TimeDelta(); | 1080 soonest_wakeup_delta = base::TimeDelta(); |
1081 } else { | 1081 } else { |
1082 base::TimeDelta delay_delta = | 1082 base::TimeDelta delay_delta = |
1083 registration.delay_until() - clock_->Now(); | 1083 registration.delay_until() - clock_->Now(); |
1084 if (delay_delta < soonest_wakeup_delta) | 1084 if (delay_delta < soonest_wakeup_delta) |
1085 soonest_wakeup_delta = delay_delta; | 1085 soonest_wakeup_delta = delay_delta; |
1086 } | 1086 } |
1087 } else { | 1087 } else { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1145 for (auto& key_and_registration : | 1145 for (auto& key_and_registration : |
1146 sw_id_and_registrations.second.registration_map) { | 1146 sw_id_and_registrations.second.registration_map) { |
1147 BackgroundSyncRegistration* registration = | 1147 BackgroundSyncRegistration* registration = |
1148 key_and_registration.second->value(); | 1148 key_and_registration.second->value(); |
1149 if (IsRegistrationReadyToFire(*registration)) { | 1149 if (IsRegistrationReadyToFire(*registration)) { |
1150 sw_id_and_keys_to_fire.push_back( | 1150 sw_id_and_keys_to_fire.push_back( |
1151 std::make_pair(service_worker_id, key_and_registration.first)); | 1151 std::make_pair(service_worker_id, key_and_registration.first)); |
1152 // The state change is not saved to persistent storage because | 1152 // The state change is not saved to persistent storage because |
1153 // if the sync event is killed mid-sync then it should return to | 1153 // if the sync event is killed mid-sync then it should return to |
1154 // SYNC_STATE_PENDING. | 1154 // SYNC_STATE_PENDING. |
1155 registration->set_sync_state(BACKGROUND_SYNC_STATE_FIRING); | 1155 registration->set_sync_state(BackgroundSyncState::FIRING); |
1156 } | 1156 } |
1157 } | 1157 } |
1158 } | 1158 } |
1159 | 1159 |
1160 if (sw_id_and_keys_to_fire.empty()) { | 1160 if (sw_id_and_keys_to_fire.empty()) { |
1161 RunInBackgroundIfNecessary(); | 1161 RunInBackgroundIfNecessary(); |
1162 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1162 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
1163 base::Bind(callback)); | 1163 base::Bind(callback)); |
1164 return; | 1164 return; |
1165 } | 1165 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1221 CreateRegistrationHandle(registration); | 1221 CreateRegistrationHandle(registration); |
1222 | 1222 |
1223 num_firing_registrations_ += 1; | 1223 num_firing_registrations_ += 1; |
1224 | 1224 |
1225 BackgroundSyncRegistrationHandle::HandleId handle_id = | 1225 BackgroundSyncRegistrationHandle::HandleId handle_id = |
1226 registration_handle->handle_id(); | 1226 registration_handle->handle_id(); |
1227 | 1227 |
1228 BackgroundSyncEventLastChance last_chance = | 1228 BackgroundSyncEventLastChance last_chance = |
1229 registration->value()->num_attempts() == | 1229 registration->value()->num_attempts() == |
1230 parameters_->max_sync_attempts - 1 | 1230 parameters_->max_sync_attempts - 1 |
1231 ? BACKGROUND_SYNC_EVENT_LAST_CHANCE_IS_LAST_CHANCE | 1231 ? BackgroundSyncEventLastChance::IS_LAST_CHANCE |
1232 : BACKGROUND_SYNC_EVENT_LAST_CHANCE_IS_NOT_LAST_CHANCE; | 1232 : BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE; |
1233 | 1233 |
1234 HasMainFrameProviderHost( | 1234 HasMainFrameProviderHost( |
1235 service_worker_registration->pattern().GetOrigin(), | 1235 service_worker_registration->pattern().GetOrigin(), |
1236 base::Bind(&BackgroundSyncMetrics::RecordEventStarted, | 1236 base::Bind(&BackgroundSyncMetrics::RecordEventStarted, |
1237 registration->value()->options()->periodicity)); | 1237 registration->value()->options()->periodicity)); |
1238 | 1238 |
1239 FireOneShotSync( | 1239 FireOneShotSync( |
1240 handle_id, service_worker_registration->active_version(), last_chance, | 1240 handle_id, service_worker_registration->active_version(), last_chance, |
1241 base::Bind(&BackgroundSyncManager::EventComplete, | 1241 base::Bind(&BackgroundSyncManager::EventComplete, |
1242 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, | 1242 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1298 if (sw_registration) { | 1298 if (sw_registration) { |
1299 HasMainFrameProviderHost( | 1299 HasMainFrameProviderHost( |
1300 sw_registration->pattern().GetOrigin(), | 1300 sw_registration->pattern().GetOrigin(), |
1301 base::Bind(&BackgroundSyncMetrics::RecordEventResult, | 1301 base::Bind(&BackgroundSyncMetrics::RecordEventResult, |
1302 registration->options()->periodicity, | 1302 registration->options()->periodicity, |
1303 status_code == SERVICE_WORKER_OK)); | 1303 status_code == SERVICE_WORKER_OK)); |
1304 } | 1304 } |
1305 | 1305 |
1306 if (registration->options()->periodicity == SYNC_ONE_SHOT) { | 1306 if (registration->options()->periodicity == SYNC_ONE_SHOT) { |
1307 if (registration->sync_state() == | 1307 if (registration->sync_state() == |
1308 BACKGROUND_SYNC_STATE_REREGISTERED_WHILE_FIRING) { | 1308 BackgroundSyncState::REREGISTERED_WHILE_FIRING) { |
1309 registration->set_sync_state(BACKGROUND_SYNC_STATE_PENDING); | 1309 registration->set_sync_state(BackgroundSyncState::PENDING); |
1310 registration->set_num_attempts(0); | 1310 registration->set_num_attempts(0); |
1311 } else if (status_code != SERVICE_WORKER_OK) { // Sync failed | 1311 } else if (status_code != SERVICE_WORKER_OK) { // Sync failed |
1312 bool can_retry = | 1312 bool can_retry = |
1313 registration->num_attempts() < parameters_->max_sync_attempts; | 1313 registration->num_attempts() < parameters_->max_sync_attempts; |
1314 if (registration->sync_state() == | 1314 if (registration->sync_state() == |
1315 BACKGROUND_SYNC_STATE_UNREGISTERED_WHILE_FIRING) { | 1315 BackgroundSyncState::UNREGISTERED_WHILE_FIRING) { |
1316 registration->set_sync_state(can_retry | 1316 registration->set_sync_state(can_retry |
1317 ? BACKGROUND_SYNC_STATE_UNREGISTERED | 1317 ? BackgroundSyncState::UNREGISTERED |
1318 : BACKGROUND_SYNC_STATE_FAILED); | 1318 : BackgroundSyncState::FAILED); |
1319 registration->RunFinishedCallbacks(); | 1319 registration->RunFinishedCallbacks(); |
1320 } else if (can_retry) { | 1320 } else if (can_retry) { |
1321 registration->set_sync_state(BACKGROUND_SYNC_STATE_PENDING); | 1321 registration->set_sync_state(BackgroundSyncState::PENDING); |
1322 registration->set_delay_until( | 1322 registration->set_delay_until( |
1323 clock_->Now() + | 1323 clock_->Now() + |
1324 parameters_->initial_retry_delay * | 1324 parameters_->initial_retry_delay * |
1325 pow(parameters_->retry_delay_factor, | 1325 pow(parameters_->retry_delay_factor, |
1326 registration->num_attempts() - 1)); | 1326 registration->num_attempts() - 1)); |
1327 } else { | 1327 } else { |
1328 registration->set_sync_state(BACKGROUND_SYNC_STATE_FAILED); | 1328 registration->set_sync_state(BackgroundSyncState::FAILED); |
1329 registration->RunFinishedCallbacks(); | 1329 registration->RunFinishedCallbacks(); |
1330 } | 1330 } |
1331 } else { // Sync succeeded | 1331 } else { // Sync succeeded |
1332 registration->set_sync_state(BACKGROUND_SYNC_STATE_SUCCESS); | 1332 registration->set_sync_state(BackgroundSyncState::SUCCESS); |
1333 registration->RunFinishedCallbacks(); | 1333 registration->RunFinishedCallbacks(); |
1334 } | 1334 } |
1335 | 1335 |
1336 if (registration->HasCompleted()) { | 1336 if (registration->HasCompleted()) { |
1337 RegistrationKey key(*registration); | 1337 RegistrationKey key(*registration); |
1338 RefCountedRegistration* active_registration = | 1338 RefCountedRegistration* active_registration = |
1339 LookupActiveRegistration(service_worker_id, key); | 1339 LookupActiveRegistration(service_worker_id, key); |
1340 if (active_registration && | 1340 if (active_registration && |
1341 active_registration->value()->id() == registration->id()) { | 1341 active_registration->value()->id() == registration->id()) { |
1342 RemoveActiveRegistration(service_worker_id, key); | 1342 RemoveActiveRegistration(service_worker_id, key); |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1507 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1507 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
1508 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1508 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1509 | 1509 |
1510 return base::Bind( | 1510 return base::Bind( |
1511 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1511 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
1512 BackgroundSyncStatus>, | 1512 BackgroundSyncStatus>, |
1513 weak_ptr_factory_.GetWeakPtr(), callback); | 1513 weak_ptr_factory_.GetWeakPtr(), callback); |
1514 } | 1514 } |
1515 | 1515 |
1516 } // namespace content | 1516 } // namespace content |
OLD | NEW |