summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/ppapi_param_traits.cc
blob: 4bfe23cbf38ad7028e6b15ba8decc9cadac4630e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/proxy/ppapi_param_traits.h"

#include <string.h>  // For memcpy

#include "ppapi/proxy/serialized_var.h"

namespace IPC {

// static
void ParamTraits<PP_InputEvent>::Write(Message* m, const param_type& p) {
  // PP_InputEvent is just POD so we can just memcpy it.
  m->WriteData(reinterpret_cast<const char*>(&p), sizeof(PP_InputEvent));
}

// static
bool ParamTraits<PP_InputEvent>::Read(const Message* m,
                                      void** iter,
                                      param_type* r) {
  const char* data;
  int data_size;
  if (!m->ReadData(iter, &data, &data_size))
    return false;
  memcpy(r, data, sizeof(PP_InputEvent));
  return true;
}

// static
void ParamTraits<PP_InputEvent>::Log(const param_type& p, std::string* l) {
}

// PP_ObjectProperty -----------------------------------------------------------

// static
void ParamTraits<PP_ObjectProperty>::Write(Message* m, const param_type& p) {
  // FIXME(brettw);
}

// static
bool ParamTraits<PP_ObjectProperty>::Read(const Message* m,
                                          void** iter,
                                          param_type* r) {
  // FIXME(brettw);
  return true;
}

// static
void ParamTraits<PP_ObjectProperty>::Log(const param_type& p, std::string* l) {
}

// PP_Point --------------------------------------------------------------------

// static
void ParamTraits<PP_Point>::Write(Message* m, const param_type& p) {
  m->WriteInt(p.x);
  m->WriteInt(p.y);
}

// static
bool ParamTraits<PP_Point>::Read(const Message* m, void** iter, param_type* r) {
  return m->ReadInt(iter, &r->x) && !m->ReadInt(iter, &r->y);
}

// static
void ParamTraits<PP_Point>::Log(const param_type& p, std::string* l) {
}

// PP_Rect ---------------------------------------------------------------------

// static
void ParamTraits<PP_Rect>::Write(Message* m, const param_type& p) {
  m->WriteInt(p.point.x);
  m->WriteInt(p.point.y);
  m->WriteInt(p.size.width);
  m->WriteInt(p.size.height);
}

// static
bool ParamTraits<PP_Rect>::Read(const Message* m,
                                void** iter,
                                param_type* r) {
  if (!m->ReadInt(iter, &r->point.x) ||
      !m->ReadInt(iter, &r->point.y) ||
      !m->ReadInt(iter, &r->size.width) ||
      !m->ReadInt(iter, &r->size.height))
    return false;
  return true;
}

// static
void ParamTraits<PP_Rect>::Log(const param_type& p, std::string* l) {
}

// PP_Size ---------------------------------------------------------------------

// static
void ParamTraits<PP_Size>::Write(Message* m, const param_type& p) {
  m->WriteInt(p.width);
  m->WriteInt(p.height);
}

// static
bool ParamTraits<PP_Size>::Read(const Message* m, void** iter, param_type* r) {
  return m->ReadInt(iter, &r->width) && m->ReadInt(iter, &r->height);
}

// static
void ParamTraits<PP_Size>::Log(const param_type& p, std::string* l) {
}

// SerializedVar ---------------------------------------------------------------

// static
void ParamTraits<pp::proxy::SerializedVar>::Write(Message* m,
                                                  const param_type& p) {
  p.WriteToMessage(m);
}

// static
bool ParamTraits<pp::proxy::SerializedVar>::Read(const Message* m,
                                                 void** iter,
                                                 param_type* r) {
  return r->ReadFromMessage(m, iter);
}

// static
void ParamTraits<pp::proxy::SerializedVar>::Log(const param_type& p,
                                                std::string* l) {
}

// std::vector<SerializedVar> --------------------------------------------------

void ParamTraits< std::vector<pp::proxy::SerializedVar> >::Write(
    Message* m,
    const param_type& p) {
  WriteParam(m, static_cast<int>(p.size()));
  for (size_t i = 0; i < p.size(); i++)
    WriteParam(m, p[i]);
}

// static
bool ParamTraits< std::vector<pp::proxy::SerializedVar> >::Read(
    const Message* m,
    void** iter,
    param_type* r) {
  // This part is just a copy of the the default ParamTraits vector Read().
  int size;
  // ReadLength() checks for < 0 itself.
  if (!m->ReadLength(iter, &size))
    return false;
  // Resizing beforehand is not safe, see BUG 1006367 for details.
  if (INT_MAX / sizeof(pp::proxy::SerializedVar) <= static_cast<size_t>(size))
    return false;

  // The default vector deserializer does resize here and then we deserialize
  // into those allocated slots. However, the implementation of vector (at
  // least in GCC's implementation), creates a new empty object using the
  // default constructor, and then sets the rest of the items to that empty
  // one using the copy constructor.
  //
  // Since we allocate the inner class when you call the default constructor
  // and transfer the inner class when you do operator=, the entire vector
  // will end up referring to the same inner class. Deserializing into this
  // will just end up overwriting the same item over and over, since all the
  // SerializedVars will refer to the same thing.
  //
  // The solution is to make a new SerializedVar for each deserialized item,
  // and then add it to the vector one at a time. Our copies are efficient so
  // this is no big deal.
  r->reserve(size);
  for (int i = 0; i < size; i++) {
    pp::proxy::SerializedVar var;
    if (!ReadParam(m, iter, &var))
      return false;
    r->push_back(var);
  }
  return true;
}

// static
void ParamTraits< std::vector<pp::proxy::SerializedVar> >::Log(
    const param_type& p,
    std::string* l) {
}


}  // namespace IPC