1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "ArgumentCoder.h"
29#include "Attachment.h"
30#include "StringReference.h"
31#include <wtf/EnumTraits.h>
32#include <wtf/Vector.h>
33
34namespace IPC {
35
36class DataReference;
37
38class Encoder final {
39 WTF_MAKE_FAST_ALLOCATED;
40public:
41 Encoder(StringReference messageReceiverName, StringReference messageName, uint64_t destinationID);
42 ~Encoder();
43
44 StringReference messageReceiverName() const { return m_messageReceiverName; }
45 StringReference messageName() const { return m_messageName; }
46 uint64_t destinationID() const { return m_destinationID; }
47
48 void setIsSyncMessage(bool);
49 bool isSyncMessage() const;
50
51 void setShouldDispatchMessageWhenWaitingForSyncReply(bool);
52 bool shouldDispatchMessageWhenWaitingForSyncReply() const;
53
54 void setFullySynchronousModeForTesting();
55
56 void wrapForTesting(std::unique_ptr<Encoder>);
57
58 void encodeFixedLengthData(const uint8_t*, size_t, unsigned alignment);
59 void encodeVariableLengthByteArray(const DataReference&);
60
61 template<typename T> void encodeEnum(T t)
62 {
63 COMPILE_ASSERT(sizeof(T) <= sizeof(uint64_t), enum_type_must_not_be_larger_than_64_bits);
64
65 encode(static_cast<uint64_t>(t));
66 }
67
68 template<typename T, std::enable_if_t<!std::is_enum<typename std::remove_const_t<std::remove_reference_t<T>>>::value>* = nullptr>
69 void encode(T&& t)
70 {
71 ArgumentCoder<typename std::remove_const<typename std::remove_reference<T>::type>::type>::encode(*this, std::forward<T>(t));
72 }
73
74 template<typename T, std::enable_if_t<std::is_enum<T>::value>* = nullptr>
75 Encoder& operator<<(T&& t)
76 {
77 encode(static_cast<uint64_t>(t));
78 return *this;
79 }
80
81 template<typename T, std::enable_if_t<!std::is_enum<T>::value>* = nullptr>
82 Encoder& operator<<(T&& t)
83 {
84 encode(std::forward<T>(t));
85 return *this;
86 }
87
88 uint8_t* buffer() const { return m_buffer; }
89 size_t bufferSize() const { return m_bufferSize; }
90
91 void addAttachment(Attachment&&);
92 Vector<Attachment> releaseAttachments();
93 void reserve(size_t);
94
95 static const bool isIPCEncoder = true;
96
97 void encode(uint64_t);
98
99private:
100 uint8_t* grow(unsigned alignment, size_t);
101
102 void encode(bool);
103 void encode(uint8_t);
104 void encode(uint16_t);
105 void encode(uint32_t);
106 void encode(int16_t);
107 void encode(int32_t);
108 void encode(int64_t);
109 void encode(float);
110 void encode(double);
111
112 template<typename E>
113 auto encode(E value) -> std::enable_if_t<std::is_enum<E>::value>
114 {
115 static_assert(sizeof(E) <= sizeof(uint64_t), "Enum type must not be larger than 64 bits.");
116
117 ASSERT(isValidEnum<E>(static_cast<uint64_t>(value)));
118 encode(static_cast<uint64_t>(value));
119 }
120
121 void encodeHeader();
122
123 StringReference m_messageReceiverName;
124 StringReference m_messageName;
125 uint64_t m_destinationID;
126
127 uint8_t m_inlineBuffer[512];
128
129 uint8_t* m_buffer;
130 uint8_t* m_bufferPointer;
131
132 size_t m_bufferSize;
133 size_t m_bufferCapacity;
134
135 Vector<Attachment> m_attachments;
136};
137
138} // namespace IPC
139