OLD | NEW |
| (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/cpp/bindings/message.h" | |
6 | |
7 #include <stdlib.h> | |
8 | |
9 #include <algorithm> | |
10 | |
11 #include "mojo/public/cpp/environment/logging.h" | |
12 | |
13 namespace mojo { | |
14 | |
15 Message::Message() : data_num_bytes_(0), data_(nullptr) { | |
16 } | |
17 | |
18 Message::~Message() { | |
19 free(data_); | |
20 | |
21 for (std::vector<Handle>::iterator it = handles_.begin(); | |
22 it != handles_.end(); | |
23 ++it) { | |
24 if (it->is_valid()) | |
25 CloseRaw(*it); | |
26 } | |
27 } | |
28 | |
29 void Message::AllocUninitializedData(uint32_t num_bytes) { | |
30 MOJO_DCHECK(!data_); | |
31 data_num_bytes_ = num_bytes; | |
32 data_ = static_cast<internal::MessageData*>(malloc(num_bytes)); | |
33 } | |
34 | |
35 void Message::AdoptData(uint32_t num_bytes, internal::MessageData* data) { | |
36 MOJO_DCHECK(!data_); | |
37 data_num_bytes_ = num_bytes; | |
38 data_ = data; | |
39 } | |
40 | |
41 void Message::Swap(Message* other) { | |
42 std::swap(data_num_bytes_, other->data_num_bytes_); | |
43 std::swap(data_, other->data_); | |
44 std::swap(handles_, other->handles_); | |
45 } | |
46 | |
47 MojoResult ReadAndDispatchMessage(MessagePipeHandle handle, | |
48 MessageReceiver* receiver, | |
49 bool* receiver_result) { | |
50 MojoResult rv; | |
51 | |
52 uint32_t num_bytes = 0, num_handles = 0; | |
53 rv = ReadMessageRaw(handle, | |
54 nullptr, | |
55 &num_bytes, | |
56 nullptr, | |
57 &num_handles, | |
58 MOJO_READ_MESSAGE_FLAG_NONE); | |
59 if (rv != MOJO_RESULT_RESOURCE_EXHAUSTED) | |
60 return rv; | |
61 | |
62 Message message; | |
63 message.AllocUninitializedData(num_bytes); | |
64 message.mutable_handles()->resize(num_handles); | |
65 | |
66 rv = ReadMessageRaw( | |
67 handle, | |
68 message.mutable_data(), | |
69 &num_bytes, | |
70 message.mutable_handles()->empty() | |
71 ? nullptr | |
72 : reinterpret_cast<MojoHandle*>(&message.mutable_handles()->front()), | |
73 &num_handles, | |
74 MOJO_READ_MESSAGE_FLAG_NONE); | |
75 if (receiver && rv == MOJO_RESULT_OK) | |
76 *receiver_result = receiver->Accept(&message); | |
77 | |
78 return rv; | |
79 } | |
80 | |
81 } // namespace mojo | |
OLD | NEW |