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_mojo_bootstrap.h"
7 #include "base/logging.h"
8 #include "base/process/process_handle.h"
9 #include "ipc/ipc_message_utils.h"
10 #include "ipc/ipc_platform_file.h"
11 #include "third_party/mojo/src/mojo/edk/embedder/platform_channel_pair.h"
17 // MojoBootstrap for the server process. You should create the instance
18 // using MojoBootstrap::Create().
19 class MojoServerBootstrap
: public MojoBootstrap
{
21 MojoServerBootstrap();
24 void SendClientPipe(int32 peer_pid
);
26 // Listener implementations
27 bool OnMessageReceived(const Message
& message
) override
;
28 void OnChannelConnected(int32 peer_pid
) override
;
30 mojo::embedder::ScopedPlatformHandle server_pipe_
;
33 DISALLOW_COPY_AND_ASSIGN(MojoServerBootstrap
);
36 MojoServerBootstrap::MojoServerBootstrap() : connected_(false) {
39 void MojoServerBootstrap::SendClientPipe(int32 peer_pid
) {
40 DCHECK_EQ(state(), STATE_INITIALIZED
);
43 mojo::embedder::PlatformChannelPair channel_pair
;
44 server_pipe_
= channel_pair
.PassServerHandle();
46 base::Process peer_process
=
48 base::Process::OpenWithAccess(peer_pid
, PROCESS_DUP_HANDLE
);
50 base::Process::Open(peer_pid
);
52 PlatformFileForTransit client_pipe
= GetFileHandleForProcess(
54 channel_pair
.PassClientHandle().release().fd
,
56 channel_pair
.PassClientHandle().release().handle
,
58 peer_process
.Handle(), true);
59 if (client_pipe
== IPC::InvalidPlatformFileForTransit()) {
61 // GetFileHandleForProcess() only fails on Windows.
64 LOG(WARNING
) << "Failed to translate file handle for client process.";
69 scoped_ptr
<Message
> message(new Message());
70 ParamTraits
<PlatformFileForTransit
>::Write(message
.get(), client_pipe
);
71 Send(message
.release());
73 set_state(STATE_WAITING_ACK
);
76 void MojoServerBootstrap::OnChannelConnected(int32 peer_pid
) {
77 DCHECK_EQ(state(), STATE_INITIALIZED
);
79 SendClientPipe(peer_pid
);
82 bool MojoServerBootstrap::OnMessageReceived(const Message
&) {
83 if (state() != STATE_WAITING_ACK
) {
84 set_state(STATE_ERROR
);
85 LOG(ERROR
) << "Got inconsistent message from client.";
89 set_state(STATE_READY
);
90 CHECK(server_pipe_
.is_valid());
91 delegate()->OnPipeAvailable(
92 mojo::embedder::ScopedPlatformHandle(server_pipe_
.release()));
97 // MojoBootstrap for client processes. You should create the instance
98 // using MojoBootstrap::Create().
99 class MojoClientBootstrap
: public MojoBootstrap
{
101 MojoClientBootstrap();
104 // Listener implementations
105 bool OnMessageReceived(const Message
& message
) override
;
106 void OnChannelConnected(int32 peer_pid
) override
;
108 DISALLOW_COPY_AND_ASSIGN(MojoClientBootstrap
);
111 MojoClientBootstrap::MojoClientBootstrap() {
114 bool MojoClientBootstrap::OnMessageReceived(const Message
& message
) {
115 if (state() != STATE_INITIALIZED
) {
116 set_state(STATE_ERROR
);
117 LOG(ERROR
) << "Got inconsistent message from server.";
121 PlatformFileForTransit pipe
;
122 base::PickleIterator
iter(message
);
123 if (!ParamTraits
<PlatformFileForTransit
>::Read(&message
, &iter
, &pipe
)) {
124 LOG(WARNING
) << "Failed to read a file handle from bootstrap channel.";
125 message
.set_dispatch_error();
131 set_state(STATE_READY
);
132 delegate()->OnPipeAvailable(
133 mojo::embedder::ScopedPlatformHandle(mojo::embedder::PlatformHandle(
134 PlatformFileForTransitToPlatformFile(pipe
))));
139 void MojoClientBootstrap::OnChannelConnected(int32 peer_pid
) {
147 scoped_ptr
<MojoBootstrap
> MojoBootstrap::Create(ChannelHandle handle
,
150 AttachmentBroker
* broker
) {
151 CHECK(mode
== Channel::MODE_CLIENT
|| mode
== Channel::MODE_SERVER
);
152 scoped_ptr
<MojoBootstrap
> self
=
153 mode
== Channel::MODE_CLIENT
154 ? scoped_ptr
<MojoBootstrap
>(new MojoClientBootstrap())
155 : scoped_ptr
<MojoBootstrap
>(new MojoServerBootstrap());
157 scoped_ptr
<Channel
> bootstrap_channel
=
158 Channel::Create(handle
, mode
, self
.get(), broker
);
159 self
->Init(bootstrap_channel
.Pass(), delegate
);
163 MojoBootstrap::MojoBootstrap() : delegate_(NULL
), state_(STATE_INITIALIZED
) {
166 MojoBootstrap::~MojoBootstrap() {
169 void MojoBootstrap::Init(scoped_ptr
<Channel
> channel
, Delegate
* delegate
) {
170 channel_
= channel
.Pass();
171 delegate_
= delegate
;
174 bool MojoBootstrap::Connect() {
175 return channel_
->Connect();
178 base::ProcessId
MojoBootstrap::GetSelfPID() const {
179 return channel_
->GetSelfPID();
182 void MojoBootstrap::OnBadMessageReceived(const Message
& message
) {
186 void MojoBootstrap::OnChannelError() {
187 if (state_
== STATE_READY
|| state_
== STATE_ERROR
)
189 DLOG(WARNING
) << "Detected error on Mojo bootstrap channel.";
193 void MojoBootstrap::Fail() {
194 set_state(STATE_ERROR
);
195 delegate()->OnBootstrapError();
198 bool MojoBootstrap::HasFailed() const {
199 return state() == STATE_ERROR
;
202 bool MojoBootstrap::Send(Message
* message
) {
203 return channel_
->Send(message
);
206 #if defined(OS_POSIX) && !defined(OS_NACL)
207 int MojoBootstrap::GetClientFileDescriptor() const {
208 return channel_
->GetClientFileDescriptor();
211 base::ScopedFD
MojoBootstrap::TakeClientFileDescriptor() {
212 return channel_
->TakeClientFileDescriptor();
214 #endif // defined(OS_POSIX) && !defined(OS_NACL)