Skip to content

Commit

Permalink
bgsync: Simplify now that service worker is on the UI thread (pt 1)
Browse files Browse the repository at this point in the history
- Replace references to core thread to just the UI thread.
- Remove redundant mentions of "OnUIThread".
- Remove some unnecessary functions that were previously needed for
  thread hopping.
- Also move GetSoonestWakeupDelta() out of content/public since it was
  not used outside of content, and remove
  GetSoonestWakeupDeltaAcrossPartitions() which was unused.

Bug: 1138155
Change-Id: I3f13122d491c05144096861d75bff3a0613c7a2d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2788171
Commit-Queue: Matt Falkenhagen <falken@chromium.org>
Reviewed-by: Rayan Kanso <rayankans@chromium.org>
Reviewed-by: Mugdha Lakhani <nator@chromium.org>
Cr-Commit-Position: refs/heads/master@{#868039}
  • Loading branch information
mfalken authored and Chromium LUCI CQ committed Mar 31, 2021
1 parent 9709e7b commit a7e178b
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 372 deletions.
180 changes: 39 additions & 141 deletions content/browser/background_sync/background_sync_context_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@
namespace content {

BackgroundSyncContextImpl::BackgroundSyncContextImpl()
: base::RefCountedDeleteOnSequence<BackgroundSyncContextImpl>(
BrowserThread::GetTaskRunnerForThread(
ServiceWorkerContext::GetCoreThreadId())),
test_wakeup_delta_(
: test_wakeup_delta_(
{{blink::mojom::BackgroundSyncType::ONE_SHOT, base::TimeDelta::Max()},
{blink::mojom::BackgroundSyncType::PERIODIC,
base::TimeDelta::Max()}}) {}
base::TimeDelta::Max()}}) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}

BackgroundSyncContextImpl::~BackgroundSyncContextImpl() {
// The destructor must run on the core thread because it implicitly accesses
// background_sync_manager_ and services_, when it runs their destructors.
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

DCHECK(!background_sync_manager_);
DCHECK(one_shot_sync_services_.empty());
Expand All @@ -54,62 +52,45 @@ void BackgroundSyncContext::FireBackgroundSyncEventsAcrossPartitions(
}
#endif

// static
void BackgroundSyncContext::GetSoonestWakeupDeltaAcrossPartitions(
blink::mojom::BackgroundSyncType sync_type,
BrowserContext* browser_context,
base::OnceCallback<void(base::TimeDelta)> callback) {
DCHECK(browser_context);
DCHECK_CURRENTLY_ON(BrowserThread::UI);

BackgroundSyncLauncher::GetSoonestWakeupDelta(sync_type, browser_context,
std::move(callback));
}

void BackgroundSyncContextImpl::Init(
const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context,
const scoped_refptr<DevToolsBackgroundServicesContextImpl>&
devtools_context) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

RunOrPostTaskOnThread(
FROM_HERE, ServiceWorkerContext::GetCoreThreadId(),
base::BindOnce(&BackgroundSyncContextImpl::CreateBackgroundSyncManager,
this, service_worker_context, devtools_context));
CreateBackgroundSyncManager(service_worker_context, devtools_context);
}

void BackgroundSyncContextImpl::Shutdown() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
RunOrPostTaskOnThread(
FROM_HERE, ServiceWorkerContext::GetCoreThreadId(),
base::BindOnce(&BackgroundSyncContextImpl::ShutdownOnCoreThread, this));
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
one_shot_sync_services_.clear();
periodic_sync_services_.clear();
background_sync_manager_.reset();
}

void BackgroundSyncContextImpl::CreateOneShotSyncService(
mojo::PendingReceiver<blink::mojom::OneShotBackgroundSyncService>
receiver) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
RunOrPostTaskOnThread(
FROM_HERE, ServiceWorkerContext::GetCoreThreadId(),
base::BindOnce(
&BackgroundSyncContextImpl::CreateOneShotSyncServiceOnCoreThread,
this, std::move(receiver)));
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(background_sync_manager_);
one_shot_sync_services_.insert(
std::make_unique<OneShotBackgroundSyncServiceImpl>(this,
std::move(receiver)));
}

