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..42fb6c239d1c18044b9be18386549e255f1c7d44 |
--- /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) { |
+ DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
+ |
+ WorkerTaskRunner* worker_task_runner = WorkerTaskRunner::Instance(); |
+ if (!worker_task_runner) { |
+ callback.Run(SERVICE_WORKER_EVENT_STATUS_ABORTED); |
+ return; |
+ } |
+ if (!worker_task_runner->GetTaskRunnerFor(thread_id)->PostTask( |
jkarlin
2015/06/19 13:51:42
Should be combined with the above conditional.
iclelland
2015/06/19 17:56:10
Done.
|
+ 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 |