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 "base/thread_task_runner_handle.h"
12 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h"
16 ChannelInit::ChannelInit() : channel_info_(nullptr), weak_factory_(this) {}
18 ChannelInit::~ChannelInit() {
20 mojo::embedder::DestroyChannel(channel_info_
,
21 base::Bind(&base::DoNothing
), nullptr);
24 mojo::ScopedMessagePipeHandle
ChannelInit::Init(
25 base::PlatformFile file
,
26 scoped_refptr
<base::TaskRunner
> io_thread_task_runner
) {
27 scoped_ptr
<IPC::ScopedIPCSupport
> ipc_support(
28 new IPC::ScopedIPCSupport(io_thread_task_runner
));
29 mojo::ScopedMessagePipeHandle message_pipe
=
30 mojo::embedder::CreateChannel(
31 mojo::embedder::ScopedPlatformHandle(
32 mojo::embedder::PlatformHandle(file
)),
33 base::Bind(&ChannelInit::OnCreatedChannel
,
34 weak_factory_
.GetWeakPtr(),
35 base::Passed(&ipc_support
)),
36 base::ThreadTaskRunnerHandle::Get()).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