OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 // NOTE: New trait definitions that will only be used by tests (and not by |
| 6 // Chrome Frame) must be placed in common_param_traits.cc. |
| 7 |
| 8 #include "content/common/common_param_traits.h" |
| 9 |
| 10 #include "content/common/content_constants.h" |
| 11 #include "net/base/host_port_pair.h" |
| 12 #include "net/base/upload_data.h" |
| 13 #include "ui/gfx/rect.h" |
| 14 |
| 15 namespace IPC { |
| 16 |
| 17 void ParamTraits<GURL>::Write(Message* m, const GURL& p) { |
| 18 m->WriteString(p.possibly_invalid_spec()); |
| 19 // TODO(brettw) bug 684583: Add encoding for query params. |
| 20 } |
| 21 |
| 22 bool ParamTraits<GURL>::Read(const Message* m, void** iter, GURL* p) { |
| 23 std::string s; |
| 24 if (!m->ReadString(iter, &s) || s.length() > content::kMaxURLChars) { |
| 25 *p = GURL(); |
| 26 return false; |
| 27 } |
| 28 *p = GURL(s); |
| 29 return true; |
| 30 } |
| 31 |
| 32 void ParamTraits<GURL>::Log(const GURL& p, std::string* l) { |
| 33 l->append(p.spec()); |
| 34 } |
| 35 |
| 36 void ParamTraits<net::URLRequestStatus>::Write(Message* m, |
| 37 const param_type& p) { |
| 38 WriteParam(m, static_cast<int>(p.status())); |
| 39 WriteParam(m, p.os_error()); |
| 40 } |
| 41 |
| 42 bool ParamTraits<net::URLRequestStatus>::Read(const Message* m, void** iter, |
| 43 param_type* r) { |
| 44 int status, os_error; |
| 45 if (!ReadParam(m, iter, &status) || |
| 46 !ReadParam(m, iter, &os_error)) |
| 47 return false; |
| 48 r->set_status(static_cast<net::URLRequestStatus::Status>(status)); |
| 49 r->set_os_error(os_error); |
| 50 return true; |
| 51 } |
| 52 |
| 53 void ParamTraits<net::URLRequestStatus>::Log(const param_type& p, |
| 54 std::string* l) { |
| 55 std::string status; |
| 56 switch (p.status()) { |
| 57 case net::URLRequestStatus::SUCCESS: |
| 58 status = "SUCCESS"; |
| 59 break; |
| 60 case net::URLRequestStatus::IO_PENDING: |
| 61 status = "IO_PENDING "; |
| 62 break; |
| 63 case net::URLRequestStatus::HANDLED_EXTERNALLY: |
| 64 status = "HANDLED_EXTERNALLY"; |
| 65 break; |
| 66 case net::URLRequestStatus::CANCELED: |
| 67 status = "CANCELED"; |
| 68 break; |
| 69 case net::URLRequestStatus::FAILED: |
| 70 status = "FAILED"; |
| 71 break; |
| 72 default: |
| 73 status = "UNKNOWN"; |
| 74 break; |
| 75 } |
| 76 if (p.status() == net::URLRequestStatus::FAILED) |
| 77 l->append("("); |
| 78 |
| 79 LogParam(status, l); |
| 80 |
| 81 if (p.status() == net::URLRequestStatus::FAILED) { |
| 82 l->append(", "); |
| 83 LogParam(p.os_error(), l); |
| 84 l->append(")"); |
| 85 } |
| 86 } |
| 87 |
| 88 // Only the net::UploadData ParamTraits<> definition needs this definition, so |
| 89 // keep this in the implementation file so we can forward declare UploadData in |
| 90 // the header. |
| 91 template <> |
| 92 struct ParamTraits<net::UploadData::Element> { |
| 93 typedef net::UploadData::Element param_type; |
| 94 static void Write(Message* m, const param_type& p) { |
| 95 WriteParam(m, static_cast<int>(p.type())); |
| 96 switch (p.type()) { |
| 97 case net::UploadData::TYPE_BYTES: { |
| 98 m->WriteData(&p.bytes()[0], static_cast<int>(p.bytes().size())); |
| 99 break; |
| 100 } |
| 101 case net::UploadData::TYPE_CHUNK: { |
| 102 std::string chunk_length = StringPrintf( |
| 103 "%X\r\n", static_cast<unsigned int>(p.bytes().size())); |
| 104 std::vector<char> bytes; |
| 105 bytes.insert(bytes.end(), chunk_length.data(), |
| 106 chunk_length.data() + chunk_length.length()); |
| 107 const char* data = &p.bytes()[0]; |
| 108 bytes.insert(bytes.end(), data, data + p.bytes().size()); |
| 109 const char* crlf = "\r\n"; |
| 110 bytes.insert(bytes.end(), crlf, crlf + strlen(crlf)); |
| 111 if (p.is_last_chunk()) { |
| 112 const char* end_of_data = "0\r\n\r\n"; |
| 113 bytes.insert(bytes.end(), end_of_data, |
| 114 end_of_data + strlen(end_of_data)); |
| 115 } |
| 116 m->WriteData(&bytes[0], static_cast<int>(bytes.size())); |
| 117 // If this element is part of a chunk upload then send over information |
| 118 // indicating if this is the last chunk. |
| 119 WriteParam(m, p.is_last_chunk()); |
| 120 break; |
| 121 } |
| 122 case net::UploadData::TYPE_FILE: { |
| 123 WriteParam(m, p.file_path()); |
| 124 WriteParam(m, p.file_range_offset()); |
| 125 WriteParam(m, p.file_range_length()); |
| 126 WriteParam(m, p.expected_file_modification_time()); |
| 127 break; |
| 128 } |
| 129 default: { |
| 130 WriteParam(m, p.blob_url()); |
| 131 break; |
| 132 } |
| 133 } |
| 134 } |
| 135 static bool Read(const Message* m, void** iter, param_type* r) { |
| 136 int type; |
| 137 if (!ReadParam(m, iter, &type)) |
| 138 return false; |
| 139 switch (type) { |
| 140 case net::UploadData::TYPE_BYTES: { |
| 141 const char* data; |
| 142 int len; |
| 143 if (!m->ReadData(iter, &data, &len)) |
| 144 return false; |
| 145 r->SetToBytes(data, len); |
| 146 break; |
| 147 } |
| 148 case net::UploadData::TYPE_CHUNK: { |
| 149 const char* data; |
| 150 int len; |
| 151 if (!m->ReadData(iter, &data, &len)) |
| 152 return false; |
| 153 r->SetToBytes(data, len); |
| 154 // If this element is part of a chunk upload then we need to explicitly |
| 155 // set the type of the element and whether it is the last chunk. |
| 156 bool is_last_chunk = false; |
| 157 if (!ReadParam(m, iter, &is_last_chunk)) |
| 158 return false; |
| 159 r->set_type(net::UploadData::TYPE_CHUNK); |
| 160 r->set_is_last_chunk(is_last_chunk); |
| 161 break; |
| 162 } |
| 163 case net::UploadData::TYPE_FILE: { |
| 164 FilePath file_path; |
| 165 uint64 offset, length; |
| 166 base::Time expected_modification_time; |
| 167 if (!ReadParam(m, iter, &file_path)) |
| 168 return false; |
| 169 if (!ReadParam(m, iter, &offset)) |
| 170 return false; |
| 171 if (!ReadParam(m, iter, &length)) |
| 172 return false; |
| 173 if (!ReadParam(m, iter, &expected_modification_time)) |
| 174 return false; |
| 175 r->SetToFilePathRange(file_path, offset, length, |
| 176 expected_modification_time); |
| 177 break; |
| 178 } |
| 179 default: { |
| 180 DCHECK(type == net::UploadData::TYPE_BLOB); |
| 181 GURL blob_url; |
| 182 if (!ReadParam(m, iter, &blob_url)) |
| 183 return false; |
| 184 r->SetToBlobUrl(blob_url); |
| 185 break; |
| 186 } |
| 187 } |
| 188 return true; |
| 189 } |
| 190 static void Log(const param_type& p, std::string* l) { |
| 191 l->append("<net::UploadData::Element>"); |
| 192 } |
| 193 }; |
| 194 |
| 195 void ParamTraits<scoped_refptr<net::UploadData> >::Write(Message* m, |
| 196 const param_type& p) { |
| 197 WriteParam(m, p.get() != NULL); |
| 198 if (p) { |
| 199 WriteParam(m, *p->elements()); |
| 200 WriteParam(m, p->identifier()); |
| 201 WriteParam(m, p->is_chunked()); |
| 202 } |
| 203 } |
| 204 |
| 205 bool ParamTraits<scoped_refptr<net::UploadData> >::Read(const Message* m, |
| 206 void** iter, |
| 207 param_type* r) { |
| 208 bool has_object; |
| 209 if (!ReadParam(m, iter, &has_object)) |
| 210 return false; |
| 211 if (!has_object) |
| 212 return true; |
| 213 std::vector<net::UploadData::Element> elements; |
| 214 if (!ReadParam(m, iter, &elements)) |
| 215 return false; |
| 216 int64 identifier; |
| 217 if (!ReadParam(m, iter, &identifier)) |
| 218 return false; |
| 219 bool is_chunked = false; |
| 220 if (!ReadParam(m, iter, &is_chunked)) |
| 221 return false; |
| 222 *r = new net::UploadData; |
| 223 (*r)->swap_elements(&elements); |
| 224 (*r)->set_identifier(identifier); |
| 225 (*r)->set_is_chunked(is_chunked); |
| 226 return true; |
| 227 } |
| 228 |
| 229 void ParamTraits<scoped_refptr<net::UploadData> >::Log(const param_type& p, |
| 230 std::string* l) { |
| 231 l->append("<net::UploadData>"); |
| 232 } |
| 233 |
| 234 void ParamTraits<net::HostPortPair>::Write(Message* m, const param_type& p) { |
| 235 WriteParam(m, p.host()); |
| 236 WriteParam(m, p.port()); |
| 237 } |
| 238 |
| 239 bool ParamTraits<net::HostPortPair>::Read(const Message* m, void** iter, |
| 240 param_type* r) { |
| 241 std::string host; |
| 242 uint16 port; |
| 243 if (!ReadParam(m, iter, &host) || !ReadParam(m, iter, &port)) |
| 244 return false; |
| 245 |
| 246 r->set_host(host); |
| 247 r->set_port(port); |
| 248 return true; |
| 249 } |
| 250 |
| 251 void ParamTraits<net::HostPortPair>::Log(const param_type& p, std::string* l) { |
| 252 l->append(p.ToString()); |
| 253 } |
| 254 |
| 255 void ParamTraits<gfx::Point>::Write(Message* m, const gfx::Point& p) { |
| 256 m->WriteInt(p.x()); |
| 257 m->WriteInt(p.y()); |
| 258 } |
| 259 |
| 260 bool ParamTraits<gfx::Point>::Read(const Message* m, void** iter, |
| 261 gfx::Point* r) { |
| 262 int x, y; |
| 263 if (!m->ReadInt(iter, &x) || |
| 264 !m->ReadInt(iter, &y)) |
| 265 return false; |
| 266 r->set_x(x); |
| 267 r->set_y(y); |
| 268 return true; |
| 269 } |
| 270 |
| 271 void ParamTraits<gfx::Point>::Log(const gfx::Point& p, std::string* l) { |
| 272 l->append(base::StringPrintf("(%d, %d)", p.x(), p.y())); |
| 273 } |
| 274 |
| 275 void ParamTraits<gfx::Rect>::Write(Message* m, const gfx::Rect& p) { |
| 276 m->WriteInt(p.x()); |
| 277 m->WriteInt(p.y()); |
| 278 m->WriteInt(p.width()); |
| 279 m->WriteInt(p.height()); |
| 280 } |
| 281 |
| 282 bool ParamTraits<gfx::Rect>::Read(const Message* m, void** iter, gfx::Rect* r) { |
| 283 int x, y, w, h; |
| 284 if (!m->ReadInt(iter, &x) || |
| 285 !m->ReadInt(iter, &y) || |
| 286 !m->ReadInt(iter, &w) || |
| 287 !m->ReadInt(iter, &h)) |
| 288 return false; |
| 289 r->set_x(x); |
| 290 r->set_y(y); |
| 291 r->set_width(w); |
| 292 r->set_height(h); |
| 293 return true; |
| 294 } |
| 295 |
| 296 void ParamTraits<gfx::Rect>::Log(const gfx::Rect& p, std::string* l) { |
| 297 l->append(base::StringPrintf("(%d, %d, %d, %d)", p.x(), p.y(), |
| 298 p.width(), p.height())); |
| 299 } |
| 300 |
| 301 } // namespace IPC |
OLD | NEW |