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