summaryrefslogtreecommitdiffstats
path: root/ipc/glue/IPDLParamTraits.h
blob: 48649d92de96bceb10a037e0dd80161d6aa51434 (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_ipc_IPDLParamTraits_h
#define mozilla_ipc_IPDLParamTraits_h

#include "chrome/common/ipc_message_utils.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Variant.h"

#include "nsTArray.h"

#include <type_traits>

namespace mozilla {
namespace ipc {

class IProtocol;

//
// IPDLParamTraits was an extended version of ParamTraits. Unlike ParamTraits,
// IPDLParamTraits passes an additional IProtocol* argument to the
// write and read methods.
//
// Previously this was required for serializing and deserializing types which
// require knowledge of which protocol they're being sent over, however the
// actor is now available through `IPC::Message{Writer,Reader}::GetActor()` so
// the extra argument is no longer necessary. Please use `IPC::ParamTraits` in
// the future.
//
// Types which implement IPDLParamTraits are also supported by ParamTraits.
//
template <typename P>
struct IPDLParamTraits {};

//
// WriteIPDLParam and ReadIPDLParam are like IPC::WriteParam and IPC::ReadParam,
// however, they also accept a redundant extra actor argument.
//
// NOTE: WriteIPDLParam takes a universal reference, so that it can support
// whatever reference type is supported by the underlying ParamTraits::Write
// implementation.
//
template <typename P>
static MOZ_NEVER_INLINE void WriteIPDLParam(IPC::MessageWriter* aWriter,
                                            IProtocol* aActor, P&& aParam) {
  MOZ_ASSERT(aActor == aWriter->GetActor());
  IPC::WriteParam(aWriter, std::forward<P>(aParam));
}

template <typename P>
static MOZ_NEVER_INLINE bool ReadIPDLParam(IPC::MessageReader* aReader,
                                           IProtocol* aActor, P* aResult) {
  MOZ_ASSERT(aActor == aReader->GetActor());
  return IPC::ReadParam(aReader, aResult);
}

}  // namespace ipc
}  // namespace mozilla

#endif  // defined(mozilla_ipc_IPDLParamTraits_h)