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 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 // A bunch of convenience functions to read/write to SerializeObjects. The | 204 // A bunch of convenience functions to read/write to SerializeObjects. The |
205 // de-serializers assume the input data will be in the correct format and fall | 205 // de-serializers assume the input data will be in the correct format and fall |
206 // back to returning safe defaults when not. | 206 // back to returning safe defaults when not. |
207 | 207 |
208 void WriteData(const void* data, int length, SerializeObject* obj) { | 208 void WriteData(const void* data, int length, SerializeObject* obj) { |
209 obj->pickle.WriteData(static_cast<const char*>(data), length); | 209 obj->pickle.WriteData(static_cast<const char*>(data), length); |
210 } | 210 } |
211 | 211 |
212 void ReadData(SerializeObject* obj, const void** data, int* length) { | 212 void ReadData(SerializeObject* obj, const void** data, int* length) { |
213 const char* tmp; | 213 const char* tmp; |
214 if (obj->pickle.ReadData(&obj->iter, &tmp, length)) { | 214 if (obj->iter.ReadData(&tmp, length)) { |
215 *data = tmp; | 215 *data = tmp; |
216 } else { | 216 } else { |
217 obj->parse_error = true; | 217 obj->parse_error = true; |
218 *data = NULL; | 218 *data = NULL; |
219 *length = 0; | 219 *length = 0; |
220 } | 220 } |
221 } | 221 } |
222 | 222 |
223 void WriteInteger(int data, SerializeObject* obj) { | 223 void WriteInteger(int data, SerializeObject* obj) { |
224 obj->pickle.WriteInt(data); | 224 obj->pickle.WriteInt(data); |
225 } | 225 } |
226 | 226 |
227 int ReadInteger(SerializeObject* obj) { | 227 int ReadInteger(SerializeObject* obj) { |
228 int tmp; | 228 int tmp; |
229 if (obj->pickle.ReadInt(&obj->iter, &tmp)) | 229 if (obj->iter.ReadInt(&tmp)) |
230 return tmp; | 230 return tmp; |
231 obj->parse_error = true; | 231 obj->parse_error = true; |
232 return 0; | 232 return 0; |
233 } | 233 } |
234 | 234 |
235 void WriteInteger64(int64 data, SerializeObject* obj) { | 235 void WriteInteger64(int64 data, SerializeObject* obj) { |
236 obj->pickle.WriteInt64(data); | 236 obj->pickle.WriteInt64(data); |
237 } | 237 } |
238 | 238 |
239 int64 ReadInteger64(SerializeObject* obj) { | 239 int64 ReadInteger64(SerializeObject* obj) { |
240 int64 tmp = 0; | 240 int64 tmp = 0; |
241 if (obj->pickle.ReadInt64(&obj->iter, &tmp)) | 241 if (obj->iter.ReadInt64(&tmp)) |
242 return tmp; | 242 return tmp; |
243 obj->parse_error = true; | 243 obj->parse_error = true; |
244 return 0; | 244 return 0; |
245 } | 245 } |
246 | 246 |
247 void WriteReal(double data, SerializeObject* obj) { | 247 void WriteReal(double data, SerializeObject* obj) { |
248 WriteData(&data, sizeof(double), obj); | 248 WriteData(&data, sizeof(double), obj); |
249 } | 249 } |
250 | 250 |
251 double ReadReal(SerializeObject* obj) { | 251 double ReadReal(SerializeObject* obj) { |
252 const void* tmp = NULL; | 252 const void* tmp = NULL; |
253 int length = 0; | 253 int length = 0; |
254 double value = 0.0; | 254 double value = 0.0; |
255 ReadData(obj, &tmp, &length); | 255 ReadData(obj, &tmp, &length); |
256 if (length == static_cast<int>(sizeof(double))) { | 256 if (length == static_cast<int>(sizeof(double))) { |
257 // Use memcpy, as tmp may not be correctly aligned. | 257 // Use memcpy, as tmp may not be correctly aligned. |
258 memcpy(&value, tmp, sizeof(double)); | 258 memcpy(&value, tmp, sizeof(double)); |
259 } else { | 259 } else { |
260 obj->parse_error = true; | 260 obj->parse_error = true; |
261 } | 261 } |
262 return value; | 262 return value; |
263 } | 263 } |
264 | 264 |
265 void WriteBoolean(bool data, SerializeObject* obj) { | 265 void WriteBoolean(bool data, SerializeObject* obj) { |
266 obj->pickle.WriteInt(data ? 1 : 0); | 266 obj->pickle.WriteInt(data ? 1 : 0); |
267 } | 267 } |
268 | 268 |
269 bool ReadBoolean(SerializeObject* obj) { | 269 bool ReadBoolean(SerializeObject* obj) { |
270 bool tmp; | 270 bool tmp; |
271 if (obj->pickle.ReadBool(&obj->iter, &tmp)) | 271 if (obj->iter.ReadBool(&tmp)) |
272 return tmp; | 272 return tmp; |
273 obj->parse_error = true; | 273 obj->parse_error = true; |
274 return false; | 274 return false; |
275 } | 275 } |
276 | 276 |
277 void WriteGURL(const GURL& url, SerializeObject* obj) { | 277 void WriteGURL(const GURL& url, SerializeObject* obj) { |
278 obj->pickle.WriteString(url.possibly_invalid_spec()); | 278 obj->pickle.WriteString(url.possibly_invalid_spec()); |
279 } | 279 } |
280 | 280 |
281 GURL ReadGURL(SerializeObject* obj) { | 281 GURL ReadGURL(SerializeObject* obj) { |
282 std::string spec; | 282 std::string spec; |
283 if (obj->pickle.ReadString(&obj->iter, &spec)) | 283 if (obj->iter.ReadString(&spec)) |
284 return GURL(spec); | 284 return GURL(spec); |
285 obj->parse_error = true; | 285 obj->parse_error = true; |
286 return GURL(); | 286 return GURL(); |
287 } | 287 } |
288 | 288 |
289 void WriteStdString(const std::string& s, SerializeObject* obj) { | 289 void WriteStdString(const std::string& s, SerializeObject* obj) { |
290 obj->pickle.WriteString(s); | 290 obj->pickle.WriteString(s); |
291 } | 291 } |
292 | 292 |
293 std::string ReadStdString(SerializeObject* obj) { | 293 std::string ReadStdString(SerializeObject* obj) { |
294 std::string s; | 294 std::string s; |
295 if (obj->pickle.ReadString(&obj->iter, &s)) | 295 if (obj->iter.ReadString(&s)) |
296 return s; | 296 return s; |
297 obj->parse_error = true; | 297 obj->parse_error = true; |
298 return std::string(); | 298 return std::string(); |
299 } | 299 } |
300 | 300 |
301 // WriteString pickles the NullableString16 as <int length><char16* data>. | 301 // WriteString pickles the NullableString16 as <int length><char16* data>. |
302 // If length == -1, then the NullableString16 itself is null. Otherwise the | 302 // If length == -1, then the NullableString16 itself is null. Otherwise the |
303 // length is the number of char16 (not bytes) in the NullableString16. | 303 // length is the number of char16 (not bytes) in the NullableString16. |
304 void WriteString(const base::NullableString16& str, SerializeObject* obj) { | 304 void WriteString(const base::NullableString16& str, SerializeObject* obj) { |
305 if (str.is_null()) { | 305 if (str.is_null()) { |
306 obj->pickle.WriteInt(-1); | 306 obj->pickle.WriteInt(-1); |
307 } else { | 307 } else { |
308 const base::char16* data = str.string().data(); | 308 const base::char16* data = str.string().data(); |
309 size_t length_in_bytes = str.string().length() * sizeof(base::char16); | 309 size_t length_in_bytes = str.string().length() * sizeof(base::char16); |
310 | 310 |
311 CHECK_LT(length_in_bytes, | 311 CHECK_LT(length_in_bytes, |
312 static_cast<size_t>(std::numeric_limits<int>::max())); | 312 static_cast<size_t>(std::numeric_limits<int>::max())); |
313 obj->pickle.WriteInt(length_in_bytes); | 313 obj->pickle.WriteInt(length_in_bytes); |
314 obj->pickle.WriteBytes(data, length_in_bytes); | 314 obj->pickle.WriteBytes(data, length_in_bytes); |
315 } | 315 } |
316 } | 316 } |
317 | 317 |
318 // This reads a serialized NullableString16 from obj. If a string can't be | 318 // This reads a serialized NullableString16 from obj. If a string can't be |
319 // read, NULL is returned. | 319 // read, NULL is returned. |
320 const base::char16* ReadStringNoCopy(SerializeObject* obj, int* num_chars) { | 320 const base::char16* ReadStringNoCopy(SerializeObject* obj, int* num_chars) { |
321 int length_in_bytes; | 321 int length_in_bytes; |
322 if (!obj->pickle.ReadInt(&obj->iter, &length_in_bytes)) { | 322 if (!obj->iter.ReadInt(&length_in_bytes)) { |
323 obj->parse_error = true; | 323 obj->parse_error = true; |
324 return NULL; | 324 return NULL; |
325 } | 325 } |
326 | 326 |
327 if (length_in_bytes < 0) | 327 if (length_in_bytes < 0) |
328 return NULL; | 328 return NULL; |
329 | 329 |
330 const char* data; | 330 const char* data; |
331 if (!obj->pickle.ReadBytes(&obj->iter, &data, length_in_bytes)) { | 331 if (!obj->iter.ReadBytes(&data, length_in_bytes)) { |
332 obj->parse_error = true; | 332 obj->parse_error = true; |
333 return NULL; | 333 return NULL; |
334 } | 334 } |
335 | 335 |
336 if (num_chars) | 336 if (num_chars) |
337 *num_chars = length_in_bytes / sizeof(base::char16); | 337 *num_chars = length_in_bytes / sizeof(base::char16); |
338 return reinterpret_cast<const base::char16*>(data); | 338 return reinterpret_cast<const base::char16*>(data); |
339 } | 339 } |
340 | 340 |
341 base::NullableString16 ReadString(SerializeObject* obj) { | 341 base::NullableString16 ReadString(SerializeObject* obj) { |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
741 float device_scale_factor, | 741 float device_scale_factor, |
742 ExplodedPageState* exploded) { | 742 ExplodedPageState* exploded) { |
743 g_device_scale_factor_for_testing = device_scale_factor; | 743 g_device_scale_factor_for_testing = device_scale_factor; |
744 bool rv = DecodePageState(encoded, exploded); | 744 bool rv = DecodePageState(encoded, exploded); |
745 g_device_scale_factor_for_testing = 0.0; | 745 g_device_scale_factor_for_testing = 0.0; |
746 return rv; | 746 return rv; |
747 } | 747 } |
748 #endif | 748 #endif |
749 | 749 |
750 } // namespace content | 750 } // namespace content |
OLD | NEW |