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.
5 #ifndef PPAPI_PROXY_PPAPI_MESSAGE_UTILS_H_
6 #define PPAPI_PROXY_PPAPI_MESSAGE_UTILS_H_
8 #include "base/basictypes.h"
9 #include "base/pickle.h"
10 #include "base/tuple.h"
11 #include "ipc/ipc_message.h"
12 #include "ipc/ipc_message_utils.h"
18 // TupleTypeMatch* check whether a tuple type contains elements of the specified
19 // types. They are used to make sure the output parameters of UnpackMessage()
20 // match the corresponding message type.
21 template <class TupleType
, class A
>
22 struct TupleTypeMatch1
{
23 static const bool kValue
= false;
26 struct TupleTypeMatch1
<base::Tuple
<A
>, A
> {
27 static const bool kValue
= true;
30 template <class TupleType
, class A
, class B
>
31 struct TupleTypeMatch2
{
32 static const bool kValue
= false;
34 template <class A
, class B
>
35 struct TupleTypeMatch2
<base::Tuple
<A
, B
>, A
, B
> {
36 static const bool kValue
= true;
39 template <class TupleType
, class A
, class B
, class C
>
40 struct TupleTypeMatch3
{
41 static const bool kValue
= false;
43 template <class A
, class B
, class C
>
44 struct TupleTypeMatch3
<base::Tuple
<A
, B
, C
>, A
, B
, C
> {
45 static const bool kValue
= true;
48 template <class TupleType
, class A
, class B
, class C
, class D
>
49 struct TupleTypeMatch4
{
50 static const bool kValue
= false;
52 template <class A
, class B
, class C
, class D
>
53 struct TupleTypeMatch4
<base::Tuple
<A
, B
, C
, D
>, A
, B
, C
, D
> {
54 static const bool kValue
= true;
57 template <class TupleType
, class A
, class B
, class C
, class D
, class E
>
58 struct TupleTypeMatch5
{
59 static const bool kValue
= false;
61 template <class A
, class B
, class C
, class D
, class E
>
62 struct TupleTypeMatch5
<base::Tuple
<A
, B
, C
, D
, E
>, A
, B
, C
, D
, E
> {
63 static const bool kValue
= true;
66 } // namespace internal
68 template <class MsgClass
, class A
>
69 bool UnpackMessage(const IPC::Message
& msg
, A
* a
) {
71 (internal::TupleTypeMatch1
<typename
MsgClass::Param
, A
>::kValue
),
72 "tuple types should match");
74 base::PickleIterator
iter(msg
);
75 return IPC::ReadParam(&msg
, &iter
, a
);
78 template <class MsgClass
, class A
, class B
>
79 bool UnpackMessage(const IPC::Message
& msg
, A
* a
, B
* b
) {
81 (internal::TupleTypeMatch2
<typename
MsgClass::Param
, A
, B
>::kValue
),
82 "tuple types should match");
84 base::PickleIterator
iter(msg
);
85 return IPC::ReadParam(&msg
, &iter
, a
) && IPC::ReadParam(&msg
, &iter
, b
);
88 template <class MsgClass
, class A
, class B
, class C
>
89 bool UnpackMessage(const IPC::Message
& msg
, A
* a
, B
* b
, C
* c
) {
91 (internal::TupleTypeMatch3
<typename
MsgClass::Param
, A
, B
, C
>::kValue
),
92 "tuple types should match");
94 base::PickleIterator
iter(msg
);
95 return IPC::ReadParam(&msg
, &iter
, a
) &&
96 IPC::ReadParam(&msg
, &iter
, b
) &&
97 IPC::ReadParam(&msg
, &iter
, c
);
100 template <class MsgClass
, class A
, class B
, class C
, class D
>
101 bool UnpackMessage(const IPC::Message
& msg
, A
* a
, B
* b
, C
* c
, D
* d
) {
103 (internal::TupleTypeMatch4
<typename
MsgClass::Param
, A
, B
, C
, D
>::kValue
),
104 "tuple types should match");
106 base::PickleIterator
iter(msg
);
107 return IPC::ReadParam(&msg
, &iter
, a
) &&
108 IPC::ReadParam(&msg
, &iter
, b
) &&
109 IPC::ReadParam(&msg
, &iter
, c
) &&
110 IPC::ReadParam(&msg
, &iter
, d
);
113 template <class MsgClass
, class A
, class B
, class C
, class D
, class E
>
114 bool UnpackMessage(const IPC::Message
& msg
, A
* a
, B
* b
, C
* c
, D
* d
, E
* e
) {
116 (internal::TupleTypeMatch5
<
117 typename
MsgClass::Param
, A
, B
, C
, D
, E
>::kValue
),
118 "tuple types should match");
120 base::PickleIterator
iter(msg
);
121 return IPC::ReadParam(&msg
, &iter
, a
) &&
122 IPC::ReadParam(&msg
, &iter
, b
) &&
123 IPC::ReadParam(&msg
, &iter
, c
) &&
124 IPC::ReadParam(&msg
, &iter
, d
) &&
125 IPC::ReadParam(&msg
, &iter
, e
);
130 #endif // PPAPI_PROXY_PPAPI_MESSAGE_UTILS_H_