OLD | NEW |
---|---|
1 // Copyright (c) 2012 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 "webkit/glue/glue_serialize_deprecated.h" | 5 #include "content/common/page_state_serialization.h" |
6 | 6 |
7 #include <string> | 7 #include <algorithm> |
8 #include <limits> | |
8 | 9 |
9 #include "base/pickle.h" | 10 #include "base/pickle.h" |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "base/strings/string_util.h" | |
10 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
11 #include "googleurl/src/gurl.h" | |
12 #include "third_party/WebKit/public/platform/WebData.h" | |
13 #include "third_party/WebKit/public/platform/WebHTTPBody.h" | |
14 #include "third_party/WebKit/public/platform/WebPoint.h" | |
15 #include "third_party/WebKit/public/platform/WebString.h" | |
16 #include "third_party/WebKit/public/platform/WebURL.h" | |
17 #include "third_party/WebKit/public/platform/WebVector.h" | |
18 #include "third_party/WebKit/public/web/WebHistoryItem.h" | |
19 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" | |
20 #include "ui/gfx/screen.h" | 14 #include "ui/gfx/screen.h" |
21 #include "webkit/base/file_path_string_conversions.h" | 15 |
22 | 16 namespace content { |
23 using WebKit::WebData; | |
24 using WebKit::WebHistoryItem; | |
25 using WebKit::WebHTTPBody; | |
26 using WebKit::WebPoint; | |
27 using WebKit::WebSerializedScriptValue; | |
28 using WebKit::WebString; | |
29 using WebKit::WebUChar; | |
30 using WebKit::WebVector; | |
31 | |
32 namespace webkit_glue { | |
33 | |
34 namespace { | 17 namespace { |
35 | 18 |
36 enum IncludeFormData { | 19 //----------------------------------------------------------------------------- |
37 NEVER_INCLUDE_FORM_DATA, | 20 |
38 INCLUDE_FORM_DATA_WITHOUT_PASSWORDS, | 21 void AppendDataToHttpBody(ExplodedHttpBody* http_body, const char* data, |
39 ALWAYS_INCLUDE_FORM_DATA | 22 int data_length) { |
40 }; | 23 ExplodedHttpBodyElement element; |
24 element.type = WebKit::WebHTTPBody::Element::TypeData; | |
25 element.data.assign(data, data_length); | |
26 http_body->elements.push_back(element); | |
27 } | |
28 | |
29 void AppendFileRangeToHttpBody(ExplodedHttpBody* http_body, | |
30 const base::NullableString16& file_path, | |
31 int file_start, | |
32 int file_length, | |
33 double file_modification_time) { | |
34 ExplodedHttpBodyElement element; | |
35 element.type = WebKit::WebHTTPBody::Element::TypeFile; | |
36 element.file_path = file_path; | |
37 element.file_start = file_start; | |
38 element.file_length = file_length; | |
39 element.file_modification_time = file_modification_time; | |
40 http_body->elements.push_back(element); | |
41 } | |
42 | |
43 void AppendURLRangeToHttpBody(ExplodedHttpBody* http_body, | |
44 const GURL& url, | |
45 int file_start, | |
46 int file_length, | |
47 double file_modification_time) { | |
48 ExplodedHttpBodyElement element; | |
49 element.type = WebKit::WebHTTPBody::Element::TypeURL; | |
50 element.url = url; | |
51 element.file_start = file_start; | |
52 element.file_length = file_length; | |
53 element.file_modification_time = file_modification_time; | |
54 http_body->elements.push_back(element); | |
55 } | |
56 | |
57 void AppendBlobToHttpBody(ExplodedHttpBody* http_body, const GURL& url) { | |
58 ExplodedHttpBodyElement element; | |
59 element.type = WebKit::WebHTTPBody::Element::TypeBlob; | |
60 element.url = url; | |
61 http_body->elements.push_back(element); | |
62 } | |
63 | |
64 //---------------------------------------------------------------------------- | |
65 | |
66 void ExtractReferencedFilesFromHttpBody( | |
67 const std::vector<ExplodedHttpBodyElement>& elements, | |
68 std::vector<base::NullableString16>* referenced_files) { | |
69 for (size_t i = 0; i < elements.size(); ++i) { | |
Tom Sepez
2013/06/24 17:19:57
nit: We trust the caller to pass in an empty vecto
darin (slow to review)
2013/06/24 18:25:44
This method is called recursively so appending is
| |
70 if (elements[i].type == WebKit::WebHTTPBody::Element::TypeFile) | |
71 referenced_files->push_back(elements[i].file_path); | |
72 } | |
73 } | |
74 | |
75 bool ExtractReferencedFilesFromDocumentState( | |
76 const std::vector<base::NullableString16>& document_state, | |
77 std::vector<base::NullableString16>* referenced_files) { | |
78 if (document_state.empty()) | |
79 return true; | |
80 | |
81 // This algorithm is adapted from Blink's core/html/FormController.cpp code. | |
82 // We only care about how that code worked when this code snapshot was taken | |
83 // as this code is only needed for backwards compat. | |
84 // | |
85 // For reference, see FormController::formStatesFromStateVector at: | |
86 // http://src.chromium.org/viewvc/blink/trunk/Source/core/html/FormController. cpp?pathrev=152274 | |
87 | |
88 size_t index = 0; | |
89 | |
90 if (document_state.size() < 3) | |
91 return false; | |
92 | |
93 index++; // Skip over magic signature. | |
94 index++; // Skip over form key. | |
95 | |
96 size_t item_count; | |
97 if (!base::StringToSizeT(document_state[index++].string(), &item_count)) | |
98 return false; | |
99 | |
100 while (item_count--) { | |
101 if (index + 1 >= document_state.size()) | |
102 return false; | |
103 | |
104 index++; // Skip over name. | |
105 const base::NullableString16& type = document_state[index++]; | |
106 | |
107 if (index >= document_state.size()) | |
108 return false; | |
109 | |
110 size_t value_size; | |
111 if (!base::StringToSizeT(document_state[index++].string(), &value_size)) | |
112 return false; | |
113 | |
114 if (index + value_size > document_state.size()) | |
Tom Sepez
2013/06/24 17:19:57
This may silently overflow.
darin (slow to review)
2013/06/24 18:25:44
Good catch. Note: the code in Blink has the same
| |
115 return false; | |
116 | |
117 if (EqualsASCII(type.string(), "file")) { | |
118 if (value_size != 2) | |
119 return false; | |
120 | |
121 referenced_files->push_back(document_state[index++]); | |
122 index++; // Skip over display name. | |
123 } else { | |
124 index += value_size; | |
125 } | |
126 } | |
127 | |
128 return true; | |
129 } | |
130 | |
131 bool RecursivelyExtractReferencedFiles( | |
132 const ExplodedFrameState& frame_state, | |
133 std::vector<base::NullableString16>* referenced_files) { | |
134 if (!frame_state.http_body.is_null) { | |
135 ExtractReferencedFilesFromHttpBody(frame_state.http_body.elements, | |
136 referenced_files); | |
137 } | |
138 | |
139 if (!ExtractReferencedFilesFromDocumentState(frame_state.document_state, | |
140 referenced_files)) | |
141 return false; | |
142 | |
143 for (size_t i = 0; i < frame_state.children.size(); ++i) { | |
144 if (!RecursivelyExtractReferencedFiles(frame_state.children[i], | |
145 referenced_files)) | |
146 return false; | |
147 } | |
148 | |
149 return true; | |
150 } | |
151 | |
152 //---------------------------------------------------------------------------- | |
41 | 153 |
42 struct SerializeObject { | 154 struct SerializeObject { |
43 SerializeObject() : version(0) {} | 155 SerializeObject() |
156 : version(0), | |
157 parse_error(false) { | |
158 } | |
159 | |
44 SerializeObject(const char* data, int len) | 160 SerializeObject(const char* data, int len) |
45 : pickle(data, len), version(0) { iter = PickleIterator(pickle); } | 161 : pickle(data, len), |
162 version(0), | |
163 parse_error(false) { | |
164 iter = PickleIterator(pickle); | |
165 } | |
46 | 166 |
47 std::string GetAsString() { | 167 std::string GetAsString() { |
48 return std::string(static_cast<const char*>(pickle.data()), pickle.size()); | 168 return std::string(static_cast<const char*>(pickle.data()), pickle.size()); |
49 } | 169 } |
50 | 170 |
51 Pickle pickle; | 171 Pickle pickle; |
52 mutable PickleIterator iter; | 172 PickleIterator iter; |
53 mutable int version; | 173 int version; |
174 bool parse_error; | |
54 }; | 175 }; |
55 | 176 |
56 // TODO(mpcomplete): obsolete versions 1 and 2 after 1/1/2008. | 177 // Version ID of serialized format. |
57 // Version ID used in reading/writing history items. | 178 // 11: Min version |
58 // 1: Initial revision. | 179 // 12: Adds support for contains_passwords in HTTP body |
59 // 2: Added case for NULL string versus "". Version 2 code can read Version 1 | |
60 // data, but not vice versa. | |
61 // 3: Version 2 was broken, it stored number of WebUChars, not number of bytes. | |
62 // This version checks and reads v1 and v2 correctly. | |
63 // 4: Adds support for storing FormData::identifier(). | |
64 // 5: Adds support for empty FormData | |
65 // 6: Adds support for documentSequenceNumbers | |
66 // 7: Adds support for stateObject | |
67 // 8: Adds support for file range and modification time | |
68 // 9: Adds support for itemSequenceNumbers | |
69 // 10: Adds support for blob | |
70 // 11: Adds support for pageScaleFactor | |
71 // 12: Adds support for hasPasswordData in HTTP body | |
72 // 13: Adds support for URL (FileSystem URL) | 180 // 13: Adds support for URL (FileSystem URL) |
73 // 14: Adds list of referenced files, version written only for first item. | 181 // 14: Adds list of referenced files, version written only for first item. |
74 // Should be const, but unit tests may modify it. | |
75 // | 182 // |
76 // NOTE: If the version is -1, then the pickle contains only a URL string. | 183 // NOTE: If the version is -1, then the pickle contains only a URL string. |
77 // See CreateHistoryStateForURL. | 184 // See ReadPageState. |
78 // | 185 // |
79 int kVersion = 14; | 186 const int kMinVersion = 11; |
80 | 187 const int kCurrentVersion = 14; |
81 // A bunch of convenience functions to read/write to SerializeObjects. | 188 |
82 // The serializers assume the input data is in the correct format and so does | 189 // A bunch of convenience functions to read/write to SerializeObjects. The |
83 // no error checking. | 190 // de-serializers assume the input data will be in the correct format and fall |
191 // back to returning safe defaults when not. | |
192 | |
84 void WriteData(const void* data, int length, SerializeObject* obj) { | 193 void WriteData(const void* data, int length, SerializeObject* obj) { |
85 obj->pickle.WriteData(static_cast<const char*>(data), length); | 194 obj->pickle.WriteData(static_cast<const char*>(data), length); |
86 } | 195 } |
87 | 196 |
88 void ReadData(const SerializeObject* obj, const void** data, int* length) { | 197 void ReadData(SerializeObject* obj, const void** data, int* length) { |
89 const char* tmp; | 198 const char* tmp; |
90 if (obj->pickle.ReadData(&obj->iter, &tmp, length)) { | 199 if (obj->pickle.ReadData(&obj->iter, &tmp, length)) { |
91 *data = tmp; | 200 *data = tmp; |
92 } else { | 201 } else { |
202 obj->parse_error = true; | |
93 *data = NULL; | 203 *data = NULL; |
94 *length = 0; | 204 *length = 0; |
95 } | 205 } |
96 } | 206 } |
97 | 207 |
98 bool ReadBytes(const SerializeObject* obj, const void** data, int length) { | 208 bool ReadBytes(SerializeObject* obj, const void** data, int length) { |
Tom Sepez
2013/06/24 17:19:57
nit: Is the source of truth about the success of t
darin (slow to review)
2013/06/24 18:25:44
It looks like ReadBytes is only called by ReadStri
| |
99 const char *tmp; | 209 const char *tmp; |
100 if (!obj->pickle.ReadBytes(&obj->iter, &tmp, length)) | 210 if (!obj->pickle.ReadBytes(&obj->iter, &tmp, length)) { |
101 return false; | 211 obj->parse_error = true; |
212 return false; | |
213 } | |
102 *data = tmp; | 214 *data = tmp; |
103 return true; | 215 return true; |
104 } | 216 } |
105 | 217 |
106 void WriteInteger(int data, SerializeObject* obj) { | 218 void WriteInteger(int data, SerializeObject* obj) { |
107 obj->pickle.WriteInt(data); | 219 obj->pickle.WriteInt(data); |
108 } | 220 } |
109 | 221 |
110 int ReadInteger(const SerializeObject* obj) { | 222 int ReadInteger(SerializeObject* obj) { |
111 int tmp; | 223 int tmp; |
112 if (obj->pickle.ReadInt(&obj->iter, &tmp)) | 224 if (obj->pickle.ReadInt(&obj->iter, &tmp)) |
113 return tmp; | 225 return tmp; |
226 obj->parse_error = true; | |
114 return 0; | 227 return 0; |
115 } | 228 } |
116 | 229 |
117 void ConsumeInteger(const SerializeObject* obj) { | 230 void ConsumeInteger(SerializeObject* obj) { |
118 int unused ALLOW_UNUSED = ReadInteger(obj); | 231 int unused ALLOW_UNUSED = ReadInteger(obj); |
119 } | 232 } |
120 | 233 |
121 void WriteInteger64(int64 data, SerializeObject* obj) { | 234 void WriteInteger64(int64 data, SerializeObject* obj) { |
122 obj->pickle.WriteInt64(data); | 235 obj->pickle.WriteInt64(data); |
123 } | 236 } |
124 | 237 |
125 int64 ReadInteger64(const SerializeObject* obj) { | 238 int64 ReadInteger64(SerializeObject* obj) { |
126 int64 tmp = 0; | 239 int64 tmp = 0; |
127 obj->pickle.ReadInt64(&obj->iter, &tmp); | 240 if (obj->pickle.ReadInt64(&obj->iter, &tmp)) |
128 return tmp; | 241 return tmp; |
242 obj->parse_error = true; | |
243 return 0; | |
129 } | 244 } |
130 | 245 |
131 void WriteReal(double data, SerializeObject* obj) { | 246 void WriteReal(double data, SerializeObject* obj) { |
132 WriteData(&data, sizeof(double), obj); | 247 WriteData(&data, sizeof(double), obj); |
133 } | 248 } |
134 | 249 |
135 double ReadReal(const SerializeObject* obj) { | 250 double ReadReal(SerializeObject* obj) { |
136 const void* tmp = NULL; | 251 const void* tmp = NULL; |
137 int length = 0; | 252 int length = 0; |
138 double value = 0.0; | 253 double value = 0.0; |
139 ReadData(obj, &tmp, &length); | 254 ReadData(obj, &tmp, &length); |
140 if (tmp && length >= static_cast<int>(sizeof(double))) { | 255 if (tmp && length >= static_cast<int>(sizeof(double))) { |
Tom Sepez
2013/06/24 17:19:57
nit: now that we have parse_error set by ReadData(
darin (slow to review)
2013/06/24 18:25:44
My goal was to avoid sprinkling parse_error checks
| |
141 // Use memcpy, as tmp may not be correctly aligned. | 256 // Use memcpy, as tmp may not be correctly aligned. |
142 memcpy(&value, tmp, sizeof(double)); | 257 memcpy(&value, tmp, sizeof(double)); |
258 } else { | |
259 obj->parse_error = true; | |
143 } | 260 } |
144 return value; | 261 return value; |
145 } | 262 } |
146 | 263 |
147 void WriteBoolean(bool data, SerializeObject* obj) { | 264 void WriteBoolean(bool data, SerializeObject* obj) { |
148 obj->pickle.WriteInt(data ? 1 : 0); | 265 obj->pickle.WriteInt(data ? 1 : 0); |
149 } | 266 } |
150 | 267 |
151 bool ReadBoolean(const SerializeObject* obj) { | 268 bool ReadBoolean(SerializeObject* obj) { |
152 bool tmp; | 269 bool tmp; |
153 if (obj->pickle.ReadBool(&obj->iter, &tmp)) | 270 if (obj->pickle.ReadBool(&obj->iter, &tmp)) |
154 return tmp; | 271 return tmp; |
272 obj->parse_error = true; | |
155 return false; | 273 return false; |
156 } | 274 } |
157 | 275 |
158 void WriteGURL(const GURL& url, SerializeObject* obj) { | 276 void WriteGURL(const GURL& url, SerializeObject* obj) { |
159 obj->pickle.WriteString(url.possibly_invalid_spec()); | 277 obj->pickle.WriteString(url.possibly_invalid_spec()); |
160 } | 278 } |
161 | 279 |
162 GURL ReadGURL(const SerializeObject* obj) { | 280 GURL ReadGURL(SerializeObject* obj) { |
163 std::string spec; | 281 std::string spec; |
164 if (obj->pickle.ReadString(&obj->iter, &spec)) | 282 if (obj->pickle.ReadString(&obj->iter, &spec)) |
165 return GURL(spec); | 283 return GURL(spec); |
284 obj->parse_error = true; | |
166 return GURL(); | 285 return GURL(); |
167 } | 286 } |
168 | 287 |
169 // Read/WriteString pickle the WebString as <int length><WebUChar* data>. | 288 // WriteString pickles the NullableString16 as <int length><char16* data>. |
170 // If length == -1, then the WebString itself is NULL (WebString()). | 289 // If length == -1, then the NullableString16 itself is null. Otherwise the |
171 // Otherwise the length is the number of WebUChars (not bytes) in the WebString. | 290 // length is the number of char16 (not bytes) in the NullableString16. |
172 void WriteString(const WebString& str, SerializeObject* obj) { | 291 void WriteString(const base::NullableString16& str, SerializeObject* obj) { |
173 base::string16 string = str; | 292 const char16* data = str.string().data(); |
Tom Sepez
2013/06/24 17:19:57
nit: can avoid computing these values until the el
darin (slow to review)
2013/06/24 18:25:44
Yes, good idea.
| |
174 const char16* data = string.data(); | 293 size_t length_in_bytes = str.string().length() * sizeof(char16); |
175 size_t length_in_uchars = string.length(); | 294 if (str.is_null()) { |
176 size_t length_in_bytes = length_in_uchars * sizeof(char16); | 295 obj->pickle.WriteInt(-1); |
177 switch (kVersion) { | 296 } else { |
178 case 1: | 297 obj->pickle.WriteInt(length_in_bytes); |
Tom Sepez
2013/06/24 17:19:57
May have a problem here with a 2GB string overflow
darin (slow to review)
2013/06/24 18:25:44
I'll add a CHECK_LT(length_in_bytes, INT_MAX)
| |
179 // Version 1 writes <length in bytes><string data>. | 298 obj->pickle.WriteBytes(data, length_in_bytes); |
180 // It saves WebString() and "" as "". | 299 } |
181 obj->pickle.WriteInt(length_in_bytes); | 300 } |
182 obj->pickle.WriteBytes(data, length_in_bytes); | 301 |
183 break; | 302 // This reads a serialized NullableString16 from obj. If a string can't be |
184 case 2: | 303 // read, NULL is returned. |
185 // Version 2 writes <length in WebUChar><string data>. | 304 const char16* ReadStringNoCopy(SerializeObject* obj, int* num_chars) { |
186 // It uses -1 in the length field to mean WebString(). | 305 int length_in_bytes; |
187 if (str.isNull()) { | 306 if (!obj->pickle.ReadInt(&obj->iter, &length_in_bytes)) { |
188 obj->pickle.WriteInt(-1); | 307 obj->parse_error = true; |
189 } else { | |
190 obj->pickle.WriteInt(length_in_uchars); | |
191 obj->pickle.WriteBytes(data, length_in_bytes); | |
192 } | |
193 break; | |
194 default: | |
195 // Version 3+ writes <length in bytes><string data>. | |
196 // It uses -1 in the length field to mean WebString(). | |
197 if (str.isNull()) { | |
198 obj->pickle.WriteInt(-1); | |
199 } else { | |
200 obj->pickle.WriteInt(length_in_bytes); | |
201 obj->pickle.WriteBytes(data, length_in_bytes); | |
202 } | |
203 break; | |
204 } | |
205 } | |
206 | |
207 // This reads a serialized WebString from obj. If a string can't be read, | |
208 // WebString() is returned. | |
209 const WebUChar* ReadStringNoCopy(const SerializeObject* obj, int* num_chars) { | |
210 int length; | |
211 | |
212 // Versions 1, 2, and 3 all start with an integer. | |
213 if (!obj->pickle.ReadInt(&obj->iter, &length)) | |
214 return NULL; | 308 return NULL; |
215 | 309 } |
216 // Starting with version 2, -1 means WebString(). | 310 |
217 if (length == -1) | 311 if (length_in_bytes == -1) |
218 return NULL; | 312 return NULL; |
Tom Sepez
2013/06/24 17:19:57
What about other negative values?
darin (slow to review)
2013/06/24 18:25:44
I changed that to "< 0"
| |
219 | 313 |
220 // In version 2, the length field was the length in WebUChars. | |
221 // In version 1 and 3 it is the length in bytes. | |
222 int bytes = length; | |
223 if (obj->version == 2) | |
224 bytes *= sizeof(WebUChar); | |
225 | |
226 const void* data; | 314 const void* data; |
227 if (!ReadBytes(obj, &data, bytes)) | 315 if (!ReadBytes(obj, &data, length_in_bytes)) { |
316 obj->parse_error = true; | |
228 return NULL; | 317 return NULL; |
318 } | |
229 | 319 |
230 if (num_chars) | 320 if (num_chars) |
231 *num_chars = bytes / sizeof(WebUChar); | 321 *num_chars = length_in_bytes / sizeof(char16); |
232 return static_cast<const WebUChar*>(data); | 322 return static_cast<const char16*>(data); |
233 } | 323 } |
234 | 324 |
235 WebString ReadString(const SerializeObject* obj) { | 325 base::NullableString16 ReadString(SerializeObject* obj) { |
236 int num_chars; | 326 int num_chars; |
237 const WebUChar* chars = ReadStringNoCopy(obj, &num_chars); | 327 const char16* chars = ReadStringNoCopy(obj, &num_chars); |
238 return chars ? WebString(chars, num_chars) : WebString(); | 328 return chars ? |
239 } | 329 base::NullableString16(base::string16(chars, num_chars), false) : |
240 | 330 base::NullableString16(); |
241 void ConsumeString(const SerializeObject* obj) { | 331 } |
242 const WebUChar* unused ALLOW_UNUSED = ReadStringNoCopy(obj, NULL); | 332 |
243 } | 333 void ConsumeString(SerializeObject* obj) { |
244 | 334 const char16* unused ALLOW_UNUSED = ReadStringNoCopy(obj, NULL); |
245 // Writes a Vector of Strings into a SerializeObject for serialization. | 335 } |
336 | |
337 template <typename T> | |
338 void WriteAndValidateVectorSize(const std::vector<T>& v, SerializeObject* obj) { | |
339 CHECK_LT(v.size(), INT_MAX / sizeof(T)); | |
340 WriteInteger(static_cast<int>(v.size()), obj); | |
341 } | |
342 | |
343 size_t ReadAndValidateVectorSize(SerializeObject* obj, size_t element_size) { | |
344 size_t num_elements = static_cast<size_t>(ReadInteger(obj)); | |
345 | |
346 // Ensure that resizing a vector to size num_elements makes sense. | |
347 if (INT_MAX / element_size <= num_elements) { | |
348 obj->parse_error = true; | |
349 return 0; | |
350 } | |
351 | |
352 // Ensure that it is plausible for the pickle to contain num_elements worth | |
353 // of data. | |
354 if (obj->pickle.payload_size() <= num_elements) { | |
355 obj->parse_error = true; | |
356 return 0; | |
357 } | |
358 | |
359 return num_elements; | |
360 } | |
361 | |
362 // Writes a Vector of strings into a SerializeObject for serialization. | |
246 void WriteStringVector( | 363 void WriteStringVector( |
247 const WebVector<WebString>& data, SerializeObject* obj) { | 364 const std::vector<base::NullableString16>& data, SerializeObject* obj) { |
248 WriteInteger(static_cast<int>(data.size()), obj); | 365 WriteAndValidateVectorSize(data, obj); |
249 for (size_t i = 0, c = data.size(); i < c; ++i) { | 366 for (size_t i = 0; i < data.size(); ++i) { |
250 unsigned ui = static_cast<unsigned>(i); // sigh | 367 WriteString(data[i], obj); |
251 WriteString(data[ui], obj); | 368 } |
252 } | 369 } |
253 } | 370 |
254 | 371 void ReadStringVector(SerializeObject* obj, |
255 WebVector<WebString> ReadStringVector(const SerializeObject* obj) { | 372 std::vector<base::NullableString16>* result) { |
256 int num_elements = ReadInteger(obj); | 373 size_t num_elements = |
257 WebVector<WebString> result(static_cast<size_t>(num_elements)); | 374 ReadAndValidateVectorSize(obj, sizeof(base::NullableString16)); |
258 for (int i = 0; i < num_elements; ++i) | 375 |
259 result[i] = ReadString(obj); | 376 result->resize(num_elements); |
260 return result; | 377 for (size_t i = 0; i < num_elements; ++i) |
261 } | 378 (*result)[i] = ReadString(obj); |
262 | 379 } |
263 void ConsumeStringVector(const SerializeObject* obj) { | 380 |
264 int num_elements = ReadInteger(obj); | 381 // Writes an ExplodedHttpBody object into a SerializeObject for serialization. |
265 for (int i = 0; i < num_elements; ++i) | 382 void WriteHttpBody(const ExplodedHttpBody& http_body, SerializeObject* obj) { |
266 ConsumeString(obj); | 383 WriteBoolean(!http_body.is_null, obj); |
267 } | 384 |
268 | 385 if (http_body.is_null) |
269 // Writes a FormData object into a SerializeObject for serialization. | |
270 void WriteFormData(const WebHTTPBody& http_body, SerializeObject* obj) { | |
271 WriteBoolean(!http_body.isNull(), obj); | |
272 | |
273 if (http_body.isNull()) | |
274 return; | 386 return; |
275 | 387 |
276 WriteInteger(static_cast<int>(http_body.elementCount()), obj); | 388 WriteAndValidateVectorSize(http_body.elements, obj); |
277 WebHTTPBody::Element element; | 389 for (size_t i = 0; i < http_body.elements.size(); ++i) { |
278 for (size_t i = 0; http_body.elementAt(i, element); ++i) { | 390 const ExplodedHttpBodyElement& element = http_body.elements[i]; |
279 WriteInteger(element.type, obj); | 391 WriteInteger(element.type, obj); |
280 if (element.type == WebHTTPBody::Element::TypeData) { | 392 if (element.type == WebKit::WebHTTPBody::Element::TypeData) { |
281 WriteData(element.data.data(), static_cast<int>(element.data.size()), | 393 WriteData(element.data.data(), static_cast<int>(element.data.size()), |
282 obj); | 394 obj); |
283 } else if (element.type == WebHTTPBody::Element::TypeFile) { | 395 } else if (element.type == WebKit::WebHTTPBody::Element::TypeFile) { |
284 WriteString(element.filePath, obj); | 396 WriteString(element.file_path, obj); |
285 WriteInteger64(element.fileStart, obj); | 397 WriteInteger64(element.file_start, obj); |
286 WriteInteger64(element.fileLength, obj); | 398 WriteInteger64(element.file_length, obj); |
287 WriteReal(element.modificationTime, obj); | 399 WriteReal(element.file_modification_time, obj); |
288 } else if (element.type == WebHTTPBody::Element::TypeURL) { | 400 } else if (element.type == WebKit::WebHTTPBody::Element::TypeURL) { |
289 WriteGURL(element.url, obj); | 401 WriteGURL(element.url, obj); |
290 WriteInteger64(element.fileStart, obj); | 402 WriteInteger64(element.file_start, obj); |
291 WriteInteger64(element.fileLength, obj); | 403 WriteInteger64(element.file_length, obj); |
292 WriteReal(element.modificationTime, obj); | 404 WriteReal(element.file_modification_time, obj); |
293 } else { | 405 } else { |
294 WriteGURL(element.url, obj); | 406 WriteGURL(element.url, obj); |
295 } | 407 } |
296 } | 408 } |
297 WriteInteger64(http_body.identifier(), obj); | 409 WriteInteger64(http_body.identifier, obj); |
298 WriteBoolean(http_body.containsPasswordData(), obj); | 410 WriteBoolean(http_body.contains_passwords, obj); |
299 } | 411 } |
300 | 412 |
301 WebHTTPBody ReadFormData(const SerializeObject* obj) { | 413 void ReadHttpBody(SerializeObject* obj, ExplodedHttpBody* http_body) { |
302 // In newer versions, an initial boolean indicates if we have form data. | 414 // An initial boolean indicates if we have an HTTP body. |
303 if (obj->version >= 5 && !ReadBoolean(obj)) | 415 if (!ReadBoolean(obj)) |
304 return WebHTTPBody(); | 416 return; |
305 | 417 http_body->is_null = false; |
306 // In older versions, 0 elements implied no form data. | 418 |
307 int num_elements = ReadInteger(obj); | 419 int num_elements = ReadInteger(obj); |
308 if (num_elements == 0 && obj->version < 5) | |
309 return WebHTTPBody(); | |
310 | |
311 WebHTTPBody http_body; | |
312 http_body.initialize(); | |
313 | 420 |
314 for (int i = 0; i < num_elements; ++i) { | 421 for (int i = 0; i < num_elements; ++i) { |
315 int type = ReadInteger(obj); | 422 int type = ReadInteger(obj); |
316 if (type == WebHTTPBody::Element::TypeData) { | 423 if (type == WebKit::WebHTTPBody::Element::TypeData) { |
317 const void* data; | 424 const void* data; |
318 int length = -1; | 425 int length = -1; |
319 ReadData(obj, &data, &length); | 426 ReadData(obj, &data, &length); |
320 if (length >= 0) | 427 if (length >= 0) { |
321 http_body.appendData(WebData(static_cast<const char*>(data), length)); | 428 AppendDataToHttpBody(http_body, static_cast<const char*>(data), |
322 } else if (type == WebHTTPBody::Element::TypeFile) { | 429 length); |
323 WebString file_path = ReadString(obj); | |
324 long long file_start = 0; | |
325 long long file_length = -1; | |
326 double modification_time = 0.0; | |
327 if (obj->version >= 8) { | |
328 file_start = ReadInteger64(obj); | |
329 file_length = ReadInteger64(obj); | |
330 modification_time = ReadReal(obj); | |
331 } | 430 } |
332 http_body.appendFileRange(file_path, file_start, file_length, | 431 } else if (type == WebKit::WebHTTPBody::Element::TypeFile) { |
333 modification_time); | 432 base::NullableString16 file_path = ReadString(obj); |
334 } else if (type == WebHTTPBody::Element::TypeURL) { | 433 int64 file_start = ReadInteger64(obj); |
434 int64 file_length = ReadInteger64(obj); | |
435 double file_modification_time = ReadReal(obj); | |
436 AppendFileRangeToHttpBody(http_body, file_path, file_start, file_length, | |
437 file_modification_time); | |
438 } else if (type == WebKit::WebHTTPBody::Element::TypeURL) { | |
335 GURL url = ReadGURL(obj); | 439 GURL url = ReadGURL(obj); |
336 long long file_start = 0; | 440 int64 file_start = ReadInteger64(obj); |
337 long long file_length = -1; | 441 int64 file_length = ReadInteger64(obj); |
338 double modification_time = 0.0; | 442 double file_modification_time = ReadReal(obj); |
339 file_start = ReadInteger64(obj); | 443 AppendURLRangeToHttpBody(http_body, url, file_start, file_length, |
340 file_length = ReadInteger64(obj); | 444 file_modification_time); |
341 modification_time = ReadReal(obj); | 445 } else if (type == WebKit::WebHTTPBody::Element::TypeBlob) { |
342 http_body.appendURLRange(url, file_start, file_length, | |
343 modification_time); | |
344 } else if (obj->version >= 10) { | |
345 GURL blob_url = ReadGURL(obj); | 446 GURL blob_url = ReadGURL(obj); |
346 http_body.appendBlob(blob_url); | 447 AppendBlobToHttpBody(http_body, blob_url); |
347 } | 448 } |
348 } | 449 } |
349 if (obj->version >= 4) | 450 http_body->identifier = ReadInteger64(obj); |
350 http_body.setIdentifier(ReadInteger64(obj)); | |
351 | 451 |
352 if (obj->version >= 12) | 452 if (obj->version >= 12) |
353 http_body.setContainsPasswordData(ReadBoolean(obj)); | 453 http_body->contains_passwords = ReadBoolean(obj); |
354 | 454 } |
355 return http_body; | 455 |
356 } | 456 // Writes the ExplodedFrameState data into the SerializeObject object for |
357 | |
358 // Writes the HistoryItem data into the SerializeObject object for | |
359 // serialization. | 457 // serialization. |
360 void WriteHistoryItem( | 458 void WriteFrameState( |
361 const WebHistoryItem& item, SerializeObject* obj, bool is_top) { | 459 const ExplodedFrameState& state, SerializeObject* obj, bool is_top) { |
362 // WARNING: This data may be persisted for later use. As such, care must be | 460 // WARNING: This data may be persisted for later use. As such, care must be |
363 // taken when changing the serialized format. If a new field needs to be | 461 // taken when changing the serialized format. If a new field needs to be |
364 // written, only adding at the end will make it easier to deal with loading | 462 // written, only adding at the end will make it easier to deal with loading |
365 // older versions. Similarly, this should NOT save fields with sensitive | 463 // older versions. Similarly, this should NOT save fields with sensitive |
366 // data, such as password fields. | 464 // data, such as password fields. |
367 | 465 |
368 if (kVersion >= 14) { | 466 WriteString(state.url_string, obj); |
369 if (is_top) { | 467 WriteString(state.original_url_string, obj); |
370 WriteInteger(kVersion, obj); | 468 WriteString(state.target, obj); |
371 | 469 WriteString(state.parent, obj); |
372 // Insert the list of referenced files, so they can be extracted easily | 470 WriteString(state.title, obj); |
373 // from the serialized data (avoiding the need to call into Blink again). | 471 WriteString(state.alternate_title, obj); |
374 WriteStringVector(item.getReferencedFilePaths(), obj); | 472 WriteReal(state.visited_time, obj); |
375 } | 473 WriteInteger(state.scroll_offset.x(), obj); |
376 } else { | 474 WriteInteger(state.scroll_offset.y(), obj); |
377 WriteInteger(kVersion, obj); | 475 WriteBoolean(state.is_target_item, obj); |
378 } | 476 WriteInteger(state.visit_count, obj); |
379 | 477 WriteString(state.referrer, obj); |
380 WriteString(item.urlString(), obj); | 478 |
381 WriteString(item.originalURLString(), obj); | 479 WriteStringVector(state.document_state, obj); |
382 WriteString(item.target(), obj); | 480 |
383 WriteString(item.parent(), obj); | 481 WriteReal(state.page_scale_factor, obj); |
384 WriteString(item.title(), obj); | 482 WriteInteger64(state.item_sequence_number, obj); |
385 WriteString(item.alternateTitle(), obj); | 483 WriteInteger64(state.document_sequence_number, obj); |
386 WriteReal(item.lastVisitedTime(), obj); | 484 |
387 WriteInteger(item.scrollOffset().x, obj); | 485 bool has_state_object = !state.state_object.is_null(); |
388 WriteInteger(item.scrollOffset().y, obj); | 486 WriteBoolean(has_state_object, obj); |
389 WriteBoolean(item.isTargetItem(), obj); | 487 if (has_state_object) |
390 WriteInteger(item.visitCount(), obj); | 488 WriteString(state.state_object, obj); |
391 WriteString(item.referrer(), obj); | 489 |
392 | 490 WriteHttpBody(state.http_body, obj); |
393 WriteStringVector(item.documentState(), obj); | 491 |
394 | 492 // NOTE: It is a quirk of the format that we still have to write the |
395 if (kVersion >= 11) | 493 // http_content_type field when the HTTP body is null. That's why this code |
396 WriteReal(item.pageScaleFactor(), obj); | 494 // is here instead of inside WriteHttpBody. |
397 if (kVersion >= 9) | 495 WriteString(state.http_body.http_content_type, obj); |
398 WriteInteger64(item.itemSequenceNumber(), obj); | |
399 if (kVersion >= 6) | |
400 WriteInteger64(item.documentSequenceNumber(), obj); | |
401 if (kVersion >= 7) { | |
402 bool has_state_object = !item.stateObject().isNull(); | |
403 WriteBoolean(has_state_object, obj); | |
404 if (has_state_object) | |
405 WriteString(item.stateObject().toString(), obj); | |
406 } | |
407 | |
408 WriteFormData(item.httpBody(), obj); | |
409 WriteString(item.httpContentType(), obj); | |
410 if (kVersion < 14) | |
411 WriteString(item.referrer(), obj); | |
412 | 496 |
413 // Subitems | 497 // Subitems |
414 const WebVector<WebHistoryItem>& children = item.children(); | 498 const std::vector<ExplodedFrameState>& children = state.children; |
415 WriteInteger(static_cast<int>(children.size()), obj); | 499 WriteAndValidateVectorSize(children, obj); |
416 for (size_t i = 0, c = children.size(); i < c; ++i) | 500 for (size_t i = 0; i < children.size(); ++i) |
417 WriteHistoryItem(children[i], obj, false); | 501 WriteFrameState(children[i], obj, false); |
418 } | 502 } |
419 | 503 |
420 // Creates a new HistoryItem tree based on the serialized string. | 504 void ReadFrameState(SerializeObject* obj, bool is_top, |
421 // Assumes the data is in the format returned by WriteHistoryItem. | 505 ExplodedFrameState* state) { |
422 WebHistoryItem ReadHistoryItem( | 506 if (obj->version < 14 && !is_top) |
423 const SerializeObject* obj, | |
424 IncludeFormData include_form_data, | |
425 bool include_scroll_offset, | |
426 bool is_top) { | |
427 if (is_top) { | |
428 obj->version = ReadInteger(obj); | |
429 | |
430 if (obj->version == -1) { | |
431 GURL url = ReadGURL(obj); | |
432 WebHistoryItem item; | |
433 item.initialize(); | |
434 item.setURLString(WebString::fromUTF8(url.possibly_invalid_spec())); | |
435 return item; | |
436 } | |
437 | |
438 if (obj->version > kVersion || obj->version < 1) | |
439 return WebHistoryItem(); | |
440 | |
441 if (obj->version >= 14) | |
442 ConsumeStringVector(obj); // Skip over list of referenced files. | |
443 } else if (obj->version < 14) { | |
444 ConsumeInteger(obj); // Skip over redundant version field. | 507 ConsumeInteger(obj); // Skip over redundant version field. |
445 } | 508 |
446 | 509 state->url_string = ReadString(obj); |
447 WebHistoryItem item; | 510 state->original_url_string = ReadString(obj); |
448 item.initialize(); | 511 state->target = ReadString(obj); |
449 | 512 state->parent = ReadString(obj); |
450 item.setURLString(ReadString(obj)); | 513 state->title = ReadString(obj); |
451 item.setOriginalURLString(ReadString(obj)); | 514 state->alternate_title = ReadString(obj); |
452 item.setTarget(ReadString(obj)); | 515 state->visited_time = ReadReal(obj); |
453 item.setParent(ReadString(obj)); | |
454 item.setTitle(ReadString(obj)); | |
455 item.setAlternateTitle(ReadString(obj)); | |
456 item.setLastVisitedTime(ReadReal(obj)); | |
457 | 516 |
458 int x = ReadInteger(obj); | 517 int x = ReadInteger(obj); |
459 int y = ReadInteger(obj); | 518 int y = ReadInteger(obj); |
460 if (include_scroll_offset) | 519 state->scroll_offset = gfx::Point(x, y); |
461 item.setScrollOffset(WebPoint(x, y)); | 520 |
462 | 521 state->is_target_item = ReadBoolean(obj); |
463 item.setIsTargetItem(ReadBoolean(obj)); | 522 state->visit_count = ReadInteger(obj); |
464 item.setVisitCount(ReadInteger(obj)); | 523 state->referrer = ReadString(obj); |
465 item.setReferrer(ReadString(obj)); | 524 |
466 | 525 ReadStringVector(obj, &state->document_state); |
467 item.setDocumentState(ReadStringVector(obj)); | 526 |
468 | 527 state->page_scale_factor = ReadReal(obj); |
469 if (obj->version >= 11) | 528 state->item_sequence_number = ReadInteger64(obj); |
470 item.setPageScaleFactor(ReadReal(obj)); | 529 state->document_sequence_number = ReadInteger64(obj); |
471 if (obj->version >= 9) | 530 |
472 item.setItemSequenceNumber(ReadInteger64(obj)); | 531 bool has_state_object = ReadBoolean(obj); |
473 if (obj->version >= 6) | 532 if (has_state_object) |
474 item.setDocumentSequenceNumber(ReadInteger64(obj)); | 533 state->state_object = ReadString(obj); |
475 if (obj->version >= 7) { | 534 |
476 bool has_state_object = ReadBoolean(obj); | 535 ReadHttpBody(obj, &state->http_body); |
477 if (has_state_object) { | 536 |
478 item.setStateObject( | 537 // NOTE: It is a quirk of the format that we still have to read the |
479 WebSerializedScriptValue::fromString(ReadString(obj))); | 538 // http_content_type field when the HTTP body is null. That's why this code |
480 } | 539 // is here instead of inside ReadHttpBody. |
481 } | 540 state->http_body.http_content_type = ReadString(obj); |
482 | |
483 // The extra referrer string is read for backwards compat. | |
484 const WebHTTPBody& http_body = ReadFormData(obj); | |
485 const WebString& http_content_type = ReadString(obj); | |
486 | 541 |
487 if (obj->version < 14) | 542 if (obj->version < 14) |
488 ConsumeString(obj); // Skip unused referrer string. | 543 ConsumeString(obj); // Skip unused referrer string. |
489 | 544 |
490 if (include_form_data == ALWAYS_INCLUDE_FORM_DATA || | |
491 (include_form_data == INCLUDE_FORM_DATA_WITHOUT_PASSWORDS && | |
492 !http_body.isNull() && !http_body.containsPasswordData())) { | |
493 // Include the full HTTP body. | |
494 item.setHTTPBody(http_body); | |
495 item.setHTTPContentType(http_content_type); | |
496 } else if (!http_body.isNull()) { | |
497 // Don't include the data in the HTTP body, but include its identifier. This | |
498 // enables fetching data from the cache. | |
499 WebHTTPBody empty_http_body; | |
500 empty_http_body.initialize(); | |
501 empty_http_body.setIdentifier(http_body.identifier()); | |
502 item.setHTTPBody(empty_http_body); | |
503 } | |
504 | |
505 #if defined(OS_ANDROID) | 545 #if defined(OS_ANDROID) |
506 if (obj->version == 11) { | 546 if (obj->version == 11) { |
507 // Now-unused values that shipped in this version of Chrome for Android when | 547 // Now-unused values that shipped in this version of Chrome for Android when |
508 // it was on a private branch. | 548 // it was on a private branch. |
509 ReadReal(obj); | 549 ReadReal(obj); |
510 ReadBoolean(obj); | 550 ReadBoolean(obj); |
511 | 551 |
512 // In this version, pageScaleFactor included deviceScaleFactor and scroll | 552 // In this version, page_scale_factor included deviceScaleFactor and scroll |
513 // offsets were premultiplied by pageScaleFactor. | 553 // offsets were premultiplied by pageScaleFactor. |
514 if (item.pageScaleFactor()) { | 554 if (state->page_scale_factor) { |
515 if (include_scroll_offset) | 555 state->scroll_offset = |
516 item.setScrollOffset( | 556 gfx::Point(state->scroll_offset.x() / state->page_scale_factor, |
517 WebPoint(item.scrollOffset().x / item.pageScaleFactor(), | 557 state->scroll_offset.y() / state->page_scale_factor); |
518 item.scrollOffset().y / item.pageScaleFactor())); | 558 state->page_scale_factor = (state->page_scale_factor / |
519 item.setPageScaleFactor(item.pageScaleFactor() / | |
520 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay() | 559 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay() |
521 .device_scale_factor()); | 560 .device_scale_factor()); |
522 } | 561 } |
523 } | 562 } |
524 #endif | 563 #endif |
525 | 564 |
526 // Subitems | 565 // Subitems |
527 int num_children = ReadInteger(obj); | 566 size_t num_children = |
528 for (int i = 0; i < num_children; ++i) | 567 ReadAndValidateVectorSize(obj, sizeof(ExplodedFrameState)); |
529 item.appendToChildren(ReadHistoryItem(obj, | 568 state->children.resize(num_children); |
530 include_form_data, | 569 for (size_t i = 0; i < num_children; ++i) |
531 include_scroll_offset, | 570 ReadFrameState(obj, false, &state->children[i]); |
532 false)); | |
533 | |
534 return item; | |
535 } | 571 } |
536 | 572 |
537 // Reconstruct a HistoryItem from a string, using our JSON Value deserializer. | 573 void WritePageState(const ExplodedPageState& state, SerializeObject* obj) { |
538 // This assumes that the given serialized string has all the required key,value | 574 WriteInteger(obj->version, obj); |
539 // pairs, and does minimal error checking. The form data of the post is restored | 575 WriteStringVector(state.referenced_files, obj); |
540 // if |include_form_data| is |ALWAYS_INCLUDE_FORM_DATA| or if the data doesn't | 576 WriteFrameState(state.top, obj, true); |
541 // contain passwords and |include_form_data| is | |
542 // |INCLUDE_FORM_DATA_WITHOUT_PASSWORDS|. Otherwise the form data is empty. If | |
543 // |include_scroll_offset| is true, the scroll offset is restored. | |
544 WebHistoryItem HistoryItemFromString( | |
545 const std::string& serialized_item, | |
546 IncludeFormData include_form_data, | |
547 bool include_scroll_offset) { | |
548 if (serialized_item.empty()) | |
549 return WebHistoryItem(); | |
550 | |
551 SerializeObject obj(serialized_item.data(), | |
552 static_cast<int>(serialized_item.length())); | |
553 return ReadHistoryItem(&obj, include_form_data, include_scroll_offset, true); | |
554 } | 577 } |
555 | 578 |
556 void ToFilePathVector(const WebVector<WebString>& input, | 579 void ReadPageState(SerializeObject* obj, ExplodedPageState* state) { |
557 std::vector<base::FilePath>* output) { | 580 obj->version = ReadInteger(obj); |
558 for (size_t i = 0; i < input.size(); ++i) | 581 |
559 output->push_back(webkit_base::WebStringToFilePath(input[i])); | 582 if (obj->version == -1) { |
583 GURL url = ReadGURL(obj); | |
584 // NOTE: GURL::possibly_invalid_spec() always returns valid UTF-8. | |
585 state->top.url_string = state->top.original_url_string = | |
586 base::NullableString16(UTF8ToUTF16(url.possibly_invalid_spec()), false); | |
587 return; | |
588 } | |
589 | |
590 if (obj->version > kCurrentVersion || obj->version < kMinVersion) { | |
591 obj->parse_error = true; | |
592 return; | |
593 } | |
594 | |
595 if (obj->version >= 14) | |
596 ReadStringVector(obj, &state->referenced_files); | |
597 | |
598 ReadFrameState(obj, true, &state->top); | |
599 | |
600 if (obj->version < 14) | |
601 RecursivelyExtractReferencedFiles(state->top, &state->referenced_files); | |
602 | |
603 // De-dupe | |
604 state->referenced_files.erase( | |
605 std::unique(state->referenced_files.begin(), | |
606 state->referenced_files.end()), | |
607 state->referenced_files.end()); | |
560 } | 608 } |
561 | 609 |
562 } // namespace | 610 } // namespace |
563 | 611 |
564 // Serialize a HistoryItem to a string, using our JSON Value serializer. | 612 ExplodedHttpBodyElement::ExplodedHttpBodyElement() |
565 std::string HistoryItemToString(const WebHistoryItem& item) { | 613 : type(WebKit::WebHTTPBody::Element::TypeData), |
566 if (item.isNull()) | 614 file_start(0), |
567 return std::string(); | 615 file_length(-1), |
568 | 616 file_modification_time(std::numeric_limits<double>::quiet_NaN()) { |
569 SerializeObject obj; | |
570 WriteHistoryItem(item, &obj, true); | |
571 return obj.GetAsString(); | |
572 } | 617 } |
573 | 618 |
574 WebHistoryItem HistoryItemFromString(const std::string& serialized_item) { | 619 ExplodedHttpBodyElement::~ExplodedHttpBodyElement() { |
575 return HistoryItemFromString(serialized_item, ALWAYS_INCLUDE_FORM_DATA, true); | |
576 } | 620 } |
577 | 621 |
578 std::vector<base::FilePath> FilePathsFromHistoryState( | 622 ExplodedHttpBody::ExplodedHttpBody() |
579 const std::string& content_state) { | 623 : identifier(0), |
580 // TODO(darin): We should avoid using the WebKit API here, so that we do not | 624 contains_passwords(false), |
581 // need to have WebKit initialized before calling this method. | 625 is_null(true) { |
582 | |
583 std::vector<base::FilePath> result; | |
584 | |
585 // In newer versions of the format, the set of referenced files is computed | |
586 // at serialization time. | |
587 SerializeObject obj(content_state.data(), | |
588 static_cast<int>(content_state.length())); | |
589 obj.version = ReadInteger(&obj); | |
590 | |
591 if (obj.version > kVersion || obj.version < 1) | |
592 return result; | |
593 | |
594 if (obj.version >= 14) { | |
595 ToFilePathVector(ReadStringVector(&obj), &result); | |
596 } else { | |
597 // TODO(darin): Delete this code path after we branch for M29. | |
598 const WebHistoryItem& item = | |
599 HistoryItemFromString(content_state, ALWAYS_INCLUDE_FORM_DATA, true); | |
600 if (!item.isNull()) | |
601 ToFilePathVector(item.getReferencedFilePaths(), &result); | |
602 } | |
603 return result; | |
604 } | 626 } |
605 | 627 |
606 // For testing purposes only. | 628 ExplodedHttpBody::~ExplodedHttpBody() { |
607 void HistoryItemToVersionedString(const WebHistoryItem& item, int version, | |
608 std::string* serialized_item) { | |
609 if (item.isNull()) { | |
610 serialized_item->clear(); | |
611 return; | |
612 } | |
613 | |
614 // Temporarily change the version. | |
615 int real_version = kVersion; | |
616 kVersion = version; | |
617 | |
618 SerializeObject obj; | |
619 WriteHistoryItem(item, &obj, true); | |
620 *serialized_item = obj.GetAsString(); | |
621 | |
622 kVersion = real_version; | |
623 } | 629 } |
624 | 630 |
625 int HistoryItemCurrentVersion() { | 631 ExplodedFrameState::ExplodedFrameState() |
626 return kVersion; | 632 : item_sequence_number(0), |
633 document_sequence_number(0), | |
634 visit_count(0), | |
635 visited_time(0.0), | |
636 page_scale_factor(0.0), | |
637 is_target_item(false) { | |
627 } | 638 } |
628 | 639 |
629 std::string RemovePasswordDataFromHistoryState( | 640 ExplodedFrameState::~ExplodedFrameState() { |
630 const std::string& content_state) { | |
631 // TODO(darin): We should avoid using the WebKit API here, so that we do not | |
632 // need to have WebKit initialized before calling this method. | |
633 const WebHistoryItem& item = | |
634 HistoryItemFromString( | |
635 content_state, INCLUDE_FORM_DATA_WITHOUT_PASSWORDS, true); | |
636 if (item.isNull()) { | |
637 // Couldn't parse the string, return an empty string. | |
638 return std::string(); | |
639 } | |
640 | |
641 return HistoryItemToString(item); | |
642 } | 641 } |
643 | 642 |
644 std::string RemoveScrollOffsetFromHistoryState( | 643 ExplodedPageState::ExplodedPageState() { |
645 const std::string& content_state) { | |
646 // TODO(darin): We should avoid using the WebKit API here, so that we do not | |
647 // need to have WebKit initialized before calling this method. | |
648 const WebHistoryItem& item = | |
649 HistoryItemFromString(content_state, ALWAYS_INCLUDE_FORM_DATA, false); | |
650 if (item.isNull()) { | |
651 // Couldn't parse the string, return an empty string. | |
652 return std::string(); | |
653 } | |
654 | |
655 return HistoryItemToString(item); | |
656 } | 644 } |
657 | 645 |
658 std::string CreateHistoryStateForURL(const GURL& url) { | 646 ExplodedPageState::~ExplodedPageState() { |
659 // We avoid using the WebKit API here, so that we do not need to have WebKit | |
660 // initialized before calling this method. Instead, we write a simple | |
661 // serialization of the given URL with a dummy version number of -1. This | |
662 // will be interpreted by ReadHistoryItem as a request to create a default | |
663 // WebHistoryItem. | |
664 SerializeObject obj; | |
665 WriteInteger(-1, &obj); | |
666 WriteGURL(url, &obj); | |
667 return obj.GetAsString(); | |
668 } | 647 } |
669 | 648 |
670 } // namespace webkit_glue | 649 bool DecodePageState(const std::string& encoded, ExplodedPageState* exploded) { |
650 *exploded = ExplodedPageState(); | |
651 | |
652 if (encoded.empty()) | |
653 return true; | |
654 | |
655 SerializeObject obj(encoded.data(), static_cast<int>(encoded.size())); | |
656 ReadPageState(&obj, exploded); | |
657 return !obj.parse_error; | |
658 } | |
659 | |
660 bool EncodePageState(const ExplodedPageState& exploded, std::string* encoded) { | |
661 SerializeObject obj; | |
662 obj.version = kCurrentVersion; | |
663 WritePageState(exploded, &obj); | |
664 *encoded = obj.GetAsString(); | |
665 return true; | |
666 } | |
667 | |
668 } // namespace content | |
OLD | NEW |