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