[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 1 | // Copyright (c) 2012 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 PPAPI_PROXY_PPAPI_MESSAGE_UTILS_H_ |
| 6 | #define PPAPI_PROXY_PPAPI_MESSAGE_UTILS_H_ |
| 7 | |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 8 | #include "base/pickle.h" |
| 9 | #include "base/tuple.h" |
| 10 | #include "ipc/ipc_message.h" |
| 11 | #include "ipc/ipc_message_utils.h" |
| 12 | |
| 13 | namespace ppapi { |
| 14 | |
| 15 | namespace internal { |
| 16 | |
| 17 | // TupleTypeMatch* check whether a tuple type contains elements of the specified |
| 18 | // types. They are used to make sure the output parameters of UnpackMessage() |
| 19 | // match the corresponding message type. |
| 20 | template <class TupleType, class A> |
| 21 | struct TupleTypeMatch1 { |
| 22 | static const bool kValue = false; |
| 23 | }; |
| 24 | template <class A> |
tzik | 9ca30219 | 2016-02-11 10:24:45 | [diff] [blame] | 25 | struct TupleTypeMatch1<std::tuple<A>, A> { |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 26 | static const bool kValue = true; |
| 27 | }; |
| 28 | |
| 29 | template <class TupleType, class A, class B> |
| 30 | struct TupleTypeMatch2 { |
| 31 | static const bool kValue = false; |
| 32 | }; |
| 33 | template <class A, class B> |
tzik | 9ca30219 | 2016-02-11 10:24:45 | [diff] [blame] | 34 | struct TupleTypeMatch2<std::tuple<A, B>, A, B> { |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 35 | static const bool kValue = true; |
| 36 | }; |
| 37 | |
| 38 | template <class TupleType, class A, class B, class C> |
| 39 | struct TupleTypeMatch3 { |
| 40 | static const bool kValue = false; |
| 41 | }; |
| 42 | template <class A, class B, class C> |
tzik | 9ca30219 | 2016-02-11 10:24:45 | [diff] [blame] | 43 | struct TupleTypeMatch3<std::tuple<A, B, C>, A, B, C> { |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 44 | static const bool kValue = true; |
| 45 | }; |
| 46 | |
| 47 | template <class TupleType, class A, class B, class C, class D> |
| 48 | struct TupleTypeMatch4 { |
| 49 | static const bool kValue = false; |
| 50 | }; |
| 51 | template <class A, class B, class C, class D> |
tzik | 9ca30219 | 2016-02-11 10:24:45 | [diff] [blame] | 52 | struct TupleTypeMatch4<std::tuple<A, B, C, D>, A, B, C, D> { |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 53 | static const bool kValue = true; |
| 54 | }; |
| 55 | |
| 56 | template <class TupleType, class A, class B, class C, class D, class E> |
| 57 | struct TupleTypeMatch5 { |
| 58 | static const bool kValue = false; |
| 59 | }; |
| 60 | template <class A, class B, class C, class D, class E> |
tzik | 9ca30219 | 2016-02-11 10:24:45 | [diff] [blame] | 61 | struct TupleTypeMatch5<std::tuple<A, B, C, D, E>, A, B, C, D, E> { |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 62 | static const bool kValue = true; |
| 63 | }; |
| 64 | |
| 65 | } // namespace internal |
| 66 | |
| 67 | template <class MsgClass, class A> |
| 68 | bool UnpackMessage(const IPC::Message& msg, A* a) { |
mostynb | d349f6d6 | 2015-01-06 00:42:03 | [diff] [blame] | 69 | static_assert( |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 70 | (internal::TupleTypeMatch1<typename MsgClass::Param, A>::kValue), |
mostynb | d349f6d6 | 2015-01-06 00:42:03 | [diff] [blame] | 71 | "tuple types should match"); |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 72 | |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 73 | base::PickleIterator iter(msg); |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 74 | return IPC::ReadParam(&msg, &iter, a); |
| 75 | } |
| 76 | |
| 77 | template <class MsgClass, class A, class B> |
| 78 | bool UnpackMessage(const IPC::Message& msg, A* a, B* b) { |
mostynb | d349f6d6 | 2015-01-06 00:42:03 | [diff] [blame] | 79 | static_assert( |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 80 | (internal::TupleTypeMatch2<typename MsgClass::Param, A, B>::kValue), |
mostynb | d349f6d6 | 2015-01-06 00:42:03 | [diff] [blame] | 81 | "tuple types should match"); |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 82 | |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 83 | base::PickleIterator iter(msg); |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 84 | return IPC::ReadParam(&msg, &iter, a) && IPC::ReadParam(&msg, &iter, b); |
| 85 | } |
| 86 | |
| 87 | template <class MsgClass, class A, class B, class C> |
| 88 | bool UnpackMessage(const IPC::Message& msg, A* a, B* b, C* c) { |
mostynb | d349f6d6 | 2015-01-06 00:42:03 | [diff] [blame] | 89 | static_assert( |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 90 | (internal::TupleTypeMatch3<typename MsgClass::Param, A, B, C>::kValue), |
mostynb | d349f6d6 | 2015-01-06 00:42:03 | [diff] [blame] | 91 | "tuple types should match"); |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 92 | |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 93 | base::PickleIterator iter(msg); |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 94 | return IPC::ReadParam(&msg, &iter, a) && |
| 95 | IPC::ReadParam(&msg, &iter, b) && |
| 96 | IPC::ReadParam(&msg, &iter, c); |
| 97 | } |
| 98 | |
| 99 | template <class MsgClass, class A, class B, class C, class D> |
| 100 | bool UnpackMessage(const IPC::Message& msg, A* a, B* b, C* c, D* d) { |
mostynb | d349f6d6 | 2015-01-06 00:42:03 | [diff] [blame] | 101 | static_assert( |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 102 | (internal::TupleTypeMatch4<typename MsgClass::Param, A, B, C, D>::kValue), |
mostynb | d349f6d6 | 2015-01-06 00:42:03 | [diff] [blame] | 103 | "tuple types should match"); |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 104 | |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 105 | base::PickleIterator iter(msg); |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 106 | return IPC::ReadParam(&msg, &iter, a) && |
| 107 | IPC::ReadParam(&msg, &iter, b) && |
| 108 | IPC::ReadParam(&msg, &iter, c) && |
| 109 | IPC::ReadParam(&msg, &iter, d); |
| 110 | } |
| 111 | |
| 112 | template <class MsgClass, class A, class B, class C, class D, class E> |
| 113 | bool UnpackMessage(const IPC::Message& msg, A* a, B* b, C* c, D* d, E* e) { |
mostynb | d349f6d6 | 2015-01-06 00:42:03 | [diff] [blame] | 114 | static_assert( |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 115 | (internal::TupleTypeMatch5< |
| 116 | typename MsgClass::Param, A, B, C, D, E>::kValue), |
mostynb | d349f6d6 | 2015-01-06 00:42:03 | [diff] [blame] | 117 | "tuple types should match"); |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 118 | |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 119 | base::PickleIterator iter(msg); |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 120 | return IPC::ReadParam(&msg, &iter, a) && |
| 121 | IPC::ReadParam(&msg, &iter, b) && |
| 122 | IPC::ReadParam(&msg, &iter, c) && |
| 123 | IPC::ReadParam(&msg, &iter, d) && |
| 124 | IPC::ReadParam(&msg, &iter, e); |
| 125 | } |
| 126 | |
| 127 | } // namespace ppapi |
| 128 | |
| 129 | #endif // PPAPI_PROXY_PPAPI_MESSAGE_UTILS_H_ |