OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef UI_OZONE_PLATFORM_DRM_GPU_PROXY_HELPERS_H_ |
| 6 #define UI_OZONE_PLATFORM_DRM_GPU_PROXY_HELPERS_H_ |
| 7 |
| 8 #include "base/bind.h" |
| 9 #include "base/location.h" |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/thread_task_runner_handle.h" |
| 12 |
| 13 namespace ui { |
| 14 |
| 15 namespace internal { |
| 16 |
| 17 template <typename... Args> |
| 18 void PostAsyncTask( |
| 19 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 20 const base::Callback<void(Args...)>& callback, |
| 21 Args... args) { |
| 22 task_runner->PostTask(FROM_HERE, base::Bind(callback, args...)); |
| 23 } |
| 24 |
| 25 template <typename Functor, typename Type, typename Return, typename... Args> |
| 26 void RunWeakPtrMethod(Functor func, |
| 27 base::WeakPtr<Type> type, |
| 28 Return* ret, |
| 29 const Args&... args) { |
| 30 if (type) |
| 31 *ret = ((*type.get()).*func)(args...); |
| 32 } |
| 33 |
| 34 } // namespace internal |
| 35 |
| 36 // Posts a task to a different thread and blocks waiting for the task to finish |
| 37 // executing. |
| 38 void PostSyncTask( |
| 39 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 40 const base::Closure& callback); |
| 41 |
| 42 // Creates a callback that will run |callback| on the calling thread. Useful |
| 43 // when posting a task on a different thread and expecting a callback when the |
| 44 // task finished (and the callback needs to run on the original thread). |
| 45 template <typename... Args> |
| 46 base::Callback<void(Args...)> CreateSafeCallback( |
| 47 const base::Callback<void(Args...)>& callback) { |
| 48 return base::Bind(&internal::PostAsyncTask<Args...>, |
| 49 base::ThreadTaskRunnerHandle::Get(), callback); |
| 50 } |
| 51 |
| 52 // Helper function allowing the caller to post a task to a different thread and |
| 53 // calling a base::WeakPtr with a method that returns a value. Since a weak |
| 54 // pointer may have expired, this function checks the pointer before attemping |
| 55 // to run the method. |
| 56 // |
| 57 // base::Bind doesn't allow us to do this since the base::WeakPtr specialization |
| 58 // allows weak pointers only when the method has a void return. |
| 59 // |
| 60 // If the weak pointer is valid, |ret| will contain the returned value from |
| 61 // type->func(args), otherwise |ret| will not be modified. |
| 62 template <typename Functor, typename Type, typename Return, typename... Args> |
| 63 base::Closure CreateWeakPtrCallback(Functor func, |
| 64 base::WeakPtr<Type> type, |
| 65 Return* ret, |
| 66 const Args&... args) { |
| 67 return base::Bind(&internal::RunWeakPtrMethod<Functor, Type, Return, Args...>, |
| 68 func, type, ret, args...); |
| 69 } |
| 70 |
| 71 } // namespace ui |
| 72 |
| 73 #endif // UI_OZONE_PLATFORM_DRM_GPU_PROXY_HELPERS_H_ |
OLD | NEW |