blob: 9d3a59cd9f1c1e74b5e19dccb959de2ea20b5e6f [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>
8
lukasza28b27bcc2016-05-25 23:45:319#include "base/strings/nullable_string16.h"
lukasza9a9ece42016-05-31 19:01:4010#include "content/child/web_url_request_util.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"
lukasza28b27bcc2016-05-25 23:45:3114#include "third_party/WebKit/public/platform/WebData.h"
[email protected]a728f0b2014-05-11 23:23:3715#include "third_party/WebKit/public/platform/WebFloatPoint.h"
[email protected]1a852ab2013-06-25 03:10:2416#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
lukasza28b27bcc2016-05-25 23:45:3123using blink::WebData;
[email protected]180ef242013-11-07 06:50:4624using blink::WebHTTPBody;
25using blink::WebHistoryItem;
26using blink::WebSerializedScriptValue;
27using blink::WebString;
28using blink::WebVector;
[email protected]691aa2f2013-05-28 22:52:0429
30namespace content {
[email protected]1a852ab2013-06-25 03:10:2431namespace {
[email protected]691aa2f2013-05-28 22:52:0432
[email protected]1a852ab2013-06-25 03:10:2433void ToNullableString16Vector(const WebVector<WebString>& input,
34 std::vector<base::NullableString16>* output) {
creisbabe66042014-12-13 03:26:1135 output->reserve(output->size() + input.size());
[email protected]1a852ab2013-06-25 03:10:2436 for (size_t i = 0; i < input.size(); ++i)
Blink Reformat1c4d759e2017-04-09 16:34:5437 output->push_back(WebString::ToNullableString16(input[i]));
[email protected]691aa2f2013-05-28 22:52:0438}
39
[email protected]9cd14ef2014-04-30 18:26:0340void GenerateFrameStateFromItem(const WebHistoryItem& item,
41 ExplodedFrameState* state) {
Blink Reformat1c4d759e2017-04-09 16:34:5442 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()) {
kinukoa4293022017-01-27 06:43:2547 state->state_object =
Blink Reformat1c4d759e2017-04-09 16:34:5448 WebString::ToNullableString16(item.StateObject().ToString());
kinukoa4293022017-01-27 06:43:2549 }
Blink Reformat1c4d759e2017-04-09 16:34:5450 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]1a852ab2013-06-25 03:10:2458
kinukoa4293022017-01-27 06:43:2559 state->http_body.http_content_type =
Blink Reformat1c4d759e2017-04-09 16:34:5460 WebString::ToNullableString16(item.HttpContentType());
61 const WebHTTPBody& http_body = item.HttpBody();
62 if (!http_body.IsNull()) {
lukasza9a9ece42016-05-31 19:01:4063 state->http_body.request_body = GetRequestBodyForWebHTTPBody(http_body);
Blink Reformat1c4d759e2017-04-09 16:34:5464 state->http_body.contains_passwords = http_body.ContainsPasswordData();
[email protected]1a852ab2013-06-25 03:10:2465 }
[email protected]1a852ab2013-06-25 03:10:2466}
67
creisbabe66042014-12-13 03:26:1168void RecursivelyGenerateFrameState(
69 HistoryEntry::HistoryNode* node,
70 ExplodedFrameState* state,
71 std::vector<base::NullableString16>* referenced_files) {
[email protected]9cd14ef2014-04-30 18:26:0372 GenerateFrameStateFromItem(node->item(), state);
Blink Reformat1c4d759e2017-04-09 16:34:5473 ToNullableString16Vector(node->item().GetReferencedFilePaths(),
creisbabe66042014-12-13 03:26:1174 referenced_files);
[email protected]9cd14ef2014-04-30 18:26:0375
leon.han21e0e482017-02-23 04:13:3276 std::vector<HistoryEntry::HistoryNode*> children = node->children();
[email protected]9cd14ef2014-04-30 18:26:0377 state->children.resize(children.size());
creisbabe66042014-12-13 03:26:1178 for (size_t i = 0; i < children.size(); ++i) {
79 RecursivelyGenerateFrameState(children[i], &state->children[i],
80 referenced_files);
81 }
[email protected]9cd14ef2014-04-30 18:26:0382}
83
84void RecursivelyGenerateHistoryItem(const ExplodedFrameState& state,
85 HistoryEntry::HistoryNode* node) {
86 WebHistoryItem item;
Blink Reformat1c4d759e2017-04-09 16:34:5487 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]1a852ab2013-06-25 03:10:2491 if (!state.state_object.is_null()) {
Blink Reformat1c4d759e2017-04-09 16:34:5492 item.SetStateObject(WebSerializedScriptValue::FromString(
93 WebString::FromUTF16(state.state_object)));
[email protected]1a852ab2013-06-25 03:10:2494 }
kinukoa4293022017-01-27 06:43:2595 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 Reformat1c4d759e2017-04-09 16:34:5498 return WebString::FromUTF16(s);
kinukoa4293022017-01-27 06:43:2599 });
Blink Reformat1c4d759e2017-04-09 16:34:54100 item.SetDocumentState(document_state);
101 item.SetScrollRestorationType(state.scroll_restoration_type);
japhet64015d42017-07-19 01:23:00102
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]1a852ab2013-06-25 03:10:24108
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 Reformat1c4d759e2017-04-09 16:34:54113 item.SetItemSequenceNumber(state.item_sequence_number);
[email protected]1a852ab2013-06-25 03:10:24114 if (state.document_sequence_number)
Blink Reformat1c4d759e2017-04-09 16:34:54115 item.SetDocumentSequenceNumber(state.document_sequence_number);
[email protected]1a852ab2013-06-25 03:10:24116
Blink Reformat1c4d759e2017-04-09 16:34:54117 item.SetHTTPContentType(
118 WebString::FromUTF16(state.http_body.http_content_type));
lukasza9a9ece42016-05-31 19:01:40119 if (state.http_body.request_body != nullptr) {
Blink Reformat1c4d759e2017-04-09 16:34:54120 item.SetHTTPBody(
lukasza9a9ece42016-05-31 19:01:40121 GetWebHTTPBodyForRequestBody(state.http_body.request_body));
[email protected]1a852ab2013-06-25 03:10:24122 }
[email protected]9cd14ef2014-04-30 18:26:03123 node->set_item(item);
[email protected]1a852ab2013-06-25 03:10:24124
[email protected]9cd14ef2014-04-30 18:26:03125 for (size_t i = 0; i < state.children.size(); ++i)
126 RecursivelyGenerateHistoryItem(state.children[i], node->AddChild());
[email protected]1a852ab2013-06-25 03:10:24127}
128
129} // namespace
130
[email protected]9cd14ef2014-04-30 18:26:03131PageState HistoryEntryToPageState(HistoryEntry* entry) {
[email protected]1a852ab2013-06-25 03:10:24132 ExplodedPageState state;
creisbabe66042014-12-13 03:26:11133 RecursivelyGenerateFrameState(entry->root_history_node(), &state.top,
134 &state.referenced_files);
[email protected]1a852ab2013-06-25 03:10:24135
136 std::string encoded_data;
creis20ada6952016-07-25 19:54:24137 EncodePageState(state, &encoded_data);
[email protected]1a852ab2013-06-25 03:10:24138 return PageState::CreateFromEncodedData(encoded_data);
139}
140
[email protected]9cd14ef2014-04-30 18:26:03141PageState SingleHistoryItemToPageState(const WebHistoryItem& item) {
142 ExplodedPageState state;
Blink Reformat1c4d759e2017-04-09 16:34:54143 ToNullableString16Vector(item.GetReferencedFilePaths(),
[email protected]9cd14ef2014-04-30 18:26:03144 &state.referenced_files);
145 GenerateFrameStateFromItem(item, &state.top);
146
147 std::string encoded_data;
creis20ada6952016-07-25 19:54:24148 EncodePageState(state, &encoded_data);
[email protected]9cd14ef2014-04-30 18:26:03149 return PageState::CreateFromEncodedData(encoded_data);
150}
151
dchengcedca5612016-04-09 01:40:15152std::unique_ptr<HistoryEntry> PageStateToHistoryEntry(
153 const PageState& page_state) {
[email protected]1a852ab2013-06-25 03:10:24154 ExplodedPageState state;
155 if (!DecodePageState(page_state.ToEncodedData(), &state))
dchengcedca5612016-04-09 01:40:15156 return std::unique_ptr<HistoryEntry>();
[email protected]1a852ab2013-06-25 03:10:24157
dchengcedca5612016-04-09 01:40:15158 std::unique_ptr<HistoryEntry> entry(new HistoryEntry());
[email protected]9cd14ef2014-04-30 18:26:03159 RecursivelyGenerateHistoryItem(state.top, entry->root_history_node());
[email protected]1a852ab2013-06-25 03:10:24160
dcheng07945f632015-12-26 07:59:32161 return entry;
[email protected]691aa2f2013-05-28 22:52:04162}
163
164} // namespace content