1 // Copyright (c) 2013 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.
5 #include "content/public/common/page_state.h"
7 #include "base/files/file_path.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "content/common/page_state_serialization.h"
14 base::NullableString16
ToNullableString16(const std::string
& utf8
) {
15 return base::NullableString16(base::UTF8ToUTF16(utf8
), false);
18 base::FilePath
ToFilePath(const base::NullableString16
& s
) {
19 return base::FilePath::FromUTF16Unsafe(s
.string());
22 void ToFilePathVector(const std::vector
<base::NullableString16
>& input
,
23 std::vector
<base::FilePath
>* output
) {
25 output
->reserve(input
.size());
26 for (size_t i
= 0; i
< input
.size(); ++i
)
27 output
->push_back(ToFilePath(input
[i
]));
30 PageState
ToPageState(const ExplodedPageState
& state
) {
31 std::string encoded_data
;
32 if (!EncodePageState(state
, &encoded_data
))
35 return PageState::CreateFromEncodedData(encoded_data
);
38 void RecursivelyRemovePasswordData(ExplodedFrameState
* state
) {
39 if (state
->http_body
.contains_passwords
)
40 state
->http_body
= ExplodedHttpBody();
43 void RecursivelyRemoveScrollOffset(ExplodedFrameState
* state
) {
44 state
->scroll_offset
= gfx::Point();
47 void RecursivelyRemoveReferrer(ExplodedFrameState
* state
) {
48 state
->referrer
= base::NullableString16();
49 state
->referrer_policy
= blink::WebReferrerPolicyDefault
;
50 for (std::vector
<ExplodedFrameState
>::iterator it
= state
->children
.begin();
51 it
!= state
->children
.end();
53 RecursivelyRemoveReferrer(&*it
);
60 PageState
PageState::CreateFromEncodedData(const std::string
& data
) {
61 return PageState(data
);
65 PageState
PageState::CreateFromURL(const GURL
& url
) {
66 ExplodedPageState state
;
68 state
.top
.url_string
= ToNullableString16(url
.possibly_invalid_spec());
70 return ToPageState(state
);
74 PageState
PageState::CreateForTesting(
76 bool body_contains_password_data
,
77 const char* optional_body_data
,
78 const base::FilePath
* optional_body_file_path
) {
79 ExplodedPageState state
;
81 state
.top
.url_string
= ToNullableString16(url
.possibly_invalid_spec());
83 if (optional_body_data
|| optional_body_file_path
) {
84 state
.top
.http_body
.is_null
= false;
85 if (optional_body_data
) {
86 ExplodedHttpBodyElement element
;
87 element
.type
= blink::WebHTTPBody::Element::TypeData
;
88 element
.data
= optional_body_data
;
89 state
.top
.http_body
.elements
.push_back(element
);
91 if (optional_body_file_path
) {
92 ExplodedHttpBodyElement element
;
93 element
.type
= blink::WebHTTPBody::Element::TypeFile
;
95 ToNullableString16(optional_body_file_path
->AsUTF8Unsafe());
96 state
.top
.http_body
.elements
.push_back(element
);
97 state
.referenced_files
.push_back(element
.file_path
);
99 state
.top
.http_body
.contains_passwords
=
100 body_contains_password_data
;
103 return ToPageState(state
);
106 PageState::PageState() {
109 bool PageState::IsValid() const {
110 return !data_
.empty();
113 bool PageState::Equals(const PageState
& other
) const {
114 return data_
== other
.data_
;
117 const std::string
& PageState::ToEncodedData() const {
121 std::vector
<base::FilePath
> PageState::GetReferencedFiles() const {
122 std::vector
<base::FilePath
> results
;
124 ExplodedPageState state
;
125 if (DecodePageState(data_
, &state
))
126 ToFilePathVector(state
.referenced_files
, &results
);
131 PageState
PageState::RemovePasswordData() const {
132 ExplodedPageState state
;
133 if (!DecodePageState(data_
, &state
))
134 return PageState(); // Oops!
136 RecursivelyRemovePasswordData(&state
.top
);
138 return ToPageState(state
);
141 PageState
PageState::RemoveScrollOffset() const {
142 ExplodedPageState state
;
143 if (!DecodePageState(data_
, &state
))
144 return PageState(); // Oops!
146 RecursivelyRemoveScrollOffset(&state
.top
);
148 return ToPageState(state
);
151 PageState
PageState::RemoveReferrer() const {
155 ExplodedPageState state
;
156 if (!DecodePageState(data_
, &state
))
157 return PageState(); // Oops!
159 RecursivelyRemoveReferrer(&state
.top
);
161 return ToPageState(state
);
164 PageState::PageState(const std::string
& data
)
166 // TODO(darin): Enable this DCHECK once tests have been fixed up to not pass
167 // bogus encoded data to CreateFromEncodedData.
171 } // namespace content