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 #if defined(OS_POSIX) | 7 #if defined(OS_POSIX) |
8 #if defined(OS_MACOSX) | 8 #if defined(OS_MACOSX) |
9 extern "C" { | 9 extern "C" { |
10 #include <sandbox.h> | 10 #include <sandbox.h> |
11 } | 11 } |
12 #endif | 12 #endif |
13 #include <fcntl.h> | 13 #include <fcntl.h> |
14 #include <sys/socket.h> | 14 #include <sys/socket.h> |
15 #include <sys/stat.h> | 15 #include <sys/stat.h> |
16 #include <unistd.h> | 16 #include <unistd.h> |
17 | 17 |
18 #include <queue> | 18 #include <queue> |
19 | 19 |
20 #include "base/callback.h" | 20 #include "base/callback.h" |
21 #include "base/file_descriptor_posix.h" | 21 #include "base/file_descriptor_posix.h" |
22 #include "base/message_loop/message_loop.h" | 22 #include "base/location.h" |
23 #include "base/pickle.h" | 23 #include "base/pickle.h" |
24 #include "base/posix/eintr_wrapper.h" | 24 #include "base/posix/eintr_wrapper.h" |
| 25 #include "base/single_thread_task_runner.h" |
25 #include "base/synchronization/waitable_event.h" | 26 #include "base/synchronization/waitable_event.h" |
26 #include "ipc/ipc_message_attachment_set.h" | 27 #include "ipc/ipc_message_attachment_set.h" |
27 #include "ipc/ipc_message_utils.h" | 28 #include "ipc/ipc_message_utils.h" |
28 #include "ipc/ipc_test_base.h" | 29 #include "ipc/ipc_test_base.h" |
29 | 30 |
30 #if defined(OS_POSIX) | 31 #if defined(OS_POSIX) |
31 #include "base/macros.h" | 32 #include "base/macros.h" |
32 #endif | 33 #endif |
33 | 34 |
34 namespace { | 35 namespace { |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 } | 244 } |
244 | 245 |
245 void Init() { | 246 void Init() { |
246 IPC::ChannelHandle in_handle("IN"); | 247 IPC::ChannelHandle in_handle("IN"); |
247 in = IPC::Channel::CreateServer(in_handle, &null_listener_); | 248 in = IPC::Channel::CreateServer(in_handle, &null_listener_); |
248 IPC::ChannelHandle out_handle( | 249 IPC::ChannelHandle out_handle( |
249 "OUT", base::FileDescriptor(in->TakeClientFileDescriptor())); | 250 "OUT", base::FileDescriptor(in->TakeClientFileDescriptor())); |
250 out = IPC::Channel::CreateClient(out_handle, &cb_listener_); | 251 out = IPC::Channel::CreateClient(out_handle, &cb_listener_); |
251 // PostTask the connect calls to make sure the callbacks happens | 252 // PostTask the connect calls to make sure the callbacks happens |
252 // on the right threads. | 253 // on the right threads. |
253 in_thread_->message_loop()->PostTask( | 254 in_thread_->task_runner()->PostTask( |
254 FROM_HERE, | 255 FROM_HERE, base::Bind(&PipeChannelHelper::Connect, in.get())); |
255 base::Bind(&PipeChannelHelper::Connect, in.get())); | 256 out_thread_->task_runner()->PostTask( |
256 out_thread_->message_loop()->PostTask( | 257 FROM_HERE, base::Bind(&PipeChannelHelper::Connect, out.get())); |
257 FROM_HERE, | |
258 base::Bind(&PipeChannelHelper::Connect, out.get())); | |
259 } | 258 } |
260 | 259 |
261 static void DestroyChannel(scoped_ptr<IPC::Channel> *c, | 260 static void DestroyChannel(scoped_ptr<IPC::Channel> *c, |
262 base::WaitableEvent *event) { | 261 base::WaitableEvent *event) { |
263 c->reset(0); | 262 c->reset(0); |
264 event->Signal(); | 263 event->Signal(); |
265 } | 264 } |
266 | 265 |
267 ~PipeChannelHelper() { | 266 ~PipeChannelHelper() { |
268 base::WaitableEvent a(true, false); | 267 base::WaitableEvent a(true, false); |
269 base::WaitableEvent b(true, false); | 268 base::WaitableEvent b(true, false); |
270 in_thread_->message_loop()->PostTask( | 269 in_thread_->task_runner()->PostTask( |
271 FROM_HERE, | 270 FROM_HERE, base::Bind(&PipeChannelHelper::DestroyChannel, &in, &a)); |
272 base::Bind(&PipeChannelHelper::DestroyChannel, &in, &a)); | 271 out_thread_->task_runner()->PostTask( |
273 out_thread_->message_loop()->PostTask( | 272 FROM_HERE, base::Bind(&PipeChannelHelper::DestroyChannel, &out, &b)); |
274 FROM_HERE, | |
275 base::Bind(&PipeChannelHelper::DestroyChannel, &out, &b)); | |
276 a.Wait(); | 273 a.Wait(); |
277 b.Wait(); | 274 b.Wait(); |
278 } | 275 } |
279 | 276 |
280 static void Connect(IPC::Channel *channel) { | 277 static void Connect(IPC::Channel *channel) { |
281 EXPECT_TRUE(channel->Connect()); | 278 EXPECT_TRUE(channel->Connect()); |
282 } | 279 } |
283 | 280 |
284 void Send(int fd) { | 281 void Send(int fd) { |
285 CHECK_EQ(base::MessageLoop::current(), in_thread_->message_loop()); | 282 CHECK_EQ(base::MessageLoop::current(), in_thread_->message_loop()); |
(...skipping 28 matching lines...) Expand all Loading... |
314 class IPCMultiSendingFdsTest : public testing::Test { | 311 class IPCMultiSendingFdsTest : public testing::Test { |
315 public: | 312 public: |
316 IPCMultiSendingFdsTest() : received_(true, false) {} | 313 IPCMultiSendingFdsTest() : received_(true, false) {} |
317 | 314 |
318 void Producer(PipeChannelHelper* dest, | 315 void Producer(PipeChannelHelper* dest, |
319 base::Thread* t, | 316 base::Thread* t, |
320 int pipes_to_send) { | 317 int pipes_to_send) { |
321 for (int i = 0; i < pipes_to_send; i++) { | 318 for (int i = 0; i < pipes_to_send; i++) { |
322 received_.Reset(); | 319 received_.Reset(); |
323 std::pair<int, int> pipe_fds = make_socket_pair(); | 320 std::pair<int, int> pipe_fds = make_socket_pair(); |
324 t->message_loop()->PostTask( | 321 t->task_runner()->PostTask( |
325 FROM_HERE, | 322 FROM_HERE, base::Bind(&PipeChannelHelper::Send, |
326 base::Bind(&PipeChannelHelper::Send, | 323 base::Unretained(dest), pipe_fds.second)); |
327 base::Unretained(dest), | |
328 pipe_fds.second)); | |
329 char tmp = 'x'; | 324 char tmp = 'x'; |
330 CHECK_EQ(1, HANDLE_EINTR(write(pipe_fds.first, &tmp, 1))); | 325 CHECK_EQ(1, HANDLE_EINTR(write(pipe_fds.first, &tmp, 1))); |
331 CHECK_EQ(0, IGNORE_EINTR(close(pipe_fds.first))); | 326 CHECK_EQ(0, IGNORE_EINTR(close(pipe_fds.first))); |
332 received_.Wait(); | 327 received_.Wait(); |
333 } | 328 } |
334 } | 329 } |
335 | 330 |
336 void ConsumerHandleFD(int fd) { | 331 void ConsumerHandleFD(int fd) { |
337 char tmp = 'y'; | 332 char tmp = 'y'; |
338 CHECK_EQ(1, HANDLE_EINTR(read(fd, &tmp, 1))); | 333 CHECK_EQ(1, HANDLE_EINTR(read(fd, &tmp, 1))); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 base::WaitableEvent received_; | 373 base::WaitableEvent received_; |
379 }; | 374 }; |
380 | 375 |
381 TEST_F(IPCMultiSendingFdsTest, StressTest) { | 376 TEST_F(IPCMultiSendingFdsTest, StressTest) { |
382 Run(); | 377 Run(); |
383 } | 378 } |
384 | 379 |
385 } // namespace | 380 } // namespace |
386 | 381 |
387 #endif // defined(OS_POSIX) | 382 #endif // defined(OS_POSIX) |
OLD | NEW |