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/CrossThreadCopier.h" |
11 #include "platform/heap/HeapAllocator.h" | 12 #include "platform/heap/HeapAllocator.h" |
12 #include <utility> | 13 #include <utility> |
13 | 14 |
14 namespace blink { | 15 namespace blink { |
15 | 16 |
16 namespace internal { | 17 namespace internal { |
17 | 18 |
18 template <typename R, typename... Args> | 19 template <typename R, typename... Args> |
19 R CallWTFFunction(Function<R(Args...)>* functor, Args... args) | 20 R CallWTFFunction(Function<R(Args...)>* functor, Args... args) |
20 { | 21 { |
21 return (*functor)(std::forward<Args>(args)...); | 22 return (*functor)(std::forward<Args>(args)...); |
22 } | 23 } |
23 | 24 |
24 } | 25 } |
25 | 26 |
26 template <typename R, typename... Args> | 27 template <typename R, typename... Args> |
27 base::Callback<R(Args...)> createBaseCallback(PassOwnPtr<Function<R(Args...)>> f
unctor) | 28 base::Callback<R(Args...)> createBaseCallback(PassOwnPtr<Function<R(Args...)>> f
unctor) |
28 { | 29 { |
29 return base::Bind(&internal::CallWTFFunction<R, Args...>, base::Owned(functo
r.leakPtr())); | 30 return base::Bind(&internal::CallWTFFunction<R, Args...>, base::Owned(functo
r.leakPtr())); |
30 } | 31 } |
31 | 32 |
32 template <typename R, typename... Args> | 33 template <typename R, typename... Args> |
33 base::Callback<R(Args...)> createBaseCallback(std::unique_ptr<Function<R(Args...
)>> functor) | 34 base::Callback<R(Args...)> createBaseCallback(std::unique_ptr<Function<R(Args...
)>> functor) |
34 { | 35 { |
35 return base::Bind(&internal::CallWTFFunction<R, Args...>, base::Owned(functo
r.release())); | 36 return base::Bind(&internal::CallWTFFunction<R, Args...>, base::Owned(functo
r.release())); |
36 } | 37 } |
37 | 38 |
| 39 template <typename T> |
| 40 struct CrossThreadCopier<mojo::InterfaceRequest<T>> { |
| 41 STATIC_ONLY(CrossThreadCopier); |
| 42 using Type = mojo::InterfaceRequest<T>; |
| 43 static Type copy(Type value) { return value; /* This is in fact a move. */ } |
| 44 }; |
| 45 |
38 } // namespace blink | 46 } // namespace blink |
39 | 47 |
40 namespace mojo { | 48 namespace mojo { |
41 | 49 |
42 // A |TypeConverter| that will create a |WTFArray<T>| containing a copy of the | 50 // A |TypeConverter| that will create a |WTFArray<T>| containing a copy of the |
43 // contents of a |blink::HeapVector<E>|, using |TypeConverter<T, E>| to copy | 51 // contents of a |blink::HeapVector<E>|, using |TypeConverter<T, E>| to copy |
44 // each element. The returned array will always be non-null. | 52 // each element. The returned array will always be non-null. |
45 template <typename T, typename E> | 53 template <typename T, typename E> |
46 struct TypeConverter<WTFArray<T>, blink::HeapVector<E>> { | 54 struct TypeConverter<WTFArray<T>, blink::HeapVector<E>> { |
47 static WTFArray<T> Convert(const blink::HeapVector<E>& input) | 55 static WTFArray<T> Convert(const blink::HeapVector<E>& input) |
48 { | 56 { |
49 WTFArray<T> result(input.size()); | 57 WTFArray<T> result(input.size()); |
50 for (size_t i = 0; i < input.size(); ++i) | 58 for (size_t i = 0; i < input.size(); ++i) |
51 result[i] = TypeConverter<T, E>::Convert(input[i]); | 59 result[i] = TypeConverter<T, E>::Convert(input[i]); |
52 return std::move(result); | 60 return std::move(result); |
53 } | 61 } |
54 }; | 62 }; |
55 | 63 |
56 } // namespace mojo | 64 } // namespace mojo |
57 | 65 |
58 #endif // MojoHelper_h | 66 #endif // MojoHelper_h |
OLD | NEW |