OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/child/background_sync/background_sync_provider_thread_proxy.h" | 5 #include "content/child/background_sync/background_sync_provider_thread_proxy.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/threading/thread_local.h" | 13 #include "base/threading/thread_local.h" |
14 #include "content/child/background_sync/background_sync_provider.h" | 14 #include "content/child/background_sync/background_sync_provider.h" |
15 | 15 |
16 using base::LazyInstance; | 16 using base::LazyInstance; |
17 using base::ThreadLocalPointer; | 17 using base::ThreadLocalPointer; |
18 | 18 |
19 namespace content { | 19 namespace content { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 // CallbackThreadAdapter<S,T> is a wrapper for WebCallbacks<S,T> which | 23 template <typename T> |
24 // switches to a specific thread before calling the wrapped callback's | 24 struct WebCallbacksMatcher; |
| 25 |
| 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>; |
| 31 }; |
| 32 |
| 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 |
25 // onSuccess or onError methods. | 35 // onSuccess or onError methods. |
26 // | 36 // |
27 // Takes ownership of the WebCallbacks object which it wraps. | 37 // Takes ownership of the WebCallbacks object which it wraps. |
28 template <typename S, typename T> | 38 template <typename X> |
29 class CallbackThreadAdapter : public blink::WebCallbacks<S, T> { | 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); |
| 44 |
30 public: | 45 public: |
31 CallbackThreadAdapter(scoped_ptr<blink::WebCallbacks<S, T>> callbacks, | 46 CallbackThreadAdapter(scoped_ptr<blink::WebCallbacks<S, T>> callbacks, |
32 int worker_thread_id) | 47 int worker_thread_id) |
33 : worker_thread_id_(worker_thread_id) { | 48 : worker_thread_id_(worker_thread_id) { |
34 callbacks_.reset(callbacks.release()); | 49 callbacks_.reset(callbacks.release()); |
35 } | 50 } |
36 | 51 |
37 virtual void onSuccess(S results) { | 52 virtual void onSuccess(S results) { |
| 53 OnSuccessType on_success = &blink::WebCallbacks<S, T>::onSuccess; |
38 // If the worker thread has been destroyed, then this task will be | 54 // If the worker thread has been destroyed, then this task will be |
39 // silently discarded. | 55 // silently discarded. |
40 WorkerTaskRunner::Instance()->PostTask( | 56 WorkerTaskRunner::Instance()->PostTask( |
41 worker_thread_id_, | 57 worker_thread_id_, |
42 base::Bind(&blink::WebCallbacks<S, T>::onSuccess, | 58 base::Bind(on_success, base::Owned(callbacks_.release()), results)); |
43 base::Owned(callbacks_.release()), results)); | |
44 } | 59 } |
45 | 60 |
46 virtual void onError(T error) { | 61 virtual void onError(T error) { |
| 62 OnErrorType on_error = &blink::WebCallbacks<S, T>::onError; |
47 // If the worker thread has been destroyed, then this task will be | 63 // If the worker thread has been destroyed, then this task will be |
48 // silently discarded. | 64 // silently discarded. |
49 WorkerTaskRunner::Instance()->PostTask( | 65 WorkerTaskRunner::Instance()->PostTask( |
50 worker_thread_id_, | 66 worker_thread_id_, |
51 base::Bind(&blink::WebCallbacks<S, T>::onError, | 67 base::Bind(on_error, base::Owned(callbacks_.release()), error)); |
52 base::Owned(callbacks_.release()), error)); | |
53 } | 68 } |
54 | 69 |
55 private: | 70 private: |
56 scoped_ptr<blink::WebCallbacks<S, T>> callbacks_; | 71 scoped_ptr<blink::WebCallbacks<S, T>> callbacks_; |
57 int worker_thread_id_; | 72 int worker_thread_id_; |
58 }; | 73 }; |
59 | 74 |
60 LazyInstance<ThreadLocalPointer<BackgroundSyncProviderThreadProxy>>::Leaky | 75 LazyInstance<ThreadLocalPointer<BackgroundSyncProviderThreadProxy>>::Leaky |
61 g_sync_provider_tls = LAZY_INSTANCE_INITIALIZER; | 76 g_sync_provider_tls = LAZY_INSTANCE_INITIALIZER; |
62 | 77 |
(...skipping 25 matching lines...) Expand all Loading... |
88 blink::WebServiceWorkerRegistration* service_worker_registration, | 103 blink::WebServiceWorkerRegistration* service_worker_registration, |
89 blink::WebSyncRegistrationCallbacks* callbacks) { | 104 blink::WebSyncRegistrationCallbacks* callbacks) { |
90 DCHECK(options); | 105 DCHECK(options); |
91 DCHECK(service_worker_registration); | 106 DCHECK(service_worker_registration); |
92 DCHECK(callbacks); | 107 DCHECK(callbacks); |
93 main_thread_task_runner_->PostTask( | 108 main_thread_task_runner_->PostTask( |
94 FROM_HERE, | 109 FROM_HERE, |
95 base::Bind(&BackgroundSyncProvider::registerBackgroundSync, | 110 base::Bind(&BackgroundSyncProvider::registerBackgroundSync, |
96 base::Unretained(sync_provider_), options, | 111 base::Unretained(sync_provider_), options, |
97 service_worker_registration, | 112 service_worker_registration, |
98 new CallbackThreadAdapter<blink::WebSyncRegistration*, | 113 new CallbackThreadAdapter<blink::WebSyncRegistrationCallbacks>( |
99 blink::WebSyncError*>( | |
100 make_scoped_ptr(callbacks), | 114 make_scoped_ptr(callbacks), |
101 WorkerTaskRunner::Instance()->CurrentWorkerId()))); | 115 WorkerTaskRunner::Instance()->CurrentWorkerId()))); |
102 } | 116 } |
103 | 117 |
104 void BackgroundSyncProviderThreadProxy::unregisterBackgroundSync( | 118 void BackgroundSyncProviderThreadProxy::unregisterBackgroundSync( |
105 blink::WebSyncRegistration::Periodicity periodicity, | 119 blink::WebSyncRegistration::Periodicity periodicity, |
106 int64_t id, | 120 int64_t id, |
107 const blink::WebString& tag, | 121 const blink::WebString& tag, |
108 blink::WebServiceWorkerRegistration* service_worker_registration, | 122 blink::WebServiceWorkerRegistration* service_worker_registration, |
109 blink::WebSyncUnregistrationCallbacks* callbacks) { | 123 blink::WebSyncUnregistrationCallbacks* callbacks) { |
110 DCHECK(service_worker_registration); | 124 DCHECK(service_worker_registration); |
111 DCHECK(callbacks); | 125 DCHECK(callbacks); |
112 main_thread_task_runner_->PostTask( | 126 main_thread_task_runner_->PostTask( |
113 FROM_HERE, | 127 FROM_HERE, |
114 base::Bind(&BackgroundSyncProvider::unregisterBackgroundSync, | 128 base::Bind( |
115 base::Unretained(sync_provider_), periodicity, id, | 129 &BackgroundSyncProvider::unregisterBackgroundSync, |
116 // We cast WebString to string16 before crossing threads | 130 base::Unretained(sync_provider_), periodicity, id, |
117 // for thread-safety. | 131 // We cast WebString to string16 before crossing threads |
118 static_cast<base::string16>(tag), service_worker_registration, | 132 // for thread-safety. |
119 new CallbackThreadAdapter<bool*, blink::WebSyncError*>( | 133 static_cast<base::string16>(tag), service_worker_registration, |
120 make_scoped_ptr(callbacks), | 134 new CallbackThreadAdapter<blink::WebSyncUnregistrationCallbacks>( |
121 WorkerTaskRunner::Instance()->CurrentWorkerId()))); | 135 make_scoped_ptr(callbacks), |
| 136 WorkerTaskRunner::Instance()->CurrentWorkerId()))); |
122 } | 137 } |
123 | 138 |
124 void BackgroundSyncProviderThreadProxy::getRegistration( | 139 void BackgroundSyncProviderThreadProxy::getRegistration( |
125 blink::WebSyncRegistration::Periodicity periodicity, | 140 blink::WebSyncRegistration::Periodicity periodicity, |
126 const blink::WebString& tag, | 141 const blink::WebString& tag, |
127 blink::WebServiceWorkerRegistration* service_worker_registration, | 142 blink::WebServiceWorkerRegistration* service_worker_registration, |
128 blink::WebSyncRegistrationCallbacks* callbacks) { | 143 blink::WebSyncRegistrationCallbacks* callbacks) { |
129 DCHECK(service_worker_registration); | 144 DCHECK(service_worker_registration); |
130 DCHECK(callbacks); | 145 DCHECK(callbacks); |
131 main_thread_task_runner_->PostTask( | 146 main_thread_task_runner_->PostTask( |
132 FROM_HERE, | 147 FROM_HERE, |
133 base::Bind(&BackgroundSyncProvider::getRegistration, | 148 base::Bind(&BackgroundSyncProvider::getRegistration, |
134 base::Unretained(sync_provider_), periodicity, | 149 base::Unretained(sync_provider_), periodicity, |
135 // We cast WebString to string16 before crossing threads | 150 // We cast WebString to string16 before crossing threads |
136 // for thread-safety. | 151 // for thread-safety. |
137 static_cast<base::string16>(tag), service_worker_registration, | 152 static_cast<base::string16>(tag), service_worker_registration, |
138 new CallbackThreadAdapter<blink::WebSyncRegistration*, | 153 new CallbackThreadAdapter<blink::WebSyncRegistrationCallbacks>( |
139 blink::WebSyncError*>( | |
140 make_scoped_ptr(callbacks), | 154 make_scoped_ptr(callbacks), |
141 WorkerTaskRunner::Instance()->CurrentWorkerId()))); | 155 WorkerTaskRunner::Instance()->CurrentWorkerId()))); |
142 } | 156 } |
143 | 157 |
144 void BackgroundSyncProviderThreadProxy::getRegistrations( | 158 void BackgroundSyncProviderThreadProxy::getRegistrations( |
145 blink::WebSyncRegistration::Periodicity periodicity, | 159 blink::WebSyncRegistration::Periodicity periodicity, |
146 blink::WebServiceWorkerRegistration* service_worker_registration, | 160 blink::WebServiceWorkerRegistration* service_worker_registration, |
147 blink::WebSyncGetRegistrationsCallbacks* callbacks) { | 161 blink::WebSyncGetRegistrationsCallbacks* callbacks) { |
148 DCHECK(service_worker_registration); | 162 DCHECK(service_worker_registration); |
149 DCHECK(callbacks); | 163 DCHECK(callbacks); |
150 main_thread_task_runner_->PostTask( | 164 main_thread_task_runner_->PostTask( |
151 FROM_HERE, | 165 FROM_HERE, |
152 base::Bind(&BackgroundSyncProvider::getRegistrations, | 166 base::Bind( |
153 base::Unretained(sync_provider_), periodicity, | 167 &BackgroundSyncProvider::getRegistrations, |
154 service_worker_registration, | 168 base::Unretained(sync_provider_), periodicity, |
155 new CallbackThreadAdapter< | 169 service_worker_registration, |
156 blink::WebVector<blink::WebSyncRegistration*>*, | 170 new CallbackThreadAdapter<blink::WebSyncGetRegistrationsCallbacks>( |
157 blink::WebSyncError*>( | 171 make_scoped_ptr(callbacks), |
158 make_scoped_ptr(callbacks), | 172 WorkerTaskRunner::Instance()->CurrentWorkerId()))); |
159 WorkerTaskRunner::Instance()->CurrentWorkerId()))); | |
160 } | 173 } |
161 | 174 |
162 void BackgroundSyncProviderThreadProxy::getPermissionStatus( | 175 void BackgroundSyncProviderThreadProxy::getPermissionStatus( |
163 blink::WebSyncRegistration::Periodicity periodicity, | 176 blink::WebSyncRegistration::Periodicity periodicity, |
164 blink::WebServiceWorkerRegistration* service_worker_registration, | 177 blink::WebServiceWorkerRegistration* service_worker_registration, |
165 blink::WebSyncGetPermissionStatusCallbacks* callbacks) { | 178 blink::WebSyncGetPermissionStatusCallbacks* callbacks) { |
166 DCHECK(service_worker_registration); | 179 DCHECK(service_worker_registration); |
167 DCHECK(callbacks); | 180 DCHECK(callbacks); |
168 main_thread_task_runner_->PostTask( | 181 main_thread_task_runner_->PostTask( |
169 FROM_HERE, | 182 FROM_HERE, |
170 base::Bind(&BackgroundSyncProvider::getPermissionStatus, | 183 base::Bind( |
171 base::Unretained(sync_provider_), periodicity, | 184 &BackgroundSyncProvider::getPermissionStatus, |
172 service_worker_registration, | 185 base::Unretained(sync_provider_), periodicity, |
173 new CallbackThreadAdapter<blink::WebSyncPermissionStatus*, | 186 service_worker_registration, |
174 blink::WebSyncError*>( | 187 new CallbackThreadAdapter<blink::WebSyncGetPermissionStatusCallbacks>( |
175 make_scoped_ptr(callbacks), | 188 make_scoped_ptr(callbacks), |
176 WorkerTaskRunner::Instance()->CurrentWorkerId()))); | 189 WorkerTaskRunner::Instance()->CurrentWorkerId()))); |
177 } | 190 } |
178 | 191 |
179 void BackgroundSyncProviderThreadProxy::OnWorkerRunLoopStopped() { | 192 void BackgroundSyncProviderThreadProxy::OnWorkerRunLoopStopped() { |
180 delete this; | 193 delete this; |
181 } | 194 } |
182 | 195 |
183 BackgroundSyncProviderThreadProxy::BackgroundSyncProviderThreadProxy( | 196 BackgroundSyncProviderThreadProxy::BackgroundSyncProviderThreadProxy( |
184 base::SingleThreadTaskRunner* main_thread_task_runner, | 197 base::SingleThreadTaskRunner* main_thread_task_runner, |
185 BackgroundSyncProvider* sync_provider) | 198 BackgroundSyncProvider* sync_provider) |
186 : main_thread_task_runner_(main_thread_task_runner), | 199 : main_thread_task_runner_(main_thread_task_runner), |
187 sync_provider_(sync_provider) { | 200 sync_provider_(sync_provider) { |
188 g_sync_provider_tls.Pointer()->Set(this); | 201 g_sync_provider_tls.Pointer()->Set(this); |
189 } | 202 } |
190 | 203 |
191 BackgroundSyncProviderThreadProxy::~BackgroundSyncProviderThreadProxy() { | 204 BackgroundSyncProviderThreadProxy::~BackgroundSyncProviderThreadProxy() { |
192 g_sync_provider_tls.Pointer()->Set(nullptr); | 205 g_sync_provider_tls.Pointer()->Set(nullptr); |
193 } | 206 } |
194 | 207 |
195 } // namespace content | 208 } // namespace content |
OLD | NEW |