blob: ac85324c1c097f5e0294936da7a1a0ef40a0e13b [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"
[email protected]1a852ab2013-06-25 03:10:2410#include "content/common/page_state_serialization.h"
[email protected]691aa2f2013-05-28 22:52:0411#include "content/public/common/page_state.h"
[email protected]9cd14ef2014-04-30 18:26:0312#include "content/renderer/history_entry.h"
lukasza28b27bcc2016-05-25 23:45:3113#include "content/renderer/http_body_conversions.h"
14#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)
37 output->push_back(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) {
[email protected]1a852ab2013-06-25 03:10:2442 state->url_string = item.urlString();
[email protected]1a852ab2013-06-25 03:10:2443 state->referrer = item.referrer();
[email protected]bd2979f2014-01-30 13:30:0444 state->referrer_policy = item.referrerPolicy();
[email protected]1a852ab2013-06-25 03:10:2445 state->target = item.target();
[email protected]1a852ab2013-06-25 03:10:2446 if (!item.stateObject().isNull())
47 state->state_object = item.stateObject().toString();
majidvpbbac25a2015-05-13 14:26:0948 state->scroll_restoration_type = item.scrollRestorationType();
jbroman0dacd112015-11-11 02:29:5549 state->visual_viewport_scroll_offset = item.visualViewportScrollOffset();
[email protected]1a852ab2013-06-25 03:10:2450 state->scroll_offset = item.scrollOffset();
51 state->item_sequence_number = item.itemSequenceNumber();
52 state->document_sequence_number =
53 item.documentSequenceNumber();
[email protected]1a852ab2013-06-25 03:10:2454 state->page_scale_factor = item.pageScaleFactor();
[email protected]1a852ab2013-06-25 03:10:2455 ToNullableString16Vector(item.documentState(), &state->document_state);
56
57 state->http_body.http_content_type = item.httpContentType();
58 const WebHTTPBody& http_body = item.httpBody();
[email protected]176fe9a2014-07-08 02:31:5359 state->http_body.is_null = http_body.isNull();
60 if (!state->http_body.is_null) {
[email protected]1a852ab2013-06-25 03:10:2461 state->http_body.identifier = http_body.identifier();
62 state->http_body.elements.resize(http_body.elementCount());
63 for (size_t i = 0; i < http_body.elementCount(); ++i) {
64 WebHTTPBody::Element element;
65 http_body.elementAt(i, element);
lukasza28b27bcc2016-05-25 23:45:3166 ConvertToHttpBodyElement(element, &state->http_body.elements[i]);
[email protected]1a852ab2013-06-25 03:10:2467 }
68 state->http_body.contains_passwords = http_body.containsPasswordData();
69 }
[email protected]1a852ab2013-06-25 03:10:2470}
71
creisbabe66042014-12-13 03:26:1172void RecursivelyGenerateFrameState(
73 HistoryEntry::HistoryNode* node,
74 ExplodedFrameState* state,
75 std::vector<base::NullableString16>* referenced_files) {
[email protected]9cd14ef2014-04-30 18:26:0376 GenerateFrameStateFromItem(node->item(), state);
creisbabe66042014-12-13 03:26:1177 ToNullableString16Vector(node->item().getReferencedFilePaths(),
78 referenced_files);
[email protected]9cd14ef2014-04-30 18:26:0379
80 std::vector<HistoryEntry::HistoryNode*>& children = node->children();
81 state->children.resize(children.size());
creisbabe66042014-12-13 03:26:1182 for (size_t i = 0; i < children.size(); ++i) {
83 RecursivelyGenerateFrameState(children[i], &state->children[i],
84 referenced_files);
85 }
[email protected]9cd14ef2014-04-30 18:26:0386}
87
88void RecursivelyGenerateHistoryItem(const ExplodedFrameState& state,
89 HistoryEntry::HistoryNode* node) {
90 WebHistoryItem item;
91 item.initialize();
92 item.setURLString(state.url_string);
93 item.setReferrer(state.referrer, state.referrer_policy);
94 item.setTarget(state.target);
[email protected]1a852ab2013-06-25 03:10:2495 if (!state.state_object.is_null()) {
[email protected]9cd14ef2014-04-30 18:26:0396 item.setStateObject(
[email protected]1a852ab2013-06-25 03:10:2497 WebSerializedScriptValue::fromString(state.state_object));
98 }
[email protected]9cd14ef2014-04-30 18:26:0399 item.setDocumentState(state.document_state);
majidvpbbac25a2015-05-13 14:26:09100 item.setScrollRestorationType(state.scroll_restoration_type);
jbroman0dacd112015-11-11 02:29:55101 item.setVisualViewportScrollOffset(state.visual_viewport_scroll_offset);
[email protected]9cd14ef2014-04-30 18:26:03102 item.setScrollOffset(state.scroll_offset);
103 item.setPageScaleFactor(state.page_scale_factor);
[email protected]1a852ab2013-06-25 03:10:24104
105 // These values are generated at WebHistoryItem construction time, and we
106 // only want to override those new values with old values if the old values
107 // are defined. A value of 0 means undefined in this context.
108 if (state.item_sequence_number)
[email protected]9cd14ef2014-04-30 18:26:03109 item.setItemSequenceNumber(state.item_sequence_number);
[email protected]1a852ab2013-06-25 03:10:24110 if (state.document_sequence_number)
[email protected]9cd14ef2014-04-30 18:26:03111 item.setDocumentSequenceNumber(state.document_sequence_number);
[email protected]1a852ab2013-06-25 03:10:24112
[email protected]9cd14ef2014-04-30 18:26:03113 item.setHTTPContentType(state.http_body.http_content_type);
[email protected]1a852ab2013-06-25 03:10:24114 if (!state.http_body.is_null) {
115 WebHTTPBody http_body;
116 http_body.initialize();
117 http_body.setIdentifier(state.http_body.identifier);
118 for (size_t i = 0; i < state.http_body.elements.size(); ++i)
lukasza28b27bcc2016-05-25 23:45:31119 AppendHttpBodyElement(state.http_body.elements[i], &http_body);
[email protected]9cd14ef2014-04-30 18:26:03120 item.setHTTPBody(http_body);
[email protected]1a852ab2013-06-25 03:10:24121 }
[email protected]9cd14ef2014-04-30 18:26:03122 node->set_item(item);
[email protected]1a852ab2013-06-25 03:10:24123
[email protected]9cd14ef2014-04-30 18:26:03124 for (size_t i = 0; i < state.children.size(); ++i)
125 RecursivelyGenerateHistoryItem(state.children[i], node->AddChild());
[email protected]1a852ab2013-06-25 03:10:24126}
127
128} // namespace
129
[email protected]9cd14ef2014-04-30 18:26:03130PageState HistoryEntryToPageState(HistoryEntry* entry) {
[email protected]1a852ab2013-06-25 03:10:24131 ExplodedPageState state;
creisbabe66042014-12-13 03:26:11132 RecursivelyGenerateFrameState(entry->root_history_node(), &state.top,
133 &state.referenced_files);
[email protected]1a852ab2013-06-25 03:10:24134
135 std::string encoded_data;
136 if (!EncodePageState(state, &encoded_data))
137 return PageState();
138
139 return PageState::CreateFromEncodedData(encoded_data);
140}
141
[email protected]9cd14ef2014-04-30 18:26:03142PageState SingleHistoryItemToPageState(const WebHistoryItem& item) {
143 ExplodedPageState state;
144 ToNullableString16Vector(item.getReferencedFilePaths(),
145 &state.referenced_files);
146 GenerateFrameStateFromItem(item, &state.top);
147
148 std::string encoded_data;
149 if (!EncodePageState(state, &encoded_data))
150 return PageState();
151
152 return PageState::CreateFromEncodedData(encoded_data);
153}
154
dchengcedca5612016-04-09 01:40:15155std::unique_ptr<HistoryEntry> PageStateToHistoryEntry(
156 const PageState& page_state) {
[email protected]1a852ab2013-06-25 03:10:24157 ExplodedPageState state;
158 if (!DecodePageState(page_state.ToEncodedData(), &state))
dchengcedca5612016-04-09 01:40:15159 return std::unique_ptr<HistoryEntry>();
[email protected]1a852ab2013-06-25 03:10:24160
dchengcedca5612016-04-09 01:40:15161 std::unique_ptr<HistoryEntry> entry(new HistoryEntry());
[email protected]9cd14ef2014-04-30 18:26:03162 RecursivelyGenerateHistoryItem(state.top, entry->root_history_node());
[email protected]1a852ab2013-06-25 03:10:24163
dcheng07945f632015-12-26 07:59:32164 return entry;
[email protected]691aa2f2013-05-28 22:52:04165}
166
167} // namespace content