Commit fc927941 authored by Rayan Kanso's avatar Rayan Kanso Committed by Commit Bot

[BackgroundSync] Limit calls to the Background Task Scheduler.

Keep track of the next wake up time before sending a schedule request to
the BackgroundSyncBackgroundTaskScheduler.

Bug: 1042041
Change-Id: I02ce7b418ca2546937d7768e8df2cd53bf15822e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2005230Reviewed-by: default avatarMugdha Lakhani <nator@chromium.org>
Commit-Queue: Rayan Kanso <rayankans@chromium.org>
Cr-Commit-Position: refs/heads/master@{#733824}
parent ca13511e
......@@ -85,7 +85,17 @@ void BackgroundSyncScheduler::CancelDelayedProcessing(
DCHECK(storage_partition);
auto& delayed_processing_info = GetDelayedProcessingInfoMap(sync_type);
delayed_processing_info.erase(storage_partition);
if (delayed_processing_info.count(storage_partition)) {
base::TimeTicks run_time =
delayed_processing_info[storage_partition]->desired_run_time();
// If this storage partition was scheduling the next wakeup, reset the
// wakeup time for |sync_type|.
if (scheduled_wakeup_time_[sync_type] == run_time)
scheduled_wakeup_time_[sync_type] = base::TimeTicks::Max();
delayed_processing_info.erase(storage_partition);
}
#if defined(OS_ANDROID)
ScheduleOrCancelBrowserWakeupForSyncType(sync_type, storage_partition);
......@@ -108,13 +118,7 @@ void BackgroundSyncScheduler::RunDelayedTaskAndPruneInfoMap(
DCHECK_CURRENTLY_ON(BrowserThread::UI);
std::move(delayed_task).Run();
auto& delayed_processing_info = GetDelayedProcessingInfoMap(sync_type);
delayed_processing_info.erase(storage_partition);
#if defined(OS_ANDROID)
ScheduleOrCancelBrowserWakeupForSyncType(sync_type, storage_partition);
#endif
CancelDelayedProcessing(storage_partition, sync_type);
}
#if defined(OS_ANDROID)
......@@ -133,6 +137,7 @@ void BackgroundSyncScheduler::ScheduleOrCancelBrowserWakeupForSyncType(
// If no more scheduled tasks remain, cancel browser wakeup.
// Canceling when there's no task scheduled is a no-op.
if (delayed_processing_info.empty()) {
scheduled_wakeup_time_[sync_type] = base::TimeTicks::Max();
controller->CancelBrowserWakeup(sync_type);
return;
}
......@@ -144,6 +149,15 @@ void BackgroundSyncScheduler::ScheduleOrCancelBrowserWakeupForSyncType(
return (lhs.second->desired_run_time() - base::TimeTicks::Now()) <
(rhs.second->desired_run_time() - base::TimeTicks::Now());
});
base::TimeTicks next_time = min_info.second->desired_run_time();
if (next_time >= scheduled_wakeup_time_[sync_type]) {
// There's an earlier wakeup time scheduled, no need to inform the
// scheduler.
return;
}
scheduled_wakeup_time_[sync_type] = next_time;
controller->ScheduleBrowserWakeUpWithDelay(
sync_type, min_info.second->desired_run_time() - base::TimeTicks::Now());
}
......
......@@ -57,6 +57,7 @@ class CONTENT_EXPORT BackgroundSyncScheduler
friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>;
friend class base::DeleteHelper<BackgroundSyncScheduler>;
friend class BackgroundSyncSchedulerTest;
std::map<StoragePartitionImpl*, std::unique_ptr<base::OneShotTimer>>&
GetDelayedProcessingInfoMap(blink::mojom::BackgroundSyncType sync_type);
......@@ -74,6 +75,12 @@ class CONTENT_EXPORT BackgroundSyncScheduler
std::map<StoragePartitionImpl*, std::unique_ptr<base::OneShotTimer>>
delayed_processing_info_periodic_;
std::map<blink::mojom::BackgroundSyncType, base::TimeTicks>
scheduled_wakeup_time_{
{blink::mojom::BackgroundSyncType::ONE_SHOT, base::TimeTicks::Max()},
{blink::mojom::BackgroundSyncType::PERIODIC, base::TimeTicks::Max()},
};
base::WeakPtrFactory<BackgroundSyncScheduler> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(BackgroundSyncScheduler);
......
......@@ -93,6 +93,14 @@ class BackgroundSyncSchedulerTest : public testing::Test {
return GetController()->GetBrowserWakeupDelay(sync_type);
}
base::TimeTicks GetBrowserWakeupTime() {
auto* scheduler = BackgroundSyncScheduler::GetFor(&test_browser_context_);
DCHECK(scheduler);
return scheduler
->scheduled_wakeup_time_[blink::mojom::BackgroundSyncType::ONE_SHOT];
}
void SetUp() override {
original_client_ = SetBrowserClientForTesting(&browser_client_);
}
......@@ -281,6 +289,7 @@ TEST_F(BackgroundSyncSchedulerTest,
base::RunLoop().RunUntilIdle();
EXPECT_LE(GetBrowserWakeupDelay(blink::mojom::BackgroundSyncType::ONE_SHOT),
base::TimeDelta::FromSeconds(1));
auto wakeup_time1 = GetBrowserWakeupTime();
CancelDelayedProcessing(GURL(kUrl_2),
blink::mojom::BackgroundSyncType::ONE_SHOT);
......@@ -290,6 +299,7 @@ TEST_F(BackgroundSyncSchedulerTest,
base::TimeDelta::FromMinutes(1));
EXPECT_GT(GetBrowserWakeupDelay(blink::mojom::BackgroundSyncType::ONE_SHOT),
base::TimeDelta::FromSeconds(1));
EXPECT_LT(wakeup_time1, GetBrowserWakeupTime());
}
#endif
......
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