page_state.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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. 4 5#include "content/public/common/page_state.h" 6 7#include "base/files/file_path.h" 8#include "base/strings/utf_string_conversions.h" 9#include "content/common/page_state_serialization.h" 10 11namespace content { 12namespace { 13 14base::NullableString16 ToNullableString16(const std::string& utf8) { 15 return base::NullableString16(base::UTF8ToUTF16(utf8), false); 16} 17 18base::FilePath ToFilePath(const base::NullableString16& s) { 19 return base::FilePath::FromUTF16Unsafe(s.string()); 20} 21 22void ToFilePathVector(const std::vector<base::NullableString16>& input, 23 std::vector<base::FilePath>* output) { 24 output->clear(); 25 output->reserve(input.size()); 26 for (size_t i = 0; i < input.size(); ++i) 27 output->push_back(ToFilePath(input[i])); 28} 29 30PageState ToPageState(const ExplodedPageState& state) { 31 std::string encoded_data; 32 if (!EncodePageState(state, &encoded_data)) 33 return PageState(); 34 35 return PageState::CreateFromEncodedData(encoded_data); 36} 37 38void RecursivelyRemovePasswordData(ExplodedFrameState* state) { 39 if (state->http_body.contains_passwords) 40 state->http_body = ExplodedHttpBody(); 41} 42 43void RecursivelyRemoveScrollOffset(ExplodedFrameState* state) { 44 state->scroll_offset = gfx::Point(); 45} 46 47void 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(); 52 ++it) { 53 RecursivelyRemoveReferrer(&*it); 54 } 55} 56 57} // namespace 58 59// static 60PageState PageState::CreateFromEncodedData(const std::string& data) { 61 return PageState(data); 62} 63 64// static 65PageState PageState::CreateFromURL(const GURL& url) { 66 ExplodedPageState state; 67 68 state.top.url_string = ToNullableString16(url.possibly_invalid_spec()); 69 70 return ToPageState(state); 71} 72 73// static 74PageState PageState::CreateForTesting( 75 const GURL& url, 76 bool body_contains_password_data, 77 const char* optional_body_data, 78 const base::FilePath* optional_body_file_path) { 79 ExplodedPageState state; 80 81 state.top.url_string = ToNullableString16(url.possibly_invalid_spec()); 82 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); 90 } 91 if (optional_body_file_path) { 92 ExplodedHttpBodyElement element; 93 element.type = blink::WebHTTPBody::Element::TypeFile; 94 element.file_path = 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); 98 } 99 state.top.http_body.contains_passwords = 100 body_contains_password_data; 101 } 102 103 return ToPageState(state); 104} 105 106PageState::PageState() { 107} 108 109bool PageState::IsValid() const { 110 return !data_.empty(); 111} 112 113bool PageState::Equals(const PageState& other) const { 114 return data_ == other.data_; 115} 116 117const std::string& PageState::ToEncodedData() const { 118 return data_; 119} 120 121std::vector<base::FilePath> PageState::GetReferencedFiles() const { 122 std::vector<base::FilePath> results; 123 124 ExplodedPageState state; 125 if (DecodePageState(data_, &state)) 126 ToFilePathVector(state.referenced_files, &results); 127 128 return results; 129} 130 131PageState PageState::RemovePasswordData() const { 132 ExplodedPageState state; 133 if (!DecodePageState(data_, &state)) 134 return PageState(); // Oops! 135 136 RecursivelyRemovePasswordData(&state.top); 137 138 return ToPageState(state); 139} 140 141PageState PageState::RemoveScrollOffset() const { 142 ExplodedPageState state; 143 if (!DecodePageState(data_, &state)) 144 return PageState(); // Oops! 145 146 RecursivelyRemoveScrollOffset(&state.top); 147 148 return ToPageState(state); 149} 150 151PageState PageState::RemoveReferrer() const { 152 if (data_.empty()) 153 return *this; 154 155 ExplodedPageState state; 156 if (!DecodePageState(data_, &state)) 157 return PageState(); // Oops! 158 159 RecursivelyRemoveReferrer(&state.top); 160 161 return ToPageState(state); 162} 163 164PageState::PageState(const std::string& data) 165 : data_(data) { 166 // TODO(darin): Enable this DCHECK once tests have been fixed up to not pass 167 // bogus encoded data to CreateFromEncodedData. 168 //DCHECK(IsValid()); 169} 170 171} // namespace content 172