void BackgroundSyncContextImpl::CreatePeriodicSyncService(
mojo::PendingReceiver<blink::mojom::PeriodicBackgroundSyncService>
receiver) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
RunOrPostTaskOnThread(
FROM_HERE, ServiceWorkerContext::GetCoreThreadId(),
base::BindOnce(
&BackgroundSyncContextImpl::CreatePeriodicSyncServiceOnCoreThread,
this, std::move(receiver)));
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(background_sync_manager_);
periodic_sync_services_.insert(
std::make_unique<PeriodicBackgroundSyncServiceImpl>(this,
std::move(receiver)));
}

void BackgroundSyncContextImpl::OneShotSyncServiceHadConnectionError(
OneShotBackgroundSyncServiceImpl* service) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(service);

auto iter = one_shot_sync_services_.find(service);
Expand All @@ -119,7 +100,7 @@ void BackgroundSyncContextImpl::OneShotSyncServiceHadConnectionError(

void BackgroundSyncContextImpl::PeriodicSyncServiceHadConnectionError(
PeriodicBackgroundSyncServiceImpl* service) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(service);

auto iter = periodic_sync_services_.find(service);
Expand All @@ -129,14 +110,14 @@ void BackgroundSyncContextImpl::PeriodicSyncServiceHadConnectionError(

BackgroundSyncManager* BackgroundSyncContextImpl::background_sync_manager()
const {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

return background_sync_manager_.get();
}

void BackgroundSyncContextImpl::set_background_sync_manager_for_testing(
std::unique_ptr<BackgroundSyncManager> manager) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

background_sync_manager_ = std::move(manager);
}
Expand All @@ -147,42 +128,10 @@ void BackgroundSyncContextImpl::set_wakeup_delta_for_testing(
test_wakeup_delta_[sync_type] = wakeup_delta;
}

void BackgroundSyncContextImpl::GetSoonestWakeupDelta(
blink::mojom::BackgroundSyncType sync_type,
base::Time last_browser_wakeup_for_periodic_sync,
base::OnceCallback<void(base::TimeDelta)> callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);

base::TimeDelta delta = GetSoonestWakeupDeltaOnCoreThread(
sync_type, last_browser_wakeup_for_periodic_sync);
std::move(callback).Run(delta);
}

void BackgroundSyncContextImpl::RevivePeriodicBackgroundSyncRegistrations(
url::Origin origin) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
RunOrPostTaskOnThread(
FROM_HERE, ServiceWorkerContext::GetCoreThreadId(),
base::BindOnce(&BackgroundSyncContextImpl::
RevivePeriodicBackgroundSyncRegistrationsOnCoreThread,
this, std::move(origin)));
}

void BackgroundSyncContextImpl::UnregisterPeriodicSyncForOrigin(
url::Origin origin) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);

RunOrPostTaskOnThread(
FROM_HERE, ServiceWorkerContext::GetCoreThreadId(),
base::BindOnce(&BackgroundSyncContextImpl::
UnregisterPeriodicSyncForOriginOnCoreThread,
this, std::move(origin)));
}

base::TimeDelta BackgroundSyncContextImpl::GetSoonestWakeupDeltaOnCoreThread(
base::TimeDelta BackgroundSyncContextImpl::GetSoonestWakeupDelta(
blink::mojom::BackgroundSyncType sync_type,
base::Time last_browser_wakeup_for_periodic_sync) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

auto test_wakeup_delta = test_wakeup_delta_[sync_type];
if (!test_wakeup_delta.is_max())
Expand All @@ -194,19 +143,19 @@ base::TimeDelta BackgroundSyncContextImpl::GetSoonestWakeupDeltaOnCoreThread(
sync_type, last_browser_wakeup_for_periodic_sync);
}

