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

Unified Diff: mojo/public/cpp/bindings/lib/serialization.h

Issue 2076313002: Mojo C++ bindings: expose serialization public API for mojo structs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/struct_ptr.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/cpp/bindings/lib/serialization.h
diff --git a/mojo/public/cpp/bindings/lib/serialization.h b/mojo/public/cpp/bindings/lib/serialization.h
index 1cda65235c7107b0733c272e1f1dff6e07891a5f..b079c18da031aa55dd09bab6706379b95d0d1629 100644
--- a/mojo/public/cpp/bindings/lib/serialization.h
+++ b/mojo/public/cpp/bindings/lib/serialization.h
@@ -5,14 +5,18 @@
#ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_
#define MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_
+#include <string.h>
+
#include "mojo/public/cpp/bindings/array_traits_carray.h"
#include "mojo/public/cpp/bindings/array_traits_standard.h"
#include "mojo/public/cpp/bindings/array_traits_stl.h"
#include "mojo/public/cpp/bindings/lib/array_serialization.h"
+#include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
#include "mojo/public/cpp/bindings/lib/map_serialization.h"
#include "mojo/public/cpp/bindings/lib/native_enum_serialization.h"
#include "mojo/public/cpp/bindings/lib/native_struct_serialization.h"
#include "mojo/public/cpp/bindings/lib/string_serialization.h"
+#include "mojo/public/cpp/bindings/lib/template_util.h"
#include "mojo/public/cpp/bindings/map_traits_standard.h"
#include "mojo/public/cpp/bindings/map_traits_stl.h"
#include "mojo/public/cpp/bindings/string_traits_standard.h"
@@ -20,4 +24,89 @@
#include "mojo/public/cpp/bindings/string_traits_string16.h"
#include "mojo/public/cpp/bindings/string_traits_string_piece.h"
+namespace mojo {
+namespace internal {
+
+template <typename MojomType, typename DataArrayType, typename UserType>
+DataArrayType StructSerializeImpl(UserType* input) {
+ static_assert(IsSpecializationOf<StructPtr, MojomType>::value ||
+ IsSpecializationOf<InlinedStructPtr, MojomType>::value,
+ "Unexpected type.");
+
+ SerializationContext context;
+ size_t size = PrepareToSerialize<MojomType>(*input, &context);
+ DCHECK_EQ(size, Align(size));
+
+ DataArrayType result(size);
+ if (size == 0)
+ return result;
+
+ void* result_buffer = &result.front();
+ // The serialization logic requires that the buffer is 8-byte aligned. If the
+ // result buffer is not properly aligned, we have to do an extra copy. In
+ // practice, this should never happen for mojo::Array (backed by std::vector).
+ bool need_copy = !IsAligned(result_buffer);
+
+ if (need_copy) {
+ // calloc sets the memory to all zero.
+ result_buffer = calloc(size, 1);
+ DCHECK(IsAligned(result_buffer));
+ }
+
+ FixedBuffer buffer;
+ buffer.Initialize(result_buffer, size);
+ typename MojomType::Struct::Data_* data = nullptr;
+ Serialize<MojomType>(*input, &buffer, &data, &context);
+
+ data->EncodePointers();
+
+ if (need_copy) {
+ memcpy(&result.front(), result_buffer, size);
+ free(result_buffer);
+ }
+
+ return result;
+}
+
+template <typename MojomType, typename DataArrayType, typename UserType>
+bool StructDeserializeImpl(DataArrayType input, UserType* output) {
+ static_assert(IsSpecializationOf<StructPtr, MojomType>::value ||
+ IsSpecializationOf<InlinedStructPtr, MojomType>::value,
+ "Unexpected type.");
+ using DataType = typename MojomType::Struct::Data_;
+
+ if (input.is_null())
+ return false;
+
+ void* input_buffer = input.empty() ? nullptr : &input.front();
+
+ // Please see comments in StructSerializeImpl.
+ bool need_copy = !IsAligned(input_buffer);
+
+ if (need_copy) {
+ input_buffer = malloc(input.size());
+ DCHECK(IsAligned(input_buffer));
+ memcpy(input_buffer, &input.front(), input.size());
+ }
+
+ ValidationContext validation_context(input_buffer, input.size(), 0);
+ bool result = false;
+ if (DataType::Validate(input_buffer, &validation_context)) {
+ auto data = reinterpret_cast<DataType*>(input_buffer);
+ if (data)
+ data->DecodePointers();
+
+ SerializationContext context;
+ result = Deserialize<MojomType>(data, output, &context);
+ }
+
+ if (need_copy)
+ free(input_buffer);
+
+ return result;
+}
+
+} // namespace internal
+} // namespace mojo
+
#endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_H_
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/struct_ptr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698