Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / public / common / common_param_traits.cc
blobdfe7a7fa4561b5a3e558b66a192ed3e963fa7739
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 if (p.possibly_invalid_spec().length() > content::GetMaxURLChars()) {
20 m->WriteString(std::string());
21 return;
24 // Beware of print-parse inconsistency which would change an invalid
25 // URL into a valid one. Ideally, the message would contain this flag
26 // so that the read side could make the check, but performing it here
27 // avoids changing the on-the-wire representation of such a fundamental
28 // type as GURL. See https://crbug.com/166486 for additional work in
29 // this area.
30 if (!p.is_valid()) {
31 m->WriteString(std::string());
32 return;
35 m->WriteString(p.possibly_invalid_spec());
36 // TODO(brettw) bug 684583: Add encoding for query params.
39 bool ParamTraits<GURL>::Read(const Message* m,
40 base::PickleIterator* iter,
41 GURL* p) {
42 std::string s;
43 if (!iter->ReadString(&s) || s.length() > content::GetMaxURLChars()) {
44 *p = GURL();
45 return false;
47 *p = GURL(s);
48 if (!s.empty() && !p->is_valid()) {
49 *p = GURL();
50 return false;
52 return true;
55 void ParamTraits<GURL>::Log(const GURL& p, std::string* l) {
56 l->append(p.spec());
59 void ParamTraits<url::Origin>::Write(Message* m, const url::Origin& p) {
60 WriteParam(m, p.unique());
61 WriteParam(m, p.scheme());
62 WriteParam(m, p.host());
63 WriteParam(m, p.port());
66 bool ParamTraits<url::Origin>::Read(const Message* m,
67 base::PickleIterator* iter,
68 url::Origin* p) {
69 bool unique;
70 std::string scheme;
71 std::string host;
72 uint16 port;
73 if (!ReadParam(m, iter, &unique) || !ReadParam(m, iter, &scheme) ||
74 !ReadParam(m, iter, &host) || !ReadParam(m, iter, &port)) {
75 *p = url::Origin();
76 return false;
79 *p = unique ? url::Origin()
80 : url::Origin::UnsafelyCreateOriginWithoutNormalization(
81 scheme, host, port);
83 // If a unique origin was created, but the unique flag wasn't set, then
84 // the values provided to 'UnsafelyCreateOriginWithoutNormalization' were
85 // invalid; kill the renderer.
86 if (!unique && p->unique())
87 return false;
89 return true;
92 void ParamTraits<url::Origin>::Log(const url::Origin& p, std::string* l) {
93 l->append(p.Serialize());
96 void ParamTraits<net::HostPortPair>::Write(Message* m, const param_type& p) {
97 WriteParam(m, p.host());
98 WriteParam(m, p.port());
101 bool ParamTraits<net::HostPortPair>::Read(const Message* m,
102 base::PickleIterator* iter,
103 param_type* r) {
104 std::string host;
105 uint16 port;
106 if (!ReadParam(m, iter, &host) || !ReadParam(m, iter, &port))
107 return false;
109 r->set_host(host);
110 r->set_port(port);
111 return true;
114 void ParamTraits<net::HostPortPair>::Log(const param_type& p, std::string* l) {
115 l->append(p.ToString());
118 void ParamTraits<net::IPEndPoint>::Write(Message* m, const param_type& p) {
119 WriteParam(m, p.address());
120 WriteParam(m, p.port());
123 bool ParamTraits<net::IPEndPoint>::Read(const Message* m,
124 base::PickleIterator* iter,
125 param_type* p) {
126 net::IPAddressNumber address;
127 uint16 port;
128 if (!ReadParam(m, iter, &address) || !ReadParam(m, iter, &port))
129 return false;
130 if (address.size() &&
131 address.size() != net::kIPv4AddressSize &&
132 address.size() != net::kIPv6AddressSize) {
133 return false;
135 *p = net::IPEndPoint(address, port);
136 return true;
139 void ParamTraits<net::IPEndPoint>::Log(const param_type& p, std::string* l) {
140 LogParam("IPEndPoint:" + p.ToString(), l);
143 void ParamTraits<content::PageState>::Write(
144 Message* m, const param_type& p) {
145 WriteParam(m, p.ToEncodedData());
148 bool ParamTraits<content::PageState>::Read(const Message* m,
149 base::PickleIterator* iter,
150 param_type* r) {
151 std::string data;
152 if (!ReadParam(m, iter, &data))
153 return false;
154 *r = content::PageState::CreateFromEncodedData(data);
155 return true;
158 void ParamTraits<content::PageState>::Log(
159 const param_type& p, std::string* l) {
160 l->append("(");
161 LogParam(p.ToEncodedData(), l);
162 l->append(")");
165 } // namespace IPC
167 // Generate param traits write methods.
168 #include "ipc/param_traits_write_macros.h"
169 namespace IPC {
170 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_
171 #include "content/public/common/common_param_traits_macros.h"
172 } // namespace IPC
174 // Generate param traits read methods.
175 #include "ipc/param_traits_read_macros.h"
176 namespace IPC {
177 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_
178 #include "content/public/common/common_param_traits_macros.h"
179 } // namespace IPC
181 // Generate param traits log methods.
182 #include "ipc/param_traits_log_macros.h"
183 namespace IPC {
184 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_
185 #include "content/public/common/common_param_traits_macros.h"
186 } // namespace IPC