blob: e94e7bca98eaeb7e7f1c3d7440626c15ce2ab78d [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
Daniel Chengafad3082017-10-06 04:48:0433void ToOptionalString16Vector(
34 const WebVector<WebString>& input,
35 std::vector<base::Optional<base::string16>>* output) {
creisbabe66042014-12-13 03:26:1136 output->reserve(output->size() + input.size());
[email protected]1a852ab2013-06-25 03:10:2437 for (size_t i = 0; i < input.size(); ++i)
Daniel Chengafad3082017-10-06 04:48:0438 output->emplace_back(WebString::ToOptionalString16(input[i]));
[email protected]691aa2f2013-05-28 22:52:0439}
40
[email protected]9cd14ef2014-04-30 18:26:0341void GenerateFrameStateFromItem(const WebHistoryItem& item,
42 ExplodedFrameState* state) {
Daniel Chengafad3082017-10-06 04:48:0443 state->url_string = WebString::ToOptionalString16(item.UrlString());
44 state->referrer = WebString::ToOptionalString16(item.GetReferrer());
Blink Reformat1c4d759e2017-04-09 16:34:5445 state->referrer_policy = item.GetReferrerPolicy();
Daniel Chengafad3082017-10-06 04:48:0446 state->target = WebString::ToOptionalString16(item.Target());
Blink Reformat1c4d759e2017-04-09 16:34:5447 if (!item.StateObject().IsNull()) {
kinukoa4293022017-01-27 06:43:2548 state->state_object =
Daniel Chengafad3082017-10-06 04:48:0449 WebString::ToOptionalString16(item.StateObject().ToString());
kinukoa4293022017-01-27 06:43:2550 }
Blink Reformat1c4d759e2017-04-09 16:34:5451 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 Chengafad3082017-10-06 04:48:0458 ToOptionalString16Vector(item.GetDocumentState(), &state->document_state);
[email protected]1a852ab2013-06-25 03:10:2459
kinukoa4293022017-01-27 06:43:2560 state->http_body.http_content_type =
Daniel Chengafad3082017-10-06 04:48:0461 WebString::ToOptionalString16(item.HttpContentType());
Blink Reformat1c4d759e2017-04-09 16:34:5462 const WebHTTPBody& http_body = item.HttpBody();
63 if (!http_body.IsNull()) {
lukasza9a9ece42016-05-31 19:01:4064 state->http_body.request_body = GetRequestBodyForWebHTTPBody(http_body);
Blink Reformat1c4d759e2017-04-09 16:34:5465 state->http_body.contains_passwords = http_body.ContainsPasswordData();
[email protected]1a852ab2013-06-25 03:10:2466 }
[email protected]1a852ab2013-06-25 03:10:2467}
68
[email protected]9cd14ef2014-04-30 18:26:0369void RecursivelyGenerateHistoryItem(const ExplodedFrameState& state,
70 HistoryEntry::HistoryNode* node) {
71 WebHistoryItem item;
Blink Reformat1c4d759e2017-04-09 16:34:5472 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 Chengafad3082017-10-06 04:48:0476 if (state.state_object) {
Blink Reformat1c4d759e2017-04-09 16:34:5477 item.SetStateObject(WebSerializedScriptValue::FromString(
Daniel Chengafad3082017-10-06 04:48:0478 WebString::FromUTF16(*state.state_object)));
[email protected]1a852ab2013-06-25 03:10:2479 }
kinukoa4293022017-01-27 06:43:2580 WebVector<WebString> document_state(state.document_state.size());
81 std::transform(state.document_state.begin(), state.document_state.end(),
Daniel Chengafad3082017-10-06 04:48:0482 document_state.begin(),
83 [](const base::Optional<base::string16>& s) {
Blink Reformat1c4d759e2017-04-09 16:34:5484 return WebString::FromUTF16(s);
kinukoa4293022017-01-27 06:43:2585 });
Blink Reformat1c4d759e2017-04-09 16:34:5486 item.SetDocumentState(document_state);
87 item.SetScrollRestorationType(state.scroll_restoration_type);
japhet64015d42017-07-19 01:23:0088
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]1a852ab2013-06-25 03:10:2494
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 Reformat1c4d759e2017-04-09 16:34:5499 item.SetItemSequenceNumber(state.item_sequence_number);
[email protected]1a852ab2013-06-25 03:10:24100 if (state.document_sequence_number)
Blink Reformat1c4d759e2017-04-09 16:34:54101 item.SetDocumentSequenceNumber(state.document_sequence_number);
[email protected]1a852ab2013-06-25 03:10:24102
Blink Reformat1c4d759e2017-04-09 16:34:54103 item.SetHTTPContentType(
104 WebString::FromUTF16(state.http_body.http_content_type));
lukasza9a9ece42016-05-31 19:01:40105 if (state.http_body.request_body != nullptr) {
Blink Reformat1c4d759e2017-04-09 16:34:54106 item.SetHTTPBody(
lukasza9a9ece42016-05-31 19:01:40107 GetWebHTTPBodyForRequestBody(state.http_body.request_body));
[email protected]1a852ab2013-06-25 03:10:24108 }
[email protected]9cd14ef2014-04-30 18:26:03109 node->set_item(item);
[email protected]1a852ab2013-06-25 03:10:24110
[email protected]9cd14ef2014-04-30 18:26:03111 for (size_t i = 0; i < state.children.size(); ++i)
112 RecursivelyGenerateHistoryItem(state.children[i], node->AddChild());
[email protected]1a852ab2013-06-25 03:10:24113}
114
115} // namespace
116
[email protected]9cd14ef2014-04-30 18:26:03117PageState SingleHistoryItemToPageState(const WebHistoryItem& item) {
118 ExplodedPageState state;
Daniel Chengafad3082017-10-06 04:48:04119 ToOptionalString16Vector(item.GetReferencedFilePaths(),
[email protected]9cd14ef2014-04-30 18:26:03120 &state.referenced_files);
121 GenerateFrameStateFromItem(item, &state.top);
122
123 std::string encoded_data;
creis20ada6952016-07-25 19:54:24124 EncodePageState(state, &encoded_data);
[email protected]9cd14ef2014-04-30 18:26:03125 return PageState::CreateFromEncodedData(encoded_data);
126}
127
dchengcedca5612016-04-09 01:40:15128std::unique_ptr<HistoryEntry> PageStateToHistoryEntry(
129 const PageState& page_state) {
[email protected]1a852ab2013-06-25 03:10:24130 ExplodedPageState state;
131 if (!DecodePageState(page_state.ToEncodedData(), &state))
dchengcedca5612016-04-09 01:40:15132 return std::unique_ptr<HistoryEntry>();
[email protected]1a852ab2013-06-25 03:10:24133
dchengcedca5612016-04-09 01:40:15134 std::unique_ptr<HistoryEntry> entry(new HistoryEntry());
[email protected]9cd14ef2014-04-30 18:26:03135 RecursivelyGenerateHistoryItem(state.top, entry->root_history_node());
[email protected]1a852ab2013-06-25 03:10:24136
dcheng07945f632015-12-26 07:59:32137 return entry;
[email protected]691aa2f2013-05-28 22:52:04138}
139
140} // namespace content