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 #ifndef IPC_IPC_CHANNEL_MOJO_H_
6 #define IPC_IPC_CHANNEL_MOJO_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/memory/weak_ptr.h"
13 #include "ipc/ipc_channel.h"
14 #include "ipc/ipc_channel_factory.h"
15 #include "ipc/ipc_export.h"
16 #include "ipc/mojo/ipc_message_pipe_reader.h"
17 #include "mojo/public/cpp/system/core.h"
27 // Mojo-based IPC::Channel implementation over a platform handle.
29 // ChannelMojo builds Mojo MessagePipe using underlying pipe given by
30 // "bootstrap" IPC::Channel which creates and owns platform pipe like
31 // named socket. The bootstrap Channel is used only for establishing
32 // the underlying connection. ChannelMojo takes its handle over once
33 // the it is made and puts MessagePipe on it.
35 // ChannelMojo has a couple of MessagePipes:
37 // * The first MessagePipe, which is built on top of bootstrap handle,
38 // is the "control" pipe. It is used to communicate out-of-band
39 // control messages that aren't visible from IPC::Listener.
41 // * The second MessagePipe, which is created by the server channel
42 // and sent to client Channel over the control pipe, is used
43 // to send IPC::Messages as an IPC::Sender.
45 // TODO(morrita): Extract handle creation part of IPC::Channel into
46 // separate class to clarify what ChannelMojo relies
48 // TODO(morrita): Add APIs to create extra MessagePipes to let
49 // Mojo-based objects talk over this Channel.
51 class IPC_MOJO_EXPORT ChannelMojo
: public Channel
{
53 // Create ChannelMojo. A bootstrap channel is created as well.
54 static scoped_ptr
<ChannelMojo
> Create(
55 const ChannelHandle
&channel_handle
, Mode mode
, Listener
* listener
,
56 scoped_refptr
<base::TaskRunner
> io_thread_task_runner
);
58 // Create a factory object for ChannelMojo.
59 // The factory is used to create Mojo-based ChannelProxy family.
60 static scoped_ptr
<ChannelFactory
> CreateFactory(
61 const ChannelHandle
&channel_handle
, Mode mode
,
62 scoped_refptr
<base::TaskRunner
> io_thread_task_runner
);
64 virtual ~ChannelMojo();
66 // Channel implementation
67 virtual bool Connect() OVERRIDE
;
68 virtual void Close() OVERRIDE
;
69 virtual bool Send(Message
* message
) OVERRIDE
;
70 virtual base::ProcessId
GetPeerPID() const OVERRIDE
;
71 virtual base::ProcessId
GetSelfPID() const OVERRIDE
;
72 virtual ChannelHandle
TakePipeHandle() OVERRIDE
;
74 #if defined(OS_POSIX) && !defined(OS_NACL)
75 virtual int GetClientFileDescriptor() const OVERRIDE
;
76 virtual int TakeClientFileDescriptor() OVERRIDE
;
77 #endif // defined(OS_POSIX) && !defined(OS_NACL)
79 // Called from MessagePipeReader implementations
80 void OnMessageReceived(Message
& message
);
81 void OnConnected(mojo::ScopedMessagePipeHandle pipe
);
82 void OnPipeClosed(internal::MessagePipeReader
* reader
);
83 void OnPipeError(internal::MessagePipeReader
* reader
);
84 void set_peer_pid(base::ProcessId pid
) { peer_pid_
= pid
; }
87 struct ChannelInfoDeleter
{
88 void operator()(mojo::embedder::ChannelInfo
* ptr
) const;
91 // ChannelMojo needs to kill its MessagePipeReader in delayed manner
92 // because the channel wants to kill these readers during the
93 // notifications invoked by them.
94 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter
;
97 class ServerControlReader
;
98 class ClientControlReader
;
101 ChannelMojo(scoped_ptr
<Channel
> bootstrap
, Mode mode
, Listener
* listener
,
102 scoped_refptr
<base::TaskRunner
> io_thread_task_runner
);
104 void InitOnIOThread();
106 scoped_ptr
<Channel
> bootstrap_
;
109 base::ProcessId peer_pid_
;
110 scoped_ptr
<mojo::embedder::ChannelInfo
,
111 ChannelInfoDeleter
> channel_info_
;
113 scoped_ptr
<ControlReader
, ReaderDeleter
> control_reader_
;
114 scoped_ptr
<MessageReader
, ReaderDeleter
> message_reader_
;
115 ScopedVector
<Message
> pending_messages_
;
117 base::WeakPtrFactory
<ChannelMojo
> weak_factory_
;
119 DISALLOW_COPY_AND_ASSIGN(ChannelMojo
);
124 #endif // IPC_IPC_CHANNEL_MOJO_H_