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.
5 #include "content/child/background_sync/background_sync_provider_thread_proxy.h"
8 #include "base/bind_helpers.h"
9 #include "base/lazy_instance.h"
10 #include "base/location.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/threading/thread_local.h"
14 #include "content/child/background_sync/background_sync_provider.h"
16 using base::LazyInstance
;
17 using base::ThreadLocalPointer
;
23 // CallbackThreadAdapter<S,T> is a wrapper for WebCallbacks<S,T> which
24 // switches to a specific thread before calling the wrapped callback's
25 // onSuccess or onError methods.
27 // Takes ownership of the WebCallbacks object which it wraps.
28 template <typename S
, typename T
>
29 class CallbackThreadAdapter
: public blink::WebCallbacks
<S
, T
> {
31 CallbackThreadAdapter(scoped_ptr
<blink::WebCallbacks
<S
, T
>> callbacks
,
33 : worker_thread_id_(worker_thread_id
) {
34 callbacks_
.reset(callbacks
.release());
37 virtual void onSuccess(S results
) {
38 // If the worker thread has been destroyed, then this task will be
39 // silently discarded.
40 WorkerTaskRunner::Instance()->PostTask(
42 base::Bind(&blink::WebCallbacks
<S
, T
>::onSuccess
,
43 base::Owned(callbacks_
.release()), results
));
46 virtual void onError(T error
) {
47 // If the worker thread has been destroyed, then this task will be
48 // silently discarded.
49 WorkerTaskRunner::Instance()->PostTask(
51 base::Bind(&blink::WebCallbacks
<S
, T
>::onError
,
52 base::Owned(callbacks_
.release()), error
));
56 scoped_ptr
<blink::WebCallbacks
<S
, T
>> callbacks_
;
57 int worker_thread_id_
;
60 LazyInstance
<ThreadLocalPointer
<BackgroundSyncProviderThreadProxy
>>::Leaky
61 g_sync_provider_tls
= LAZY_INSTANCE_INITIALIZER
;
63 } // anonymous namespace
66 BackgroundSyncProviderThreadProxy
*
67 BackgroundSyncProviderThreadProxy::GetThreadInstance(
68 base::SingleThreadTaskRunner
* main_thread_task_runner
,
69 BackgroundSyncProvider
* sync_provider
) {
70 if (g_sync_provider_tls
.Pointer()->Get())
71 return g_sync_provider_tls
.Pointer()->Get();
73 if (!WorkerTaskRunner::Instance()->CurrentWorkerId()) {
74 // This could happen if GetThreadInstance is called very late (say by a
75 // garbage collected SyncRegistration).
79 BackgroundSyncProviderThreadProxy
* instance
=
80 new BackgroundSyncProviderThreadProxy(main_thread_task_runner
,
82 WorkerTaskRunner::Instance()->AddStopObserver(instance
);
86 void BackgroundSyncProviderThreadProxy::registerBackgroundSync(
87 const blink::WebSyncRegistration
* options
,
88 blink::WebServiceWorkerRegistration
* service_worker_registration
,
89 blink::WebSyncRegistrationCallbacks
* callbacks
) {
91 DCHECK(service_worker_registration
);
93 main_thread_task_runner_
->PostTask(
95 base::Bind(&BackgroundSyncProvider::registerBackgroundSync
,
96 base::Unretained(sync_provider_
), options
,
97 service_worker_registration
,
98 new CallbackThreadAdapter
<blink::WebSyncRegistration
*,
99 blink::WebSyncError
*>(
100 make_scoped_ptr(callbacks
),
101 WorkerTaskRunner::Instance()->CurrentWorkerId())));
104 void BackgroundSyncProviderThreadProxy::unregisterBackgroundSync(
105 blink::WebSyncRegistration::Periodicity periodicity
,
107 const blink::WebString
& tag
,
108 blink::WebServiceWorkerRegistration
* service_worker_registration
,
109 blink::WebSyncUnregistrationCallbacks
* callbacks
) {
110 DCHECK(service_worker_registration
);
112 main_thread_task_runner_
->PostTask(
114 base::Bind(&BackgroundSyncProvider::unregisterBackgroundSync
,
115 base::Unretained(sync_provider_
), periodicity
, id
,
116 // We cast WebString to string16 before crossing threads
117 // for thread-safety.
118 static_cast<base::string16
>(tag
), service_worker_registration
,
119 new CallbackThreadAdapter
<bool*, blink::WebSyncError
*>(
120 make_scoped_ptr(callbacks
),
121 WorkerTaskRunner::Instance()->CurrentWorkerId())));
124 void BackgroundSyncProviderThreadProxy::getRegistration(
125 blink::WebSyncRegistration::Periodicity periodicity
,
126 const blink::WebString
& tag
,
127 blink::WebServiceWorkerRegistration
* service_worker_registration
,
128 blink::WebSyncRegistrationCallbacks
* callbacks
) {
129 DCHECK(service_worker_registration
);
131 main_thread_task_runner_
->PostTask(
133 base::Bind(&BackgroundSyncProvider::getRegistration
,
134 base::Unretained(sync_provider_
), periodicity
,
135 // We cast WebString to string16 before crossing threads
136 // for thread-safety.
137 static_cast<base::string16
>(tag
), service_worker_registration
,
138 new CallbackThreadAdapter
<blink::WebSyncRegistration
*,
139 blink::WebSyncError
*>(
140 make_scoped_ptr(callbacks
),
141 WorkerTaskRunner::Instance()->CurrentWorkerId())));
144 void BackgroundSyncProviderThreadProxy::getRegistrations(
145 blink::WebSyncRegistration::Periodicity periodicity
,
146 blink::WebServiceWorkerRegistration
* service_worker_registration
,
147 blink::WebSyncGetRegistrationsCallbacks
* callbacks
) {
148 DCHECK(service_worker_registration
);
150 main_thread_task_runner_
->PostTask(
152 base::Bind(&BackgroundSyncProvider::getRegistrations
,
153 base::Unretained(sync_provider_
), periodicity
,
154 service_worker_registration
,
155 new CallbackThreadAdapter
<
156 blink::WebVector
<blink::WebSyncRegistration
*>*,
157 blink::WebSyncError
*>(
158 make_scoped_ptr(callbacks
),
159 WorkerTaskRunner::Instance()->CurrentWorkerId())));
162 void BackgroundSyncProviderThreadProxy::getPermissionStatus(
163 blink::WebSyncRegistration::Periodicity periodicity
,
164 blink::WebServiceWorkerRegistration
* service_worker_registration
,
165 blink::WebSyncGetPermissionStatusCallbacks
* callbacks
) {
166 DCHECK(service_worker_registration
);
168 main_thread_task_runner_
->PostTask(
170 base::Bind(&BackgroundSyncProvider::getPermissionStatus
,
171 base::Unretained(sync_provider_
), periodicity
,
172 service_worker_registration
,
173 new CallbackThreadAdapter
<blink::WebSyncPermissionStatus
*,
174 blink::WebSyncError
*>(
175 make_scoped_ptr(callbacks
),
176 WorkerTaskRunner::Instance()->CurrentWorkerId())));
179 void BackgroundSyncProviderThreadProxy::OnWorkerRunLoopStopped() {
183 BackgroundSyncProviderThreadProxy::BackgroundSyncProviderThreadProxy(
184 base::SingleThreadTaskRunner
* main_thread_task_runner
,
185 BackgroundSyncProvider
* sync_provider
)
186 : main_thread_task_runner_(main_thread_task_runner
),
187 sync_provider_(sync_provider
) {
188 g_sync_provider_tls
.Pointer()->Set(this);
191 BackgroundSyncProviderThreadProxy::~BackgroundSyncProviderThreadProxy() {
192 g_sync_provider_tls
.Pointer()->Set(nullptr);
195 } // namespace content