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 "base/synchronization/lock.h"
14 #include "ipc/ipc_channel.h"
15 #include "ipc/ipc_channel_factory.h"
16 #include "ipc/ipc_export.h"
17 #include "ipc/mojo/ipc_message_pipe_reader.h"
18 #include "ipc/mojo/ipc_mojo_bootstrap.h"
19 #include "ipc/mojo/scoped_ipc_support.h"
20 #include "third_party/mojo/src/mojo/edk/embedder/channel_info_forward.h"
21 #include "third_party/mojo/src/mojo/public/cpp/system/core.h"
25 // Mojo-based IPC::Channel implementation over a platform handle.
27 // ChannelMojo builds Mojo MessagePipe using underlying pipe given by
28 // "bootstrap" IPC::Channel which creates and owns platform pipe like
29 // named socket. The bootstrap Channel is used only for establishing
30 // the underlying connection. ChannelMojo takes its handle over once
31 // the it is made and puts MessagePipe on it.
33 // ChannelMojo has a couple of MessagePipes:
35 // * The first MessagePipe, which is built on top of bootstrap handle,
36 // is the "control" pipe. It is used to communicate out-of-band
37 // control messages that aren't visible from IPC::Listener.
39 // * The second MessagePipe, which is created by the server channel
40 // and sent to client Channel over the control pipe, is used
41 // to send IPC::Messages as an IPC::Sender.
43 // TODO(morrita): Extract handle creation part of IPC::Channel into
44 // separate class to clarify what ChannelMojo relies
46 // TODO(morrita): Add APIs to create extra MessagePipes to let
47 // Mojo-based objects talk over this Channel.
49 class IPC_MOJO_EXPORT ChannelMojo
51 public MojoBootstrap::Delegate
,
52 public NON_EXPORTED_BASE(internal::MessagePipeReader::Delegate
) {
56 virtual ~Delegate() {}
57 virtual base::WeakPtr
<Delegate
> ToWeakPtr() = 0;
58 virtual void OnChannelCreated(base::WeakPtr
<ChannelMojo
> channel
) = 0;
61 // True if ChannelMojo should be used regardless of the flag.
62 static bool ShouldBeUsed();
64 // Create ChannelMojo. A bootstrap channel is created as well.
65 // |host| must not be null for server channels.
66 static scoped_ptr
<ChannelMojo
> Create(
68 scoped_refptr
<base::TaskRunner
> io_runner
,
69 const ChannelHandle
& channel_handle
,
73 // Create a factory object for ChannelMojo.
74 // The factory is used to create Mojo-based ChannelProxy family.
75 // |host| must not be null.
76 static scoped_ptr
<ChannelFactory
> CreateServerFactory(
78 scoped_refptr
<base::TaskRunner
> io_runner
,
79 const ChannelHandle
& channel_handle
);
81 static scoped_ptr
<ChannelFactory
> CreateClientFactory(
83 scoped_refptr
<base::TaskRunner
> io_runner
,
84 const ChannelHandle
& channel_handle
);
86 ~ChannelMojo() override
;
88 // ChannelMojoHost tells the client handle using this API.
89 void OnClientLaunched(base::ProcessHandle handle
);
91 // Channel implementation
92 bool Connect() override
;
93 void Close() override
;
94 bool Send(Message
* message
) override
;
95 bool IsSendThreadSafe() const override
;
96 base::ProcessId
GetPeerPID() const override
;
97 base::ProcessId
GetSelfPID() const override
;
99 #if defined(OS_POSIX) && !defined(OS_NACL)
100 int GetClientFileDescriptor() const override
;
101 base::ScopedFD
TakeClientFileDescriptor() override
;
102 #endif // defined(OS_POSIX) && !defined(OS_NACL)
104 // These access protected API of IPC::Message, which has ChannelMojo
105 // as a friend class.
106 static MojoResult
WriteToMessageAttachmentSet(
107 const std::vector
<MojoHandle
>& handle_buffer
,
109 static MojoResult
ReadFromMessageAttachmentSet(
111 std::vector
<MojoHandle
>* handles
);
113 // MojoBootstrapDelegate implementation
114 void OnBootstrapError() override
;
116 // MessagePipeReader::Delegate
117 void OnMessageReceived(Message
& message
) override
;
118 void OnPipeClosed(internal::MessagePipeReader
* reader
) override
;
119 void OnPipeError(internal::MessagePipeReader
* reader
) override
;
122 ChannelMojo(Delegate
* delegate
,
123 scoped_refptr
<base::TaskRunner
> io_runner
,
124 const ChannelHandle
& channel_handle
,
128 mojo::ScopedMessagePipeHandle
CreateMessagingPipe(
129 mojo::embedder::ScopedPlatformHandle handle
);
130 void InitMessageReader(mojo::ScopedMessagePipeHandle pipe
, int32_t peer_pid
);
132 Listener
* listener() const { return listener_
; }
133 void set_peer_pid(base::ProcessId pid
) { peer_pid_
= pid
; }
136 struct ChannelInfoDeleter
{
137 void operator()(mojo::embedder::ChannelInfo
* ptr
) const;
140 // ChannelMojo needs to kill its MessagePipeReader in delayed manner
141 // because the channel wants to kill these readers during the
142 // notifications invoked by them.
143 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter
;
145 void InitOnIOThread(ChannelMojo::Delegate
* delegate
);
147 scoped_ptr
<MojoBootstrap
> bootstrap_
;
148 base::WeakPtr
<Delegate
> delegate_
;
151 base::ProcessId peer_pid_
;
152 scoped_refptr
<base::TaskRunner
> io_runner_
;
153 scoped_ptr
<mojo::embedder::ChannelInfo
,
154 ChannelInfoDeleter
> channel_info_
;
156 // Guards |message_reader_| and |pending_messages_|
158 // * The contents of |pending_messages_| can be modified from any thread.
159 // * |message_reader_| is modified only from the IO thread,
160 // but they can be referenced from other threads.
162 scoped_ptr
<internal::MessagePipeReader
, ReaderDeleter
> message_reader_
;
163 ScopedVector
<Message
> pending_messages_
;
165 scoped_ptr
<ScopedIPCSupport
> ipc_support_
;
167 base::WeakPtrFactory
<ChannelMojo
> weak_factory_
;
169 DISALLOW_COPY_AND_ASSIGN(ChannelMojo
);
174 #endif // IPC_IPC_CHANNEL_MOJO_H_