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 } |
| 32 |
| 33 void BackgroundSyncClientImpl::Sync(content::SyncRegistrationPtr registration, |
| 34 int thread_id, |
| 35 const SyncCallback& callback) { |
| 36 WorkerTaskRunner* worker_task_runner = WorkerTaskRunner::Instance(); |
| 37 if (!worker_task_runner) { |
| 38 callback.Run(SERVICE_WORKER_EVENT_STATUS_ABORT); |
| 39 return; |
| 40 } |
| 41 // TODO(iclelland): If the worker is dead, GetTaskRunnerFor(thread_id) will |
| 42 // still return a task runner. Detect that state and call callback with an |
| 43 // abort status. |
| 44 worker_task_runner->GetTaskRunnerFor(thread_id)->PostTask( |
| 45 FROM_HERE, base::Bind(&BackgroundSyncClientImpl::DispatchSyncForWorker, |
| 46 base::Unretained(this), |
| 47 base::Passed(registration.Pass()), callback)); |
| 48 } |
| 49 |
| 50 void BackgroundSyncClientImpl::DispatchSyncForWorker( |
| 51 content::SyncRegistrationPtr options, |
| 52 const SyncCallback& callback) { |
| 53 ServiceWorkerContextClient* client = |
| 54 ServiceWorkerContextClient::ThreadSpecificInstance(); |
| 55 if (!client) { |
| 56 callback.Run(SERVICE_WORKER_EVENT_STATUS_ABORT); |
| 57 return; |
| 58 } |
| 59 client->DispatchSyncEvent( |
| 60 base::Bind(&BackgroundSyncClientImpl::OnSyncComplete, |
| 61 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 62 } |
| 63 |
| 64 void BackgroundSyncClientImpl::OnSyncComplete(const SyncCallback& callback, |
| 65 ServiceWorkerEventStatus status) { |
| 66 if (!main_thread_task_runner_->RunsTasksOnCurrentThread()) { |
| 67 // Re-run on main thread |
| 68 main_thread_task_runner_->PostTask( |
| 69 FROM_HERE, base::Bind(&BackgroundSyncClientImpl::OnSyncComplete, |
| 70 base::Unretained(this), callback, status)); |
| 71 return; |
| 72 } |
| 73 callback.Run(status); |
| 74 } |
| 75 |
| 76 } // namespace content |
OLD | NEW |