OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_ |
| 7 |
| 8 #include "mojo/public/cpp/bindings/lib/array_internal.h" |
| 9 #include "mojo/public/cpp/bindings/lib/validate_params.h" |
| 10 #include "mojo/public/cpp/bindings/lib/validation_errors.h" |
| 11 |
| 12 namespace mojo { |
| 13 namespace internal { |
| 14 |
| 15 template <typename MapKey> |
| 16 struct MapDataKeyValidateParams { |
| 17 public: |
| 18 typedef NoValidateParams ElementValidateParams; |
| 19 static const uint32_t expected_num_elements = 0; |
| 20 static const bool element_is_nullable = false; |
| 21 }; |
| 22 |
| 23 template <> |
| 24 struct MapDataKeyValidateParams<mojo::internal::Array_Data<char>*> { |
| 25 public: |
| 26 typedef ArrayValidateParams<0, false, NoValidateParams> ElementValidateParams; |
| 27 static const uint32_t expected_num_elements = 0; |
| 28 static const bool element_is_nullable = false; |
| 29 }; |
| 30 |
| 31 // Map serializes into a struct which has two arrays as struct fields, the keys |
| 32 // and the values. |
| 33 template <typename First, typename Second> |
| 34 class Map_Data { |
| 35 public: |
| 36 static Map_Data* New(mojo::internal::Buffer* buf) { |
| 37 return new (buf->Allocate(sizeof(Map_Data))) Map_Data(); |
| 38 } |
| 39 |
| 40 template <typename ValueParams> |
| 41 static bool Validate(const void* data, |
| 42 mojo::internal::BoundsChecker* bounds_checker) { |
| 43 if (!data) |
| 44 return true; |
| 45 |
| 46 if (!ValidateStructHeader(data, sizeof(Map_Data), 2, bounds_checker)) |
| 47 return false; |
| 48 |
| 49 const Map_Data* MOJO_ALLOW_UNUSED object = |
| 50 static_cast<const Map_Data*>(data); |
| 51 if (!mojo::internal::ValidateEncodedPointer(&object->first.offset)) { |
| 52 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER); |
| 53 return false; |
| 54 } |
| 55 if (!object->first.offset) { |
| 56 ReportValidationError( |
| 57 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 58 "null key array in map struct"); |
| 59 return false; |
| 60 } |
| 61 if (!internal::Array_Data<First>::template Validate< |
| 62 MapDataKeyValidateParams<First>>( |
| 63 mojo::internal::DecodePointerRaw(&object->first.offset), |
| 64 bounds_checker)) { |
| 65 return false; |
| 66 } |
| 67 |
| 68 if (!mojo::internal::ValidateEncodedPointer(&object->second.offset)) { |
| 69 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER); |
| 70 return false; |
| 71 } |
| 72 if (!object->second.offset) { |
| 73 ReportValidationError( |
| 74 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 75 "null value array in map struct"); |
| 76 return false; |
| 77 } |
| 78 if (!internal::Array_Data<Second>::template Validate<ValueParams>( |
| 79 mojo::internal::DecodePointerRaw(&object->second.offset), |
| 80 bounds_checker)) { |
| 81 return false; |
| 82 } |
| 83 |
| 84 const ArrayHeader* first_header = static_cast<const ArrayHeader*>( |
| 85 mojo::internal::DecodePointerRaw(&object->first.offset)); |
| 86 const ArrayHeader* second_header = static_cast<const ArrayHeader*>( |
| 87 mojo::internal::DecodePointerRaw(&object->second.offset)); |
| 88 if (first_header->num_elements != second_header->num_elements) { |
| 89 ReportValidationError( |
| 90 mojo::internal::VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP); |
| 91 return false; |
| 92 } |
| 93 |
| 94 return true; |
| 95 } |
| 96 |
| 97 mojo::internal::StructHeader header_; |
| 98 |
| 99 mojo::internal::ArrayPointer<First> first; |
| 100 mojo::internal::ArrayPointer<Second> second; |
| 101 |
| 102 void EncodePointersAndHandles(std::vector<mojo::Handle>* handles) { |
| 103 mojo::internal::Encode(&first, handles); |
| 104 mojo::internal::Encode(&second, handles); |
| 105 } |
| 106 |
| 107 void DecodePointersAndHandles(std::vector<mojo::Handle>* handles) { |
| 108 mojo::internal::Decode(&first, handles); |
| 109 mojo::internal::Decode(&second, handles); |
| 110 } |
| 111 |
| 112 private: |
| 113 Map_Data() { |
| 114 header_.num_bytes = sizeof(*this); |
| 115 header_.num_fields = 2; |
| 116 } |
| 117 ~Map_Data() = delete; |
| 118 }; |
| 119 |
| 120 } // namespace internal |
| 121 } // namespace mojo |
| 122 |
| 123 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_ |
OLD | NEW |