Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ipc / ipc_test_base.cc
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.
4
5 #include "build/build_config.h"
6
7 #include "ipc/ipc_test_base.h"
8
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"
14
15 #if defined(OS_POSIX)
16 #include "base/posix/global_descriptors.h"
17 #endif
18
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";
23 }
24
25 IPCTestBase::IPCTestBase()
26     : client_process_(base::kNullProcessHandle) {
27 }
28
29 IPCTestBase::~IPCTestBase() {
30 }
31
32 void IPCTestBase::SetUp() {
33   MultiProcessTest::SetUp();
34
35   // Construct a fresh IO Message loop for the duration of each test.
36   DCHECK(!message_loop_.get());
37   message_loop_.reset(new base::MessageLoopForIO());
38 }
39
40 void IPCTestBase::TearDown() {
41   DCHECK(message_loop_.get());
42   message_loop_.reset();
43   MultiProcessTest::TearDown();
44 }
45
46 void IPCTestBase::Init(const std::string& test_client_name) {
47   DCHECK(!test_client_name.empty());
48   DCHECK(test_client_name_.empty());
49   test_client_name_ = test_client_name;
50 }
51
52 void IPCTestBase::CreateChannel(IPC::Listener* listener) {
53   return CreateChannelFromChannelHandle(GetChannelName(test_client_name_),
54                                         listener);
55 }
56
57 bool IPCTestBase::ConnectChannel() {
58   CHECK(channel_.get());
59   return channel_->Connect();
60 }
61
62 scoped_ptr<IPC::Channel> IPCTestBase::ReleaseChannel() {
63   return channel_.Pass();
64 }
65
66 void IPCTestBase::SetChannel(scoped_ptr<IPC::Channel> channel) {
67   channel_ = channel.Pass();
68 }
69
70
71 void IPCTestBase::DestroyChannel() {
72   DCHECK(channel_.get());
73   channel_.reset();
74 }
75
76 void IPCTestBase::CreateChannelFromChannelHandle(
77     const IPC::ChannelHandle& channel_handle,
78     IPC::Listener* listener) {
79   CHECK(!channel_.get());
80   CHECK(!channel_proxy_.get());
81   channel_ = IPC::Channel::CreateServer(channel_handle, listener);
82 }
83
84 void IPCTestBase::CreateChannelProxy(
85     IPC::Listener* listener,
86     base::SingleThreadTaskRunner* ipc_task_runner) {
87   CHECK(!channel_.get());
88   CHECK(!channel_proxy_.get());
89   channel_proxy_ = IPC::ChannelProxy::Create(GetChannelName(test_client_name_),
90                                              IPC::Channel::MODE_SERVER,
91                                              listener,
92                                              ipc_task_runner);
93 }
94
95 void IPCTestBase::DestroyChannelProxy() {
96   CHECK(channel_proxy_.get());
97   channel_proxy_.reset();
98 }
99
100 bool IPCTestBase::StartClient() {
101   DCHECK(client_process_ == base::kNullProcessHandle);
102
103   std::string test_main = test_client_name_ + "TestClientMain";
104
105 #if defined(OS_WIN)
106   client_process_ = SpawnChild(test_main);
107 #elif defined(OS_POSIX)
108   base::FileHandleMappingVector fds_to_map;
109   const int ipcfd = channel_.get()
110       ? channel_->GetClientFileDescriptor()
111       : channel_proxy_->GetClientFileDescriptor();
112   if (ipcfd > -1)
113     fds_to_map.push_back(std::pair<int, int>(ipcfd,
114         kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor));
115   base::LaunchOptions options;
116   options.fds_to_remap = &fds_to_map;
117   client_process_ = SpawnChildWithOptions(test_main, options);
118 #endif
119
120   return client_process_ != base::kNullProcessHandle;
121 }
122
123 bool IPCTestBase::WaitForClientShutdown() {
124   DCHECK(client_process_ != base::kNullProcessHandle);
125
126   bool rv = base::WaitForSingleProcess(client_process_,
127                                        base::TimeDelta::FromSeconds(5));
128   base::CloseProcessHandle(client_process_);
129   client_process_ = base::kNullProcessHandle;
130   return rv;
131 }
132
133 scoped_refptr<base::TaskRunner> IPCTestBase::io_thread_task_runner() {
134   return message_loop_->message_loop_proxy();
135 }