[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" |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 10 | #include "content/common/page_state_serialization.h" |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 11 | #include "content/public/common/page_state.h" |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 12 | #include "content/renderer/history_entry.h" |
lukasza | 28b27bcc | 2016-05-25 23:45:31 | [diff] [blame^] | 13 | #include "content/renderer/http_body_conversions.h" |
| 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 | |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 33 | void ToNullableString16Vector(const WebVector<WebString>& input, |
| 34 | std::vector<base::NullableString16>* output) { |
creis | babe6604 | 2014-12-13 03:26:11 | [diff] [blame] | 35 | output->reserve(output->size() + input.size()); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 36 | for (size_t i = 0; i < input.size(); ++i) |
| 37 | output->push_back(input[i]); |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 38 | } |
| 39 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 40 | void GenerateFrameStateFromItem(const WebHistoryItem& item, |
| 41 | ExplodedFrameState* state) { |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 42 | state->url_string = item.urlString(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 43 | state->referrer = item.referrer(); |
[email protected] | bd2979f | 2014-01-30 13:30:04 | [diff] [blame] | 44 | state->referrer_policy = item.referrerPolicy(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 45 | state->target = item.target(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 46 | if (!item.stateObject().isNull()) |
| 47 | state->state_object = item.stateObject().toString(); |
majidvp | bbac25a | 2015-05-13 14:26:09 | [diff] [blame] | 48 | state->scroll_restoration_type = item.scrollRestorationType(); |
jbroman | 0dacd11 | 2015-11-11 02:29:55 | [diff] [blame] | 49 | state->visual_viewport_scroll_offset = item.visualViewportScrollOffset(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 50 | state->scroll_offset = item.scrollOffset(); |
| 51 | state->item_sequence_number = item.itemSequenceNumber(); |
| 52 | state->document_sequence_number = |
| 53 | item.documentSequenceNumber(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 54 | state->page_scale_factor = item.pageScaleFactor(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 55 | 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] | 176fe9a | 2014-07-08 02:31:53 | [diff] [blame] | 59 | state->http_body.is_null = http_body.isNull(); |
| 60 | if (!state->http_body.is_null) { |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 61 | 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); |
lukasza | 28b27bcc | 2016-05-25 23:45:31 | [diff] [blame^] | 66 | ConvertToHttpBodyElement(element, &state->http_body.elements[i]); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 67 | } |
| 68 | state->http_body.contains_passwords = http_body.containsPasswordData(); |
| 69 | } |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 70 | } |
| 71 | |
creis | babe6604 | 2014-12-13 03:26:11 | [diff] [blame] | 72 | void RecursivelyGenerateFrameState( |
| 73 | HistoryEntry::HistoryNode* node, |
| 74 | ExplodedFrameState* state, |
| 75 | std::vector<base::NullableString16>* referenced_files) { |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 76 | GenerateFrameStateFromItem(node->item(), state); |
creis | babe6604 | 2014-12-13 03:26:11 | [diff] [blame] | 77 | ToNullableString16Vector(node->item().getReferencedFilePaths(), |
| 78 | referenced_files); |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 79 | |
| 80 | std::vector<HistoryEntry::HistoryNode*>& children = node->children(); |
| 81 | state->children.resize(children.size()); |
creis | babe6604 | 2014-12-13 03:26:11 | [diff] [blame] | 82 | for (size_t i = 0; i < children.size(); ++i) { |
| 83 | RecursivelyGenerateFrameState(children[i], &state->children[i], |
| 84 | referenced_files); |
| 85 | } |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void 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] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 95 | if (!state.state_object.is_null()) { |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 96 | item.setStateObject( |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 97 | WebSerializedScriptValue::fromString(state.state_object)); |
| 98 | } |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 99 | item.setDocumentState(state.document_state); |
majidvp | bbac25a | 2015-05-13 14:26:09 | [diff] [blame] | 100 | item.setScrollRestorationType(state.scroll_restoration_type); |
jbroman | 0dacd11 | 2015-11-11 02:29:55 | [diff] [blame] | 101 | item.setVisualViewportScrollOffset(state.visual_viewport_scroll_offset); |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 102 | item.setScrollOffset(state.scroll_offset); |
| 103 | item.setPageScaleFactor(state.page_scale_factor); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 104 | |
| 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] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 109 | item.setItemSequenceNumber(state.item_sequence_number); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 110 | if (state.document_sequence_number) |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 111 | item.setDocumentSequenceNumber(state.document_sequence_number); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 112 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 113 | item.setHTTPContentType(state.http_body.http_content_type); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 114 | 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) |
lukasza | 28b27bcc | 2016-05-25 23:45:31 | [diff] [blame^] | 119 | AppendHttpBodyElement(state.http_body.elements[i], &http_body); |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 120 | item.setHTTPBody(http_body); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 121 | } |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 122 | node->set_item(item); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 123 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 124 | for (size_t i = 0; i < state.children.size(); ++i) |
| 125 | RecursivelyGenerateHistoryItem(state.children[i], node->AddChild()); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | } // namespace |
| 129 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 130 | PageState HistoryEntryToPageState(HistoryEntry* entry) { |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 131 | ExplodedPageState state; |
creis | babe6604 | 2014-12-13 03:26:11 | [diff] [blame] | 132 | RecursivelyGenerateFrameState(entry->root_history_node(), &state.top, |
| 133 | &state.referenced_files); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 134 | |
| 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] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 142 | PageState 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 | |
dcheng | cedca561 | 2016-04-09 01:40:15 | [diff] [blame] | 155 | std::unique_ptr<HistoryEntry> PageStateToHistoryEntry( |
| 156 | const PageState& page_state) { |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 157 | ExplodedPageState state; |
| 158 | if (!DecodePageState(page_state.ToEncodedData(), &state)) |
dcheng | cedca561 | 2016-04-09 01:40:15 | [diff] [blame] | 159 | return std::unique_ptr<HistoryEntry>(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 160 | |
dcheng | cedca561 | 2016-04-09 01:40:15 | [diff] [blame] | 161 | std::unique_ptr<HistoryEntry> entry(new HistoryEntry()); |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 162 | RecursivelyGenerateHistoryItem(state.top, entry->root_history_node()); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 163 | |
dcheng | 07945f63 | 2015-12-26 07:59:32 | [diff] [blame] | 164 | return entry; |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | } // namespace content |