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 // Data types for keys. |
| 16 template <typename MapKey> |
| 17 struct MapKeyValidateParams { |
| 18 public: |
| 19 typedef NoValidateParams ElementValidateParams; |
| 20 static const uint32_t expected_num_elements = 0; |
| 21 static const bool element_is_nullable = false; |
| 22 }; |
| 23 |
| 24 // For non-nullable strings only. (Which is OK; map keys can't be null.) |
| 25 template <> |
| 26 struct MapKeyValidateParams<mojo::internal::Array_Data<char>*> { |
| 27 public: |
| 28 typedef ArrayValidateParams<0, false, NoValidateParams> ElementValidateParams; |
| 29 static const uint32_t expected_num_elements = 0; |
| 30 static const bool element_is_nullable = false; |
| 31 }; |
| 32 |
| 33 // Map serializes into a struct which has two arrays as struct fields, the keys |
| 34 // and the values. |
| 35 template <typename Key, typename Value> |
| 36 class Map_Data { |
| 37 public: |
| 38 static Map_Data* New(Buffer* buf) { |
| 39 return new (buf->Allocate(sizeof(Map_Data))) Map_Data(); |
| 40 } |
| 41 |
| 42 template <typename ValueParams> |
| 43 static bool Validate(const void* data, BoundsChecker* bounds_checker) { |
| 44 if (!data) |
| 45 return true; |
| 46 |
| 47 if (!ValidateStructHeader(data, sizeof(Map_Data), 2, bounds_checker)) |
| 48 return false; |
| 49 |
| 50 const Map_Data* object = static_cast<const Map_Data*>(data); |
| 51 if (!ValidateEncodedPointer(&object->keys.offset)) { |
| 52 ReportValidationError(VALIDATION_ERROR_ILLEGAL_POINTER); |
| 53 return false; |
| 54 } |
| 55 if (!object->keys.offset) { |
| 56 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 57 "null key array in map struct"); |
| 58 return false; |
| 59 } |
| 60 if (!Array_Data<Key>::template Validate<MapKeyValidateParams<Key>>( |
| 61 DecodePointerRaw(&object->keys.offset), bounds_checker)) { |
| 62 return false; |
| 63 } |
| 64 |
| 65 if (!ValidateEncodedPointer(&object->values.offset)) { |
| 66 ReportValidationError(VALIDATION_ERROR_ILLEGAL_POINTER); |
| 67 return false; |
| 68 } |
| 69 if (!object->values.offset) { |
| 70 ReportValidationError(VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
| 71 "null value array in map struct"); |
| 72 return false; |
| 73 } |
| 74 if (!Array_Data<Value>::template Validate<ValueParams>( |
| 75 DecodePointerRaw(&object->values.offset), bounds_checker)) { |
| 76 return false; |
| 77 } |
| 78 |
| 79 const ArrayHeader* key_header = |
| 80 static_cast<const ArrayHeader*>(DecodePointerRaw(&object->keys.offset)); |
| 81 const ArrayHeader* value_header = static_cast<const ArrayHeader*>( |
| 82 DecodePointerRaw(&object->values.offset)); |
| 83 if (key_header->num_elements != value_header->num_elements) { |
| 84 ReportValidationError(VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP); |
| 85 return false; |
| 86 } |
| 87 |
| 88 return true; |
| 89 } |
| 90 |
| 91 StructHeader header_; |
| 92 |
| 93 ArrayPointer<Key> keys; |
| 94 ArrayPointer<Value> values; |
| 95 |
| 96 void EncodePointersAndHandles(std::vector<mojo::Handle>* handles) { |
| 97 Encode(&keys, handles); |
| 98 Encode(&values, handles); |
| 99 } |
| 100 |
| 101 void DecodePointersAndHandles(std::vector<mojo::Handle>* handles) { |
| 102 Decode(&keys, handles); |
| 103 Decode(&values, handles); |
| 104 } |
| 105 |
| 106 private: |
| 107 Map_Data() { |
| 108 header_.num_bytes = sizeof(*this); |
| 109 header_.num_fields = 2; |
| 110 } |
| 111 ~Map_Data() = delete; |
| 112 }; |
| 113 static_assert(sizeof(Map_Data<char, char>) == 24, "Bad sizeof(Map_Data)"); |
| 114 |
| 115 } // namespace internal |
| 116 } // namespace mojo |
| 117 |
| 118 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_ |
OLD | NEW |