Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: mojo/public/libs/message/message_builder.cc

Issue 23439010: Remove now defunct MessageBuilder class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 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/public/libs/message/message_builder.h"
6
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include <limits>
11
12 #include "base/logging.h"
13 #include "mojo/public/libs/message/message.h"
14
15 namespace mojo {
16 namespace {
17
18 const size_t kAlignment = 8U;
19 const size_t kMessageHeaderSize = 8U;
20
21 } // namespace
22
23 MessageBuilder::MessageBuilder() {
24 }
25
26 MessageBuilder::~MessageBuilder() {
27 }
28
29 bool MessageBuilder::Initialize(uint32_t message_name) {
30 uint32_t message_header[2] = {
31 0, // Length in bytes to be filled out in Finish.
32 message_name
33 };
34 COMPILE_ASSERT(kMessageHeaderSize == sizeof(message_header),
35 unexpected_message_header_size);
36
37 return AppendRawBytes(&message_header, sizeof(message_header));
38 }
39
40 bool MessageBuilder::Append(uint16_t name, int8_t value) {
41 return AppendU32(name, kMessageFieldType_Int8, static_cast<uint8_t>(value));
42 }
43
44 bool MessageBuilder::Append(uint16_t name, int16_t value) {
45 return AppendU32(name, kMessageFieldType_Int16, static_cast<uint16_t>(value));
46 }
47
48 bool MessageBuilder::Append(uint16_t name, int32_t value) {
49 return AppendU32(name, kMessageFieldType_Int32, static_cast<uint32_t>(value));
50 }
51
52 bool MessageBuilder::Append(uint16_t name, int64_t value) {
53 return AppendU64(name, kMessageFieldType_Int64, static_cast<uint64_t>(value));
54 }
55
56 bool MessageBuilder::Append(uint16_t name, uint8_t value) {
57 return AppendU32(name, kMessageFieldType_Uint8, value);
58 }
59
60 bool MessageBuilder::Append(uint16_t name, uint16_t value) {
61 return AppendU32(name, kMessageFieldType_Uint16, value);
62 }
63
64 bool MessageBuilder::Append(uint16_t name, uint32_t value) {
65 return AppendU32(name, kMessageFieldType_Uint32, value);
66 }
67
68 bool MessageBuilder::Append(uint16_t name, uint64_t value) {
69 return AppendU64(name, kMessageFieldType_Uint64, value);
70 }
71
72 bool MessageBuilder::Append(uint16_t name, const std::string& value) {
73 return AppendArray(name, kMessageFieldType_String, value.data(),
74 value.size());
75 }
76
77 bool MessageBuilder::Append(uint16_t name, const void* bytes,
78 size_t num_bytes) {
79 return AppendArray(name, kMessageFieldType_Bytes, bytes, num_bytes);
80 }
81
82 bool MessageBuilder::Append(uint16_t name, Handle handle) {
83 uint32_t index = static_cast<uint32_t>(handles_.size());
84
85 if (!AppendU32(name, kMessageFieldType_Handle, index))
86 return false;
87
88 handles_.push_back(handle);
89
90 // TODO(darin): Should we even worry about memory allocation failure?
91 return handles_.size() == index + 1;
92 }
93
94 bool MessageBuilder::Append(uint16_t name, const Message& message) {
95 if (!AppendArray(name, kMessageFieldType_Message, &message.data()[0],
96 message.data().size()))
97 return false;
98
99 size_t size_before = handles_.size();
100 handles_.insert(handles_.end(),
101 message.handles().begin(),
102 message.handles().end());
103
104 // TODO(darin): Should we even worry about memory allocation failure?
105 return handles_.size() == size_before + message.handles().size();
106 }
107
108 bool MessageBuilder::Finish(Message* message) {
109 if (data_.empty())
110 return false;
111
112 // Initialize should have been called.
113 DCHECK(data_.size() >= kMessageHeaderSize);
114
115 uint32_t payload_size =
116 static_cast<uint32_t>(data_.size() - sizeof(uint32_t));
117
118 // Stamp payload size into the message.
119 memcpy(&data_[0], &payload_size, sizeof(payload_size));
120
121 message->data_.swap(data_);
122 message->handles_.swap(handles_);
123 return true;
124 }
125
126 bool MessageBuilder::AppendU32(uint16_t name, MessageFieldType field_type,
127 uint32_t value) {
128 return AppendHeader(name, field_type) &&
129 AppendRawBytes(&value, sizeof(value));
130 }
131
132 bool MessageBuilder::AppendU64(uint16_t name, MessageFieldType field_type,
133 uint64_t value) {
134 // Insert padding to achieve 8-byte alignment.
135 return AppendHeader(name, field_type) &&
136 AppendPadding(4U) &&
137 AppendRawBytes(&value, sizeof(value));
138 }
139
140 bool MessageBuilder::AppendArray(uint16_t name, MessageFieldType field_type,
141 const void* bytes, size_t num_bytes) {
142 if (num_bytes > std::numeric_limits<uint32_t>::max())
143 return false;
144 uint32_t size = static_cast<uint32_t>(num_bytes);
145
146 // Append padding to achieve 8-byte alignment.
147 size_t padding_size = kAlignment - num_bytes % kAlignment;
148
149 return AppendHeader(name, field_type) &&
150 AppendRawBytes(&size, sizeof(size)) &&
151 AppendRawBytes(bytes, size) &&
152 AppendPadding(padding_size);
153 }
154
155 bool MessageBuilder::AppendHeader(uint16_t name, MessageFieldType field_type) {
156 // Initialize should have been called.
157 DCHECK(data_.size() >= kMessageHeaderSize);
158
159 uint32_t header = static_cast<uint32_t>(name) << 16 |
160 static_cast<uint32_t>(field_type);
161 return AppendRawBytes(&header, sizeof(header));
162 }
163
164 bool MessageBuilder::AppendPadding(size_t num_bytes) {
165 if (num_bytes == 0)
166 return true;
167 DCHECK(num_bytes < kAlignment);
168
169 const uint8_t kNulls[kAlignment - 1] = { 0 };
170 return AppendRawBytes(kNulls, num_bytes);
171 }
172
173 bool MessageBuilder::AppendRawBytes(const void* bytes, size_t num_bytes) {
174 if (!num_bytes)
175 return true;
176
177 const uint8_t* data_start = static_cast<const uint8_t*>(bytes);
178 const uint8_t* data_end = data_start + num_bytes;
179
180 size_t size_before = data_.size();
181
182 // TODO(darin): Call reserve() here?
183 data_.insert(data_.end(), data_start, data_end);
184
185 // TODO(darin): Should we even worry about memory allocation failure?
186 return data_.size() == size_before + num_bytes;
187 }
188
189 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/libs/message/message_builder.h ('k') | mojo/public/libs/message/message_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698