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 (!WorkerTaskRunner::Instance()->CurrentWorkerId()) {
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 WorkerTaskRunner::Instance()->AddStopObserver(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(
111 base::Bind(&BackgroundSyncProvider::registerBackgroundSync
,
112 base::Unretained(sync_provider_
), options
,
113 service_worker_registration
, requested_from_service_worker
,
114 new CallbackThreadAdapter
<blink::WebSyncRegistrationCallbacks
>(
115 make_scoped_ptr(callbacks
),
116 WorkerTaskRunner::Instance()->CurrentWorkerId())));
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
),
137 WorkerTaskRunner::Instance()->CurrentWorkerId())));
140 void BackgroundSyncProviderThreadProxy::getRegistration(
141 blink::WebSyncRegistration::Periodicity periodicity
,
142 const blink::WebString
& tag
,
143 blink::WebServiceWorkerRegistration
* service_worker_registration
,
144 blink::WebSyncRegistrationCallbacks
* callbacks
) {
145 DCHECK(service_worker_registration
);
147 main_thread_task_runner_
->PostTask(
149 base::Bind(&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
),
156 WorkerTaskRunner::Instance()->CurrentWorkerId())));
159 void BackgroundSyncProviderThreadProxy::getRegistrations(
160 blink::WebSyncRegistration::Periodicity periodicity
,
161 blink::WebServiceWorkerRegistration
* service_worker_registration
,
162 blink::WebSyncGetRegistrationsCallbacks
* callbacks
) {
163 DCHECK(service_worker_registration
);
165 main_thread_task_runner_
->PostTask(
168 &BackgroundSyncProvider::getRegistrations
,
169 base::Unretained(sync_provider_
), periodicity
,
170 service_worker_registration
,
171 new CallbackThreadAdapter
<blink::WebSyncGetRegistrationsCallbacks
>(
172 make_scoped_ptr(callbacks
),
173 WorkerTaskRunner::Instance()->CurrentWorkerId())));
176 void BackgroundSyncProviderThreadProxy::getPermissionStatus(
177 blink::WebSyncRegistration::Periodicity periodicity
,
178 blink::WebServiceWorkerRegistration
* service_worker_registration
,
179 blink::WebSyncGetPermissionStatusCallbacks
* callbacks
) {
180 DCHECK(service_worker_registration
);
182 main_thread_task_runner_
->PostTask(
185 &BackgroundSyncProvider::getPermissionStatus
,
186 base::Unretained(sync_provider_
), periodicity
,
187 service_worker_registration
,
188 new CallbackThreadAdapter
<blink::WebSyncGetPermissionStatusCallbacks
>(
189 make_scoped_ptr(callbacks
),
190 WorkerTaskRunner::Instance()->CurrentWorkerId())));
193 void BackgroundSyncProviderThreadProxy::OnWorkerRunLoopStopped() {
197 BackgroundSyncProviderThreadProxy::BackgroundSyncProviderThreadProxy(
198 base::SingleThreadTaskRunner
* main_thread_task_runner
,
199 BackgroundSyncProvider
* sync_provider
)
200 : main_thread_task_runner_(main_thread_task_runner
),
201 sync_provider_(sync_provider
) {
202 g_sync_provider_tls
.Pointer()->Set(this);
205 BackgroundSyncProviderThreadProxy::~BackgroundSyncProviderThreadProxy() {
206 g_sync_provider_tls
.Pointer()->Set(nullptr);
209 } // namespace content