blob: 2d618233e5b4b3acea0715f34078fd454d4e0443 [file] [log] [blame]
[email protected]dc064352014-04-25 08:36:381// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]691aa2f2013-05-28 22:52:042// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]dc064352014-04-25 08:36:385#include "content/renderer/history_serialization.h"
[email protected]691aa2f2013-05-28 22:52:046
avi1023d012015-12-25 02:39:147#include <stddef.h>
Morten Stenshorneaf5e5272017-10-09 23:11:138#include <algorithm>
avi1023d012015-12-25 02:39:149
lukasza28b27bcc2016-05-25 23:45:3110#include "base/strings/nullable_string16.h"
[email protected]1a852ab2013-06-25 03:10:2411#include "content/common/page_state_serialization.h"
[email protected]691aa2f2013-05-28 22:52:0412#include "content/public/common/page_state.h"
[email protected]9cd14ef2014-04-30 18:26:0313#include "content/renderer/history_entry.h"
John Abd-El-Malek6b56ef712017-10-21 22:52:4614#include "content/renderer/loader/web_url_request_util.h"
Blink Reformata30d4232018-04-07 15:31:0615#include "third_party/blink/public/platform/web_data.h"
16#include "third_party/blink/public/platform/web_float_point.h"
17#include "third_party/blink/public/platform/web_http_body.h"
18#include "third_party/blink/public/platform/web_point.h"
19#include "third_party/blink/public/platform/web_string.h"
20#include "third_party/blink/public/platform/web_vector.h"
21#include "third_party/blink/public/web/web_history_item.h"
22#include "third_party/blink/public/web/web_serialized_script_value.h"
[email protected]1a852ab2013-06-25 03:10:2423
lukasza28b27bcc2016-05-25 23:45:3124using blink::WebData;
[email protected]180ef242013-11-07 06:50:4625using blink::WebHTTPBody;
26using blink::WebHistoryItem;
27using blink::WebSerializedScriptValue;
28using blink::WebString;
29using blink::WebVector;
[email protected]691aa2f2013-05-28 22:52:0430
31namespace content {
[email protected]1a852ab2013-06-25 03:10:2432namespace {
[email protected]691aa2f2013-05-28 22:52:0433
Daniel Chengafad3082017-10-06 04:48:0434void ToOptionalString16Vector(
35 const WebVector<WebString>& input,
36 std::vector<base::Optional<base::string16>>* output) {
creisbabe66042014-12-13 03:26:1137 output->reserve(output->size() + input.size());
[email protected]1a852ab2013-06-25 03:10:2438 for (size_t i = 0; i < input.size(); ++i)
Daniel Chengafad3082017-10-06 04:48:0439 output->emplace_back(WebString::ToOptionalString16(input[i]));
[email protected]691aa2f2013-05-28 22:52:0440}
41
[email protected]9cd14ef2014-04-30 18:26:0342void GenerateFrameStateFromItem(const WebHistoryItem& item,
43 ExplodedFrameState* state) {
Daniel Chengafad3082017-10-06 04:48:0444 state->url_string = WebString::ToOptionalString16(item.UrlString());
45 state->referrer = WebString::ToOptionalString16(item.GetReferrer());
Blink Reformat1c4d759e2017-04-09 16:34:5446 state->referrer_policy = item.GetReferrerPolicy();
Daniel Chengafad3082017-10-06 04:48:0447 state->target = WebString::ToOptionalString16(item.Target());
Blink Reformat1c4d759e2017-04-09 16:34:5448 if (!item.StateObject().IsNull()) {
kinukoa4293022017-01-27 06:43:2549 state->state_object =
Daniel Chengafad3082017-10-06 04:48:0450 WebString::ToOptionalString16(item.StateObject().ToString());
kinukoa4293022017-01-27 06:43:2551 }
Blink Reformat1c4d759e2017-04-09 16:34:5452 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 Chengafad3082017-10-06 04:48:0459 ToOptionalString16Vector(item.GetDocumentState(), &state->document_state);
[email protected]1a852ab2013-06-25 03:10:2460
kinukoa4293022017-01-27 06:43:2561 state->http_body.http_content_type =
Daniel Chengafad3082017-10-06 04:48:0462 WebString::ToOptionalString16(item.HttpContentType());
Blink Reformat1c4d759e2017-04-09 16:34:5463 const WebHTTPBody& http_body = item.HttpBody();
64 if (!http_body.IsNull()) {
lukasza9a9ece42016-05-31 19:01:4065 state->http_body.request_body = GetRequestBodyForWebHTTPBody(http_body);
Blink Reformat1c4d759e2017-04-09 16:34:5466 state->http_body.contains_passwords = http_body.ContainsPasswordData();
[email protected]1a852ab2013-06-25 03:10:2467 }
Patrick Noland2352f562017-11-09 00:52:3168
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]1a852ab2013-06-25 03:10:2474}
75
[email protected]9cd14ef2014-04-30 18:26:0376void RecursivelyGenerateHistoryItem(const ExplodedFrameState& state,
77 HistoryEntry::HistoryNode* node) {
78 WebHistoryItem item;
Blink Reformat1c4d759e2017-04-09 16:34:5479 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 Chengafad3082017-10-06 04:48:0483 if (state.state_object) {
Blink Reformat1c4d759e2017-04-09 16:34:5484 item.SetStateObject(WebSerializedScriptValue::FromString(
Daniel Chengafad3082017-10-06 04:48:0485 WebString::FromUTF16(*state.state_object)));
[email protected]1a852ab2013-06-25 03:10:2486 }
kinukoa4293022017-01-27 06:43:2587 WebVector<WebString> document_state(state.document_state.size());
88 std::transform(state.document_state.begin(), state.document_state.end(),
Daniel Chengafad3082017-10-06 04:48:0489 document_state.begin(),
90 [](const base::Optional<base::string16>& s) {
Blink Reformat1c4d759e2017-04-09 16:34:5491 return WebString::FromUTF16(s);
kinukoa4293022017-01-27 06:43:2592 });
Blink Reformat1c4d759e2017-04-09 16:34:5493 item.SetDocumentState(document_state);
94 item.SetScrollRestorationType(state.scroll_restoration_type);
japhet64015d42017-07-19 01:23:0095
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]1a852ab2013-06-25 03:10:24101
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 Reformat1c4d759e2017-04-09 16:34:54106 item.SetItemSequenceNumber(state.item_sequence_number);
[email protected]1a852ab2013-06-25 03:10:24107 if (state.document_sequence_number)
Blink Reformat1c4d759e2017-04-09 16:34:54108 item.SetDocumentSequenceNumber(state.document_sequence_number);
[email protected]1a852ab2013-06-25 03:10:24109
Blink Reformat1c4d759e2017-04-09 16:34:54110 item.SetHTTPContentType(
111 WebString::FromUTF16(state.http_body.http_content_type));
lukasza9a9ece42016-05-31 19:01:40112 if (state.http_body.request_body != nullptr) {
Blink Reformat1c4d759e2017-04-09 16:34:54113 item.SetHTTPBody(
Matt Falkenhagen34eb2812017-12-15 03:36:39114 GetWebHTTPBodyForRequestBody(*state.http_body.request_body));
[email protected]1a852ab2013-06-25 03:10:24115 }
Patrick Noland2352f562017-11-09 00:52:31116
117 item.SetScrollAnchorData({WebString::FromUTF16(state.scroll_anchor_selector),
118 state.scroll_anchor_offset,
119 state.scroll_anchor_simhash});
[email protected]9cd14ef2014-04-30 18:26:03120 node->set_item(item);
[email protected]1a852ab2013-06-25 03:10:24121
[email protected]9cd14ef2014-04-30 18:26:03122 for (size_t i = 0; i < state.children.size(); ++i)
123 RecursivelyGenerateHistoryItem(state.children[i], node->AddChild());
[email protected]1a852ab2013-06-25 03:10:24124}
125
126} // namespace
127
[email protected]9cd14ef2014-04-30 18:26:03128PageState SingleHistoryItemToPageState(const WebHistoryItem& item) {
129 ExplodedPageState state;
Daniel Chengafad3082017-10-06 04:48:04130 ToOptionalString16Vector(item.GetReferencedFilePaths(),
[email protected]9cd14ef2014-04-30 18:26:03131 &state.referenced_files);
132 GenerateFrameStateFromItem(item, &state.top);
133
134 std::string encoded_data;
creis20ada6952016-07-25 19:54:24135 EncodePageState(state, &encoded_data);
[email protected]9cd14ef2014-04-30 18:26:03136 return PageState::CreateFromEncodedData(encoded_data);
137}
138
dchengcedca5612016-04-09 01:40:15139std::unique_ptr<HistoryEntry> PageStateToHistoryEntry(
140 const PageState& page_state) {
[email protected]1a852ab2013-06-25 03:10:24141 ExplodedPageState state;
142 if (!DecodePageState(page_state.ToEncodedData(), &state))
dchengcedca5612016-04-09 01:40:15143 return std::unique_ptr<HistoryEntry>();
[email protected]1a852ab2013-06-25 03:10:24144
dchengcedca5612016-04-09 01:40:15145 std::unique_ptr<HistoryEntry> entry(new HistoryEntry());
[email protected]9cd14ef2014-04-30 18:26:03146 RecursivelyGenerateHistoryItem(state.top, entry->root_history_node());
[email protected]1a852ab2013-06-25 03:10:24147
dcheng07945f632015-12-26 07:59:32148 return entry;
[email protected]691aa2f2013-05-28 22:52:04149}
150
151} // namespace content