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(
if (delayed_processing_scheduled(sync_type) && !can_fire_with_connectivity &&
!GetNumFiringRegistrations(sync_type)) {
// TODO(crbug.com/996166): Split out a path to cancel delayed processing.
ScheduleDelayedProcessingOfRegistrations(sync_type);
CancelDelayedProcessingOfRegistrations(sync_type);
delayed_processing_scheduled(sync_type) = false;
} else if (can_fire_with_connectivity ||
GetNumFiringRegistrations(sync_type)) {
......@@ -1851,6 +1850,13 @@ void BackgroundSyncManager::ScheduleDelayedProcessingOfRegistrations(
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(
blink::mojom::BackgroundSyncType sync_type,
bool reschedule,
......
......@@ -331,14 +331,19 @@ class CONTENT_EXPORT BackgroundSyncManager
// 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
// completed). If background processing is required it calls out to the
// BackgroundSyncController to enable it.
// completed). If background processing is required it calls out to
// BackgroundSyncProxy to enable it.
// 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
// called by FireReadyEvents.
void ScheduleDelayedProcessingOfRegistrations(
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|.
// |reschedule| is true when it's ok to schedule background processing from
// this method, false otherwise.
......
......@@ -62,20 +62,30 @@ class BackgroundSyncProxy::Core {
auto* scheduler = BackgroundSyncScheduler::GetFor(browser_context());
DCHECK(scheduler);
if (delay == base::TimeDelta::Max()) {
scheduler->CancelDelayedProcessing(
service_worker_context_->storage_partition(), sync_type);
} else {
scheduler->ScheduleDelayedProcessing(
service_worker_context_->storage_partition(), sync_type, delay,
base::BindOnce(
[](base::OnceClosure delayed_task) {
RunOrPostTaskOnThread(FROM_HERE,
ServiceWorkerContext::GetCoreThreadId(),
std::move(delayed_task));
},
std::move(delayed_task)));
}
DCHECK(delay != base::TimeDelta::Max());
scheduler->ScheduleDelayedProcessing(
service_worker_context_->storage_partition(), sync_type, delay,
base::BindOnce(
[](base::OnceClosure delayed_task) {
RunOrPostTaskOnThread(FROM_HERE,
ServiceWorkerContext::GetCoreThreadId(),
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(
......@@ -123,6 +133,15 @@ void BackgroundSyncProxy::ScheduleDelayedProcessing(
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(
std::set<url::Origin> suspended_origins) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
......
......@@ -37,6 +37,8 @@ class CONTENT_EXPORT BackgroundSyncProxy {
blink::mojom::BackgroundSyncType sync_type,
base::TimeDelta delay,
base::OnceClosure delayed_task);
virtual void CancelDelayedProcessing(
blink::mojom::BackgroundSyncType sync_type);
void SendSuspendedPeriodicSyncOrigins(
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