[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 | |
| 16 | using WebKit::WebHTTPBody; |
| 17 | using WebKit::WebHistoryItem; |
| 18 | using WebKit::WebSerializedScriptValue; |
| 19 | using WebKit::WebString; |
| 20 | using WebKit::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] | 8438889 | 2013-09-07 04:20:18 | [diff] [blame] | 45 | output->filesystem_url = input.url; |
[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] | 8438889 | 2013-09-07 04:20:18 | [diff] [blame] | 51 | output->deprecated_blob_url = input.blobURL; |
[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] | 1a852ab | 2013-06-25 03:10:24 | [diff] [blame] | 70 | http_body->appendURLRange( |
[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] | 8438889 | 2013-09-07 04:20:18 | [diff] [blame] | 77 | http_body->appendBlob(element.deprecated_blob_url); |
[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(); |
| 87 | state->target = item.target(); |
| 88 | state->parent = item.parent(); |
| 89 | state->title = item.title(); |
| 90 | state->alternate_title = item.alternateTitle(); |
| 91 | if (!item.stateObject().isNull()) |
| 92 | state->state_object = item.stateObject().toString(); |
| 93 | state->scroll_offset = item.scrollOffset(); |
| 94 | state->item_sequence_number = item.itemSequenceNumber(); |
| 95 | state->document_sequence_number = |
| 96 | item.documentSequenceNumber(); |
| 97 | state->visit_count = item.visitCount(); |
| 98 | state->visited_time = item.lastVisitedTime(); |
| 99 | state->page_scale_factor = item.pageScaleFactor(); |
| 100 | state->is_target_item = item.isTargetItem(); |
| 101 | ToNullableString16Vector(item.documentState(), &state->document_state); |
| 102 | |
| 103 | state->http_body.http_content_type = item.httpContentType(); |
| 104 | const WebHTTPBody& http_body = item.httpBody(); |
| 105 | if (!(state->http_body.is_null = http_body.isNull())) { |
| 106 | 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 | } |
| 115 | |
| 116 | const WebVector<WebHistoryItem>& children = item.children(); |
| 117 | state->children.resize(children.size()); |
| 118 | for (size_t i = 0; i < children.size(); ++i) { |
| 119 | if (!RecursivelyGenerateFrameState(children[i], &state->children[i])) |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | bool RecursivelyGenerateHistoryItem(const ExplodedFrameState& state, |
| 127 | WebHistoryItem* item) { |
| 128 | item->setURLString(state.url_string); |
| 129 | item->setOriginalURLString(state.original_url_string); |
| 130 | item->setReferrer(state.referrer); |
| 131 | item->setTarget(state.target); |
| 132 | item->setParent(state.parent); |
| 133 | item->setTitle(state.title); |
| 134 | item->setAlternateTitle(state.alternate_title); |
| 135 | if (!state.state_object.is_null()) { |
| 136 | item->setStateObject( |
| 137 | WebSerializedScriptValue::fromString(state.state_object)); |
| 138 | } |
| 139 | item->setDocumentState(state.document_state); |
| 140 | item->setScrollOffset(state.scroll_offset); |
| 141 | item->setVisitCount(state.visit_count); |
| 142 | item->setLastVisitedTime(state.visited_time); |
| 143 | item->setPageScaleFactor(state.page_scale_factor); |
| 144 | item->setIsTargetItem(state.is_target_item); |
| 145 | |
| 146 | // These values are generated at WebHistoryItem construction time, and we |
| 147 | // only want to override those new values with old values if the old values |
| 148 | // are defined. A value of 0 means undefined in this context. |
| 149 | if (state.item_sequence_number) |
| 150 | item->setItemSequenceNumber(state.item_sequence_number); |
| 151 | if (state.document_sequence_number) |
| 152 | item->setDocumentSequenceNumber(state.document_sequence_number); |
| 153 | |
| 154 | item->setHTTPContentType(state.http_body.http_content_type); |
| 155 | if (!state.http_body.is_null) { |
| 156 | WebHTTPBody http_body; |
| 157 | http_body.initialize(); |
| 158 | http_body.setIdentifier(state.http_body.identifier); |
| 159 | for (size_t i = 0; i < state.http_body.elements.size(); ++i) |
| 160 | AppendHTTPBodyElement(state.http_body.elements[i], &http_body); |
| 161 | item->setHTTPBody(http_body); |
| 162 | } |
| 163 | |
| 164 | for (size_t i = 0; i < state.children.size(); ++i) { |
| 165 | WebHistoryItem child_item; |
| 166 | child_item.initialize(); |
| 167 | if (!RecursivelyGenerateHistoryItem(state.children[i], &child_item)) |
| 168 | return false; |
| 169 | item->appendToChildren(child_item); |
| 170 | } |
| 171 | |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | } // namespace |
| 176 | |
| 177 | PageState HistoryItemToPageState(const WebHistoryItem& item) { |
| 178 | ExplodedPageState state; |
| 179 | ToNullableString16Vector(item.getReferencedFilePaths(), |
| 180 | &state.referenced_files); |
| 181 | |
| 182 | if (!RecursivelyGenerateFrameState(item, &state.top)) |
| 183 | return PageState(); |
| 184 | |
| 185 | std::string encoded_data; |
| 186 | if (!EncodePageState(state, &encoded_data)) |
| 187 | return PageState(); |
| 188 | |
| 189 | return PageState::CreateFromEncodedData(encoded_data); |
| 190 | } |
| 191 | |
| 192 | WebHistoryItem PageStateToHistoryItem(const PageState& page_state) { |
| 193 | ExplodedPageState state; |
| 194 | if (!DecodePageState(page_state.ToEncodedData(), &state)) |
| 195 | return WebHistoryItem(); |
| 196 | |
| 197 | WebHistoryItem item; |
| 198 | item.initialize(); |
| 199 | if (!RecursivelyGenerateHistoryItem(state.top, &item)) |
| 200 | return WebHistoryItem(); |
| 201 | |
| 202 | return item; |
[email protected] | 691aa2f | 2013-05-28 22:52:04 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | } // namespace content |