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) { | |
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>& state, | |
jamesr
2013/06/21 23:24:53
could we call this document_state?
darin (slow to review)
2013/06/24 08:02:10
Done.
| |
77 std::vector<base::NullableString16>* referenced_files) { | |
78 if (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. | |
jamesr
2013/06/21 23:24:53
could we have a link to that file at that rev?
darin (slow to review)
2013/06/24 08:02:10
Done.
| |
84 | |
85 size_t index = 0; | |
86 | |
87 if (state.size() < 3) | |
88 return false; | |
89 | |
90 index++; // Skip over magic signature. | |
91 index++; // Skip over form key. | |
92 | |
93 size_t item_count; | |
94 if (!base::StringToSizeT(state[index++].string(), &item_count)) | |
95 return false; | |
96 | |
97 while (item_count--) { | |
98 if (index + 1 >= state.size()) | |
99 return false; | |
100 | |
101 state[index++]; // name | |
jamesr
2013/06/21 23:24:53
the state[] access here is a bit confusing - how a
darin (slow to review)
2013/06/24 08:02:10
Done. Yeah, there was no point to the state[] bit
| |
102 const base::NullableString16& type = state[index++]; | |
103 | |
104 if (index >= state.size()) | |
105 return false; | |
106 | |
107 size_t value_size; | |
108 if (!base::StringToSizeT(state[index++].string(), &value_size)) | |
jamesr
2013/06/21 23:24:53
should we check state[index].is_null() ? is the st
darin (slow to review)
2013/06/24 08:02:10
If the string is null, then the string() accessor
| |
109 return false; | |
110 | |
111 if (index + value_size > state.size()) | |
112 return false; | |
113 | |
114 if (EqualsASCII(type.string(), "file")) { | |
115 if (value_size != 2) | |
116 return false; | |
117 | |
118 referenced_files->push_back(state[index++]); | |
119 index++; // Skip over display name. | |
120 } else { | |
121 index += value_size; | |
122 } | |
123 } | |
124 | |
125 return true; | |
126 } | |
127 | |
128 bool RecursivelyExtractReferencedFiles( | |
129 const ExplodedFrameState& frame_state, | |
130 std::vector<base::NullableString16>* referenced_files) { | |
131 if (!frame_state.http_body.is_null) { | |
132 ExtractReferencedFilesFromHttpBody(frame_state.http_body.elements, | |
133 referenced_files); | |
134 } | |
135 | |
136 if (!ExtractReferencedFilesFromDocumentState(frame_state.document_state, | |
137 referenced_files)) | |
138 return false; | |
139 | |
140 for (size_t i = 0; i < frame_state.children.size(); ++i) { | |
141 if (!RecursivelyExtractReferencedFiles(frame_state.children[i], | |
142 referenced_files)) | |
143 return false; | |
144 } | |
145 | |
146 return true; | |
147 } | |
148 | |
149 //---------------------------------------------------------------------------- | |
41 | 150 |
42 struct SerializeObject { | 151 struct SerializeObject { |
43 SerializeObject() : version(0) {} | 152 SerializeObject() |
153 : version(0), | |
154 parse_error(false) { | |
155 } | |
156 | |
44 SerializeObject(const char* data, int len) | 157 SerializeObject(const char* data, int len) |
45 : pickle(data, len), version(0) { iter = PickleIterator(pickle); } | 158 : pickle(data, len), |
159 version(0), | |
160 parse_error(false) { | |
161 iter = PickleIterator(pickle); | |
162 } | |
46 | 163 |
47 std::string GetAsString() { | 164 std::string GetAsString() { |
48 return std::string(static_cast<const char*>(pickle.data()), pickle.size()); | 165 return std::string(static_cast<const char*>(pickle.data()), pickle.size()); |
49 } | 166 } |
50 | 167 |
51 Pickle pickle; | 168 Pickle pickle; |
52 mutable PickleIterator iter; | 169 PickleIterator iter; |
53 mutable int version; | 170 int version; |
171 bool parse_error; | |
54 }; | 172 }; |
55 | 173 |
56 // TODO(mpcomplete): obsolete versions 1 and 2 after 1/1/2008. | 174 // Version ID of serialized format. |
57 // Version ID used in reading/writing history items. | 175 // 11: Min version |
58 // 1: Initial revision. | 176 // 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) | 177 // 13: Adds support for URL (FileSystem URL) |
73 // 14: Adds list of referenced files, version written only for first item. | 178 // 14: Adds list of referenced files, version written only for first item. |
74 // Should be const, but unit tests may modify it. | |
75 // | 179 // |
76 // NOTE: If the version is -1, then the pickle contains only a URL string. | 180 // NOTE: If the version is -1, then the pickle contains only a URL string. |
77 // See CreateHistoryStateForURL. | 181 // See ReadPageState. |
78 // | 182 // |
79 int kVersion = 14; | 183 const int kMinVersion = 11; |
80 | 184 const int kCurrentVersion = 14; |
81 // A bunch of convenience functions to read/write to SerializeObjects. | 185 |
82 // The serializers assume the input data is in the correct format and so does | 186 // A bunch of convenience functions to read/write to SerializeObjects. The |
83 // no error checking. | 187 // de-serializers assume the input data will be in the correct format and fall |
188 // back to returning safe defaults when not. | |
189 | |
84 void WriteData(const void* data, int length, SerializeObject* obj) { | 190 void WriteData(const void* data, int length, SerializeObject* obj) { |
85 obj->pickle.WriteData(static_cast<const char*>(data), length); | 191 obj->pickle.WriteData(static_cast<const char*>(data), length); |
86 } | 192 } |
87 | 193 |
88 void ReadData(const SerializeObject* obj, const void** data, int* length) { | 194 void ReadData(SerializeObject* obj, const void** data, int* length) { |
89 const char* tmp; | 195 const char* tmp; |
90 if (obj->pickle.ReadData(&obj->iter, &tmp, length)) { | 196 if (obj->pickle.ReadData(&obj->iter, &tmp, length)) { |
91 *data = tmp; | 197 *data = tmp; |
92 } else { | 198 } else { |
199 obj->parse_error = true; | |
93 *data = NULL; | 200 *data = NULL; |
94 *length = 0; | 201 *length = 0; |
95 } | 202 } |
96 } | 203 } |
97 | 204 |
98 bool ReadBytes(const SerializeObject* obj, const void** data, int length) { | 205 bool ReadBytes(SerializeObject* obj, const void** data, int length) { |
99 const char *tmp; | 206 const char *tmp; |
100 if (!obj->pickle.ReadBytes(&obj->iter, &tmp, length)) | 207 if (!obj->pickle.ReadBytes(&obj->iter, &tmp, length)) { |
101 return false; | 208 obj->parse_error = true; |
209 return false; | |
210 } | |
102 *data = tmp; | 211 *data = tmp; |
103 return true; | 212 return true; |
104 } | 213 } |
105 | 214 |
106 void WriteInteger(int data, SerializeObject* obj) { | 215 void WriteInteger(int data, SerializeObject* obj) { |
107 obj->pickle.WriteInt(data); | 216 obj->pickle.WriteInt(data); |
108 } | 217 } |
109 | 218 |
110 int ReadInteger(const SerializeObject* obj) { | 219 int ReadInteger(SerializeObject* obj) { |
111 int tmp; | 220 int tmp; |
112 if (obj->pickle.ReadInt(&obj->iter, &tmp)) | 221 if (obj->pickle.ReadInt(&obj->iter, &tmp)) |
113 return tmp; | 222 return tmp; |
223 obj->parse_error = true; | |
114 return 0; | 224 return 0; |
115 } | 225 } |
116 | 226 |
117 void ConsumeInteger(const SerializeObject* obj) { | 227 void ConsumeInteger(SerializeObject* obj) { |
118 int unused ALLOW_UNUSED = ReadInteger(obj); | 228 int unused ALLOW_UNUSED = ReadInteger(obj); |
119 } | 229 } |
120 | 230 |
121 void WriteInteger64(int64 data, SerializeObject* obj) { | 231 void WriteInteger64(int64 data, SerializeObject* obj) { |
122 obj->pickle.WriteInt64(data); | 232 obj->pickle.WriteInt64(data); |
123 } | 233 } |
124 | 234 |
125 int64 ReadInteger64(const SerializeObject* obj) { | 235 int64 ReadInteger64(SerializeObject* obj) { |
126 int64 tmp = 0; | 236 int64 tmp = 0; |
127 obj->pickle.ReadInt64(&obj->iter, &tmp); | 237 if (obj->pickle.ReadInt64(&obj->iter, &tmp)) |
128 return tmp; | 238 return tmp; |
239 obj->parse_error = true; | |
240 return 0; | |
129 } | 241 } |
130 | 242 |
131 void WriteReal(double data, SerializeObject* obj) { | 243 void WriteReal(double data, SerializeObject* obj) { |
132 WriteData(&data, sizeof(double), obj); | 244 WriteData(&data, sizeof(double), obj); |
jamesr
2013/06/21 23:24:53
hmm, we're reading/writing the double's bits raw,
darin (slow to review)
2013/06/24 08:02:10
In at least the case where Read/WriteReal is used
| |
133 } | 245 } |
134 | 246 |
135 double ReadReal(const SerializeObject* obj) { | 247 double ReadReal(SerializeObject* obj) { |
136 const void* tmp = NULL; | 248 const void* tmp = NULL; |
137 int length = 0; | 249 int length = 0; |
138 double value = 0.0; | 250 double value = 0.0; |
139 ReadData(obj, &tmp, &length); | 251 ReadData(obj, &tmp, &length); |
140 if (tmp && length >= static_cast<int>(sizeof(double))) { | 252 if (tmp && length >= static_cast<int>(sizeof(double))) { |
141 // Use memcpy, as tmp may not be correctly aligned. | 253 // Use memcpy, as tmp may not be correctly aligned. |
142 memcpy(&value, tmp, sizeof(double)); | 254 memcpy(&value, tmp, sizeof(double)); |
255 } else { | |
256 obj->parse_error = true; | |
143 } | 257 } |
144 return value; | 258 return value; |
145 } | 259 } |
146 | 260 |
147 void WriteBoolean(bool data, SerializeObject* obj) { | 261 void WriteBoolean(bool data, SerializeObject* obj) { |
148 obj->pickle.WriteInt(data ? 1 : 0); | 262 obj->pickle.WriteInt(data ? 1 : 0); |
149 } | 263 } |
150 | 264 |
151 bool ReadBoolean(const SerializeObject* obj) { | 265 bool ReadBoolean(SerializeObject* obj) { |
152 bool tmp; | 266 bool tmp; |
153 if (obj->pickle.ReadBool(&obj->iter, &tmp)) | 267 if (obj->pickle.ReadBool(&obj->iter, &tmp)) |
154 return tmp; | 268 return tmp; |
269 obj->parse_error = true; | |
155 return false; | 270 return false; |
156 } | 271 } |
157 | 272 |
158 void WriteGURL(const GURL& url, SerializeObject* obj) { | 273 void WriteGURL(const GURL& url, SerializeObject* obj) { |
159 obj->pickle.WriteString(url.possibly_invalid_spec()); | 274 obj->pickle.WriteString(url.possibly_invalid_spec()); |
160 } | 275 } |
161 | 276 |
162 GURL ReadGURL(const SerializeObject* obj) { | 277 GURL ReadGURL(SerializeObject* obj) { |
163 std::string spec; | 278 std::string spec; |
164 if (obj->pickle.ReadString(&obj->iter, &spec)) | 279 if (obj->pickle.ReadString(&obj->iter, &spec)) |
165 return GURL(spec); | 280 return GURL(spec); |
281 obj->parse_error = true; | |
166 return GURL(); | 282 return GURL(); |
167 } | 283 } |
168 | 284 |
169 // Read/WriteString pickle the WebString as <int length><WebUChar* data>. | 285 // WriteString pickles the NullableString16 as <int length><char16* data>. |
170 // If length == -1, then the WebString itself is NULL (WebString()). | 286 // 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. | 287 // length is the number of char16 (not bytes) in the NullableString16. |
172 void WriteString(const WebString& str, SerializeObject* obj) { | 288 void WriteString(const base::NullableString16& str, SerializeObject* obj) { |
173 base::string16 string = str; | 289 const char16* data = str.string().data(); |
174 const char16* data = string.data(); | 290 size_t length_in_bytes = str.string().length() * sizeof(char16); |
175 size_t length_in_uchars = string.length(); | 291 if (str.is_null()) { |
176 size_t length_in_bytes = length_in_uchars * sizeof(char16); | 292 obj->pickle.WriteInt(-1); |
177 switch (kVersion) { | 293 } else { |
178 case 1: | 294 obj->pickle.WriteInt(length_in_bytes); |
179 // Version 1 writes <length in bytes><string data>. | 295 obj->pickle.WriteBytes(data, length_in_bytes); |
180 // It saves WebString() and "" as "". | 296 } |
181 obj->pickle.WriteInt(length_in_bytes); | 297 } |
182 obj->pickle.WriteBytes(data, length_in_bytes); | 298 |
183 break; | 299 // This reads a serialized NullableString16 from obj. If a string can't be |
184 case 2: | 300 // read, NULL is returned. |
185 // Version 2 writes <length in WebUChar><string data>. | 301 const char16* ReadStringNoCopy(SerializeObject* obj, int* num_chars) { |
186 // It uses -1 in the length field to mean WebString(). | 302 int length_in_bytes; |
187 if (str.isNull()) { | 303 if (!obj->pickle.ReadInt(&obj->iter, &length_in_bytes)) { |
188 obj->pickle.WriteInt(-1); | 304 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; | 305 return NULL; |
215 | 306 } |
216 // Starting with version 2, -1 means WebString(). | 307 |
217 if (length == -1) | 308 if (length_in_bytes == -1) |
218 return NULL; | 309 return NULL; |
219 | 310 |
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; | 311 const void* data; |
227 if (!ReadBytes(obj, &data, bytes)) | 312 if (!ReadBytes(obj, &data, length_in_bytes)) { |
313 obj->parse_error = true; | |
228 return NULL; | 314 return NULL; |
315 } | |
229 | 316 |
230 if (num_chars) | 317 if (num_chars) |
231 *num_chars = bytes / sizeof(WebUChar); | 318 *num_chars = length_in_bytes / sizeof(char16); |
232 return static_cast<const WebUChar*>(data); | 319 return static_cast<const char16*>(data); |
233 } | 320 } |
234 | 321 |
235 WebString ReadString(const SerializeObject* obj) { | 322 base::NullableString16 ReadString(SerializeObject* obj) { |
236 int num_chars; | 323 int num_chars; |
237 const WebUChar* chars = ReadStringNoCopy(obj, &num_chars); | 324 const char16* chars = ReadStringNoCopy(obj, &num_chars); |
238 return chars ? WebString(chars, num_chars) : WebString(); | 325 return chars ? |
239 } | 326 base::NullableString16(base::string16(chars, num_chars), false) : |
240 | 327 base::NullableString16(); |
241 void ConsumeString(const SerializeObject* obj) { | 328 } |
242 const WebUChar* unused ALLOW_UNUSED = ReadStringNoCopy(obj, NULL); | 329 |
243 } | 330 void ConsumeString(SerializeObject* obj) { |
244 | 331 const char16* unused ALLOW_UNUSED = ReadStringNoCopy(obj, NULL); |
245 // Writes a Vector of Strings into a SerializeObject for serialization. | 332 } |
333 | |
334 // Writes a Vector of strings into a SerializeObject for serialization. | |
246 void WriteStringVector( | 335 void WriteStringVector( |
247 const WebVector<WebString>& data, SerializeObject* obj) { | 336 const std::vector<base::NullableString16>& data, SerializeObject* obj) { |
248 WriteInteger(static_cast<int>(data.size()), obj); | 337 WriteInteger(static_cast<int>(data.size()), obj); |
jamesr
2013/06/21 23:24:53
what if data.size() > INT_MAX? it looks like this
darin (slow to review)
2013/06/24 08:02:10
Yeah, good catch. This code doesn't do much at al
| |
249 for (size_t i = 0, c = data.size(); i < c; ++i) { | 338 for (size_t i = 0; i < data.size(); ++i) { |
250 unsigned ui = static_cast<unsigned>(i); // sigh | 339 WriteString(data[i], obj); |
251 WriteString(data[ui], obj); | 340 } |
252 } | 341 } |
253 } | 342 |
254 | 343 void ReadStringVector(SerializeObject* obj, |
255 WebVector<WebString> ReadStringVector(const SerializeObject* obj) { | 344 std::vector<base::NullableString16>* result) { |
256 int num_elements = ReadInteger(obj); | 345 size_t num_elements = static_cast<size_t>(ReadInteger(obj)); |
257 WebVector<WebString> result(static_cast<size_t>(num_elements)); | 346 |
258 for (int i = 0; i < num_elements; ++i) | 347 // Ensure that num_elements makes sense. |
259 result[i] = ReadString(obj); | 348 if (INT_MAX / sizeof(base::NullableString16) <= num_elements) { |
jamesr
2013/06/21 23:26:53
rather than duplicating this check for the resize(
darin (slow to review)
2013/06/24 08:02:10
I went with a ReadAndValidateVectorSize method. I
| |
260 return result; | 349 obj->parse_error = true; |
261 } | |
262 | |
263 void ConsumeStringVector(const SerializeObject* obj) { | |
264 int num_elements = ReadInteger(obj); | |
265 for (int i = 0; i < num_elements; ++i) | |
266 ConsumeString(obj); | |
267 } | |
268 | |
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; | 350 return; |
275 | 351 } |
276 WriteInteger(static_cast<int>(http_body.elementCount()), obj); | 352 |
277 WebHTTPBody::Element element; | 353 result->resize(num_elements); |
278 for (size_t i = 0; http_body.elementAt(i, element); ++i) { | 354 for (size_t i = 0; i < num_elements; ++i) |
355 (*result)[i] = ReadString(obj); | |
356 } | |
357 | |
358 // Writes an ExplodedHttpBody object into a SerializeObject for serialization. | |
359 void WriteHttpBody(const ExplodedHttpBody& http_body, SerializeObject* obj) { | |
360 WriteBoolean(!http_body.is_null, obj); | |
361 | |
362 if (http_body.is_null) | |
363 return; | |
364 | |
365 WriteInteger(static_cast<int>(http_body.elements.size()), obj); | |
jamesr
2013/06/21 23:24:53
this could write out a bogus value if size() is ri
darin (slow to review)
2013/06/24 08:02:10
OK. I went with a function named WriteAndValidate
| |
366 for (size_t i = 0; i < http_body.elements.size(); ++i) { | |
367 const ExplodedHttpBodyElement& element = http_body.elements[i]; | |
279 WriteInteger(element.type, obj); | 368 WriteInteger(element.type, obj); |
280 if (element.type == WebHTTPBody::Element::TypeData) { | 369 if (element.type == WebKit::WebHTTPBody::Element::TypeData) { |
281 WriteData(element.data.data(), static_cast<int>(element.data.size()), | 370 WriteData(element.data.data(), static_cast<int>(element.data.size()), |
282 obj); | 371 obj); |
283 } else if (element.type == WebHTTPBody::Element::TypeFile) { | 372 } else if (element.type == WebKit::WebHTTPBody::Element::TypeFile) { |
284 WriteString(element.filePath, obj); | 373 WriteString(element.file_path, obj); |
285 WriteInteger64(element.fileStart, obj); | 374 WriteInteger64(element.file_start, obj); |
286 WriteInteger64(element.fileLength, obj); | 375 WriteInteger64(element.file_length, obj); |
287 WriteReal(element.modificationTime, obj); | 376 WriteReal(element.file_modification_time, obj); |
288 } else if (element.type == WebHTTPBody::Element::TypeURL) { | 377 } else if (element.type == WebKit::WebHTTPBody::Element::TypeURL) { |
289 WriteGURL(element.url, obj); | 378 WriteGURL(element.url, obj); |
290 WriteInteger64(element.fileStart, obj); | 379 WriteInteger64(element.file_start, obj); |
291 WriteInteger64(element.fileLength, obj); | 380 WriteInteger64(element.file_length, obj); |
292 WriteReal(element.modificationTime, obj); | 381 WriteReal(element.file_modification_time, obj); |
293 } else { | 382 } else { |
294 WriteGURL(element.url, obj); | 383 WriteGURL(element.url, obj); |
295 } | 384 } |
296 } | 385 } |
297 WriteInteger64(http_body.identifier(), obj); | 386 WriteInteger64(http_body.identifier, obj); |
298 WriteBoolean(http_body.containsPasswordData(), obj); | 387 WriteBoolean(http_body.contains_passwords, obj); |
299 } | 388 } |
300 | 389 |
301 WebHTTPBody ReadFormData(const SerializeObject* obj) { | 390 void ReadHttpBody(SerializeObject* obj, ExplodedHttpBody* http_body) { |
302 // In newer versions, an initial boolean indicates if we have form data. | 391 // An initial boolean indicates if we have an HTTP body. |
303 if (obj->version >= 5 && !ReadBoolean(obj)) | 392 if (!ReadBoolean(obj)) |
304 return WebHTTPBody(); | 393 return; |
305 | 394 http_body->is_null = false; |
306 // In older versions, 0 elements implied no form data. | 395 |
307 int num_elements = ReadInteger(obj); | 396 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 | 397 |
314 for (int i = 0; i < num_elements; ++i) { | 398 for (int i = 0; i < num_elements; ++i) { |
315 int type = ReadInteger(obj); | 399 int type = ReadInteger(obj); |
316 if (type == WebHTTPBody::Element::TypeData) { | 400 if (type == WebKit::WebHTTPBody::Element::TypeData) { |
317 const void* data; | 401 const void* data; |
318 int length = -1; | 402 int length = -1; |
319 ReadData(obj, &data, &length); | 403 ReadData(obj, &data, &length); |
320 if (length >= 0) | 404 if (length >= 0) { |
321 http_body.appendData(WebData(static_cast<const char*>(data), length)); | 405 AppendDataToHttpBody(http_body, static_cast<const char*>(data), |
322 } else if (type == WebHTTPBody::Element::TypeFile) { | 406 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 } | 407 } |
332 http_body.appendFileRange(file_path, file_start, file_length, | 408 } else if (type == WebKit::WebHTTPBody::Element::TypeFile) { |
333 modification_time); | 409 base::NullableString16 file_path = ReadString(obj); |
334 } else if (type == WebHTTPBody::Element::TypeURL) { | 410 int64 file_start = ReadInteger64(obj); |
411 int64 file_length = ReadInteger64(obj); | |
412 double file_modification_time = ReadReal(obj); | |
413 AppendFileRangeToHttpBody(http_body, file_path, file_start, file_length, | |
414 file_modification_time); | |
415 } else if (type == WebKit::WebHTTPBody::Element::TypeURL) { | |
335 GURL url = ReadGURL(obj); | 416 GURL url = ReadGURL(obj); |
336 long long file_start = 0; | 417 int64 file_start = ReadInteger64(obj); |
337 long long file_length = -1; | 418 int64 file_length = ReadInteger64(obj); |
338 double modification_time = 0.0; | 419 double file_modification_time = ReadReal(obj); |
339 file_start = ReadInteger64(obj); | 420 AppendURLRangeToHttpBody(http_body, url, file_start, file_length, |
340 file_length = ReadInteger64(obj); | 421 file_modification_time); |
341 modification_time = ReadReal(obj); | 422 } 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); | 423 GURL blob_url = ReadGURL(obj); |
346 http_body.appendBlob(blob_url); | 424 AppendBlobToHttpBody(http_body, blob_url); |
347 } | 425 } |
348 } | 426 } |
349 if (obj->version >= 4) | 427 http_body->identifier = ReadInteger64(obj); |
350 http_body.setIdentifier(ReadInteger64(obj)); | |
351 | 428 |
352 if (obj->version >= 12) | 429 if (obj->version >= 12) |
353 http_body.setContainsPasswordData(ReadBoolean(obj)); | 430 http_body->contains_passwords = ReadBoolean(obj); |
354 | 431 } |
355 return http_body; | 432 |
356 } | 433 // Writes the ExplodedFrameState data into the SerializeObject object for |
357 | |
358 // Writes the HistoryItem data into the SerializeObject object for | |
359 // serialization. | 434 // serialization. |
360 void WriteHistoryItem( | 435 void WriteFrameState( |
361 const WebHistoryItem& item, SerializeObject* obj, bool is_top) { | 436 const ExplodedFrameState& state, SerializeObject* obj, bool is_top) { |
362 // WARNING: This data may be persisted for later use. As such, care must be | 437 // 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 | 438 // 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 | 439 // 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 | 440 // older versions. Similarly, this should NOT save fields with sensitive |
366 // data, such as password fields. | 441 // data, such as password fields. |
367 | 442 |
368 if (kVersion >= 14) { | 443 WriteString(state.url_string, obj); |
369 if (is_top) { | 444 WriteString(state.original_url_string, obj); |
370 WriteInteger(kVersion, obj); | 445 WriteString(state.target, obj); |
371 | 446 WriteString(state.parent, obj); |
372 // Insert the list of referenced files, so they can be extracted easily | 447 WriteString(state.title, obj); |
373 // from the serialized data (avoiding the need to call into Blink again). | 448 WriteString(state.alternate_title, obj); |
374 WriteStringVector(item.getReferencedFilePaths(), obj); | 449 WriteReal(state.visited_time, obj); |
375 } | 450 WriteInteger(state.scroll_offset.x(), obj); |
376 } else { | 451 WriteInteger(state.scroll_offset.y(), obj); |
377 WriteInteger(kVersion, obj); | 452 WriteBoolean(state.is_target_item, obj); |
378 } | 453 WriteInteger(state.visit_count, obj); |
379 | 454 WriteString(state.referrer, obj); |
380 WriteString(item.urlString(), obj); | 455 |
381 WriteString(item.originalURLString(), obj); | 456 WriteStringVector(state.document_state, obj); |
382 WriteString(item.target(), obj); | 457 |
383 WriteString(item.parent(), obj); | 458 WriteReal(state.page_scale_factor, obj); |
384 WriteString(item.title(), obj); | 459 WriteInteger64(state.item_sequence_number, obj); |
385 WriteString(item.alternateTitle(), obj); | 460 WriteInteger64(state.document_sequence_number, obj); |
386 WriteReal(item.lastVisitedTime(), obj); | 461 |
387 WriteInteger(item.scrollOffset().x, obj); | 462 bool has_state_object = !state.state_object.is_null(); |
388 WriteInteger(item.scrollOffset().y, obj); | 463 WriteBoolean(has_state_object, obj); |
jamesr
2013/06/21 23:24:53
hmm, why is this different from the other base::Nu
darin (slow to review)
2013/06/24 08:02:10
Added a comment. Previously, we were serializing
| |
389 WriteBoolean(item.isTargetItem(), obj); | 464 if (has_state_object) |
390 WriteInteger(item.visitCount(), obj); | 465 WriteString(state.state_object, obj); |
391 WriteString(item.referrer(), obj); | 466 |
392 | 467 WriteHttpBody(state.http_body, obj); |
393 WriteStringVector(item.documentState(), obj); | 468 WriteString(state.http_body.http_content_type, obj); |
jamesr
2013/06/21 23:24:53
why isn't this part of WriteHttpBody() ?
darin (slow to review)
2013/06/24 08:02:10
Just historical. I would move it, but it is actua
| |
394 | |
395 if (kVersion >= 11) | |
396 WriteReal(item.pageScaleFactor(), obj); | |
397 if (kVersion >= 9) | |
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 | 469 |
413 // Subitems | 470 // Subitems |
414 const WebVector<WebHistoryItem>& children = item.children(); | 471 const std::vector<ExplodedFrameState>& children = state.children; |
415 WriteInteger(static_cast<int>(children.size()), obj); | 472 WriteInteger(static_cast<int>(children.size()), obj); |
416 for (size_t i = 0, c = children.size(); i < c; ++i) | 473 for (size_t i = 0, c = children.size(); i < c; ++i) |
417 WriteHistoryItem(children[i], obj, false); | 474 WriteFrameState(children[i], obj, false); |
418 } | 475 } |
419 | 476 |
420 // Creates a new HistoryItem tree based on the serialized string. | 477 void ReadFrameState(SerializeObject* obj, bool is_top, |
421 // Assumes the data is in the format returned by WriteHistoryItem. | 478 ExplodedFrameState* state) { |
422 WebHistoryItem ReadHistoryItem( | 479 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. | 480 ConsumeInteger(obj); // Skip over redundant version field. |
445 } | 481 |
446 | 482 state->url_string = ReadString(obj); |
447 WebHistoryItem item; | 483 state->original_url_string = ReadString(obj); |
448 item.initialize(); | 484 state->target = ReadString(obj); |
449 | 485 state->parent = ReadString(obj); |
450 item.setURLString(ReadString(obj)); | 486 state->title = ReadString(obj); |
451 item.setOriginalURLString(ReadString(obj)); | 487 state->alternate_title = ReadString(obj); |
452 item.setTarget(ReadString(obj)); | 488 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 | 489 |
458 int x = ReadInteger(obj); | 490 int x = ReadInteger(obj); |
459 int y = ReadInteger(obj); | 491 int y = ReadInteger(obj); |
460 if (include_scroll_offset) | 492 state->scroll_offset = gfx::Point(x, y); |
461 item.setScrollOffset(WebPoint(x, y)); | 493 |
462 | 494 state->is_target_item = ReadBoolean(obj); |
463 item.setIsTargetItem(ReadBoolean(obj)); | 495 state->visit_count = ReadInteger(obj); |
464 item.setVisitCount(ReadInteger(obj)); | 496 state->referrer = ReadString(obj); |
465 item.setReferrer(ReadString(obj)); | 497 |
466 | 498 ReadStringVector(obj, &state->document_state); |
467 item.setDocumentState(ReadStringVector(obj)); | 499 |
468 | 500 state->page_scale_factor = ReadReal(obj); |
469 if (obj->version >= 11) | 501 state->item_sequence_number = ReadInteger64(obj); |
470 item.setPageScaleFactor(ReadReal(obj)); | 502 state->document_sequence_number = ReadInteger64(obj); |
471 if (obj->version >= 9) | 503 |
472 item.setItemSequenceNumber(ReadInteger64(obj)); | 504 bool has_state_object = ReadBoolean(obj); |
473 if (obj->version >= 6) | 505 if (has_state_object) |
474 item.setDocumentSequenceNumber(ReadInteger64(obj)); | 506 state->state_object = ReadString(obj); |
475 if (obj->version >= 7) { | 507 |
476 bool has_state_object = ReadBoolean(obj); | 508 ReadHttpBody(obj, &state->http_body); |
477 if (has_state_object) { | 509 state->http_body.http_content_type = ReadString(obj); |
478 item.setStateObject( | |
479 WebSerializedScriptValue::fromString(ReadString(obj))); | |
480 } | |
481 } | |
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 | 510 |
487 if (obj->version < 14) | 511 if (obj->version < 14) |
488 ConsumeString(obj); // Skip unused referrer string. | 512 ConsumeString(obj); // Skip unused referrer string. |
489 | 513 |
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) | 514 #if defined(OS_ANDROID) |
506 if (obj->version == 11) { | 515 if (obj->version == 11) { |
507 // Now-unused values that shipped in this version of Chrome for Android when | 516 // Now-unused values that shipped in this version of Chrome for Android when |
508 // it was on a private branch. | 517 // it was on a private branch. |
509 ReadReal(obj); | 518 ReadReal(obj); |
510 ReadBoolean(obj); | 519 ReadBoolean(obj); |
511 | 520 |
512 // In this version, pageScaleFactor included deviceScaleFactor and scroll | 521 // In this version, page_scale_factor included deviceScaleFactor and scroll |
513 // offsets were premultiplied by pageScaleFactor. | 522 // offsets were premultiplied by pageScaleFactor. |
514 if (item.pageScaleFactor()) { | 523 if (state->page_scale_factor) { |
515 if (include_scroll_offset) | 524 state->scroll_offset = |
516 item.setScrollOffset( | 525 gfx::Point(state->scroll_offset.x() / state->page_scale_factor, |
517 WebPoint(item.scrollOffset().x / item.pageScaleFactor(), | 526 state->scroll_offset.y() / state->page_scale_factor); |
518 item.scrollOffset().y / item.pageScaleFactor())); | 527 state->page_scale_factor = (state->page_scale_factor / |
519 item.setPageScaleFactor(item.pageScaleFactor() / | |
520 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay() | 528 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay() |
521 .device_scale_factor()); | 529 .device_scale_factor()); |
522 } | 530 } |
523 } | 531 } |
524 #endif | 532 #endif |
525 | 533 |
526 // Subitems | 534 // Subitems |
527 int num_children = ReadInteger(obj); | 535 int num_children = ReadInteger(obj); |
536 state->children.resize(num_children); | |
jamesr
2013/06/21 23:24:53
i think we need to validate this number a bit more
darin (slow to review)
2013/06/24 08:02:10
It turns out that the cursor position of the Pickl
| |
528 for (int i = 0; i < num_children; ++i) | 537 for (int i = 0; i < num_children; ++i) |
529 item.appendToChildren(ReadHistoryItem(obj, | 538 ReadFrameState(obj, false, &state->children[i]); |
530 include_form_data, | |
531 include_scroll_offset, | |
532 false)); | |
533 | |
534 return item; | |
535 } | 539 } |
536 | 540 |
537 // Reconstruct a HistoryItem from a string, using our JSON Value deserializer. | 541 void WritePageState(const ExplodedPageState& state, SerializeObject* obj) { |
538 // This assumes that the given serialized string has all the required key,value | 542 WriteInteger(obj->version, obj); |
539 // pairs, and does minimal error checking. The form data of the post is restored | 543 WriteStringVector(state.referenced_files, obj); |
540 // if |include_form_data| is |ALWAYS_INCLUDE_FORM_DATA| or if the data doesn't | 544 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 } | 545 } |
555 | 546 |
556 void ToFilePathVector(const WebVector<WebString>& input, | 547 void ReadPageState(SerializeObject* obj, ExplodedPageState* state) { |
557 std::vector<base::FilePath>* output) { | 548 obj->version = ReadInteger(obj); |
558 for (size_t i = 0; i < input.size(); ++i) | 549 |
559 output->push_back(webkit_base::WebStringToFilePath(input[i])); | 550 if (obj->version == -1) { |
551 GURL url = ReadGURL(obj); | |
552 state->top.url_string = | |
553 base::NullableString16(UTF8ToUTF16(url.possibly_invalid_spec()), false); | |
jamesr
2013/06/21 23:24:53
might be worth a comment that GURL::possibly_inval
darin (slow to review)
2013/06/24 08:02:10
Done. I'm so used to coding with GURL ;-)
| |
554 return; | |
555 } | |
556 | |
557 if (obj->version > kCurrentVersion || obj->version < kMinVersion) { | |
558 obj->parse_error = true; | |
559 return; | |
560 } | |
561 | |
562 if (obj->version >= 14) | |
563 ReadStringVector(obj, &state->referenced_files); | |
564 | |
565 ReadFrameState(obj, true, &state->top); | |
566 | |
567 if (obj->version < 14) | |
568 RecursivelyExtractReferencedFiles(state->top, &state->referenced_files); | |
569 | |
570 // De-dupe | |
571 state->referenced_files.erase( | |
572 std::unique(state->referenced_files.begin(), | |
573 state->referenced_files.end()), | |
574 state->referenced_files.end()); | |
560 } | 575 } |
561 | 576 |
562 } // namespace | 577 } // namespace |
563 | 578 |
564 // Serialize a HistoryItem to a string, using our JSON Value serializer. | 579 ExplodedHttpBodyElement::ExplodedHttpBodyElement() |
565 std::string HistoryItemToString(const WebHistoryItem& item) { | 580 : type(WebKit::WebHTTPBody::Element::TypeData), |
566 if (item.isNull()) | 581 file_start(0), |
567 return std::string(); | 582 file_length(-1), |
568 | 583 file_modification_time(std::numeric_limits<double>::quiet_NaN()) { |
569 SerializeObject obj; | |
570 WriteHistoryItem(item, &obj, true); | |
571 return obj.GetAsString(); | |
572 } | 584 } |
573 | 585 |
574 WebHistoryItem HistoryItemFromString(const std::string& serialized_item) { | 586 ExplodedHttpBodyElement::~ExplodedHttpBodyElement() { |
575 return HistoryItemFromString(serialized_item, ALWAYS_INCLUDE_FORM_DATA, true); | |
576 } | 587 } |
577 | 588 |
578 std::vector<base::FilePath> FilePathsFromHistoryState( | 589 ExplodedHttpBody::ExplodedHttpBody() |
579 const std::string& content_state) { | 590 : identifier(0), |
580 // TODO(darin): We should avoid using the WebKit API here, so that we do not | 591 contains_passwords(false), |
581 // need to have WebKit initialized before calling this method. | 592 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 } | 593 } |
605 | 594 |
606 // For testing purposes only. | 595 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 } | 596 } |
624 | 597 |
625 int HistoryItemCurrentVersion() { | 598 ExplodedFrameState::ExplodedFrameState() |
626 return kVersion; | 599 : item_sequence_number(0), |
600 document_sequence_number(0), | |
601 visit_count(0), | |
602 visited_time(0.0), | |
603 page_scale_factor(0.0), | |
604 is_target_item(false) { | |
627 } | 605 } |
628 | 606 |
629 std::string RemovePasswordDataFromHistoryState( | 607 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 } | 608 } |
643 | 609 |
644 std::string RemoveScrollOffsetFromHistoryState( | 610 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 } | 611 } |
657 | 612 |
658 std::string CreateHistoryStateForURL(const GURL& url) { | 613 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 } | 614 } |
669 | 615 |
670 } // namespace webkit_glue | 616 bool DecodePageState(const std::string& encoded, ExplodedPageState* exploded) { |
617 *exploded = ExplodedPageState(); | |
618 | |
619 if (encoded.empty()) | |
620 return true; | |
621 | |
622 SerializeObject obj(encoded.data(), static_cast<int>(encoded.size())); | |
623 ReadPageState(&obj, exploded); | |
624 return !obj.parse_error; | |
625 } | |
626 | |
627 bool EncodePageState(const ExplodedPageState& exploded, std::string* encoded) { | |
628 SerializeObject obj; | |
629 obj.version = kCurrentVersion; | |
630 WritePageState(exploded, &obj); | |
631 *encoded = obj.GetAsString(); | |
632 return true; | |
633 } | |
634 | |
635 } // namespace content | |
OLD | NEW |