• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
7 #include "content/common/page_state_serialization.h"
8 #include "content/public/common/page_state.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 blink::WebHTTPBody;
17 using blink::WebHistoryItem;
18 using blink::WebSerializedScriptValue;
19 using blink::WebString;
20 using blink::WebVector;
21 
22 namespace content {
23 namespace {
24 
ToNullableString16Vector(const WebVector<WebString> & input,std::vector<base::NullableString16> * output)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]);
30 }
31 
ToExplodedHttpBodyElement(const WebHTTPBody::Element & input,ExplodedHttpBodyElement * output)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::TypeFileSystemURL:
45       output->filesystem_url = input.fileSystemURL;
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->blob_uuid = input.blobUUID.utf8();
52       break;
53   }
54 }
55 
AppendHTTPBodyElement(const ExplodedHttpBodyElement & element,WebHTTPBody * http_body)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::TypeFileSystemURL:
70       http_body->appendFileSystemURLRange(
71           element.filesystem_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(WebString::fromUTF8(element.blob_uuid));
78       break;
79   }
80 }
81 
RecursivelyGenerateFrameState(const WebHistoryItem & item,ExplodedFrameState * state)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   if (!item.stateObject().isNull())
89     state->state_object = item.stateObject().toString();
90   state->scroll_offset = item.scrollOffset();
91   state->item_sequence_number = item.itemSequenceNumber();
92   state->document_sequence_number =
93       item.documentSequenceNumber();
94   state->target_frame_id = item.targetFrameID();
95   state->page_scale_factor = item.pageScaleFactor();
96   ToNullableString16Vector(item.documentState(), &state->document_state);
97 
98   state->http_body.http_content_type = item.httpContentType();
99   const WebHTTPBody& http_body = item.httpBody();
100   if (!(state->http_body.is_null = http_body.isNull())) {
101     state->http_body.identifier = http_body.identifier();
102     state->http_body.elements.resize(http_body.elementCount());
103     for (size_t i = 0; i < http_body.elementCount(); ++i) {
104       WebHTTPBody::Element element;
105       http_body.elementAt(i, element);
106       ToExplodedHttpBodyElement(element, &state->http_body.elements[i]);
107     }
108     state->http_body.contains_passwords = http_body.containsPasswordData();
109   }
110 
111   const WebVector<WebHistoryItem>& children = item.children();
112   state->children.resize(children.size());
113   for (size_t i = 0; i < children.size(); ++i) {
114     if (!RecursivelyGenerateFrameState(children[i], &state->children[i]))
115       return false;
116   }
117 
118   return true;
119 }
120 
RecursivelyGenerateHistoryItem(const ExplodedFrameState & state,WebHistoryItem * item)121 bool RecursivelyGenerateHistoryItem(const ExplodedFrameState& state,
122                                     WebHistoryItem* item) {
123   item->setURLString(state.url_string);
124   item->setOriginalURLString(state.original_url_string);
125   item->setReferrer(state.referrer);
126   item->setTarget(state.target);
127   if (!state.state_object.is_null()) {
128     item->setStateObject(
129         WebSerializedScriptValue::fromString(state.state_object));
130   }
131   item->setDocumentState(state.document_state);
132   item->setScrollOffset(state.scroll_offset);
133   item->setPageScaleFactor(state.page_scale_factor);
134 
135   // These values are generated at WebHistoryItem construction time, and we
136   // only want to override those new values with old values if the old values
137   // are defined.  A value of 0 means undefined in this context.
138   if (state.item_sequence_number)
139     item->setItemSequenceNumber(state.item_sequence_number);
140   if (state.document_sequence_number)
141     item->setDocumentSequenceNumber(state.document_sequence_number);
142 
143   item->setTargetFrameID(state.target_frame_id);
144 
145   item->setHTTPContentType(state.http_body.http_content_type);
146   if (!state.http_body.is_null) {
147     WebHTTPBody http_body;
148     http_body.initialize();
149     http_body.setIdentifier(state.http_body.identifier);
150     for (size_t i = 0; i < state.http_body.elements.size(); ++i)
151       AppendHTTPBodyElement(state.http_body.elements[i], &http_body);
152     item->setHTTPBody(http_body);
153   }
154 
155   for (size_t i = 0; i < state.children.size(); ++i) {
156     WebHistoryItem child_item;
157     child_item.initialize();
158     if (!RecursivelyGenerateHistoryItem(state.children[i], &child_item))
159       return false;
160     item->appendToChildren(child_item);
161   }
162 
163   return true;
164 }
165 
166 }  // namespace
167 
HistoryItemToPageState(const WebHistoryItem & item)168 PageState HistoryItemToPageState(const WebHistoryItem& item) {
169   ExplodedPageState state;
170   ToNullableString16Vector(item.getReferencedFilePaths(),
171                            &state.referenced_files);
172 
173   if (!RecursivelyGenerateFrameState(item, &state.top))
174     return PageState();
175 
176   std::string encoded_data;
177   if (!EncodePageState(state, &encoded_data))
178     return PageState();
179 
180   return PageState::CreateFromEncodedData(encoded_data);
181 }
182 
PageStateToHistoryItem(const PageState & page_state)183 WebHistoryItem PageStateToHistoryItem(const PageState& page_state) {
184   ExplodedPageState state;
185   if (!DecodePageState(page_state.ToEncodedData(), &state))
186     return WebHistoryItem();
187 
188   WebHistoryItem item;
189   item.initialize();
190   if (!RecursivelyGenerateHistoryItem(state.top, &item))
191     return WebHistoryItem();
192 
193   return item;
194 }
195 
196 }  // namespace content
197