| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "webkit/glue/glue_serialize_deprecated.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/pickle.h" | |
| 10 #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" | |
| 21 #include "webkit/base/file_path_string_conversions.h" | |
| 22 | |
| 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 { | |
| 35 | |
| 36 enum IncludeFormData { | |
| 37 NEVER_INCLUDE_FORM_DATA, | |
| 38 INCLUDE_FORM_DATA_WITHOUT_PASSWORDS, | |
| 39 ALWAYS_INCLUDE_FORM_DATA | |
| 40 }; | |
| 41 | |
| 42 struct SerializeObject { | |
| 43 SerializeObject() : version(0) {} | |
| 44 SerializeObject(const char* data, int len) | |
| 45 : pickle(data, len), version(0) { iter = PickleIterator(pickle); } | |
| 46 | |
| 47 std::string GetAsString() { | |
| 48 return std::string(static_cast<const char*>(pickle.data()), pickle.size()); | |
| 49 } | |
| 50 | |
| 51 Pickle pickle; | |
| 52 mutable PickleIterator iter; | |
| 53 mutable int version; | |
| 54 }; | |
| 55 | |
| 56 // TODO(mpcomplete): obsolete versions 1 and 2 after 1/1/2008. | |
| 57 // Version ID used in reading/writing history items. | |
| 58 // 1: Initial revision. | |
| 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) | |
| 73 // 14: Adds list of referenced files, version written only for first item. | |
| 74 // Should be const, but unit tests may modify it. | |
| 75 // | |
| 76 // NOTE: If the version is -1, then the pickle contains only a URL string. | |
| 77 // See CreateHistoryStateForURL. | |
| 78 // | |
| 79 int kVersion = 14; | |
| 80 | |
| 81 // A bunch of convenience functions to read/write to SerializeObjects. | |
| 82 // The serializers assume the input data is in the correct format and so does | |
| 83 // no error checking. | |
| 84 void WriteData(const void* data, int length, SerializeObject* obj) { | |
| 85 obj->pickle.WriteData(static_cast<const char*>(data), length); | |
| 86 } | |
| 87 | |
| 88 void ReadData(const SerializeObject* obj, const void** data, int* length) { | |
| 89 const char* tmp; | |
| 90 if (obj->pickle.ReadData(&obj->iter, &tmp, length)) { | |
| 91 *data = tmp; | |
| 92 } else { | |
| 93 *data = NULL; | |
| 94 *length = 0; | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 bool ReadBytes(const SerializeObject* obj, const void** data, int length) { | |
| 99 const char *tmp; | |
| 100 if (!obj->pickle.ReadBytes(&obj->iter, &tmp, length)) | |
| 101 return false; | |
| 102 *data = tmp; | |
| 103 return true; | |
| 104 } | |
| 105 | |
| 106 void WriteInteger(int data, SerializeObject* obj) { | |
| 107 obj->pickle.WriteInt(data); | |
| 108 } | |
| 109 | |
| 110 int ReadInteger(const SerializeObject* obj) { | |
| 111 int tmp; | |
| 112 if (obj->pickle.ReadInt(&obj->iter, &tmp)) | |
| 113 return tmp; | |
| 114 return 0; | |
| 115 } | |
| 116 | |
| 117 void ConsumeInteger(const SerializeObject* obj) { | |
| 118 int unused ALLOW_UNUSED = ReadInteger(obj); | |
| 119 } | |
| 120 | |
| 121 void WriteInteger64(int64 data, SerializeObject* obj) { | |
| 122 obj->pickle.WriteInt64(data); | |
| 123 } | |
| 124 | |
| 125 int64 ReadInteger64(const SerializeObject* obj) { | |
| 126 int64 tmp = 0; | |
| 127 obj->pickle.ReadInt64(&obj->iter, &tmp); | |
| 128 return tmp; | |
| 129 } | |
| 130 | |
| 131 void WriteReal(double data, SerializeObject* obj) { | |
| 132 WriteData(&data, sizeof(double), obj); | |
| 133 } | |
| 134 | |
| 135 double ReadReal(const SerializeObject* obj) { | |
| 136 const void* tmp = NULL; | |
| 137 int length = 0; | |
| 138 double value = 0.0; | |
| 139 ReadData(obj, &tmp, &length); | |
| 140 if (tmp && length >= static_cast<int>(sizeof(double))) { | |
| 141 // Use memcpy, as tmp may not be correctly aligned. | |
| 142 memcpy(&value, tmp, sizeof(double)); | |
| 143 } | |
| 144 return value; | |
| 145 } | |
| 146 | |
| 147 void WriteBoolean(bool data, SerializeObject* obj) { | |
| 148 obj->pickle.WriteInt(data ? 1 : 0); | |
| 149 } | |
| 150 | |
| 151 bool ReadBoolean(const SerializeObject* obj) { | |
| 152 bool tmp; | |
| 153 if (obj->pickle.ReadBool(&obj->iter, &tmp)) | |
| 154 return tmp; | |
| 155 return false; | |
| 156 } | |
| 157 | |
| 158 void WriteGURL(const GURL& url, SerializeObject* obj) { | |
| 159 obj->pickle.WriteString(url.possibly_invalid_spec()); | |
| 160 } | |
| 161 | |
| 162 GURL ReadGURL(const SerializeObject* obj) { | |
| 163 std::string spec; | |
| 164 if (obj->pickle.ReadString(&obj->iter, &spec)) | |
| 165 return GURL(spec); | |
| 166 return GURL(); | |
| 167 } | |
| 168 | |
| 169 // Read/WriteString pickle the WebString as <int length><WebUChar* data>. | |
| 170 // If length == -1, then the WebString itself is NULL (WebString()). | |
| 171 // Otherwise the length is the number of WebUChars (not bytes) in the WebString. | |
| 172 void WriteString(const WebString& str, SerializeObject* obj) { | |
| 173 base::string16 string = str; | |
| 174 const char16* data = string.data(); | |
| 175 size_t length_in_uchars = string.length(); | |
| 176 size_t length_in_bytes = length_in_uchars * sizeof(char16); | |
| 177 switch (kVersion) { | |
| 178 case 1: | |
| 179 // Version 1 writes <length in bytes><string data>. | |
| 180 // It saves WebString() and "" as "". | |
| 181 obj->pickle.WriteInt(length_in_bytes); | |
| 182 obj->pickle.WriteBytes(data, length_in_bytes); | |
| 183 break; | |
| 184 case 2: | |
| 185 // Version 2 writes <length in WebUChar><string data>. | |
| 186 // It uses -1 in the length field to mean WebString(). | |
| 187 if (str.isNull()) { | |
| 188 obj->pickle.WriteInt(-1); | |
| 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; | |
| 215 | |
| 216 // Starting with version 2, -1 means WebString(). | |
| 217 if (length == -1) | |
| 218 return NULL; | |
| 219 | |
| 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; | |
| 227 if (!ReadBytes(obj, &data, bytes)) | |
| 228 return NULL; | |
| 229 | |
| 230 if (num_chars) | |
| 231 *num_chars = bytes / sizeof(WebUChar); | |
| 232 return static_cast<const WebUChar*>(data); | |
| 233 } | |
| 234 | |
| 235 WebString ReadString(const SerializeObject* obj) { | |
| 236 int num_chars; | |
| 237 const WebUChar* chars = ReadStringNoCopy(obj, &num_chars); | |
| 238 return chars ? WebString(chars, num_chars) : WebString(); | |
| 239 } | |
| 240 | |
| 241 void ConsumeString(const SerializeObject* obj) { | |
| 242 const WebUChar* unused ALLOW_UNUSED = ReadStringNoCopy(obj, NULL); | |
| 243 } | |
| 244 | |
| 245 // Writes a Vector of Strings into a SerializeObject for serialization. | |
| 246 void WriteStringVector( | |
| 247 const WebVector<WebString>& data, SerializeObject* obj) { | |
| 248 WriteInteger(static_cast<int>(data.size()), obj); | |
| 249 for (size_t i = 0, c = data.size(); i < c; ++i) { | |
| 250 unsigned ui = static_cast<unsigned>(i); // sigh | |
| 251 WriteString(data[ui], obj); | |
| 252 } | |
| 253 } | |
| 254 | |
| 255 WebVector<WebString> ReadStringVector(const SerializeObject* obj) { | |
| 256 int num_elements = ReadInteger(obj); | |
| 257 WebVector<WebString> result(static_cast<size_t>(num_elements)); | |
| 258 for (int i = 0; i < num_elements; ++i) | |
| 259 result[i] = ReadString(obj); | |
| 260 return result; | |
| 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; | |
| 275 | |
| 276 WriteInteger(static_cast<int>(http_body.elementCount()), obj); | |
| 277 WebHTTPBody::Element element; | |
| 278 for (size_t i = 0; http_body.elementAt(i, element); ++i) { | |
| 279 WriteInteger(element.type, obj); | |
| 280 if (element.type == WebHTTPBody::Element::TypeData) { | |
| 281 WriteData(element.data.data(), static_cast<int>(element.data.size()), | |
| 282 obj); | |
| 283 } else if (element.type == WebHTTPBody::Element::TypeFile) { | |
| 284 WriteString(element.filePath, obj); | |
| 285 WriteInteger64(element.fileStart, obj); | |
| 286 WriteInteger64(element.fileLength, obj); | |
| 287 WriteReal(element.modificationTime, obj); | |
| 288 } else if (element.type == WebHTTPBody::Element::TypeURL) { | |
| 289 WriteGURL(element.url, obj); | |
| 290 WriteInteger64(element.fileStart, obj); | |
| 291 WriteInteger64(element.fileLength, obj); | |
| 292 WriteReal(element.modificationTime, obj); | |
| 293 } else { | |
| 294 WriteGURL(element.url, obj); | |
| 295 } | |
| 296 } | |
| 297 WriteInteger64(http_body.identifier(), obj); | |
| 298 WriteBoolean(http_body.containsPasswordData(), obj); | |
| 299 } | |
| 300 | |
| 301 WebHTTPBody ReadFormData(const SerializeObject* obj) { | |
| 302 // In newer versions, an initial boolean indicates if we have form data. | |
| 303 if (obj->version >= 5 && !ReadBoolean(obj)) | |
| 304 return WebHTTPBody(); | |
| 305 | |
| 306 // In older versions, 0 elements implied no form data. | |
| 307 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 | |
| 314 for (int i = 0; i < num_elements; ++i) { | |
| 315 int type = ReadInteger(obj); | |
| 316 if (type == WebHTTPBody::Element::TypeData) { | |
| 317 const void* data; | |
| 318 int length = -1; | |
| 319 ReadData(obj, &data, &length); | |
| 320 if (length >= 0) | |
| 321 http_body.appendData(WebData(static_cast<const char*>(data), length)); | |
| 322 } else if (type == WebHTTPBody::Element::TypeFile) { | |
| 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 } | |
| 332 http_body.appendFileRange(file_path, file_start, file_length, | |
| 333 modification_time); | |
| 334 } else if (type == WebHTTPBody::Element::TypeURL) { | |
| 335 GURL url = ReadGURL(obj); | |
| 336 long long file_start = 0; | |
| 337 long long file_length = -1; | |
| 338 double modification_time = 0.0; | |
| 339 file_start = ReadInteger64(obj); | |
| 340 file_length = ReadInteger64(obj); | |
| 341 modification_time = ReadReal(obj); | |
| 342 http_body.appendURLRange(url, file_start, file_length, | |
| 343 modification_time); | |
| 344 } else if (obj->version >= 10) { | |
| 345 GURL blob_url = ReadGURL(obj); | |
| 346 http_body.appendBlob(blob_url); | |
| 347 } | |
| 348 } | |
| 349 if (obj->version >= 4) | |
| 350 http_body.setIdentifier(ReadInteger64(obj)); | |
| 351 | |
| 352 if (obj->version >= 12) | |
| 353 http_body.setContainsPasswordData(ReadBoolean(obj)); | |
| 354 | |
| 355 return http_body; | |
| 356 } | |
| 357 | |
| 358 // Writes the HistoryItem data into the SerializeObject object for | |
| 359 // serialization. | |
| 360 void WriteHistoryItem( | |
| 361 const WebHistoryItem& item, SerializeObject* obj, bool is_top) { | |
| 362 // 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 | |
| 364 // 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 | |
| 366 // data, such as password fields. | |
| 367 | |
| 368 if (kVersion >= 14) { | |
| 369 if (is_top) { | |
| 370 WriteInteger(kVersion, obj); | |
| 371 | |
| 372 // Insert the list of referenced files, so they can be extracted easily | |
| 373 // from the serialized data (avoiding the need to call into Blink again). | |
| 374 WriteStringVector(item.getReferencedFilePaths(), obj); | |
| 375 } | |
| 376 } else { | |
| 377 WriteInteger(kVersion, obj); | |
| 378 } | |
| 379 | |
| 380 WriteString(item.urlString(), obj); | |
| 381 WriteString(item.originalURLString(), obj); | |
| 382 WriteString(item.target(), obj); | |
| 383 WriteString(item.parent(), obj); | |
| 384 WriteString(item.title(), obj); | |
| 385 WriteString(item.alternateTitle(), obj); | |
| 386 WriteReal(item.lastVisitedTime(), obj); | |
| 387 WriteInteger(item.scrollOffset().x, obj); | |
| 388 WriteInteger(item.scrollOffset().y, obj); | |
| 389 WriteBoolean(item.isTargetItem(), obj); | |
| 390 WriteInteger(item.visitCount(), obj); | |
| 391 WriteString(item.referrer(), obj); | |
| 392 | |
| 393 WriteStringVector(item.documentState(), 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 | |
| 413 // Subitems | |
| 414 const WebVector<WebHistoryItem>& children = item.children(); | |
| 415 WriteInteger(static_cast<int>(children.size()), obj); | |
| 416 for (size_t i = 0, c = children.size(); i < c; ++i) | |
| 417 WriteHistoryItem(children[i], obj, false); | |
| 418 } | |
| 419 | |
| 420 // Creates a new HistoryItem tree based on the serialized string. | |
| 421 // Assumes the data is in the format returned by WriteHistoryItem. | |
| 422 WebHistoryItem ReadHistoryItem( | |
| 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. | |
| 445 } | |
| 446 | |
| 447 WebHistoryItem item; | |
| 448 item.initialize(); | |
| 449 | |
| 450 item.setURLString(ReadString(obj)); | |
| 451 item.setOriginalURLString(ReadString(obj)); | |
| 452 item.setTarget(ReadString(obj)); | |
| 453 item.setParent(ReadString(obj)); | |
| 454 item.setTitle(ReadString(obj)); | |
| 455 item.setAlternateTitle(ReadString(obj)); | |
| 456 item.setLastVisitedTime(ReadReal(obj)); | |
| 457 | |
| 458 int x = ReadInteger(obj); | |
| 459 int y = ReadInteger(obj); | |
| 460 if (include_scroll_offset) | |
| 461 item.setScrollOffset(WebPoint(x, y)); | |
| 462 | |
| 463 item.setIsTargetItem(ReadBoolean(obj)); | |
| 464 item.setVisitCount(ReadInteger(obj)); | |
| 465 item.setReferrer(ReadString(obj)); | |
| 466 | |
| 467 item.setDocumentState(ReadStringVector(obj)); | |
| 468 | |
| 469 if (obj->version >= 11) | |
| 470 item.setPageScaleFactor(ReadReal(obj)); | |
| 471 if (obj->version >= 9) | |
| 472 item.setItemSequenceNumber(ReadInteger64(obj)); | |
| 473 if (obj->version >= 6) | |
| 474 item.setDocumentSequenceNumber(ReadInteger64(obj)); | |
| 475 if (obj->version >= 7) { | |
| 476 bool has_state_object = ReadBoolean(obj); | |
| 477 if (has_state_object) { | |
| 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 | |
| 487 if (obj->version < 14) | |
| 488 ConsumeString(obj); // Skip unused referrer string. | |
| 489 | |
| 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) | |
| 506 if (obj->version == 11) { | |
| 507 // Now-unused values that shipped in this version of Chrome for Android when | |
| 508 // it was on a private branch. | |
| 509 ReadReal(obj); | |
| 510 ReadBoolean(obj); | |
| 511 | |
| 512 // In this version, pageScaleFactor included deviceScaleFactor and scroll | |
| 513 // offsets were premultiplied by pageScaleFactor. | |
| 514 if (item.pageScaleFactor()) { | |
| 515 if (include_scroll_offset) | |
| 516 item.setScrollOffset( | |
| 517 WebPoint(item.scrollOffset().x / item.pageScaleFactor(), | |
| 518 item.scrollOffset().y / item.pageScaleFactor())); | |
| 519 item.setPageScaleFactor(item.pageScaleFactor() / | |
| 520 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay() | |
| 521 .device_scale_factor()); | |
| 522 } | |
| 523 } | |
| 524 #endif | |
| 525 | |
| 526 // Subitems | |
| 527 int num_children = ReadInteger(obj); | |
| 528 for (int i = 0; i < num_children; ++i) | |
| 529 item.appendToChildren(ReadHistoryItem(obj, | |
| 530 include_form_data, | |
| 531 include_scroll_offset, | |
| 532 false)); | |
| 533 | |
| 534 return item; | |
| 535 } | |
| 536 | |
| 537 // Reconstruct a HistoryItem from a string, using our JSON Value deserializer. | |
| 538 // This assumes that the given serialized string has all the required key,value | |
| 539 // pairs, and does minimal error checking. The form data of the post is restored | |
| 540 // if |include_form_data| is |ALWAYS_INCLUDE_FORM_DATA| or if the data doesn't | |
| 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 } | |
| 555 | |
| 556 void ToFilePathVector(const WebVector<WebString>& input, | |
| 557 std::vector<base::FilePath>* output) { | |
| 558 for (size_t i = 0; i < input.size(); ++i) | |
| 559 output->push_back(webkit_base::WebStringToFilePath(input[i])); | |
| 560 } | |
| 561 | |
| 562 } // namespace | |
| 563 | |
| 564 // Serialize a HistoryItem to a string, using our JSON Value serializer. | |
| 565 std::string HistoryItemToString(const WebHistoryItem& item) { | |
| 566 if (item.isNull()) | |
| 567 return std::string(); | |
| 568 | |
| 569 SerializeObject obj; | |
| 570 WriteHistoryItem(item, &obj, true); | |
| 571 return obj.GetAsString(); | |
| 572 } | |
| 573 | |
| 574 WebHistoryItem HistoryItemFromString(const std::string& serialized_item) { | |
| 575 return HistoryItemFromString(serialized_item, ALWAYS_INCLUDE_FORM_DATA, true); | |
| 576 } | |
| 577 | |
| 578 std::vector<base::FilePath> FilePathsFromHistoryState( | |
| 579 const std::string& content_state) { | |
| 580 // TODO(darin): We should avoid using the WebKit API here, so that we do not | |
| 581 // need to have WebKit initialized before calling this method. | |
| 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 } | |
| 605 | |
| 606 // For testing purposes only. | |
| 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 } | |
| 624 | |
| 625 int HistoryItemCurrentVersion() { | |
| 626 return kVersion; | |
| 627 } | |
| 628 | |
| 629 std::string RemovePasswordDataFromHistoryState( | |
| 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 } | |
| 643 | |
| 644 std::string RemoveScrollOffsetFromHistoryState( | |
| 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 } | |
| 657 | |
| 658 std::string CreateHistoryStateForURL(const GURL& url) { | |
| 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 } | |
| 669 | |
| 670 } // namespace webkit_glue | |
| OLD | NEW |