Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: content/common/common_param_traits.cc

Issue 6628035: Move resource related IPCs to their own file in content. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/common/common_param_traits.h ('k') | content/common/content_message_generator.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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/common/common_param_traits.h" 5 #include "content/common/common_param_traits.h"
6 6
7 #include "content/common/content_constants.h" 7 #include "content/common/content_constants.h"
8 #include "net/base/host_port_pair.h"
9 #include "net/base/upload_data.h"
10 #include "net/http/http_response_headers.h"
11 #include "webkit/glue/resource_loader_bridge.h"
8 12
9 namespace IPC { 13 namespace IPC {
10 14
11 void ParamTraits<GURL>::Write(Message* m, const GURL& p) { 15 void ParamTraits<GURL>::Write(Message* m, const GURL& p) {
12 m->WriteString(p.possibly_invalid_spec()); 16 m->WriteString(p.possibly_invalid_spec());
13 // TODO(brettw) bug 684583: Add encoding for query params. 17 // TODO(brettw) bug 684583: Add encoding for query params.
14 } 18 }
15 19
16 bool ParamTraits<GURL>::Read(const Message* m, void** iter, GURL* p) { 20 bool ParamTraits<GURL>::Read(const Message* m, void** iter, GURL* p) {
17 std::string s; 21 std::string s;
18 if (!m->ReadString(iter, &s) || s.length() > content::kMaxURLChars) { 22 if (!m->ReadString(iter, &s) || s.length() > content::kMaxURLChars) {
19 *p = GURL(); 23 *p = GURL();
20 return false; 24 return false;
21 } 25 }
22 *p = GURL(s); 26 *p = GURL(s);
23 return true; 27 return true;
24 } 28 }
25 29
26 void ParamTraits<GURL>::Log(const GURL& p, std::string* l) { 30 void ParamTraits<GURL>::Log(const GURL& p, std::string* l) {
27 l->append(p.spec()); 31 l->append(p.spec());
28 } 32 }
29 33
34
35 void ParamTraits<ResourceType::Type>::Write(Message* m, const param_type& p) {
36 m->WriteInt(p);
37 }
38
39 bool ParamTraits<ResourceType::Type>::Read(const Message* m,
40 void** iter,
41 param_type* p) {
42 int type;
43 if (!m->ReadInt(iter, &type) || !ResourceType::ValidType(type))
44 return false;
45 *p = ResourceType::FromInt(type);
46 return true;
47 }
48
49 void ParamTraits<ResourceType::Type>::Log(const param_type& p, std::string* l) {
50 std::string type;
51 switch (p) {
52 case ResourceType::MAIN_FRAME:
53 type = "MAIN_FRAME";
54 break;
55 case ResourceType::SUB_FRAME:
56 type = "SUB_FRAME";
57 break;
58 case ResourceType::SUB_RESOURCE:
59 type = "SUB_RESOURCE";
60 break;
61 case ResourceType::OBJECT:
62 type = "OBJECT";
63 break;
64 case ResourceType::MEDIA:
65 type = "MEDIA";
66 break;
67 default:
68 type = "UNKNOWN";
69 break;
70 }
71
72 LogParam(type, l);
73 }
74
75 void ParamTraits<net::URLRequestStatus>::Write(Message* m,
76 const param_type& p) {
77 WriteParam(m, static_cast<int>(p.status()));
78 WriteParam(m, p.os_error());
79 }
80
81 bool ParamTraits<net::URLRequestStatus>::Read(const Message* m, void** iter,
82 param_type* r) {
83 int status, os_error;
84 if (!ReadParam(m, iter, &status) ||
85 !ReadParam(m, iter, &os_error))
86 return false;
87 r->set_status(static_cast<net::URLRequestStatus::Status>(status));
88 r->set_os_error(os_error);
89 return true;
90 }
91
92 void ParamTraits<net::URLRequestStatus>::Log(const param_type& p,
93 std::string* l) {
94 std::string status;
95 switch (p.status()) {
96 case net::URLRequestStatus::SUCCESS:
97 status = "SUCCESS";
98 break;
99 case net::URLRequestStatus::IO_PENDING:
100 status = "IO_PENDING ";
101 break;
102 case net::URLRequestStatus::HANDLED_EXTERNALLY:
103 status = "HANDLED_EXTERNALLY";
104 break;
105 case net::URLRequestStatus::CANCELED:
106 status = "CANCELED";
107 break;
108 case net::URLRequestStatus::FAILED:
109 status = "FAILED";
110 break;
111 default:
112 status = "UNKNOWN";
113 break;
114 }
115 if (p.status() == net::URLRequestStatus::FAILED)
116 l->append("(");
117
118 LogParam(status, l);
119
120 if (p.status() == net::URLRequestStatus::FAILED) {
121 l->append(", ");
122 LogParam(p.os_error(), l);
123 l->append(")");
124 }
125 }
126
127 // Only the net::UploadData ParamTraits<> definition needs this definition, so
128 // keep this in the implementation file so we can forward declare UploadData in
129 // the header.
130 template <>
131 struct ParamTraits<net::UploadData::Element> {
132 typedef net::UploadData::Element param_type;
133 static void Write(Message* m, const param_type& p) {
134 WriteParam(m, static_cast<int>(p.type()));
135 switch (p.type()) {
136 case net::UploadData::TYPE_BYTES: {
137 m->WriteData(&p.bytes()[0], static_cast<int>(p.bytes().size()));
138 break;
139 }
140 case net::UploadData::TYPE_CHUNK: {
141 std::string chunk_length = StringPrintf(
142 "%X\r\n", static_cast<unsigned int>(p.bytes().size()));
143 std::vector<char> bytes;
144 bytes.insert(bytes.end(), chunk_length.data(),
145 chunk_length.data() + chunk_length.length());
146 const char* data = &p.bytes()[0];
147 bytes.insert(bytes.end(), data, data + p.bytes().size());
148 const char* crlf = "\r\n";
149 bytes.insert(bytes.end(), crlf, crlf + strlen(crlf));
150 if (p.is_last_chunk()) {
151 const char* end_of_data = "0\r\n\r\n";
152 bytes.insert(bytes.end(), end_of_data,
153 end_of_data + strlen(end_of_data));
154 }
155 m->WriteData(&bytes[0], static_cast<int>(bytes.size()));
156 // If this element is part of a chunk upload then send over information
157 // indicating if this is the last chunk.
158 WriteParam(m, p.is_last_chunk());
159 break;
160 }
161 case net::UploadData::TYPE_FILE: {
162 WriteParam(m, p.file_path());
163 WriteParam(m, p.file_range_offset());
164 WriteParam(m, p.file_range_length());
165 WriteParam(m, p.expected_file_modification_time());
166 break;
167 }
168 default: {
169 WriteParam(m, p.blob_url());
170 break;
171 }
172 }
173 }
174 static bool Read(const Message* m, void** iter, param_type* r) {
175 int type;
176 if (!ReadParam(m, iter, &type))
177 return false;
178 switch (type) {
179 case net::UploadData::TYPE_BYTES: {
180 const char* data;
181 int len;
182 if (!m->ReadData(iter, &data, &len))
183 return false;
184 r->SetToBytes(data, len);
185 break;
186 }
187 case net::UploadData::TYPE_CHUNK: {
188 const char* data;
189 int len;
190 if (!m->ReadData(iter, &data, &len))
191 return false;
192 r->SetToBytes(data, len);
193 // If this element is part of a chunk upload then we need to explicitly
194 // set the type of the element and whether it is the last chunk.
195 bool is_last_chunk = false;
196 if (!ReadParam(m, iter, &is_last_chunk))
197 return false;
198 r->set_type(net::UploadData::TYPE_CHUNK);
199 r->set_is_last_chunk(is_last_chunk);
200 break;
201 }
202 case net::UploadData::TYPE_FILE: {
203 FilePath file_path;
204 uint64 offset, length;
205 base::Time expected_modification_time;
206 if (!ReadParam(m, iter, &file_path))
207 return false;
208 if (!ReadParam(m, iter, &offset))
209 return false;
210 if (!ReadParam(m, iter, &length))
211 return false;
212 if (!ReadParam(m, iter, &expected_modification_time))
213 return false;
214 r->SetToFilePathRange(file_path, offset, length,
215 expected_modification_time);
216 break;
217 }
218 default: {
219 DCHECK(type == net::UploadData::TYPE_BLOB);
220 GURL blob_url;
221 if (!ReadParam(m, iter, &blob_url))
222 return false;
223 r->SetToBlobUrl(blob_url);
224 break;
225 }
226 }
227 return true;
228 }
229 static void Log(const param_type& p, std::string* l) {
230 l->append("<net::UploadData::Element>");
231 }
232 };
233
234 void ParamTraits<scoped_refptr<net::UploadData> >::Write(Message* m,
235 const param_type& p) {
236 WriteParam(m, p.get() != NULL);
237 if (p) {
238 WriteParam(m, *p->elements());
239 WriteParam(m, p->identifier());
240 WriteParam(m, p->is_chunked());
241 }
242 }
243
244 bool ParamTraits<scoped_refptr<net::UploadData> >::Read(const Message* m,
245 void** iter,
246 param_type* r) {
247 bool has_object;
248 if (!ReadParam(m, iter, &has_object))
249 return false;
250 if (!has_object)
251 return true;
252 std::vector<net::UploadData::Element> elements;
253 if (!ReadParam(m, iter, &elements))
254 return false;
255 int64 identifier;
256 if (!ReadParam(m, iter, &identifier))
257 return false;
258 bool is_chunked = false;
259 if (!ReadParam(m, iter, &is_chunked))
260 return false;
261 *r = new net::UploadData;
262 (*r)->swap_elements(&elements);
263 (*r)->set_identifier(identifier);
264 (*r)->set_is_chunked(is_chunked);
265 return true;
266 }
267
268 void ParamTraits<scoped_refptr<net::UploadData> >::Log(const param_type& p,
269 std::string* l) {
270 l->append("<net::UploadData>");
271 }
272
273 void ParamTraits<net::HostPortPair>::Write(Message* m, const param_type& p) {
274 WriteParam(m, p.host());
275 WriteParam(m, p.port());
276 }
277
278 bool ParamTraits<net::HostPortPair>::Read(const Message* m, void** iter,
279 param_type* r) {
280 std::string host;
281 uint16 port;
282 if (!ReadParam(m, iter, &host) || !ReadParam(m, iter, &port))
283 return false;
284
285 r->set_host(host);
286 r->set_port(port);
287 return true;
288 }
289
290 void ParamTraits<net::HostPortPair>::Log(const param_type& p, std::string* l) {
291 l->append(p.ToString());
292 }
293
294 void ParamTraits<scoped_refptr<net::HttpResponseHeaders> >::Write(
295 Message* m, const param_type& p) {
296 WriteParam(m, p.get() != NULL);
297 if (p) {
298 // Do not disclose Set-Cookie headers over IPC.
299 p->Persist(m, net::HttpResponseHeaders::PERSIST_SANS_COOKIES);
300 }
301 }
302
303 bool ParamTraits<scoped_refptr<net::HttpResponseHeaders> >::Read(
304 const Message* m, void** iter, param_type* r) {
305 bool has_object;
306 if (!ReadParam(m, iter, &has_object))
307 return false;
308 if (has_object)
309 *r = new net::HttpResponseHeaders(*m, iter);
310 return true;
311 }
312
313 void ParamTraits<scoped_refptr<net::HttpResponseHeaders> >::Log(
314 const param_type& p, std::string* l) {
315 l->append("<HttpResponseHeaders>");
316 }
317
318 void ParamTraits<webkit_glue::ResourceLoadTimingInfo>::Write(
319 Message* m, const param_type& p) {
320 WriteParam(m, p.base_time.is_null());
321 if (p.base_time.is_null())
322 return;
323 WriteParam(m, p.base_time);
324 WriteParam(m, p.proxy_start);
325 WriteParam(m, p.proxy_end);
326 WriteParam(m, p.dns_start);
327 WriteParam(m, p.dns_end);
328 WriteParam(m, p.connect_start);
329 WriteParam(m, p.connect_end);
330 WriteParam(m, p.ssl_start);
331 WriteParam(m, p.ssl_end);
332 WriteParam(m, p.send_start);
333 WriteParam(m, p.send_end);
334 WriteParam(m, p.receive_headers_start);
335 WriteParam(m, p.receive_headers_end);
336 }
337
338 bool ParamTraits<webkit_glue::ResourceLoadTimingInfo>::Read(
339 const Message* m, void** iter, param_type* r) {
340 bool is_null;
341 if (!ReadParam(m, iter, &is_null))
342 return false;
343 if (is_null)
344 return true;
345
346 return
347 ReadParam(m, iter, &r->base_time) &&
348 ReadParam(m, iter, &r->proxy_start) &&
349 ReadParam(m, iter, &r->proxy_end) &&
350 ReadParam(m, iter, &r->dns_start) &&
351 ReadParam(m, iter, &r->dns_end) &&
352 ReadParam(m, iter, &r->connect_start) &&
353 ReadParam(m, iter, &r->connect_end) &&
354 ReadParam(m, iter, &r->ssl_start) &&
355 ReadParam(m, iter, &r->ssl_end) &&
356 ReadParam(m, iter, &r->send_start) &&
357 ReadParam(m, iter, &r->send_end) &&
358 ReadParam(m, iter, &r->receive_headers_start) &&
359 ReadParam(m, iter, &r->receive_headers_end);
360 }
361
362 void ParamTraits<webkit_glue::ResourceLoadTimingInfo>::Log(const param_type& p,
363 std::string* l) {
364 l->append("(");
365 LogParam(p.base_time, l);
366 l->append(", ");
367 LogParam(p.proxy_start, l);
368 l->append(", ");
369 LogParam(p.proxy_end, l);
370 l->append(", ");
371 LogParam(p.dns_start, l);
372 l->append(", ");
373 LogParam(p.dns_end, l);
374 l->append(", ");
375 LogParam(p.connect_start, l);
376 l->append(", ");
377 LogParam(p.connect_end, l);
378 l->append(", ");
379 LogParam(p.ssl_start, l);
380 l->append(", ");
381 LogParam(p.ssl_end, l);
382 l->append(", ");
383 LogParam(p.send_start, l);
384 l->append(", ");
385 LogParam(p.send_end, l);
386 l->append(", ");
387 LogParam(p.receive_headers_start, l);
388 l->append(", ");
389 LogParam(p.receive_headers_end, l);
390 l->append(")");
391 }
392
393 void ParamTraits<scoped_refptr<webkit_glue::ResourceDevToolsInfo> >::Write(
394 Message* m, const param_type& p) {
395 WriteParam(m, p.get() != NULL);
396 if (p.get()) {
397 WriteParam(m, p->http_status_code);
398 WriteParam(m, p->http_status_text);
399 WriteParam(m, p->request_headers);
400 WriteParam(m, p->response_headers);
401 }
402 }
403
404 bool ParamTraits<scoped_refptr<webkit_glue::ResourceDevToolsInfo> >::Read(
405 const Message* m, void** iter, param_type* r) {
406 bool has_object;
407 if (!ReadParam(m, iter, &has_object))
408 return false;
409 if (!has_object)
410 return true;
411 *r = new webkit_glue::ResourceDevToolsInfo();
412 return
413 ReadParam(m, iter, &(*r)->http_status_code) &&
414 ReadParam(m, iter, &(*r)->http_status_text) &&
415 ReadParam(m, iter, &(*r)->request_headers) &&
416 ReadParam(m, iter, &(*r)->response_headers);
417 }
418
419 void ParamTraits<scoped_refptr<webkit_glue::ResourceDevToolsInfo> >::Log(
420 const param_type& p, std::string* l) {
421 l->append("(");
422 if (p) {
423 LogParam(p->request_headers, l);
424 l->append(", ");
425 LogParam(p->response_headers, l);
426 }
427 l->append(")");
428 }
429
30 } // namespace IPC 430 } // namespace IPC
OLDNEW
« no previous file with comments | « content/common/common_param_traits.h ('k') | content/common/content_message_generator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698