1 // Copyright 2016 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/edk/system/ports_message.h"
6
7 #include "base/memory/ptr_util.h"
8 #include "mojo/edk/system/node_channel.h"
9
10 namespace mojo {
11 namespace edk {
12
13 // static
NewUserMessage(size_t num_payload_bytes,size_t num_ports,size_t num_handles)14 std::unique_ptr<PortsMessage> PortsMessage::NewUserMessage(
15 size_t num_payload_bytes,
16 size_t num_ports,
17 size_t num_handles) {
18 return base::WrapUnique(
19 new PortsMessage(num_payload_bytes, num_ports, num_handles));
20 }
21
~PortsMessage()22 PortsMessage::~PortsMessage() {}
23
PortsMessage(size_t num_payload_bytes,size_t num_ports,size_t num_handles)24 PortsMessage::PortsMessage(size_t num_payload_bytes,
25 size_t num_ports,
26 size_t num_handles)
27 : ports::Message(num_payload_bytes, num_ports) {
28 size_t size = num_header_bytes_ + num_ports_bytes_ + num_payload_bytes;
29 void* ptr;
30 channel_message_ = NodeChannel::CreatePortsMessage(size, &ptr, num_handles);
31 InitializeUserMessageHeader(ptr);
32 }
33
PortsMessage(size_t num_header_bytes,size_t num_payload_bytes,size_t num_ports_bytes,Channel::MessagePtr channel_message)34 PortsMessage::PortsMessage(size_t num_header_bytes,
35 size_t num_payload_bytes,
36 size_t num_ports_bytes,
37 Channel::MessagePtr channel_message)
38 : ports::Message(num_header_bytes,
39 num_payload_bytes,
40 num_ports_bytes) {
41 if (channel_message) {
42 channel_message_ = std::move(channel_message);
43 void* data;
44 size_t num_data_bytes;
45 NodeChannel::GetPortsMessageData(channel_message_.get(), &data,
46 &num_data_bytes);
47 start_ = static_cast<char*>(data);
48 } else {
49 // TODO: Clean this up. In practice this branch of the constructor should
50 // only be reached from Node-internal calls to AllocMessage, which never
51 // carry ports or non-header bytes.
52 CHECK_EQ(num_payload_bytes, 0u);
53 CHECK_EQ(num_ports_bytes, 0u);
54 void* ptr;
55 channel_message_ =
56 NodeChannel::CreatePortsMessage(num_header_bytes, &ptr, 0);
57 start_ = static_cast<char*>(ptr);
58 }
59 }
60
61 } // namespace edk
62 } // namespace mojo
63