• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "mojo/public/cpp/bindings/lib/native_struct_serialization.h"
6 
7 #include "mojo/public/cpp/bindings/lib/serialization.h"
8 
9 namespace mojo {
10 namespace internal {
11 
12 // static
PrepareToSerialize(const NativeStructPtr & input,SerializationContext * context)13 size_t UnmappedNativeStructSerializerImpl::PrepareToSerialize(
14     const NativeStructPtr& input,
15     SerializationContext* context) {
16   if (!input)
17     return 0;
18   return internal::PrepareToSerialize<ArrayDataView<uint8_t>>(input->data,
19                                                               context);
20 }
21 
22 // static
Serialize(const NativeStructPtr & input,Buffer * buffer,NativeStruct_Data ** output,SerializationContext * context)23 void UnmappedNativeStructSerializerImpl::Serialize(
24     const NativeStructPtr& input,
25     Buffer* buffer,
26     NativeStruct_Data** output,
27     SerializationContext* context) {
28   if (!input) {
29     *output = nullptr;
30     return;
31   }
32 
33   Array_Data<uint8_t>* data = nullptr;
34   const ContainerValidateParams params(0, false, nullptr);
35   internal::Serialize<ArrayDataView<uint8_t>>(input->data, buffer, &data,
36                                               &params, context);
37   *output = reinterpret_cast<NativeStruct_Data*>(data);
38 }
39 
40 // static
Deserialize(NativeStruct_Data * input,NativeStructPtr * output,SerializationContext * context)41 bool UnmappedNativeStructSerializerImpl::Deserialize(
42     NativeStruct_Data* input,
43     NativeStructPtr* output,
44     SerializationContext* context) {
45   Array_Data<uint8_t>* data = reinterpret_cast<Array_Data<uint8_t>*>(input);
46 
47   NativeStructPtr result(NativeStruct::New());
48   if (!internal::Deserialize<ArrayDataView<uint8_t>>(data, &result->data,
49                                                      context)) {
50     output = nullptr;
51     return false;
52   }
53   if (!result->data)
54     *output = nullptr;
55   else
56     result.Swap(output);
57   return true;
58 }
59 
60 }  // namespace internal
61 }  // namespace mojo
62