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 callback.Run(SERVICE_WORKER_EVENT_STATUS_ABORTED); | |
42 return; | |
43 } | |
44 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.
| |
45 FROM_HERE, | |
46 base::Bind(&BackgroundSyncClientImpl::DispatchSyncOnWorkerThread, | |
47 main_thread_task_runner_, | |
48 base::Passed(registration.Pass()), callback))) { | |
49 callback.Run(SERVICE_WORKER_EVENT_STATUS_ABORTED); | |
50 } | |
51 } | |
52 | |
53 // static | |
54 void BackgroundSyncClientImpl::DispatchSyncOnWorkerThread( | |
55 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread_task_runner, | |
56 content::SyncRegistrationPtr registration, | |
57 const SyncCallback& callback) { | |
58 ServiceWorkerContextClient* client = | |
59 ServiceWorkerContextClient::ThreadSpecificInstance(); | |
60 if (!client) { | |
61 OnSyncCompleteOnWorkerThread(main_thread_task_runner, callback, | |
62 SERVICE_WORKER_EVENT_STATUS_ABORTED); | |
63 return; | |
64 } | |
65 client->DispatchSyncEvent( | |
66 base::Bind(&BackgroundSyncClientImpl::OnSyncCompleteOnWorkerThread, | |
67 main_thread_task_runner, callback)); | |
68 } | |
69 | |
70 // static | |
71 void BackgroundSyncClientImpl::OnSyncCompleteOnWorkerThread( | |
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 |