summaryrefslogtreecommitdiffstats
path: root/mojo/public/cpp/bindings/lib/native_serialization.h
blob: 52fe816bfd1ec943f63a0a0e020e2d8788e98c68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// 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 <stddef.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 {

// Generated bindings for native-only types will specialize this to |true|.
// It can be used as a signal (by e.g. the Array serializer) for when to use
// SerializeNative_ with a type.
template <typename E>
struct ShouldUseNativeSerializer { static const bool value = false; };

template <typename T>
size_t GetSerializedSizeNative_(const T& value) {
  base::PickleSizer sizer;
  IPC::ParamTraits<T>::GetSize(&sizer, value);
  return Align(sizer.payload_size() + sizeof(ArrayHeader));
}

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)));

  // Remember where the Pickle started before writing.
  base::Pickle* pickle = pickler->pickle();
  const char* data_start = pickle->end_of_payload();

#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
  const char* payload_base = pickle->payload();
  size_t size_before_write = pickle->payload_size();
#endif

  IPC::ParamTraits<T>::Write(pickle, value);

#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
  // Ensure the pickle buffer hasn't moved.
  DCHECK_EQ(payload_base, pickle->payload());
  // Explicitly validate that the value returned by GetSize() always equals the
  // number of bytes actually written by Write().
  DCHECK_GE(pickle->payload_size(), size_before_write);
  size_t bytes_written = pickle->payload_size() - size_before_write;
  DCHECK_EQ(Align(bytes_written + sizeof(ArrayHeader)),
            GetSerializedSizeNative_(value));
#endif

  // Fix up the ArrayHeader so that num_elements contains the length of the
  // pickled data.
  size_t pickled_size = pickle->end_of_payload() - data_start;
  size_t total_size = pickled_size + sizeof(ArrayHeader);
  DCHECK_LT(total_size, std::numeric_limits<uint32_t>::max());
  header->num_bytes = static_cast<uint32_t>(total_size);
  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,
                        T* out,
                        SerializationContext* context) {
  if (!data)
    return true;

  // Construct a temporary base::Pickle view over the array data. Note that
  // the Array_Data is laid out like this:
  //
  //   [num_bytes (4 bytes)] [num_elements (4 bytes)] [elements...]
  //
  // and base::Pickle expects to view data like this:
  //
  //   [payload_size (4 bytes)] [header bytes ...] [payload...]
  //
  // Because ArrayHeader's num_bytes includes the length of the header and
  // Pickle's payload_size does not, we need to adjust the stored value
  // momentarily so Pickle can view the data.
  ArrayHeader* header = reinterpret_cast<ArrayHeader*>(data);
  DCHECK_GE(header->num_bytes, sizeof(ArrayHeader));
  header->num_bytes -= sizeof(ArrayHeader);

  {
    // 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_bytes + sizeof(ArrayHeader));
    base::PickleIterator iter(pickle_view);
    if (!IPC::ParamTraits<T>::Read(&pickle_view, &iter, out))
      return false;
  }

  // Return the header to its original state.
  header->num_bytes += sizeof(ArrayHeader);

  return true;
}

}  // namespace internal
}  // namespace mojo

#endif  // MOJO_PUBLIC_CPP_BINDINGS_LIB_NATIVE_SERIALIZATION_H_