Update V8 to version 3.29.78 (based on bleeding_edge revision r24051).
[chromium-blink-merge.git] / ipc / mojo / ipc_channel_mojo_readers.cc
blob7af96f8d7cfc8d6fc731aab91f2511fa1bfbd26a
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
3 // found in the LICENSE file.
5 #include "ipc/mojo/ipc_channel_mojo_readers.h"
7 #include "ipc/mojo/ipc_channel_mojo.h"
8 #include "mojo/embedder/embedder.h"
10 #if defined(OS_POSIX) && !defined(OS_NACL)
11 #include "ipc/file_descriptor_set_posix.h"
12 #endif
14 namespace IPC {
15 namespace internal {
17 namespace {
19 // TODO(morrita): This should be built using higher-level Mojo construct
20 // for clarity and extensibility.
21 class HelloMessage {
22 public:
23 static Pickle CreateRequest(int32 pid) {
24 Pickle request;
25 request.WriteString(kHelloRequestMagic);
26 request.WriteInt(pid);
27 return request;
30 static bool ReadRequest(Pickle& pickle, int32* pid) {
31 PickleIterator iter(pickle);
32 std::string hello;
33 if (!iter.ReadString(&hello)) {
34 DLOG(WARNING) << "Failed to Read magic string.";
35 return false;
38 if (hello != kHelloRequestMagic) {
39 DLOG(WARNING) << "Magic mismatch:" << hello;
40 return false;
43 int read_pid;
44 if (!iter.ReadInt(&read_pid)) {
45 DLOG(WARNING) << "Failed to Read PID.";
46 return false;
49 *pid = read_pid;
50 return true;
53 static Pickle CreateResponse(int32 pid) {
54 Pickle request;
55 request.WriteString(kHelloResponseMagic);
56 request.WriteInt(pid);
57 return request;
60 static bool ReadResponse(Pickle& pickle, int32* pid) {
61 PickleIterator iter(pickle);
62 std::string hello;
63 if (!iter.ReadString(&hello)) {
64 DLOG(WARNING) << "Failed to read magic string.";
65 return false;
68 if (hello != kHelloResponseMagic) {
69 DLOG(WARNING) << "Magic mismatch:" << hello;
70 return false;
73 int read_pid;
74 if (!iter.ReadInt(&read_pid)) {
75 DLOG(WARNING) << "Failed to read PID.";
76 return false;
79 *pid = read_pid;
80 return true;
83 private:
84 static const char* kHelloRequestMagic;
85 static const char* kHelloResponseMagic;
88 const char* HelloMessage::kHelloRequestMagic = "MREQ";
89 const char* HelloMessage::kHelloResponseMagic = "MRES";
91 } // namespace
93 //------------------------------------------------------------------------------
95 MessageReader::MessageReader(mojo::ScopedMessagePipeHandle pipe,
96 ChannelMojo* owner)
97 : internal::MessagePipeReader(pipe.Pass()), owner_(owner) {
100 void MessageReader::OnMessageReceived() {
101 Message message(data_buffer().empty() ? "" : &data_buffer()[0],
102 static_cast<uint32>(data_buffer().size()));
104 std::vector<MojoHandle> handle_buffer;
105 TakeHandleBuffer(&handle_buffer);
106 #if defined(OS_POSIX) && !defined(OS_NACL)
107 MojoResult write_result =
108 ChannelMojo::WriteToFileDescriptorSet(handle_buffer, &message);
109 if (write_result != MOJO_RESULT_OK) {
110 CloseWithError(write_result);
111 return;
113 #else
114 DCHECK(handle_buffer.empty());
115 #endif
117 message.TraceMessageEnd();
118 owner_->OnMessageReceived(message);
121 void MessageReader::OnPipeClosed() {
122 if (!owner_)
123 return;
124 owner_->OnPipeClosed(this);
125 owner_ = NULL;
128 void MessageReader::OnPipeError(MojoResult error) {
129 if (!owner_)
130 return;
131 owner_->OnPipeError(this);
134 bool MessageReader::Send(scoped_ptr<Message> message) {
135 DCHECK(IsValid());
137 message->TraceMessageBegin();
138 std::vector<MojoHandle> handles;
139 #if defined(OS_POSIX) && !defined(OS_NACL)
140 MojoResult read_result =
141 ChannelMojo::ReadFromFileDescriptorSet(*message, &handles);
142 if (read_result != MOJO_RESULT_OK) {
143 std::for_each(handles.begin(), handles.end(), &MojoClose);
144 CloseWithError(read_result);
145 return false;
147 #endif
148 MojoResult write_result =
149 MojoWriteMessage(handle(),
150 message->data(),
151 static_cast<uint32>(message->size()),
152 handles.empty() ? NULL : &handles[0],
153 static_cast<uint32>(handles.size()),
154 MOJO_WRITE_MESSAGE_FLAG_NONE);
155 if (MOJO_RESULT_OK != write_result) {
156 std::for_each(handles.begin(), handles.end(), &MojoClose);
157 CloseWithError(write_result);
158 return false;
161 return true;
164 //------------------------------------------------------------------------------
166 ControlReader::ControlReader(mojo::ScopedMessagePipeHandle pipe,
167 ChannelMojo* owner)
168 : internal::MessagePipeReader(pipe.Pass()), owner_(owner) {
171 void ControlReader::OnPipeClosed() {
172 if (!owner_)
173 return;
174 owner_->OnPipeClosed(this);
175 owner_ = NULL;
178 void ControlReader::OnPipeError(MojoResult error) {
179 if (!owner_)
180 return;
181 owner_->OnPipeError(this);
184 bool ControlReader::Connect() {
185 return true;
188 //------------------------------------------------------------------------------
190 ServerControlReader::ServerControlReader(mojo::ScopedMessagePipeHandle pipe,
191 ChannelMojo* owner)
192 : ControlReader(pipe.Pass(), owner) {
195 ServerControlReader::~ServerControlReader() {
198 bool ServerControlReader::Connect() {
199 MojoResult result = SendHelloRequest();
200 if (result != MOJO_RESULT_OK) {
201 CloseWithError(result);
202 return false;
205 return true;
208 MojoResult ServerControlReader::SendHelloRequest() {
209 DCHECK(IsValid());
210 DCHECK(!message_pipe_.is_valid());
212 mojo::ScopedMessagePipeHandle self;
213 mojo::ScopedMessagePipeHandle peer;
214 MojoResult create_result =
215 mojo::CreateMessagePipe(NULL, &message_pipe_, &peer);
216 if (MOJO_RESULT_OK != create_result) {
217 DLOG(WARNING) << "mojo::CreateMessagePipe failed: " << create_result;
218 return create_result;
221 MojoHandle peer_to_send = peer.get().value();
222 Pickle request = HelloMessage::CreateRequest(owner_->GetSelfPID());
223 MojoResult write_result =
224 MojoWriteMessage(handle(),
225 request.data(),
226 static_cast<uint32>(request.size()),
227 &peer_to_send,
229 MOJO_WRITE_MESSAGE_FLAG_NONE);
230 if (MOJO_RESULT_OK != write_result) {
231 DLOG(WARNING) << "Writing Hello request failed: " << create_result;
232 return write_result;
235 // |peer| is sent and no longer owned by |this|.
236 (void)peer.release();
237 return MOJO_RESULT_OK;
240 MojoResult ServerControlReader::RespondHelloResponse() {
241 Pickle request(data_buffer().empty() ? "" : &data_buffer()[0],
242 static_cast<uint32>(data_buffer().size()));
244 int32 read_pid = 0;
245 if (!HelloMessage::ReadResponse(request, &read_pid)) {
246 DLOG(ERROR) << "Failed to parse Hello response.";
247 return MOJO_RESULT_UNKNOWN;
250 base::ProcessId pid = static_cast<base::ProcessId>(read_pid);
251 owner_->set_peer_pid(pid);
252 owner_->OnConnected(message_pipe_.Pass());
253 return MOJO_RESULT_OK;
256 void ServerControlReader::OnMessageReceived() {
257 MojoResult result = RespondHelloResponse();
258 if (result != MOJO_RESULT_OK)
259 CloseWithError(result);
262 //------------------------------------------------------------------------------
264 ClientControlReader::ClientControlReader(mojo::ScopedMessagePipeHandle pipe,
265 ChannelMojo* owner)
266 : ControlReader(pipe.Pass(), owner) {
269 MojoResult ClientControlReader::RespondHelloRequest(
270 MojoHandle message_channel) {
271 DCHECK(IsValid());
273 mojo::ScopedMessagePipeHandle received_pipe(
274 (mojo::MessagePipeHandle(message_channel)));
276 int32 read_request = 0;
277 Pickle request(data_buffer().empty() ? "" : &data_buffer()[0],
278 static_cast<uint32>(data_buffer().size()));
279 if (!HelloMessage::ReadRequest(request, &read_request)) {
280 DLOG(ERROR) << "Hello request has wrong magic.";
281 return MOJO_RESULT_UNKNOWN;
284 base::ProcessId pid = read_request;
285 Pickle response = HelloMessage::CreateResponse(owner_->GetSelfPID());
286 MojoResult write_result =
287 MojoWriteMessage(handle(),
288 response.data(),
289 static_cast<uint32>(response.size()),
290 NULL,
292 MOJO_WRITE_MESSAGE_FLAG_NONE);
293 if (MOJO_RESULT_OK != write_result) {
294 DLOG(ERROR) << "Writing Hello response failed: " << write_result;
295 return write_result;
298 owner_->set_peer_pid(pid);
299 owner_->OnConnected(received_pipe.Pass());
300 return MOJO_RESULT_OK;
303 void ClientControlReader::OnMessageReceived() {
304 std::vector<MojoHandle> handle_buffer;
305 TakeHandleBuffer(&handle_buffer);
306 if (handle_buffer.size() != 1) {
307 DLOG(ERROR) << "Hello request doesn't contains required handle: "
308 << handle_buffer.size();
309 CloseWithError(MOJO_RESULT_UNKNOWN);
310 return;
313 MojoResult result = RespondHelloRequest(handle_buffer[0]);
314 if (result != MOJO_RESULT_OK) {
315 DLOG(ERROR) << "Failed to respond Hello request. Closing: " << result;
316 CloseWithError(result);
320 } // namespace internal
321 } // namespace IPC