Chromium Code Reviews| 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 | |
|
viettrungluu
2014/10/08 16:21:56
nit: extra blank line
| |
| 32 // Map serializes into a struct which has two arrays as struct fields, the keys | |
| 33 // and the values. | |
| 34 template <typename First, typename Second> | |
| 35 class Map_Data { | |
| 36 public: | |
| 37 static Map_Data* New(mojo::internal::Buffer* buf) { | |
| 38 return new (buf->Allocate(sizeof(Map_Data))) Map_Data(); | |
| 39 } | |
| 40 | |
| 41 template <typename ValueParams> | |
| 42 static bool Validate(const void* data, | |
| 43 mojo::internal::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* MOJO_ALLOW_UNUSED object = | |
| 51 static_cast<const Map_Data*>(data); | |
| 52 if (!mojo::internal::ValidateEncodedPointer(&object->first.offset)) { | |
| 53 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER); | |
| 54 return false; | |
| 55 } | |
| 56 if (!object->first.offset) { | |
| 57 ReportValidationError( | |
| 58 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | |
| 59 "null key array in map struct"); | |
| 60 return false; | |
| 61 } | |
| 62 if (!internal::Array_Data<First>::template Validate< | |
| 63 MapDataKeyValidateParams<First> > ( | |
| 64 mojo::internal::DecodePointerRaw(&object->first.offset), | |
| 65 bounds_checker)) { | |
| 66 return false; | |
| 67 } | |
| 68 | |
| 69 if (!mojo::internal::ValidateEncodedPointer(&object->second.offset)) { | |
| 70 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER); | |
| 71 return false; | |
| 72 } | |
| 73 if (!object->second.offset) { | |
| 74 ReportValidationError( | |
| 75 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | |
| 76 "null value array in map struct"); | |
| 77 return false; | |
| 78 } | |
| 79 if (!internal::Array_Data<Second>::template Validate<ValueParams>( | |
| 80 mojo::internal::DecodePointerRaw(&object->second.offset), | |
| 81 bounds_checker)) { | |
| 82 return false; | |
| 83 } | |
| 84 | |
| 85 const ArrayHeader* first_header = static_cast<const ArrayHeader*>( | |
| 86 mojo::internal::DecodePointerRaw(&object->first.offset)); | |
| 87 const ArrayHeader* second_header = static_cast<const ArrayHeader*>( | |
| 88 mojo::internal::DecodePointerRaw(&object->second.offset)); | |
| 89 if (first_header->num_elements != second_header->num_elements) { | |
| 90 ReportValidationError( | |
| 91 mojo::internal::VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP); | |
| 92 return false; | |
| 93 } | |
| 94 | |
| 95 return true; | |
| 96 } | |
| 97 | |
| 98 mojo::internal::StructHeader header_; | |
| 99 | |
| 100 mojo::internal::ArrayPointer<First> first; | |
| 101 mojo::internal::ArrayPointer<Second> second; | |
| 102 | |
| 103 void EncodePointersAndHandles(std::vector<mojo::Handle>* handles) { | |
| 104 mojo::internal::Encode(&first, handles); | |
| 105 mojo::internal::Encode(&second, handles); | |
| 106 } | |
| 107 | |
| 108 void DecodePointersAndHandles(std::vector<mojo::Handle>* handles) { | |
| 109 mojo::internal::Decode(&first, handles); | |
| 110 mojo::internal::Decode(&second, handles); | |
| 111 } | |
| 112 | |
| 113 private: | |
| 114 Map_Data() { | |
| 115 header_.num_bytes = sizeof(*this); | |
| 116 header_.num_fields = 2; | |
| 117 } | |
| 118 ~Map_Data() = delete; | |
| 119 }; | |
| 120 | |
| 121 } // namespace internal | |
| 122 } // namespace mojo | |
| 123 | |
| 124 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_ | |
| OLD | NEW |