Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/public/common/page_state.h" | 5 #include "content/public/common/page_state.h" |
| 6 | 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 | |
| 7 namespace content { | 11 namespace content { |
| 12 namespace { | |
| 13 | |
| 14 base::NullableString16 ToNullableString16(const std::string& utf8) { | |
| 15 return base::NullableString16(UTF8ToUTF16(utf8), false); | |
| 16 } | |
| 17 | |
| 18 base::FilePath ToFilePath(const base::NullableString16& s) { | |
| 19 return base::FilePath::FromUTF8Unsafe(UTF16ToUTF8(s.string())); | |
| 20 } | |
| 21 | |
| 22 void ToFilePathVector(const std::vector<base::NullableString16>& input, | |
| 23 std::vector<base::FilePath>* output) { | |
| 24 output->reserve(input.size()); | |
|
Tom Sepez
2013/06/24 17:19:57
nit: again, we assume that the callers only pass e
| |
| 25 for (size_t i = 0; i < input.size(); ++i) | |
| 26 output->push_back(ToFilePath(input[i])); | |
| 27 } | |
| 28 | |
| 29 PageState ToPageState(const ExplodedPageState& state) { | |
| 30 std::string encoded_data; | |
| 31 if (!EncodePageState(state, &encoded_data)) | |
| 32 return PageState(); | |
| 33 | |
| 34 return PageState::CreateFromEncodedData(encoded_data); | |
| 35 } | |
| 36 | |
| 37 void RecursivelyRemovePasswordData(ExplodedFrameState* state) { | |
| 38 if (state->http_body.contains_passwords) | |
| 39 state->http_body = ExplodedHttpBody(); | |
| 40 } | |
| 41 | |
| 42 void RecursivelyRemoveScrollOffset(ExplodedFrameState* state) { | |
| 43 state->scroll_offset = gfx::Point(); | |
| 44 } | |
| 45 | |
| 46 } // namespace | |
| 8 | 47 |
| 9 // static | 48 // static |
| 10 PageState PageState::CreateFromEncodedData(const std::string& data) { | 49 PageState PageState::CreateFromEncodedData(const std::string& data) { |
| 11 return PageState(data); | 50 return PageState(data); |
| 12 } | 51 } |
| 13 | 52 |
| 53 // static | |
| 54 PageState PageState::CreateFromURL(const GURL& url) { | |
| 55 ExplodedPageState state; | |
| 56 | |
| 57 state.top.url_string = state.top.original_url_string = | |
| 58 ToNullableString16(url.possibly_invalid_spec()); | |
| 59 | |
| 60 return ToPageState(state); | |
| 61 } | |
| 62 | |
| 63 // static | |
| 64 PageState PageState::CreateForTesting( | |
| 65 const GURL& url, | |
| 66 bool body_contains_password_data, | |
| 67 const char* optional_body_data, | |
| 68 const base::FilePath* optional_body_file_path) { | |
| 69 ExplodedPageState state; | |
| 70 | |
| 71 state.top.url_string = state.top.original_url_string = | |
| 72 ToNullableString16(url.possibly_invalid_spec()); | |
| 73 | |
| 74 if (optional_body_data || optional_body_file_path) { | |
| 75 state.top.http_body.is_null = false; | |
| 76 if (optional_body_data) { | |
| 77 ExplodedHttpBodyElement element; | |
| 78 element.type = WebKit::WebHTTPBody::Element::TypeData; | |
| 79 element.data = optional_body_data; | |
| 80 state.top.http_body.elements.push_back(element); | |
| 81 } | |
| 82 if (optional_body_file_path) { | |
| 83 ExplodedHttpBodyElement element; | |
| 84 element.type = WebKit::WebHTTPBody::Element::TypeFile; | |
| 85 element.file_path = | |
| 86 ToNullableString16(optional_body_file_path->AsUTF8Unsafe()); | |
| 87 state.top.http_body.elements.push_back(element); | |
| 88 state.referenced_files.push_back(element.file_path); | |
| 89 } | |
| 90 state.top.http_body.contains_passwords = | |
| 91 body_contains_password_data; | |
| 92 } | |
| 93 | |
| 94 return ToPageState(state); | |
| 95 } | |
| 96 | |
| 14 PageState::PageState() { | 97 PageState::PageState() { |
| 15 } | 98 } |
| 16 | 99 |
| 17 bool PageState::IsValid() const { | 100 bool PageState::IsValid() const { |
| 18 return !data_.empty(); | 101 return !data_.empty(); |
| 19 } | 102 } |
| 20 | 103 |
| 21 bool PageState::Equals(const PageState& other) const { | 104 bool PageState::Equals(const PageState& other) const { |
| 22 return data_ == other.data_; | 105 return data_ == other.data_; |
| 23 } | 106 } |
| 24 | 107 |
| 25 const std::string& PageState::ToEncodedData() const { | 108 const std::string& PageState::ToEncodedData() const { |
| 26 return data_; | 109 return data_; |
| 27 } | 110 } |
| 28 | 111 |
| 112 std::vector<base::FilePath> PageState::GetReferencedFiles() const { | |
| 113 std::vector<base::FilePath> results; | |
| 114 | |
| 115 ExplodedPageState state; | |
| 116 if (DecodePageState(data_, &state)) | |
| 117 ToFilePathVector(state.referenced_files, &results); | |
| 118 | |
| 119 return results; | |
| 120 } | |
| 121 | |
| 122 PageState PageState::RemovePasswordData() const { | |
| 123 ExplodedPageState state; | |
| 124 if (!DecodePageState(data_, &state)) | |
| 125 return PageState(); // Oops! | |
| 126 | |
| 127 RecursivelyRemovePasswordData(&state.top); | |
| 128 | |
| 129 return ToPageState(state); | |
| 130 } | |
| 131 | |
| 132 PageState PageState::RemoveScrollOffset() const { | |
| 133 ExplodedPageState state; | |
| 134 if (!DecodePageState(data_, &state)) | |
| 135 return PageState(); // Oops! | |
| 136 | |
| 137 RecursivelyRemoveScrollOffset(&state.top); | |
| 138 | |
| 139 return ToPageState(state); | |
| 140 } | |
| 141 | |
| 29 PageState::PageState(const std::string& data) | 142 PageState::PageState(const std::string& data) |
| 30 : data_(data) { | 143 : data_(data) { |
| 31 // TODO(darin): Enable this DCHECK once tests have been fixed up to not pass | 144 // TODO(darin): Enable this DCHECK once tests have been fixed up to not pass |
| 32 // bogus encoded data to CreateFromEncodedData. | 145 // bogus encoded data to CreateFromEncodedData. |
| 33 //DCHECK(IsValid()); | 146 //DCHECK(IsValid()); |
| 34 } | 147 } |
| 35 | 148 |
| 36 } // namespace content | 149 } // namespace content |
| OLD | NEW |