| 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..92dfb80b43e01e4262c2c088e569ee53519f6e18
|
| --- /dev/null
|
| +++ b/content/renderer/background_sync/background_sync_client_impl.cc
|
| @@ -0,0 +1,78 @@
|
| +// 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/embedded_worker_context_client.h"
|
| +#include "content/renderer/service_worker/service_worker_script_context.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) {
|
| +}
|
| +
|
| +void BackgroundSyncClientImpl::Sync(content::SyncRegistrationPtr registration,
|
| + int thread_id,
|
| + const SyncCallback& callback) {
|
| + WorkerTaskRunner* worker_task_runner = WorkerTaskRunner::Instance();
|
| + if (!worker_task_runner) {
|
| + callback.Run(BACKGROUND_SYNC_EVENT_STATUS_ABORT);
|
| + 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.
|
| + worker_task_runner->GetTaskRunnerFor(thread_id)->PostTask(
|
| + FROM_HERE, base::Bind(&BackgroundSyncClientImpl::DispatchSyncForWorker,
|
| + base::Unretained(this),
|
| + base::Passed(registration.Pass()), callback));
|
| +}
|
| +
|
| +void BackgroundSyncClientImpl::DispatchSyncForWorker(
|
| + content::SyncRegistrationPtr options,
|
| + const SyncCallback& callback) {
|
| + EmbeddedWorkerContextClient* client =
|
| + EmbeddedWorkerContextClient::ThreadSpecificInstance();
|
| + if (!client) {
|
| + callback.Run(BACKGROUND_SYNC_EVENT_STATUS_ABORT);
|
| + return;
|
| + }
|
| + client->script_context()->DispatchSyncEvent(
|
| + base::Bind(&BackgroundSyncClientImpl::OnSyncComplete,
|
| + weak_ptr_factory_.GetWeakPtr(), callback));
|
| +}
|
| +
|
| +void BackgroundSyncClientImpl::OnSyncComplete(
|
| + const SyncCallback& callback,
|
| + BackgroundSyncEventStatus status) {
|
| + if (!main_thread_task_runner_->RunsTasksOnCurrentThread()) {
|
| + // Re-run on main thread
|
| + main_thread_task_runner_->PostTask(
|
| + FROM_HERE, base::Bind(&BackgroundSyncClientImpl::OnSyncComplete,
|
| + base::Unretained(this), callback, status));
|
| + return;
|
| + }
|
| + callback.Run(status);
|
| +}
|
| +
|
| +} // namespace content
|
|
|