Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / public / common / common_param_traits.cc
bloba0b5bd3d4982e13f4922bf4cb9942ff513581c36
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 <string>
9 #include "content/public/common/content_constants.h"
10 #include "content/public/common/page_state.h"
11 #include "content/public/common/referrer.h"
12 #include "content/public/common/url_utils.h"
13 #include "net/base/host_port_pair.h"
14 #include "net/base/ip_endpoint.h"
16 namespace IPC {
18 void ParamTraits<GURL>::Write(Message* m, const GURL& p) {
19 DCHECK(p.possibly_invalid_spec().length() <= content::GetMaxURLChars());
21 // Beware of print-parse inconsistency which would change an invalid
22 // URL into a valid one. Ideally, the message would contain this flag
23 // so that the read side could make the check, but performing it here
24 // avoids changing the on-the-wire representation of such a fundamental
25 // type as GURL. See https://crbug.com/166486 for additional work in
26 // this area.
27 if (!p.is_valid()) {
28 m->WriteString(std::string());
29 return;
32 m->WriteString(p.possibly_invalid_spec());
33 // TODO(brettw) bug 684583: Add encoding for query params.
36 bool ParamTraits<GURL>::Read(const Message* m, PickleIterator* iter, GURL* p) {
37 std::string s;
38 if (!iter->ReadString(&s) || s.length() > content::GetMaxURLChars()) {
39 *p = GURL();
40 return false;
42 *p = GURL(s);
43 if (!s.empty() && !p->is_valid()) {
44 *p = GURL();
45 return false;
47 return true;
50 void ParamTraits<GURL>::Log(const GURL& p, std::string* l) {
51 l->append(p.spec());
54 void ParamTraits<url::Origin>::Write(Message* m,
55 const url::Origin& p) {
56 m->WriteString(p.string());
59 bool ParamTraits<url::Origin>::Read(const Message* m,
60 PickleIterator* iter,
61 url::Origin* p) {
62 std::string s;
63 if (!iter->ReadString(&s)) {
64 *p = url::Origin();
65 return false;
67 *p = url::Origin(s);
68 return true;
71 void ParamTraits<url::Origin>::Log(const url::Origin& p, std::string* l) {
72 l->append(p.string());
75 void ParamTraits<net::HostPortPair>::Write(Message* m, const param_type& p) {
76 WriteParam(m, p.host());
77 WriteParam(m, p.port());
80 bool ParamTraits<net::HostPortPair>::Read(const Message* m,
81 PickleIterator* iter,
82 param_type* r) {
83 std::string host;
84 uint16 port;
85 if (!ReadParam(m, iter, &host) || !ReadParam(m, iter, &port))
86 return false;
88 r->set_host(host);
89 r->set_port(port);
90 return true;
93 void ParamTraits<net::HostPortPair>::Log(const param_type& p, std::string* l) {
94 l->append(p.ToString());
97 void ParamTraits<net::IPEndPoint>::Write(Message* m, const param_type& p) {
98 WriteParam(m, p.address());
99 WriteParam(m, p.port());
102 bool ParamTraits<net::IPEndPoint>::Read(const Message* m, PickleIterator* iter,
103 param_type* p) {
104 net::IPAddressNumber address;
105 uint16 port;
106 if (!ReadParam(m, iter, &address) || !ReadParam(m, iter, &port))
107 return false;
108 if (address.size() &&
109 address.size() != net::kIPv4AddressSize &&
110 address.size() != net::kIPv6AddressSize) {
111 return false;
113 *p = net::IPEndPoint(address, port);
114 return true;
117 void ParamTraits<net::IPEndPoint>::Log(const param_type& p, std::string* l) {
118 LogParam("IPEndPoint:" + p.ToString(), l);
121 void ParamTraits<content::PageState>::Write(
122 Message* m, const param_type& p) {
123 WriteParam(m, p.ToEncodedData());
126 bool ParamTraits<content::PageState>::Read(
127 const Message* m, PickleIterator* iter, param_type* r) {
128 std::string data;
129 if (!ReadParam(m, iter, &data))
130 return false;
131 *r = content::PageState::CreateFromEncodedData(data);
132 return true;
135 void ParamTraits<content::PageState>::Log(
136 const param_type& p, std::string* l) {
137 l->append("(");
138 LogParam(p.ToEncodedData(), l);
139 l->append(")");
142 } // namespace IPC
144 // Generate param traits write methods.
145 #include "ipc/param_traits_write_macros.h"
146 namespace IPC {
147 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_
148 #include "content/public/common/common_param_traits_macros.h"
149 } // namespace IPC
151 // Generate param traits read methods.
152 #include "ipc/param_traits_read_macros.h"
153 namespace IPC {
154 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_
155 #include "content/public/common/common_param_traits_macros.h"
156 } // namespace IPC
158 // Generate param traits log methods.
159 #include "ipc/param_traits_log_macros.h"
160 namespace IPC {
161 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_
162 #include "content/public/common/common_param_traits_macros.h"
163 } // namespace IPC