[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/public/renderer/history_item_serialization.h" |
| 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] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 9 | #include "third_party/WebKit/public/platform/WebHTTPBody.h" |
| 10 | #include "third_party/WebKit/public/platform/WebPoint.h" |
| 11 | #include "third_party/WebKit/public/platform/WebString.h" |
| 12 | #include "third_party/WebKit/public/platform/WebVector.h" |
| 13 | #include "third_party/WebKit/public/web/WebHistoryItem.h" |
| 14 | #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" |
| 15 | |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 16 | using blink::WebHTTPBody; |
| 17 | using blink::WebHistoryItem; |
| 18 | using blink::WebSerializedScriptValue; |
| 19 | using blink::WebString; |
| 20 | using blink::WebVector; |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 21 | |
| 22 | namespace content { |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 23 | namespace { |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 24 | |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 25 | void ToNullableString16Vector(const WebVector<WebString>& input, |
| 26 | std::vector<base::NullableString16>* output) { |
| 27 | output->reserve(input.size()); |
| 28 | for (size_t i = 0; i < input.size(); ++i) |
| 29 | output->push_back(input[i]); |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 30 | } |
| 31 | |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 32 | void ToExplodedHttpBodyElement(const WebHTTPBody::Element& input, |
| 33 | ExplodedHttpBodyElement* output) { |
| 34 | switch (input.type) { |
| 35 | case WebHTTPBody::Element::TypeData: |
| 36 | output->data.assign(input.data.data(), input.data.size()); |
| 37 | break; |
| 38 | case WebHTTPBody::Element::TypeFile: |
| 39 | output->file_path = input.filePath; |
| 40 | output->file_start = input.fileStart; |
| 41 | output->file_length = input.fileLength; |
| 42 | output->file_modification_time = input.modificationTime; |
| 43 | break; |
[email protected] | 05ffe5c | 2013-09-10 18:29:34 | [diff] [blame] | 44 | case WebHTTPBody::Element::TypeFileSystemURL: |
[email protected] | 2d68191 | 2013-09-24 04:59:55 | [diff] [blame] | 45 | output->filesystem_url = input.fileSystemURL; |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 46 | output->file_start = input.fileStart; |
| 47 | output->file_length = input.fileLength; |
| 48 | output->file_modification_time = input.modificationTime; |
| 49 | break; |
| 50 | case WebHTTPBody::Element::TypeBlob: |
[email protected] | 2d68191 | 2013-09-24 04:59:55 | [diff] [blame] | 51 | output->blob_uuid = input.blobUUID.utf8(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 52 | break; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void AppendHTTPBodyElement(const ExplodedHttpBodyElement& element, |
| 57 | WebHTTPBody* http_body) { |
| 58 | switch (element.type) { |
| 59 | case WebHTTPBody::Element::TypeData: |
| 60 | http_body->appendData(element.data); |
| 61 | break; |
| 62 | case WebHTTPBody::Element::TypeFile: |
| 63 | http_body->appendFileRange( |
| 64 | element.file_path, |
| 65 | element.file_start, |
| 66 | element.file_length, |
| 67 | element.file_modification_time); |
| 68 | break; |
[email protected] | 05ffe5c | 2013-09-10 18:29:34 | [diff] [blame] | 69 | case WebHTTPBody::Element::TypeFileSystemURL: |
[email protected] | 2d68191 | 2013-09-24 04:59:55 | [diff] [blame] | 70 | http_body->appendFileSystemURLRange( |
[email protected] | 8438889 | 2013-09-07 04:20:18 | [diff] [blame] | 71 | element.filesystem_url, |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 72 | element.file_start, |
| 73 | element.file_length, |
| 74 | element.file_modification_time); |
| 75 | break; |
| 76 | case WebHTTPBody::Element::TypeBlob: |
[email protected] | 2d68191 | 2013-09-24 04:59:55 | [diff] [blame] | 77 | http_body->appendBlob(WebString::fromUTF8(element.blob_uuid)); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | bool RecursivelyGenerateFrameState(const WebHistoryItem& item, |
| 83 | ExplodedFrameState* state) { |
| 84 | state->url_string = item.urlString(); |
| 85 | state->original_url_string = item.originalURLString(); |
| 86 | state->referrer = item.referrer(); |
[email protected] | bd2979f | 2014-01-30 13:30:04 | [diff] [blame^] | 87 | state->referrer_policy = item.referrerPolicy(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 88 | state->target = item.target(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 89 | 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] | 804c8a5d | 2013-11-05 01:03:56 | [diff] [blame] | 95 | state->target_frame_id = item.targetFrameID(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 96 | state->page_scale_factor = item.pageScaleFactor(); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 97 | ToNullableString16Vector(item.documentState(), &state->document_state); |
| 98 | |
| 99 | state->http_body.http_content_type = item.httpContentType(); |
| 100 | const WebHTTPBody& http_body = item.httpBody(); |
| 101 | if (!(state->http_body.is_null = http_body.isNull())) { |
| 102 | state->http_body.identifier = http_body.identifier(); |
| 103 | state->http_body.elements.resize(http_body.elementCount()); |
| 104 | for (size_t i = 0; i < http_body.elementCount(); ++i) { |
| 105 | WebHTTPBody::Element element; |
| 106 | http_body.elementAt(i, element); |
| 107 | ToExplodedHttpBodyElement(element, &state->http_body.elements[i]); |
| 108 | } |
| 109 | state->http_body.contains_passwords = http_body.containsPasswordData(); |
| 110 | } |
| 111 | |
| 112 | const WebVector<WebHistoryItem>& children = item.children(); |
| 113 | state->children.resize(children.size()); |
| 114 | for (size_t i = 0; i < children.size(); ++i) { |
| 115 | if (!RecursivelyGenerateFrameState(children[i], &state->children[i])) |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | bool RecursivelyGenerateHistoryItem(const ExplodedFrameState& state, |
| 123 | WebHistoryItem* item) { |
| 124 | item->setURLString(state.url_string); |
| 125 | item->setOriginalURLString(state.original_url_string); |
[email protected] | bd2979f | 2014-01-30 13:30:04 | [diff] [blame^] | 126 | item->setReferrer(state.referrer, state.referrer_policy); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 127 | item->setTarget(state.target); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 128 | if (!state.state_object.is_null()) { |
| 129 | item->setStateObject( |
| 130 | WebSerializedScriptValue::fromString(state.state_object)); |
| 131 | } |
| 132 | item->setDocumentState(state.document_state); |
| 133 | item->setScrollOffset(state.scroll_offset); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 134 | item->setPageScaleFactor(state.page_scale_factor); |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 135 | |
| 136 | // These values are generated at WebHistoryItem construction time, and we |
| 137 | // only want to override those new values with old values if the old values |
| 138 | // are defined. A value of 0 means undefined in this context. |
| 139 | if (state.item_sequence_number) |
| 140 | item->setItemSequenceNumber(state.item_sequence_number); |
| 141 | if (state.document_sequence_number) |
| 142 | item->setDocumentSequenceNumber(state.document_sequence_number); |
| 143 | |
[email protected] | 804c8a5d | 2013-11-05 01:03:56 | [diff] [blame] | 144 | item->setTargetFrameID(state.target_frame_id); |
| 145 | |
[email protected] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 146 | item->setHTTPContentType(state.http_body.http_content_type); |
| 147 | if (!state.http_body.is_null) { |
| 148 | WebHTTPBody http_body; |
| 149 | http_body.initialize(); |
| 150 | http_body.setIdentifier(state.http_body.identifier); |
| 151 | for (size_t i = 0; i < state.http_body.elements.size(); ++i) |
| 152 | AppendHTTPBodyElement(state.http_body.elements[i], &http_body); |
| 153 | item->setHTTPBody(http_body); |
| 154 | } |
| 155 | |
| 156 | for (size_t i = 0; i < state.children.size(); ++i) { |
| 157 | WebHistoryItem child_item; |
| 158 | child_item.initialize(); |
| 159 | if (!RecursivelyGenerateHistoryItem(state.children[i], &child_item)) |
| 160 | return false; |
| 161 | item->appendToChildren(child_item); |
| 162 | } |
| 163 | |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | } // namespace |
| 168 | |
| 169 | PageState HistoryItemToPageState(const WebHistoryItem& item) { |
| 170 | ExplodedPageState state; |
| 171 | ToNullableString16Vector(item.getReferencedFilePaths(), |
| 172 | &state.referenced_files); |
| 173 | |
| 174 | if (!RecursivelyGenerateFrameState(item, &state.top)) |
| 175 | return PageState(); |
| 176 | |
| 177 | std::string encoded_data; |
| 178 | if (!EncodePageState(state, &encoded_data)) |
| 179 | return PageState(); |
| 180 | |
| 181 | return PageState::CreateFromEncodedData(encoded_data); |
| 182 | } |
| 183 | |
| 184 | WebHistoryItem PageStateToHistoryItem(const PageState& page_state) { |
| 185 | ExplodedPageState state; |
| 186 | if (!DecodePageState(page_state.ToEncodedData(), &state)) |
| 187 | return WebHistoryItem(); |
| 188 | |
| 189 | WebHistoryItem item; |
| 190 | item.initialize(); |
| 191 | if (!RecursivelyGenerateHistoryItem(state.top, &item)) |
| 192 | return WebHistoryItem(); |
| 193 | |
| 194 | return item; |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | } // namespace content |