1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ipc/mojo/ipc_channel_mojo.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/lazy_instance.h"
#include "ipc/ipc_listener.h"
#include "ipc/ipc_logging.h"
#include "ipc/ipc_message_attachment_set.h"
#include "ipc/ipc_message_macros.h"
#include "ipc/mojo/client_channel.mojom.h"
#include "ipc/mojo/ipc_mojo_bootstrap.h"
#include "ipc/mojo/ipc_mojo_handle_attachment.h"
#include "third_party/mojo/src/mojo/edk/embedder/embedder.h"
#include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h"
#if defined(OS_POSIX) && !defined(OS_NACL)
#include "ipc/ipc_platform_file_attachment_posix.h"
#endif
namespace IPC {
namespace {
class MojoChannelFactory : public ChannelFactory {
public:
MojoChannelFactory(ChannelMojo::Delegate* delegate,
ChannelHandle channel_handle,
Channel::Mode mode)
: delegate_(delegate), channel_handle_(channel_handle), mode_(mode) {}
std::string GetName() const override {
return channel_handle_.name;
}
scoped_ptr<Channel> BuildChannel(Listener* listener) override {
return ChannelMojo::Create(delegate_, channel_handle_, mode_, listener);
}
private:
ChannelMojo::Delegate* delegate_;
ChannelHandle channel_handle_;
Channel::Mode mode_;
};
//------------------------------------------------------------------------------
class ClientChannelMojo : public ChannelMojo,
public ClientChannel,
public mojo::ErrorHandler {
public:
ClientChannelMojo(ChannelMojo::Delegate* delegate,
const ChannelHandle& handle,
Listener* listener);
~ClientChannelMojo() override;
// MojoBootstrap::Delegate implementation
void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override;
// mojo::ErrorHandler implementation
void OnConnectionError() override;
// ClientChannel implementation
void Init(
mojo::ScopedMessagePipeHandle pipe,
int32_t peer_pid,
const mojo::Callback<void(int32_t)>& callback) override;
private:
mojo::Binding<ClientChannel> binding_;
DISALLOW_COPY_AND_ASSIGN(ClientChannelMojo);
};
ClientChannelMojo::ClientChannelMojo(ChannelMojo::Delegate* delegate,
const ChannelHandle& handle,
Listener* listener)
: ChannelMojo(delegate, handle, Channel::MODE_CLIENT, listener),
binding_(this) {
}
ClientChannelMojo::~ClientChannelMojo() {
}
void ClientChannelMojo::OnPipeAvailable(
mojo::embedder::ScopedPlatformHandle handle) {
binding_.Bind(CreateMessagingPipe(handle.Pass()));
}
void ClientChannelMojo::OnConnectionError() {
listener()->OnChannelError();
}
void ClientChannelMojo::Init(
mojo::ScopedMessagePipeHandle pipe,
int32_t peer_pid,
const mojo::Callback<void(int32_t)>& callback) {
InitMessageReader(pipe.Pass(), static_cast<base::ProcessId>(peer_pid));
callback.Run(GetSelfPID());
}
//------------------------------------------------------------------------------
class ServerChannelMojo : public ChannelMojo, public mojo::ErrorHandler {
public:
ServerChannelMojo(ChannelMojo::Delegate* delegate,
const ChannelHandle& handle,
Listener* listener);
~ServerChannelMojo() override;
// MojoBootstrap::Delegate implementation
void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override;
// mojo::ErrorHandler implementation
void OnConnectionError() override;
// Channel override
void Close() override;
private:
// ClientChannelClient implementation
void ClientChannelWasInitialized(int32_t peer_pid);
mojo::InterfacePtr<ClientChannel> client_channel_;
mojo::ScopedMessagePipeHandle message_pipe_;
DISALLOW_COPY_AND_ASSIGN(ServerChannelMojo);
};
ServerChannelMojo::ServerChannelMojo(ChannelMojo::Delegate* delegate,
const ChannelHandle& handle,
Listener* listener)
: ChannelMojo(delegate, handle, Channel::MODE_SERVER, listener) {
}
ServerChannelMojo::~ServerChannelMojo() {
Close();
}
void ServerChannelMojo::OnPipeAvailable(
mojo::embedder::ScopedPlatformHandle handle) {
mojo::ScopedMessagePipeHandle peer;
MojoResult create_result =
mojo::CreateMessagePipe(nullptr, &message_pipe_, &peer);
if (create_result != MOJO_RESULT_OK) {
LOG(WARNING) << "mojo::CreateMessagePipe failed: " << create_result;
listener()->OnChannelError();
return;
}
client_channel_.Bind(CreateMessagingPipe(handle.Pass()));
client_channel_.set_error_handler(this);
client_channel_->Init(
peer.Pass(),
static_cast<int32_t>(GetSelfPID()),
base::Bind(&ServerChannelMojo::ClientChannelWasInitialized,
base::Unretained(this)));
}
void ServerChannelMojo::ClientChannelWasInitialized(int32_t peer_pid) {
InitMessageReader(message_pipe_.Pass(), peer_pid);
}
void ServerChannelMojo::OnConnectionError() {
listener()->OnChannelError();
}
void ServerChannelMojo::Close() {
client_channel_.reset();
message_pipe_.reset();
ChannelMojo::Close();
}
#if defined(OS_POSIX) && !defined(OS_NACL)
base::ScopedFD TakeOrDupFile(internal::PlatformFileAttachment* attachment) {
return attachment->Owns() ? base::ScopedFD(attachment->TakePlatformFile())
: base::ScopedFD(dup(attachment->file()));
}
#endif
} // namespace
//------------------------------------------------------------------------------
void ChannelMojo::ChannelInfoDeleter::operator()(
mojo::embedder::ChannelInfo* ptr) const {
mojo::embedder::DestroyChannelOnIOThread(ptr);
}
//------------------------------------------------------------------------------
// static
bool ChannelMojo::ShouldBeUsed() {
// TODO(morrita): Remove this if it sticks.
return true;
}
// static
scoped_ptr<ChannelMojo> ChannelMojo::Create(ChannelMojo::Delegate* delegate,
const ChannelHandle& channel_handle,
Mode mode,
Listener* listener) {
switch (mode) {
case Channel::MODE_CLIENT:
return make_scoped_ptr(
new ClientChannelMojo(delegate, channel_handle, listener));
case Channel::MODE_SERVER:
return make_scoped_ptr(
new ServerChannelMojo(delegate, channel_handle, listener));
default:
NOTREACHED();
return nullptr;
}
}
// static
scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory(
ChannelMojo::Delegate* delegate,
const ChannelHandle& channel_handle) {
return make_scoped_ptr(
new MojoChannelFactory(delegate, channel_handle, Channel::MODE_SERVER));
}
// static
scoped_ptr<ChannelFactory> ChannelMojo::CreateClientFactory(
ChannelMojo::Delegate* delegate,
const ChannelHandle& channel_handle) {
return make_scoped_ptr(
new MojoChannelFactory(delegate, channel_handle, Channel::MODE_CLIENT));
}
ChannelMojo::ChannelMojo(ChannelMojo::Delegate* delegate,
const ChannelHandle& handle,
Mode mode,
Listener* listener)
: mode_(mode),
listener_(listener),
peer_pid_(base::kNullProcessId),
weak_factory_(this) {
// Create MojoBootstrap after all members are set as it touches
// ChannelMojo from a different thread.
bootstrap_ = MojoBootstrap::Create(handle, mode, this);
if (delegate) {
if (delegate->GetIOTaskRunner() ==
base::MessageLoop::current()->message_loop_proxy()) {
InitDelegate(delegate);
} else {
delegate->GetIOTaskRunner()->PostTask(
FROM_HERE,
base::Bind(
&ChannelMojo::InitDelegate, base::Unretained(this), delegate));
}
}
}
ChannelMojo::~ChannelMojo() {
Close();
}
void ChannelMojo::InitDelegate(ChannelMojo::Delegate* delegate) {
ipc_support_.reset(
new ScopedIPCSupport(base::MessageLoop::current()->task_runner()));
delegate_ = delegate->ToWeakPtr();
delegate_->OnChannelCreated(weak_factory_.GetWeakPtr());
}
mojo::ScopedMessagePipeHandle ChannelMojo::CreateMessagingPipe(
mojo::embedder::ScopedPlatformHandle handle) {
DCHECK(!channel_info_.get());
mojo::embedder::ChannelInfo* channel_info;
mojo::ScopedMessagePipeHandle pipe =
mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info);
channel_info_.reset(channel_info);
return pipe.Pass();
}
bool ChannelMojo::Connect() {
DCHECK(!message_reader_);
return bootstrap_->Connect();
}
void ChannelMojo::Close() {
message_reader_.reset();
channel_info_.reset();
ipc_support_.reset();
}
void ChannelMojo::OnBootstrapError() {
listener_->OnChannelError();
}
void ChannelMojo::InitMessageReader(mojo::ScopedMessagePipeHandle pipe,
int32_t peer_pid) {
message_reader_ =
make_scoped_ptr(new internal::MessagePipeReader(pipe.Pass(), this));
for (size_t i = 0; i < pending_messages_.size(); ++i) {
bool sent = message_reader_->Send(make_scoped_ptr(pending_messages_[i]));
pending_messages_[i] = nullptr;
if (!sent) {
pending_messages_.clear();
listener_->OnChannelError();
return;
}
}
pending_messages_.clear();
set_peer_pid(peer_pid);
listener_->OnChannelConnected(static_cast<int32_t>(GetPeerPID()));
if (message_reader_)
message_reader_->ReadMessagesThenWait();
}
void ChannelMojo::OnPipeClosed(internal::MessagePipeReader* reader) {
Close();
}
void ChannelMojo::OnPipeError(internal::MessagePipeReader* reader) {
listener_->OnChannelError();
}
bool ChannelMojo::Send(Message* message) {
if (!message_reader_) {
pending_messages_.push_back(message);
return true;
}
return message_reader_->Send(make_scoped_ptr(message));
}
base::ProcessId ChannelMojo::GetPeerPID() const {
return peer_pid_;
}
base::ProcessId ChannelMojo::GetSelfPID() const {
return bootstrap_->GetSelfPID();
}
void ChannelMojo::OnClientLaunched(base::ProcessHandle handle) {
bootstrap_->OnClientLaunched(handle);
}
void ChannelMojo::OnMessageReceived(Message& message) {
TRACE_EVENT2("ipc,toplevel", "ChannelMojo::OnMessageReceived",
"class", IPC_MESSAGE_ID_CLASS(message.type()),
"line", IPC_MESSAGE_ID_LINE(message.type()));
listener_->OnMessageReceived(message);
if (message.dispatch_error())
listener_->OnBadMessageReceived(message);
}
#if defined(OS_POSIX) && !defined(OS_NACL)
int ChannelMojo::GetClientFileDescriptor() const {
return bootstrap_->GetClientFileDescriptor();
}
base::ScopedFD ChannelMojo::TakeClientFileDescriptor() {
return bootstrap_->TakeClientFileDescriptor();
}
#endif // defined(OS_POSIX) && !defined(OS_NACL)
// static
MojoResult ChannelMojo::ReadFromMessageAttachmentSet(
Message* message,
std::vector<MojoHandle>* handles) {
// We dup() the handles in IPC::Message to transmit.
// IPC::MessageAttachmentSet has intricate lifecycle semantics
// of FDs, so just to dup()-and-own them is the safest option.
if (message->HasAttachments()) {
MessageAttachmentSet* set = message->attachment_set();
for (unsigned i = 0; i < set->size(); ++i) {
scoped_refptr<MessageAttachment> attachment = set->GetAttachmentAt(i);
switch (attachment->GetType()) {
case MessageAttachment::TYPE_PLATFORM_FILE:
#if defined(OS_POSIX) && !defined(OS_NACL)
{
base::ScopedFD file =
TakeOrDupFile(static_cast<IPC::internal::PlatformFileAttachment*>(
attachment.get()));
if (!file.is_valid()) {
DPLOG(WARNING) << "Failed to dup FD to transmit.";
set->CommitAll();
return MOJO_RESULT_UNKNOWN;
}
MojoHandle wrapped_handle;
MojoResult wrap_result = CreatePlatformHandleWrapper(
mojo::embedder::ScopedPlatformHandle(
mojo::embedder::PlatformHandle(file.release())),
&wrapped_handle);
if (MOJO_RESULT_OK != wrap_result) {
LOG(WARNING) << "Pipe failed to wrap handles. Closing: "
<< wrap_result;
set->CommitAll();
return wrap_result;
}
handles->push_back(wrapped_handle);
}
#else
NOTREACHED();
#endif // defined(OS_POSIX) && !defined(OS_NACL)
break;
case MessageAttachment::TYPE_MOJO_HANDLE: {
mojo::ScopedHandle handle =
static_cast<IPC::internal::MojoHandleAttachment*>(
attachment.get())->TakeHandle();
handles->push_back(handle.release().value());
} break;
}
}
set->CommitAll();
}
return MOJO_RESULT_OK;
}
// static
MojoResult ChannelMojo::WriteToMessageAttachmentSet(
const std::vector<MojoHandle>& handle_buffer,
Message* message) {
for (size_t i = 0; i < handle_buffer.size(); ++i) {
bool ok = message->attachment_set()->AddAttachment(
new IPC::internal::MojoHandleAttachment(
mojo::MakeScopedHandle(mojo::Handle(handle_buffer[i]))));
DCHECK(ok);
if (!ok) {
LOG(ERROR) << "Failed to add new Mojo handle.";
return MOJO_RESULT_UNKNOWN;
}
}
return MOJO_RESULT_OK;
}
} // namespace IPC
|