OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/public/renderer/history_item_serialization.h" | 5 #include "content/public/renderer/history_item_serialization.h" |
6 | 6 |
| 7 #include "content/common/page_state_serialization.h" |
7 #include "content/public/common/page_state.h" | 8 #include "content/public/common/page_state.h" |
8 #include "webkit/glue/glue_serialize_deprecated.h" | 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 #include "webkit/base/file_path_string_conversions.h" |
| 16 |
| 17 using WebKit::WebHTTPBody; |
| 18 using WebKit::WebHistoryItem; |
| 19 using WebKit::WebSerializedScriptValue; |
| 20 using WebKit::WebString; |
| 21 using WebKit::WebVector; |
9 | 22 |
10 namespace content { | 23 namespace content { |
11 | 24 namespace { |
12 PageState HistoryItemToPageState(const WebKit::WebHistoryItem& item) { | 25 |
13 return PageState::CreateFromEncodedData( | 26 void ToFilePathVector(const WebVector<WebString>& input, |
14 webkit_glue::HistoryItemToString(item)); | 27 std::vector<base::FilePath>* output) { |
15 } | 28 output->reserve(input.size()); |
16 | 29 for (size_t i = 0; i < input.size(); ++i) |
17 WebKit::WebHistoryItem PageStateToHistoryItem(const PageState& state) { | 30 output->push_back(webkit_base::WebStringToFilePath(input[i])); |
18 return webkit_glue::HistoryItemFromString(state.ToEncodedData()); | 31 } |
| 32 |
| 33 void ToNullableString16Vector(const WebVector<WebString>& input, |
| 34 std::vector<NullableString16>* output) { |
| 35 output->reserve(input.size()); |
| 36 for (size_t i = 0; i < input.size(); ++i) |
| 37 output->push_back(input[i]); |
| 38 } |
| 39 |
| 40 void ToExplodedHttpBodyElement(const WebHTTPBody::Element& input, |
| 41 ExplodedHttpBodyElement* output) { |
| 42 switch (input.type) { |
| 43 case WebHTTPBody::Element::TypeData: |
| 44 output->data.assign(input.data.data(), input.data.size()); |
| 45 break; |
| 46 case WebHTTPBody::Element::TypeFile: |
| 47 output->file_path = webkit_base::WebStringToFilePath(input.filePath); |
| 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::TypeURL: |
| 53 output->url = input.url; |
| 54 output->file_start = input.fileStart; |
| 55 output->file_length = input.fileLength; |
| 56 output->file_modification_time = input.modificationTime; |
| 57 break; |
| 58 case WebHTTPBody::Element::TypeBlob: |
| 59 output->url = input.url; |
| 60 break; |
| 61 } |
| 62 } |
| 63 |
| 64 void AppendHTTPBodyElement(const ExplodedHttpBodyElement& element, |
| 65 WebHTTPBody* http_body) { |
| 66 switch (element.type) { |
| 67 case WebHTTPBody::Element::TypeData: |
| 68 http_body->appendData(element.data); |
| 69 break; |
| 70 case WebHTTPBody::Element::TypeFile: |
| 71 http_body->appendFileRange( |
| 72 webkit_base::FilePathToWebString(element.file_path), |
| 73 element.file_start, |
| 74 element.file_length, |
| 75 element.file_modification_time); |
| 76 break; |
| 77 case WebHTTPBody::Element::TypeURL: |
| 78 http_body->appendURLRange( |
| 79 element.url, |
| 80 element.file_start, |
| 81 element.file_length, |
| 82 element.file_modification_time); |
| 83 break; |
| 84 case WebHTTPBody::Element::TypeBlob: |
| 85 http_body->appendBlob(element.url); |
| 86 break; |
| 87 } |
| 88 } |
| 89 |
| 90 bool RecursivelyGenerateFrameState(const WebHistoryItem& item, |
| 91 ExplodedFrameState* exploded_frame_state) { |
| 92 exploded_frame_state->url_string = item.urlString(); |
| 93 exploded_frame_state->original_url_string = item.originalURLString(); |
| 94 exploded_frame_state->referrer = item.referrer(); |
| 95 exploded_frame_state->target = item.target(); |
| 96 exploded_frame_state->parent = item.parent(); |
| 97 exploded_frame_state->title = item.title(); |
| 98 exploded_frame_state->alternate_title = item.alternateTitle(); |
| 99 if (!item.stateObject().isNull()) |
| 100 exploded_frame_state->state_object = item.stateObject().toString(); |
| 101 exploded_frame_state->scroll_offset = item.scrollOffset(); |
| 102 exploded_frame_state->item_sequence_number = item.itemSequenceNumber(); |
| 103 exploded_frame_state->document_sequence_number = |
| 104 item.documentSequenceNumber(); |
| 105 exploded_frame_state->visit_count = item.visitCount(); |
| 106 exploded_frame_state->visited_time = item.lastVisitedTime(); |
| 107 exploded_frame_state->page_scale_factor = item.pageScaleFactor(); |
| 108 exploded_frame_state->is_target_item = item.isTargetItem(); |
| 109 ToNullableString16Vector(item.documentState(), |
| 110 &exploded_frame_state->document_state); |
| 111 |
| 112 exploded_frame_state->http_body.http_content_type = item.httpContentType(); |
| 113 const WebHTTPBody& http_body = item.httpBody(); |
| 114 if (!(exploded_frame_state->http_body.is_null = http_body.isNull())) { |
| 115 exploded_frame_state->http_body.identifier = http_body.identifier(); |
| 116 exploded_frame_state->http_body.elements.resize(http_body.elementCount()); |
| 117 for (size_t i = 0; i < http_body.elementCount(); ++i) { |
| 118 WebHTTPBody::Element element; |
| 119 http_body.elementAt(i, element); |
| 120 ToExplodedHttpBodyElement(element, |
| 121 &exploded_frame_state->http_body.elements[i]); |
| 122 } |
| 123 exploded_frame_state->http_body.contains_passwords = |
| 124 http_body.containsPasswordData(); |
| 125 } |
| 126 |
| 127 const WebVector<WebHistoryItem>& children = item.children(); |
| 128 exploded_frame_state->children.resize(children.size()); |
| 129 for (size_t i = 0; i < children.size(); ++i) { |
| 130 if (!RecursivelyGenerateFrameState(children[i], |
| 131 &exploded_frame_state->children[i])) |
| 132 return false; |
| 133 } |
| 134 |
| 135 return true; |
| 136 } |
| 137 |
| 138 bool RecursivelyGenerateHistoryItem( |
| 139 const ExplodedFrameState& exploded_frame_state, |
| 140 WebHistoryItem* item) { |
| 141 item->setURLString(exploded_frame_state.url_string); |
| 142 item->setOriginalURLString(exploded_frame_state.original_url_string); |
| 143 item->setReferrer(exploded_frame_state.referrer); |
| 144 item->setTarget(exploded_frame_state.target); |
| 145 item->setParent(exploded_frame_state.parent); |
| 146 item->setTitle(exploded_frame_state.title); |
| 147 item->setAlternateTitle(exploded_frame_state.alternate_title); |
| 148 if (!exploded_frame_state.state_object.is_null()) { |
| 149 item->setStateObject( |
| 150 WebSerializedScriptValue::fromString( |
| 151 exploded_frame_state.state_object)); |
| 152 } |
| 153 item->setDocumentState(exploded_frame_state.document_state); |
| 154 item->setScrollOffset(exploded_frame_state.scroll_offset); |
| 155 item->setVisitCount(exploded_frame_state.visit_count); |
| 156 item->setLastVisitedTime(exploded_frame_state.visited_time); |
| 157 item->setPageScaleFactor(exploded_frame_state.page_scale_factor); |
| 158 item->setIsTargetItem(exploded_frame_state.is_target_item); |
| 159 |
| 160 // These values are generated at WebHistoryItem construction time, and we |
| 161 // only want to override those new values with old values if the old values |
| 162 // are defined. A value of 0 means undefined in this context. |
| 163 if (exploded_frame_state.item_sequence_number) |
| 164 item->setItemSequenceNumber(exploded_frame_state.item_sequence_number); |
| 165 if (exploded_frame_state.document_sequence_number) { |
| 166 item->setDocumentSequenceNumber( |
| 167 exploded_frame_state.document_sequence_number); |
| 168 } |
| 169 |
| 170 item->setHTTPContentType(exploded_frame_state.http_body.http_content_type); |
| 171 if (!exploded_frame_state.http_body.is_null) { |
| 172 WebHTTPBody http_body; |
| 173 http_body.initialize(); |
| 174 http_body.setIdentifier(exploded_frame_state.http_body.identifier); |
| 175 for (size_t i = 0; i < exploded_frame_state.http_body.elements.size(); |
| 176 ++i) { |
| 177 AppendHTTPBodyElement(exploded_frame_state.http_body.elements[i], |
| 178 &http_body); |
| 179 } |
| 180 item->setHTTPBody(http_body); |
| 181 } |
| 182 |
| 183 for (size_t i = 0; i < exploded_frame_state.children.size(); ++i) { |
| 184 WebHistoryItem child_item; |
| 185 child_item.initialize(); |
| 186 if (!RecursivelyGenerateHistoryItem(exploded_frame_state.children[i], |
| 187 &child_item)) |
| 188 return false; |
| 189 item->appendToChildren(child_item); |
| 190 } |
| 191 |
| 192 return true; |
| 193 } |
| 194 |
| 195 } // namespace |
| 196 |
| 197 PageState HistoryItemToPageState(const WebHistoryItem& item) { |
| 198 ExplodedPageState exploded_page_state; |
| 199 ToFilePathVector(item.getReferencedFilePaths(), |
| 200 &exploded_page_state.referenced_files); |
| 201 |
| 202 if (!RecursivelyGenerateFrameState(item, &exploded_page_state.top)) |
| 203 return PageState(); |
| 204 |
| 205 std::string encoded_data; |
| 206 if (!EncodePageState(exploded_page_state, &encoded_data)) |
| 207 return PageState(); |
| 208 |
| 209 return PageState::CreateFromEncodedData(encoded_data); |
| 210 } |
| 211 |
| 212 WebHistoryItem PageStateToHistoryItem(const PageState& page_state) { |
| 213 ExplodedPageState exploded_page_state; |
| 214 if (!DecodePageState(page_state.ToEncodedData(), &exploded_page_state)) |
| 215 return WebHistoryItem(); |
| 216 |
| 217 WebHistoryItem item; |
| 218 item.initialize(); |
| 219 if (!RecursivelyGenerateHistoryItem(exploded_page_state.top, &item)) |
| 220 return WebHistoryItem(); |
| 221 |
| 222 return item; |
19 } | 223 } |
20 | 224 |
21 } // namespace content | 225 } // namespace content |
OLD | NEW |