blob: dd78609bc42065d0476184a1d5feddef91ef759a [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]1a852ab2013-06-25 03:10:2410#include "third_party/WebKit/public/platform/WebHTTPBody.h"
11#include "third_party/WebKit/public/platform/WebPoint.h"
12#include "third_party/WebKit/public/platform/WebString.h"
13#include "third_party/WebKit/public/platform/WebVector.h"
14#include "third_party/WebKit/public/web/WebHistoryItem.h"
15#include "third_party/WebKit/public/web/WebSerializedScriptValue.h"
16
[email protected]180ef242013-11-07 06:50:4617using blink::WebHTTPBody;
18using blink::WebHistoryItem;
19using blink::WebSerializedScriptValue;
20using blink::WebString;
21using blink::WebVector;
[email protected]691aa2f2013-05-28 22:52:0422
23namespace content {
[email protected]1a852ab2013-06-25 03:10:2424namespace {
[email protected]691aa2f2013-05-28 22:52:0425
[email protected]1a852ab2013-06-25 03:10:2426void ToNullableString16Vector(const WebVector<WebString>& input,
27 std::vector<base::NullableString16>* output) {
28 output->reserve(input.size());
29 for (size_t i = 0; i < input.size(); ++i)
30 output->push_back(input[i]);
[email protected]691aa2f2013-05-28 22:52:0431}
32
[email protected]1a852ab2013-06-25 03:10:2433void ToExplodedHttpBodyElement(const WebHTTPBody::Element& input,
34 ExplodedHttpBodyElement* output) {
35 switch (input.type) {
36 case WebHTTPBody::Element::TypeData:
37 output->data.assign(input.data.data(), input.data.size());
38 break;
39 case WebHTTPBody::Element::TypeFile:
40 output->file_path = input.filePath;
41 output->file_start = input.fileStart;
42 output->file_length = input.fileLength;
43 output->file_modification_time = input.modificationTime;
44 break;
[email protected]05ffe5c2013-09-10 18:29:3445 case WebHTTPBody::Element::TypeFileSystemURL:
[email protected]2d681912013-09-24 04:59:5546 output->filesystem_url = input.fileSystemURL;
[email protected]1a852ab2013-06-25 03:10:2447 output->file_start = input.fileStart;
48 output->file_length = input.fileLength;
49 output->file_modification_time = input.modificationTime;
50 break;
51 case WebHTTPBody::Element::TypeBlob:
[email protected]2d681912013-09-24 04:59:5552 output->blob_uuid = input.blobUUID.utf8();
[email protected]1a852ab2013-06-25 03:10:2453 break;
54 }
55}
56
57void AppendHTTPBodyElement(const ExplodedHttpBodyElement& element,
58 WebHTTPBody* http_body) {
59 switch (element.type) {
60 case WebHTTPBody::Element::TypeData:
61 http_body->appendData(element.data);
62 break;
63 case WebHTTPBody::Element::TypeFile:
64 http_body->appendFileRange(
65 element.file_path,
66 element.file_start,
67 element.file_length,
68 element.file_modification_time);
69 break;
[email protected]05ffe5c2013-09-10 18:29:3470 case WebHTTPBody::Element::TypeFileSystemURL:
[email protected]2d681912013-09-24 04:59:5571 http_body->appendFileSystemURLRange(
[email protected]84388892013-09-07 04:20:1872 element.filesystem_url,
[email protected]1a852ab2013-06-25 03:10:2473 element.file_start,
74 element.file_length,
75 element.file_modification_time);
76 break;
77 case WebHTTPBody::Element::TypeBlob:
[email protected]2d681912013-09-24 04:59:5578 http_body->appendBlob(WebString::fromUTF8(element.blob_uuid));
[email protected]1a852ab2013-06-25 03:10:2479 break;
80 }
81}
82
[email protected]9cd14ef2014-04-30 18:26:0383void GenerateFrameStateFromItem(const WebHistoryItem& item,
84 ExplodedFrameState* state) {
[email protected]1a852ab2013-06-25 03:10:2485 state->url_string = item.urlString();
[email protected]1a852ab2013-06-25 03:10:2486 state->referrer = item.referrer();
[email protected]bd2979f2014-01-30 13:30:0487 state->referrer_policy = item.referrerPolicy();
[email protected]1a852ab2013-06-25 03:10:2488 state->target = item.target();
[email protected]1a852ab2013-06-25 03:10:2489 if (!item.stateObject().isNull())
90 state->state_object = item.stateObject().toString();
91 state->scroll_offset = item.scrollOffset();
92 state->item_sequence_number = item.itemSequenceNumber();
93 state->document_sequence_number =
94 item.documentSequenceNumber();
[email protected]1a852ab2013-06-25 03:10:2495 state->page_scale_factor = item.pageScaleFactor();
[email protected]1a852ab2013-06-25 03:10:2496 ToNullableString16Vector(item.documentState(), &state->document_state);
97
98 state->http_body.http_content_type = item.httpContentType();
99 const WebHTTPBody& http_body = item.httpBody();
100 if (!(state->http_body.is_null = http_body.isNull())) {
101 state->http_body.identifier = http_body.identifier();
102 state->http_body.elements.resize(http_body.elementCount());
103 for (size_t i = 0; i < http_body.elementCount(); ++i) {
104 WebHTTPBody::Element element;
105 http_body.elementAt(i, element);
106 ToExplodedHttpBodyElement(element, &state->http_body.elements[i]);
107 }
108 state->http_body.contains_passwords = http_body.containsPasswordData();
109 }
[email protected]1a852ab2013-06-25 03:10:24110}
111
[email protected]9cd14ef2014-04-30 18:26:03112void RecursivelyGenerateFrameState(HistoryEntry::HistoryNode* node,
113 ExplodedFrameState* state) {
114 GenerateFrameStateFromItem(node->item(), state);
115
116 std::vector<HistoryEntry::HistoryNode*>& children = node->children();
117 state->children.resize(children.size());
118 for (size_t i = 0; i < children.size(); ++i)
119 RecursivelyGenerateFrameState(children[i], &state->children[i]);
120}
121
122void RecursivelyGenerateHistoryItem(const ExplodedFrameState& state,
123 HistoryEntry::HistoryNode* node) {
124 WebHistoryItem item;
125 item.initialize();
126 item.setURLString(state.url_string);
127 item.setReferrer(state.referrer, state.referrer_policy);
128 item.setTarget(state.target);
[email protected]1a852ab2013-06-25 03:10:24129 if (!state.state_object.is_null()) {
[email protected]9cd14ef2014-04-30 18:26:03130 item.setStateObject(
[email protected]1a852ab2013-06-25 03:10:24131 WebSerializedScriptValue::fromString(state.state_object));
132 }
[email protected]9cd14ef2014-04-30 18:26:03133 item.setDocumentState(state.document_state);
134 item.setScrollOffset(state.scroll_offset);
135 item.setPageScaleFactor(state.page_scale_factor);
[email protected]1a852ab2013-06-25 03:10:24136
137 // These values are generated at WebHistoryItem construction time, and we
138 // only want to override those new values with old values if the old values
139 // are defined. A value of 0 means undefined in this context.
140 if (state.item_sequence_number)
[email protected]9cd14ef2014-04-30 18:26:03141 item.setItemSequenceNumber(state.item_sequence_number);
[email protected]1a852ab2013-06-25 03:10:24142 if (state.document_sequence_number)
[email protected]9cd14ef2014-04-30 18:26:03143 item.setDocumentSequenceNumber(state.document_sequence_number);
[email protected]1a852ab2013-06-25 03:10:24144
[email protected]9cd14ef2014-04-30 18:26:03145 item.setHTTPContentType(state.http_body.http_content_type);
[email protected]1a852ab2013-06-25 03:10:24146 if (!state.http_body.is_null) {
147 WebHTTPBody http_body;
148 http_body.initialize();
149 http_body.setIdentifier(state.http_body.identifier);
150 for (size_t i = 0; i < state.http_body.elements.size(); ++i)
151 AppendHTTPBodyElement(state.http_body.elements[i], &http_body);
[email protected]9cd14ef2014-04-30 18:26:03152 item.setHTTPBody(http_body);
[email protected]1a852ab2013-06-25 03:10:24153 }
[email protected]9cd14ef2014-04-30 18:26:03154 node->set_item(item);
[email protected]1a852ab2013-06-25 03:10:24155
[email protected]9cd14ef2014-04-30 18:26:03156 for (size_t i = 0; i < state.children.size(); ++i)
157 RecursivelyGenerateHistoryItem(state.children[i], node->AddChild());
[email protected]1a852ab2013-06-25 03:10:24158}
159
160} // namespace
161
[email protected]9cd14ef2014-04-30 18:26:03162PageState HistoryEntryToPageState(HistoryEntry* entry) {
[email protected]1a852ab2013-06-25 03:10:24163 ExplodedPageState state;
[email protected]9cd14ef2014-04-30 18:26:03164 ToNullableString16Vector(entry->root().getReferencedFilePaths(),
[email protected]1a852ab2013-06-25 03:10:24165 &state.referenced_files);
166
[email protected]9cd14ef2014-04-30 18:26:03167 RecursivelyGenerateFrameState(entry->root_history_node(), &state.top);
[email protected]1a852ab2013-06-25 03:10:24168
169 std::string encoded_data;
170 if (!EncodePageState(state, &encoded_data))
171 return PageState();
172
173 return PageState::CreateFromEncodedData(encoded_data);
174}
175
[email protected]9cd14ef2014-04-30 18:26:03176PageState SingleHistoryItemToPageState(const WebHistoryItem& item) {
177 ExplodedPageState state;
178 ToNullableString16Vector(item.getReferencedFilePaths(),
179 &state.referenced_files);
180 GenerateFrameStateFromItem(item, &state.top);
181
182 std::string encoded_data;
183 if (!EncodePageState(state, &encoded_data))
184 return PageState();
185
186 return PageState::CreateFromEncodedData(encoded_data);
187}
188
[email protected]477f1e72014-05-01 00:49:09189scoped_ptr<HistoryEntry> PageStateToHistoryEntry(const PageState& page_state) {
[email protected]1a852ab2013-06-25 03:10:24190 ExplodedPageState state;
191 if (!DecodePageState(page_state.ToEncodedData(), &state))
[email protected]477f1e72014-05-01 00:49:09192 return scoped_ptr<HistoryEntry>();
[email protected]1a852ab2013-06-25 03:10:24193
[email protected]477f1e72014-05-01 00:49:09194 scoped_ptr<HistoryEntry> entry(new HistoryEntry());
[email protected]9cd14ef2014-04-30 18:26:03195 RecursivelyGenerateHistoryItem(state.top, entry->root_history_node());
[email protected]1a852ab2013-06-25 03:10:24196
[email protected]477f1e72014-05-01 00:49:09197 return entry.Pass();
[email protected]691aa2f2013-05-28 22:52:04198}
199
200} // namespace content