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

[Background Sync] Add CancelDelayedProcessing()

Before this change, we special case base::TimeDelta::Max() to mean we'd
like to cancel delayed processing of Background Sync registrations.

This introduces a CancelDelayedProcessing() flow to make code more
readable and get rid of this special case.

Bug: 996166
Change-Id: Iee5f78a8e8f9e9d2abd8cc1e3e78778807829011
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1906408
Commit-Queue: Mugdha Lakhani <nator@chromium.org>
Reviewed-by: default avatarRayan Kanso <rayankans@chromium.org>
Cr-Commit-Position: refs/heads/master@{#715282}
parent 5c1d8295
...@@ -1522,8 +1522,7 @@ void BackgroundSyncManager::ScheduleOrCancelDelayedProcessing( ...@@ -1522,8 +1522,7 @@ void BackgroundSyncManager::ScheduleOrCancelDelayedProcessing(
if (delayed_processing_scheduled(sync_type) && !can_fire_with_connectivity && if (delayed_processing_scheduled(sync_type) && !can_fire_with_connectivity &&
!GetNumFiringRegistrations(sync_type)) { !GetNumFiringRegistrations(sync_type)) {
// TODO(crbug.com/996166): Split out a path to cancel delayed processing. CancelDelayedProcessingOfRegistrations(sync_type);
ScheduleDelayedProcessingOfRegistrations(sync_type);
delayed_processing_scheduled(sync_type) = false; delayed_processing_scheduled(sync_type) = false;
} else if (can_fire_with_connectivity || } else if (can_fire_with_connectivity ||
GetNumFiringRegistrations(sync_type)) { GetNumFiringRegistrations(sync_type)) {
...@@ -1851,6 +1850,13 @@ void BackgroundSyncManager::ScheduleDelayedProcessingOfRegistrations( ...@@ -1851,6 +1850,13 @@ void BackgroundSyncManager::ScheduleDelayedProcessingOfRegistrations(
std::move(fire_events_callback)); std::move(fire_events_callback));
} }
void BackgroundSyncManager::CancelDelayedProcessingOfRegistrations(
blink::mojom::BackgroundSyncType sync_type) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
proxy_->CancelDelayedProcessing(sync_type);
}
void BackgroundSyncManager::FireReadyEvents( void BackgroundSyncManager::FireReadyEvents(
blink::mojom::BackgroundSyncType sync_type, blink::mojom::BackgroundSyncType sync_type,
bool reschedule, bool reschedule,
......
...@@ -331,14 +331,19 @@ class CONTENT_EXPORT BackgroundSyncManager ...@@ -331,14 +331,19 @@ class CONTENT_EXPORT BackgroundSyncManager
// Determines if the browser needs to be able to run in the background (e.g., // Determines if the browser needs to be able to run in the background (e.g.,
// to run a pending registration or verify that a firing registration // to run a pending registration or verify that a firing registration
// completed). If background processing is required it calls out to the // completed). If background processing is required it calls out to
// BackgroundSyncController to enable it. // BackgroundSyncProxy to enable it.
// Assumes that all registrations in the pending state are not currently ready // Assumes that all registrations in the pending state are not currently ready
// to fire. Therefore this should not be called directly and should only be // to fire. Therefore this should not be called directly and should only be
// called by FireReadyEvents. // called by FireReadyEvents.
void ScheduleDelayedProcessingOfRegistrations( void ScheduleDelayedProcessingOfRegistrations(
blink::mojom::BackgroundSyncType sync_type); blink::mojom::BackgroundSyncType sync_type);
// Cancels waking up of the browser to process (Periodic) BackgroundSync
// registrations.
void CancelDelayedProcessingOfRegistrations(
blink::mojom::BackgroundSyncType sync_type);
// Fires ready events for |sync_type|. // Fires ready events for |sync_type|.
// |reschedule| is true when it's ok to schedule background processing from // |reschedule| is true when it's ok to schedule background processing from
// this method, false otherwise. // this method, false otherwise.
......
...@@ -62,20 +62,30 @@ class BackgroundSyncProxy::Core { ...@@ -62,20 +62,30 @@ class BackgroundSyncProxy::Core {
auto* scheduler = BackgroundSyncScheduler::GetFor(browser_context()); auto* scheduler = BackgroundSyncScheduler::GetFor(browser_context());
DCHECK(scheduler); DCHECK(scheduler);
if (delay == base::TimeDelta::Max()) { DCHECK(delay != base::TimeDelta::Max());
scheduler->CancelDelayedProcessing(
service_worker_context_->storage_partition(), sync_type); scheduler->ScheduleDelayedProcessing(
} else { service_worker_context_->storage_partition(), sync_type, delay,
scheduler->ScheduleDelayedProcessing( base::BindOnce(
service_worker_context_->storage_partition(), sync_type, delay, [](base::OnceClosure delayed_task) {
base::BindOnce( RunOrPostTaskOnThread(FROM_HERE,
[](base::OnceClosure delayed_task) { ServiceWorkerContext::GetCoreThreadId(),
RunOrPostTaskOnThread(FROM_HERE, std::move(delayed_task));
ServiceWorkerContext::GetCoreThreadId(), },
std::move(delayed_task)); std::move(delayed_task)));
}, }
std::move(delayed_task)));
} void CancelDelayedProcessing(blink::mojom::BackgroundSyncType sync_type) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!browser_context())
return;
auto* scheduler = BackgroundSyncScheduler::GetFor(browser_context());
DCHECK(scheduler);
scheduler->CancelDelayedProcessing(
service_worker_context_->storage_partition(), sync_type);
} }
void SendSuspendedPeriodicSyncOrigins( void SendSuspendedPeriodicSyncOrigins(
...@@ -123,6 +133,15 @@ void BackgroundSyncProxy::ScheduleDelayedProcessing( ...@@ -123,6 +133,15 @@ void BackgroundSyncProxy::ScheduleDelayedProcessing(
sync_type, delay, std::move(delayed_task))); sync_type, delay, std::move(delayed_task)));
} }
void BackgroundSyncProxy::CancelDelayedProcessing(
blink::mojom::BackgroundSyncType sync_type) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
RunOrPostTaskOnThread(FROM_HERE, BrowserThread::UI,
base::BindOnce(&Core::CancelDelayedProcessing,
ui_core_weak_ptr_, sync_type));
}
void BackgroundSyncProxy::SendSuspendedPeriodicSyncOrigins( void BackgroundSyncProxy::SendSuspendedPeriodicSyncOrigins(
std::set<url::Origin> suspended_origins) { std::set<url::Origin> suspended_origins) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId()); DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
......
...@@ -37,6 +37,8 @@ class CONTENT_EXPORT BackgroundSyncProxy { ...@@ -37,6 +37,8 @@ class CONTENT_EXPORT BackgroundSyncProxy {
blink::mojom::BackgroundSyncType sync_type, blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay, base::TimeDelta delay,
base::OnceClosure delayed_task); base::OnceClosure delayed_task);
virtual void CancelDelayedProcessing(
blink::mojom::BackgroundSyncType sync_type);
void SendSuspendedPeriodicSyncOrigins( void SendSuspendedPeriodicSyncOrigins(
std::set<url::Origin> suspended_origins); std::set<url::Origin> suspended_origins);
......
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