Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(382)

Side by Side Diff: ipc/mojo/ipc_channel_mojo.cc

Issue 382333002: Introduce ChannelMojo (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "ipc/mojo/ipc_channel_mojo.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/lazy_instance.h"
10 #include "ipc/ipc_listener.h"
11 #include "mojo/embedder/embedder.h"
12
13 #if defined(OS_POSIX) && !defined(OS_NACL)
14 #include "ipc/file_descriptor_set_posix.h"
15 #endif
16
17 namespace IPC {
18
19 namespace {
20
21 // IPC::Listener for bootstrap channels.
22 // It should never receive any message.
23 class NullListener : public Listener {
24 public:
25 virtual bool OnMessageReceived(const Message&) OVERRIDE {
26 NOTREACHED();
27 return false;
28 }
29
30 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE {
31 NOTREACHED();
32 }
33
34 virtual void OnChannelError() OVERRIDE {
35 NOTREACHED();
36 }
37
38 virtual void OnBadMessageReceived(const Message& message) OVERRIDE {
39 NOTREACHED();
40 }
41 };
42
43 base::LazyInstance<NullListener> g_null_listener = LAZY_INSTANCE_INITIALIZER;
44
45 class MojoChannelFactory : public ChannelFactory {
46 public:
47 MojoChannelFactory(
48 ChannelHandle channel_handle,
49 Channel::Mode mode,
50 scoped_refptr<base::TaskRunner> io_thread_task_runner)
51 : channel_handle_(channel_handle),
52 mode_(mode),
53 io_thread_task_runner_(io_thread_task_runner) {
54 }
55
56 virtual std::string GetName() const OVERRIDE {
57 return channel_handle_.name;
58 }
59
60 virtual scoped_ptr<Channel> BuildChannel(Listener* listener) OVERRIDE {
61 return ChannelMojo::Create(
62 channel_handle_,
63 mode_,
64 listener,
65 io_thread_task_runner_).PassAs<Channel>();
66 }
67
68 private:
69 ChannelHandle channel_handle_;
70 Channel::Mode mode_;
71 scoped_refptr<base::TaskRunner> io_thread_task_runner_;
72 };
73
74 mojo::embedder::PlatformHandle ToPlatformHandle(
75 const ChannelHandle& handle) {
76 #if defined(OS_POSIX) && !defined(OS_NACL)
77 return mojo::embedder::PlatformHandle(handle.socket.fd);
78 #elif defined(OS_WIN)
79 return mojo::embedder::PlatformHandle(handle.pipe.handle);
80 #else
81 #error "Unsupported Platform!"
82 #endif
83 }
84
85 //------------------------------------------------------------------------------
86
87 // TODO(morrita): This should be built using higher-level Mojo construct
88 // for clarity and extensibility.
89 class HelloMessage {
90 public:
91 static Pickle CreateRequest(int32 pid) {
92 Pickle request;
93 request.WriteString(kHelloRequestMagic);
94 request.WriteInt(pid);
95 return request;
96 }
97
98 static bool ReadRequest(Pickle& pickle, int32* pid) {
99 PickleIterator iter(pickle);
100 std::string hello;
101 if (!iter.ReadString(&hello)) {
102 DLOG(WARNING) << "Failed to Read magic string.";
103 return false;
104 }
105
106 if (hello != kHelloRequestMagic) {
107 DLOG(WARNING) << "Magic mismatch:" << hello;
108 return false;
109 }
110
111 int read_pid;
112 if (!iter.ReadInt(&read_pid)) {
113 DLOG(WARNING) << "Failed to Read PID.";
114 return false;
115 }
116
117 *pid = read_pid;
118 return true;
119 }
120
121 static Pickle CreateResponse(int32 pid) {
122 Pickle request;
123 request.WriteString(kHelloResponseMagic);
124 request.WriteInt(pid);
125 return request;
126 }
127
128 static bool ReadResponse(Pickle& pickle, int32* pid) {
129 PickleIterator iter(pickle);
130 std::string hello;
131 if (!iter.ReadString(&hello)) {
132 DLOG(WARNING) << "Failed to read magic string.";
133 return false;
134 }
135
136 if (hello != kHelloResponseMagic) {
137 DLOG(WARNING) << "Magic mismatch:" << hello;
138 return false;
139 }
140
141 int read_pid;
142 if (!iter.ReadInt(&read_pid)) {
143 DLOG(WARNING) << "Failed to read PID.";
144 return false;
145 }
146
147 *pid = read_pid;
148 return true;
149 }
150
151 private:
152 static const char* kHelloRequestMagic;
153 static const char* kHelloResponseMagic;
154 };
155
156 const char* HelloMessage::kHelloRequestMagic = "MREQ";
157 const char* HelloMessage::kHelloResponseMagic = "MRES";
158
159 } // namespace
160
161 //------------------------------------------------------------------------------
162
163 // A MessagePipeReader implemenation for IPC::Message communication.
164 class ChannelMojo::MessageReader : public internal::MessagePipeReader {
165 public:
166 MessageReader(mojo::ScopedMessagePipeHandle pipe, ChannelMojo* owner)
167 : internal::MessagePipeReader(pipe.Pass()),
168 owner_(owner) {}
169
170 bool Send(scoped_ptr<Message> message);
171 virtual void OnMessageReceived() OVERRIDE;
172 virtual void OnPipeClosed() OVERRIDE;
173 virtual void OnPipeError(MojoResult error) OVERRIDE;
174
175 private:
176 ChannelMojo* owner_;
177 };
178
179 void ChannelMojo::MessageReader::OnMessageReceived() {
180 Message message(data_buffer().empty() ? "" : &data_buffer()[0],
181 static_cast<uint32>(data_buffer().size()));
182
183 std::vector<MojoHandle> handle_buffer;
184 TakeHandleBuffer(&handle_buffer);
185 #if defined(OS_POSIX) && !defined(OS_NACL)
186 for (size_t i = 0; i < handle_buffer.size(); ++i) {
187 mojo::embedder::ScopedPlatformHandle platform_handle;
188 MojoResult unwrap_result = mojo::embedder::PassWrappedPlatformHandle(
189 handle_buffer[i], &platform_handle);
190 if (unwrap_result != MOJO_RESULT_OK) {
191 DLOG(WARNING) << "Pipe failed to covert handles. Closing: "
192 << unwrap_result;
193 CloseWithError(unwrap_result);
194 return;
195 }
196
197 bool ok = message.file_descriptor_set()->Add(platform_handle.release().fd);
198 DCHECK(ok);
199 }
200 #else
201 DCHECK(handle_buffer.empty());
202 #endif
203
204 message.TraceMessageEnd();
205 owner_->OnMessageReceived(message);
206 }
207
208 void ChannelMojo::MessageReader::OnPipeClosed() {
209 if (!owner_)
210 return;
211 owner_->OnPipeClosed(this);
212 owner_ = NULL;
213 }
214
215 void ChannelMojo::MessageReader::OnPipeError(MojoResult error) {
216 if (!owner_)
217 return;
218 owner_->OnPipeError(this);
219 }
220
221 bool ChannelMojo::MessageReader::Send(scoped_ptr<Message> message) {
222 DCHECK(IsValid());
223
224 message->TraceMessageBegin();
225 std::vector<MojoHandle> handles;
226 #if defined(OS_POSIX) && !defined(OS_NACL)
227 if (message->HasFileDescriptors()) {
228 FileDescriptorSet* fdset = message->file_descriptor_set();
229 for (size_t i = 0; i < fdset->size(); ++i) {
230 MojoHandle wrapped_handle;
231 MojoResult wrap_result = CreatePlatformHandleWrapper(
232 mojo::embedder::ScopedPlatformHandle(
233 mojo::embedder::PlatformHandle(
234 fdset->GetDescriptorAt(i))),
235 &wrapped_handle);
236 if (MOJO_RESULT_OK != wrap_result) {
237 DLOG(WARNING) << "Pipe failed to wrap handles. Closing: "
238 << wrap_result;
239 CloseWithError(wrap_result);
240 return false;
241 }
242
243 handles.push_back(wrapped_handle);
244 }
245 }
246 #endif
247 MojoResult write_result = MojoWriteMessage(
248 handle(),
249 message->data(), static_cast<uint32>(message->size()),
250 handles.empty() ? NULL : &handles[0],
251 static_cast<uint32>(handles.size()),
252 MOJO_WRITE_MESSAGE_FLAG_NONE);
253 if (MOJO_RESULT_OK != write_result) {
254 CloseWithError(write_result);
255 return false;
256 }
257
258 return true;
259 }
260
261 //------------------------------------------------------------------------------
262
263 // MessagePipeReader implemenation for control messages.
264 // Actual message handling is implemented by sublcasses.
265 class ChannelMojo::ControlReader : public internal::MessagePipeReader {
266 public:
267 ControlReader(mojo::ScopedMessagePipeHandle pipe, ChannelMojo* owner)
268 : internal::MessagePipeReader(pipe.Pass()),
269 owner_(owner) {}
270
271 virtual bool Connect() { return true; }
272 virtual void OnPipeClosed() OVERRIDE;
273 virtual void OnPipeError(MojoResult error) OVERRIDE;
274
275 protected:
276 ChannelMojo* owner_;
277 };
278
279 void ChannelMojo::ControlReader::OnPipeClosed() {
280 if (!owner_)
281 return;
282 owner_->OnPipeClosed(this);
283 owner_ = NULL;
284 }
285
286 void ChannelMojo::ControlReader::OnPipeError(MojoResult error) {
287 if (!owner_)
288 return;
289 owner_->OnPipeError(this);
290 }
291
292 //------------------------------------------------------------------------------
293
294 // ControlReader for server-side ChannelMojo.
295 class ChannelMojo::ServerControlReader : public ChannelMojo::ControlReader {
296 public:
297 ServerControlReader(mojo::ScopedMessagePipeHandle pipe, ChannelMojo* owner)
298 : ControlReader(pipe.Pass(), owner) { }
299
300 virtual bool Connect() OVERRIDE;
301 virtual void OnMessageReceived() OVERRIDE;
302
303 private:
304 MojoResult SendHelloRequest();
305 MojoResult RespondHelloResponse();
306
307 mojo::ScopedMessagePipeHandle message_pipe_;
308 };
309
310 bool ChannelMojo::ServerControlReader::Connect() {
311 MojoResult result = SendHelloRequest();
312 if (result != MOJO_RESULT_OK) {
313 CloseWithError(result);
314 return false;
315 }
316
317 return true;
318 }
319
320 MojoResult ChannelMojo::ServerControlReader::SendHelloRequest() {
321 DCHECK(IsValid());
322 DCHECK(!message_pipe_.is_valid());
323
324 mojo::ScopedMessagePipeHandle self;
325 mojo::ScopedMessagePipeHandle peer;
326 MojoResult create_result = mojo::CreateMessagePipe(
327 NULL, &message_pipe_, &peer);
328 if (MOJO_RESULT_OK != create_result) {
329 DLOG(WARNING) << "mojo::CreateMessagePipe failed: " << create_result;
330 return create_result;
331 }
332
333 MojoHandle peer_to_send = peer.get().value();
334 Pickle request = HelloMessage::CreateRequest(owner_->GetSelfPID());
335 MojoResult write_result = MojoWriteMessage(
336 handle(),
337 request.data(), static_cast<uint32>(request.size()),
338 &peer_to_send, 1,
339 MOJO_WRITE_MESSAGE_FLAG_NONE);
340 if (MOJO_RESULT_OK != write_result) {
341 DLOG(WARNING) << "Writing Hello request failed: " << create_result;
342 return write_result;
343 }
344
345 // |peer| is sent and no longer owned by |this|.
346 (void)peer.release();
347 return MOJO_RESULT_OK;
348 }
349
350 MojoResult ChannelMojo::ServerControlReader::RespondHelloResponse() {
351 Pickle request(data_buffer().empty() ? "" : &data_buffer()[0],
352 static_cast<uint32>(data_buffer().size()));
353
354 int32 read_pid = 0;
355 if (!HelloMessage::ReadResponse(request, &read_pid)) {
356 DLOG(ERROR) << "Failed to parse Hello response.";
357 return MOJO_RESULT_UNKNOWN;
358 }
359
360 base::ProcessId pid = static_cast<base::ProcessId>(read_pid);
361 owner_->set_peer_pid(pid);
362 owner_->OnConnected(message_pipe_.Pass());
363 return MOJO_RESULT_OK;
364 }
365
366 void ChannelMojo::ServerControlReader::OnMessageReceived() {
367 MojoResult result = RespondHelloResponse();
368 if (result != MOJO_RESULT_OK)
369 CloseWithError(result);
370 }
371
372 //------------------------------------------------------------------------------
373
374 // ControlReader for client-side ChannelMojo.
375 class ChannelMojo::ClientControlReader : public ChannelMojo::ControlReader {
376 public:
377 ClientControlReader(mojo::ScopedMessagePipeHandle pipe, ChannelMojo* owner)
378 : ControlReader(pipe.Pass(), owner) {}
379
380 virtual void OnMessageReceived() OVERRIDE;
381
382 private:
383 MojoResult RespondHelloRequest(MojoHandle message_channel);
384 };
385
386 MojoResult ChannelMojo::ClientControlReader::RespondHelloRequest(
387 MojoHandle message_channel) {
388 DCHECK(IsValid());
389
390 mojo::ScopedMessagePipeHandle received_pipe(
391 (mojo::MessagePipeHandle(message_channel)));
392
393 int32 read_request = 0;
394 Pickle request(data_buffer().empty() ? "" : &data_buffer()[0],
395 static_cast<uint32>(data_buffer().size()));
396 if (!HelloMessage::ReadRequest(request, &read_request)) {
397 DLOG(ERROR) << "Hello request has wrong magic.";
398 return MOJO_RESULT_UNKNOWN;
399 }
400
401 base::ProcessId pid = read_request;
402 Pickle response = HelloMessage::CreateResponse(owner_->GetSelfPID());
403 MojoResult write_result = MojoWriteMessage(
404 handle(),
405 response.data(), static_cast<uint32>(response.size()),
406 NULL, 0,
407 MOJO_WRITE_MESSAGE_FLAG_NONE);
408 if (MOJO_RESULT_OK != write_result) {
409 DLOG(ERROR) << "Writing Hello response failed: " << write_result;
410 return write_result;
411 }
412
413 owner_->set_peer_pid(pid);
414 owner_->OnConnected(received_pipe.Pass());
415 return MOJO_RESULT_OK;
416 }
417
418 void ChannelMojo::ClientControlReader::OnMessageReceived() {
419 std::vector<MojoHandle> handle_buffer;
420 TakeHandleBuffer(&handle_buffer);
421 if (handle_buffer.size() != 1) {
422 DLOG(ERROR) << "Hello request doesn't contains required handle: "
423 << handle_buffer.size();
424 CloseWithError(MOJO_RESULT_UNKNOWN);
425 return;
426 }
427
428 MojoResult result = RespondHelloRequest(handle_buffer[0]);
429 if (result != MOJO_RESULT_OK) {
430 DLOG(ERROR) << "Failed to respond Hello request. Closing: "
431 << result;
432 CloseWithError(result);
433 }
434 }
435
436 //------------------------------------------------------------------------------
437
438 void ChannelMojo::ChannelInfoDeleter::operator()(
439 mojo::embedder::ChannelInfo* ptr) const {
440 mojo::embedder::DestroyChannelOnIOThread(ptr);
441 }
442
443 //------------------------------------------------------------------------------
444
445 // static
446 scoped_ptr<ChannelMojo> ChannelMojo::Create(
447 scoped_ptr<Channel> bootstrap, Mode mode, Listener* listener,
448 scoped_refptr<base::TaskRunner> io_thread_task_runner) {
449 return make_scoped_ptr(new ChannelMojo(
450 bootstrap.Pass(), mode, listener, io_thread_task_runner));
451 }
452
453 // static
454 scoped_ptr<ChannelMojo> ChannelMojo::Create(
455 const ChannelHandle &channel_handle, Mode mode, Listener* listener,
456 scoped_refptr<base::TaskRunner> io_thread_task_runner) {
457 return Create(
458 Channel::Create(channel_handle, mode, g_null_listener.Pointer()),
459 mode, listener, io_thread_task_runner);
460 }
461
462 // static
463 scoped_ptr<ChannelFactory> ChannelMojo::CreateFactory(
464 const ChannelHandle &channel_handle, Mode mode,
465 scoped_refptr<base::TaskRunner> io_thread_task_runner) {
466 return make_scoped_ptr(
467 new MojoChannelFactory(
468 channel_handle, mode,
469 io_thread_task_runner)).PassAs<ChannelFactory>();
470 }
471
472 ChannelMojo::ChannelMojo(
473 scoped_ptr<Channel> bootstrap, Mode mode, Listener* listener,
474 scoped_refptr<base::TaskRunner> io_thread_task_runner)
475 : weak_factory_(this),
476 bootstrap_(bootstrap.Pass()),
477 mode_(mode), listener_(listener),
478 peer_pid_(base::kNullProcessId) {
479 DCHECK(mode_ == MODE_SERVER || mode_ == MODE_CLIENT);
480 mojo::ScopedMessagePipeHandle control_pipe
481 = mojo::embedder::CreateChannel(
482 mojo::embedder::ScopedPlatformHandle(
483 ToPlatformHandle(bootstrap_->TakePipeHandle())),
484 io_thread_task_runner,
485 base::Bind(&ChannelMojo::DidCreateChannel, base::Unretained(this)),
486 io_thread_task_runner);
487
488 // MessagePipeReader, that is crated in InitOnIOThread(), should live only in
489 // IO thread, but IPC::Channel can be instantiated outside of it.
490 // So we move the creation to the appropriate thread.
491 if (base::MessageLoopProxy::current() == io_thread_task_runner) {
492 InitOnIOThread(control_pipe.Pass());
493 } else {
494 io_thread_task_runner->PostTask(
495 FROM_HERE,
496 base::Bind(&ChannelMojo::InitOnIOThread,
497 weak_factory_.GetWeakPtr(),
498 base::Passed(control_pipe.Pass())));
499 }
500 }
501
502 ChannelMojo::~ChannelMojo() {
503 Close();
504 }
505
506 void ChannelMojo::InitOnIOThread(mojo::ScopedMessagePipeHandle control_pipe) {
507 control_reader_ = CreateControlReader(control_pipe.Pass());
508 }
509
510 scoped_ptr<ChannelMojo::ControlReader> ChannelMojo::CreateControlReader(
511 mojo::ScopedMessagePipeHandle pipe) {
512 if (MODE_SERVER == mode_) {
513 return make_scoped_ptr(
514 new ServerControlReader(pipe.Pass(), this)).PassAs<ControlReader>();
515 }
516
517 DCHECK(mode_ == MODE_CLIENT);
518 return make_scoped_ptr(
519 new ClientControlReader(pipe.Pass(), this)).PassAs<ControlReader>();
520 }
521
522 bool ChannelMojo::Connect() {
523 DCHECK(!message_reader_);
524 return control_reader_->Connect();
525 }
526
527 void ChannelMojo::Close() {
528 control_reader_.reset();
529 message_reader_.reset();
530 channel_info_.reset();
531 }
532
533 void ChannelMojo::OnConnected(mojo::ScopedMessagePipeHandle pipe) {
534 message_reader_ = make_scoped_ptr(new MessageReader(pipe.Pass(), this));
535
536 for (size_t i = 0; i < pending_messages_.size(); ++i) {
537 message_reader_->Send(make_scoped_ptr(pending_messages_[i]));
538 pending_messages_[i] = NULL;
539 }
540
541 pending_messages_.clear();
542
543 listener_->OnChannelConnected(GetPeerPID());
544 }
545
546 void ChannelMojo::OnPipeClosed(internal::MessagePipeReader* reader) {
547 Close();
548 }
549
550 void ChannelMojo::OnPipeError(internal::MessagePipeReader* reader) {
551 listener_->OnChannelError();
552 }
553
554
555 bool ChannelMojo::Send(Message* message) {
556 if (!message_reader_) {
557 pending_messages_.push_back(message);
558 return true;
559 }
560
561 return message_reader_->Send(make_scoped_ptr(message));
562 }
563
564 base::ProcessId ChannelMojo::GetPeerPID() const {
565 return peer_pid_;
566 }
567
568 base::ProcessId ChannelMojo::GetSelfPID() const {
569 return bootstrap_->GetSelfPID();
570 }
571
572 ChannelHandle ChannelMojo::TakePipeHandle() {
573 return bootstrap_->TakePipeHandle();
574 }
575
576 void ChannelMojo::DidCreateChannel(mojo::embedder::ChannelInfo* info) {
577 channel_info_.reset(info);
578 }
579
580 void ChannelMojo::OnMessageReceived(Message& message) {
581 listener_->OnMessageReceived(message);
582 if (message.dispatch_error())
583 listener_->OnBadMessageReceived(message);
584 }
585
586 #if defined(OS_POSIX) && !defined(OS_NACL)
587 int ChannelMojo::GetClientFileDescriptor() const {
588 return bootstrap_->GetClientFileDescriptor();
589 }
590
591 int ChannelMojo::TakeClientFileDescriptor() {
592 return bootstrap_->TakeClientFileDescriptor();
593 }
594 #endif // defined(OS_POSIX) && !defined(OS_NACL)
595
596 } // namespace IPC
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698