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/embedded_worker_context_client.h" |
| 12 #include "content/renderer/service_worker/service_worker_script_context.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 // static |
| 17 void BackgroundSyncClientImpl::Create( |
| 18 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, |
| 19 mojo::InterfaceRequest<BackgroundSyncServiceClient> request) { |
| 20 new BackgroundSyncClientImpl(main_thread_task_runner, request.Pass()); |
| 21 } |
| 22 |
| 23 BackgroundSyncClientImpl::~BackgroundSyncClientImpl() { |
| 24 } |
| 25 |
| 26 BackgroundSyncClientImpl::BackgroundSyncClientImpl( |
| 27 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, |
| 28 mojo::InterfaceRequest<BackgroundSyncServiceClient> request) |
| 29 : binding_(this, request.Pass()), |
| 30 main_thread_task_runner_(main_thread_task_runner), |
| 31 weak_ptr_factory_(this) { |
| 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 if (!worker_task_runner) { |
| 39 callback.Run(BACKGROUND_SYNC_EVENT_STATUS_ABORT); |
| 40 return; |
| 41 } |
| 42 // TODO(iclelland): If the worker is dead, GetTaskRunnerFor(thread_id) will |
| 43 // still return a task runner. Detect that state and call callback with an |
| 44 // abort status. |
| 45 worker_task_runner->GetTaskRunnerFor(thread_id)->PostTask( |
| 46 FROM_HERE, base::Bind(&BackgroundSyncClientImpl::DispatchSyncForWorker, |
| 47 base::Unretained(this), |
| 48 base::Passed(registration.Pass()), callback)); |
| 49 } |
| 50 |
| 51 void BackgroundSyncClientImpl::DispatchSyncForWorker( |
| 52 content::SyncRegistrationPtr options, |
| 53 const SyncCallback& callback) { |
| 54 EmbeddedWorkerContextClient* client = |
| 55 EmbeddedWorkerContextClient::ThreadSpecificInstance(); |
| 56 if (!client) { |
| 57 callback.Run(BACKGROUND_SYNC_EVENT_STATUS_ABORT); |
| 58 return; |
| 59 } |
| 60 client->script_context()->DispatchSyncEvent( |
| 61 base::Bind(&BackgroundSyncClientImpl::OnSyncComplete, |
| 62 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 63 } |
| 64 |
| 65 void BackgroundSyncClientImpl::OnSyncComplete( |
| 66 const SyncCallback& callback, |
| 67 BackgroundSyncEventStatus status) { |
| 68 if (!main_thread_task_runner_->RunsTasksOnCurrentThread()) { |
| 69 // Re-run on main thread |
| 70 main_thread_task_runner_->PostTask( |
| 71 FROM_HERE, base::Bind(&BackgroundSyncClientImpl::OnSyncComplete, |
| 72 base::Unretained(this), callback, status)); |
| 73 return; |
| 74 } |
| 75 callback.Run(status); |
| 76 } |
| 77 |
| 78 } // namespace content |
OLD | NEW |