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
;
24 struct WebCallbacksMatcher
;
26 template <typename Stype
, typename Ttype
>
27 struct WebCallbacksMatcher
<blink::WebCallbacks
<Stype
, Ttype
>> {
30 using WebCallbacks
= typename
blink::WebCallbacks
<S
, T
>;
33 // CallbackThreadAdapter<WebCallbacks<S, T>> is a wrapper for WebCallbacks<S, T>
34 // which switches to a specific thread before calling the wrapped callback's
35 // onSuccess or onError methods.
37 // Takes ownership of the WebCallbacks object which it wraps.
39 class CallbackThreadAdapter
: public WebCallbacksMatcher
<X
>::WebCallbacks
{
40 using S
= typename WebCallbacksMatcher
<X
>::S
;
41 using T
= typename WebCallbacksMatcher
<X
>::T
;
42 using OnSuccessType
= void (blink::WebCallbacks
<S
, T
>::*)(S
);
43 using OnErrorType
= void (blink::WebCallbacks
<S
, T
>::*)(T
);
46 CallbackThreadAdapter(scoped_ptr
<blink::WebCallbacks
<S
, T
>> callbacks
,
48 : worker_thread_id_(worker_thread_id
) {
49 callbacks_
.reset(callbacks
.release());
52 virtual void onSuccess(S results
) {
53 OnSuccessType on_success
= &blink::WebCallbacks
<S
, T
>::onSuccess
;
54 // If the worker thread has been destroyed, then this task will be
55 // silently discarded.
56 WorkerTaskRunner::Instance()->PostTask(
58 base::Bind(on_success
, base::Owned(callbacks_
.release()), results
));
61 virtual void onError(T error
) {
62 OnErrorType on_error
= &blink::WebCallbacks
<S
, T
>::onError
;
63 // If the worker thread has been destroyed, then this task will be
64 // silently discarded.
65 WorkerTaskRunner::Instance()->PostTask(
67 base::Bind(on_error
, base::Owned(callbacks_
.release()), error
));
71 scoped_ptr
<blink::WebCallbacks
<S
, T
>> callbacks_
;
72 int worker_thread_id_
;
75 LazyInstance
<ThreadLocalPointer
<BackgroundSyncProviderThreadProxy
>>::Leaky
76 g_sync_provider_tls
= LAZY_INSTANCE_INITIALIZER
;
78 } // anonymous namespace
81 BackgroundSyncProviderThreadProxy
*
82 BackgroundSyncProviderThreadProxy::GetThreadInstance(
83 base::SingleThreadTaskRunner
* main_thread_task_runner
,
84 BackgroundSyncProvider
* sync_provider
) {
85 if (g_sync_provider_tls
.Pointer()->Get())
86 return g_sync_provider_tls
.Pointer()->Get();
88 if (!WorkerThread::GetCurrentId()) {
89 // This could happen if GetThreadInstance is called very late (say by a
90 // garbage collected SyncRegistration).
94 BackgroundSyncProviderThreadProxy
* instance
=
95 new BackgroundSyncProviderThreadProxy(main_thread_task_runner
,
97 WorkerThread::AddObserver(instance
);
101 void BackgroundSyncProviderThreadProxy::registerBackgroundSync(
102 const blink::WebSyncRegistration
* options
,
103 blink::WebServiceWorkerRegistration
* service_worker_registration
,
104 bool requested_from_service_worker
,
105 blink::WebSyncRegistrationCallbacks
* callbacks
) {
107 DCHECK(service_worker_registration
);
109 main_thread_task_runner_
->PostTask(
112 &BackgroundSyncProvider::registerBackgroundSync
,
113 base::Unretained(sync_provider_
), options
,
114 service_worker_registration
, requested_from_service_worker
,
115 new CallbackThreadAdapter
<blink::WebSyncRegistrationCallbacks
>(
116 make_scoped_ptr(callbacks
), WorkerThread::GetCurrentId())));
119 void BackgroundSyncProviderThreadProxy::unregisterBackgroundSync(
120 blink::WebSyncRegistration::Periodicity periodicity
,
122 const blink::WebString
& tag
,
123 blink::WebServiceWorkerRegistration
* service_worker_registration
,
124 blink::WebSyncUnregistrationCallbacks
* callbacks
) {
125 DCHECK(service_worker_registration
);
127 main_thread_task_runner_
->PostTask(
130 &BackgroundSyncProvider::unregisterBackgroundSync
,
131 base::Unretained(sync_provider_
), periodicity
, id
,
132 // We cast WebString to string16 before crossing threads
133 // for thread-safety.
134 static_cast<base::string16
>(tag
), service_worker_registration
,
135 new CallbackThreadAdapter
<blink::WebSyncUnregistrationCallbacks
>(
136 make_scoped_ptr(callbacks
), WorkerThread::GetCurrentId())));
139 void BackgroundSyncProviderThreadProxy::getRegistration(
140 blink::WebSyncRegistration::Periodicity periodicity
,
141 const blink::WebString
& tag
,
142 blink::WebServiceWorkerRegistration
* service_worker_registration
,
143 blink::WebSyncRegistrationCallbacks
* callbacks
) {
144 DCHECK(service_worker_registration
);
146 main_thread_task_runner_
->PostTask(
149 &BackgroundSyncProvider::getRegistration
,
150 base::Unretained(sync_provider_
), periodicity
,
151 // We cast WebString to string16 before crossing threads
152 // for thread-safety.
153 static_cast<base::string16
>(tag
), service_worker_registration
,
154 new CallbackThreadAdapter
<blink::WebSyncRegistrationCallbacks
>(
155 make_scoped_ptr(callbacks
), WorkerThread::GetCurrentId())));
158 void BackgroundSyncProviderThreadProxy::getRegistrations(
159 blink::WebSyncRegistration::Periodicity periodicity
,
160 blink::WebServiceWorkerRegistration
* service_worker_registration
,
161 blink::WebSyncGetRegistrationsCallbacks
* callbacks
) {
162 DCHECK(service_worker_registration
);
164 main_thread_task_runner_
->PostTask(
167 &BackgroundSyncProvider::getRegistrations
,
168 base::Unretained(sync_provider_
), periodicity
,
169 service_worker_registration
,
170 new CallbackThreadAdapter
<blink::WebSyncGetRegistrationsCallbacks
>(
171 make_scoped_ptr(callbacks
), WorkerThread::GetCurrentId())));
174 void BackgroundSyncProviderThreadProxy::getPermissionStatus(
175 blink::WebSyncRegistration::Periodicity periodicity
,
176 blink::WebServiceWorkerRegistration
* service_worker_registration
,
177 blink::WebSyncGetPermissionStatusCallbacks
* callbacks
) {
178 DCHECK(service_worker_registration
);
180 main_thread_task_runner_
->PostTask(
183 &BackgroundSyncProvider::getPermissionStatus
,
184 base::Unretained(sync_provider_
), periodicity
,
185 service_worker_registration
,
186 new CallbackThreadAdapter
<blink::WebSyncGetPermissionStatusCallbacks
>(
187 make_scoped_ptr(callbacks
), WorkerThread::GetCurrentId())));
190 void BackgroundSyncProviderThreadProxy::WillStopCurrentWorkerThread() {
194 BackgroundSyncProviderThreadProxy::BackgroundSyncProviderThreadProxy(
195 base::SingleThreadTaskRunner
* main_thread_task_runner
,
196 BackgroundSyncProvider
* sync_provider
)
197 : main_thread_task_runner_(main_thread_task_runner
),
198 sync_provider_(sync_provider
) {
199 g_sync_provider_tls
.Pointer()->Set(this);
202 BackgroundSyncProviderThreadProxy::~BackgroundSyncProviderThreadProxy() {
203 g_sync_provider_tls
.Pointer()->Set(nullptr);
206 } // namespace content