Home | History | Annotate | Download | only in proxy
      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 
      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"
     13 
     14 namespace ppapi {
     15 
     16 namespace internal {
     17 
     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;
     24 };
     25 template <class A>
     26 struct TupleTypeMatch1<Tuple1<A>, A> {
     27   static const bool kValue = true;
     28 };
     29 
     30 template <class TupleType, class A, class B>
     31 struct TupleTypeMatch2 {
     32   static const bool kValue = false;
     33 };
     34 template <class A, class B>
     35 struct TupleTypeMatch2<Tuple2<A, B>, A, B> {
     36   static const bool kValue = true;
     37 };
     38 
     39 template <class TupleType, class A, class B, class C>
     40 struct TupleTypeMatch3 {
     41   static const bool kValue = false;
     42 };
     43 template <class A, class B, class C>
     44 struct TupleTypeMatch3<Tuple3<A, B, C>, A, B, C> {
     45   static const bool kValue = true;
     46 };
     47 
     48 template <class TupleType, class A, class B, class C, class D>
     49 struct TupleTypeMatch4 {
     50   static const bool kValue = false;
     51 };
     52 template <class A, class B, class C, class D>
     53 struct TupleTypeMatch4<Tuple4<A, B, C, D>, A, B, C, D> {
     54   static const bool kValue = true;
     55 };
     56 
     57 template <class TupleType, class A, class B, class C, class D, class E>
     58 struct TupleTypeMatch5 {
     59   static const bool kValue = false;
     60 };
     61 template <class A, class B, class C, class D, class E>
     62 struct TupleTypeMatch5<Tuple5<A, B, C, D, E>, A, B, C, D, E> {
     63   static const bool kValue = true;
     64 };
     65 
     66 }  // namespace internal
     67 
     68 template <class MsgClass, class A>
     69 bool UnpackMessage(const IPC::Message& msg, A* a) {
     70   COMPILE_ASSERT(
     71       (internal::TupleTypeMatch1<typename MsgClass::Param, A>::kValue),
     72       tuple_types_dont_match);
     73 
     74   PickleIterator iter(msg);
     75   return IPC::ReadParam(&msg, &iter, a);
     76 }
     77 
     78 template <class MsgClass, class A, class B>
     79 bool UnpackMessage(const IPC::Message& msg, A* a, B* b) {
     80   COMPILE_ASSERT(
     81       (internal::TupleTypeMatch2<typename MsgClass::Param, A, B>::kValue),
     82       tuple_types_dont_match);
     83 
     84   PickleIterator iter(msg);
     85   return IPC::ReadParam(&msg, &iter, a) && IPC::ReadParam(&msg, &iter, b);
     86 }
     87 
     88 template <class MsgClass, class A, class B, class C>
     89 bool UnpackMessage(const IPC::Message& msg, A* a, B* b, C* c) {
     90   COMPILE_ASSERT(
     91       (internal::TupleTypeMatch3<typename MsgClass::Param, A, B, C>::kValue),
     92       tuple_types_dont_match);
     93 
     94   PickleIterator iter(msg);
     95   return IPC::ReadParam(&msg, &iter, a) &&
     96          IPC::ReadParam(&msg, &iter, b) &&
     97          IPC::ReadParam(&msg, &iter, c);
     98 }
     99 
    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) {
    102   COMPILE_ASSERT(
    103       (internal::TupleTypeMatch4<typename MsgClass::Param, A, B, C, D>::kValue),
    104       tuple_types_dont_match);
    105 
    106   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);
    111 }
    112 
    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) {
    115   COMPILE_ASSERT(
    116       (internal::TupleTypeMatch5<
    117            typename MsgClass::Param, A, B, C, D, E>::kValue),
    118       tuple_types_dont_match);
    119 
    120   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);
    126 }
    127 
    128 }  // namespace ppapi
    129 
    130 #endif  // PPAPI_PROXY_PPAPI_MESSAGE_UTILS_H_
    131 
    132