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 blink::WebSyncRegistrationCallbacks
* callbacks
) {
106 DCHECK(service_worker_registration
);
108 main_thread_task_runner_
->PostTask(
110 base::Bind(&BackgroundSyncProvider::registerBackgroundSync
,
111 base::Unretained(sync_provider_
), options
,
112 service_worker_registration
,
113 new CallbackThreadAdapter
<blink::WebSyncRegistrationCallbacks
>(
114 make_scoped_ptr(callbacks
),
115 WorkerTaskRunner::Instance()->CurrentWorkerId())));
118 void BackgroundSyncProviderThreadProxy::unregisterBackgroundSync(
119 blink::WebSyncRegistration::Periodicity periodicity
,
121 const blink::WebString
& tag
,
122 blink::WebServiceWorkerRegistration
* service_worker_registration
,
123 blink::WebSyncUnregistrationCallbacks
* callbacks
) {
124 DCHECK(service_worker_registration
);
126 main_thread_task_runner_
->PostTask(
129 &BackgroundSyncProvider::unregisterBackgroundSync
,
130 base::Unretained(sync_provider_
), periodicity
, id
,
131 // We cast WebString to string16 before crossing threads
132 // for thread-safety.
133 static_cast<base::string16
>(tag
), service_worker_registration
,
134 new CallbackThreadAdapter
<blink::WebSyncUnregistrationCallbacks
>(
135 make_scoped_ptr(callbacks
),
136 WorkerTaskRunner::Instance()->CurrentWorkerId())));
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(
148 base::Bind(&BackgroundSyncProvider::getRegistration
,
149 base::Unretained(sync_provider_
), periodicity
,
150 // We cast WebString to string16 before crossing threads
151 // for thread-safety.
152 static_cast<base::string16
>(tag
), service_worker_registration
,
153 new CallbackThreadAdapter
<blink::WebSyncRegistrationCallbacks
>(
154 make_scoped_ptr(callbacks
),
155 WorkerTaskRunner::Instance()->CurrentWorkerId())));
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
),
172 WorkerTaskRunner::Instance()->CurrentWorkerId())));
175 void BackgroundSyncProviderThreadProxy::getPermissionStatus(
176 blink::WebSyncRegistration::Periodicity periodicity
,
177 blink::WebServiceWorkerRegistration
* service_worker_registration
,
178 blink::WebSyncGetPermissionStatusCallbacks
* callbacks
) {
179 DCHECK(service_worker_registration
);
181 main_thread_task_runner_
->PostTask(
184 &BackgroundSyncProvider::getPermissionStatus
,
185 base::Unretained(sync_provider_
), periodicity
,
186 service_worker_registration
,
187 new CallbackThreadAdapter
<blink::WebSyncGetPermissionStatusCallbacks
>(
188 make_scoped_ptr(callbacks
),
189 WorkerTaskRunner::Instance()->CurrentWorkerId())));
192 void BackgroundSyncProviderThreadProxy::OnWorkerRunLoopStopped() {
196 BackgroundSyncProviderThreadProxy::BackgroundSyncProviderThreadProxy(
197 base::SingleThreadTaskRunner
* main_thread_task_runner
,
198 BackgroundSyncProvider
* sync_provider
)
199 : main_thread_task_runner_(main_thread_task_runner
),
200 sync_provider_(sync_provider
) {
201 g_sync_provider_tls
.Pointer()->Set(this);
204 BackgroundSyncProviderThreadProxy::~BackgroundSyncProviderThreadProxy() {
205 g_sync_provider_tls
.Pointer()->Set(nullptr);
208 } // namespace content