OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/background_sync/background_sync_client_impl.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
| 9 #include "base/memory/weak_ptr.h" |
| 10 #include "content/child/worker_task_runner.h" |
| 11 #include "content/renderer/service_worker/service_worker_context_client.h" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 // static |
| 16 void BackgroundSyncClientImpl::Create( |
| 17 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, |
| 18 mojo::InterfaceRequest<BackgroundSyncServiceClient> request) { |
| 19 new BackgroundSyncClientImpl(main_thread_task_runner, request.Pass()); |
| 20 } |
| 21 |
| 22 BackgroundSyncClientImpl::~BackgroundSyncClientImpl() { |
| 23 } |
| 24 |
| 25 BackgroundSyncClientImpl::BackgroundSyncClientImpl( |
| 26 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, |
| 27 mojo::InterfaceRequest<BackgroundSyncServiceClient> request) |
| 28 : binding_(this, request.Pass()), |
| 29 main_thread_task_runner_(main_thread_task_runner), |
| 30 weak_ptr_factory_(this) { |
| 31 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 32 } |
| 33 |
| 34 void BackgroundSyncClientImpl::Sync(content::SyncRegistrationPtr registration, |
| 35 int thread_id, |
| 36 const SyncCallback& callback) { |
| 37 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 38 |
| 39 WorkerTaskRunner* worker_task_runner = WorkerTaskRunner::Instance(); |
| 40 if (!worker_task_runner || |
| 41 !worker_task_runner->GetTaskRunnerFor(thread_id)->PostTask( |
| 42 FROM_HERE, |
| 43 base::Bind(&BackgroundSyncClientImpl::DispatchSyncOnWorkerThread, |
| 44 main_thread_task_runner_, |
| 45 base::Passed(registration.Pass()), callback))) { |
| 46 callback.Run(SERVICE_WORKER_EVENT_STATUS_ABORTED); |
| 47 } |
| 48 } |
| 49 |
| 50 // static |
| 51 void BackgroundSyncClientImpl::DispatchSyncOnWorkerThread( |
| 52 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, |
| 53 content::SyncRegistrationPtr registration, |
| 54 const SyncCallback& callback) { |
| 55 ServiceWorkerContextClient* client = |
| 56 ServiceWorkerContextClient::ThreadSpecificInstance(); |
| 57 if (!client) { |
| 58 OnSyncCompleteOnWorkerThread(main_thread_task_runner, callback, |
| 59 SERVICE_WORKER_EVENT_STATUS_ABORTED); |
| 60 return; |
| 61 } |
| 62 client->DispatchSyncEvent( |
| 63 base::Bind(&BackgroundSyncClientImpl::OnSyncCompleteOnWorkerThread, |
| 64 main_thread_task_runner, callback)); |
| 65 } |
| 66 |
| 67 // static |
| 68 void BackgroundSyncClientImpl::OnSyncCompleteOnWorkerThread( |
| 69 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, |
| 70 const SyncCallback& callback, |
| 71 ServiceWorkerEventStatus status) { |
| 72 // Run callback on main thread |
| 73 main_thread_task_runner->PostTask( |
| 74 FROM_HERE, |
| 75 base::Bind(&BackgroundSyncClientImpl::OnSyncCompleteOnMainThread, |
| 76 callback, status)); |
| 77 } |
| 78 |
| 79 // static |
| 80 void BackgroundSyncClientImpl::OnSyncCompleteOnMainThread( |
| 81 const SyncCallback& callback, |
| 82 ServiceWorkerEventStatus status) { |
| 83 callback.Run(status); |
| 84 } |
| 85 |
| 86 } // namespace content |
OLD | NEW |