| 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..de28fb02f8d2d5980e16003e983fdc76de260b1b
|
| --- /dev/null
|
| +++ b/content/renderer/background_sync/background_sync_client_impl.cc
|
| @@ -0,0 +1,86 @@
|
| +// 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) {
|
| + DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread());
|
| +
|
| + WorkerTaskRunner* worker_task_runner = WorkerTaskRunner::Instance();
|
| + if (!worker_task_runner ||
|
| + !worker_task_runner->GetTaskRunnerFor(thread_id)->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&BackgroundSyncClientImpl::DispatchSyncOnWorkerThread,
|
| + main_thread_task_runner_,
|
| + base::Passed(registration.Pass()), callback))) {
|
| + callback.Run(SERVICE_WORKER_EVENT_STATUS_ABORTED);
|
| + }
|
| +}
|
| +
|
| +// 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,
|
| + callback, status));
|
| +}
|
| +
|
| +// static
|
| +void BackgroundSyncClientImpl::OnSyncCompleteOnMainThread(
|
| + const SyncCallback& callback,
|
| + ServiceWorkerEventStatus status) {
|
| + callback.Run(status);
|
| +}
|
| +
|
| +} // namespace content
|
|
|