• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/macros.h"
9 #include "mojo/public/cpp/bindings/lib/array_internal.h"
10 #include "mojo/public/cpp/bindings/lib/validate_params.h"
11 #include "mojo/public/cpp/bindings/lib/validation_errors.h"
12 #include "mojo/public/cpp/bindings/lib/validation_util.h"
13 
14 namespace mojo {
15 namespace internal {
16 
17 // Map serializes into a struct which has two arrays as struct fields, the keys
18 // and the values.
19 template <typename Key, typename Value>
20 class Map_Data {
21  public:
22   class BufferWriter {
23    public:
24     BufferWriter() = default;
25 
Allocate(Buffer * buffer)26     void Allocate(Buffer* buffer) {
27       buffer_ = buffer;
28       index_ = buffer_->Allocate(sizeof(Map_Data));
29       new (data()) Map_Data();
30     }
31 
is_null()32     bool is_null() const { return !buffer_; }
data()33     Map_Data* data() {
34       DCHECK(!is_null());
35       return buffer_->Get<Map_Data>(index_);
36     }
37     Map_Data* operator->() { return data(); }
38 
39    private:
40     Buffer* buffer_ = nullptr;
41     size_t index_ = 0;
42 
43     DISALLOW_COPY_AND_ASSIGN(BufferWriter);
44   };
45 
46   // |validate_params| must have non-null |key_validate_params| and
47   // |element_validate_params| members.
Validate(const void * data,ValidationContext * validation_context,const ContainerValidateParams * validate_params)48   static bool Validate(const void* data,
49                        ValidationContext* validation_context,
50                        const ContainerValidateParams* validate_params) {
51     if (!data)
52       return true;
53 
54     if (!ValidateStructHeaderAndClaimMemory(data, validation_context))
55       return false;
56 
57     const Map_Data* object = static_cast<const Map_Data*>(data);
58     if (object->header_.num_bytes != sizeof(Map_Data) ||
59         object->header_.version != 0) {
60       ReportValidationError(validation_context,
61                             VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER);
62       return false;
63     }
64 
65     if (!ValidatePointerNonNullable(object->keys, 0, validation_context) ||
66         !ValidateContainer(object->keys, validation_context,
67                            validate_params->key_validate_params)) {
68       return false;
69     }
70 
71     if (!ValidatePointerNonNullable(object->values, 1, validation_context) ||
72         !ValidateContainer(object->values, validation_context,
73                            validate_params->element_validate_params)) {
74       return false;
75     }
76 
77     if (object->keys.Get()->size() != object->values.Get()->size()) {
78       ReportValidationError(validation_context,
79                             VALIDATION_ERROR_DIFFERENT_SIZED_ARRAYS_IN_MAP);
80       return false;
81     }
82 
83     return true;
84   }
85 
86   StructHeader header_;
87 
88   Pointer<Array_Data<Key>> keys;
89   Pointer<Array_Data<Value>> values;
90 
91  private:
Map_Data()92   Map_Data() {
93     header_.num_bytes = sizeof(*this);
94     header_.version = 0;
95   }
96   ~Map_Data() = delete;
97 };
98 static_assert(sizeof(Map_Data<char, char>) == 24, "Bad sizeof(Map_Data)");
99 
100 }  // namespace internal
101 }  // namespace mojo
102 
103 #endif  // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_DATA_INTERNAL_H_
104