Loosen up heuristics for detecting account creation forms.
[chromium-blink-merge.git] / content / public / common / common_param_traits.cc
blobfae54fb526cd9f2cd6b7eab87d9663b79732e465
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.
5 #include "content/public/common/common_param_traits.h"
7 #include "content/public/common/content_constants.h"
8 #include "content/public/common/referrer.h"
9 #include "net/base/host_port_pair.h"
10 #include "net/base/upload_data.h"
11 #include "net/http/http_response_headers.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "ui/base/range/range.h"
14 #include "ui/gfx/rect.h"
15 #include "ui/gfx/rect_f.h"
16 #include "webkit/glue/resource_request_body.h"
18 namespace {
20 struct SkBitmap_Data {
21 // The configuration for the bitmap (bits per pixel, etc).
22 SkBitmap::Config fConfig;
24 // The width of the bitmap in pixels.
25 uint32 fWidth;
27 // The height of the bitmap in pixels.
28 uint32 fHeight;
30 void InitSkBitmapDataForTransfer(const SkBitmap& bitmap) {
31 fConfig = bitmap.config();
32 fWidth = bitmap.width();
33 fHeight = bitmap.height();
36 // Returns whether |bitmap| successfully initialized.
37 bool InitSkBitmapFromData(SkBitmap* bitmap, const char* pixels,
38 size_t total_pixels) const {
39 if (total_pixels) {
40 bitmap->setConfig(fConfig, fWidth, fHeight, 0);
41 if (!bitmap->allocPixels())
42 return false;
43 if (total_pixels != bitmap->getSize())
44 return false;
45 memcpy(bitmap->getPixels(), pixels, total_pixels);
47 return true;
51 } // namespace
53 namespace IPC {
55 void ParamTraits<GURL>::Write(Message* m, const GURL& p) {
56 DCHECK(p.possibly_invalid_spec().length() <= content::kMaxURLChars);
57 m->WriteString(p.possibly_invalid_spec());
58 // TODO(brettw) bug 684583: Add encoding for query params.
61 bool ParamTraits<GURL>::Read(const Message* m, PickleIterator* iter, GURL* p) {
62 std::string s;
63 if (!m->ReadString(iter, &s) || s.length() > content::kMaxURLChars) {
64 *p = GURL();
65 return false;
67 *p = GURL(s);
68 return true;
71 void ParamTraits<GURL>::Log(const GURL& p, std::string* l) {
72 l->append(p.spec());
75 void ParamTraits<net::URLRequestStatus>::Write(Message* m,
76 const param_type& p) {
77 WriteParam(m, static_cast<int>(p.status()));
78 WriteParam(m, p.error());
81 bool ParamTraits<net::URLRequestStatus>::Read(const Message* m,
82 PickleIterator* iter,
83 param_type* r) {
84 int status, error;
85 if (!ReadParam(m, iter, &status) || !ReadParam(m, iter, &error))
86 return false;
87 r->set_status(static_cast<net::URLRequestStatus::Status>(status));
88 r->set_error(error);
89 return true;
92 void ParamTraits<net::URLRequestStatus>::Log(const param_type& p,
93 std::string* l) {
94 std::string status;
95 switch (p.status()) {
96 case net::URLRequestStatus::SUCCESS:
97 status = "SUCCESS";
98 break;
99 case net::URLRequestStatus::IO_PENDING:
100 status = "IO_PENDING ";
101 break;
102 case net::URLRequestStatus::CANCELED:
103 status = "CANCELED";
104 break;
105 case net::URLRequestStatus::FAILED:
106 status = "FAILED";
107 break;
108 default:
109 status = "UNKNOWN";
110 break;
112 if (p.status() == net::URLRequestStatus::FAILED)
113 l->append("(");
115 LogParam(status, l);
117 if (p.status() == net::URLRequestStatus::FAILED) {
118 l->append(", ");
119 LogParam(p.error(), l);
120 l->append(")");
124 // Only the net::UploadData ParamTraits<> definition needs this definition, so
125 // keep this in the implementation file so we can forward declare UploadData in
126 // the header.
127 template <>
128 struct ParamTraits<net::UploadElement> {
129 typedef net::UploadElement param_type;
130 static void Write(Message* m, const param_type& p) {
131 WriteParam(m, static_cast<int>(p.type()));
132 switch (p.type()) {
133 case net::UploadElement::TYPE_BYTES: {
134 m->WriteData(p.bytes(), static_cast<int>(p.bytes_length()));
135 break;
137 default: {
138 DCHECK(p.type() == net::UploadElement::TYPE_FILE);
139 WriteParam(m, p.file_path());
140 WriteParam(m, p.file_range_offset());
141 WriteParam(m, p.file_range_length());
142 WriteParam(m, p.expected_file_modification_time());
143 break;
147 static bool Read(const Message* m, PickleIterator* iter, param_type* r) {
148 int type;
149 if (!ReadParam(m, iter, &type))
150 return false;
151 switch (type) {
152 case net::UploadElement::TYPE_BYTES: {
153 const char* data;
154 int len;
155 if (!m->ReadData(iter, &data, &len))
156 return false;
157 r->SetToBytes(data, len);
158 break;
160 default: {
161 DCHECK(type == net::UploadElement::TYPE_FILE);
162 FilePath file_path;
163 uint64 offset, length;
164 base::Time expected_modification_time;
165 if (!ReadParam(m, iter, &file_path))
166 return false;
167 if (!ReadParam(m, iter, &offset))
168 return false;
169 if (!ReadParam(m, iter, &length))
170 return false;
171 if (!ReadParam(m, iter, &expected_modification_time))
172 return false;
173 r->SetToFilePathRange(file_path, offset, length,
174 expected_modification_time);
175 break;
178 return true;
180 static void Log(const param_type& p, std::string* l) {
181 l->append("<net::UploadElement>");
185 void ParamTraits<scoped_refptr<net::UploadData> >::Write(Message* m,
186 const param_type& p) {
187 WriteParam(m, p.get() != NULL);
188 if (p) {
189 WriteParam(m, *p->elements());
190 WriteParam(m, p->identifier());
191 WriteParam(m, p->is_chunked());
192 WriteParam(m, p->last_chunk_appended());
196 bool ParamTraits<scoped_refptr<net::UploadData> >::Read(const Message* m,
197 PickleIterator* iter,
198 param_type* r) {
199 bool has_object;
200 if (!ReadParam(m, iter, &has_object))
201 return false;
202 if (!has_object)
203 return true;
204 std::vector<net::UploadElement> elements;
205 if (!ReadParam(m, iter, &elements))
206 return false;
207 int64 identifier;
208 if (!ReadParam(m, iter, &identifier))
209 return false;
210 bool is_chunked = false;
211 if (!ReadParam(m, iter, &is_chunked))
212 return false;
213 bool last_chunk_appended = false;
214 if (!ReadParam(m, iter, &last_chunk_appended))
215 return false;
216 *r = new net::UploadData;
217 (*r)->swap_elements(&elements);
218 (*r)->set_identifier(identifier);
219 (*r)->set_is_chunked(is_chunked);
220 (*r)->set_last_chunk_appended(last_chunk_appended);
221 return true;
224 void ParamTraits<scoped_refptr<net::UploadData> >::Log(const param_type& p,
225 std::string* l) {
226 l->append("<net::UploadData>");
229 void ParamTraits<webkit_base::DataElement>::Write(
230 Message* m, const param_type& p) {
231 WriteParam(m, static_cast<int>(p.type()));
232 switch (p.type()) {
233 case webkit_base::DataElement::TYPE_BYTES: {
234 m->WriteData(p.bytes(), static_cast<int>(p.length()));
235 break;
237 case webkit_base::DataElement::TYPE_FILE: {
238 WriteParam(m, p.path());
239 WriteParam(m, p.offset());
240 WriteParam(m, p.length());
241 WriteParam(m, p.expected_modification_time());
242 break;
244 case webkit_base::DataElement::TYPE_FILE_FILESYSTEM: {
245 WriteParam(m, p.url());
246 WriteParam(m, p.offset());
247 WriteParam(m, p.length());
248 WriteParam(m, p.expected_modification_time());
249 break;
251 default: {
252 DCHECK(p.type() == webkit_base::DataElement::TYPE_BLOB);
253 WriteParam(m, p.url());
254 WriteParam(m, p.offset());
255 WriteParam(m, p.length());
256 break;
261 bool ParamTraits<webkit_base::DataElement>::Read(
262 const Message* m, PickleIterator* iter, param_type* r) {
263 int type;
264 if (!ReadParam(m, iter, &type))
265 return false;
266 switch (type) {
267 case webkit_base::DataElement::TYPE_BYTES: {
268 const char* data;
269 int len;
270 if (!m->ReadData(iter, &data, &len))
271 return false;
272 r->SetToBytes(data, len);
273 break;
275 case webkit_base::DataElement::TYPE_FILE: {
276 FilePath file_path;
277 uint64 offset, length;
278 base::Time expected_modification_time;
279 if (!ReadParam(m, iter, &file_path))
280 return false;
281 if (!ReadParam(m, iter, &offset))
282 return false;
283 if (!ReadParam(m, iter, &length))
284 return false;
285 if (!ReadParam(m, iter, &expected_modification_time))
286 return false;
287 r->SetToFilePathRange(file_path, offset, length,
288 expected_modification_time);
289 break;
291 case webkit_base::DataElement::TYPE_FILE_FILESYSTEM: {
292 GURL file_system_url;
293 uint64 offset, length;
294 base::Time expected_modification_time;
295 if (!ReadParam(m, iter, &file_system_url))
296 return false;
297 if (!ReadParam(m, iter, &offset))
298 return false;
299 if (!ReadParam(m, iter, &length))
300 return false;
301 if (!ReadParam(m, iter, &expected_modification_time))
302 return false;
303 r->SetToFileSystemUrlRange(file_system_url, offset, length,
304 expected_modification_time);
305 break;
307 default: {
308 DCHECK(type == webkit_base::DataElement::TYPE_BLOB);
309 GURL blob_url;
310 uint64 offset, length;
311 if (!ReadParam(m, iter, &blob_url))
312 return false;
313 if (!ReadParam(m, iter, &offset))
314 return false;
315 if (!ReadParam(m, iter, &length))
316 return false;
317 r->SetToBlobUrlRange(blob_url, offset, length);
318 break;
321 return true;
324 void ParamTraits<webkit_base::DataElement>::Log(
325 const param_type& p, std::string* l) {
326 l->append("<webkit_base::DataElement>");
329 void ParamTraits<scoped_refptr<webkit_glue::ResourceRequestBody> >::Write(
330 Message* m,
331 const param_type& p) {
332 WriteParam(m, p.get() != NULL);
333 if (p) {
334 WriteParam(m, *p->elements());
335 WriteParam(m, p->identifier());
339 bool ParamTraits<scoped_refptr<webkit_glue::ResourceRequestBody> >::Read(
340 const Message* m,
341 PickleIterator* iter,
342 param_type* r) {
343 bool has_object;
344 if (!ReadParam(m, iter, &has_object))
345 return false;
346 if (!has_object)
347 return true;
348 std::vector<webkit_base::DataElement> elements;
349 if (!ReadParam(m, iter, &elements))
350 return false;
351 int64 identifier;
352 if (!ReadParam(m, iter, &identifier))
353 return false;
354 *r = new webkit_glue::ResourceRequestBody;
355 (*r)->swap_elements(&elements);
356 (*r)->set_identifier(identifier);
357 return true;
360 void ParamTraits<scoped_refptr<webkit_glue::ResourceRequestBody> >::Log(
361 const param_type& p, std::string* l) {
362 l->append("<webkit_glue::ResourceRequestBody>");
365 void ParamTraits<net::HostPortPair>::Write(Message* m, const param_type& p) {
366 WriteParam(m, p.host());
367 WriteParam(m, p.port());
370 bool ParamTraits<net::HostPortPair>::Read(const Message* m,
371 PickleIterator* iter,
372 param_type* r) {
373 std::string host;
374 uint16 port;
375 if (!ReadParam(m, iter, &host) || !ReadParam(m, iter, &port))
376 return false;
378 r->set_host(host);
379 r->set_port(port);
380 return true;
383 void ParamTraits<net::HostPortPair>::Log(const param_type& p, std::string* l) {
384 l->append(p.ToString());
387 void ParamTraits<scoped_refptr<net::HttpResponseHeaders> >::Write(
388 Message* m, const param_type& p) {
389 WriteParam(m, p.get() != NULL);
390 if (p) {
391 // Do not disclose Set-Cookie headers over IPC.
392 p->Persist(m, net::HttpResponseHeaders::PERSIST_SANS_COOKIES);
396 bool ParamTraits<scoped_refptr<net::HttpResponseHeaders> >::Read(
397 const Message* m, PickleIterator* iter, param_type* r) {
398 bool has_object;
399 if (!ReadParam(m, iter, &has_object))
400 return false;
401 if (has_object)
402 *r = new net::HttpResponseHeaders(*m, iter);
403 return true;
406 void ParamTraits<scoped_refptr<net::HttpResponseHeaders> >::Log(
407 const param_type& p, std::string* l) {
408 l->append("<HttpResponseHeaders>");
411 void ParamTraits<net::IPEndPoint>::Write(Message* m, const param_type& p) {
412 WriteParam(m, p.address());
413 WriteParam(m, p.port());
416 bool ParamTraits<net::IPEndPoint>::Read(const Message* m, PickleIterator* iter,
417 param_type* p) {
418 net::IPAddressNumber address;
419 int port;
420 if (!ReadParam(m, iter, &address) || !ReadParam(m, iter, &port))
421 return false;
422 *p = net::IPEndPoint(address, port);
423 return true;
426 void ParamTraits<net::IPEndPoint>::Log(const param_type& p, std::string* l) {
427 LogParam("IPEndPoint:" + p.ToString(), l);
430 void ParamTraits<content::Referrer>::Write(
431 Message* m, const param_type& p) {
432 WriteParam(m, p.url);
433 WriteParam(m, p.policy);
436 bool ParamTraits<content::Referrer>::Read(
437 const Message* m, PickleIterator* iter, param_type* r) {
438 return ReadParam(m, iter, &r->url) && ReadParam(m, iter, &r->policy);
441 void ParamTraits<content::Referrer>::Log(
442 const param_type& p, std::string* l) {
443 l->append("(");
444 LogParam(p.url, l);
445 l->append(",");
446 LogParam(p.policy, l);
447 l->append(")");
450 void ParamTraits<gfx::Point>::Write(Message* m, const gfx::Point& p) {
451 m->WriteInt(p.x());
452 m->WriteInt(p.y());
455 bool ParamTraits<gfx::Point>::Read(const Message* m, PickleIterator* iter,
456 gfx::Point* r) {
457 int x, y;
458 if (!m->ReadInt(iter, &x) ||
459 !m->ReadInt(iter, &y))
460 return false;
461 r->set_x(x);
462 r->set_y(y);
463 return true;
466 void ParamTraits<gfx::Point>::Log(const gfx::Point& p, std::string* l) {
467 l->append(base::StringPrintf("(%d, %d)", p.x(), p.y()));
470 void ParamTraits<gfx::Size>::Write(Message* m, const gfx::Size& p) {
471 m->WriteInt(p.width());
472 m->WriteInt(p.height());
475 bool ParamTraits<gfx::Size>::Read(const Message* m,
476 PickleIterator* iter,
477 gfx::Size* r) {
478 int w, h;
479 if (!m->ReadInt(iter, &w) ||
480 !m->ReadInt(iter, &h))
481 return false;
482 r->set_width(w);
483 r->set_height(h);
484 return true;
487 void ParamTraits<gfx::Size>::Log(const gfx::Size& p, std::string* l) {
488 l->append(base::StringPrintf("(%d, %d)", p.width(), p.height()));
491 void ParamTraits<gfx::Rect>::Write(Message* m, const gfx::Rect& p) {
492 m->WriteInt(p.x());
493 m->WriteInt(p.y());
494 m->WriteInt(p.width());
495 m->WriteInt(p.height());
498 bool ParamTraits<gfx::Rect>::Read(const Message* m,
499 PickleIterator* iter,
500 gfx::Rect* r) {
501 int x, y, w, h;
502 if (!m->ReadInt(iter, &x) ||
503 !m->ReadInt(iter, &y) ||
504 !m->ReadInt(iter, &w) ||
505 !m->ReadInt(iter, &h))
506 return false;
507 r->set_x(x);
508 r->set_y(y);
509 r->set_width(w);
510 r->set_height(h);
511 return true;
514 void ParamTraits<gfx::Rect>::Log(const gfx::Rect& p, std::string* l) {
515 l->append(base::StringPrintf("(%d, %d, %d, %d)", p.x(), p.y(),
516 p.width(), p.height()));
519 void ParamTraits<gfx::RectF>::Write(Message* m, const gfx::RectF& p) {
520 ParamTraits<float>::Write(m, p.x());
521 ParamTraits<float>::Write(m, p.y());
522 ParamTraits<float>::Write(m, p.width());
523 ParamTraits<float>::Write(m, p.height());
526 bool ParamTraits<gfx::RectF>::Read(const Message* m,
527 PickleIterator* iter,
528 gfx::RectF* r) {
529 float x, y, w, h;
530 if (!ParamTraits<float>::Read(m, iter, &x) ||
531 !ParamTraits<float>::Read(m, iter, &y) ||
532 !ParamTraits<float>::Read(m, iter, &w) ||
533 !ParamTraits<float>::Read(m, iter, &h))
534 return false;
535 r->set_x(x);
536 r->set_y(y);
537 r->set_width(w);
538 r->set_height(h);
539 return true;
542 void ParamTraits<gfx::RectF>::Log(const gfx::RectF& p, std::string* l) {
543 l->append(base::StringPrintf("(%f, %f, %f, %f)", p.x(), p.y(),
544 p.width(), p.height()));
547 void ParamTraits<ui::Range>::Write(Message* m, const ui::Range& r) {
548 m->WriteUInt64(r.start());
549 m->WriteUInt64(r.end());
552 bool ParamTraits<ui::Range>::Read(const Message* m,
553 PickleIterator* iter,
554 ui::Range* r) {
555 uint64 start, end;
556 if (!m->ReadUInt64(iter, &start) || !m->ReadUInt64(iter, &end))
557 return false;
558 r->set_start(start);
559 r->set_end(end);
560 return true;
563 void ParamTraits<ui::Range>::Log(const ui::Range& r, std::string* l) {
564 l->append(base::StringPrintf("(%"PRIuS", %"PRIuS")", r.start(), r.end()));
567 void ParamTraits<SkBitmap>::Write(Message* m, const SkBitmap& p) {
568 size_t fixed_size = sizeof(SkBitmap_Data);
569 SkBitmap_Data bmp_data;
570 bmp_data.InitSkBitmapDataForTransfer(p);
571 m->WriteData(reinterpret_cast<const char*>(&bmp_data),
572 static_cast<int>(fixed_size));
573 size_t pixel_size = p.getSize();
574 SkAutoLockPixels p_lock(p);
575 m->WriteData(reinterpret_cast<const char*>(p.getPixels()),
576 static_cast<int>(pixel_size));
579 bool ParamTraits<SkBitmap>::Read(const Message* m,
580 PickleIterator* iter,
581 SkBitmap* r) {
582 const char* fixed_data;
583 int fixed_data_size = 0;
584 if (!m->ReadData(iter, &fixed_data, &fixed_data_size) ||
585 (fixed_data_size <= 0)) {
586 NOTREACHED();
587 return false;
589 if (fixed_data_size != sizeof(SkBitmap_Data))
590 return false; // Message is malformed.
592 const char* variable_data;
593 int variable_data_size = 0;
594 if (!m->ReadData(iter, &variable_data, &variable_data_size) ||
595 (variable_data_size < 0)) {
596 NOTREACHED();
597 return false;
599 const SkBitmap_Data* bmp_data =
600 reinterpret_cast<const SkBitmap_Data*>(fixed_data);
601 return bmp_data->InitSkBitmapFromData(r, variable_data, variable_data_size);
604 void ParamTraits<SkBitmap>::Log(const SkBitmap& p, std::string* l) {
605 l->append("<SkBitmap>");
608 } // namespace IPC
610 // Generate param traits write methods.
611 #include "ipc/param_traits_write_macros.h"
612 namespace IPC {
613 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_
614 #include "content/public/common/common_param_traits_macros.h"
615 } // namespace IPC
617 // Generate param traits read methods.
618 #include "ipc/param_traits_read_macros.h"
619 namespace IPC {
620 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_
621 #include "content/public/common/common_param_traits_macros.h"
622 } // namespace IPC
624 // Generate param traits log methods.
625 #include "ipc/param_traits_log_macros.h"
626 namespace IPC {
627 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_
628 #include "content/public/common/common_param_traits_macros.h"
629 } // namespace IPC