Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / content / child / background_sync / background_sync_provider_thread_proxy.cc
blobb3013c484b922429d42a76a193e572f613a34e68
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"
7 #include "base/bind.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;
19 namespace content {
21 namespace {
23 template <typename T>
24 struct WebCallbacksMatcher;
26 template <typename Stype, typename Ttype>
27 struct WebCallbacksMatcher<blink::WebCallbacks<Stype, Ttype>> {
28 using S = Stype;
29 using T = 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.
38 template <typename X>
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);
45 public:
46 CallbackThreadAdapter(scoped_ptr<blink::WebCallbacks<S, T>> callbacks,
47 int worker_thread_id)
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(
57 worker_thread_id_,
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(
66 worker_thread_id_,
67 base::Bind(on_error, base::Owned(callbacks_.release()), error));
70 private:
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 void DuplicateRegistrationHandleCallbackOnSWThread(
79 const BackgroundSyncService::DuplicateRegistrationHandleCallback& callback,
80 BackgroundSyncError error,
81 SyncRegistrationPtr registration) {
82 callback.Run(error, registration.Pass());
85 void DuplicateRegistrationHandleCallbackOnMainThread(
86 int worker_thread_id,
87 const BackgroundSyncService::DuplicateRegistrationHandleCallback& callback,
88 BackgroundSyncError error,
89 SyncRegistrationPtr registration) {
90 WorkerTaskRunner::Instance()->PostTask(
91 worker_thread_id,
92 base::Bind(&DuplicateRegistrationHandleCallbackOnSWThread, callback,
93 error, base::Passed(registration.Pass())));
96 } // anonymous namespace
98 // static
99 BackgroundSyncProviderThreadProxy*
100 BackgroundSyncProviderThreadProxy::GetThreadInstance(
101 base::SingleThreadTaskRunner* main_thread_task_runner,
102 BackgroundSyncProvider* sync_provider) {
103 if (g_sync_provider_tls.Pointer()->Get())
104 return g_sync_provider_tls.Pointer()->Get();
106 if (!WorkerThread::GetCurrentId()) {
107 // This could happen if GetThreadInstance is called very late (say by a
108 // garbage collected SyncRegistration).
109 return nullptr;
112 BackgroundSyncProviderThreadProxy* instance =
113 new BackgroundSyncProviderThreadProxy(main_thread_task_runner,
114 sync_provider);
115 WorkerThread::AddObserver(instance);
116 return instance;
119 void BackgroundSyncProviderThreadProxy::registerBackgroundSync(
120 const blink::WebSyncRegistration* options,
121 blink::WebServiceWorkerRegistration* service_worker_registration,
122 bool requested_from_service_worker,
123 blink::WebSyncRegistrationCallbacks* callbacks) {
124 DCHECK(options);
125 DCHECK(service_worker_registration);
126 DCHECK(callbacks);
127 main_thread_task_runner_->PostTask(
128 FROM_HERE,
129 base::Bind(
130 &BackgroundSyncProvider::registerBackgroundSync,
131 base::Unretained(sync_provider_), options,
132 service_worker_registration, requested_from_service_worker,
133 new CallbackThreadAdapter<blink::WebSyncRegistrationCallbacks>(
134 make_scoped_ptr(callbacks), WorkerThread::GetCurrentId())));
137 void BackgroundSyncProviderThreadProxy::unregisterBackgroundSync(
138 blink::WebSyncRegistration::Periodicity periodicity,
139 int64_t id,
140 const blink::WebString& tag,
141 blink::WebServiceWorkerRegistration* service_worker_registration,
142 blink::WebSyncUnregistrationCallbacks* callbacks) {
143 DCHECK(service_worker_registration);
144 DCHECK(callbacks);
145 main_thread_task_runner_->PostTask(
146 FROM_HERE,
147 base::Bind(
148 &BackgroundSyncProvider::unregisterBackgroundSync,
149 base::Unretained(sync_provider_), periodicity, id,
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::WebSyncUnregistrationCallbacks>(
154 make_scoped_ptr(callbacks), WorkerThread::GetCurrentId())));
157 void BackgroundSyncProviderThreadProxy::getRegistration(
158 blink::WebSyncRegistration::Periodicity periodicity,
159 const blink::WebString& tag,
160 blink::WebServiceWorkerRegistration* service_worker_registration,
161 blink::WebSyncRegistrationCallbacks* callbacks) {
162 DCHECK(service_worker_registration);
163 DCHECK(callbacks);
164 main_thread_task_runner_->PostTask(
165 FROM_HERE,
166 base::Bind(
167 &BackgroundSyncProvider::getRegistration,
168 base::Unretained(sync_provider_), periodicity,
169 // We cast WebString to string16 before crossing threads
170 // for thread-safety.
171 static_cast<base::string16>(tag), service_worker_registration,
172 new CallbackThreadAdapter<blink::WebSyncRegistrationCallbacks>(
173 make_scoped_ptr(callbacks), WorkerThread::GetCurrentId())));
176 void BackgroundSyncProviderThreadProxy::getRegistrations(
177 blink::WebSyncRegistration::Periodicity periodicity,
178 blink::WebServiceWorkerRegistration* service_worker_registration,
179 blink::WebSyncGetRegistrationsCallbacks* callbacks) {
180 DCHECK(service_worker_registration);
181 DCHECK(callbacks);
182 main_thread_task_runner_->PostTask(
183 FROM_HERE,
184 base::Bind(
185 &BackgroundSyncProvider::getRegistrations,
186 base::Unretained(sync_provider_), periodicity,
187 service_worker_registration,
188 new CallbackThreadAdapter<blink::WebSyncGetRegistrationsCallbacks>(
189 make_scoped_ptr(callbacks), WorkerThread::GetCurrentId())));
192 void BackgroundSyncProviderThreadProxy::getPermissionStatus(
193 blink::WebSyncRegistration::Periodicity periodicity,
194 blink::WebServiceWorkerRegistration* service_worker_registration,
195 blink::WebSyncGetPermissionStatusCallbacks* callbacks) {
196 DCHECK(service_worker_registration);
197 DCHECK(callbacks);
198 main_thread_task_runner_->PostTask(
199 FROM_HERE,
200 base::Bind(
201 &BackgroundSyncProvider::getPermissionStatus,
202 base::Unretained(sync_provider_), periodicity,
203 service_worker_registration,
204 new CallbackThreadAdapter<blink::WebSyncGetPermissionStatusCallbacks>(
205 make_scoped_ptr(callbacks), WorkerThread::GetCurrentId())));
208 void BackgroundSyncProviderThreadProxy::releaseRegistration(int64_t handle_id) {
209 main_thread_task_runner_->PostTask(
210 FROM_HERE, base::Bind(&BackgroundSyncProvider::releaseRegistration,
211 base::Unretained(sync_provider_), handle_id));
214 void BackgroundSyncProviderThreadProxy::notifyWhenDone(
215 int64_t handle_id,
216 blink::WebSyncNotifyWhenDoneCallbacks* callbacks) {
217 DCHECK(callbacks);
219 main_thread_task_runner_->PostTask(
220 FROM_HERE,
221 base::Bind(
222 &BackgroundSyncProvider::notifyWhenDone,
223 base::Unretained(sync_provider_), handle_id,
224 new CallbackThreadAdapter<blink::WebSyncNotifyWhenDoneCallbacks>(
225 make_scoped_ptr(callbacks), WorkerThread::GetCurrentId())));
228 void BackgroundSyncProviderThreadProxy::DuplicateRegistrationHandle(
229 int64 handle_id,
230 const BackgroundSyncService::DuplicateRegistrationHandleCallback&
231 callback) {
232 main_thread_task_runner_->PostTask(
233 FROM_HERE,
234 base::Bind(&BackgroundSyncProvider::DuplicateRegistrationHandle,
235 base::Unretained(sync_provider_), handle_id,
236 base::Bind(&DuplicateRegistrationHandleCallbackOnMainThread,
237 WorkerThread::GetCurrentId(), callback)));
240 void BackgroundSyncProviderThreadProxy::WillStopCurrentWorkerThread() {
241 delete this;
244 BackgroundSyncProviderThreadProxy::BackgroundSyncProviderThreadProxy(
245 base::SingleThreadTaskRunner* main_thread_task_runner,
246 BackgroundSyncProvider* sync_provider)
247 : main_thread_task_runner_(main_thread_task_runner),
248 sync_provider_(sync_provider) {
249 g_sync_provider_tls.Pointer()->Set(this);
252 BackgroundSyncProviderThreadProxy::~BackgroundSyncProviderThreadProxy() {
253 g_sync_provider_tls.Pointer()->Set(nullptr);
256 } // namespace content