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 510 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 AreOptionConditionsMet(options) | 521 AreOptionConditionsMet(options) |
522 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE | 522 ? BackgroundSyncMetrics::REGISTRATION_COULD_FIRE |
523 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; | 523 : BackgroundSyncMetrics::REGISTRATION_COULD_NOT_FIRE; |
524 BackgroundSyncMetrics::CountRegisterSuccess( | 524 BackgroundSyncMetrics::CountRegisterSuccess( |
525 existing_registration->options()->periodicity, | 525 existing_registration->options()->periodicity, |
526 registration_could_fire, | 526 registration_could_fire, |
527 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE); | 527 BackgroundSyncMetrics::REGISTRATION_IS_DUPLICATE); |
528 | 528 |
529 if (existing_registration->IsFiring()) { | 529 if (existing_registration->IsFiring()) { |
530 existing_registration->set_sync_state( | 530 existing_registration->set_sync_state( |
531 BACKGROUND_SYNC_STATE_REREGISTERED_WHILE_FIRING); | 531 BackgroundSyncState::REREGISTERED_WHILE_FIRING); |
532 } | 532 } |
533 | 533 |
534 base::ThreadTaskRunnerHandle::Get()->PostTask( | 534 base::ThreadTaskRunnerHandle::Get()->PostTask( |
535 FROM_HERE, | 535 FROM_HERE, |
536 base::Bind( | 536 base::Bind( |
537 callback, BACKGROUND_SYNC_STATUS_OK, | 537 callback, BACKGROUND_SYNC_STATUS_OK, |
538 base::Passed(CreateRegistrationHandle(existing_registration_ref)))); | 538 base::Passed(CreateRegistrationHandle(existing_registration_ref)))); |
539 return; | 539 return; |
540 } else { | 540 } else { |
541 existing_registration_ref->value()->SetUnregisteredState(); | 541 existing_registration_ref->value()->SetUnregisteredState(); |
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
958 } | 958 } |
959 | 959 |
960 void BackgroundSyncManager::NotifyWhenFinished( | 960 void BackgroundSyncManager::NotifyWhenFinished( |
961 BackgroundSyncRegistrationHandle::HandleId handle_id, | 961 BackgroundSyncRegistrationHandle::HandleId handle_id, |
962 const StatusAndStateCallback& callback) { | 962 const StatusAndStateCallback& callback) { |
963 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 963 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
964 | 964 |
965 if (disabled_) { | 965 if (disabled_) { |
966 base::ThreadTaskRunnerHandle::Get()->PostTask( | 966 base::ThreadTaskRunnerHandle::Get()->PostTask( |
967 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 967 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
968 BACKGROUND_SYNC_STATE_FAILED)); | 968 BackgroundSyncState::FAILED)); |
969 return; | 969 return; |
970 } | 970 } |
971 | 971 |
972 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = | 972 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = |
973 DuplicateRegistrationHandle(handle_id); | 973 DuplicateRegistrationHandle(handle_id); |
974 | 974 |
975 op_scheduler_.ScheduleOperation( | 975 op_scheduler_.ScheduleOperation( |
976 base::Bind(&BackgroundSyncManager::NotifyWhenFinishedImpl, | 976 base::Bind(&BackgroundSyncManager::NotifyWhenFinishedImpl, |
977 weak_ptr_factory_.GetWeakPtr(), | 977 weak_ptr_factory_.GetWeakPtr(), |
978 base::Passed(std::move(registration_handle)), callback)); | 978 base::Passed(std::move(registration_handle)), callback)); |
979 } | 979 } |
980 | 980 |
981 void BackgroundSyncManager::NotifyWhenFinishedImpl( | 981 void BackgroundSyncManager::NotifyWhenFinishedImpl( |
982 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, | 982 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, |
983 const StatusAndStateCallback& callback) { | 983 const StatusAndStateCallback& callback) { |
984 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 984 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
985 DCHECK_EQ(SYNC_ONE_SHOT, registration_handle->options()->periodicity); | 985 DCHECK_EQ(SYNC_ONE_SHOT, registration_handle->options()->periodicity); |
986 | 986 |
987 if (disabled_) { | 987 if (disabled_) { |
988 base::ThreadTaskRunnerHandle::Get()->PostTask( | 988 base::ThreadTaskRunnerHandle::Get()->PostTask( |
989 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, | 989 FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
990 BACKGROUND_SYNC_STATE_FAILED)); | 990 BackgroundSyncState::FAILED)); |
991 return; | 991 return; |
992 } | 992 } |
993 | 993 |
994 if (!registration_handle->registration()->HasCompleted()) { | 994 if (!registration_handle->registration()->HasCompleted()) { |
995 registration_handle->registration()->AddFinishedCallback( | 995 registration_handle->registration()->AddFinishedCallback( |
996 base::Bind(&BackgroundSyncManager::NotifyWhenFinishedInvokeCallback, | 996 base::Bind(&BackgroundSyncManager::NotifyWhenFinishedInvokeCallback, |
997 weak_ptr_factory_.GetWeakPtr(), callback)); | 997 weak_ptr_factory_.GetWeakPtr(), callback)); |
998 op_scheduler_.CompleteOperationAndRunNext(); | 998 op_scheduler_.CompleteOperationAndRunNext(); |
999 return; | 999 return; |
1000 } | 1000 } |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1080 } | 1080 } |
1081 | 1081 |
1082 bool BackgroundSyncManager::IsRegistrationReadyToFire( | 1082 bool BackgroundSyncManager::IsRegistrationReadyToFire( |
1083 const BackgroundSyncRegistration& registration) { | 1083 const BackgroundSyncRegistration& registration) { |
1084 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1084 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1085 | 1085 |
1086 // TODO(jkarlin): Add support for firing periodic registrations. | 1086 // TODO(jkarlin): Add support for firing periodic registrations. |
1087 if (registration.options()->periodicity == SYNC_PERIODIC) | 1087 if (registration.options()->periodicity == SYNC_PERIODIC) |
1088 return false; | 1088 return false; |
1089 | 1089 |
1090 if (registration.sync_state() != BACKGROUND_SYNC_STATE_PENDING) | 1090 if (registration.sync_state() != BackgroundSyncState::PENDING) |
1091 return false; | 1091 return false; |
1092 | 1092 |
1093 if (clock_->Now() < registration.delay_until()) | 1093 if (clock_->Now() < registration.delay_until()) |
1094 return false; | 1094 return false; |
1095 | 1095 |
1096 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); | 1096 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); |
1097 | 1097 |
1098 return AreOptionConditionsMet(*registration.options()); | 1098 return AreOptionConditionsMet(*registration.options()); |
1099 } | 1099 } |
1100 | 1100 |
1101 void BackgroundSyncManager::RunInBackgroundIfNecessary() { | 1101 void BackgroundSyncManager::RunInBackgroundIfNecessary() { |
1102 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1102 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1103 base::TimeDelta soonest_wakeup_delta = base::TimeDelta::Max(); | 1103 base::TimeDelta soonest_wakeup_delta = base::TimeDelta::Max(); |
1104 | 1104 |
1105 for (const auto& sw_id_and_registrations : active_registrations_) { | 1105 for (const auto& sw_id_and_registrations : active_registrations_) { |
1106 for (const auto& key_and_registration : | 1106 for (const auto& key_and_registration : |
1107 sw_id_and_registrations.second.registration_map) { | 1107 sw_id_and_registrations.second.registration_map) { |
1108 const BackgroundSyncRegistration& registration = | 1108 const BackgroundSyncRegistration& registration = |
1109 *key_and_registration.second->value(); | 1109 *key_and_registration.second->value(); |
1110 if (registration.sync_state() == BACKGROUND_SYNC_STATE_PENDING) { | 1110 if (registration.sync_state() == BackgroundSyncState::PENDING) { |
1111 if (registration.options()->periodicity == SYNC_ONE_SHOT) { | 1111 if (registration.options()->periodicity == SYNC_ONE_SHOT) { |
1112 if (clock_->Now() >= registration.delay_until()) { | 1112 if (clock_->Now() >= registration.delay_until()) { |
1113 soonest_wakeup_delta = base::TimeDelta(); | 1113 soonest_wakeup_delta = base::TimeDelta(); |
1114 } else { | 1114 } else { |
1115 base::TimeDelta delay_delta = | 1115 base::TimeDelta delay_delta = |
1116 registration.delay_until() - clock_->Now(); | 1116 registration.delay_until() - clock_->Now(); |
1117 if (delay_delta < soonest_wakeup_delta) | 1117 if (delay_delta < soonest_wakeup_delta) |
1118 soonest_wakeup_delta = delay_delta; | 1118 soonest_wakeup_delta = delay_delta; |
1119 } | 1119 } |
1120 } else { | 1120 } else { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1178 for (auto& key_and_registration : | 1178 for (auto& key_and_registration : |
1179 sw_id_and_registrations.second.registration_map) { | 1179 sw_id_and_registrations.second.registration_map) { |
1180 BackgroundSyncRegistration* registration = | 1180 BackgroundSyncRegistration* registration = |
1181 key_and_registration.second->value(); | 1181 key_and_registration.second->value(); |
1182 if (IsRegistrationReadyToFire(*registration)) { | 1182 if (IsRegistrationReadyToFire(*registration)) { |
1183 sw_id_and_keys_to_fire.push_back( | 1183 sw_id_and_keys_to_fire.push_back( |
1184 std::make_pair(service_worker_id, key_and_registration.first)); | 1184 std::make_pair(service_worker_id, key_and_registration.first)); |
1185 // The state change is not saved to persistent storage because | 1185 // The state change is not saved to persistent storage because |
1186 // if the sync event is killed mid-sync then it should return to | 1186 // if the sync event is killed mid-sync then it should return to |
1187 // SYNC_STATE_PENDING. | 1187 // SYNC_STATE_PENDING. |
1188 registration->set_sync_state(BACKGROUND_SYNC_STATE_FIRING); | 1188 registration->set_sync_state(BackgroundSyncState::FIRING); |
1189 } | 1189 } |
1190 } | 1190 } |
1191 } | 1191 } |
1192 | 1192 |
1193 if (sw_id_and_keys_to_fire.empty()) { | 1193 if (sw_id_and_keys_to_fire.empty()) { |
1194 RunInBackgroundIfNecessary(); | 1194 RunInBackgroundIfNecessary(); |
1195 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 1195 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
1196 base::Bind(callback)); | 1196 base::Bind(callback)); |
1197 return; | 1197 return; |
1198 } | 1198 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1254 CreateRegistrationHandle(registration); | 1254 CreateRegistrationHandle(registration); |
1255 | 1255 |
1256 num_firing_registrations_ += 1; | 1256 num_firing_registrations_ += 1; |
1257 | 1257 |
1258 BackgroundSyncRegistrationHandle::HandleId handle_id = | 1258 BackgroundSyncRegistrationHandle::HandleId handle_id = |
1259 registration_handle->handle_id(); | 1259 registration_handle->handle_id(); |
1260 | 1260 |
1261 BackgroundSyncEventLastChance last_chance = | 1261 BackgroundSyncEventLastChance last_chance = |
1262 registration->value()->num_attempts() == | 1262 registration->value()->num_attempts() == |
1263 parameters_->max_sync_attempts - 1 | 1263 parameters_->max_sync_attempts - 1 |
1264 ? BACKGROUND_SYNC_EVENT_LAST_CHANCE_IS_LAST_CHANCE | 1264 ? BackgroundSyncEventLastChance::IS_LAST_CHANCE |
1265 : BACKGROUND_SYNC_EVENT_LAST_CHANCE_IS_NOT_LAST_CHANCE; | 1265 : BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE; |
1266 | 1266 |
1267 HasMainFrameProviderHost( | 1267 HasMainFrameProviderHost( |
1268 service_worker_registration->pattern().GetOrigin(), | 1268 service_worker_registration->pattern().GetOrigin(), |
1269 base::Bind(&BackgroundSyncMetrics::RecordEventStarted, | 1269 base::Bind(&BackgroundSyncMetrics::RecordEventStarted, |
1270 registration->value()->options()->periodicity)); | 1270 registration->value()->options()->periodicity)); |
1271 | 1271 |
1272 FireOneShotSync( | 1272 FireOneShotSync( |
1273 handle_id, service_worker_registration->active_version(), last_chance, | 1273 handle_id, service_worker_registration->active_version(), last_chance, |
1274 base::Bind(&BackgroundSyncManager::EventComplete, | 1274 base::Bind(&BackgroundSyncManager::EventComplete, |
1275 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, | 1275 weak_ptr_factory_.GetWeakPtr(), service_worker_registration, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1331 if (sw_registration) { | 1331 if (sw_registration) { |
1332 HasMainFrameProviderHost( | 1332 HasMainFrameProviderHost( |
1333 sw_registration->pattern().GetOrigin(), | 1333 sw_registration->pattern().GetOrigin(), |
1334 base::Bind(&BackgroundSyncMetrics::RecordEventResult, | 1334 base::Bind(&BackgroundSyncMetrics::RecordEventResult, |
1335 registration->options()->periodicity, | 1335 registration->options()->periodicity, |
1336 status_code == SERVICE_WORKER_OK)); | 1336 status_code == SERVICE_WORKER_OK)); |
1337 } | 1337 } |
1338 | 1338 |
1339 if (registration->options()->periodicity == SYNC_ONE_SHOT) { | 1339 if (registration->options()->periodicity == SYNC_ONE_SHOT) { |
1340 if (registration->sync_state() == | 1340 if (registration->sync_state() == |
1341 BACKGROUND_SYNC_STATE_REREGISTERED_WHILE_FIRING) { | 1341 BackgroundSyncState::REREGISTERED_WHILE_FIRING) { |
1342 registration->set_sync_state(BACKGROUND_SYNC_STATE_PENDING); | 1342 registration->set_sync_state(BackgroundSyncState::PENDING); |
1343 registration->set_num_attempts(0); | 1343 registration->set_num_attempts(0); |
1344 } else if (status_code != SERVICE_WORKER_OK) { // Sync failed | 1344 } else if (status_code != SERVICE_WORKER_OK) { // Sync failed |
1345 bool can_retry = | 1345 bool can_retry = |
1346 registration->num_attempts() < parameters_->max_sync_attempts; | 1346 registration->num_attempts() < parameters_->max_sync_attempts; |
1347 if (registration->sync_state() == | 1347 if (registration->sync_state() == |
1348 BACKGROUND_SYNC_STATE_UNREGISTERED_WHILE_FIRING) { | 1348 BackgroundSyncState::UNREGISTERED_WHILE_FIRING) { |
1349 registration->set_sync_state(can_retry | 1349 registration->set_sync_state(can_retry |
1350 ? BACKGROUND_SYNC_STATE_UNREGISTERED | 1350 ? BackgroundSyncState::UNREGISTERED |
1351 : BACKGROUND_SYNC_STATE_FAILED); | 1351 : BackgroundSyncState::FAILED); |
1352 registration->RunFinishedCallbacks(); | 1352 registration->RunFinishedCallbacks(); |
1353 } else if (can_retry) { | 1353 } else if (can_retry) { |
1354 registration->set_sync_state(BACKGROUND_SYNC_STATE_PENDING); | 1354 registration->set_sync_state(BackgroundSyncState::PENDING); |
1355 registration->set_delay_until( | 1355 registration->set_delay_until( |
1356 clock_->Now() + | 1356 clock_->Now() + |
1357 parameters_->initial_retry_delay * | 1357 parameters_->initial_retry_delay * |
1358 pow(parameters_->retry_delay_factor, | 1358 pow(parameters_->retry_delay_factor, |
1359 registration->num_attempts() - 1)); | 1359 registration->num_attempts() - 1)); |
1360 } else { | 1360 } else { |
1361 registration->set_sync_state(BACKGROUND_SYNC_STATE_FAILED); | 1361 registration->set_sync_state(BackgroundSyncState::FAILED); |
1362 registration->RunFinishedCallbacks(); | 1362 registration->RunFinishedCallbacks(); |
1363 } | 1363 } |
1364 } else { // Sync succeeded | 1364 } else { // Sync succeeded |
1365 registration->set_sync_state(BACKGROUND_SYNC_STATE_SUCCESS); | 1365 registration->set_sync_state(BackgroundSyncState::SUCCESS); |
1366 registration->RunFinishedCallbacks(); | 1366 registration->RunFinishedCallbacks(); |
1367 } | 1367 } |
1368 | 1368 |
1369 if (registration->HasCompleted()) { | 1369 if (registration->HasCompleted()) { |
1370 RegistrationKey key(*registration); | 1370 RegistrationKey key(*registration); |
1371 RefCountedRegistration* active_registration = | 1371 RefCountedRegistration* active_registration = |
1372 LookupActiveRegistration(service_worker_id, key); | 1372 LookupActiveRegistration(service_worker_id, key); |
1373 if (active_registration && | 1373 if (active_registration && |
1374 active_registration->value()->id() == registration->id()) { | 1374 active_registration->value()->id() == registration->id()) { |
1375 RemoveActiveRegistration(service_worker_id, key); | 1375 RemoveActiveRegistration(service_worker_id, key); |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1540 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1540 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
1541 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1541 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1542 | 1542 |
1543 return base::Bind( | 1543 return base::Bind( |
1544 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1544 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
1545 BackgroundSyncStatus>, | 1545 BackgroundSyncStatus>, |
1546 weak_ptr_factory_.GetWeakPtr(), callback); | 1546 weak_ptr_factory_.GetWeakPtr(), callback); |
1547 } | 1547 } |
1548 | 1548 |
1549 } // namespace content | 1549 } // namespace content |
OLD | NEW |