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