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 for (std::vector<ExplodedFrameState>::iterator it = state->children.begin();
50 it != state->children.end();
52 RecursivelyRemoveReferrer(&*it);
59 PageState PageState::CreateFromEncodedData(const std::string& data) {
60 return PageState(data);
64 PageState PageState::CreateFromURL(const GURL& url) {
65 ExplodedPageState state;
67 state.top.url_string = state.top.original_url_string =
68 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 = state.top.original_url_string =
82 ToNullableString16(url.possibly_invalid_spec());
84 if (optional_body_data || optional_body_file_path) {
85 state.top.http_body.is_null = false;
86 if (optional_body_data) {
87 ExplodedHttpBodyElement element;
88 element.type = blink::WebHTTPBody::Element::TypeData;
89 element.data = optional_body_data;
90 state.top.http_body.elements.push_back(element);
92 if (optional_body_file_path) {
93 ExplodedHttpBodyElement element;
94 element.type = blink::WebHTTPBody::Element::TypeFile;
96 ToNullableString16(optional_body_file_path->AsUTF8Unsafe());
97 state.top.http_body.elements.push_back(element);
98 state.referenced_files.push_back(element.file_path);
100 state.top.http_body.contains_passwords =
101 body_contains_password_data;
104 return ToPageState(state);
107 PageState::PageState() {
110 bool PageState::IsValid() const {
111 return !data_.empty();
114 bool PageState::Equals(const PageState& other) const {
115 return data_ == other.data_;
118 const std::string& PageState::ToEncodedData() const {
122 std::vector<base::FilePath> PageState::GetReferencedFiles() const {
123 std::vector<base::FilePath> results;
125 ExplodedPageState state;
126 if (DecodePageState(data_, &state))
127 ToFilePathVector(state.referenced_files, &results);
132 PageState PageState::RemovePasswordData() const {
133 ExplodedPageState state;
134 if (!DecodePageState(data_, &state))
135 return PageState(); // Oops!
137 RecursivelyRemovePasswordData(&state.top);
139 return ToPageState(state);
142 PageState PageState::RemoveScrollOffset() const {
143 ExplodedPageState state;
144 if (!DecodePageState(data_, &state))
145 return PageState(); // Oops!
147 RecursivelyRemoveScrollOffset(&state.top);
149 return ToPageState(state);
152 PageState PageState::RemoveReferrer() const {
153 ExplodedPageState state;
154 if (!DecodePageState(data_, &state))
155 return PageState(); // Oops!
157 RecursivelyRemoveReferrer(&state.top);
159 return ToPageState(state);
162 PageState::PageState(const std::string& data)
164 // TODO(darin): Enable this DCHECK once tests have been fixed up to not pass
165 // bogus encoded data to CreateFromEncodedData.
169 } // namespace content