[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 | |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 7 | #include "content/common/page_state_serialization.h" |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 8 | #include "content/public/common/page_state.h" |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 9 | #include "content/renderer/history_entry.h" |
[email protected] | a728f0b | 2014-05-11 23:23:37 | [diff] [blame] | 10 | #include "third_party/WebKit/public/platform/WebFloatPoint.h" |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 11 | #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] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 18 | using blink::WebHTTPBody; |
| 19 | using blink::WebHistoryItem; |
| 20 | using blink::WebSerializedScriptValue; |
| 21 | using blink::WebString; |
| 22 | using blink::WebVector; |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 23 | |
| 24 | namespace content { |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 25 | namespace { |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 26 | |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 27 | void 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] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 32 | } |
| 33 | |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 34 | void 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] | 05ffe5c | 2013-09-10 18:29:34 | [diff] [blame] | 46 | case WebHTTPBody::Element::TypeFileSystemURL: |
[email protected] | 2d68191 | 2013-09-24 04:59:55 | [diff] [blame] | 47 | output->filesystem_url = input.fileSystemURL; |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 48 | 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] | 2d68191 | 2013-09-24 04:59:55 | [diff] [blame] | 53 | output->blob_uuid = input.blobUUID.utf8(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 54 | break; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void 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] | 05ffe5c | 2013-09-10 18:29:34 | [diff] [blame] | 71 | case WebHTTPBody::Element::TypeFileSystemURL: |
[email protected] | 2d68191 | 2013-09-24 04:59:55 | [diff] [blame] | 72 | http_body->appendFileSystemURLRange( |
[email protected] | 8438889 | 2013-09-07 04:20:18 | [diff] [blame] | 73 | element.filesystem_url, |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 74 | element.file_start, |
| 75 | element.file_length, |
| 76 | element.file_modification_time); |
| 77 | break; |
| 78 | case WebHTTPBody::Element::TypeBlob: |
[email protected] | 2d68191 | 2013-09-24 04:59:55 | [diff] [blame] | 79 | http_body->appendBlob(WebString::fromUTF8(element.blob_uuid)); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 84 | void GenerateFrameStateFromItem(const WebHistoryItem& item, |
| 85 | ExplodedFrameState* state) { |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 86 | state->url_string = item.urlString(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 87 | state->referrer = item.referrer(); |
[email protected] | bd2979f | 2014-01-30 13:30:04 | [diff] [blame] | 88 | state->referrer_policy = item.referrerPolicy(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 89 | state->target = item.target(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 90 | if (!item.stateObject().isNull()) |
| 91 | state->state_object = item.stateObject().toString(); |
[email protected] | a728f0b | 2014-05-11 23:23:37 | [diff] [blame] | 92 | state->pinch_viewport_scroll_offset = item.pinchViewportScrollOffset(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 93 | state->scroll_offset = item.scrollOffset(); |
| 94 | state->item_sequence_number = item.itemSequenceNumber(); |
| 95 | state->document_sequence_number = |
| 96 | item.documentSequenceNumber(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 97 | state->page_scale_factor = item.pageScaleFactor(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 98 | ToNullableString16Vector(item.documentState(), &state->document_state); |
| 99 | |
| 100 | state->http_body.http_content_type = item.httpContentType(); |
| 101 | const WebHTTPBody& http_body = item.httpBody(); |
[email protected] | 176fe9a | 2014-07-08 02:31:53 | [diff] [blame^] | 102 | state->http_body.is_null = http_body.isNull(); |
| 103 | if (!state->http_body.is_null) { |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 104 | state->http_body.identifier = http_body.identifier(); |
| 105 | state->http_body.elements.resize(http_body.elementCount()); |
| 106 | for (size_t i = 0; i < http_body.elementCount(); ++i) { |
| 107 | WebHTTPBody::Element element; |
| 108 | http_body.elementAt(i, element); |
| 109 | ToExplodedHttpBodyElement(element, &state->http_body.elements[i]); |
| 110 | } |
| 111 | state->http_body.contains_passwords = http_body.containsPasswordData(); |
| 112 | } |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 113 | } |
| 114 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 115 | void RecursivelyGenerateFrameState(HistoryEntry::HistoryNode* node, |
| 116 | ExplodedFrameState* state) { |
| 117 | GenerateFrameStateFromItem(node->item(), state); |
| 118 | |
| 119 | std::vector<HistoryEntry::HistoryNode*>& children = node->children(); |
| 120 | state->children.resize(children.size()); |
| 121 | for (size_t i = 0; i < children.size(); ++i) |
| 122 | RecursivelyGenerateFrameState(children[i], &state->children[i]); |
| 123 | } |
| 124 | |
| 125 | void RecursivelyGenerateHistoryItem(const ExplodedFrameState& state, |
| 126 | HistoryEntry::HistoryNode* node) { |
| 127 | WebHistoryItem item; |
| 128 | item.initialize(); |
| 129 | item.setURLString(state.url_string); |
| 130 | item.setReferrer(state.referrer, state.referrer_policy); |
| 131 | item.setTarget(state.target); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 132 | if (!state.state_object.is_null()) { |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 133 | item.setStateObject( |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 134 | WebSerializedScriptValue::fromString(state.state_object)); |
| 135 | } |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 136 | item.setDocumentState(state.document_state); |
[email protected] | a728f0b | 2014-05-11 23:23:37 | [diff] [blame] | 137 | item.setPinchViewportScrollOffset(state.pinch_viewport_scroll_offset); |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 138 | item.setScrollOffset(state.scroll_offset); |
| 139 | item.setPageScaleFactor(state.page_scale_factor); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 140 | |
| 141 | // These values are generated at WebHistoryItem construction time, and we |
| 142 | // only want to override those new values with old values if the old values |
| 143 | // are defined. A value of 0 means undefined in this context. |
| 144 | if (state.item_sequence_number) |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 145 | item.setItemSequenceNumber(state.item_sequence_number); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 146 | if (state.document_sequence_number) |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 147 | item.setDocumentSequenceNumber(state.document_sequence_number); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 148 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 149 | item.setHTTPContentType(state.http_body.http_content_type); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 150 | if (!state.http_body.is_null) { |
| 151 | WebHTTPBody http_body; |
| 152 | http_body.initialize(); |
| 153 | http_body.setIdentifier(state.http_body.identifier); |
| 154 | for (size_t i = 0; i < state.http_body.elements.size(); ++i) |
| 155 | AppendHTTPBodyElement(state.http_body.elements[i], &http_body); |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 156 | item.setHTTPBody(http_body); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 157 | } |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 158 | node->set_item(item); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 159 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 160 | for (size_t i = 0; i < state.children.size(); ++i) |
| 161 | RecursivelyGenerateHistoryItem(state.children[i], node->AddChild()); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | } // namespace |
| 165 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 166 | PageState HistoryEntryToPageState(HistoryEntry* entry) { |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 167 | ExplodedPageState state; |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 168 | ToNullableString16Vector(entry->root().getReferencedFilePaths(), |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 169 | &state.referenced_files); |
| 170 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 171 | RecursivelyGenerateFrameState(entry->root_history_node(), &state.top); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 172 | |
| 173 | std::string encoded_data; |
| 174 | if (!EncodePageState(state, &encoded_data)) |
| 175 | return PageState(); |
| 176 | |
| 177 | return PageState::CreateFromEncodedData(encoded_data); |
| 178 | } |
| 179 | |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 180 | PageState SingleHistoryItemToPageState(const WebHistoryItem& item) { |
| 181 | ExplodedPageState state; |
| 182 | ToNullableString16Vector(item.getReferencedFilePaths(), |
| 183 | &state.referenced_files); |
| 184 | GenerateFrameStateFromItem(item, &state.top); |
| 185 | |
| 186 | std::string encoded_data; |
| 187 | if (!EncodePageState(state, &encoded_data)) |
| 188 | return PageState(); |
| 189 | |
| 190 | return PageState::CreateFromEncodedData(encoded_data); |
| 191 | } |
| 192 | |
[email protected] | 477f1e7 | 2014-05-01 00:49:09 | [diff] [blame] | 193 | scoped_ptr<HistoryEntry> PageStateToHistoryEntry(const PageState& page_state) { |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 194 | ExplodedPageState state; |
| 195 | if (!DecodePageState(page_state.ToEncodedData(), &state)) |
[email protected] | 477f1e7 | 2014-05-01 00:49:09 | [diff] [blame] | 196 | return scoped_ptr<HistoryEntry>(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 197 | |
[email protected] | 477f1e7 | 2014-05-01 00:49:09 | [diff] [blame] | 198 | scoped_ptr<HistoryEntry> entry(new HistoryEntry()); |
[email protected] | 9cd14ef | 2014-04-30 18:26:03 | [diff] [blame] | 199 | RecursivelyGenerateHistoryItem(state.top, entry->root_history_node()); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 200 | |
[email protected] | 477f1e7 | 2014-05-01 00:49:09 | [diff] [blame] | 201 | return entry.Pass(); |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | } // namespace content |