| 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 "net/base/host_port_pair.h" | 12 #include "net/base/host_port_pair.h" |
| 13 #include "net/base/ip_address.h" | 13 #include "net/base/ip_address.h" |
| 14 #include "net/base/ip_endpoint.h" | 14 #include "net/base/ip_endpoint.h" |
| 15 | 15 |
| 16 namespace IPC { | 16 namespace IPC { |
| 17 | 17 |
| 18 void ParamTraits<GURL>::Write(Message* m, const GURL& p) { | 18 void ParamTraits<GURL>::Write(base::Pickle* m, const GURL& p) { |
| 19 if (p.possibly_invalid_spec().length() > content::kMaxURLChars) { | 19 if (p.possibly_invalid_spec().length() > content::kMaxURLChars) { |
| 20 m->WriteString(std::string()); | 20 m->WriteString(std::string()); |
| 21 return; | 21 return; |
| 22 } | 22 } |
| 23 | 23 |
| 24 // Beware of print-parse inconsistency which would change an invalid | 24 // Beware of print-parse inconsistency which would change an invalid |
| 25 // URL into a valid one. Ideally, the message would contain this flag | 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 | 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 | 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 | 28 // type as GURL. See https://crbug.com/166486 for additional work in |
| 29 // this area. | 29 // this area. |
| 30 if (!p.is_valid()) { | 30 if (!p.is_valid()) { |
| 31 m->WriteString(std::string()); | 31 m->WriteString(std::string()); |
| 32 return; | 32 return; |
| 33 } | 33 } |
| 34 | 34 |
| 35 m->WriteString(p.possibly_invalid_spec()); | 35 m->WriteString(p.possibly_invalid_spec()); |
| 36 // TODO(brettw) bug 684583: Add encoding for query params. | 36 // TODO(brettw) bug 684583: Add encoding for query params. |
| 37 } | 37 } |
| 38 | 38 |
| 39 bool ParamTraits<GURL>::Read(const Message* m, | 39 bool ParamTraits<GURL>::Read(const base::Pickle* m, |
| 40 base::PickleIterator* iter, | 40 base::PickleIterator* iter, |
| 41 GURL* p) { | 41 GURL* p) { |
| 42 std::string s; | 42 std::string s; |
| 43 if (!iter->ReadString(&s) || s.length() > content::kMaxURLChars) { | 43 if (!iter->ReadString(&s) || s.length() > content::kMaxURLChars) { |
| 44 *p = GURL(); | 44 *p = GURL(); |
| 45 return false; | 45 return false; |
| 46 } | 46 } |
| 47 *p = GURL(s); | 47 *p = GURL(s); |
| 48 if (!s.empty() && !p->is_valid()) { | 48 if (!s.empty() && !p->is_valid()) { |
| 49 *p = GURL(); | 49 *p = GURL(); |
| 50 return false; | 50 return false; |
| 51 } | 51 } |
| 52 return true; | 52 return true; |
| 53 } | 53 } |
| 54 | 54 |
| 55 void ParamTraits<GURL>::Log(const GURL& p, std::string* l) { | 55 void ParamTraits<GURL>::Log(const GURL& p, std::string* l) { |
| 56 l->append(p.spec()); | 56 l->append(p.spec()); |
| 57 } | 57 } |
| 58 | 58 |
| 59 void ParamTraits<url::Origin>::Write(Message* m, const url::Origin& p) { | 59 void ParamTraits<url::Origin>::Write(base::Pickle* m, const url::Origin& p) { |
| 60 WriteParam(m, p.unique()); | 60 WriteParam(m, p.unique()); |
| 61 WriteParam(m, p.scheme()); | 61 WriteParam(m, p.scheme()); |
| 62 WriteParam(m, p.host()); | 62 WriteParam(m, p.host()); |
| 63 WriteParam(m, p.port()); | 63 WriteParam(m, p.port()); |
| 64 } | 64 } |
| 65 | 65 |
| 66 bool ParamTraits<url::Origin>::Read(const Message* m, | 66 bool ParamTraits<url::Origin>::Read(const base::Pickle* m, |
| 67 base::PickleIterator* iter, | 67 base::PickleIterator* iter, |
| 68 url::Origin* p) { | 68 url::Origin* p) { |
| 69 bool unique; | 69 bool unique; |
| 70 std::string scheme; | 70 std::string scheme; |
| 71 std::string host; | 71 std::string host; |
| 72 uint16_t port; | 72 uint16_t port; |
| 73 if (!ReadParam(m, iter, &unique) || !ReadParam(m, iter, &scheme) || | 73 if (!ReadParam(m, iter, &unique) || !ReadParam(m, iter, &scheme) || |
| 74 !ReadParam(m, iter, &host) || !ReadParam(m, iter, &port)) { | 74 !ReadParam(m, iter, &host) || !ReadParam(m, iter, &port)) { |
| 75 *p = url::Origin(); | 75 *p = url::Origin(); |
| 76 return false; | 76 return false; |
| 77 } | 77 } |
| 78 | 78 |
| 79 *p = unique ? url::Origin() | 79 *p = unique ? url::Origin() |
| 80 : url::Origin::UnsafelyCreateOriginWithoutNormalization( | 80 : url::Origin::UnsafelyCreateOriginWithoutNormalization( |
| 81 scheme, host, port); | 81 scheme, host, port); |
| 82 | 82 |
| 83 // If a unique origin was created, but the unique flag wasn't set, then | 83 // If a unique origin was created, but the unique flag wasn't set, then |
| 84 // the values provided to 'UnsafelyCreateOriginWithoutNormalization' were | 84 // the values provided to 'UnsafelyCreateOriginWithoutNormalization' were |
| 85 // invalid; kill the renderer. | 85 // invalid; kill the renderer. |
| 86 if (!unique && p->unique()) | 86 if (!unique && p->unique()) |
| 87 return false; | 87 return false; |
| 88 | 88 |
| 89 return true; | 89 return true; |
| 90 } | 90 } |
| 91 | 91 |
| 92 void ParamTraits<url::Origin>::Log(const url::Origin& p, std::string* l) { | 92 void ParamTraits<url::Origin>::Log(const url::Origin& p, std::string* l) { |
| 93 l->append(p.Serialize()); | 93 l->append(p.Serialize()); |
| 94 } | 94 } |
| 95 | 95 |
| 96 void ParamTraits<net::HostPortPair>::Write(Message* m, const param_type& p) { | 96 void ParamTraits<net::HostPortPair>::Write(base::Pickle* m, |
| 97 const param_type& p) { |
| 97 WriteParam(m, p.host()); | 98 WriteParam(m, p.host()); |
| 98 WriteParam(m, p.port()); | 99 WriteParam(m, p.port()); |
| 99 } | 100 } |
| 100 | 101 |
| 101 bool ParamTraits<net::HostPortPair>::Read(const Message* m, | 102 bool ParamTraits<net::HostPortPair>::Read(const base::Pickle* m, |
| 102 base::PickleIterator* iter, | 103 base::PickleIterator* iter, |
| 103 param_type* r) { | 104 param_type* r) { |
| 104 std::string host; | 105 std::string host; |
| 105 uint16_t port; | 106 uint16_t port; |
| 106 if (!ReadParam(m, iter, &host) || !ReadParam(m, iter, &port)) | 107 if (!ReadParam(m, iter, &host) || !ReadParam(m, iter, &port)) |
| 107 return false; | 108 return false; |
| 108 | 109 |
| 109 r->set_host(host); | 110 r->set_host(host); |
| 110 r->set_port(port); | 111 r->set_port(port); |
| 111 return true; | 112 return true; |
| 112 } | 113 } |
| 113 | 114 |
| 114 void ParamTraits<net::HostPortPair>::Log(const param_type& p, std::string* l) { | 115 void ParamTraits<net::HostPortPair>::Log(const param_type& p, std::string* l) { |
| 115 l->append(p.ToString()); | 116 l->append(p.ToString()); |
| 116 } | 117 } |
| 117 | 118 |
| 118 void ParamTraits<net::IPEndPoint>::Write(Message* m, const param_type& p) { | 119 void ParamTraits<net::IPEndPoint>::Write(base::Pickle* m, const param_type& p) { |
| 119 WriteParam(m, p.address()); | 120 WriteParam(m, p.address()); |
| 120 WriteParam(m, p.port()); | 121 WriteParam(m, p.port()); |
| 121 } | 122 } |
| 122 | 123 |
| 123 bool ParamTraits<net::IPEndPoint>::Read(const Message* m, | 124 bool ParamTraits<net::IPEndPoint>::Read(const base::Pickle* m, |
| 124 base::PickleIterator* iter, | 125 base::PickleIterator* iter, |
| 125 param_type* p) { | 126 param_type* p) { |
| 126 net::IPAddressNumber address; | 127 net::IPAddressNumber address; |
| 127 uint16_t port; | 128 uint16_t port; |
| 128 if (!ReadParam(m, iter, &address) || !ReadParam(m, iter, &port)) | 129 if (!ReadParam(m, iter, &address) || !ReadParam(m, iter, &port)) |
| 129 return false; | 130 return false; |
| 130 if (address.size() && | 131 if (address.size() && |
| 131 address.size() != net::kIPv4AddressSize && | 132 address.size() != net::kIPv4AddressSize && |
| 132 address.size() != net::kIPv6AddressSize) { | 133 address.size() != net::kIPv6AddressSize) { |
| 133 return false; | 134 return false; |
| 134 } | 135 } |
| 135 *p = net::IPEndPoint(address, port); | 136 *p = net::IPEndPoint(address, port); |
| 136 return true; | 137 return true; |
| 137 } | 138 } |
| 138 | 139 |
| 139 void ParamTraits<net::IPEndPoint>::Log(const param_type& p, std::string* l) { | 140 void ParamTraits<net::IPEndPoint>::Log(const param_type& p, std::string* l) { |
| 140 LogParam("IPEndPoint:" + p.ToString(), l); | 141 LogParam("IPEndPoint:" + p.ToString(), l); |
| 141 } | 142 } |
| 142 | 143 |
| 143 void ParamTraits<net::IPAddress>::Write(Message* m, const param_type& p) { | 144 void ParamTraits<net::IPAddress>::Write(base::Pickle* m, const param_type& p) { |
| 144 WriteParam(m, p.bytes()); | 145 WriteParam(m, p.bytes()); |
| 145 } | 146 } |
| 146 | 147 |
| 147 bool ParamTraits<net::IPAddress>::Read(const Message* m, | 148 bool ParamTraits<net::IPAddress>::Read(const base::Pickle* m, |
| 148 base::PickleIterator* iter, | 149 base::PickleIterator* iter, |
| 149 param_type* p) { | 150 param_type* p) { |
| 150 std::vector<uint8_t> bytes; | 151 std::vector<uint8_t> bytes; |
| 151 if (!ReadParam(m, iter, &bytes)) | 152 if (!ReadParam(m, iter, &bytes)) |
| 152 return false; | 153 return false; |
| 153 if (bytes.size() && | 154 if (bytes.size() && |
| 154 bytes.size() != net::IPAddress::kIPv4AddressSize && | 155 bytes.size() != net::IPAddress::kIPv4AddressSize && |
| 155 bytes.size() != net::IPAddress::kIPv6AddressSize) { | 156 bytes.size() != net::IPAddress::kIPv6AddressSize) { |
| 156 return false; | 157 return false; |
| 157 } | 158 } |
| 158 *p = net::IPAddress(bytes); | 159 *p = net::IPAddress(bytes); |
| 159 return true; | 160 return true; |
| 160 } | 161 } |
| 161 | 162 |
| 162 void ParamTraits<net::IPAddress>::Log(const param_type& p, std::string* l) { | 163 void ParamTraits<net::IPAddress>::Log(const param_type& p, std::string* l) { |
| 163 LogParam("IPAddress:" + (p.empty() ? "(empty)" : p.ToString()), l); | 164 LogParam("IPAddress:" + (p.empty() ? "(empty)" : p.ToString()), l); |
| 164 } | 165 } |
| 165 | 166 |
| 166 void ParamTraits<content::PageState>::Write( | 167 void ParamTraits<content::PageState>::Write(base::Pickle* m, |
| 167 Message* m, const param_type& p) { | 168 const param_type& p) { |
| 168 WriteParam(m, p.ToEncodedData()); | 169 WriteParam(m, p.ToEncodedData()); |
| 169 } | 170 } |
| 170 | 171 |
| 171 bool ParamTraits<content::PageState>::Read(const Message* m, | 172 bool ParamTraits<content::PageState>::Read(const base::Pickle* m, |
| 172 base::PickleIterator* iter, | 173 base::PickleIterator* iter, |
| 173 param_type* r) { | 174 param_type* r) { |
| 174 std::string data; | 175 std::string data; |
| 175 if (!ReadParam(m, iter, &data)) | 176 if (!ReadParam(m, iter, &data)) |
| 176 return false; | 177 return false; |
| 177 *r = content::PageState::CreateFromEncodedData(data); | 178 *r = content::PageState::CreateFromEncodedData(data); |
| 178 return true; | 179 return true; |
| 179 } | 180 } |
| 180 | 181 |
| 181 void ParamTraits<content::PageState>::Log( | 182 void ParamTraits<content::PageState>::Log( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 200 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_ | 201 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_ |
| 201 #include "content/public/common/common_param_traits_macros.h" | 202 #include "content/public/common/common_param_traits_macros.h" |
| 202 } // namespace IPC | 203 } // namespace IPC |
| 203 | 204 |
| 204 // Generate param traits log methods. | 205 // Generate param traits log methods. |
| 205 #include "ipc/param_traits_log_macros.h" | 206 #include "ipc/param_traits_log_macros.h" |
| 206 namespace IPC { | 207 namespace IPC { |
| 207 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_ | 208 #undef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_MACROS_H_ |
| 208 #include "content/public/common/common_param_traits_macros.h" | 209 #include "content/public/common/common_param_traits_macros.h" |
| 209 } // namespace IPC | 210 } // namespace IPC |
| OLD | NEW |