Chromium Code Reviews| Index: mojo/public/cpp/bindings/lib/map_serialization.h |
| diff --git a/mojo/public/cpp/bindings/lib/map_serialization.h b/mojo/public/cpp/bindings/lib/map_serialization.h |
| index 8ef83b3a3a554947606e20eb0aa01beaa53d06a5..c317c2d61a92ae7e2ce1330f369ef4e3739aa346 100644 |
| --- a/mojo/public/cpp/bindings/lib/map_serialization.h |
| +++ b/mojo/public/cpp/bindings/lib/map_serialization.h |
| @@ -121,6 +121,11 @@ inline size_t GetSerializedSize_(const Map<MapKey, MapValue>& input) { |
| value_data_size; |
| } |
| +// SerializeMap_ will return VALIDATION_ERROR_NONE on success and sets |
|
viettrungluu
2015/10/09 01:35:19
"sets" -> "set"
vardhan
2015/10/09 22:31:28
Done.
|
| +// |output| accordingly. On failure, |input| will be partially serialized into |
| +// |output| up until an error occurs (which is propagated up and returned by |
| +// SerializeMap_), in which case |buf| is also partially consumed. |
| +// |
| // We don't need an ArrayValidateParams instance for key validation since |
| // we can deduce it from the Key type. (which can only be primitive types or |
| // non-nullable strings.) |
| @@ -128,45 +133,55 @@ template <typename MapKey, |
| typename MapValue, |
| typename DataKey, |
| typename DataValue> |
| -inline void SerializeMap_( |
| +inline internal::ValidationError SerializeMap_( |
| Map<MapKey, MapValue>* input, |
| internal::Buffer* buf, |
| internal::Map_Data<DataKey, DataValue>** output, |
| const internal::ArrayValidateParams* value_validate_params) { |
| - if (input && *input) { |
| - internal::Map_Data<DataKey, DataValue>* result = |
| - internal::Map_Data<DataKey, DataValue>::New(buf); |
| - internal::Array_Data<DataKey>* keys_data = |
| - internal::Array_Data<DataKey>::New(input->size(), buf); |
| + if (input->is_null()) { |
| + // |input| could be a nullable map, in which case |output| is serialized as |
| + // null, which is valid. |
| + *output = nullptr; |
| + return internal::VALIDATION_ERROR_NONE; |
| + } |
| + |
| + internal::Map_Data<DataKey, DataValue>* result = |
| + internal::Map_Data<DataKey, DataValue>::New(buf); |
| - if (result && keys_data) { |
| - result->keys.ptr = keys_data; |
| + // We *must* serialize the keys before we allocate an Array_Data for the |
| + // values. |
| + internal::Array_Data<DataKey>* keys_data = |
| + internal::Array_Data<DataKey>::New(input->size(), buf); |
| + result->keys.ptr = keys_data; |
| - // We *must* serialize the keys before we allocate an Array_Data for the |
| - // values. |
| - internal::MapKeyIterator<MapKey, MapValue> key_iter(input); |
| - const internal::ArrayValidateParams* key_validate_params = |
| - internal::MapKeyValidateParamsFactory<DataKey>::Get(); |
| + internal::MapKeyIterator<MapKey, MapValue> key_iter(input); |
| + const internal::ArrayValidateParams* key_validate_params = |
| + internal::MapKeyValidateParamsFactory<DataKey>::Get(); |
| + auto keys_retval = |
| internal::ArraySerializer<MapKey, DataKey>::SerializeElements( |
| key_iter.begin(), input->size(), buf, result->keys.ptr, |
| key_validate_params); |
| + if (keys_retval != internal::VALIDATION_ERROR_NONE) |
| + return keys_retval; |
| - // Now we try allocate an Array_Data for the values |
| - internal::Array_Data<DataValue>* values_data = |
| - internal::Array_Data<DataValue>::New(input->size(), buf); |
| - if (values_data) { |
| - result->values.ptr = values_data; |
| - internal::MapValueIterator<MapKey, MapValue> value_iter(input); |
| - internal::ArraySerializer<MapValue, DataValue>::SerializeElements( |
| - value_iter.begin(), input->size(), buf, result->values.ptr, |
| - value_validate_params); |
| - } |
| - } |
| - *output = result; |
| - } else { |
| - *output = nullptr; |
| - } |
| + // Now we try allocate an Array_Data for the values |
| + internal::Array_Data<DataValue>* values_data = |
| + internal::Array_Data<DataValue>::New(input->size(), buf); |
| + result->values.ptr = values_data; |
| + |
| + internal::MapValueIterator<MapKey, MapValue> value_iter(input); |
| + |
| + auto values_retval = |
| + internal::ArraySerializer<MapValue, DataValue>::SerializeElements( |
| + value_iter.begin(), input->size(), buf, result->values.ptr, |
| + value_validate_params); |
| + |
|
viettrungluu
2015/10/09 01:35:19
nit: delete this blank line
vardhan
2015/10/09 22:31:28
Done.
|
| + if (values_retval != internal::VALIDATION_ERROR_NONE) |
| + return values_retval; |
| + |
| + *output = result; |
| + return internal::VALIDATION_ERROR_NONE; |
| } |
| template <typename MapKey, |