Move content_settings_pattern and content_settings_pattern_parser to the content_sett...
[chromium-blink-merge.git] / ipc / ipc_test_base.cc
blob80ddd2da2ea6c2842236a85a12392086086c0eea
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 "ipc/ipc_test_base.h"
9 #include "base/command_line.h"
10 #include "base/process/kill.h"
11 #include "base/threading/thread.h"
12 #include "base/time/time.h"
13 #include "ipc/ipc_descriptors.h"
15 #if defined(OS_POSIX)
16 #include "base/posix/global_descriptors.h"
17 #endif
19 // static
20 std::string IPCTestBase::GetChannelName(const std::string& test_client_name) {
21 DCHECK(!test_client_name.empty());
22 return test_client_name + "__Channel";
25 IPCTestBase::IPCTestBase()
26 : client_process_(base::kNullProcessHandle) {
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 return CreateChannelFromChannelHandle(GetChannelName(test_client_name_),
56 listener);
59 bool IPCTestBase::ConnectChannel() {
60 CHECK(channel_.get());
61 return channel_->Connect();
64 scoped_ptr<IPC::Channel> IPCTestBase::ReleaseChannel() {
65 return channel_.Pass();
68 void IPCTestBase::SetChannel(scoped_ptr<IPC::Channel> channel) {
69 channel_ = channel.Pass();
73 void IPCTestBase::DestroyChannel() {
74 DCHECK(channel_.get());
75 channel_.reset();
78 void IPCTestBase::CreateChannelFromChannelHandle(
79 const IPC::ChannelHandle& channel_handle,
80 IPC::Listener* listener) {
81 CHECK(!channel_.get());
82 CHECK(!channel_proxy_.get());
83 channel_ = IPC::Channel::CreateServer(channel_handle, 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(GetChannelName(test_client_name_),
92 IPC::Channel::MODE_SERVER,
93 listener,
94 ipc_task_runner);
97 void IPCTestBase::DestroyChannelProxy() {
98 CHECK(channel_proxy_.get());
99 channel_proxy_.reset();
102 bool IPCTestBase::StartClient() {
103 DCHECK(client_process_ == base::kNullProcessHandle);
105 std::string test_main = test_client_name_ + "TestClientMain";
107 #if defined(OS_WIN)
108 client_process_ = SpawnChild(test_main);
109 #elif defined(OS_POSIX)
110 base::FileHandleMappingVector fds_to_map;
111 const int ipcfd = channel_.get()
112 ? channel_->GetClientFileDescriptor()
113 : channel_proxy_->GetClientFileDescriptor();
114 if (ipcfd > -1)
115 fds_to_map.push_back(std::pair<int, int>(ipcfd,
116 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor));
117 base::LaunchOptions options;
118 options.fds_to_remap = &fds_to_map;
119 client_process_ = SpawnChildWithOptions(test_main, options);
120 #endif
122 return client_process_ != base::kNullProcessHandle;
125 bool IPCTestBase::WaitForClientShutdown() {
126 DCHECK(client_process_ != base::kNullProcessHandle);
128 bool rv = base::WaitForSingleProcess(client_process_,
129 base::TimeDelta::FromSeconds(5));
130 base::CloseProcessHandle(client_process_);
131 client_process_ = base::kNullProcessHandle;
132 return rv;
135 scoped_refptr<base::TaskRunner> IPCTestBase::task_runner() {
136 return message_loop_->message_loop_proxy();