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 |
| 16 using WebKit::WebHTTPBody; |
| 17 using WebKit::WebHistoryItem; |
| 18 using WebKit::WebSerializedScriptValue; |
| 19 using WebKit::WebString; |
| 20 using WebKit::WebVector; |
9 | 21 |
10 namespace content { | 22 namespace content { |
11 | 23 namespace { |
12 PageState HistoryItemToPageState(const WebKit::WebHistoryItem& item) { | 24 |
13 return PageState::CreateFromEncodedData( | 25 void ToNullableString16Vector(const WebVector<WebString>& input, |
14 webkit_glue::HistoryItemToString(item)); | 26 std::vector<base::NullableString16>* output) { |
15 } | 27 output->reserve(input.size()); |
16 | 28 for (size_t i = 0; i < input.size(); ++i) |
17 WebKit::WebHistoryItem PageStateToHistoryItem(const PageState& state) { | 29 output->push_back(input[i]); |
18 return webkit_glue::HistoryItemFromString(state.ToEncodedData()); | 30 } |
| 31 |
| 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; |
| 44 case WebHTTPBody::Element::TypeURL: |
| 45 output->url = input.url; |
| 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: |
| 51 output->url = input.url; |
| 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; |
| 69 case WebHTTPBody::Element::TypeURL: |
| 70 http_body->appendURLRange( |
| 71 element.url, |
| 72 element.file_start, |
| 73 element.file_length, |
| 74 element.file_modification_time); |
| 75 break; |
| 76 case WebHTTPBody::Element::TypeBlob: |
| 77 http_body->appendBlob(element.url); |
| 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; |
19 } | 203 } |
20 | 204 |
21 } // namespace content | 205 } // namespace content |
OLD | NEW |