blob: 7a8167bcfa01980e0e6432e2c403035eea49588e [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
[email protected]1a852ab2013-06-25 03:10:247#include "content/common/page_state_serialization.h"
[email protected]691aa2f2013-05-28 22:52:048#include "content/public/common/page_state.h"
[email protected]9cd14ef2014-04-30 18:26:039#include "content/renderer/history_entry.h"
[email protected]a728f0b2014-05-11 23:23:3710#include "third_party/WebKit/public/platform/WebFloatPoint.h"
[email protected]1a852ab2013-06-25 03:10:2411#include "third_party/WebKit/public/platform/WebHTTPBody.h"
12#include "third_party/WebKit/public/platform/WebPoint.h"
13#include "third_party/WebKit/public/platform/WebString.h"
14#include "third_party/WebKit/public/platform/WebVector.h"
15#include "third_party/WebKit/public/web/WebHistoryItem.h"
16#include "third_party/WebKit/public/web/WebSerializedScriptValue.h"
17
[email protected]180ef242013-11-07 06:50:4618using blink::WebHTTPBody;
19using blink::WebHistoryItem;
20using blink::WebSerializedScriptValue;
21using blink::WebString;
22using blink::WebVector;
[email protected]691aa2f2013-05-28 22:52:0423
24namespace content {
[email protected]1a852ab2013-06-25 03:10:2425namespace {
[email protected]691aa2f2013-05-28 22:52:0426
[email protected]1a852ab2013-06-25 03:10:2427void ToNullableString16Vector(const WebVector<WebString>& input,
28 std::vector<base::NullableString16>* output) {
29 output->reserve(input.size());
30 for (size_t i = 0; i < input.size(); ++i)
31 output->push_back(input[i]);
[email protected]691aa2f2013-05-28 22:52:0432}
33
[email protected]1a852ab2013-06-25 03:10:2434void ToExplodedHttpBodyElement(const WebHTTPBody::Element& input,
35 ExplodedHttpBodyElement* output) {
36 switch (input.type) {
37 case WebHTTPBody::Element::TypeData:
38 output->data.assign(input.data.data(), input.data.size());
39 break;
40 case WebHTTPBody::Element::TypeFile:
41 output->file_path = input.filePath;
42 output->file_start = input.fileStart;
43 output->file_length = input.fileLength;
44 output->file_modification_time = input.modificationTime;
45 break;
[email protected]05ffe5c2013-09-10 18:29:3446 case WebHTTPBody::Element::TypeFileSystemURL:
[email protected]2d681912013-09-24 04:59:5547 output->filesystem_url = input.fileSystemURL;
[email protected]1a852ab2013-06-25 03:10:2448 output->file_start = input.fileStart;
49 output->file_length = input.fileLength;
50 output->file_modification_time = input.modificationTime;
51 break;
52 case WebHTTPBody::Element::TypeBlob:
[email protected]2d681912013-09-24 04:59:5553 output->blob_uuid = input.blobUUID.utf8();
[email protected]1a852ab2013-06-25 03:10:2454 break;
55 }
56}
57
58void AppendHTTPBodyElement(const ExplodedHttpBodyElement& element,
59 WebHTTPBody* http_body) {
60 switch (element.type) {
61 case WebHTTPBody::Element::TypeData:
62 http_body->appendData(element.data);
63 break;
64 case WebHTTPBody::Element::TypeFile:
65 http_body->appendFileRange(
66 element.file_path,
67 element.file_start,
68 element.file_length,
69 element.file_modification_time);
70 break;
[email protected]05ffe5c2013-09-10 18:29:3471 case WebHTTPBody::Element::TypeFileSystemURL:
[email protected]2d681912013-09-24 04:59:5572 http_body->appendFileSystemURLRange(
[email protected]84388892013-09-07 04:20:1873 element.filesystem_url,
[email protected]1a852ab2013-06-25 03:10:2474 element.file_start,
75 element.file_length,
76 element.file_modification_time);
77 break;
78 case WebHTTPBody::Element::TypeBlob:
[email protected]2d681912013-09-24 04:59:5579 http_body->appendBlob(WebString::fromUTF8(element.blob_uuid));
[email protected]1a852ab2013-06-25 03:10:2480 break;
81 }
82}
83
[email protected]9cd14ef2014-04-30 18:26:0384void GenerateFrameStateFromItem(const WebHistoryItem& item,
85 ExplodedFrameState* state) {
[email protected]1a852ab2013-06-25 03:10:2486 state->url_string = item.urlString();
[email protected]1a852ab2013-06-25 03:10:2487 state->referrer = item.referrer();
[email protected]bd2979f2014-01-30 13:30:0488 state->referrer_policy = item.referrerPolicy();
[email protected]1a852ab2013-06-25 03:10:2489 state->target = item.target();
[email protected]1a852ab2013-06-25 03:10:2490 if (!item.stateObject().isNull())
91 state->state_object = item.stateObject().toString();
[email protected]a728f0b2014-05-11 23:23:3792 state->pinch_viewport_scroll_offset = item.pinchViewportScrollOffset();
[email protected]1a852ab2013-06-25 03:10:2493 state->scroll_offset = item.scrollOffset();
94 state->item_sequence_number = item.itemSequenceNumber();
95 state->document_sequence_number =
96 item.documentSequenceNumber();
[email protected]4e86dbc22014-07-28 20:44:0097 state->frame_sequence_number =
98 item.frameSequenceNumber();
[email protected]1a852ab2013-06-25 03:10:2499 state->page_scale_factor = item.pageScaleFactor();
[email protected]1a852ab2013-06-25 03:10:24100 ToNullableString16Vector(item.documentState(), &state->document_state);
101
102 state->http_body.http_content_type = item.httpContentType();
103 const WebHTTPBody& http_body = item.httpBody();
[email protected]176fe9a2014-07-08 02:31:53104 state->http_body.is_null = http_body.isNull();
105 if (!state->http_body.is_null) {
[email protected]1a852ab2013-06-25 03:10:24106 state->http_body.identifier = http_body.identifier();
107 state->http_body.elements.resize(http_body.elementCount());
108 for (size_t i = 0; i < http_body.elementCount(); ++i) {
109 WebHTTPBody::Element element;
110 http_body.elementAt(i, element);
111 ToExplodedHttpBodyElement(element, &state->http_body.elements[i]);
112 }
113 state->http_body.contains_passwords = http_body.containsPasswordData();
114 }
[email protected]1a852ab2013-06-25 03:10:24115}
116
[email protected]9cd14ef2014-04-30 18:26:03117void RecursivelyGenerateFrameState(HistoryEntry::HistoryNode* node,
118 ExplodedFrameState* state) {
119 GenerateFrameStateFromItem(node->item(), state);
120
121 std::vector<HistoryEntry::HistoryNode*>& children = node->children();
122 state->children.resize(children.size());
123 for (size_t i = 0; i < children.size(); ++i)
124 RecursivelyGenerateFrameState(children[i], &state->children[i]);
125}
126
127void RecursivelyGenerateHistoryItem(const ExplodedFrameState& state,
128 HistoryEntry::HistoryNode* node) {
129 WebHistoryItem item;
130 item.initialize();
131 item.setURLString(state.url_string);
132 item.setReferrer(state.referrer, state.referrer_policy);
133 item.setTarget(state.target);
[email protected]1a852ab2013-06-25 03:10:24134 if (!state.state_object.is_null()) {
[email protected]9cd14ef2014-04-30 18:26:03135 item.setStateObject(
[email protected]1a852ab2013-06-25 03:10:24136 WebSerializedScriptValue::fromString(state.state_object));
137 }
[email protected]9cd14ef2014-04-30 18:26:03138 item.setDocumentState(state.document_state);
[email protected]a728f0b2014-05-11 23:23:37139 item.setPinchViewportScrollOffset(state.pinch_viewport_scroll_offset);
[email protected]9cd14ef2014-04-30 18:26:03140 item.setScrollOffset(state.scroll_offset);
141 item.setPageScaleFactor(state.page_scale_factor);
[email protected]1a852ab2013-06-25 03:10:24142
143 // These values are generated at WebHistoryItem construction time, and we
144 // only want to override those new values with old values if the old values
145 // are defined. A value of 0 means undefined in this context.
146 if (state.item_sequence_number)
[email protected]9cd14ef2014-04-30 18:26:03147 item.setItemSequenceNumber(state.item_sequence_number);
[email protected]1a852ab2013-06-25 03:10:24148 if (state.document_sequence_number)
[email protected]9cd14ef2014-04-30 18:26:03149 item.setDocumentSequenceNumber(state.document_sequence_number);
[email protected]4e86dbc22014-07-28 20:44:00150 if (state.frame_sequence_number)
151 item.setFrameSequenceNumber(state.frame_sequence_number);
[email protected]1a852ab2013-06-25 03:10:24152
[email protected]9cd14ef2014-04-30 18:26:03153 item.setHTTPContentType(state.http_body.http_content_type);
[email protected]1a852ab2013-06-25 03:10:24154 if (!state.http_body.is_null) {
155 WebHTTPBody http_body;
156 http_body.initialize();
157 http_body.setIdentifier(state.http_body.identifier);
158 for (size_t i = 0; i < state.http_body.elements.size(); ++i)
159 AppendHTTPBodyElement(state.http_body.elements[i], &http_body);
[email protected]9cd14ef2014-04-30 18:26:03160 item.setHTTPBody(http_body);
[email protected]1a852ab2013-06-25 03:10:24161 }
[email protected]9cd14ef2014-04-30 18:26:03162 node->set_item(item);
[email protected]1a852ab2013-06-25 03:10:24163
[email protected]9cd14ef2014-04-30 18:26:03164 for (size_t i = 0; i < state.children.size(); ++i)
165 RecursivelyGenerateHistoryItem(state.children[i], node->AddChild());
[email protected]1a852ab2013-06-25 03:10:24166}
167
168} // namespace
169
[email protected]9cd14ef2014-04-30 18:26:03170PageState HistoryEntryToPageState(HistoryEntry* entry) {
[email protected]1a852ab2013-06-25 03:10:24171 ExplodedPageState state;
[email protected]9cd14ef2014-04-30 18:26:03172 ToNullableString16Vector(entry->root().getReferencedFilePaths(),
[email protected]1a852ab2013-06-25 03:10:24173 &state.referenced_files);
174
[email protected]9cd14ef2014-04-30 18:26:03175 RecursivelyGenerateFrameState(entry->root_history_node(), &state.top);
[email protected]1a852ab2013-06-25 03:10:24176
177 std::string encoded_data;
178 if (!EncodePageState(state, &encoded_data))
179 return PageState();
180
181 return PageState::CreateFromEncodedData(encoded_data);
182}
183
[email protected]9cd14ef2014-04-30 18:26:03184PageState SingleHistoryItemToPageState(const WebHistoryItem& item) {
185 ExplodedPageState state;
186 ToNullableString16Vector(item.getReferencedFilePaths(),
187 &state.referenced_files);
188 GenerateFrameStateFromItem(item, &state.top);
189
190 std::string encoded_data;
191 if (!EncodePageState(state, &encoded_data))
192 return PageState();
193
194 return PageState::CreateFromEncodedData(encoded_data);
195}
196
[email protected]477f1e72014-05-01 00:49:09197scoped_ptr<HistoryEntry> PageStateToHistoryEntry(const PageState& page_state) {
[email protected]1a852ab2013-06-25 03:10:24198 ExplodedPageState state;
199 if (!DecodePageState(page_state.ToEncodedData(), &state))
[email protected]477f1e72014-05-01 00:49:09200 return scoped_ptr<HistoryEntry>();
[email protected]1a852ab2013-06-25 03:10:24201
[email protected]477f1e72014-05-01 00:49:09202 scoped_ptr<HistoryEntry> entry(new HistoryEntry());
[email protected]9cd14ef2014-04-30 18:26:03203 RecursivelyGenerateHistoryItem(state.top, entry->root_history_node());
[email protected]1a852ab2013-06-25 03:10:24204
[email protected]477f1e72014-05-01 00:49:09205 return entry.Pass();
[email protected]691aa2f2013-05-28 22:52:04206}
207
208} // namespace content