OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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/ipc/message/ipc_message_builder.h" | |
6 | |
7 #include <assert.h> | |
8 #include <stdlib.h> | |
9 #include <string.h> | |
10 #include <limits> | |
11 | |
12 #include "mojo/ipc/message/ipc_message.h" | |
13 | |
14 namespace mojo { | |
15 namespace { | |
16 | |
17 // Aligns 'value' by rounding it up to the next multiple of 'alignment' | |
18 uint32_t AlignInt(uint32_t value, uint32_t alignment) { | |
19 return value + (alignment - (value % alignment)) % alignment; | |
20 } | |
21 | |
22 } // namespace | |
23 | |
24 IPCMessageBuilder::IPCMessageBuilder() { | |
25 } | |
26 | |
27 IPCMessageBuilder::~IPCMessageBuilder() { | |
28 } | |
29 | |
30 bool IPCMessageBuilder::Initialize(uint32_t type) { | |
31 uint32_t message_header[2] = { | |
32 type, | |
33 0 // Length in bytes to be filled out in Finish. | |
34 }; | |
35 return AppendRawBytes(&message_header, sizeof(message_header)); | |
36 } | |
37 | |
38 bool IPCMessageBuilder::Append(uint16_t name, int8_t value) { | |
39 return AppendU32(name, kIPCMessageFieldType_Int8, | |
40 static_cast<uint32_t>(value)); | |
41 } | |
42 | |
43 bool IPCMessageBuilder::Append(uint16_t name, int16_t value) { | |
44 return AppendU32(name, kIPCMessageFieldType_Int16, | |
45 static_cast<uint32_t>(value)); | |
46 } | |
47 | |
48 bool IPCMessageBuilder::Append(uint16_t name, int32_t value) { | |
49 return AppendU32(name, kIPCMessageFieldType_Int32, | |
50 static_cast<uint32_t>(value)); | |
51 } | |
52 | |
53 bool IPCMessageBuilder::Append(uint16_t name, int64_t value) { | |
54 return AppendU64(name, kIPCMessageFieldType_Int64, | |
55 static_cast<uint64_t>(value)); | |
56 } | |
57 | |
58 bool IPCMessageBuilder::Append(uint16_t name, uint8_t value) { | |
59 return AppendU32(name, kIPCMessageFieldType_Uint8, | |
60 static_cast<uint32_t>(value)); | |
61 } | |
62 | |
63 bool IPCMessageBuilder::Append(uint16_t name, uint16_t value) { | |
64 return AppendU32(name, kIPCMessageFieldType_Uint16, | |
65 static_cast<uint32_t>(value)); | |
66 } | |
67 | |
68 bool IPCMessageBuilder::Append(uint16_t name, uint32_t value) { | |
69 return AppendU32(name, kIPCMessageFieldType_Uint32, value); | |
70 } | |
71 | |
72 bool IPCMessageBuilder::Append(uint16_t name, uint64_t value) { | |
73 return AppendU64(name, kIPCMessageFieldType_Uint32, value); | |
74 } | |
75 | |
76 bool IPCMessageBuilder::Append(uint16_t name, const std::string& value) { | |
77 return AppendVariableLength(name, kIPCMessageFieldType_String, value.data(), | |
78 value.size()); | |
79 } | |
80 | |
81 bool IPCMessageBuilder::Append(uint16_t name, const void* bytes, | |
82 size_t num_bytes) { | |
83 return AppendVariableLength(name, kIPCMessageFieldType_Bytes, bytes, | |
84 num_bytes); | |
85 } | |
86 | |
87 bool IPCMessageBuilder::Append(uint16_t name, Handle handle) { | |
88 if (!AppendU32(name, kIPCMessageFieldType_Handle, handle.value)) | |
vtl
2013/09/11 17:37:16
handle.value isn't going to be a universal (cross-
| |
89 return false; | |
90 | |
91 handles_.push_back(handle); | |
92 return true; | |
93 } | |
94 | |
95 bool IPCMessageBuilder::Append(uint16_t name, const IPCMessage& message) { | |
96 if (!AppendVariableLength(name, kIPCMessageFieldType_Message, | |
97 &message.data()[0], message.data().size())) | |
98 return false; | |
99 | |
100 size_t size_before = handles_.size(); | |
101 handles_.insert(handles_.end(), | |
102 message.handles().begin(), | |
103 message.handles().end()); | |
104 | |
105 // TODO(darin): Should we even worry about memory allocation failure? | |
106 return handles_.size() == size_before + message.handles().size(); | |
107 } | |
108 | |
109 bool IPCMessageBuilder::Finish(IPCMessage* message) { | |
110 if (data_.empty()) // Initialize was not called! | |
111 return false; | |
112 | |
113 uint32_t payload_size = data_.size() - 2U*sizeof(uint32_t); | |
114 assert(payload_size < data_.size()); | |
115 | |
116 // Stamp payload size into the message. | |
117 reinterpret_cast<uint32_t*>(&data_[0])[1] = payload_size; | |
118 | |
119 message->data_.swap(data_); | |
120 message->handles_.swap(handles_); | |
121 return true; | |
122 } | |
123 | |
124 bool IPCMessageBuilder::AppendU32(uint16_t name, IPCMessageFieldType field_type, | |
125 uint32_t value) { | |
126 return AppendHeader(name, field_type) && | |
127 AppendRawBytes(&value, sizeof(value)); | |
128 } | |
129 | |
130 bool IPCMessageBuilder::AppendU64(uint16_t name, IPCMessageFieldType field_type, | |
131 uint64_t value) { | |
132 return AppendHeader(name, field_type) && | |
133 AppendRawBytes(&value, sizeof(value)); | |
134 } | |
135 | |
136 bool IPCMessageBuilder::AppendVariableLength(uint16_t name, | |
137 IPCMessageFieldType field_type, | |
138 const void* bytes, | |
139 size_t num_bytes) { | |
140 if (num_bytes > std::numeric_limits<uint32_t>::max()) | |
141 return false; | |
142 | |
143 uint32_t size = static_cast<uint32_t>(num_bytes); | |
144 uint32_t aligned_size = AlignInt(size, 4U); | |
145 | |
146 const uint8_t kNulls[4] = { 0 }; | |
147 | |
148 return AppendHeader(name, field_type) && | |
149 AppendRawBytes(&size, sizeof(size)) && | |
150 AppendRawBytes(bytes, size) && | |
151 AppendRawBytes(kNulls, aligned_size - size); | |
152 } | |
153 | |
154 bool IPCMessageBuilder::AppendHeader(uint16_t name, | |
155 IPCMessageFieldType field_type) { | |
156 uint32_t header = static_cast<uint32_t>(name) << 16 | | |
157 static_cast<uint32_t>(field_type); | |
158 return AppendRawBytes(&header, sizeof(header)); | |
159 } | |
160 | |
161 bool IPCMessageBuilder::AppendRawBytes(const void* bytes, size_t num_bytes) { | |
162 if (!num_bytes) | |
163 return true; | |
164 | |
165 const uint8_t* data_start = static_cast<const uint8_t*>(bytes); | |
166 const uint8_t* data_end = data_start + num_bytes; | |
167 | |
168 size_t size_before = data_.size(); | |
169 | |
170 // TODO(darin): Call reserve() here? | |
171 data_.insert(data_.end(), data_start, data_end); | |
172 | |
173 // TODO(darin): Should we even worry about memory allocation failure? | |
174 return data_.size() == size_before + num_bytes; | |
175 } | |
176 | |
177 } // namespace mojo | |
OLD | NEW |