Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: mojo/public/cpp/bindings/lib/validate_params.h

Issue 2036623002: Validate map keys in C++ Mojo bindings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATE_PARAMS_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATE_PARAMS_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATE_PARAMS_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATE_PARAMS_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
11 11
12 namespace mojo { 12 namespace mojo {
13 namespace internal { 13 namespace internal {
14 14
15 using ValidateEnumFunc = bool (*)(int32_t); 15 using ValidateEnumFunc = bool (*)(int32_t);
16 16
17 // TODO(tibell): Rename to ContainerValidateParams.
17 class ArrayValidateParams { 18 class ArrayValidateParams {
18 public: 19 public:
20 // Validate a map. A map is validated as a pair of arrays, one for the keys
yzshen1 2016/06/02 19:52:28 style nit: Validate -> Validates. Because the sent
tibell 2016/06/02 23:55:01 Done (Python has the opposite convention.)
21 // and one for the values. Both arguments must be non-null.
22 //
23 // ArrayValidateParams takes ownership of |in_key_validate params| and
24 // |in_element_validate params|.
25 ArrayValidateParams(ArrayValidateParams* in_key_validate_params,
26 ArrayValidateParams* in_element_validate_params)
27 : expected_num_elements(0),
yzshen1 2016/06/02 19:52:28 Nit: Please consider doing initialization at their
tibell 2016/06/02 23:55:02 Done.
28 element_is_nullable(false),
29 key_validate_params(in_key_validate_params),
30 element_validate_params(in_element_validate_params),
31 validate_enum_func(nullptr) {
32 DCHECK(in_key_validate_params)
33 << "Map validate params require key validate params";
34 DCHECK(in_element_validate_params)
35 << "Map validate params require element validate params";
36 }
37
38 // Validate an array.
39 //
19 // ArrayValidateParams takes ownership of |in_element_validate params|. 40 // ArrayValidateParams takes ownership of |in_element_validate params|.
20 ArrayValidateParams(uint32_t in_expected_num_elements, 41 ArrayValidateParams(uint32_t in_expected_num_elements,
21 bool in_element_is_nullable, 42 bool in_element_is_nullable,
22 ArrayValidateParams* in_element_validate_params) 43 ArrayValidateParams* in_element_validate_params)
23 : expected_num_elements(in_expected_num_elements), 44 : expected_num_elements(in_expected_num_elements),
24 element_is_nullable(in_element_is_nullable), 45 element_is_nullable(in_element_is_nullable),
46 key_validate_params(nullptr),
25 element_validate_params(in_element_validate_params), 47 element_validate_params(in_element_validate_params),
26 validate_enum_func(nullptr) {} 48 validate_enum_func(nullptr) {}
27 49
28 // Validates an array of enums. 50 // Validates an array of enums.
29 ArrayValidateParams(uint32_t in_expected_num_elements, 51 ArrayValidateParams(uint32_t in_expected_num_elements,
30 ValidateEnumFunc in_validate_enum_func) 52 ValidateEnumFunc in_validate_enum_func)
31 : expected_num_elements(in_expected_num_elements), 53 : expected_num_elements(in_expected_num_elements),
32 element_is_nullable(false), 54 element_is_nullable(false),
55 key_validate_params(nullptr),
33 element_validate_params(nullptr), 56 element_validate_params(nullptr),
34 validate_enum_func(in_validate_enum_func) {} 57 validate_enum_func(in_validate_enum_func) {}
35 58
36 ~ArrayValidateParams() { 59 ~ArrayValidateParams() {
37 if (element_validate_params) 60 if (element_validate_params)
38 delete element_validate_params; 61 delete element_validate_params;
62 if (key_validate_params)
63 delete key_validate_params;
39 } 64 }
40 65
41 // If |expected_num_elements| is not 0, the array is expected to have exactly 66 // If |expected_num_elements| is not 0, the array is expected to have exactly
42 // that number of elements. 67 // that number of elements.
43 uint32_t expected_num_elements; 68 uint32_t expected_num_elements;
44 69
45 // Whether the elements are nullable. 70 // Whether the elements are nullable.
46 bool element_is_nullable; 71 bool element_is_nullable;
47 72
73 // Validation information for map key. It is either a pointer to another
74 // instance of ArrayValidateParams (if keys are arrays or maps), or nullptr.
yzshen1 2016/06/02 19:52:28 This comment needs to be revised: the param is for
tibell 2016/06/02 23:55:01 Done.
75 ArrayValidateParams* key_validate_params;
76
48 // Validation information for elements. It is either a pointer to another 77 // Validation information for elements. It is either a pointer to another
yzshen1 2016/06/02 19:59:41 It seems this comment needs to be updated, too. In
tibell 2016/06/02 23:55:01 Done.
49 // instance of ArrayValidateParams (if elements are arrays or maps), or 78 // instance of ArrayValidateParams (if elements are arrays or maps), or
50 // nullptr. In the case of maps, this is used to validate the value array. 79 // nullptr. In the case of maps, this is used to validate the value array.
51 ArrayValidateParams* element_validate_params; 80 ArrayValidateParams* element_validate_params;
52 81
53 // Validation function for enum elements. 82 // Validation function for enum elements.
54 ValidateEnumFunc validate_enum_func; 83 ValidateEnumFunc validate_enum_func;
55 84
56 private: 85 private:
57 DISALLOW_COPY_AND_ASSIGN(ArrayValidateParams); 86 DISALLOW_COPY_AND_ASSIGN(ArrayValidateParams);
58 }; 87 };
59 88
60 } // namespace internal 89 } // namespace internal
61 } // namespace mojo 90 } // namespace mojo
62 91
63 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATE_PARAMS_H_ 92 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_VALIDATE_PARAMS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698