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

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

Issue 1387993002: mojo::Serialize*_() calls now propogate/return validation errors. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Fixed MOJO_DCHECK issues, removed SerializationWarningObserver & addressing trung's CL comments Created 5 years, 2 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_MAP_SERIALIZATION_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_
7 7
8 #include "mojo/public/cpp/bindings/lib/array_internal.h" 8 #include "mojo/public/cpp/bindings/lib/array_internal.h"
9 #include "mojo/public/cpp/bindings/lib/array_serialization.h" 9 #include "mojo/public/cpp/bindings/lib/array_serialization.h"
10 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" 10 #include "mojo/public/cpp/bindings/lib/bindings_internal.h"
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 internal::MapSerializer<MapKey, DataKey>::GetItemSize(it.GetKey()); 114 internal::MapSerializer<MapKey, DataKey>::GetItemSize(it.GetKey());
115 value_data_size += 115 value_data_size +=
116 internal::MapSerializer<MapValue, DataValue>::GetItemSize( 116 internal::MapSerializer<MapValue, DataValue>::GetItemSize(
117 it.GetValue()); 117 it.GetValue());
118 } 118 }
119 119
120 return struct_overhead + key_base_size + key_data_size + value_base_size + 120 return struct_overhead + key_base_size + key_data_size + value_base_size +
121 value_data_size; 121 value_data_size;
122 } 122 }
123 123
124 // SerializeMap_ will return VALIDATION_ERROR_NONE on success and set
125 // |output| accordingly. On failure, |input| will be partially serialized into
126 // |output| up until an error occurs (which is propagated up and returned by
127 // SerializeMap_), in which case |buf| is also partially consumed.
128 //
124 // We don't need an ArrayValidateParams instance for key validation since 129 // We don't need an ArrayValidateParams instance for key validation since
125 // we can deduce it from the Key type. (which can only be primitive types or 130 // we can deduce it from the Key type. (which can only be primitive types or
126 // non-nullable strings.) 131 // non-nullable strings.)
127 template <typename MapKey, 132 template <typename MapKey,
128 typename MapValue, 133 typename MapValue,
129 typename DataKey, 134 typename DataKey,
130 typename DataValue> 135 typename DataValue>
131 inline void SerializeMap_( 136 inline internal::ValidationError SerializeMap_(
132 Map<MapKey, MapValue>* input, 137 Map<MapKey, MapValue>* input,
133 internal::Buffer* buf, 138 internal::Buffer* buf,
134 internal::Map_Data<DataKey, DataValue>** output, 139 internal::Map_Data<DataKey, DataValue>** output,
135 const internal::ArrayValidateParams* value_validate_params) { 140 const internal::ArrayValidateParams* value_validate_params) {
136 if (input && *input) { 141 if (input->is_null()) {
137 internal::Map_Data<DataKey, DataValue>* result = 142 // |input| could be a nullable map, in which case |output| is serialized as
138 internal::Map_Data<DataKey, DataValue>::New(buf); 143 // null, which is valid.
139 internal::Array_Data<DataKey>* keys_data = 144 *output = nullptr;
140 internal::Array_Data<DataKey>::New(input->size(), buf); 145 return internal::VALIDATION_ERROR_NONE;
146 }
141 147
142 if (result && keys_data) { 148 internal::Map_Data<DataKey, DataValue>* result =
143 result->keys.ptr = keys_data; 149 internal::Map_Data<DataKey, DataValue>::New(buf);
144 150
145 // We *must* serialize the keys before we allocate an Array_Data for the 151 // We *must* serialize the keys before we allocate an Array_Data for the
146 // values. 152 // values.
147 internal::MapKeyIterator<MapKey, MapValue> key_iter(input); 153 internal::Array_Data<DataKey>* keys_data =
148 const internal::ArrayValidateParams* key_validate_params = 154 internal::Array_Data<DataKey>::New(input->size(), buf);
149 internal::MapKeyValidateParamsFactory<DataKey>::Get(); 155 result->keys.ptr = keys_data;
150 156
157 internal::MapKeyIterator<MapKey, MapValue> key_iter(input);
158 const internal::ArrayValidateParams* key_validate_params =
159 internal::MapKeyValidateParamsFactory<DataKey>::Get();
160
161 auto keys_retval =
151 internal::ArraySerializer<MapKey, DataKey>::SerializeElements( 162 internal::ArraySerializer<MapKey, DataKey>::SerializeElements(
152 key_iter.begin(), input->size(), buf, result->keys.ptr, 163 key_iter.begin(), input->size(), buf, result->keys.ptr,
153 key_validate_params); 164 key_validate_params);
165 if (keys_retval != internal::VALIDATION_ERROR_NONE)
166 return keys_retval;
154 167
155 // Now we try allocate an Array_Data for the values 168 // Now we try allocate an Array_Data for the values
156 internal::Array_Data<DataValue>* values_data = 169 internal::Array_Data<DataValue>* values_data =
157 internal::Array_Data<DataValue>::New(input->size(), buf); 170 internal::Array_Data<DataValue>::New(input->size(), buf);
158 if (values_data) { 171 result->values.ptr = values_data;
159 result->values.ptr = values_data; 172
160 internal::MapValueIterator<MapKey, MapValue> value_iter(input); 173 internal::MapValueIterator<MapKey, MapValue> value_iter(input);
161 internal::ArraySerializer<MapValue, DataValue>::SerializeElements( 174
162 value_iter.begin(), input->size(), buf, result->values.ptr, 175 auto values_retval =
163 value_validate_params); 176 internal::ArraySerializer<MapValue, DataValue>::SerializeElements(
164 } 177 value_iter.begin(), input->size(), buf, result->values.ptr,
165 } 178 value_validate_params);
166 *output = result; 179 if (values_retval != internal::VALIDATION_ERROR_NONE)
167 } else { 180 return values_retval;
168 *output = nullptr; 181
169 } 182 *output = result;
183 return internal::VALIDATION_ERROR_NONE;
170 } 184 }
171 185
172 template <typename MapKey, 186 template <typename MapKey,
173 typename MapValue, 187 typename MapValue,
174 typename DataKey, 188 typename DataKey,
175 typename DataValue> 189 typename DataValue>
176 inline void Deserialize_(internal::Map_Data<DataKey, DataValue>* input, 190 inline void Deserialize_(internal::Map_Data<DataKey, DataValue>* input,
177 Map<MapKey, MapValue>* output) { 191 Map<MapKey, MapValue>* output) {
178 if (input) { 192 if (input) {
179 Array<MapKey> keys; 193 Array<MapKey> keys;
180 Array<MapValue> values; 194 Array<MapValue> values;
181 195
182 Deserialize_(input->keys.ptr, &keys); 196 Deserialize_(input->keys.ptr, &keys);
183 Deserialize_(input->values.ptr, &values); 197 Deserialize_(input->values.ptr, &values);
184 198
185 *output = Map<MapKey, MapValue>(keys.Pass(), values.Pass()); 199 *output = Map<MapKey, MapValue>(keys.Pass(), values.Pass());
186 } else { 200 } else {
187 output->reset(); 201 output->reset();
188 } 202 }
189 } 203 }
190 204
191 } // namespace mojo 205 } // namespace mojo
192 206
193 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ 207 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/control_message_proxy.cc ('k') | mojo/public/cpp/bindings/lib/map_serialization_forward.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698