blob: 4bfe23cbf38ad7028e6b15ba8decc9cadac4630e [file] [log] [blame]
[email protected]c2932f5e2010-11-03 03:22:331// Copyright (c) 2010 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#include "ppapi/proxy/ppapi_param_traits.h"
6
7#include <string.h> // For memcpy
8
9#include "ppapi/proxy/serialized_var.h"
10
11namespace IPC {
12
13// static
14void ParamTraits<PP_InputEvent>::Write(Message* m, const param_type& p) {
15 // PP_InputEvent is just POD so we can just memcpy it.
16 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(PP_InputEvent));
17}
18
19// static
20bool ParamTraits<PP_InputEvent>::Read(const Message* m,
21 void** iter,
22 param_type* r) {
23 const char* data;
24 int data_size;
25 if (!m->ReadData(iter, &data, &data_size))
26 return false;
27 memcpy(r, data, sizeof(PP_InputEvent));
28 return true;
29}
30
31// static
32void ParamTraits<PP_InputEvent>::Log(const param_type& p, std::string* l) {
33}
34
35// PP_ObjectProperty -----------------------------------------------------------
36
37// static
38void ParamTraits<PP_ObjectProperty>::Write(Message* m, const param_type& p) {
39 // FIXME(brettw);
40}
41
42// static
43bool ParamTraits<PP_ObjectProperty>::Read(const Message* m,
44 void** iter,
45 param_type* r) {
46 // FIXME(brettw);
47 return true;
48}
49
50// static
51void ParamTraits<PP_ObjectProperty>::Log(const param_type& p, std::string* l) {
52}
53
54// PP_Point --------------------------------------------------------------------
55
56// static
57void ParamTraits<PP_Point>::Write(Message* m, const param_type& p) {
58 m->WriteInt(p.x);
59 m->WriteInt(p.y);
60}
61
62// static
63bool ParamTraits<PP_Point>::Read(const Message* m, void** iter, param_type* r) {
64 return m->ReadInt(iter, &r->x) && !m->ReadInt(iter, &r->y);
65}
66
67// static
68void ParamTraits<PP_Point>::Log(const param_type& p, std::string* l) {
69}
70
71// PP_Rect ---------------------------------------------------------------------
72
73// static
74void ParamTraits<PP_Rect>::Write(Message* m, const param_type& p) {
75 m->WriteInt(p.point.x);
76 m->WriteInt(p.point.y);
77 m->WriteInt(p.size.width);
78 m->WriteInt(p.size.height);
79}
80
81// static
82bool ParamTraits<PP_Rect>::Read(const Message* m,
83 void** iter,
84 param_type* r) {
85 if (!m->ReadInt(iter, &r->point.x) ||
86 !m->ReadInt(iter, &r->point.y) ||
87 !m->ReadInt(iter, &r->size.width) ||
88 !m->ReadInt(iter, &r->size.height))
89 return false;
90 return true;
91}
92
93// static
94void ParamTraits<PP_Rect>::Log(const param_type& p, std::string* l) {
95}
96
97// PP_Size ---------------------------------------------------------------------
98
99// static
100void ParamTraits<PP_Size>::Write(Message* m, const param_type& p) {
101 m->WriteInt(p.width);
102 m->WriteInt(p.height);
103}
104
105// static
106bool ParamTraits<PP_Size>::Read(const Message* m, void** iter, param_type* r) {
107 return m->ReadInt(iter, &r->width) && m->ReadInt(iter, &r->height);
108}
109
110// static
111void ParamTraits<PP_Size>::Log(const param_type& p, std::string* l) {
112}
113
114// SerializedVar ---------------------------------------------------------------
115
116// static
117void ParamTraits<pp::proxy::SerializedVar>::Write(Message* m,
118 const param_type& p) {
119 p.WriteToMessage(m);
120}
121
122// static
123bool ParamTraits<pp::proxy::SerializedVar>::Read(const Message* m,
124 void** iter,
125 param_type* r) {
126 return r->ReadFromMessage(m, iter);
127}
128
129// static
130void ParamTraits<pp::proxy::SerializedVar>::Log(const param_type& p,
131 std::string* l) {
132}
133
134// std::vector<SerializedVar> --------------------------------------------------
135
136void ParamTraits< std::vector<pp::proxy::SerializedVar> >::Write(
137 Message* m,
138 const param_type& p) {
139 WriteParam(m, static_cast<int>(p.size()));
140 for (size_t i = 0; i < p.size(); i++)
141 WriteParam(m, p[i]);
142}
143
144// static
145bool ParamTraits< std::vector<pp::proxy::SerializedVar> >::Read(
146 const Message* m,
147 void** iter,
148 param_type* r) {
149 // This part is just a copy of the the default ParamTraits vector Read().
150 int size;
151 // ReadLength() checks for < 0 itself.
152 if (!m->ReadLength(iter, &size))
153 return false;
154 // Resizing beforehand is not safe, see BUG 1006367 for details.
155 if (INT_MAX / sizeof(pp::proxy::SerializedVar) <= static_cast<size_t>(size))
156 return false;
157
158 // The default vector deserializer does resize here and then we deserialize
159 // into those allocated slots. However, the implementation of vector (at
160 // least in GCC's implementation), creates a new empty object using the
161 // default constructor, and then sets the rest of the items to that empty
162 // one using the copy constructor.
163 //
164 // Since we allocate the inner class when you call the default constructor
165 // and transfer the inner class when you do operator=, the entire vector
166 // will end up referring to the same inner class. Deserializing into this
167 // will just end up overwriting the same item over and over, since all the
168 // SerializedVars will refer to the same thing.
169 //
170 // The solution is to make a new SerializedVar for each deserialized item,
171 // and then add it to the vector one at a time. Our copies are efficient so
172 // this is no big deal.
173 r->reserve(size);
174 for (int i = 0; i < size; i++) {
175 pp::proxy::SerializedVar var;
176 if (!ReadParam(m, iter, &var))
177 return false;
178 r->push_back(var);
179 }
180 return true;
181}
182
183// static
184void ParamTraits< std::vector<pp::proxy::SerializedVar> >::Log(
185 const param_type& p,
186 std::string* l) {
187}
188
189
190} // namespace IPC