OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/public/common/common_param_traits.h" | 5 #include "content/public/common/common_param_traits.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "content/public/common/content_constants.h" | 9 #include "content/public/common/content_constants.h" |
10 #include "content/public/common/page_state.h" | 10 #include "content/public/common/page_state.h" |
11 #include "content/public/common/referrer.h" | 11 #include "content/public/common/referrer.h" |
12 #include "content/public/common/url_utils.h" | 12 #include "content/public/common/url_utils.h" |
13 #include "net/base/host_port_pair.h" | 13 #include "net/base/host_port_pair.h" |
14 #include "net/base/ip_endpoint.h" | 14 #include "net/base/ip_endpoint.h" |
15 #include "third_party/skia/include/core/SkBitmap.h" | |
16 #include "ui/gfx/rect.h" | |
17 #include "ui/gfx/rect_f.h" | |
18 | |
19 namespace { | |
20 | |
21 struct SkBitmap_Data { | |
22 // The configuration for the bitmap (bits per pixel, etc). | |
23 SkBitmap::Config fConfig; | |
24 | |
25 // The width of the bitmap in pixels. | |
26 uint32 fWidth; | |
27 | |
28 // The height of the bitmap in pixels. | |
29 uint32 fHeight; | |
30 | |
31 void InitSkBitmapDataForTransfer(const SkBitmap& bitmap) { | |
32 fConfig = bitmap.config(); | |
33 fWidth = bitmap.width(); | |
34 fHeight = bitmap.height(); | |
35 } | |
36 | |
37 // Returns whether |bitmap| successfully initialized. | |
38 bool InitSkBitmapFromData(SkBitmap* bitmap, const char* pixels, | |
39 size_t total_pixels) const { | |
40 if (total_pixels) { | |
41 bitmap->setConfig(fConfig, fWidth, fHeight, 0); | |
42 if (!bitmap->allocPixels()) | |
43 return false; | |
44 if (total_pixels != bitmap->getSize()) | |
45 return false; | |
46 memcpy(bitmap->getPixels(), pixels, total_pixels); | |
47 } | |
48 return true; | |
49 } | |
50 }; | |
51 | |
52 } // namespace | |
53 | 15 |
54 namespace IPC { | 16 namespace IPC { |
55 | 17 |
56 void ParamTraits<GURL>::Write(Message* m, const GURL& p) { | 18 void ParamTraits<GURL>::Write(Message* m, const GURL& p) { |
57 DCHECK(p.possibly_invalid_spec().length() <= content::GetMaxURLChars()); | 19 DCHECK(p.possibly_invalid_spec().length() <= content::GetMaxURLChars()); |
58 | 20 |
59 // Beware of print-parse inconsistency which would change an invalid | 21 // Beware of print-parse inconsistency which would change an invalid |
60 // URL into a valid one. Ideally, the message would contain this flag | 22 // URL into a valid one. Ideally, the message would contain this flag |
61 // so that the read side could make the check, but performing it here | 23 // so that the read side could make the check, but performing it here |
62 // avoids changing the on-the-wire representation of such a fundamental | 24 // avoids changing the on-the-wire representation of such a fundamental |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 return true; | 132 return true; |
171 } | 133 } |
172 | 134 |
173 void ParamTraits<content::PageState>::Log( | 135 void ParamTraits<content::PageState>::Log( |
174 const param_type& p, std::string* l) { | 136 const param_type& p, std::string* l) { |
175 l->append("("); | 137 l->append("("); |
176 LogParam(p.ToEncodedData(), l); | 138 LogParam(p.ToEncodedData(), l); |
177 l->append(")"); | 139 l->append(")"); |
178 } | 140 } |
179 | 141 |
180 void ParamTraits<gfx::Point>::Write(Message* m, const gfx::Point& p) { | |
181 m->WriteInt(p.x()); | |
182 m->WriteInt(p.y()); | |
183 } | |
184 | |
185 bool ParamTraits<gfx::Point>::Read(const Message* m, PickleIterator* iter, | |
186 gfx::Point* r) { | |
187 int x, y; | |
188 if (!m->ReadInt(iter, &x) || | |
189 !m->ReadInt(iter, &y)) | |
190 return false; | |
191 r->set_x(x); | |
192 r->set_y(y); | |
193 return true; | |
194 } | |
195 | |
196 void ParamTraits<gfx::Point>::Log(const gfx::Point& p, std::string* l) { | |
197 l->append(base::StringPrintf("(%d, %d)", p.x(), p.y())); | |
198 } | |
199 | |
200 void ParamTraits<gfx::PointF>::Write(Message* m, const gfx::PointF& v) { | |
201 ParamTraits<float>::Write(m, v.x()); | |
202 ParamTraits<float>::Write(m, v.y()); | |
203 } | |
204 | |
205 bool ParamTraits<gfx::PointF>::Read(const Message* m, | |
206 PickleIterator* iter, | |
207 gfx::PointF* r) { | |
208 float x, y; | |
209 if (!ParamTraits<float>::Read(m, iter, &x) || | |
210 !ParamTraits<float>::Read(m, iter, &y)) | |
211 return false; | |
212 r->set_x(x); | |
213 r->set_y(y); | |
214 return true; | |
215 } | |
216 | |
217 void ParamTraits<gfx::PointF>::Log(const gfx::PointF& v, std::string* l) { | |
218 l->append(base::StringPrintf("(%f, %f)", v.x(), v.y())); | |
219 } | |
220 | |
221 void ParamTraits<gfx::Size>::Write(Message* m, const gfx::Size& p) { | |
222 DCHECK_GE(p.width(), 0); | |
223 DCHECK_GE(p.height(), 0); | |
224 int values[2] = { p.width(), p.height() }; | |
225 m->WriteBytes(&values, sizeof(int) * 2); | |
226 } | |
227 | |
228 bool ParamTraits<gfx::Size>::Read(const Message* m, | |
229 PickleIterator* iter, | |
230 gfx::Size* r) { | |
231 const char* char_values; | |
232 if (!m->ReadBytes(iter, &char_values, sizeof(int) * 2)) | |
233 return false; | |
234 const int* values = reinterpret_cast<const int*>(char_values); | |
235 if (values[0] < 0 || values[1] < 0) | |
236 return false; | |
237 r->set_width(values[0]); | |
238 r->set_height(values[1]); | |
239 return true; | |
240 } | |
241 | |
242 void ParamTraits<gfx::Size>::Log(const gfx::Size& p, std::string* l) { | |
243 l->append(base::StringPrintf("(%d, %d)", p.width(), p.height())); | |
244 } | |
245 | |
246 void ParamTraits<gfx::SizeF>::Write(Message* m, const gfx::SizeF& p) { | |
247 float values[2] = { p.width(), p.height() }; | |
248 m->WriteBytes(&values, sizeof(float) * 2); | |
249 } | |
250 | |
251 bool ParamTraits<gfx::SizeF>::Read(const Message* m, | |
252 PickleIterator* iter, | |
253 gfx::SizeF* r) { | |
254 const char* char_values; | |
255 if (!m->ReadBytes(iter, &char_values, sizeof(float) * 2)) | |
256 return false; | |
257 const float* values = reinterpret_cast<const float*>(char_values); | |
258 r->set_width(values[0]); | |
259 r->set_height(values[1]); | |
260 return true; | |
261 } | |
262 | |
263 void ParamTraits<gfx::SizeF>::Log(const gfx::SizeF& p, std::string* l) { | |
264 l->append(base::StringPrintf("(%f, %f)", p.width(), p.height())); | |
265 } | |
266 | |
267 void ParamTraits<gfx::Vector2d>::Write(Message* m, const gfx::Vector2d& p) { | |
268 int values[2] = { p.x(), p.y() }; | |
269 m->WriteBytes(&values, sizeof(int) * 2); | |
270 } | |
271 | |
272 bool ParamTraits<gfx::Vector2d>::Read(const Message* m, | |
273 PickleIterator* iter, | |
274 gfx::Vector2d* r) { | |
275 const char* char_values; | |
276 if (!m->ReadBytes(iter, &char_values, sizeof(int) * 2)) | |
277 return false; | |
278 const int* values = reinterpret_cast<const int*>(char_values); | |
279 r->set_x(values[0]); | |
280 r->set_y(values[1]); | |
281 return true; | |
282 } | |
283 | |
284 void ParamTraits<gfx::Vector2d>::Log(const gfx::Vector2d& v, std::string* l) { | |
285 l->append(base::StringPrintf("(%d, %d)", v.x(), v.y())); | |
286 } | |
287 | |
288 void ParamTraits<gfx::Vector2dF>::Write(Message* m, const gfx::Vector2dF& p) { | |
289 float values[2] = { p.x(), p.y() }; | |
290 m->WriteBytes(&values, sizeof(float) * 2); | |
291 } | |
292 | |
293 bool ParamTraits<gfx::Vector2dF>::Read(const Message* m, | |
294 PickleIterator* iter, | |
295 gfx::Vector2dF* r) { | |
296 const char* char_values; | |
297 if (!m->ReadBytes(iter, &char_values, sizeof(float) * 2)) | |
298 return false; | |
299 const float* values = reinterpret_cast<const float*>(char_values); | |
300 r->set_x(values[0]); | |
301 r->set_y(values[1]); | |
302 return true; | |
303 } | |
304 | |
305 void ParamTraits<gfx::Vector2dF>::Log(const gfx::Vector2dF& v, std::string* l) { | |
306 l->append(base::StringPrintf("(%f, %f)", v.x(), v.y())); | |
307 } | |
308 | |
309 void ParamTraits<gfx::Rect>::Write(Message* m, const gfx::Rect& p) { | |
310 int values[4] = { p.x(), p.y(), p.width(), p.height() }; | |
311 m->WriteBytes(&values, sizeof(int) * 4); | |
312 } | |
313 | |
314 bool ParamTraits<gfx::Rect>::Read(const Message* m, | |
315 PickleIterator* iter, | |
316 gfx::Rect* r) { | |
317 const char* char_values; | |
318 if (!m->ReadBytes(iter, &char_values, sizeof(int) * 4)) | |
319 return false; | |
320 const int* values = reinterpret_cast<const int*>(char_values); | |
321 if (values[2] < 0 || values[3] < 0) | |
322 return false; | |
323 r->SetRect(values[0], values[1], values[2], values[3]); | |
324 return true; | |
325 } | |
326 | |
327 void ParamTraits<gfx::Rect>::Log(const gfx::Rect& p, std::string* l) { | |
328 l->append(base::StringPrintf("(%d, %d, %d, %d)", p.x(), p.y(), | |
329 p.width(), p.height())); | |
330 } | |
331 | |
332 void ParamTraits<gfx::RectF>::Write(Message* m, const gfx::RectF& p) { | |
333 float values[4] = { p.x(), p.y(), p.width(), p.height() }; | |
334 m->WriteBytes(&values, sizeof(float) * 4); | |
335 } | |
336 | |
337 bool ParamTraits<gfx::RectF>::Read(const Message* m, | |
338 PickleIterator* iter, | |
339 gfx::RectF* r) { | |
340 const char* char_values; | |
341 if (!m->ReadBytes(iter, &char_values, sizeof(float) * 4)) | |
342 return false; | |
343 const float* values = reinterpret_cast<const float*>(char_values); | |
344 r->SetRect(values[0], values[1], values[2], values[3]); | |
345 return true; | |
346 } | |
347 | |
348 void ParamTraits<gfx::RectF>::Log(const gfx::RectF& p, std::string* l) { | |
349 l->append(base::StringPrintf("(%f, %f, %f, %f)", p.x(), p.y(), | |
350 p.width(), p.height())); | |
351 } | |
352 | |
353 void ParamTraits<SkBitmap>::Write(Message* m, const SkBitmap& p) { | |
354 size_t fixed_size = sizeof(SkBitmap_Data); | |
355 SkBitmap_Data bmp_data; | |
356 bmp_data.InitSkBitmapDataForTransfer(p); | |
357 m->WriteData(reinterpret_cast<const char*>(&bmp_data), | |
358 static_cast<int>(fixed_size)); | |
359 size_t pixel_size = p.getSize(); | |
360 SkAutoLockPixels p_lock(p); | |
361 m->WriteData(reinterpret_cast<const char*>(p.getPixels()), | |
362 static_cast<int>(pixel_size)); | |
363 } | |
364 | |
365 bool ParamTraits<SkBitmap>::Read(const Message* m, | |
366 PickleIterator* iter, | |
367 SkBitmap* r) { | |
368 const char* fixed_data; | |
369 int fixed_data_size = 0; | |
370 if (!m->ReadData(iter, &fixed_data, &fixed_data_size) || | |
371 (fixed_data_size <= 0)) { | |
372 NOTREACHED(); | |
373 return false; | |
374 } | |
375 if (fixed_data_size != sizeof(SkBitmap_Data)) | |
376 return false; // Message is malformed. | |
377 | |
378 const char* variable_data; | |
379 int variable_data_size = 0; | |
380 if (!m->ReadData(iter, &variable_data, &variable_data_size) || | |
381 (variable_data_size < 0)) { | |
382 NOTREACHED(); | |
383 return false; | |
384 } | |
385 const SkBitmap_Data* bmp_data = | |
386 reinterpret_cast<const SkBitmap_Data*>(fixed_data); | |
387 return bmp_data->InitSkBitmapFromData(r, variable_data, variable_data_size); | |
388 } | |
389 | |
390 void ParamTraits<SkBitmap>::Log(const SkBitmap& p, std::string* l) { | |
391 l->append("<SkBitmap>"); | |
392 } | |
393 | |
394 } // namespace IPC | 142 } // namespace IPC |
395 | 143 |
396 // Generate param traits write methods. | 144 // Generate param traits write methods. |
397 #include "ipc/param_traits_write_macros.h" | 145 #include "ipc/param_traits_write_macros.h" |
398 namespace IPC { | 146 namespace IPC { |
399 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_ | 147 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_ |
400 #include "content/public/common/common_param_traits_macros.h" | 148 #include "content/public/common/common_param_traits_macros.h" |
401 } // namespace IPC | 149 } // namespace IPC |
402 | 150 |
403 // Generate param traits read methods. | 151 // Generate param traits read methods. |
404 #include "ipc/param_traits_read_macros.h" | 152 #include "ipc/param_traits_read_macros.h" |
405 namespace IPC { | 153 namespace IPC { |
406 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_ | 154 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_ |
407 #include "content/public/common/common_param_traits_macros.h" | 155 #include "content/public/common/common_param_traits_macros.h" |
408 } // namespace IPC | 156 } // namespace IPC |
409 | 157 |
410 // Generate param traits log methods. | 158 // Generate param traits log methods. |
411 #include "ipc/param_traits_log_macros.h" | 159 #include "ipc/param_traits_log_macros.h" |
412 namespace IPC { | 160 namespace IPC { |
413 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_ | 161 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_ |
414 #include "content/public/common/common_param_traits_macros.h" | 162 #include "content/public/common/common_param_traits_macros.h" |
415 } // namespace IPC | 163 } // namespace IPC |
OLD | NEW |