Home | History | Annotate | Download | only in common
      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 // This file is used to define IPC::ParamTraits<> specializations for a number
      6 // of types so that they can be serialized over IPC.  IPC::ParamTraits<>
      7 // specializations for basic types (like int and std::string) and types in the
      8 // 'base' project can be found in ipc/ipc_message_utils.h.  This file contains
      9 // specializations for types that are used by the content code, and which need
     10 // manual serialization code.  This is usually because they're not structs with
     11 // public members, or because the same type is being used in multiple
     12 // *_messages.h headers.
     13 
     14 #ifndef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_H_
     15 #define CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_H_
     16 
     17 #include "base/memory/ref_counted.h"
     18 #include "content/common/content_export.h"
     19 #include "content/public/common/common_param_traits_macros.h"
     20 #include "ipc/ipc_message_utils.h"
     21 #include "ui/gfx/native_widget_types.h"
     22 #include "ui/surface/transport_dib.h"
     23 #include "url/gurl.h"
     24 
     25 class SkBitmap;
     26 
     27 namespace content {
     28 class PageState;
     29 struct Referrer;
     30 }
     31 
     32 namespace gfx {
     33 class Point;
     34 class Rect;
     35 class RectF;
     36 class Size;
     37 class Vector2d;
     38 }  // namespace gfx
     39 
     40 namespace net {
     41 class HostPortPair;
     42 }
     43 
     44 namespace IPC {
     45 
     46 template <>
     47 struct CONTENT_EXPORT ParamTraits<GURL> {
     48   typedef GURL param_type;
     49   static void Write(Message* m, const param_type& p);
     50   static bool Read(const Message* m, PickleIterator* iter, param_type* p);
     51   static void Log(const param_type& p, std::string* l);
     52 };
     53 
     54 template<>
     55 struct CONTENT_EXPORT ParamTraits<net::HostPortPair> {
     56   typedef net::HostPortPair param_type;
     57   static void Write(Message* m, const param_type& p);
     58   static bool Read(const Message* m, PickleIterator* iter, param_type* r);
     59   static void Log(const param_type& p, std::string* l);
     60 };
     61 
     62 template <>
     63 struct CONTENT_EXPORT ParamTraits<content::PageState> {
     64   typedef content::PageState param_type;
     65   static void Write(Message* m, const param_type& p);
     66   static bool Read(const Message* m, PickleIterator* iter, param_type* p);
     67   static void Log(const param_type& p, std::string* l);
     68 };
     69 
     70 template <>
     71 struct CONTENT_EXPORT ParamTraits<content::Referrer> {
     72   typedef content::Referrer param_type;
     73   static void Write(Message* m, const param_type& p);
     74   static bool Read(const Message* m, PickleIterator* iter, param_type* p);
     75   static void Log(const param_type& p, std::string* l);
     76 };
     77 
     78 template <>
     79 struct CONTENT_EXPORT ParamTraits<gfx::Point> {
     80   typedef gfx::Point param_type;
     81   static void Write(Message* m, const param_type& p);
     82   static bool Read(const Message* m, PickleIterator* iter, param_type* r);
     83   static void Log(const param_type& p, std::string* l);
     84 };
     85 
     86 template <>
     87 struct CONTENT_EXPORT ParamTraits<gfx::PointF> {
     88   typedef gfx::PointF param_type;
     89   static void Write(Message* m, const param_type& p);
     90   static bool Read(const Message* m, PickleIterator* iter, param_type* r);
     91   static void Log(const param_type& p, std::string* l);
     92 };
     93 
     94 template <>
     95 struct CONTENT_EXPORT ParamTraits<gfx::Size> {
     96   typedef gfx::Size param_type;
     97   static void Write(Message* m, const param_type& p);
     98   static bool Read(const Message* m, PickleIterator* iter, param_type* r);
     99   static void Log(const param_type& p, std::string* l);
    100 };
    101 
    102 template <>
    103 struct CONTENT_EXPORT ParamTraits<gfx::SizeF> {
    104   typedef gfx::SizeF param_type;
    105   static void Write(Message* m, const param_type& p);
    106   static bool Read(const Message* m, PickleIterator* iter, param_type* r);
    107   static void Log(const param_type& p, std::string* l);
    108 };
    109 
    110 template <>
    111 struct CONTENT_EXPORT ParamTraits<gfx::Vector2d> {
    112   typedef gfx::Vector2d param_type;
    113   static void Write(Message* m, const param_type& p);
    114   static bool Read(const Message* m, PickleIterator* iter, param_type* r);
    115   static void Log(const param_type& p, std::string* l);
    116 };
    117 
    118 template <>
    119 struct CONTENT_EXPORT ParamTraits<gfx::Vector2dF> {
    120   typedef gfx::Vector2dF param_type;
    121   static void Write(Message* m, const param_type& p);
    122   static bool Read(const Message* m, PickleIterator* iter, param_type* r);
    123   static void Log(const param_type& p, std::string* l);
    124 };
    125 
    126 template <>
    127 struct CONTENT_EXPORT ParamTraits<gfx::Rect> {
    128   typedef gfx::Rect param_type;
    129   static void Write(Message* m, const param_type& p);
    130   static bool Read(const Message* m, PickleIterator* iter, param_type* r);
    131   static void Log(const param_type& p, std::string* l);
    132 };
    133 
    134 template <>
    135 struct CONTENT_EXPORT ParamTraits<gfx::RectF> {
    136   typedef gfx::RectF param_type;
    137   static void Write(Message* m, const param_type& p);
    138   static bool Read(const Message* m, PickleIterator* iter, param_type* r);
    139   static void Log(const param_type& p, std::string* l);
    140 };
    141 
    142 template <>
    143 struct ParamTraits<gfx::NativeWindow> {
    144   typedef gfx::NativeWindow param_type;
    145   static void Write(Message* m, const param_type& p) {
    146 #if defined(OS_WIN)
    147     // HWNDs are always 32 bits on Windows, even on 64 bit systems.
    148     m->WriteUInt32(reinterpret_cast<uint32>(p));
    149 #else
    150     m->WriteData(reinterpret_cast<const char*>(&p), sizeof(p));
    151 #endif
    152   }
    153   static bool Read(const Message* m, PickleIterator* iter, param_type* r) {
    154 #if defined(OS_WIN)
    155     return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r));
    156 #else
    157     const char *data;
    158     int data_size = 0;
    159     bool result = m->ReadData(iter, &data, &data_size);
    160     if (result && data_size == sizeof(gfx::NativeWindow)) {
    161       memcpy(r, data, sizeof(gfx::NativeWindow));
    162     } else {
    163       result = false;
    164       NOTREACHED();
    165     }
    166     return result;
    167 #endif
    168   }
    169   static void Log(const param_type& p, std::string* l) {
    170     l->append("<gfx::NativeWindow>");
    171   }
    172 };
    173 
    174 #if defined(OS_WIN)
    175 template<>
    176 struct ParamTraits<TransportDIB::Id> {
    177   typedef TransportDIB::Id param_type;
    178   static void Write(Message* m, const param_type& p) {
    179     WriteParam(m, p.handle);
    180     WriteParam(m, p.sequence_num);
    181   }
    182   static bool Read(const Message* m, PickleIterator* iter, param_type* r) {
    183     return (ReadParam(m, iter, &r->handle) &&
    184             ReadParam(m, iter, &r->sequence_num));
    185   }
    186   static void Log(const param_type& p, std::string* l) {
    187     l->append("TransportDIB(");
    188     LogParam(p.handle, l);
    189     l->append(", ");
    190     LogParam(p.sequence_num, l);
    191     l->append(")");
    192   }
    193 };
    194 #endif
    195 
    196 #if defined(TOOLKIT_GTK)
    197 template<>
    198 struct ParamTraits<TransportDIB::Id> {
    199   typedef TransportDIB::Id param_type;
    200   static void Write(Message* m, const param_type& p) {
    201     WriteParam(m, p.shmkey);
    202   }
    203   static bool Read(const Message* m, PickleIterator* iter, param_type* r) {
    204     return ReadParam(m, iter, &r->shmkey);
    205   }
    206   static void Log(const param_type& p, std::string* l) {
    207     l->append("TransportDIB(");
    208     LogParam(p.shmkey, l);
    209     l->append(")");
    210   }
    211 };
    212 #endif
    213 
    214 template <>
    215 struct CONTENT_EXPORT ParamTraits<SkBitmap> {
    216   typedef SkBitmap param_type;
    217   static void Write(Message* m, const param_type& p);
    218 
    219   // Note: This function expects parameter |r| to be of type &SkBitmap since
    220   // r->SetConfig() and r->SetPixels() are called.
    221   static bool Read(const Message* m, PickleIterator* iter, param_type* r);
    222 
    223   static void Log(const param_type& p, std::string* l);
    224 };
    225 
    226 }  // namespace IPC
    227 
    228 #endif  // CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_H_
    229