Commit b4b90d56 authored by Mugdha Lakhani's avatar Mugdha Lakhani Committed by Commit Bot

[Background Sync] Fix bug in setting num_attempts.

We should only increment num_attempts for Periodic Background Sync if
the event has been unsuccessful. This also extracts out the logic of
calculating num_attempts into a helper method.

Bug: 989444
Change-Id: I820f91cd2799be9803295cf9baf3dbd194fd3b97
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1729078Reviewed-by: default avatarRayan Kanso <rayankans@chromium.org>
Commit-Queue: Mugdha Lakhani <nator@chromium.org>
Cr-Commit-Position: refs/heads/master@{#682712}
parent 3b795d49
......@@ -315,6 +315,28 @@ std::string GetEventStatusString(blink::ServiceWorkerStatusCode status_code) {
}
}
int GetNumAttemptsAfterEvent(BackgroundSyncType sync_type,
int current_num_attempts,
int max_attempts,
blink::mojom::BackgroundSyncState sync_state,
bool succeeded) {
int num_attempts = ++current_num_attempts;
if (sync_type == BackgroundSyncType::PERIODIC) {
if (succeeded)
return 0;
if (num_attempts == max_attempts)
return 0;
}
if (sync_state ==
blink::mojom::BackgroundSyncState::REREGISTERED_WHILE_FIRING) {
return 0;
}
return num_attempts;
}
// This prevents the browser process from shutting down when the last browser
// window is closed and there are one-shot Background Sync events ready to fire.
std::unique_ptr<BackgroundSyncController::BackgroundSyncEventKeepAlive>
......@@ -1918,16 +1940,12 @@ void BackgroundSyncManager::EventCompleteImpl(
registration->sync_state());
// It's important to update |num_attempts| before we update |delay_until|.
registration->set_num_attempts(registration->num_attempts() + 1);
if ((registration->sync_type() == BackgroundSyncType::PERIODIC &&
registration->num_attempts() == registration->max_attempts()) ||
(registration->sync_state() ==
blink::mojom::BackgroundSyncState::REREGISTERED_WHILE_FIRING)) {
registration->set_num_attempts(0);
}
bool succeeded = status_code == blink::ServiceWorkerStatusCode::kOk;
registration->set_num_attempts(GetNumAttemptsAfterEvent(
registration->sync_type(), registration->num_attempts(),
registration->max_attempts(), registration->sync_state(), succeeded));
// If |delay_until| needs to be updated, get updated delay.
bool succeeded = status_code == blink::ServiceWorkerStatusCode::kOk;
if (registration->sync_type() == BackgroundSyncType::PERIODIC ||
(!succeeded &&
registration->num_attempts() < registration->max_attempts())) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment