Move GN build files for cacheinvalidation into the main tree.
[chromium-blink-merge.git] / ipc / ipc_test_base.cc
blob4de55431b38bf9c9242bdb88c5fb3e737620eac9
1 // Copyright (c) 2012 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 "build/build_config.h"
7 #include "base/single_thread_task_runner.h"
8 #include "ipc/ipc_test_base.h"
10 #include "base/command_line.h"
11 #include "base/process/kill.h"
12 #include "base/threading/thread.h"
13 #include "base/time/time.h"
14 #include "ipc/ipc_descriptors.h"
16 #if defined(OS_POSIX)
17 #include "base/posix/global_descriptors.h"
18 #endif
20 // static
21 std::string IPCTestBase::GetChannelName(const std::string& test_client_name) {
22 DCHECK(!test_client_name.empty());
23 return test_client_name + "__Channel";
26 IPCTestBase::IPCTestBase() {
29 IPCTestBase::~IPCTestBase() {
32 void IPCTestBase::TearDown() {
33 message_loop_.reset();
34 MultiProcessTest::TearDown();
37 void IPCTestBase::Init(const std::string& test_client_name) {
38 InitWithCustomMessageLoop(
39 test_client_name,
40 scoped_ptr<base::MessageLoop>(new base::MessageLoopForIO()));
43 void IPCTestBase::InitWithCustomMessageLoop(
44 const std::string& test_client_name,
45 scoped_ptr<base::MessageLoop> message_loop) {
46 DCHECK(!test_client_name.empty());
47 DCHECK(test_client_name_.empty());
48 DCHECK(!message_loop_);
50 test_client_name_ = test_client_name;
51 message_loop_ = message_loop.Pass();
54 void IPCTestBase::CreateChannel(IPC::Listener* listener) {
55 CreateChannelFromChannelHandle(GetTestChannelHandle(), listener);
58 bool IPCTestBase::ConnectChannel() {
59 CHECK(channel_.get());
60 return channel_->Connect();
63 scoped_ptr<IPC::Channel> IPCTestBase::ReleaseChannel() {
64 return channel_.Pass();
67 void IPCTestBase::SetChannel(scoped_ptr<IPC::Channel> channel) {
68 channel_ = channel.Pass();
72 void IPCTestBase::DestroyChannel() {
73 DCHECK(channel_.get());
74 channel_.reset();
77 void IPCTestBase::CreateChannelFromChannelHandle(
78 const IPC::ChannelHandle& channel_handle,
79 IPC::Listener* listener) {
80 CHECK(!channel_.get());
81 CHECK(!channel_proxy_.get());
82 channel_ = CreateChannelFactory(
83 channel_handle, task_runner().get())->BuildChannel(listener);
86 void IPCTestBase::CreateChannelProxy(
87 IPC::Listener* listener,
88 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) {
89 CHECK(!channel_.get());
90 CHECK(!channel_proxy_.get());
91 channel_proxy_ = IPC::ChannelProxy::Create(
92 CreateChannelFactory(GetTestChannelHandle(), ipc_task_runner.get()),
93 listener, ipc_task_runner);
96 void IPCTestBase::DestroyChannelProxy() {
97 CHECK(channel_proxy_.get());
98 channel_proxy_.reset();
101 std::string IPCTestBase::GetTestMainName() const {
102 return test_client_name_ + "TestClientMain";
105 bool IPCTestBase::DidStartClient() {
106 DCHECK(client_process_.IsValid());
107 return client_process_.IsValid();
110 #if defined(OS_POSIX)
112 bool IPCTestBase::StartClient() {
113 return StartClientWithFD(channel_
114 ? channel_->GetClientFileDescriptor()
115 : channel_proxy_->GetClientFileDescriptor());
118 bool IPCTestBase::StartClientWithFD(int ipcfd) {
119 DCHECK(!client_process_.IsValid());
121 base::FileHandleMappingVector fds_to_map;
122 if (ipcfd > -1)
123 fds_to_map.push_back(std::pair<int, int>(ipcfd,
124 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor));
125 base::LaunchOptions options;
126 options.fds_to_remap = &fds_to_map;
127 client_process_ = SpawnChildWithOptions(GetTestMainName(), options);
129 return DidStartClient();
132 #elif defined(OS_WIN)
134 bool IPCTestBase::StartClient() {
135 DCHECK(!client_process_.IsValid());
136 client_process_ = SpawnChild(GetTestMainName());
137 return DidStartClient();
140 #endif
142 bool IPCTestBase::WaitForClientShutdown() {
143 DCHECK(client_process_.IsValid());
145 int exit_code;
146 bool rv = client_process_.WaitForExitWithTimeout(
147 base::TimeDelta::FromSeconds(5), &exit_code);
148 client_process_.Close();
149 return rv;
152 IPC::ChannelHandle IPCTestBase::GetTestChannelHandle() {
153 return GetChannelName(test_client_name_);
156 scoped_refptr<base::SequencedTaskRunner> IPCTestBase::task_runner() {
157 return message_loop_->task_runner();
160 scoped_ptr<IPC::ChannelFactory> IPCTestBase::CreateChannelFactory(
161 const IPC::ChannelHandle& handle,
162 base::SequencedTaskRunner* runner) {
163 return IPC::ChannelFactory::Create(handle, IPC::Channel::MODE_SERVER,
164 nullptr);