[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> |
Morten Stenshorne | af5e527 | 2017-10-09 23:11:13 | [diff] [blame] | 8 | #include <algorithm> |
avi | 1023d01 | 2015-12-25 02:39:14 | [diff] [blame] | 9 | |
lukasza | 28b27bcc | 2016-05-25 23:45:31 | [diff] [blame] | 10 | #include "base/strings/nullable_string16.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" |
John Abd-El-Malek | 6b56ef71 | 2017-10-21 22:52:46 | [diff] [blame] | 14 | #include "content/renderer/loader/web_url_request_util.h" |
lukasza | 28b27bcc | 2016-05-25 23:45:31 | [diff] [blame] | 15 | #include "third_party/WebKit/public/platform/WebData.h" |
[email protected] | a728f0b | 2014-05-11 23:23:37 | [diff] [blame] | 16 | #include "third_party/WebKit/public/platform/WebFloatPoint.h" |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 17 | #include "third_party/WebKit/public/platform/WebHTTPBody.h" |
| 18 | #include "third_party/WebKit/public/platform/WebPoint.h" |
| 19 | #include "third_party/WebKit/public/platform/WebString.h" |
| 20 | #include "third_party/WebKit/public/platform/WebVector.h" |
| 21 | #include "third_party/WebKit/public/web/WebHistoryItem.h" |
| 22 | #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" |
| 23 | |
lukasza | 28b27bcc | 2016-05-25 23:45:31 | [diff] [blame] | 24 | using blink::WebData; |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 25 | using blink::WebHTTPBody; |
| 26 | using blink::WebHistoryItem; |
| 27 | using blink::WebSerializedScriptValue; |
| 28 | using blink::WebString; |
| 29 | using blink::WebVector; |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 30 | |
| 31 | namespace content { |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 32 | namespace { |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 33 | |
Daniel Cheng | afad308 | 2017-10-06 04:48:04 | [diff] [blame] | 34 | void ToOptionalString16Vector( |
| 35 | const WebVector<WebString>& input, |
| 36 | std::vector<base::Optional<base::string16>>* output) { |
creis | babe6604 | 2014-12-13 03:26:11 | [diff] [blame] | 37 | output->reserve(output->size() + input.size()); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 38 | for (size_t i = 0; i < input.size(); ++i) |
Daniel Cheng | afad308 | 2017-10-06 04:48:04 | [diff] [blame] | 39 | output->emplace_back(WebString::ToOptionalString16(input[i])); |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 40 | } |
| 41 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 42 | void GenerateFrameStateFromItem(const WebHistoryItem& item, |
| 43 | ExplodedFrameState* state) { |
Daniel Cheng | afad308 | 2017-10-06 04:48:04 | [diff] [blame] | 44 | state->url_string = WebString::ToOptionalString16(item.UrlString()); |
| 45 | state->referrer = WebString::ToOptionalString16(item.GetReferrer()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 46 | state->referrer_policy = item.GetReferrerPolicy(); |
Daniel Cheng | afad308 | 2017-10-06 04:48:04 | [diff] [blame] | 47 | state->target = WebString::ToOptionalString16(item.Target()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 48 | if (!item.StateObject().IsNull()) { |
kinuko | a429302 | 2017-01-27 06:43:25 | [diff] [blame] | 49 | state->state_object = |
Daniel Cheng | afad308 | 2017-10-06 04:48:04 | [diff] [blame] | 50 | WebString::ToOptionalString16(item.StateObject().ToString()); |
kinuko | a429302 | 2017-01-27 06:43:25 | [diff] [blame] | 51 | } |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 52 | state->scroll_restoration_type = item.ScrollRestorationType(); |
| 53 | state->visual_viewport_scroll_offset = item.VisualViewportScrollOffset(); |
| 54 | state->scroll_offset = item.GetScrollOffset(); |
| 55 | state->item_sequence_number = item.ItemSequenceNumber(); |
| 56 | state->document_sequence_number = item.DocumentSequenceNumber(); |
| 57 | state->page_scale_factor = item.PageScaleFactor(); |
| 58 | state->did_save_scroll_or_scale_state = item.DidSaveScrollOrScaleState(); |
Daniel Cheng | afad308 | 2017-10-06 04:48:04 | [diff] [blame] | 59 | ToOptionalString16Vector(item.GetDocumentState(), &state->document_state); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 60 | |
kinuko | a429302 | 2017-01-27 06:43:25 | [diff] [blame] | 61 | state->http_body.http_content_type = |
Daniel Cheng | afad308 | 2017-10-06 04:48:04 | [diff] [blame] | 62 | WebString::ToOptionalString16(item.HttpContentType()); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 63 | const WebHTTPBody& http_body = item.HttpBody(); |
| 64 | if (!http_body.IsNull()) { |
lukasza | 9a9ece4 | 2016-05-31 19:01:40 | [diff] [blame] | 65 | state->http_body.request_body = GetRequestBodyForWebHTTPBody(http_body); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 66 | state->http_body.contains_passwords = http_body.ContainsPasswordData(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 67 | } |
Patrick Noland | 2352f56 | 2017-11-09 00:52:31 | [diff] [blame] | 68 | |
| 69 | blink::ScrollAnchorData anchor = item.GetScrollAnchorData(); |
| 70 | state->scroll_anchor_selector = |
| 71 | WebString::ToOptionalString16(anchor.selector_); |
| 72 | state->scroll_anchor_offset = anchor.offset_; |
| 73 | state->scroll_anchor_simhash = anchor.simhash_; |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 74 | } |
| 75 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 76 | void RecursivelyGenerateHistoryItem(const ExplodedFrameState& state, |
| 77 | HistoryEntry::HistoryNode* node) { |
| 78 | WebHistoryItem item; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 79 | item.Initialize(); |
| 80 | item.SetURLString(WebString::FromUTF16(state.url_string)); |
| 81 | item.SetReferrer(WebString::FromUTF16(state.referrer), state.referrer_policy); |
| 82 | item.SetTarget(WebString::FromUTF16(state.target)); |
Daniel Cheng | afad308 | 2017-10-06 04:48:04 | [diff] [blame] | 83 | if (state.state_object) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 84 | item.SetStateObject(WebSerializedScriptValue::FromString( |
Daniel Cheng | afad308 | 2017-10-06 04:48:04 | [diff] [blame] | 85 | WebString::FromUTF16(*state.state_object))); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 86 | } |
kinuko | a429302 | 2017-01-27 06:43:25 | [diff] [blame] | 87 | WebVector<WebString> document_state(state.document_state.size()); |
| 88 | std::transform(state.document_state.begin(), state.document_state.end(), |
Daniel Cheng | afad308 | 2017-10-06 04:48:04 | [diff] [blame] | 89 | document_state.begin(), |
| 90 | [](const base::Optional<base::string16>& s) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 91 | return WebString::FromUTF16(s); |
kinuko | a429302 | 2017-01-27 06:43:25 | [diff] [blame] | 92 | }); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 93 | item.SetDocumentState(document_state); |
| 94 | item.SetScrollRestorationType(state.scroll_restoration_type); |
japhet | 64015d4 | 2017-07-19 01:23:00 | [diff] [blame] | 95 | |
| 96 | if (state.did_save_scroll_or_scale_state) { |
| 97 | item.SetVisualViewportScrollOffset(state.visual_viewport_scroll_offset); |
| 98 | item.SetScrollOffset(state.scroll_offset); |
| 99 | item.SetPageScaleFactor(state.page_scale_factor); |
| 100 | } |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 101 | |
| 102 | // These values are generated at WebHistoryItem construction time, and we |
| 103 | // only want to override those new values with old values if the old values |
| 104 | // are defined. A value of 0 means undefined in this context. |
| 105 | if (state.item_sequence_number) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 106 | item.SetItemSequenceNumber(state.item_sequence_number); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 107 | if (state.document_sequence_number) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 108 | item.SetDocumentSequenceNumber(state.document_sequence_number); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 109 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 110 | item.SetHTTPContentType( |
| 111 | WebString::FromUTF16(state.http_body.http_content_type)); |
lukasza | 9a9ece4 | 2016-05-31 19:01:40 | [diff] [blame] | 112 | if (state.http_body.request_body != nullptr) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 113 | item.SetHTTPBody( |
Matt Falkenhagen | 34eb281 | 2017-12-15 03:36:39 | [diff] [blame^] | 114 | GetWebHTTPBodyForRequestBody(*state.http_body.request_body)); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 115 | } |
Patrick Noland | 2352f56 | 2017-11-09 00:52:31 | [diff] [blame] | 116 | |
| 117 | item.SetScrollAnchorData({WebString::FromUTF16(state.scroll_anchor_selector), |
| 118 | state.scroll_anchor_offset, |
| 119 | state.scroll_anchor_simhash}); |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 120 | node->set_item(item); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 121 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 122 | for (size_t i = 0; i < state.children.size(); ++i) |
| 123 | RecursivelyGenerateHistoryItem(state.children[i], node->AddChild()); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | } // namespace |
| 127 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 128 | PageState SingleHistoryItemToPageState(const WebHistoryItem& item) { |
| 129 | ExplodedPageState state; |
Daniel Cheng | afad308 | 2017-10-06 04:48:04 | [diff] [blame] | 130 | ToOptionalString16Vector(item.GetReferencedFilePaths(), |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 131 | &state.referenced_files); |
| 132 | GenerateFrameStateFromItem(item, &state.top); |
| 133 | |
| 134 | std::string encoded_data; |
creis | 20ada695 | 2016-07-25 19:54:24 | [diff] [blame] | 135 | EncodePageState(state, &encoded_data); |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 136 | return PageState::CreateFromEncodedData(encoded_data); |
| 137 | } |
| 138 | |
dcheng | cedca561 | 2016-04-09 01:40:15 | [diff] [blame] | 139 | std::unique_ptr<HistoryEntry> PageStateToHistoryEntry( |
| 140 | const PageState& page_state) { |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 141 | ExplodedPageState state; |
| 142 | if (!DecodePageState(page_state.ToEncodedData(), &state)) |
dcheng | cedca561 | 2016-04-09 01:40:15 | [diff] [blame] | 143 | return std::unique_ptr<HistoryEntry>(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 144 | |
dcheng | cedca561 | 2016-04-09 01:40:15 | [diff] [blame] | 145 | std::unique_ptr<HistoryEntry> entry(new HistoryEntry()); |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 146 | RecursivelyGenerateHistoryItem(state.top, entry->root_history_node()); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 147 | |
dcheng | 07945f63 | 2015-12-26 07:59:32 | [diff] [blame] | 148 | return entry; |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | } // namespace content |