summaryrefslogtreecommitdiffstats
path: root/content/test/render_thread_impl_browser_test_ipc_helper.cc
blob: 8f454e2a7b72ba8f9322cbb935f4f5450afe94ae (plain)
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
// 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 "content/test/render_thread_impl_browser_test_ipc_helper.h"

#include "content/common/mojo/channel_init.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {

class RenderThreadImplBrowserIPCTestHelper::DummyListener
    : public IPC::Listener {
 public:
  bool OnMessageReceived(const IPC::Message& message) override { return true; }
};

RenderThreadImplBrowserIPCTestHelper::RenderThreadImplBrowserIPCTestHelper() {
  message_loop_.reset(new base::MessageLoopForIO());

  channel_id_ = IPC::Channel::GenerateVerifiedChannelID("");
  dummy_listener_.reset(new DummyListener());

  SetupIpcThread();

  if (IPC::ChannelMojo::ShouldBeUsed()) {
    SetupMojo();
  } else {
    channel_ = IPC::ChannelProxy::Create(channel_id_, IPC::Channel::MODE_SERVER,
                                         dummy_listener_.get(),
                                         ipc_thread_->task_runner());
  }
}

RenderThreadImplBrowserIPCTestHelper::~RenderThreadImplBrowserIPCTestHelper() {
}

void RenderThreadImplBrowserIPCTestHelper::SetupIpcThread() {
  ipc_thread_.reset(new base::Thread("test_ipc_thread"));
  base::Thread::Options options;
  options.message_loop_type = base::MessageLoop::TYPE_IO;
  ASSERT_TRUE(ipc_thread_->StartWithOptions(options));
}

void RenderThreadImplBrowserIPCTestHelper::SetupMojo() {
  InitializeMojo();

  ipc_support_.reset(new IPC::ScopedIPCSupport(ipc_thread_->task_runner()));
  mojo_application_host_.reset(new MojoApplicationHost());
  mojo_application_host_->OverrideIOTaskRunnerForTest(
      ipc_thread_->task_runner());

  channel_ = IPC::ChannelProxy::Create(
      IPC::ChannelMojo::CreateServerFactory(ipc_thread_->task_runner(),
                                            channel_id_),
      dummy_listener_.get(), ipc_thread_->task_runner());

  mojo_application_host_->Init();
  mojo_application_host_->Activate(channel_.get(),
                                   base::GetCurrentProcessHandle());
}

scoped_refptr<base::SingleThreadTaskRunner>
RenderThreadImplBrowserIPCTestHelper::GetIOTaskRunner() const {
  return ipc_thread_->task_runner();
}

}  // namespace content