OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef MojoHelper_h | 5 #ifndef MojoHelper_h |
6 #define MojoHelper_h | 6 #define MojoHelper_h |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "mojo/public/cpp/bindings/callback.h" | 9 #include "mojo/public/cpp/bindings/callback.h" |
10 #include "mojo/public/cpp/bindings/wtf_array.h" | 10 #include "mojo/public/cpp/bindings/wtf_array.h" |
11 #include "platform/heap/HeapAllocator.h" | 11 #include "platform/heap/HeapAllocator.h" |
12 #include <utility> | 12 #include <utility> |
13 | 13 |
14 namespace blink { | 14 namespace blink { |
15 | 15 |
| 16 namespace internal { |
| 17 |
| 18 template <typename R, typename... Args> |
| 19 R CallWTFFunction(Function<R(Args...)>* functor, Args... args) |
| 20 { |
| 21 return (*functor)(std::forward<Args>(args)...); |
| 22 } |
| 23 |
| 24 } |
| 25 |
16 // Binds an instance of a class to its member function. Does not bind anything | 26 // Binds an instance of a class to its member function. Does not bind anything |
17 // else. Provides limited access to base::Bind() function. base::Bind() could | 27 // else. Provides limited access to base::Bind() function. base::Bind() could |
18 // be dangerous if it's used across threads, so we don't want to allow general | 28 // be dangerous if it's used across threads, so we don't want to allow general |
19 // use of base::Bind(). See also "platform/ThreadSafeFunctional.h". | 29 // use of base::Bind(). See also "platform/ThreadSafeFunctional.h". |
20 template <typename Class, typename ReturnType, typename... Args> | 30 template <typename Class, typename ReturnType, typename... Args> |
21 mojo::Callback<ReturnType(Args...)> | 31 mojo::Callback<ReturnType(Args...)> |
22 sameThreadBindForMojo(ReturnType (Class::*method)(Args...), Class* instance) | 32 sameThreadBindForMojo(ReturnType (Class::*method)(Args...), Class* instance) |
23 { | 33 { |
24 return base::Bind(method, base::Unretained(instance)); | 34 return base::Bind(method, base::Unretained(instance)); |
25 } | 35 } |
26 | 36 |
| 37 template <typename R, typename... Args> |
| 38 base::Callback<R(Args...)> createBaseCallback(PassOwnPtr<Function<R(Args...)>> f
unctor) |
| 39 { |
| 40 return base::Bind(&internal::CallWTFFunction<R, Args...>, base::Owned(functo
r.leakPtr())); |
| 41 } |
| 42 |
27 } // namespace blink | 43 } // namespace blink |
28 | 44 |
29 namespace mojo { | 45 namespace mojo { |
30 | 46 |
31 // A |TypeConverter| that will create a |WTFArray<T>| containing a copy of the | 47 // A |TypeConverter| that will create a |WTFArray<T>| containing a copy of the |
32 // contents of a |blink::HeapVector<E>|, using |TypeConverter<T, E>| to copy | 48 // contents of a |blink::HeapVector<E>|, using |TypeConverter<T, E>| to copy |
33 // each element. The returned array will always be non-null. | 49 // each element. The returned array will always be non-null. |
34 template <typename T, typename E> | 50 template <typename T, typename E> |
35 struct TypeConverter<WTFArray<T>, blink::HeapVector<E>> { | 51 struct TypeConverter<WTFArray<T>, blink::HeapVector<E>> { |
36 static WTFArray<T> Convert(const blink::HeapVector<E>& input) | 52 static WTFArray<T> Convert(const blink::HeapVector<E>& input) |
37 { | 53 { |
38 WTFArray<T> result(input.size()); | 54 WTFArray<T> result(input.size()); |
39 for (size_t i = 0; i < input.size(); ++i) | 55 for (size_t i = 0; i < input.size(); ++i) |
40 result[i] = TypeConverter<T, E>::Convert(input[i]); | 56 result[i] = TypeConverter<T, E>::Convert(input[i]); |
41 return std::move(result); | 57 return std::move(result); |
42 } | 58 } |
43 }; | 59 }; |
44 | 60 |
45 } // namespace mojo | 61 } // namespace mojo |
46 | 62 |
47 #endif // MojoHelper_h | 63 #endif // MojoHelper_h |
OLD | NEW |