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 WorkerTaskRunner* worker_task_runner = WorkerTaskRunner::Instance(); |
| 38 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 39 if (!worker_task_runner) { |
| 40 callback.Run(SERVICE_WORKER_EVENT_STATUS_ABORT); |
| 41 return; |
| 42 } |
| 43 // TODO(iclelland): If the worker is dead, GetTaskRunnerFor(thread_id) will |
| 44 // still return a task runner. Detect that state and call callback with an |
| 45 // abort status. |
| 46 worker_task_runner->GetTaskRunnerFor(thread_id)->PostTask( |
| 47 FROM_HERE, base::Bind(&BackgroundSyncClientImpl::DispatchSyncForWorker, |
| 48 main_thread_task_runner_, |
| 49 base::Passed(registration.Pass()), callback)); |
| 50 } |
| 51 |
| 52 // static |
| 53 void BackgroundSyncClientImpl::DispatchSyncForWorker( |
| 54 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, |
| 55 |
| 56 content::SyncRegistrationPtr registration, |
| 57 const SyncCallback& callback) { |
| 58 ServiceWorkerContextClient* client = |
| 59 ServiceWorkerContextClient::ThreadSpecificInstance(); |
| 60 if (!client) { |
| 61 OnSyncComplete(main_thread_task_runner, callback, |
| 62 SERVICE_WORKER_EVENT_STATUS_ABORT); |
| 63 return; |
| 64 } |
| 65 client->DispatchSyncEvent( |
| 66 base::Bind(&BackgroundSyncClientImpl::OnSyncComplete, |
| 67 main_thread_task_runner, callback)); |
| 68 } |
| 69 |
| 70 // static |
| 71 void BackgroundSyncClientImpl::OnSyncComplete( |
| 72 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, |
| 73 const SyncCallback& callback, |
| 74 ServiceWorkerEventStatus status) { |
| 75 // Run callback on main thread |
| 76 main_thread_task_runner->PostTask( |
| 77 FROM_HERE, |
| 78 base::Bind(&BackgroundSyncClientImpl::OnSyncCompleteOnMainThread, |
| 79 callback, status)); |
| 80 } |
| 81 |
| 82 // static |
| 83 void BackgroundSyncClientImpl::OnSyncCompleteOnMainThread( |
| 84 const SyncCallback& callback, |
| 85 ServiceWorkerEventStatus status) { |
| 86 callback.Run(status); |
| 87 } |
| 88 |
| 89 } // namespace content |
OLD | NEW |