| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <string.h> // For |memcpy()|. | 9 #include <string.h> // For |memcpy()|. |
| 10 | 10 |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 struct ArraySerializer<MojomType, | 346 struct ArraySerializer<MojomType, |
| 347 MaybeConstUserType, | 347 MaybeConstUserType, |
| 348 UserTypeIterator, | 348 UserTypeIterator, |
| 349 typename std::enable_if<BelongsTo< | 349 typename std::enable_if<BelongsTo< |
| 350 typename MojomType::Element, | 350 typename MojomType::Element, |
| 351 MojomTypeCategory::ARRAY | MojomTypeCategory::MAP | | 351 MojomTypeCategory::ARRAY | MojomTypeCategory::MAP | |
| 352 MojomTypeCategory::STRING | | 352 MojomTypeCategory::STRING | |
| 353 MojomTypeCategory::STRUCT>::value>::type> { | 353 MojomTypeCategory::STRUCT>::value>::type> { |
| 354 using UserType = typename std::remove_const<MaybeConstUserType>::type; | 354 using UserType = typename std::remove_const<MaybeConstUserType>::type; |
| 355 using Data = typename MojomTypeTraits<MojomType>::Data; | 355 using Data = typename MojomTypeTraits<MojomType>::Data; |
| 356 using DataElement = typename Data::Element; | |
| 357 using Element = typename MojomType::Element; | 356 using Element = typename MojomType::Element; |
| 357 using DataElementPtr = typename MojomTypeTraits<Element>::Data*; |
| 358 using Traits = ArrayTraits<UserType>; | 358 using Traits = ArrayTraits<UserType>; |
| 359 | 359 |
| 360 static size_t GetSerializedSize(UserTypeIterator* input, | 360 static size_t GetSerializedSize(UserTypeIterator* input, |
| 361 SerializationContext* context) { | 361 SerializationContext* context) { |
| 362 size_t element_count = input->GetSize(); | 362 size_t element_count = input->GetSize(); |
| 363 size_t size = | 363 size_t size = sizeof(Data) + element_count * sizeof(typename Data::Element); |
| 364 sizeof(Data) + | |
| 365 element_count * | |
| 366 sizeof(Pointer<typename std::remove_pointer<DataElement>::type>); | |
| 367 for (size_t i = 0; i < element_count; ++i) | 364 for (size_t i = 0; i < element_count; ++i) |
| 368 size += PrepareToSerialize<Element>(input->GetNext(), context); | 365 size += PrepareToSerialize<Element>(input->GetNext(), context); |
| 369 return size; | 366 return size; |
| 370 } | 367 } |
| 371 | 368 |
| 372 static void SerializeElements(UserTypeIterator* input, | 369 static void SerializeElements(UserTypeIterator* input, |
| 373 Buffer* buf, | 370 Buffer* buf, |
| 374 Data* output, | 371 Data* output, |
| 375 const ContainerValidateParams* validate_params, | 372 const ContainerValidateParams* validate_params, |
| 376 SerializationContext* context) { | 373 SerializationContext* context) { |
| 377 size_t size = input->GetSize(); | 374 size_t size = input->GetSize(); |
| 378 for (size_t i = 0; i < size; ++i) { | 375 for (size_t i = 0; i < size; ++i) { |
| 379 DataElement element; | 376 DataElementPtr data_ptr; |
| 380 SerializeCaller<Element>::Run(input->GetNext(), buf, &element, | 377 SerializeCaller<Element>::Run(input->GetNext(), buf, &data_ptr, |
| 381 validate_params->element_validate_params, | 378 validate_params->element_validate_params, |
| 382 context); | 379 context); |
| 383 output->at(i) = element; | 380 output->at(i).Set(data_ptr); |
| 384 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( | 381 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING( |
| 385 !validate_params->element_is_nullable && !element, | 382 !validate_params->element_is_nullable && !data_ptr, |
| 386 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 383 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 387 MakeMessageWithArrayIndex("null in array expecting valid pointers", | 384 MakeMessageWithArrayIndex("null in array expecting valid pointers", |
| 388 size, i)); | 385 size, i)); |
| 389 } | 386 } |
| 390 } | 387 } |
| 391 static bool DeserializeElements(Data* input, | 388 static bool DeserializeElements(Data* input, |
| 392 UserType* output, | 389 UserType* output, |
| 393 SerializationContext* context) { | 390 SerializationContext* context) { |
| 394 bool success = true; | |
| 395 if (!Traits::Resize(*output, input->size())) | 391 if (!Traits::Resize(*output, input->size())) |
| 396 return false; | 392 return false; |
| 397 ArrayIterator<Traits, UserType> iterator(*output); | 393 ArrayIterator<Traits, UserType> iterator(*output); |
| 398 for (size_t i = 0; i < input->size(); ++i) { | 394 for (size_t i = 0; i < input->size(); ++i) { |
| 399 // Note that we rely on complete deserialization taking place in order to | 395 if (!Deserialize<Element>(input->at(i).Get(), &iterator.GetNext(), |
| 400 // transfer ownership of all encoded handles. Therefore we don't | 396 context)) |
| 401 // short-circuit on failure here. | 397 return false; |
| 402 if (!Deserialize<Element>(input->at(i), &iterator.GetNext(), context)) { | |
| 403 success = false; | |
| 404 } | |
| 405 } | 398 } |
| 406 return success; | 399 return true; |
| 407 } | 400 } |
| 408 | 401 |
| 409 private: | 402 private: |
| 410 template <typename T, | 403 template <typename T, |
| 411 bool is_array_or_map = BelongsTo<T, | 404 bool is_array_or_map = BelongsTo<T, |
| 412 MojomTypeCategory::ARRAY | | 405 MojomTypeCategory::ARRAY | |
| 413 MojomTypeCategory::MAP>::value> | 406 MojomTypeCategory::MAP>::value> |
| 414 struct SerializeCaller { | 407 struct SerializeCaller { |
| 415 template <typename InputElementType> | 408 template <typename InputElementType> |
| 416 static void Run(InputElementType&& input, | 409 static void Run(InputElementType&& input, |
| 417 Buffer* buf, | 410 Buffer* buf, |
| 418 DataElement* output, | 411 DataElementPtr* output, |
| 419 const ContainerValidateParams* validate_params, | 412 const ContainerValidateParams* validate_params, |
| 420 SerializationContext* context) { | 413 SerializationContext* context) { |
| 421 Serialize<T>(std::forward<InputElementType>(input), buf, output, context); | 414 Serialize<T>(std::forward<InputElementType>(input), buf, output, context); |
| 422 } | 415 } |
| 423 }; | 416 }; |
| 424 | 417 |
| 425 template <typename T> | 418 template <typename T> |
| 426 struct SerializeCaller<T, true> { | 419 struct SerializeCaller<T, true> { |
| 427 template <typename InputElementType> | 420 template <typename InputElementType> |
| 428 static void Run(InputElementType&& input, | 421 static void Run(InputElementType&& input, |
| 429 Buffer* buf, | 422 Buffer* buf, |
| 430 DataElement* output, | 423 DataElementPtr* output, |
| 431 const ContainerValidateParams* validate_params, | 424 const ContainerValidateParams* validate_params, |
| 432 SerializationContext* context) { | 425 SerializationContext* context) { |
| 433 Serialize<T>(std::forward<InputElementType>(input), buf, output, | 426 Serialize<T>(std::forward<InputElementType>(input), buf, output, |
| 434 validate_params, context); | 427 validate_params, context); |
| 435 } | 428 } |
| 436 }; | 429 }; |
| 437 }; | 430 }; |
| 438 | 431 |
| 439 // Handles serialization and deserialization of arrays of unions. | 432 // Handles serialization and deserialization of arrays of unions. |
| 440 template <typename MojomType, | 433 template <typename MojomType, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 !validate_params->element_is_nullable && output->at(i).is_null(), | 473 !validate_params->element_is_nullable && output->at(i).is_null(), |
| 481 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | 474 VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 482 MakeMessageWithArrayIndex("null in array expecting valid unions", | 475 MakeMessageWithArrayIndex("null in array expecting valid unions", |
| 483 size, i)); | 476 size, i)); |
| 484 } | 477 } |
| 485 } | 478 } |
| 486 | 479 |
| 487 static bool DeserializeElements(Data* input, | 480 static bool DeserializeElements(Data* input, |
| 488 UserType* output, | 481 UserType* output, |
| 489 SerializationContext* context) { | 482 SerializationContext* context) { |
| 490 bool success = true; | |
| 491 if (!Traits::Resize(*output, input->size())) | 483 if (!Traits::Resize(*output, input->size())) |
| 492 return false; | 484 return false; |
| 493 ArrayIterator<Traits, UserType> iterator(*output); | 485 ArrayIterator<Traits, UserType> iterator(*output); |
| 494 for (size_t i = 0; i < input->size(); ++i) { | 486 for (size_t i = 0; i < input->size(); ++i) { |
| 495 // Note that we rely on complete deserialization taking place in order to | 487 if (!Deserialize<Element>(&input->at(i), &iterator.GetNext(), context)) |
| 496 // transfer ownership of all encoded handles. Therefore we don't | 488 return false; |
| 497 // short-circuit on failure here. | |
| 498 if (!Deserialize<Element>(&input->at(i), &iterator.GetNext(), context)) { | |
| 499 success = false; | |
| 500 } | |
| 501 } | 489 } |
| 502 return success; | 490 return true; |
| 503 } | 491 } |
| 504 }; | 492 }; |
| 505 | 493 |
| 506 template <typename Element, typename MaybeConstUserType> | 494 template <typename Element, typename MaybeConstUserType> |
| 507 struct Serializer<Array<Element>, MaybeConstUserType> { | 495 struct Serializer<Array<Element>, MaybeConstUserType> { |
| 508 using UserType = typename std::remove_const<MaybeConstUserType>::type; | 496 using UserType = typename std::remove_const<MaybeConstUserType>::type; |
| 509 using Traits = ArrayTraits<UserType>; | 497 using Traits = ArrayTraits<UserType>; |
| 510 using Impl = ArraySerializer<Array<Element>, | 498 using Impl = ArraySerializer<Array<Element>, |
| 511 MaybeConstUserType, | 499 MaybeConstUserType, |
| 512 ArrayIterator<Traits, MaybeConstUserType>>; | 500 ArrayIterator<Traits, MaybeConstUserType>>; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 if (!input) | 539 if (!input) |
| 552 return CallSetToNullIfExists<Traits>(output); | 540 return CallSetToNullIfExists<Traits>(output); |
| 553 return Impl::DeserializeElements(input, output, context); | 541 return Impl::DeserializeElements(input, output, context); |
| 554 } | 542 } |
| 555 }; | 543 }; |
| 556 | 544 |
| 557 } // namespace internal | 545 } // namespace internal |
| 558 } // namespace mojo | 546 } // namespace mojo |
| 559 | 547 |
| 560 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ | 548 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ |
| OLD | NEW |