| 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 template <typename S, typename T> |
| 24 void RunOnSuccess(scoped_ptr<blink::WebCallbacks<S, T>> callbacks, S* results) { |
| 25 callbacks->onSuccess(results); |
| 26 } |
| 27 |
| 23 // CallbackThreadAdapter<S,T> is a wrapper for WebCallbacks<S,T> which | 28 // CallbackThreadAdapter<S,T> is a wrapper for WebCallbacks<S,T> which |
| 24 // switches to a specific thread before calling the wrapped callback's | 29 // switches to a specific thread before calling the wrapped callback's |
| 25 // onSuccess or onError methods. | 30 // onSuccess or onError methods. |
| 26 // | 31 // |
| 27 // Takes ownership of the WebCallbacks object which it wraps. | 32 // Takes ownership of the WebCallbacks object which it wraps. |
| 28 template <typename S, typename T> | 33 template <typename S, typename T> |
| 29 class CallbackThreadAdapter : public blink::WebCallbacks<S, T> { | 34 class CallbackThreadAdapter : public blink::WebCallbacks<S, T> { |
| 30 public: | 35 public: |
| 31 CallbackThreadAdapter(scoped_ptr<blink::WebCallbacks<S, T>> callbacks, | 36 CallbackThreadAdapter(scoped_ptr<blink::WebCallbacks<S, T>> callbacks, |
| 32 int worker_thread_id) | 37 int worker_thread_id) |
| 33 : worker_thread_id_(worker_thread_id) { | 38 : worker_thread_id_(worker_thread_id) { |
| 34 callbacks_.reset(callbacks.release()); | 39 callbacks_.reset(callbacks.release()); |
| 35 } | 40 } |
| 36 | 41 |
| 37 virtual void onSuccess(S* results) { | 42 virtual void onSuccess(S* results) { |
| 38 // If the worker thread has been destroyed, then this task will be | 43 // If the worker thread has been destroyed, then this task will be |
| 39 // silently discarded. | 44 // silently discarded. |
| 40 WorkerTaskRunner::Instance()->PostTask( | 45 WorkerTaskRunner::Instance()->PostTask( |
| 41 worker_thread_id_, | 46 worker_thread_id_, |
| 42 base::Bind(&blink::WebCallbacks<S, T>::onSuccess, | 47 base::Bind(&RunOnSuccess<S, T>, base::Passed(callbacks_.Pass()), |
| 43 base::Owned(callbacks_.release()), results)); | 48 results)); |
| 44 } | 49 } |
| 45 | 50 |
| 46 virtual void onError(T* error) { | 51 virtual void onError(T* error) { |
| 47 // If the worker thread has been destroyed, then this task will be | 52 // If the worker thread has been destroyed, then this task will be |
| 48 // silently discarded. | 53 // silently discarded. |
| 49 WorkerTaskRunner::Instance()->PostTask( | 54 WorkerTaskRunner::Instance()->PostTask( |
| 50 worker_thread_id_, | 55 worker_thread_id_, |
| 51 base::Bind(&blink::WebCallbacks<S, T>::onError, | 56 base::Bind(&blink::WebCallbacks<S, T>::onError, |
| 52 base::Owned(callbacks_.release()), error)); | 57 base::Owned(callbacks_.release()), error)); |
| 53 } | 58 } |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 : main_thread_task_runner_(main_thread_task_runner), | 181 : main_thread_task_runner_(main_thread_task_runner), |
| 177 sync_provider_(sync_provider) { | 182 sync_provider_(sync_provider) { |
| 178 g_sync_provider_tls.Pointer()->Set(this); | 183 g_sync_provider_tls.Pointer()->Set(this); |
| 179 } | 184 } |
| 180 | 185 |
| 181 BackgroundSyncProviderThreadProxy::~BackgroundSyncProviderThreadProxy() { | 186 BackgroundSyncProviderThreadProxy::~BackgroundSyncProviderThreadProxy() { |
| 182 g_sync_provider_tls.Pointer()->Set(nullptr); | 187 g_sync_provider_tls.Pointer()->Set(nullptr); |
| 183 } | 188 } |
| 184 | 189 |
| 185 } // namespace content | 190 } // namespace content |
| OLD | NEW |