[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / content / common / mojo / channel_init.cc
blobe81515c2da0eda8f573fd5a418817fed7b2c33c9
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"
7 #include "base/bind.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"
14 namespace content {
16 ChannelInit::ChannelInit() : channel_info_(nullptr), weak_factory_(this) {}
18 ChannelInit::~ChannelInit() {
19 if (channel_info_)
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() {
41 if (channel_info_)
42 mojo::embedder::WillDestroyChannelSoon(channel_info_);
45 void ChannelInit::ShutdownOnIOThread() {
46 if (channel_info_)
47 mojo::embedder::DestroyChannelOnIOThread(channel_info_);
48 channel_info_ = nullptr;
49 ipc_support_.reset();
52 // static
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.
58 if (!self) {
59 mojo::embedder::DestroyChannel(channel,
60 base::Bind(&base::DoNothing), nullptr);
61 return;
64 DCHECK(!self->channel_info_);
65 self->channel_info_ = channel;
66 self->ipc_support_ = ipc_support.Pass();
69 } // namespace content