Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 {%- import "struct_macros.tmpl" as struct_macros %} | |
| 2 {%- set class_name = struct.name ~ "_Data" %} | 1 {%- set class_name = struct.name ~ "_Data" %} |
| 3 | 2 |
| 3 {#- TODO(yzshen): Consider eliminating _validate_object() and | |
| 4 _validate_handle(). #} | |
| 5 | |
| 6 {#- Validates the specified struct field, which is supposed to be an object | |
|
viettrungluu
2015/02/13 00:51:34
At least it's all in one file now, which already m
yzshen1
2015/02/17 22:17:08
I am thinking about converting the code into C++ t
viettrungluu
2015/02/17 22:32:50
A C++ template is one definite possibility, though
| |
| 7 (struct/array/string/map/union). | |
| 8 This macro is expanded by the Validate() method. #} | |
| 9 {%- macro _validate_object(struct, packed_field) %} | |
| 10 {%- set name = packed_field.field.name %} | |
| 11 {%- set kind = packed_field.field.kind %} | |
| 12 {%- set wrapper_type = kind|cpp_wrapper_type %} | |
| 13 {%- if not kind|is_nullable_kind %} | |
| 14 if (!object->{{name}}.offset) { | |
| 15 ReportValidationError( | |
| 16 mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, | |
| 17 "null {{name}} field in {{struct.name}} struct"); | |
|
viettrungluu
2015/02/13 00:51:34
E.g., one might imagine turning this into "null %s
yzshen1
2015/02/17 22:17:08
Yeah, this makes good sense. I will make such a ch
| |
| 18 return false; | |
| 19 } | |
| 20 {%- endif %} | |
| 21 if (!mojo::internal::ValidateEncodedPointer(&object->{{name}}.offset)) { | |
| 22 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER); | |
| 23 return false; | |
| 24 } | |
| 25 {%- if kind|is_array_kind or kind|is_string_kind %} | |
| 26 if (!{{wrapper_type}}::Data_::Validate< | |
| 27 {{kind|get_array_validate_params|indent(10)}}>( | |
| 28 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), | |
| 29 bounds_checker)) { | |
| 30 {%- elif kind|is_map_kind %} | |
| 31 if (!{{wrapper_type}}::Data_::Validate< | |
| 32 {{kind.value_kind|get_map_validate_params|indent(10)}}>( | |
| 33 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), | |
| 34 bounds_checker)) { | |
| 35 {%- elif kind|is_struct_kind %} | |
| 36 if (!{{kind|get_name_for_kind}}::Data_::Validate( | |
| 37 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), | |
| 38 bounds_checker)) { | |
| 39 {%- else %} | |
| 40 if (!{{wrapper_type}}::Data_::Validate( | |
| 41 mojo::internal::DecodePointerRaw(&object->{{name}}.offset), | |
| 42 bounds_checker)) { | |
| 43 {%- endif %} | |
| 44 return false; | |
| 45 } | |
| 46 {%- endmacro %} | |
| 47 | |
| 48 {#- Validates the specified struct field, which is supposed to be a handle. | |
| 49 This macro is expanded by the Validate() method. #} | |
| 50 {%- macro _validate_handle(struct, packed_field) %} | |
| 51 {%- set name = packed_field.field.name %} | |
| 52 {%- set kind = packed_field.field.kind %} | |
| 53 {%- if not kind|is_nullable_kind %} | |
| 54 if (object->{{name}}.value() == mojo::internal::kEncodedInvalidHandleValue) { | |
| 55 ReportValidationError( | |
| 56 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, | |
| 57 "invalid {{name}} field in {{struct.name}} struct"); | |
| 58 return false; | |
| 59 } | |
| 60 {%- endif %} | |
| 61 if (!bounds_checker->ClaimHandle(object->{{name}})) { | |
| 62 ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_HANDLE); | |
| 63 return false; | |
| 64 } | |
| 65 {%- endmacro %} | |
| 66 | |
| 4 // static | 67 // static |
| 5 {{class_name}}* {{class_name}}::New(mojo::internal::Buffer* buf) { | 68 {{class_name}}* {{class_name}}::New(mojo::internal::Buffer* buf) { |
| 6 return new (buf->Allocate(sizeof({{class_name}}))) {{class_name}}(); | 69 return new (buf->Allocate(sizeof({{class_name}}))) {{class_name}}(); |
| 7 } | 70 } |
| 8 | 71 |
| 9 // static | 72 // static |
| 10 bool {{class_name}}::Validate(const void* data, | 73 bool {{class_name}}::Validate(const void* data, |
| 11 mojo::internal::BoundsChecker* bounds_checker) { | 74 mojo::internal::BoundsChecker* bounds_checker) { |
| 12 {{struct_macros.validate(struct)}} | 75 if (!data) |
|
viettrungluu
2015/02/13 00:51:33
Personally, I find this much more readable already
yzshen1
2015/02/17 22:17:08
Yay!
| |
| 76 return true; | |
| 77 | |
| 78 if (!ValidateStructHeaderAndClaimMemory(data, bounds_checker)) | |
| 79 return false; | |
| 80 | |
| 81 // NOTE: The memory backing |object| may be smaller than |sizeof(*object)| if | |
| 82 // the message comes from an older version. | |
| 83 const {{class_name}}* object = static_cast<const {{class_name}}*>(data); | |
| 84 | |
| 85 static const uint32_t kVersionSizePairs[{{struct.versions|length}}][2] = { | |
|
viettrungluu
2015/02/13 00:51:33
Readability note (not to be addressed in this CL):
yzshen1
2015/02/17 22:17:08
That makes sense.
I will make it in a separate CL,
| |
| 86 {%- for version in struct.versions -%} | |
| 87 { {{version.version}}, {{version.num_bytes}} }{% if not loop.last %}, {% end if -%} | |
| 88 {%- endfor -%} | |
| 89 }; | |
| 90 | |
| 91 if (object->header_.version <= {{struct.versions[-1].version}}) { | |
| 92 for (size_t i = 0; i < {{struct.versions|length}}; ++i) { | |
|
viettrungluu
2015/02/13 00:51:34
Side comment (not to be addressed in this CL):
*
yzshen1
2015/02/17 22:17:08
Yeah. That makes sense. Thanks! I will make the ch
| |
| 93 if (object->header_.version <= kVersionSizePairs[i][0]) { | |
| 94 if (object->header_.num_bytes == kVersionSizePairs[i][1]) | |
| 95 break; | |
| 96 | |
| 97 ReportValidationError( | |
| 98 mojo::internal::VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER); | |
| 99 return false; | |
| 100 } | |
| 101 } | |
| 102 } else if (object->header_.num_bytes < {{struct.versions[-1].num_bytes}}) { | |
| 103 ReportValidationError( | |
| 104 mojo::internal::VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER); | |
| 105 return false; | |
| 106 } | |
| 107 | |
| 108 {#- Before validating fields introduced at a certain version, we need to add | |
| 109 a version check, which makes sure we skip further validation if |object| | |
| 110 is from an earlier version. |last_checked_version| records the last | |
| 111 version that we have added such version check. #} | |
| 112 {%- set last_checked_version = 0 %} | |
| 113 {%- for packed_field in struct.packed.packed_fields_in_ordinal_order %} | |
| 114 {%- set kind = packed_field.field.kind %} | |
| 115 {%- if kind|is_object_kind or kind|is_any_handle_kind %} | |
| 116 {%- if packed_field.min_version > last_checked_version %} | |
| 117 {%- set last_checked_version = packed_field.min_version %} | |
| 118 if (object->header_.version < {{packed_field.min_version}}) | |
| 119 return true; | |
| 120 {%- endif %} | |
| 121 {%- if kind|is_object_kind %} | |
| 122 {{_validate_object(struct, packed_field)}} | |
| 123 {%- elif kind|is_any_handle_kind %} | |
| 124 {{_validate_handle(struct, packed_field)}} | |
| 125 {%- endif %} | |
| 126 {%- endif %} | |
| 127 {%- endfor %} | |
| 128 | |
| 129 return true; | |
| 130 } | |
| 131 | |
| 132 void {{class_name}}::EncodePointersAndHandles( | |
| 133 std::vector<mojo::Handle>* handles) { | |
| 134 MOJO_CHECK(header_.version == {{struct.versions[-1].version}}); | |
| 135 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} | |
| 136 {%- if pf.field.kind|is_object_kind %} | |
| 137 mojo::internal::Encode(&{{pf.field.name}}, handles); | |
| 138 {%- elif pf.field.kind|is_any_handle_kind %} | |
| 139 mojo::internal::EncodeHandle(&{{pf.field.name}}, handles); | |
| 140 {%- endif %} | |
| 141 {%- endfor %} | |
| 142 } | |
| 143 | |
| 144 void {{class_name}}::DecodePointersAndHandles( | |
| 145 std::vector<mojo::Handle>* handles) { | |
| 146 // NOTE: The memory backing |this| may has be smaller than |sizeof(*this)|, if | |
| 147 // the message comes from an older version. | |
| 148 {#- Before decoding fields introduced at a certain version, we need to add | |
| 149 a version check, which makes sure we skip further decoding if |this| | |
| 150 is from an earlier version. |last_checked_version| records the last | |
| 151 version that we have added such version check. #} | |
| 152 {%- set last_checked_version = 0 %} | |
| 153 {%- for pf in struct.packed.packed_fields_in_ordinal_order %} | |
| 154 {%- set name = pf.field.name %} | |
| 155 {%- set kind = pf.field.kind %} | |
| 156 {%- if kind|is_object_kind or kind|is_any_handle_kind %} | |
| 157 {%- if pf.min_version > last_checked_version %} | |
| 158 {%- set last_checked_version = pf.min_version %} | |
| 159 if (header_.version < {{pf.min_version}}) | |
| 160 return; | |
| 161 {%- endif %} | |
| 162 {%- if kind|is_object_kind %} | |
| 163 mojo::internal::Decode(&{{name}}, handles); | |
| 164 {%- else %} | |
| 165 mojo::internal::DecodeHandle(&{{name}}, handles); | |
| 166 {%- endif %} | |
| 167 {%- endif %} | |
| 168 {%- endfor %} | |
| 13 } | 169 } |
| 14 | 170 |
| 15 {{class_name}}::{{class_name}}() { | 171 {{class_name}}::{{class_name}}() { |
| 16 header_.num_bytes = sizeof(*this); | 172 header_.num_bytes = sizeof(*this); |
| 17 header_.version = {{struct.versions[-1].version}}; | 173 header_.version = {{struct.versions[-1].version}}; |
| 18 } | 174 } |
| 19 | |
| 20 void {{class_name}}::EncodePointersAndHandles( | |
| 21 std::vector<mojo::Handle>* handles) { | |
| 22 {{struct_macros.encodes(struct)}} | |
| 23 } | |
| 24 | |
| 25 void {{class_name}}::DecodePointersAndHandles( | |
| 26 std::vector<mojo::Handle>* handles) { | |
| 27 {{struct_macros.decodes(struct)}} | |
| 28 } | |
| OLD | NEW |