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/cpp/bindings/lib/message_builder.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include "mojo/public/cpp/bindings/lib/serialization_util.h"
11 #include "mojo/public/cpp/bindings/message.h"
12
13 namespace mojo {
14 namespace internal {
15
16 template <typename Header>
Allocate(Buffer * buf,Header ** header)17 void Allocate(Buffer* buf, Header** header) {
18 *header = static_cast<Header*>(buf->Allocate(sizeof(Header)));
19 (*header)->num_bytes = sizeof(Header);
20 }
21
MessageBuilder(uint32_t name,size_t payload_size)22 MessageBuilder::MessageBuilder(uint32_t name, size_t payload_size) {
23 InitializeMessage(sizeof(MessageHeader) + payload_size);
24
25 MessageHeader* header;
26 Allocate(message_.buffer(), &header);
27 header->version = 0;
28 header->name = name;
29 }
30
~MessageBuilder()31 MessageBuilder::~MessageBuilder() {
32 }
33
MessageBuilder()34 MessageBuilder::MessageBuilder() {}
35
InitializeMessage(size_t size)36 void MessageBuilder::InitializeMessage(size_t size) {
37 message_.Initialize(static_cast<uint32_t>(Align(size)),
38 true /* zero_initialized */);
39 }
40
MessageWithRequestIDBuilder(uint32_t name,size_t payload_size,uint32_t flags,uint64_t request_id)41 MessageWithRequestIDBuilder::MessageWithRequestIDBuilder(uint32_t name,
42 size_t payload_size,
43 uint32_t flags,
44 uint64_t request_id) {
45 InitializeMessage(sizeof(MessageHeaderWithRequestID) + payload_size);
46
47 MessageHeaderWithRequestID* header;
48 Allocate(message_.buffer(), &header);
49 header->version = 1;
50 header->name = name;
51 header->flags = flags;
52 header->request_id = request_id;
53 }
54
55 } // namespace internal
56 } // namespace mojo
57