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

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

Issue 1987053002: Deduplicating code performing WebHTTPBody::Element conversions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: s/data/test_body/g Created 4 years, 7 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
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>
11 11
12 #include "base/pickle.h" 12 #include "base/pickle.h"
13 #include "base/strings/nullable_string16.h"
13 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
16 #include "build/build_config.h" 17 #include "build/build_config.h"
17 #include "content/common/resource_request_body.h" 18 #include "content/common/resource_request_body.h"
18 #include "ui/display/display.h" 19 #include "ui/display/display.h"
19 #include "ui/display/screen.h" 20 #include "ui/display/screen.h"
20 21
21 namespace content { 22 namespace content {
22 namespace { 23 namespace {
23 24
24 #if defined(OS_ANDROID) 25 #if defined(OS_ANDROID)
25 float g_device_scale_factor_for_testing = 0.0; 26 float g_device_scale_factor_for_testing = 0.0;
26 #endif 27 #endif
27 28
28 //----------------------------------------------------------------------------- 29 //-----------------------------------------------------------------------------
29 30
30 void AppendDataToHttpBody(ExplodedHttpBody* http_body, const char* data, 31 void AppendDataToHttpBody(ExplodedHttpBody* http_body, const char* data,
31 int data_length) { 32 int data_length) {
32 ExplodedHttpBodyElement element; 33 ExplodedHttpBodyElement element;
33 element.type = blink::WebHTTPBody::Element::TypeData; 34 element.SetToBytes(data, data_length);
34 element.data.assign(data, data_length);
35 http_body->elements.push_back(element); 35 http_body->elements.push_back(element);
36 } 36 }
37 37
38 void AppendFileRangeToHttpBody(ExplodedHttpBody* http_body, 38 void AppendFileRangeToHttpBody(ExplodedHttpBody* http_body,
39 const base::NullableString16& file_path, 39 const base::NullableString16& file_path,
40 int file_start, 40 int file_start,
41 int file_length, 41 int file_length,
42 double file_modification_time) { 42 double file_modification_time) {
43 ExplodedHttpBodyElement element; 43 ExplodedHttpBodyElement element;
44 element.type = blink::WebHTTPBody::Element::TypeFile; 44 element.SetToFilePathRange(
45 element.file_path = file_path; 45 base::FilePath::FromUTF16Unsafe(file_path.string()),
46 element.file_start = file_start; 46 static_cast<uint64_t>(file_start), static_cast<uint64_t>(file_length),
47 element.file_length = file_length; 47 base::Time::FromDoubleT(file_modification_time));
48 element.file_modification_time = file_modification_time;
49 http_body->elements.push_back(element); 48 http_body->elements.push_back(element);
50 } 49 }
51 50
52 void AppendURLRangeToHttpBody(ExplodedHttpBody* http_body, 51 void AppendURLRangeToHttpBody(ExplodedHttpBody* http_body,
53 const GURL& url, 52 const GURL& url,
54 int file_start, 53 int file_start,
55 int file_length, 54 int file_length,
56 double file_modification_time) { 55 double file_modification_time) {
57 ExplodedHttpBodyElement element; 56 ExplodedHttpBodyElement element;
58 element.type = blink::WebHTTPBody::Element::TypeFileSystemURL; 57 element.SetToFileSystemUrlRange(
59 element.filesystem_url = url; 58 url, static_cast<uint64_t>(file_start),
60 element.file_start = file_start; 59 static_cast<uint64_t>(file_length),
61 element.file_length = file_length; 60 base::Time::FromDoubleT(file_modification_time));
62 element.file_modification_time = file_modification_time;
63 http_body->elements.push_back(element); 61 http_body->elements.push_back(element);
64 } 62 }
65 63
66 void AppendBlobToHttpBody(ExplodedHttpBody* http_body, 64 void AppendBlobToHttpBody(ExplodedHttpBody* http_body,
67 const std::string& uuid) { 65 const std::string& uuid) {
68 ExplodedHttpBodyElement element; 66 ExplodedHttpBodyElement element;
69 element.type = blink::WebHTTPBody::Element::TypeBlob; 67 element.SetToBlob(uuid);
70 element.blob_uuid = uuid;
71 http_body->elements.push_back(element); 68 http_body->elements.push_back(element);
72 } 69 }
73 70
74 //---------------------------------------------------------------------------- 71 //----------------------------------------------------------------------------
75 72
76 void AppendReferencedFilesFromHttpBody( 73 void AppendReferencedFilesFromHttpBody(
77 const std::vector<ExplodedHttpBodyElement>& elements, 74 const std::vector<ExplodedHttpBodyElement>& elements,
78 std::vector<base::NullableString16>* referenced_files) { 75 std::vector<base::NullableString16>* referenced_files) {
79 for (size_t i = 0; i < elements.size(); ++i) { 76 for (size_t i = 0; i < elements.size(); ++i) {
80 if (elements[i].type == blink::WebHTTPBody::Element::TypeFile) 77 if (elements[i].type() == ExplodedHttpBodyElement::TYPE_FILE)
81 referenced_files->push_back(elements[i].file_path); 78 referenced_files->push_back(
79 base::NullableString16(elements[i].path().AsUTF16Unsafe(), false));
82 } 80 }
83 } 81 }
84 82
85 bool AppendReferencedFilesFromDocumentState( 83 bool AppendReferencedFilesFromDocumentState(
86 const std::vector<base::NullableString16>& document_state, 84 const std::vector<base::NullableString16>& document_state,
87 std::vector<base::NullableString16>* referenced_files) { 85 std::vector<base::NullableString16>* referenced_files) {
88 if (document_state.empty()) 86 if (document_state.empty())
89 return true; 87 return true;
90 88
91 // This algorithm is adapted from Blink's core/html/FormController.cpp code. 89 // This algorithm is adapted from Blink's core/html/FormController.cpp code.
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 // Writes an ExplodedHttpBody object into a SerializeObject for serialization. 398 // Writes an ExplodedHttpBody object into a SerializeObject for serialization.
401 void WriteHttpBody(const ExplodedHttpBody& http_body, SerializeObject* obj) { 399 void WriteHttpBody(const ExplodedHttpBody& http_body, SerializeObject* obj) {
402 WriteBoolean(!http_body.is_null, obj); 400 WriteBoolean(!http_body.is_null, obj);
403 401
404 if (http_body.is_null) 402 if (http_body.is_null)
405 return; 403 return;
406 404
407 WriteAndValidateVectorSize(http_body.elements, obj); 405 WriteAndValidateVectorSize(http_body.elements, obj);
408 for (size_t i = 0; i < http_body.elements.size(); ++i) { 406 for (size_t i = 0; i < http_body.elements.size(); ++i) {
409 const ExplodedHttpBodyElement& element = http_body.elements[i]; 407 const ExplodedHttpBodyElement& element = http_body.elements[i];
410 WriteInteger(element.type, obj); 408 switch (element.type()) {
411 if (element.type == blink::WebHTTPBody::Element::TypeData) { 409 case ExplodedHttpBodyElement::TYPE_BYTES:
412 WriteData(element.data.data(), static_cast<int>(element.data.size()), 410 WriteInteger(blink::WebHTTPBody::Element::TypeData, obj);
413 obj); 411 WriteData(element.bytes(), static_cast<int>(element.length()), obj);
414 } else if (element.type == blink::WebHTTPBody::Element::TypeFile) { 412 break;
415 WriteString(element.file_path, obj); 413 case ExplodedHttpBodyElement::TYPE_FILE:
416 WriteInteger64(element.file_start, obj); 414 WriteInteger(blink::WebHTTPBody::Element::TypeFile, obj);
417 WriteInteger64(element.file_length, obj); 415 WriteString(
418 WriteReal(element.file_modification_time, obj); 416 base::NullableString16(element.path().AsUTF16Unsafe(), false), obj);
419 } else if (element.type == 417 WriteInteger64(static_cast<int64_t>(element.offset()), obj);
420 blink::WebHTTPBody::Element::TypeFileSystemURL) { 418 WriteInteger64(static_cast<int64_t>(element.length()), obj);
421 WriteGURL(element.filesystem_url, obj); 419 WriteReal(element.expected_modification_time().ToDoubleT(), obj);
422 WriteInteger64(element.file_start, obj); 420 break;
423 WriteInteger64(element.file_length, obj); 421 case ExplodedHttpBodyElement::TYPE_FILE_FILESYSTEM:
424 WriteReal(element.file_modification_time, obj); 422 WriteInteger(blink::WebHTTPBody::Element::TypeFileSystemURL, obj);
425 } else { 423 WriteGURL(element.filesystem_url(), obj);
426 DCHECK(element.type == blink::WebHTTPBody::Element::TypeBlob); 424 WriteInteger64(static_cast<int64_t>(element.offset()), obj);
427 WriteStdString(element.blob_uuid, obj); 425 WriteInteger64(static_cast<int64_t>(element.length()), obj);
426 WriteReal(element.expected_modification_time().ToDoubleT(), obj);
427 break;
428 case ExplodedHttpBodyElement::TYPE_BLOB:
429 WriteInteger(blink::WebHTTPBody::Element::TypeBlob, obj);
430 WriteStdString(element.blob_uuid(), obj);
431 break;
432 case ExplodedHttpBodyElement::TYPE_BYTES_DESCRIPTION:
433 case ExplodedHttpBodyElement::TYPE_DISK_CACHE_ENTRY:
434 default:
435 NOTREACHED();
436 continue;
428 } 437 }
429 } 438 }
430 WriteInteger64(http_body.identifier, obj); 439 WriteInteger64(http_body.identifier, obj);
431 WriteBoolean(http_body.contains_passwords, obj); 440 WriteBoolean(http_body.contains_passwords, obj);
432 } 441 }
433 442
434 void ReadHttpBody(SerializeObject* obj, ExplodedHttpBody* http_body) { 443 void ReadHttpBody(SerializeObject* obj, ExplodedHttpBody* http_body) {
435 // An initial boolean indicates if we have an HTTP body. 444 // An initial boolean indicates if we have an HTTP body.
436 if (!ReadBoolean(obj)) 445 if (!ReadBoolean(obj))
437 return; 446 return;
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
660 669
661 // De-dupe 670 // De-dupe
662 state->referenced_files.erase( 671 state->referenced_files.erase(
663 std::unique(state->referenced_files.begin(), 672 std::unique(state->referenced_files.begin(),
664 state->referenced_files.end()), 673 state->referenced_files.end()),
665 state->referenced_files.end()); 674 state->referenced_files.end());
666 } 675 }
667 676
668 } // namespace 677 } // namespace
669 678
670 ExplodedHttpBodyElement::ExplodedHttpBodyElement()
671 : type(blink::WebHTTPBody::Element::TypeData),
672 file_start(0),
673 file_length(-1),
674 file_modification_time(std::numeric_limits<double>::quiet_NaN()) {
675 }
676
677 ExplodedHttpBodyElement::ExplodedHttpBodyElement(
678 const ExplodedHttpBodyElement& other) = default;
679
680 ExplodedHttpBodyElement::~ExplodedHttpBodyElement() {
681 }
682
683 ExplodedHttpBody::ExplodedHttpBody() 679 ExplodedHttpBody::ExplodedHttpBody()
684 : identifier(0), 680 : identifier(0),
685 contains_passwords(false), 681 contains_passwords(false),
686 is_null(true) { 682 is_null(true) {
687 } 683 }
688 684
689 ExplodedHttpBody::~ExplodedHttpBody() { 685 ExplodedHttpBody::~ExplodedHttpBody() {
690 } 686 }
691 687
692 ExplodedFrameState::ExplodedFrameState() 688 ExplodedFrameState::ExplodedFrameState()
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
751 return true; 747 return true;
752 } 748 }
753 749
754 bool GeneratePostData(const ExplodedHttpBody& exploded, 750 bool GeneratePostData(const ExplodedHttpBody& exploded,
755 ResourceRequestBody* http_body) { 751 ResourceRequestBody* http_body) {
756 if (exploded.is_null) 752 if (exploded.is_null)
757 return false; 753 return false;
758 754
759 http_body->set_identifier(exploded.identifier); 755 http_body->set_identifier(exploded.identifier);
760 for (auto element : exploded.elements) 756 for (auto element : exploded.elements)
761 http_body->AppendExplodedHTTPBodyElement(element); 757 http_body->elements_mutable()->push_back(element);
762 758
763 return true; 759 return true;
764 } 760 }
765 761
766 #if defined(OS_ANDROID) 762 #if defined(OS_ANDROID)
767 bool DecodePageStateWithDeviceScaleFactorForTesting( 763 bool DecodePageStateWithDeviceScaleFactorForTesting(
768 const std::string& encoded, 764 const std::string& encoded,
769 float device_scale_factor, 765 float device_scale_factor,
770 ExplodedPageState* exploded) { 766 ExplodedPageState* exploded) {
771 g_device_scale_factor_for_testing = device_scale_factor; 767 g_device_scale_factor_for_testing = device_scale_factor;
772 bool rv = DecodePageState(encoded, exploded); 768 bool rv = DecodePageState(encoded, exploded);
773 g_device_scale_factor_for_testing = 0.0; 769 g_device_scale_factor_for_testing = 0.0;
774 return rv; 770 return rv;
775 } 771 }
776 #endif 772 #endif
777 773
778 } // namespace content 774 } // namespace content
OLDNEW
« no previous file with comments | « content/common/page_state_serialization.h ('k') | content/common/page_state_serialization_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698