Index: mojo/public/cpp/bindings/lib/native_serialization.h |
diff --git a/mojo/public/cpp/bindings/lib/native_serialization.h b/mojo/public/cpp/bindings/lib/native_serialization.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2322afa42dcae0656c2307f6aa4466c884d099da |
--- /dev/null |
+++ b/mojo/public/cpp/bindings/lib/native_serialization.h |
@@ -0,0 +1,96 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_ |
+#define MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_ |
+ |
+#include <stdint.h> |
+ |
+#include <limits> |
+ |
+#include "base/logging.h" |
+#include "base/pickle.h" |
+#include "ipc/ipc_param_traits.h" |
+#include "mojo/public/cpp/bindings/lib/array_internal.h" |
+#include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
+#include "mojo/public/cpp/bindings/lib/bindings_serialization.h" |
+#include "mojo/public/cpp/bindings/lib/pickle_buffer.h" |
+ |
+namespace mojo { |
+namespace internal { |
+ |
+template <typename T> |
+size_t GetSerializedSizeNative_(const T& value) { |
+ return IPC::ParamTraits<T>::GetSize(value); |
+} |
+ |
+template <typename T> |
+void SerializeNative_(const T& value, |
+ Buffer* buffer, |
+ Array_Data<uint8_t>** out) { |
+ PickleBuffer* pickler = buffer->AsPickleBuffer(); |
+ DCHECK(pickler) << "Native types can only be used with PickleBuffers."; |
+ |
+ ArrayHeader* header = |
+ reinterpret_cast<ArrayHeader*>(buffer->Allocate(sizeof(ArrayHeader))); |
+ header->num_bytes = sizeof(ArrayHeader); |
yzshen1
2015/12/15 21:20:26
header->num_bytes should be the total size of the
Ken Rockot(use gerrit already)
2015/12/15 23:31:07
Oops, I was confused by this and at some point con
|
+ |
+ // Remember where the Pickle started before writing. |
+ const char* data_start = pickler->end_of_payload(); |
+ |
+ IPC::ParamTraits<T>::Write(pickler, value); |
+ |
+ // Fix up the ArrayHeader so that num_elements contains the length of the |
+ // pickled data. |
+ size_t pickled_size = pickler->end_of_payload() - data_start; |
+ DCHECK_LT(pickled_size, std::numeric_limits<uint32_t>::max()); |
+ header->num_elements = static_cast<uint32_t>(pickled_size); |
+ |
+ *out = reinterpret_cast<Array_Data<uint8_t>*>(header); |
+} |
+ |
+template <typename T> |
+bool DeserializeNative_(Array_Data<uint8_t>* data, |
yzshen1
2015/12/15 21:20:26
Please handle the case |data| is nullptr, in that
Ken Rockot(use gerrit already)
2015/12/15 23:31:07
Done, sort of. I'd rather not force native types t
yzshen1
2015/12/15 23:55:29
Okay. The other deserialization routines don't mak
|
+ T* out, |
+ SerializationContext* context) { |
+ // Construct a temporary base::Pickle view over the array data. Note that |
+ // the Array_Data is laid out like this: |
+ // |
+ // [header_size (4 bytes)] [element_count (4 bytes)] [elements...] |
+ // |
+ // and base::Pickle expects to view data like this: |
+ // |
+ // [payload_size (4 bytes)] [header bytes ...] [payload...] |
+ // |
+ // Conveniently we can get Pickle to do what we want by hacking up the first |
+ // 4 bytes so the element count is ignored as part of the header, and the |
+ // Pickle payload consists of only the ParamTraits-serialized bytes. |
+ ArrayHeader* header = reinterpret_cast<ArrayHeader*>(data); |
+ DCHECK_EQ(header->num_bytes, 8u); |
yzshen1
2015/12/15 21:20:26
Please see my comment on line 37.
Ken Rockot(use gerrit already)
2015/12/15 23:31:07
Fixed
|
+ |
+ // Write the number of elements to the Pickle header location. It will treat |
+ // this as the bytes size. |
+ header->num_bytes = header->num_elements; |
+ |
+ { |
+ // Construct a view over the full Array_Data, including our hacked up |
+ // header. Pickle will infer from this that the header is 8 bytes long, |
+ // and the payload will contain all of the pickled bytes. |
+ base::Pickle pickle_view(reinterpret_cast<const char*>(header), |
+ header->num_elements + 8); |
yzshen1
2015/12/15 21:20:26
Please use sizeof(ArrayHeader) instead of 8.
Ken Rockot(use gerrit already)
2015/12/15 23:31:07
Done
|
+ base::PickleIterator iter(pickle_view); |
+ if (!IPC::ParamTraits<T>::Read(&pickle_view, &iter, out)) { |
+ return false; |
+ } |
+ } |
+ |
+ // Just to be nice, we return the message buffer to its original state. |
+ header->num_bytes = sizeof(ArrayHeader); |
+ return true; |
+} |
+ |
+} // namespace internal |
+} // namespace mojo |
+ |
+#endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_ |