void BackgroundSyncContextImpl::
RevivePeriodicBackgroundSyncRegistrationsOnCoreThread(url::Origin origin) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
void BackgroundSyncContextImpl::RevivePeriodicBackgroundSyncRegistrations(
url::Origin origin) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

if (!background_sync_manager_)
return;

background_sync_manager_->RevivePeriodicSyncRegistrations(std::move(origin));
}

void BackgroundSyncContextImpl::UnregisterPeriodicSyncForOriginOnCoreThread(
void BackgroundSyncContextImpl::UnregisterPeriodicSyncForOrigin(
url::Origin origin) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

if (!background_sync_manager_)
return;
Expand All @@ -217,76 +166,25 @@ void BackgroundSyncContextImpl::UnregisterPeriodicSyncForOriginOnCoreThread(
void BackgroundSyncContextImpl::FireBackgroundSyncEvents(
blink::mojom::BackgroundSyncType sync_type,
base::OnceClosure done_closure) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
RunOrPostTaskOnThread(
FROM_HERE, ServiceWorkerContext::GetCoreThreadId(),
base::BindOnce(
&BackgroundSyncContextImpl::FireBackgroundSyncEventsOnCoreThread,
this, sync_type, std::move(done_closure)));
}

void BackgroundSyncContextImpl::FireBackgroundSyncEventsOnCoreThread(
blink::mojom::BackgroundSyncType sync_type,
base::OnceClosure done_closure) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());

DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!background_sync_manager_) {
DidFireBackgroundSyncEventsOnCoreThread(std::move(done_closure));
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
std::move(done_closure));
return;
}

background_sync_manager_->FireReadyEvents(
sync_type, /* reschedule= */ false,
base::BindOnce(
&BackgroundSyncContextImpl::DidFireBackgroundSyncEventsOnCoreThread,
this, std::move(done_closure)));
}

void BackgroundSyncContextImpl::DidFireBackgroundSyncEventsOnCoreThread(
base::OnceClosure done_closure) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());

// Use PostTask() rather than RunOrPostTaskOnThread() to ensure the callback
// is called asynchronously.
GetUIThreadTaskRunner({})->PostTask(FROM_HERE, std::move(done_closure));
background_sync_manager_->FireReadyEvents(sync_type, /* reschedule= */ false,
std::move(done_closure));
}

void BackgroundSyncContextImpl::CreateBackgroundSyncManager(
scoped_refptr<ServiceWorkerContextWrapper> service_worker_context,
scoped_refptr<DevToolsBackgroundServicesContextImpl> devtools_context) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!background_sync_manager_);

background_sync_manager_ = BackgroundSyncManager::Create(
std::move(service_worker_context), std::move(devtools_context));
}

void BackgroundSyncContextImpl::CreateOneShotSyncServiceOnCoreThread(
mojo::PendingReceiver<blink::mojom::OneShotBackgroundSyncService>
receiver) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
DCHECK(background_sync_manager_);
one_shot_sync_services_.insert(
std::make_unique<OneShotBackgroundSyncServiceImpl>(this,
std::move(receiver)));
}

void BackgroundSyncContextImpl::CreatePeriodicSyncServiceOnCoreThread(
mojo::PendingReceiver<blink::mojom::PeriodicBackgroundSyncService>
receiver) {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
DCHECK(background_sync_manager_);
periodic_sync_services_.insert(
std::make_unique<PeriodicBackgroundSyncServiceImpl>(this,
std::move(receiver)));
}

void BackgroundSyncContextImpl::ShutdownOnCoreThread() {
DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());

one_shot_sync_services_.clear();
periodic_sync_services_.clear();
background_sync_manager_.reset();
}

} // namespace content

0 comments on commit a7e178b

Please sign in to comment.