| 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 <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "mojo/public/cpp/bindings/lib/array_internal.h" | |
| 12 #include "mojo/public/cpp/bindings/lib/validate_params.h" | |
| 13 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | |
| 14 #include "mojo/public/cpp/bindings/lib/validation_util.h" | |
| 15 | |
| 16 namespace mojo { | |
| 17 namespace internal { | |
| 18 | |
| 19 inline const ArrayValidateParams* GetMapKeyValidateParamsDefault() { | |
| 20 // The memory allocated here never gets released to not cause an exit time | |
| 21 // destructor. | |
| 22 static const ArrayValidateParams* validate_params = | |
| 23 new ArrayValidateParams(0, false, nullptr); | |
| 24 return validate_params; | |
| 25 } | |
| 26 | |
| 27 inline const ArrayValidateParams* GetMapKeyValidateParamsForStrings() { | |
| 28 // The memory allocated here never gets released to not cause an exit time | |
| 29 // destructor. | |
| 30 static const ArrayValidateParams* validate_params = new ArrayValidateParams( | |
| 31 0, false, new ArrayValidateParams(0, false, nullptr)); | |
| 32 return validate_params; | |
| 33 } | |
| 34 | |
| 35 template <typename MapKey> | |
| 36 struct MapKeyValidateParamsFactory { | |
| 37 static const ArrayValidateParams* Get() { | |
| 38 return GetMapKeyValidateParamsDefault(); | |
| 39 } | |
| 40 }; | |
| 41 | |
| 42 // For non-nullable strings only. (Which is OK; map keys can't be null.) | |
| 43 template <> | |
| 44 struct MapKeyValidateParamsFactory<mojo::internal::Array_Data<char>*> { | |
| 45 static const ArrayValidateParams* Get() { | |
| 46 return GetMapKeyValidateParamsForStrings(); | |
| 47 } | |
| 48 }; | |
| 49 | |
| 50 // Map serializes into a struct which has two arrays as struct fields, the keys | |
| 51 // and the values. | |
| 52 // TODO(vardhan): Fill out the missing validation error messages. | |
| 53 template <typename Key, typename Value> | |
| 54 class Map_Data { | |
| 55 public: | |
| 56 static Map_Data* New(Buffer* buf) { | |
| 57 return new (buf->Allocate(sizeof(Map_Data))) Map_Data(); | |
| 58 } | |
| 59 | |
| 60 static ValidationError Validate( | |
| 61 const void* data, | |
| 62 BoundsChecker* bounds_checker, | |
| 63 const ArrayValidateParams* value_validate_params, | |
| 64 std::string* err) { | |
| 65 if (!data) | |
| 66 return ValidationError::NONE; | |
| 67 | |
| 68 ValidationError retval = | |
| 69 ValidateStructHeaderAndClaimMemory(data, bounds_checker, err); | |
| 70 if (retval != ValidationError::NONE) | |
| 71 return retval; | |
| 72 | |
| 73 const Map_Data* object = static_cast<const Map_Data*>(data); | |
| 74 if (object->header_.num_bytes != sizeof(Map_Data) || | |
| 75 object->header_.version != 0) { | |
| 76 MOJO_INTERNAL_DEBUG_SET_ERROR_MSG(err) << ""; | |
| 77 return ValidationError::UNEXPECTED_STRUCT_HEADER; | |
| 78 } | |
| 79 | |
| 80 if (!ValidateEncodedPointer(&object->keys.offset)) { | |
| 81 MOJO_INTERNAL_DEBUG_SET_ERROR_MSG(err) << ""; | |
| 82 return ValidationError::ILLEGAL_POINTER; | |
| 83 } | |
| 84 | |
| 85 if (!object->keys.offset) { | |
| 86 MOJO_INTERNAL_DEBUG_SET_ERROR_MSG(err) << "null key array in map struct"; | |
| 87 return ValidationError::UNEXPECTED_NULL_POINTER; | |
| 88 } | |
| 89 | |
| 90 const ArrayValidateParams* key_validate_params = | |
| 91 MapKeyValidateParamsFactory<Key>::Get(); | |
| 92 retval = | |
| 93 Array_Data<Key>::Validate(DecodePointerRaw(&object->keys.offset), | |
| 94 bounds_checker, key_validate_params, err); | |
| 95 if (retval != ValidationError::NONE) | |
| 96 return retval; | |
| 97 | |
| 98 if (!ValidateEncodedPointer(&object->values.offset)) { | |
| 99 MOJO_INTERNAL_DEBUG_SET_ERROR_MSG(err) << ""; | |
| 100 return ValidationError::ILLEGAL_POINTER; | |
| 101 } | |
| 102 | |
| 103 if (!object->values.offset) { | |
| 104 MOJO_INTERNAL_DEBUG_SET_ERROR_MSG(err) | |
| 105 << "null value array in map struct"; | |
| 106 return ValidationError::UNEXPECTED_NULL_POINTER; | |
| 107 } | |
| 108 | |
| 109 retval = | |
| 110 Array_Data<Value>::Validate(DecodePointerRaw(&object->values.offset), | |
| 111 bounds_checker, value_validate_params, err); | |
| 112 if (retval != ValidationError::NONE) | |
| 113 return retval; | |
| 114 | |
| 115 const ArrayHeader* key_header = | |
| 116 static_cast<const ArrayHeader*>(DecodePointerRaw(&object->keys.offset)); | |
| 117 const ArrayHeader* value_header = static_cast<const ArrayHeader*>( | |
| 118 DecodePointerRaw(&object->values.offset)); | |
| 119 if (key_header->num_elements != value_header->num_elements) { | |
| 120 MOJO_INTERNAL_DEBUG_SET_ERROR_MSG(err) << ""; | |
| 121 return ValidationError::DIFFERENT_SIZED_ARRAYS_IN_MAP; | |
| 122 } | |
| 123 | |
| 124 return ValidationError::NONE; | |
| 125 } | |
| 126 | |
| 127 StructHeader header_; | |
| 128 | |
| 129 ArrayPointer<Key> keys; | |
| 130 ArrayPointer<Value> values; | |
| 131 | |
| 132 void EncodePointersAndHandles(std::vector<mojo::Handle>* handles) { | |
| 133 Encode(&keys, handles); | |
| 134 Encode(&values, handles); | |
| 135 } | |
| 136 | |
| 137 void DecodePointersAndHandles(std::vector<mojo::Handle>* handles) { | |
| 138 Decode(&keys, handles); | |
| 139 Decode(&values, handles); | |
| 140 } | |
| 141 | |
| 142 private: | |
| 143 Map_Data() { | |
| 144 header_.num_bytes = sizeof(*this); | |
| 145 header_.version = 0; | |
| 146 } | |
| 147 ~Map_Data() = delete; | |
| 148 }; | |
| 149 static_assert(sizeof(Map_Data<char, char>) == 24, "Bad sizeof(Map_Data)"); | |
| 150 | |
| 151 } // namespace internal | |
| 152 } // namespace mojo | |
| 153 | |
| 154 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_ | |
| OLD | NEW |