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"
17 base::LazyInstance
<scoped_refptr
<base::TaskRunner
>>
18 g_single_process_task_runner
= LAZY_INSTANCE_INITIALIZER
;
22 ChannelInit::ChannelInit() : channel_info_(nullptr), weak_factory_(this) {}
24 ChannelInit::~ChannelInit() {
26 mojo::embedder::DestroyChannel(channel_info_
,
27 base::Bind(&base::DoNothing
), nullptr);
30 mojo::ScopedMessagePipeHandle
ChannelInit::Init(
31 base::PlatformFile file
,
32 scoped_refptr
<base::TaskRunner
> io_thread_task_runner
) {
33 scoped_ptr
<IPC::ScopedIPCSupport
> ipc_support(
34 new IPC::ScopedIPCSupport(io_thread_task_runner
));
35 mojo::ScopedMessagePipeHandle message_pipe
=
36 mojo::embedder::CreateChannel(
37 mojo::embedder::ScopedPlatformHandle(
38 mojo::embedder::PlatformHandle(file
)),
39 io_thread_task_runner
,
40 base::Bind(&ChannelInit::OnCreatedChannel
,
41 weak_factory_
.GetWeakPtr(),
42 base::Passed(&ipc_support
)),
43 base::MessageLoop::current()->task_runner()).Pass();
44 return message_pipe
.Pass();
47 void ChannelInit::WillDestroySoon() {
49 mojo::embedder::WillDestroyChannelSoon(channel_info_
);
53 scoped_refptr
<base::TaskRunner
> ChannelInit::GetSingleProcessIOTaskRunner() {
54 return g_single_process_task_runner
.Get();
58 void ChannelInit::SetSingleProcessIOTaskRunner(
59 scoped_refptr
<base::TaskRunner
> io_task_runner
) {
60 g_single_process_task_runner
.Get() = io_task_runner
;
64 void ChannelInit::OnCreatedChannel(
65 base::WeakPtr
<ChannelInit
> self
,
66 scoped_ptr
<IPC::ScopedIPCSupport
> ipc_support
,
67 mojo::embedder::ChannelInfo
* channel
) {
68 // If |self| was already destroyed, shut the channel down.
70 mojo::embedder::DestroyChannel(channel
,
71 base::Bind(&base::DoNothing
), nullptr);
75 DCHECK(!self
->channel_info_
);
76 self
->channel_info_
= channel
;
77 self
->ipc_support_
= ipc_support
.Pass();
80 } // namespace content