OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <sys/socket.h> | 5 #include <sys/socket.h> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/location.h" |
9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
10 #include "base/posix/eintr_wrapper.h" | 11 #include "base/posix/eintr_wrapper.h" |
| 12 #include "base/single_thread_task_runner.h" |
11 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
12 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
13 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
14 #include "ipc/unix_domain_socket_util.h" | 16 #include "ipc/unix_domain_socket_util.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 | 18 |
17 namespace { | 19 namespace { |
18 | 20 |
19 class SocketAcceptor : public base::MessageLoopForIO::Watcher { | 21 class SocketAcceptor : public base::MessageLoopForIO::Watcher { |
20 public: | 22 public: |
21 SocketAcceptor(int fd, base::MessageLoopProxy* target_thread) | 23 SocketAcceptor(int fd, base::SingleThreadTaskRunner* target_thread) |
22 : server_fd_(-1), | 24 : server_fd_(-1), |
23 target_thread_(target_thread), | 25 target_thread_(target_thread), |
24 started_watching_event_(false, false), | 26 started_watching_event_(false, false), |
25 accepted_event_(false, false) { | 27 accepted_event_(false, false) { |
26 target_thread->PostTask(FROM_HERE, | 28 target_thread->PostTask(FROM_HERE, |
27 base::Bind(&SocketAcceptor::StartWatching, base::Unretained(this), fd)); | 29 base::Bind(&SocketAcceptor::StartWatching, base::Unretained(this), fd)); |
28 } | 30 } |
29 | 31 |
30 ~SocketAcceptor() override { | 32 ~SocketAcceptor() override { |
31 Close(); | 33 Close(); |
(...skipping 30 matching lines...) Expand all Loading... |
62 } | 64 } |
63 void OnFileCanReadWithoutBlocking(int fd) override { | 65 void OnFileCanReadWithoutBlocking(int fd) override { |
64 ASSERT_EQ(-1, server_fd_); | 66 ASSERT_EQ(-1, server_fd_); |
65 IPC::ServerAcceptConnection(fd, &server_fd_); | 67 IPC::ServerAcceptConnection(fd, &server_fd_); |
66 watcher_->StopWatchingFileDescriptor(); | 68 watcher_->StopWatchingFileDescriptor(); |
67 accepted_event_.Signal(); | 69 accepted_event_.Signal(); |
68 } | 70 } |
69 void OnFileCanWriteWithoutBlocking(int fd) override {} | 71 void OnFileCanWriteWithoutBlocking(int fd) override {} |
70 | 72 |
71 int server_fd_; | 73 int server_fd_; |
72 base::MessageLoopProxy* target_thread_; | 74 base::SingleThreadTaskRunner* target_thread_; |
73 scoped_ptr<base::MessageLoopForIO::FileDescriptorWatcher> watcher_; | 75 scoped_ptr<base::MessageLoopForIO::FileDescriptorWatcher> watcher_; |
74 base::WaitableEvent started_watching_event_; | 76 base::WaitableEvent started_watching_event_; |
75 base::WaitableEvent accepted_event_; | 77 base::WaitableEvent accepted_event_; |
76 | 78 |
77 DISALLOW_COPY_AND_ASSIGN(SocketAcceptor); | 79 DISALLOW_COPY_AND_ASSIGN(SocketAcceptor); |
78 }; | 80 }; |
79 | 81 |
80 const base::FilePath GetChannelDir() { | 82 const base::FilePath GetChannelDir() { |
81 base::FilePath tmp_dir; | 83 base::FilePath tmp_dir; |
82 PathService::Get(base::DIR_TEMP, &tmp_dir); | 84 PathService::Get(base::DIR_TEMP, &tmp_dir); |
(...skipping 13 matching lines...) Expand all Loading... |
96 worker_.StartWithOptions(options); | 98 worker_.StartWithOptions(options); |
97 } | 99 } |
98 | 100 |
99 bool CreateServerSocket() { | 101 bool CreateServerSocket() { |
100 IPC::CreateServerUnixDomainSocket(socket_name_, &server_listen_fd_); | 102 IPC::CreateServerUnixDomainSocket(socket_name_, &server_listen_fd_); |
101 if (server_listen_fd_ < 0) | 103 if (server_listen_fd_ < 0) |
102 return false; | 104 return false; |
103 struct stat socket_stat; | 105 struct stat socket_stat; |
104 stat(socket_name_.value().c_str(), &socket_stat); | 106 stat(socket_name_.value().c_str(), &socket_stat); |
105 EXPECT_TRUE(S_ISSOCK(socket_stat.st_mode)); | 107 EXPECT_TRUE(S_ISSOCK(socket_stat.st_mode)); |
106 acceptor_.reset(new SocketAcceptor(server_listen_fd_, | 108 acceptor_.reset( |
107 worker_.message_loop_proxy().get())); | 109 new SocketAcceptor(server_listen_fd_, worker_.task_runner().get())); |
108 acceptor_->WaitUntilReady(); | 110 acceptor_->WaitUntilReady(); |
109 return true; | 111 return true; |
110 } | 112 } |
111 | 113 |
112 bool CreateClientSocket() { | 114 bool CreateClientSocket() { |
113 DCHECK(server_listen_fd_ >= 0); | 115 DCHECK(server_listen_fd_ >= 0); |
114 IPC::CreateClientUnixDomainSocket(socket_name_, &client_fd_); | 116 IPC::CreateClientUnixDomainSocket(socket_name_, &client_fd_); |
115 if (client_fd_ < 0) | 117 if (client_fd_ < 0) |
116 return false; | 118 return false; |
117 acceptor_->WaitForAccept(); | 119 acceptor_->WaitForAccept(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 HANDLE_EINTR(send(connection.client_fd(), buffer, buf_len, 0)); | 164 HANDLE_EINTR(send(connection.client_fd(), buffer, buf_len, 0)); |
163 ASSERT_EQ(buf_len, sent_bytes); | 165 ASSERT_EQ(buf_len, sent_bytes); |
164 char recv_buf[sizeof(buffer)]; | 166 char recv_buf[sizeof(buffer)]; |
165 size_t received_bytes = | 167 size_t received_bytes = |
166 HANDLE_EINTR(recv(connection.server_fd(), recv_buf, buf_len, 0)); | 168 HANDLE_EINTR(recv(connection.server_fd(), recv_buf, buf_len, 0)); |
167 ASSERT_EQ(buf_len, received_bytes); | 169 ASSERT_EQ(buf_len, received_bytes); |
168 ASSERT_EQ(0, memcmp(recv_buf, buffer, buf_len)); | 170 ASSERT_EQ(0, memcmp(recv_buf, buffer, buf_len)); |
169 } | 171 } |
170 | 172 |
171 } // namespace | 173 } // namespace |
OLD | NEW |