Chromium Code Reviews| Index: content/renderer/background_sync/background_sync_client_impl.cc |
| diff --git a/content/renderer/background_sync/background_sync_client_impl.cc b/content/renderer/background_sync/background_sync_client_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0b9ff777ff73c90e91cd419f659b7377c66d84c4 |
| --- /dev/null |
| +++ b/content/renderer/background_sync/background_sync_client_impl.cc |
| @@ -0,0 +1,89 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/renderer/background_sync/background_sync_client_impl.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/location.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "content/child/worker_task_runner.h" |
| +#include "content/renderer/service_worker/service_worker_context_client.h" |
| + |
| +namespace content { |
| + |
| +// static |
| +void BackgroundSyncClientImpl::Create( |
| + const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, |
| + mojo::InterfaceRequest<BackgroundSyncServiceClient> request) { |
| + new BackgroundSyncClientImpl(main_thread_task_runner, request.Pass()); |
| +} |
| + |
| +BackgroundSyncClientImpl::~BackgroundSyncClientImpl() { |
| +} |
| + |
| +BackgroundSyncClientImpl::BackgroundSyncClientImpl( |
| + const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, |
| + mojo::InterfaceRequest<BackgroundSyncServiceClient> request) |
| + : binding_(this, request.Pass()), |
| + main_thread_task_runner_(main_thread_task_runner), |
| + weak_ptr_factory_(this) { |
| + DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| +} |
| + |
| +void BackgroundSyncClientImpl::Sync(content::SyncRegistrationPtr registration, |
| + int thread_id, |
| + const SyncCallback& callback) { |
| + WorkerTaskRunner* worker_task_runner = WorkerTaskRunner::Instance(); |
| + DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
|
jkarlin
2015/06/18 18:13:56
DCHECK should be first line, ideally with a newlin
iclelland
2015/06/19 13:44:32
Done.
|
| + if (!worker_task_runner) { |
| + callback.Run(SERVICE_WORKER_EVENT_STATUS_ABORTED); |
| + return; |
| + } |
| + // TODO(iclelland): If the worker is dead, GetTaskRunnerFor(thread_id) will |
| + // still return a task runner. Detect that state and call callback with an |
| + // abort status. |
|
jkarlin
2015/06/18 18:13:56
PostTask will return false on a deleted task runne
iclelland
2015/06/19 13:44:32
Done. Thanks.
|
| + worker_task_runner->GetTaskRunnerFor(thread_id)->PostTask( |
| + FROM_HERE, |
| + base::Bind(&BackgroundSyncClientImpl::DispatchSyncOnWorkerThread, |
| + main_thread_task_runner_, base::Passed(registration.Pass()), |
| + callback)); |
| +} |
| + |
| +// static |
| +void BackgroundSyncClientImpl::DispatchSyncOnWorkerThread( |
| + const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, |
| + content::SyncRegistrationPtr registration, |
| + const SyncCallback& callback) { |
| + ServiceWorkerContextClient* client = |
| + ServiceWorkerContextClient::ThreadSpecificInstance(); |
| + if (!client) { |
| + OnSyncCompleteOnWorkerThread(main_thread_task_runner, callback, |
| + SERVICE_WORKER_EVENT_STATUS_ABORTED); |
| + return; |
| + } |
| + client->DispatchSyncEvent( |
| + base::Bind(&BackgroundSyncClientImpl::OnSyncCompleteOnWorkerThread, |
| + main_thread_task_runner, callback)); |
| +} |
| + |
| +// static |
| +void BackgroundSyncClientImpl::OnSyncCompleteOnWorkerThread( |
| + const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, |
| + const SyncCallback& callback, |
| + ServiceWorkerEventStatus status) { |
| + // Run callback on main thread |
| + main_thread_task_runner->PostTask( |
| + FROM_HERE, |
| + base::Bind(&BackgroundSyncClientImpl::OnSyncCompleteOnMainThread, |
|
jkarlin
2015/06/18 18:13:57
base::Bind(callback, status) and then delete OnSyn
iclelland
2015/06/19 13:44:32
This doesn't appear to be possible; callback is a
|
| + callback, status)); |
| +} |
| + |
| +// static |
| +void BackgroundSyncClientImpl::OnSyncCompleteOnMainThread( |
| + const SyncCallback& callback, |
| + ServiceWorkerEventStatus status) { |
| + callback.Run(status); |
| +} |
| + |
| +} // namespace content |