[email protected] | dc06435 | 2014-04-25 08:36:38 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | dc06435 | 2014-04-25 08:36:38 | [diff] [blame] | 5 | #include "content/renderer/history_serialization.h" |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 6 | |
avi | 1023d01 | 2015-12-25 02:39:14 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
lukasza | 28b27bcc | 2016-05-25 23:45:31 | [diff] [blame] | 9 | #include "base/strings/nullable_string16.h" |
lukasza | 9a9ece4 | 2016-05-31 19:01:40 | [diff] [blame] | 10 | #include "content/child/web_url_request_util.h" |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 11 | #include "content/common/page_state_serialization.h" |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 12 | #include "content/public/common/page_state.h" |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 13 | #include "content/renderer/history_entry.h" |
lukasza | 28b27bcc | 2016-05-25 23:45:31 | [diff] [blame] | 14 | #include "third_party/WebKit/public/platform/WebData.h" |
[email protected] | a728f0b | 2014-05-11 23:23:37 | [diff] [blame] | 15 | #include "third_party/WebKit/public/platform/WebFloatPoint.h" |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 16 | #include "third_party/WebKit/public/platform/WebHTTPBody.h" |
| 17 | #include "third_party/WebKit/public/platform/WebPoint.h" |
| 18 | #include "third_party/WebKit/public/platform/WebString.h" |
| 19 | #include "third_party/WebKit/public/platform/WebVector.h" |
| 20 | #include "third_party/WebKit/public/web/WebHistoryItem.h" |
| 21 | #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" |
| 22 | |
lukasza | 28b27bcc | 2016-05-25 23:45:31 | [diff] [blame] | 23 | using blink::WebData; |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 24 | using blink::WebHTTPBody; |
| 25 | using blink::WebHistoryItem; |
| 26 | using blink::WebSerializedScriptValue; |
| 27 | using blink::WebString; |
| 28 | using blink::WebVector; |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 29 | |
| 30 | namespace content { |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 31 | namespace { |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 32 | |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 33 | void ToNullableString16Vector(const WebVector<WebString>& input, |
| 34 | std::vector<base::NullableString16>* output) { |
creis | babe6604 | 2014-12-13 03:26:11 | [diff] [blame] | 35 | output->reserve(output->size() + input.size()); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 36 | for (size_t i = 0; i < input.size(); ++i) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 37 | output->push_back(WebString::ToNullableString16(input[i])); |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 38 | } |
| 39 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 40 | void GenerateFrameStateFromItem(const WebHistoryItem& item, |
| 41 | ExplodedFrameState* state) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 42 | state->url_string = WebString::ToNullableString16(item.UrlString()); |
| 43 | state->referrer = WebString::ToNullableString16(item.GetReferrer()); |
| 44 | state->referrer_policy = item.GetReferrerPolicy(); |
| 45 | state->target = WebString::ToNullableString16(item.Target()); |
| 46 | if (!item.StateObject().IsNull()) { |
kinuko | a429302 | 2017-01-27 06:43:25 | [diff] [blame] | 47 | state->state_object = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 48 | WebString::ToNullableString16(item.StateObject().ToString()); |
kinuko | a429302 | 2017-01-27 06:43:25 | [diff] [blame] | 49 | } |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 50 | state->scroll_restoration_type = item.ScrollRestorationType(); |
| 51 | state->visual_viewport_scroll_offset = item.VisualViewportScrollOffset(); |
| 52 | state->scroll_offset = item.GetScrollOffset(); |
| 53 | state->item_sequence_number = item.ItemSequenceNumber(); |
| 54 | state->document_sequence_number = item.DocumentSequenceNumber(); |
| 55 | state->page_scale_factor = item.PageScaleFactor(); |
| 56 | state->did_save_scroll_or_scale_state = item.DidSaveScrollOrScaleState(); |
| 57 | ToNullableString16Vector(item.GetDocumentState(), &state->document_state); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 58 | |
kinuko | a429302 | 2017-01-27 06:43:25 | [diff] [blame] | 59 | state->http_body.http_content_type = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 60 | WebString::ToNullableString16(item.HttpContentType()); |
| 61 | const WebHTTPBody& http_body = item.HttpBody(); |
| 62 | if (!http_body.IsNull()) { |
lukasza | 9a9ece4 | 2016-05-31 19:01:40 | [diff] [blame] | 63 | state->http_body.request_body = GetRequestBodyForWebHTTPBody(http_body); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 64 | state->http_body.contains_passwords = http_body.ContainsPasswordData(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 65 | } |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 66 | } |
| 67 | |
creis | babe6604 | 2014-12-13 03:26:11 | [diff] [blame] | 68 | void RecursivelyGenerateFrameState( |
| 69 | HistoryEntry::HistoryNode* node, |
| 70 | ExplodedFrameState* state, |
| 71 | std::vector<base::NullableString16>* referenced_files) { |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 72 | GenerateFrameStateFromItem(node->item(), state); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 73 | ToNullableString16Vector(node->item().GetReferencedFilePaths(), |
creis | babe6604 | 2014-12-13 03:26:11 | [diff] [blame] | 74 | referenced_files); |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 75 | |
leon.han | 21e0e48 | 2017-02-23 04:13:32 | [diff] [blame] | 76 | std::vector<HistoryEntry::HistoryNode*> children = node->children(); |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 77 | state->children.resize(children.size()); |
creis | babe6604 | 2014-12-13 03:26:11 | [diff] [blame] | 78 | for (size_t i = 0; i < children.size(); ++i) { |
| 79 | RecursivelyGenerateFrameState(children[i], &state->children[i], |
| 80 | referenced_files); |
| 81 | } |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void RecursivelyGenerateHistoryItem(const ExplodedFrameState& state, |
| 85 | HistoryEntry::HistoryNode* node) { |
| 86 | WebHistoryItem item; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 87 | item.Initialize(); |
| 88 | item.SetURLString(WebString::FromUTF16(state.url_string)); |
| 89 | item.SetReferrer(WebString::FromUTF16(state.referrer), state.referrer_policy); |
| 90 | item.SetTarget(WebString::FromUTF16(state.target)); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 91 | if (!state.state_object.is_null()) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 92 | item.SetStateObject(WebSerializedScriptValue::FromString( |
| 93 | WebString::FromUTF16(state.state_object))); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 94 | } |
kinuko | a429302 | 2017-01-27 06:43:25 | [diff] [blame] | 95 | WebVector<WebString> document_state(state.document_state.size()); |
| 96 | std::transform(state.document_state.begin(), state.document_state.end(), |
| 97 | document_state.begin(), [](const base::NullableString16& s) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 98 | return WebString::FromUTF16(s); |
kinuko | a429302 | 2017-01-27 06:43:25 | [diff] [blame] | 99 | }); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 100 | item.SetDocumentState(document_state); |
| 101 | item.SetScrollRestorationType(state.scroll_restoration_type); |
japhet | 64015d4 | 2017-07-19 01:23:00 | [diff] [blame] | 102 | |
| 103 | if (state.did_save_scroll_or_scale_state) { |
| 104 | item.SetVisualViewportScrollOffset(state.visual_viewport_scroll_offset); |
| 105 | item.SetScrollOffset(state.scroll_offset); |
| 106 | item.SetPageScaleFactor(state.page_scale_factor); |
| 107 | } |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 108 | |
| 109 | // These values are generated at WebHistoryItem construction time, and we |
| 110 | // only want to override those new values with old values if the old values |
| 111 | // are defined. A value of 0 means undefined in this context. |
| 112 | if (state.item_sequence_number) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 113 | item.SetItemSequenceNumber(state.item_sequence_number); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 114 | if (state.document_sequence_number) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 115 | item.SetDocumentSequenceNumber(state.document_sequence_number); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 116 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 117 | item.SetHTTPContentType( |
| 118 | WebString::FromUTF16(state.http_body.http_content_type)); |
lukasza | 9a9ece4 | 2016-05-31 19:01:40 | [diff] [blame] | 119 | if (state.http_body.request_body != nullptr) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 120 | item.SetHTTPBody( |
lukasza | 9a9ece4 | 2016-05-31 19:01:40 | [diff] [blame] | 121 | GetWebHTTPBodyForRequestBody(state.http_body.request_body)); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 122 | } |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 123 | node->set_item(item); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 124 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 125 | for (size_t i = 0; i < state.children.size(); ++i) |
| 126 | RecursivelyGenerateHistoryItem(state.children[i], node->AddChild()); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | } // namespace |
| 130 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 131 | PageState HistoryEntryToPageState(HistoryEntry* entry) { |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 132 | ExplodedPageState state; |
creis | babe6604 | 2014-12-13 03:26:11 | [diff] [blame] | 133 | RecursivelyGenerateFrameState(entry->root_history_node(), &state.top, |
| 134 | &state.referenced_files); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 135 | |
| 136 | std::string encoded_data; |
creis | 20ada695 | 2016-07-25 19:54:24 | [diff] [blame] | 137 | EncodePageState(state, &encoded_data); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 138 | return PageState::CreateFromEncodedData(encoded_data); |
| 139 | } |
| 140 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 141 | PageState SingleHistoryItemToPageState(const WebHistoryItem& item) { |
| 142 | ExplodedPageState state; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 143 | ToNullableString16Vector(item.GetReferencedFilePaths(), |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 144 | &state.referenced_files); |
| 145 | GenerateFrameStateFromItem(item, &state.top); |
| 146 | |
| 147 | std::string encoded_data; |
creis | 20ada695 | 2016-07-25 19:54:24 | [diff] [blame] | 148 | EncodePageState(state, &encoded_data); |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 149 | return PageState::CreateFromEncodedData(encoded_data); |
| 150 | } |
| 151 | |
dcheng | cedca561 | 2016-04-09 01:40:15 | [diff] [blame] | 152 | std::unique_ptr<HistoryEntry> PageStateToHistoryEntry( |
| 153 | const PageState& page_state) { |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 154 | ExplodedPageState state; |
| 155 | if (!DecodePageState(page_state.ToEncodedData(), &state)) |
dcheng | cedca561 | 2016-04-09 01:40:15 | [diff] [blame] | 156 | return std::unique_ptr<HistoryEntry>(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 157 | |
dcheng | cedca561 | 2016-04-09 01:40:15 | [diff] [blame] | 158 | std::unique_ptr<HistoryEntry> entry(new HistoryEntry()); |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 159 | RecursivelyGenerateHistoryItem(state.top, entry->root_history_node()); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 160 | |
dcheng | 07945f63 | 2015-12-26 07:59:32 | [diff] [blame] | 161 | return entry; |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | } // namespace content |