Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: content/common/page_state_serialization.cc

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

Powered by Google App Engine
This is Rietveld 408576698