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

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

Issue 2038233002: Using ResourceRequestBody as the type of HTTP body outside of //content. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@make-resource-request-body-public
Patch Set: Rebasing... Created 4 years, 6 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
« no previous file with comments | « content/common/page_state_serialization.h ('k') | content/content_common.gypi » ('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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/page_state_serialization.h" 5 #include "content/common/page_state_serialization.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <limits> 10 #include <limits>
(...skipping 10 matching lines...) Expand all
21 21
22 namespace content { 22 namespace content {
23 namespace { 23 namespace {
24 24
25 #if defined(OS_ANDROID) 25 #if defined(OS_ANDROID)
26 float g_device_scale_factor_for_testing = 0.0; 26 float g_device_scale_factor_for_testing = 0.0;
27 #endif 27 #endif
28 28
29 //----------------------------------------------------------------------------- 29 //-----------------------------------------------------------------------------
30 30
31 void AppendDataToHttpBody(ExplodedHttpBody* http_body, const char* data, 31 void AppendDataToRequestBody(
32 int data_length) { 32 const scoped_refptr<ResourceRequestBodyImpl>& request_body,
33 http_body->request_body->AppendBytes(data, data_length); 33 const char* data,
34 int data_length) {
35 request_body->AppendBytes(data, data_length);
34 } 36 }
35 37
36 void AppendFileRangeToHttpBody(ExplodedHttpBody* http_body, 38 void AppendFileRangeToRequestBody(
37 const base::NullableString16& file_path, 39 const scoped_refptr<ResourceRequestBodyImpl>& request_body,
38 int file_start, 40 const base::NullableString16& file_path,
39 int file_length, 41 int file_start,
40 double file_modification_time) { 42 int file_length,
41 http_body->request_body->AppendFileRange( 43 double file_modification_time) {
44 request_body->AppendFileRange(
42 base::FilePath::FromUTF16Unsafe(file_path.string()), 45 base::FilePath::FromUTF16Unsafe(file_path.string()),
43 static_cast<uint64_t>(file_start), static_cast<uint64_t>(file_length), 46 static_cast<uint64_t>(file_start), static_cast<uint64_t>(file_length),
44 base::Time::FromDoubleT(file_modification_time)); 47 base::Time::FromDoubleT(file_modification_time));
45 } 48 }
46 49
47 void AppendURLRangeToHttpBody(ExplodedHttpBody* http_body, 50 void AppendURLRangeToRequestBody(
48 const GURL& url, 51 const scoped_refptr<ResourceRequestBodyImpl>& request_body,
49 int file_start, 52 const GURL& url,
50 int file_length, 53 int file_start,
51 double file_modification_time) { 54 int file_length,
52 http_body->request_body->AppendFileSystemFileRange( 55 double file_modification_time) {
56 request_body->AppendFileSystemFileRange(
53 url, static_cast<uint64_t>(file_start), 57 url, static_cast<uint64_t>(file_start),
54 static_cast<uint64_t>(file_length), 58 static_cast<uint64_t>(file_length),
55 base::Time::FromDoubleT(file_modification_time)); 59 base::Time::FromDoubleT(file_modification_time));
56 } 60 }
57 61
58 void AppendBlobToHttpBody(ExplodedHttpBody* http_body, 62 void AppendBlobToRequestBody(
59 const std::string& uuid) { 63 const scoped_refptr<ResourceRequestBodyImpl>& request_body,
60 http_body->request_body->AppendBlob(uuid); 64 const std::string& uuid) {
65 request_body->AppendBlob(uuid);
61 } 66 }
62 67
63 //---------------------------------------------------------------------------- 68 //----------------------------------------------------------------------------
64 69
65 void AppendReferencedFilesFromHttpBody( 70 void AppendReferencedFilesFromHttpBody(
66 const std::vector<ResourceRequestBodyImpl::Element>& elements, 71 const std::vector<ResourceRequestBodyImpl::Element>& elements,
67 std::vector<base::NullableString16>* referenced_files) { 72 std::vector<base::NullableString16>* referenced_files) {
68 for (size_t i = 0; i < elements.size(); ++i) { 73 for (size_t i = 0; i < elements.size(); ++i) {
69 if (elements[i].type() == ResourceRequestBodyImpl::Element::TYPE_FILE) 74 if (elements[i].type() == ResourceRequestBodyImpl::Element::TYPE_FILE)
70 referenced_files->push_back( 75 referenced_files->push_back(
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 void ReadStringVector(SerializeObject* obj, 385 void ReadStringVector(SerializeObject* obj,
381 std::vector<base::NullableString16>* result) { 386 std::vector<base::NullableString16>* result) {
382 size_t num_elements = 387 size_t num_elements =
383 ReadAndValidateVectorSize(obj, sizeof(base::NullableString16)); 388 ReadAndValidateVectorSize(obj, sizeof(base::NullableString16));
384 389
385 result->resize(num_elements); 390 result->resize(num_elements);
386 for (size_t i = 0; i < num_elements; ++i) 391 for (size_t i = 0; i < num_elements; ++i)
387 (*result)[i] = ReadString(obj); 392 (*result)[i] = ReadString(obj);
388 } 393 }
389 394
390 // Writes an ExplodedHttpBody object into a SerializeObject for serialization. 395 void WriteResourceRequestBody(const ResourceRequestBodyImpl& request_body,
391 void WriteHttpBody(const ExplodedHttpBody& http_body, SerializeObject* obj) { 396 SerializeObject* obj) {
392 bool is_null = http_body.request_body == nullptr; 397 WriteAndValidateVectorSize(*request_body.elements(), obj);
393 WriteBoolean(!is_null, obj); 398 for (const auto& element : *request_body.elements()) {
394 if (is_null)
395 return;
396
397 WriteAndValidateVectorSize(*http_body.request_body->elements(), obj);
398 for (const auto& element : *http_body.request_body->elements()) {
399 switch (element.type()) { 399 switch (element.type()) {
400 case ResourceRequestBodyImpl::Element::TYPE_BYTES: 400 case ResourceRequestBodyImpl::Element::TYPE_BYTES:
401 WriteInteger(blink::WebHTTPBody::Element::TypeData, obj); 401 WriteInteger(blink::WebHTTPBody::Element::TypeData, obj);
402 WriteData(element.bytes(), static_cast<int>(element.length()), obj); 402 WriteData(element.bytes(), static_cast<int>(element.length()), obj);
403 break; 403 break;
404 case ResourceRequestBodyImpl::Element::TYPE_FILE: 404 case ResourceRequestBodyImpl::Element::TYPE_FILE:
405 WriteInteger(blink::WebHTTPBody::Element::TypeFile, obj); 405 WriteInteger(blink::WebHTTPBody::Element::TypeFile, obj);
406 WriteString( 406 WriteString(
407 base::NullableString16(element.path().AsUTF16Unsafe(), false), obj); 407 base::NullableString16(element.path().AsUTF16Unsafe(), false), obj);
408 WriteInteger64(static_cast<int64_t>(element.offset()), obj); 408 WriteInteger64(static_cast<int64_t>(element.offset()), obj);
(...skipping 11 matching lines...) Expand all
420 WriteInteger(blink::WebHTTPBody::Element::TypeBlob, obj); 420 WriteInteger(blink::WebHTTPBody::Element::TypeBlob, obj);
421 WriteStdString(element.blob_uuid(), obj); 421 WriteStdString(element.blob_uuid(), obj);
422 break; 422 break;
423 case ResourceRequestBodyImpl::Element::TYPE_BYTES_DESCRIPTION: 423 case ResourceRequestBodyImpl::Element::TYPE_BYTES_DESCRIPTION:
424 case ResourceRequestBodyImpl::Element::TYPE_DISK_CACHE_ENTRY: 424 case ResourceRequestBodyImpl::Element::TYPE_DISK_CACHE_ENTRY:
425 default: 425 default:
426 NOTREACHED(); 426 NOTREACHED();
427 continue; 427 continue;
428 } 428 }
429 } 429 }
430 WriteInteger64(http_body.request_body->identifier(), obj); 430 WriteInteger64(request_body.identifier(), obj);
431 WriteBoolean(http_body.contains_passwords, obj);
432 } 431 }
433 432
434 void ReadHttpBody(SerializeObject* obj, ExplodedHttpBody* http_body) { 433 void ReadResourceRequestBody(
435 // An initial boolean indicates if we have an HTTP body. 434 SerializeObject* obj,
436 if (!ReadBoolean(obj)) 435 const scoped_refptr<ResourceRequestBodyImpl>& request_body) {
437 return;
438
439 http_body->request_body = new ResourceRequestBodyImpl();
440 int num_elements = ReadInteger(obj); 436 int num_elements = ReadInteger(obj);
441 for (int i = 0; i < num_elements; ++i) { 437 for (int i = 0; i < num_elements; ++i) {
442 int type = ReadInteger(obj); 438 int type = ReadInteger(obj);
443 if (type == blink::WebHTTPBody::Element::TypeData) { 439 if (type == blink::WebHTTPBody::Element::TypeData) {
444 const void* data; 440 const void* data;
445 int length = -1; 441 int length = -1;
446 ReadData(obj, &data, &length); 442 ReadData(obj, &data, &length);
447 if (length >= 0) { 443 if (length >= 0) {
448 AppendDataToHttpBody(http_body, static_cast<const char*>(data), 444 AppendDataToRequestBody(request_body, static_cast<const char*>(data),
449 length); 445 length);
450 } 446 }
451 } else if (type == blink::WebHTTPBody::Element::TypeFile) { 447 } else if (type == blink::WebHTTPBody::Element::TypeFile) {
452 base::NullableString16 file_path = ReadString(obj); 448 base::NullableString16 file_path = ReadString(obj);
453 int64_t file_start = ReadInteger64(obj); 449 int64_t file_start = ReadInteger64(obj);
454 int64_t file_length = ReadInteger64(obj); 450 int64_t file_length = ReadInteger64(obj);
455 double file_modification_time = ReadReal(obj); 451 double file_modification_time = ReadReal(obj);
456 AppendFileRangeToHttpBody(http_body, file_path, file_start, file_length, 452 AppendFileRangeToRequestBody(request_body, file_path, file_start,
457 file_modification_time); 453 file_length, file_modification_time);
458 } else if (type == blink::WebHTTPBody::Element::TypeFileSystemURL) { 454 } else if (type == blink::WebHTTPBody::Element::TypeFileSystemURL) {
459 GURL url = ReadGURL(obj); 455 GURL url = ReadGURL(obj);
460 int64_t file_start = ReadInteger64(obj); 456 int64_t file_start = ReadInteger64(obj);
461 int64_t file_length = ReadInteger64(obj); 457 int64_t file_length = ReadInteger64(obj);
462 double file_modification_time = ReadReal(obj); 458 double file_modification_time = ReadReal(obj);
463 AppendURLRangeToHttpBody(http_body, url, file_start, file_length, 459 AppendURLRangeToRequestBody(request_body, url, file_start, file_length,
464 file_modification_time); 460 file_modification_time);
465 } else if (type == blink::WebHTTPBody::Element::TypeBlob) { 461 } else if (type == blink::WebHTTPBody::Element::TypeBlob) {
466 if (obj->version >= 16) { 462 if (obj->version >= 16) {
467 std::string blob_uuid = ReadStdString(obj); 463 std::string blob_uuid = ReadStdString(obj);
468 AppendBlobToHttpBody(http_body, blob_uuid); 464 AppendBlobToRequestBody(request_body, blob_uuid);
469 } else { 465 } else {
470 ReadGURL(obj); // Skip the obsolete blob url value. 466 ReadGURL(obj); // Skip the obsolete blob url value.
471 } 467 }
472 } 468 }
473 } 469 }
474 http_body->request_body->set_identifier(ReadInteger64(obj)); 470 request_body->set_identifier(ReadInteger64(obj));
471 }
472
473 // Writes an ExplodedHttpBody object into a SerializeObject for serialization.
474 void WriteHttpBody(const ExplodedHttpBody& http_body, SerializeObject* obj) {
475 bool is_null = http_body.request_body == nullptr;
476 WriteBoolean(!is_null, obj);
477 if (is_null)
478 return;
479
480 WriteResourceRequestBody(*http_body.request_body, obj);
481 WriteBoolean(http_body.contains_passwords, obj);
482 }
483
484 void ReadHttpBody(SerializeObject* obj, ExplodedHttpBody* http_body) {
485 // An initial boolean indicates if we have an HTTP body.
486 if (!ReadBoolean(obj))
487 return;
488
489 http_body->request_body = new ResourceRequestBodyImpl();
490 ReadResourceRequestBody(obj, http_body->request_body);
475 491
476 if (obj->version >= 12) 492 if (obj->version >= 12)
477 http_body->contains_passwords = ReadBoolean(obj); 493 http_body->contains_passwords = ReadBoolean(obj);
478 } 494 }
479 495
480 // Writes the ExplodedFrameState data into the SerializeObject object for 496 // Writes the ExplodedFrameState data into the SerializeObject object for
481 // serialization. 497 // serialization.
482 void WriteFrameState( 498 void WriteFrameState(
483 const ExplodedFrameState& state, SerializeObject* obj, bool is_top) { 499 const ExplodedFrameState& state, SerializeObject* obj, bool is_top) {
484 // WARNING: This data may be persisted for later use. As such, care must be 500 // WARNING: This data may be persisted for later use. As such, care must be
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
736 #if defined(OS_ANDROID) 752 #if defined(OS_ANDROID)
737 bool DecodePageStateWithDeviceScaleFactorForTesting( 753 bool DecodePageStateWithDeviceScaleFactorForTesting(
738 const std::string& encoded, 754 const std::string& encoded,
739 float device_scale_factor, 755 float device_scale_factor,
740 ExplodedPageState* exploded) { 756 ExplodedPageState* exploded) {
741 g_device_scale_factor_for_testing = device_scale_factor; 757 g_device_scale_factor_for_testing = device_scale_factor;
742 bool rv = DecodePageState(encoded, exploded); 758 bool rv = DecodePageState(encoded, exploded);
743 g_device_scale_factor_for_testing = 0.0; 759 g_device_scale_factor_for_testing = 0.0;
744 return rv; 760 return rv;
745 } 761 }
762
763 scoped_refptr<ResourceRequestBodyImpl> DecodeResourceRequestBody(
764 const char* data,
765 size_t size) {
766 scoped_refptr<ResourceRequestBodyImpl> result = new ResourceRequestBodyImpl();
767 SerializeObject obj(data, static_cast<int>(size));
768 ReadResourceRequestBody(&obj, result);
769 return obj.parse_error ? nullptr : result;
770 }
771
772 std::string EncodeResourceRequestBody(
773 const ResourceRequestBodyImpl& resource_request_body) {
774 SerializeObject obj;
775 obj.version = kCurrentVersion;
776 WriteResourceRequestBody(resource_request_body, &obj);
777 return obj.GetAsString();
778 }
779
746 #endif 780 #endif
747 781
748 } // namespace content 782 } // namespace content
OLDNEW
« no previous file with comments | « content/common/page_state_serialization.h ('k') | content/content_common.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698