Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / child / background_sync / background_sync_provider_thread_proxy.cc
blob90584bfc8380a6d31a2280ddd33303e83e52439a
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 // 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> {
30 public:
31 CallbackThreadAdapter(scoped_ptr<blink::WebCallbacks<S, T>> callbacks,
32 int worker_thread_id)
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(
41 worker_thread_id_,
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(
50 worker_thread_id_,
51 base::Bind(&blink::WebCallbacks<S, T>::onError,
52 base::Owned(callbacks_.release()), error));
55 private:
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
65 BackgroundSyncProviderThreadProxy*
66 BackgroundSyncProviderThreadProxy::GetThreadInstance(
67 base::SingleThreadTaskRunner* main_thread_task_runner,
68 BackgroundSyncProvider* sync_provider) {
69 if (g_sync_provider_tls.Pointer()->Get())
70 return g_sync_provider_tls.Pointer()->Get();
72 BackgroundSyncProviderThreadProxy* instance =
73 new BackgroundSyncProviderThreadProxy(main_thread_task_runner,
74 sync_provider);
75 DCHECK(WorkerTaskRunner::Instance()->CurrentWorkerId());
76 WorkerTaskRunner::Instance()->AddStopObserver(instance);
77 return instance;
80 void BackgroundSyncProviderThreadProxy::registerBackgroundSync(
81 const blink::WebSyncRegistration* options,
82 blink::WebServiceWorkerRegistration* service_worker_registration,
83 blink::WebSyncRegistrationCallbacks* callbacks) {
84 DCHECK(options);
85 DCHECK(service_worker_registration);
86 DCHECK(callbacks);
87 main_thread_task_runner_->PostTask(
88 FROM_HERE,
89 base::Bind(&BackgroundSyncProvider::registerBackgroundSync,
90 base::Unretained(sync_provider_), options,
91 service_worker_registration,
92 new CallbackThreadAdapter<blink::WebSyncRegistration*,
93 blink::WebSyncError*>(
94 make_scoped_ptr(callbacks),
95 WorkerTaskRunner::Instance()->CurrentWorkerId())));
98 void BackgroundSyncProviderThreadProxy::unregisterBackgroundSync(
99 blink::WebSyncRegistration::Periodicity periodicity,
100 int64_t id,
101 const blink::WebString& tag,
102 blink::WebServiceWorkerRegistration* service_worker_registration,
103 blink::WebSyncUnregistrationCallbacks* callbacks) {
104 DCHECK(service_worker_registration);
105 DCHECK(callbacks);
106 main_thread_task_runner_->PostTask(
107 FROM_HERE,
108 base::Bind(&BackgroundSyncProvider::unregisterBackgroundSync,
109 base::Unretained(sync_provider_), periodicity, id,
110 // We cast WebString to string16 before crossing threads
111 // for thread-safety.
112 static_cast<base::string16>(tag), service_worker_registration,
113 new CallbackThreadAdapter<bool*, blink::WebSyncError*>(
114 make_scoped_ptr(callbacks),
115 WorkerTaskRunner::Instance()->CurrentWorkerId())));
118 void BackgroundSyncProviderThreadProxy::getRegistration(
119 blink::WebSyncRegistration::Periodicity periodicity,
120 const blink::WebString& tag,
121 blink::WebServiceWorkerRegistration* service_worker_registration,
122 blink::WebSyncRegistrationCallbacks* callbacks) {
123 DCHECK(service_worker_registration);
124 DCHECK(callbacks);
125 main_thread_task_runner_->PostTask(
126 FROM_HERE,
127 base::Bind(&BackgroundSyncProvider::getRegistration,
128 base::Unretained(sync_provider_), periodicity,
129 // We cast WebString to string16 before crossing threads
130 // for thread-safety.
131 static_cast<base::string16>(tag), service_worker_registration,
132 new CallbackThreadAdapter<blink::WebSyncRegistration*,
133 blink::WebSyncError*>(
134 make_scoped_ptr(callbacks),
135 WorkerTaskRunner::Instance()->CurrentWorkerId())));
138 void BackgroundSyncProviderThreadProxy::getRegistrations(
139 blink::WebSyncRegistration::Periodicity periodicity,
140 blink::WebServiceWorkerRegistration* service_worker_registration,
141 blink::WebSyncGetRegistrationsCallbacks* callbacks) {
142 DCHECK(service_worker_registration);
143 DCHECK(callbacks);
144 main_thread_task_runner_->PostTask(
145 FROM_HERE,
146 base::Bind(&BackgroundSyncProvider::getRegistrations,
147 base::Unretained(sync_provider_), periodicity,
148 service_worker_registration,
149 new CallbackThreadAdapter<
150 blink::WebVector<blink::WebSyncRegistration*>*,
151 blink::WebSyncError*>(
152 make_scoped_ptr(callbacks),
153 WorkerTaskRunner::Instance()->CurrentWorkerId())));
156 void BackgroundSyncProviderThreadProxy::getPermissionStatus(
157 blink::WebSyncRegistration::Periodicity periodicity,
158 blink::WebServiceWorkerRegistration* service_worker_registration,
159 blink::WebSyncGetPermissionStatusCallbacks* callbacks) {
160 DCHECK(service_worker_registration);
161 DCHECK(callbacks);
162 main_thread_task_runner_->PostTask(
163 FROM_HERE,
164 base::Bind(&BackgroundSyncProvider::getPermissionStatus,
165 base::Unretained(sync_provider_), periodicity,
166 service_worker_registration,
167 new CallbackThreadAdapter<blink::WebSyncPermissionStatus*,
168 blink::WebSyncError*>(
169 make_scoped_ptr(callbacks),
170 WorkerTaskRunner::Instance()->CurrentWorkerId())));
173 void BackgroundSyncProviderThreadProxy::OnWorkerRunLoopStopped() {
174 delete this;
177 BackgroundSyncProviderThreadProxy::BackgroundSyncProviderThreadProxy(
178 base::SingleThreadTaskRunner* main_thread_task_runner,
179 BackgroundSyncProvider* sync_provider)
180 : main_thread_task_runner_(main_thread_task_runner),
181 sync_provider_(sync_provider) {
182 g_sync_provider_tls.Pointer()->Set(this);
185 BackgroundSyncProviderThreadProxy::~BackgroundSyncProviderThreadProxy() {
186 g_sync_provider_tls.Pointer()->Set(nullptr);
189 } // namespace content