1 // Copyright 2015 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 "content/common/mojo/channel_init.h"
8 #include "base/bind_helpers.h"
9 #include "base/lazy_instance.h"
10 #include "base/message_loop/message_loop.h"
11 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h"
15 ChannelInit::ChannelInit() : channel_info_(nullptr), weak_factory_(this) {}
17 ChannelInit::~ChannelInit() {
19 mojo::embedder::DestroyChannel(channel_info_
,
20 base::Bind(&base::DoNothing
), nullptr);
23 mojo::ScopedMessagePipeHandle
ChannelInit::Init(
24 base::PlatformFile file
,
25 scoped_refptr
<base::TaskRunner
> io_thread_task_runner
) {
26 scoped_ptr
<IPC::ScopedIPCSupport
> ipc_support(
27 new IPC::ScopedIPCSupport(io_thread_task_runner
));
28 mojo::ScopedMessagePipeHandle message_pipe
=
29 mojo::embedder::CreateChannel(
30 mojo::embedder::ScopedPlatformHandle(
31 mojo::embedder::PlatformHandle(file
)),
32 io_thread_task_runner
,
33 base::Bind(&ChannelInit::OnCreatedChannel
,
34 weak_factory_
.GetWeakPtr(),
35 base::Passed(&ipc_support
)),
36 base::MessageLoop::current()->task_runner()).Pass();
37 return message_pipe
.Pass();
40 void ChannelInit::WillDestroySoon() {
42 mojo::embedder::WillDestroyChannelSoon(channel_info_
);
45 void ChannelInit::ShutdownOnIOThread() {
47 mojo::embedder::DestroyChannelOnIOThread(channel_info_
);
48 channel_info_
= nullptr;
53 void ChannelInit::OnCreatedChannel(
54 base::WeakPtr
<ChannelInit
> self
,
55 scoped_ptr
<IPC::ScopedIPCSupport
> ipc_support
,
56 mojo::embedder::ChannelInfo
* channel
) {
57 // If |self| was already destroyed, shut the channel down.
59 mojo::embedder::DestroyChannel(channel
,
60 base::Bind(&base::DoNothing
), nullptr);
64 DCHECK(!self
->channel_info_
);
65 self
->channel_info_
= channel
;
66 self
->ipc_support_
= ipc_support
.Pass();
69 } // namespace content