Commit 77b33572 authored by Mugdha Lakhani's avatar Mugdha Lakhani Committed by Commit Bot

[Background Sync] Update delay_until calculation.

For Periodic Background Sync, we decide the next event time for a
registration based on other existing periodicsync registrations for this
origin. Skip this check for one-shot Background Sync registrations.

Currently, if we have both a one-shot and a periodic Background Sync
registration for a given origin, this check will be applied to the
one-shot sync registration as well. This is unnecessary. This CL removes
it and adds a unit test.

Bug: 989003
Change-Id: Iff2b1d0444878444c6c1743f6ae0882caca86dc8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1725638Reviewed-by: default avatarRayan Kanso <rayankans@chromium.org>
Commit-Queue: Mugdha Lakhani <nator@chromium.org>
Cr-Commit-Position: refs/heads/master@{#682268}
parent 7b94b055
......@@ -895,7 +895,7 @@ void BackgroundSyncManager::RegisterDidGetDelay(
// registration, so set delay_until to override its default value.
if (registration.sync_type() == BackgroundSyncType::PERIODIC) {
registration.set_delay_until(GetDelayUntilAfterApplyingMinGapForOrigin(
registration.origin(), delay));
BackgroundSyncType::PERIODIC, registration.origin(), delay));
}
ServiceWorkerRegistration* sw_registration =
......@@ -1489,11 +1489,15 @@ base::TimeDelta BackgroundSyncManager::MaybeApplyBrowserWakeupCountLimit(
}
base::Time BackgroundSyncManager::GetDelayUntilAfterApplyingMinGapForOrigin(
BackgroundSyncType sync_type,
const url::Origin& origin,
base::TimeDelta delay) const {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
base::Time now_plus_delay = clock_->Now() + delay;
if (sync_type != BackgroundSyncType::PERIODIC)
return now_plus_delay;
base::Time soonest_wakeup_time_for_origin =
GetSoonestPeriodicSyncEventTimeForOrigin(origin);
if (soonest_wakeup_time_for_origin.is_null())
......@@ -1981,7 +1985,7 @@ void BackgroundSyncManager::EventCompleteDidGetDelay(
registration->set_sync_state(blink::mojom::BackgroundSyncState::PENDING);
registration_completed = false;
registration->set_delay_until(GetDelayUntilAfterApplyingMinGapForOrigin(
registration->origin(), delay));
registration->sync_type(), registration->origin(), delay));
std::string event_name = GetSyncEventName(registration->sync_type()) +
(succeeded ? " event completed" : " event failed");
......
......@@ -170,7 +170,9 @@ class CONTENT_EXPORT BackgroundSyncManager
// correct starting point to add to |delay| to so that the resulting
// |delay_until| for the |registration| ensures the minimum gap between
// periodicsync events fired for the origin.
// |delay| is only updated if |sync_type| is periodic.
base::Time GetDelayUntilAfterApplyingMinGapForOrigin(
blink::mojom::BackgroundSyncType sync_type,
const url::Origin& origin,
base::TimeDelta delay) const;
......
......@@ -1274,6 +1274,38 @@ TEST_F(BackgroundSyncManagerTest, TestSupensionAndRevival) {
Unregister(sync_options_2_);
}
TEST_F(BackgroundSyncManagerTest, CrossRegistrationLimitsForOrigin) {
InitPeriodicSyncEventTest();
base::TimeDelta thirteen_hours = base::TimeDelta::FromHours(13);
sync_options_1_.min_interval = thirteen_hours.InMilliseconds();
EXPECT_TRUE(Register(sync_options_1_));
EXPECT_EQ(0, periodic_sync_events_called_);
EXPECT_TRUE(GetRegistration(sync_options_1_));
// Register another periodic sync with the same origin, but a smaller
// minInterval. Expect the delay to be set to the larger minInterval from the
// already registered periodic sync.
base::TimeDelta twelve_hours = base::TimeDelta::FromHours(12);
sync_options_2_.min_interval = twelve_hours.InMilliseconds();
EXPECT_TRUE(Register(sync_options_2_));
EXPECT_EQ(0, periodic_sync_events_called_);
EXPECT_TRUE(GetRegistration(sync_options_2_));
// Advance clock.
test_clock_.Advance(twelve_hours);
FireReadyEvents();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, periodic_sync_events_called_);
test_clock_.Advance(base::TimeDelta::FromHours(1));
FireReadyEvents();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(2, periodic_sync_events_called_);
}
TEST_F(BackgroundSyncManagerTest, ReregisterMidSyncFirstAttemptFails) {
InitDelayedSyncEventTest();
RegisterAndVerifySyncEventDelayed(sync_options_1_);
......
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