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"
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"
18 void ParamTraits
<GURL
>::Write(Message
* m
, const GURL
& p
) {
19 if (p
.possibly_invalid_spec().length() > content::GetMaxURLChars()) {
20 m
->WriteString(std::string());
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
31 m
->WriteString(std::string());
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
,
43 if (!iter
->ReadString(&s
) || s
.length() > content::GetMaxURLChars()) {
48 if (!s
.empty() && !p
->is_valid()) {
55 void ParamTraits
<GURL
>::Log(const GURL
& p
, std::string
* l
) {
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
,
73 if (!ReadParam(m
, iter
, &unique
) || !ReadParam(m
, iter
, &scheme
) ||
74 !ReadParam(m
, iter
, &host
) || !ReadParam(m
, iter
, &port
)) {
79 *p
= unique
? url::Origin()
80 : url::Origin::UnsafelyCreateOriginWithoutNormalization(
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())
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
,
106 if (!ReadParam(m
, iter
, &host
) || !ReadParam(m
, iter
, &port
))
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
,
126 net::IPAddressNumber address
;
128 if (!ReadParam(m
, iter
, &address
) || !ReadParam(m
, iter
, &port
))
130 if (address
.size() &&
131 address
.size() != net::kIPv4AddressSize
&&
132 address
.size() != net::kIPv6AddressSize
) {
135 *p
= net::IPEndPoint(address
, port
);
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
,
152 if (!ReadParam(m
, iter
, &data
))
154 *r
= content::PageState::CreateFromEncodedData(data
);
158 void ParamTraits
<content::PageState
>::Log(
159 const param_type
& p
, std::string
* l
) {
161 LogParam(p
.ToEncodedData(), l
);
167 // Generate param traits write methods.
168 #include "ipc/param_traits_write_macros.h"
170 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_
171 #include "content/public/common/common_param_traits_macros.h"
174 // Generate param traits read methods.
175 #include "ipc/param_traits_read_macros.h"
177 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_
178 #include "content/public/common/common_param_traits_macros.h"
181 // Generate param traits log methods.
182 #include "ipc/param_traits_log_macros.h"
184 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_
185 #include "content/public/common/common_param_traits_macros.h"