OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #include "ipc/ipc_test_base.h" | 7 #include "ipc/ipc_test_base.h" |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/process/kill.h" | 10 #include "base/process/kill.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 scoped_ptr<base::MessageLoop> message_loop) { | 45 scoped_ptr<base::MessageLoop> message_loop) { |
46 DCHECK(!test_client_name.empty()); | 46 DCHECK(!test_client_name.empty()); |
47 DCHECK(test_client_name_.empty()); | 47 DCHECK(test_client_name_.empty()); |
48 DCHECK(!message_loop_); | 48 DCHECK(!message_loop_); |
49 | 49 |
50 test_client_name_ = test_client_name; | 50 test_client_name_ = test_client_name; |
51 message_loop_ = message_loop.Pass(); | 51 message_loop_ = message_loop.Pass(); |
52 } | 52 } |
53 | 53 |
54 void IPCTestBase::CreateChannel(IPC::Listener* listener) { | 54 void IPCTestBase::CreateChannel(IPC::Listener* listener) { |
55 CreateChannelFromChannelHandle( | 55 CreateChannelFromChannelHandle(GetTestChannelHandle(), listener); |
56 GetChannelName(test_client_name_), listener); | |
57 } | 56 } |
58 | 57 |
59 bool IPCTestBase::ConnectChannel() { | 58 bool IPCTestBase::ConnectChannel() { |
60 CHECK(channel_.get()); | 59 CHECK(channel_.get()); |
61 return channel_->Connect(); | 60 return channel_->Connect(); |
62 } | 61 } |
63 | 62 |
64 scoped_ptr<IPC::Channel> IPCTestBase::ReleaseChannel() { | 63 scoped_ptr<IPC::Channel> IPCTestBase::ReleaseChannel() { |
65 return channel_.Pass(); | 64 return channel_.Pass(); |
66 } | 65 } |
(...skipping 16 matching lines...) Expand all Loading... |
83 channel_ = CreateChannelFactory( | 82 channel_ = CreateChannelFactory( |
84 channel_handle, task_runner().get())->BuildChannel(listener); | 83 channel_handle, task_runner().get())->BuildChannel(listener); |
85 } | 84 } |
86 | 85 |
87 void IPCTestBase::CreateChannelProxy( | 86 void IPCTestBase::CreateChannelProxy( |
88 IPC::Listener* listener, | 87 IPC::Listener* listener, |
89 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { | 88 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { |
90 CHECK(!channel_.get()); | 89 CHECK(!channel_.get()); |
91 CHECK(!channel_proxy_.get()); | 90 CHECK(!channel_proxy_.get()); |
92 channel_proxy_ = IPC::ChannelProxy::Create( | 91 channel_proxy_ = IPC::ChannelProxy::Create( |
93 CreateChannelFactory(GetChannelName(test_client_name_), | 92 CreateChannelFactory(GetTestChannelHandle(), ipc_task_runner.get()), |
94 ipc_task_runner.get()), | |
95 listener, | 93 listener, |
96 ipc_task_runner); | 94 ipc_task_runner); |
97 } | 95 } |
98 | 96 |
99 void IPCTestBase::DestroyChannelProxy() { | 97 void IPCTestBase::DestroyChannelProxy() { |
100 CHECK(channel_proxy_.get()); | 98 CHECK(channel_proxy_.get()); |
101 channel_proxy_.reset(); | 99 channel_proxy_.reset(); |
102 } | 100 } |
103 | 101 |
104 bool IPCTestBase::StartClient() { | 102 bool IPCTestBase::StartClient() { |
| 103 CHECK(channel_ || channel_proxy_); |
| 104 #if defined(OS_WIN) |
| 105 return StartClient(0); |
| 106 #elif defined(OS_POSIX) |
| 107 return StartClient(channel_ ? channel_->GetClientFileDescriptor() |
| 108 : channel_proxy_->GetClientFileDescriptor()); |
| 109 #else |
| 110 NOTREACHED(); |
| 111 return false; |
| 112 #endif |
| 113 } |
| 114 |
| 115 bool IPCTestBase::StartClient(int ipcfd) { |
105 DCHECK(client_process_ == base::kNullProcessHandle); | 116 DCHECK(client_process_ == base::kNullProcessHandle); |
106 | 117 |
107 std::string test_main = test_client_name_ + "TestClientMain"; | 118 std::string test_main = test_client_name_ + "TestClientMain"; |
108 | 119 |
109 #if defined(OS_WIN) | 120 #if defined(OS_WIN) |
110 client_process_ = SpawnChild(test_main); | 121 client_process_ = SpawnChild(test_main); |
111 #elif defined(OS_POSIX) | 122 #elif defined(OS_POSIX) |
112 base::FileHandleMappingVector fds_to_map; | 123 base::FileHandleMappingVector fds_to_map; |
113 const int ipcfd = channel_.get() | |
114 ? channel_->GetClientFileDescriptor() | |
115 : channel_proxy_->GetClientFileDescriptor(); | |
116 if (ipcfd > -1) | 124 if (ipcfd > -1) |
117 fds_to_map.push_back(std::pair<int, int>(ipcfd, | 125 fds_to_map.push_back(std::pair<int, int>(ipcfd, |
118 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); | 126 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor)); |
119 base::LaunchOptions options; | 127 base::LaunchOptions options; |
120 options.fds_to_remap = &fds_to_map; | 128 options.fds_to_remap = &fds_to_map; |
121 client_process_ = SpawnChildWithOptions(test_main, options); | 129 client_process_ = SpawnChildWithOptions(test_main, options); |
122 #endif | 130 #endif |
123 | 131 |
| 132 if (channel_) |
| 133 channel_->OnClientLaunched(client_process_); |
| 134 if (channel_proxy_) |
| 135 channel_proxy_->OnClientLaunched(client_process_); |
| 136 |
124 return client_process_ != base::kNullProcessHandle; | 137 return client_process_ != base::kNullProcessHandle; |
125 } | 138 } |
126 | 139 |
127 bool IPCTestBase::WaitForClientShutdown() { | 140 bool IPCTestBase::WaitForClientShutdown() { |
128 DCHECK(client_process_ != base::kNullProcessHandle); | 141 DCHECK(client_process_ != base::kNullProcessHandle); |
129 | 142 |
130 bool rv = base::WaitForSingleProcess(client_process_, | 143 bool rv = base::WaitForSingleProcess(client_process_, |
131 base::TimeDelta::FromSeconds(5)); | 144 base::TimeDelta::FromSeconds(5)); |
132 base::CloseProcessHandle(client_process_); | 145 base::CloseProcessHandle(client_process_); |
133 client_process_ = base::kNullProcessHandle; | 146 client_process_ = base::kNullProcessHandle; |
134 return rv; | 147 return rv; |
135 } | 148 } |
136 | 149 |
| 150 IPC::ChannelHandle IPCTestBase::GetTestChannelHandle() { |
| 151 return GetChannelName(test_client_name_); |
| 152 } |
| 153 |
137 scoped_refptr<base::TaskRunner> IPCTestBase::task_runner() { | 154 scoped_refptr<base::TaskRunner> IPCTestBase::task_runner() { |
138 return message_loop_->message_loop_proxy(); | 155 return message_loop_->message_loop_proxy(); |
139 } | 156 } |
140 | 157 |
141 scoped_ptr<IPC::ChannelFactory> IPCTestBase::CreateChannelFactory( | 158 scoped_ptr<IPC::ChannelFactory> IPCTestBase::CreateChannelFactory( |
142 const IPC::ChannelHandle& handle, | 159 const IPC::ChannelHandle& handle, |
143 base::TaskRunner* runner) { | 160 base::TaskRunner* runner) { |
144 return IPC::ChannelFactory::Create(handle, IPC::Channel::MODE_SERVER); | 161 return IPC::ChannelFactory::Create(handle, IPC::Channel::MODE_SERVER); |
145 } | 162 } |
OLD | NEW |