OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ipc/ipc_message_pipe_reader.h" | 5 #include "ipc/ipc_message_pipe_reader.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 sender_.reset(); | 79 sender_.reset(); |
80 if (binding_.is_bound()) | 80 if (binding_.is_bound()) |
81 binding_.Close(); | 81 binding_.Close(); |
82 } | 82 } |
83 | 83 |
84 bool MessagePipeReader::Send(std::unique_ptr<Message> message) { | 84 bool MessagePipeReader::Send(std::unique_ptr<Message> message) { |
85 TRACE_EVENT_WITH_FLOW0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), | 85 TRACE_EVENT_WITH_FLOW0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), |
86 "MessagePipeReader::Send", | 86 "MessagePipeReader::Send", |
87 message->flags(), | 87 message->flags(), |
88 TRACE_EVENT_FLAG_FLOW_OUT); | 88 TRACE_EVENT_FLAG_FLOW_OUT); |
89 base::Optional<std::vector<mojom::SerializedHandlePtr>> handles; | 89 mojo::Array<mojom::SerializedHandlePtr> handles(nullptr); |
90 MojoResult result = MOJO_RESULT_OK; | 90 MojoResult result = MOJO_RESULT_OK; |
91 result = ChannelMojo::ReadFromMessageAttachmentSet(message.get(), &handles); | 91 result = ChannelMojo::ReadFromMessageAttachmentSet(message.get(), &handles); |
92 if (result != MOJO_RESULT_OK) | 92 if (result != MOJO_RESULT_OK) |
93 return false; | 93 return false; |
94 | 94 |
95 std::vector<uint8_t> data(message->size()); | 95 mojo::Array<uint8_t> data(message->size()); |
96 std::copy(reinterpret_cast<const uint8_t*>(message->data()), | 96 std::copy(reinterpret_cast<const uint8_t*>(message->data()), |
97 reinterpret_cast<const uint8_t*>(message->data()) + message->size(), | 97 reinterpret_cast<const uint8_t*>(message->data()) + message->size(), |
98 data.data()); | 98 &data[0]); |
99 | 99 |
100 MessageSerializer serializer; | 100 MessageSerializer serializer; |
101 mojom::ChannelProxy proxy(&serializer); | 101 mojom::ChannelProxy proxy(&serializer); |
102 proxy.Receive(data, std::move(handles)); | 102 proxy.Receive(std::move(data), std::move(handles)); |
103 mojo::Message* mojo_message = serializer.message(); | 103 mojo::Message* mojo_message = serializer.message(); |
104 | 104 |
105 size_t num_handles = mojo_message->handles()->size(); | 105 size_t num_handles = mojo_message->handles()->size(); |
106 DCHECK_LE(num_handles, std::numeric_limits<uint32_t>::max()); | 106 DCHECK_LE(num_handles, std::numeric_limits<uint32_t>::max()); |
107 | 107 |
108 mojo_message->set_interface_id(sender_interface_id_); | 108 mojo_message->set_interface_id(sender_interface_id_); |
109 result = mojo::WriteMessageNew(sender_pipe_, mojo_message->TakeMojoMessage(), | 109 result = mojo::WriteMessageNew(sender_pipe_, mojo_message->TakeMojoMessage(), |
110 MOJO_WRITE_MESSAGE_FLAG_NONE); | 110 MOJO_WRITE_MESSAGE_FLAG_NONE); |
111 | 111 |
112 DVLOG(4) << "Send " << message->type() << ": " << message->size(); | 112 DVLOG(4) << "Send " << message->type() << ": " << message->size(); |
113 return result == MOJO_RESULT_OK; | 113 return result == MOJO_RESULT_OK; |
114 } | 114 } |
115 | 115 |
116 void MessagePipeReader::GetRemoteInterface( | 116 void MessagePipeReader::GetRemoteInterface( |
117 const std::string& name, | 117 const std::string& name, |
118 mojo::ScopedInterfaceEndpointHandle handle) { | 118 mojo::ScopedInterfaceEndpointHandle handle) { |
119 mojom::GenericInterfaceAssociatedRequest request; | 119 mojom::GenericInterfaceAssociatedRequest request; |
120 request.Bind(std::move(handle)); | 120 request.Bind(std::move(handle)); |
121 sender_->GetAssociatedInterface(name, std::move(request)); | 121 sender_->GetAssociatedInterface(name, std::move(request)); |
122 } | 122 } |
123 | 123 |
124 void MessagePipeReader::SetPeerPid(int32_t peer_pid) { | 124 void MessagePipeReader::SetPeerPid(int32_t peer_pid) { |
125 peer_pid_ = peer_pid; | 125 peer_pid_ = peer_pid; |
126 delegate_->OnPeerPidReceived(); | 126 delegate_->OnPeerPidReceived(); |
127 } | 127 } |
128 | 128 |
129 void MessagePipeReader::Receive( | 129 void MessagePipeReader::Receive( |
130 const std::vector<uint8_t>& data, | 130 mojo::Array<uint8_t> data, |
131 base::Optional<std::vector<mojom::SerializedHandlePtr>> handles) { | 131 mojo::Array<mojom::SerializedHandlePtr> handles) { |
132 DCHECK_NE(peer_pid_, base::kNullProcessId); | 132 DCHECK_NE(peer_pid_, base::kNullProcessId); |
133 Message message( | 133 Message message( |
134 data.empty() ? "" : reinterpret_cast<const char*>(data.data()), | 134 data.size() == 0 ? "" : reinterpret_cast<const char*>(&data[0]), |
135 static_cast<uint32_t>(data.size())); | 135 static_cast<uint32_t>(data.size())); |
136 message.set_sender_pid(peer_pid_); | 136 message.set_sender_pid(peer_pid_); |
137 | 137 |
138 DVLOG(4) << "Receive " << message.type() << ": " << message.size(); | 138 DVLOG(4) << "Receive " << message.type() << ": " << message.size(); |
139 MojoResult write_result = | 139 MojoResult write_result = |
140 ChannelMojo::WriteToMessageAttachmentSet(std::move(handles), &message); | 140 ChannelMojo::WriteToMessageAttachmentSet(std::move(handles), &message); |
141 if (write_result != MOJO_RESULT_OK) { | 141 if (write_result != MOJO_RESULT_OK) { |
142 OnPipeError(write_result); | 142 OnPipeError(write_result); |
143 return; | 143 return; |
144 } | 144 } |
145 | 145 |
146 TRACE_EVENT_WITH_FLOW0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), | 146 TRACE_EVENT_WITH_FLOW0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), |
147 "MessagePipeReader::Receive", | 147 "MessagePipeReader::Receive", |
148 message.flags(), | 148 message.flags(), |
149 TRACE_EVENT_FLAG_FLOW_IN); | 149 TRACE_EVENT_FLAG_FLOW_IN); |
150 delegate_->OnMessageReceived(message); | 150 delegate_->OnMessageReceived(message); |
151 } | 151 } |
152 | 152 |
153 void MessagePipeReader::GetAssociatedInterface( | 153 void MessagePipeReader::GetAssociatedInterface( |
154 const std::string& name, | 154 const mojo::String& name, |
155 mojom::GenericInterfaceAssociatedRequest request) { | 155 mojom::GenericInterfaceAssociatedRequest request) { |
156 DCHECK(thread_checker_.CalledOnValidThread()); | 156 DCHECK(thread_checker_.CalledOnValidThread()); |
157 if (delegate_) | 157 if (delegate_) |
158 delegate_->OnAssociatedInterfaceRequest(name, request.PassHandle()); | 158 delegate_->OnAssociatedInterfaceRequest(name, request.PassHandle()); |
159 } | 159 } |
160 | 160 |
161 void MessagePipeReader::OnPipeError(MojoResult error) { | 161 void MessagePipeReader::OnPipeError(MojoResult error) { |
162 DCHECK(thread_checker_.CalledOnValidThread()); | 162 DCHECK(thread_checker_.CalledOnValidThread()); |
163 | 163 |
164 Close(); | 164 Close(); |
165 | 165 |
166 // NOTE: The delegate call below may delete |this|. | 166 // NOTE: The delegate call below may delete |this|. |
167 if (delegate_) | 167 if (delegate_) |
168 delegate_->OnPipeError(); | 168 delegate_->OnPipeError(); |
169 } | 169 } |
170 | 170 |
171 } // namespace internal | 171 } // namespace internal |
172 } // namespace IPC | 172 } // namespace IPC |
OLD | NEW |