| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "content/common/page_state_serialization.h" | 5 #include "content/common/page_state_serialization.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/pickle.h" | 10 #include "base/pickle.h" |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 return std::string(); | 304 return std::string(); |
| 305 } | 305 } |
| 306 | 306 |
| 307 // WriteString pickles the NullableString16 as <int length><char16* data>. | 307 // WriteString pickles the NullableString16 as <int length><char16* data>. |
| 308 // If length == -1, then the NullableString16 itself is null. Otherwise the | 308 // If length == -1, then the NullableString16 itself is null. Otherwise the |
| 309 // length is the number of char16 (not bytes) in the NullableString16. | 309 // length is the number of char16 (not bytes) in the NullableString16. |
| 310 void WriteString(const base::NullableString16& str, SerializeObject* obj) { | 310 void WriteString(const base::NullableString16& str, SerializeObject* obj) { |
| 311 if (str.is_null()) { | 311 if (str.is_null()) { |
| 312 obj->pickle.WriteInt(-1); | 312 obj->pickle.WriteInt(-1); |
| 313 } else { | 313 } else { |
| 314 const char16* data = str.string().data(); | 314 const base::char16* data = str.string().data(); |
| 315 size_t length_in_bytes = str.string().length() * sizeof(char16); | 315 size_t length_in_bytes = str.string().length() * sizeof(base::char16); |
| 316 | 316 |
| 317 CHECK_LT(length_in_bytes, | 317 CHECK_LT(length_in_bytes, |
| 318 static_cast<size_t>(std::numeric_limits<int>::max())); | 318 static_cast<size_t>(std::numeric_limits<int>::max())); |
| 319 obj->pickle.WriteInt(length_in_bytes); | 319 obj->pickle.WriteInt(length_in_bytes); |
| 320 obj->pickle.WriteBytes(data, length_in_bytes); | 320 obj->pickle.WriteBytes(data, length_in_bytes); |
| 321 } | 321 } |
| 322 } | 322 } |
| 323 | 323 |
| 324 // This reads a serialized NullableString16 from obj. If a string can't be | 324 // This reads a serialized NullableString16 from obj. If a string can't be |
| 325 // read, NULL is returned. | 325 // read, NULL is returned. |
| 326 const char16* ReadStringNoCopy(SerializeObject* obj, int* num_chars) { | 326 const base::char16* ReadStringNoCopy(SerializeObject* obj, int* num_chars) { |
| 327 int length_in_bytes; | 327 int length_in_bytes; |
| 328 if (!obj->pickle.ReadInt(&obj->iter, &length_in_bytes)) { | 328 if (!obj->pickle.ReadInt(&obj->iter, &length_in_bytes)) { |
| 329 obj->parse_error = true; | 329 obj->parse_error = true; |
| 330 return NULL; | 330 return NULL; |
| 331 } | 331 } |
| 332 | 332 |
| 333 if (length_in_bytes < 0) | 333 if (length_in_bytes < 0) |
| 334 return NULL; | 334 return NULL; |
| 335 | 335 |
| 336 const char* data; | 336 const char* data; |
| 337 if (!obj->pickle.ReadBytes(&obj->iter, &data, length_in_bytes)) { | 337 if (!obj->pickle.ReadBytes(&obj->iter, &data, length_in_bytes)) { |
| 338 obj->parse_error = true; | 338 obj->parse_error = true; |
| 339 return NULL; | 339 return NULL; |
| 340 } | 340 } |
| 341 | 341 |
| 342 if (num_chars) | 342 if (num_chars) |
| 343 *num_chars = length_in_bytes / sizeof(char16); | 343 *num_chars = length_in_bytes / sizeof(base::char16); |
| 344 return reinterpret_cast<const char16*>(data); | 344 return reinterpret_cast<const base::char16*>(data); |
| 345 } | 345 } |
| 346 | 346 |
| 347 base::NullableString16 ReadString(SerializeObject* obj) { | 347 base::NullableString16 ReadString(SerializeObject* obj) { |
| 348 int num_chars; | 348 int num_chars; |
| 349 const char16* chars = ReadStringNoCopy(obj, &num_chars); | 349 const base::char16* chars = ReadStringNoCopy(obj, &num_chars); |
| 350 return chars ? | 350 return chars ? |
| 351 base::NullableString16(base::string16(chars, num_chars), false) : | 351 base::NullableString16(base::string16(chars, num_chars), false) : |
| 352 base::NullableString16(); | 352 base::NullableString16(); |
| 353 } | 353 } |
| 354 | 354 |
| 355 void ConsumeString(SerializeObject* obj) { | 355 void ConsumeString(SerializeObject* obj) { |
| 356 const char16* unused ALLOW_UNUSED = ReadStringNoCopy(obj, NULL); | 356 const base::char16* unused ALLOW_UNUSED = ReadStringNoCopy(obj, NULL); |
| 357 } | 357 } |
| 358 | 358 |
| 359 template <typename T> | 359 template <typename T> |
| 360 void WriteAndValidateVectorSize(const std::vector<T>& v, SerializeObject* obj) { | 360 void WriteAndValidateVectorSize(const std::vector<T>& v, SerializeObject* obj) { |
| 361 CHECK_LT(v.size(), std::numeric_limits<int>::max() / sizeof(T)); | 361 CHECK_LT(v.size(), std::numeric_limits<int>::max() / sizeof(T)); |
| 362 WriteInteger(static_cast<int>(v.size()), obj); | 362 WriteInteger(static_cast<int>(v.size()), obj); |
| 363 } | 363 } |
| 364 | 364 |
| 365 size_t ReadAndValidateVectorSize(SerializeObject* obj, size_t element_size) { | 365 size_t ReadAndValidateVectorSize(SerializeObject* obj, size_t element_size) { |
| 366 size_t num_elements = static_cast<size_t>(ReadInteger(obj)); | 366 size_t num_elements = static_cast<size_t>(ReadInteger(obj)); |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 703 float device_scale_factor, | 703 float device_scale_factor, |
| 704 ExplodedPageState* exploded) { | 704 ExplodedPageState* exploded) { |
| 705 g_device_scale_factor_for_testing = device_scale_factor; | 705 g_device_scale_factor_for_testing = device_scale_factor; |
| 706 bool rv = DecodePageState(encoded, exploded); | 706 bool rv = DecodePageState(encoded, exploded); |
| 707 g_device_scale_factor_for_testing = 0.0; | 707 g_device_scale_factor_for_testing = 0.0; |
| 708 return rv; | 708 return rv; |
| 709 } | 709 } |
| 710 #endif | 710 #endif |
| 711 | 711 |
| 712 } // namespace content | 712 } // namespace content |
| OLD | NEW |