| 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 "ipc/unix_domain_socket_util.h" | 5 #include "ipc/unix_domain_socket_util.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <sys/socket.h> | 8 #include <sys/socket.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 class SocketAcceptor : public base::MessageLoopForIO::Watcher { | 26 class SocketAcceptor : public base::MessageLoopForIO::Watcher { |
| 27 public: | 27 public: |
| 28 SocketAcceptor(int fd, base::SingleThreadTaskRunner* target_thread) | 28 SocketAcceptor(int fd, base::SingleThreadTaskRunner* target_thread) |
| 29 : server_fd_(-1), | 29 : server_fd_(-1), |
| 30 target_thread_(target_thread), | 30 target_thread_(target_thread), |
| 31 started_watching_event_( | 31 started_watching_event_( |
| 32 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 32 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 33 base::WaitableEvent::InitialState::NOT_SIGNALED), | 33 base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 34 stopped_watching_event_( |
| 35 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 36 base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 34 accepted_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 37 accepted_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 35 base::WaitableEvent::InitialState::NOT_SIGNALED) { | 38 base::WaitableEvent::InitialState::NOT_SIGNALED) { |
| 36 target_thread->PostTask(FROM_HERE, | 39 target_thread->PostTask(FROM_HERE, |
| 37 base::Bind(&SocketAcceptor::StartWatching, base::Unretained(this), fd)); | 40 base::Bind(&SocketAcceptor::StartWatching, base::Unretained(this), fd)); |
| 38 } | 41 } |
| 39 | 42 |
| 40 ~SocketAcceptor() override { | 43 ~SocketAcceptor() override { |
| 41 Close(); | 44 Close(); |
| 42 } | 45 } |
| 43 | 46 |
| 44 int server_fd() const { return server_fd_; } | 47 int server_fd() const { return server_fd_; } |
| 45 | 48 |
| 46 void WaitUntilReady() { | 49 void WaitUntilReady() { |
| 47 started_watching_event_.Wait(); | 50 started_watching_event_.Wait(); |
| 48 } | 51 } |
| 49 | 52 |
| 50 void WaitForAccept() { | 53 void WaitForAccept() { |
| 51 accepted_event_.Wait(); | 54 accepted_event_.Wait(); |
| 52 } | 55 } |
| 53 | 56 |
| 54 void Close() { | 57 void Close() { |
| 55 if (watcher_.get()) { | 58 if (watcher_.get()) { |
| 56 target_thread_->PostTask(FROM_HERE, | 59 target_thread_->PostTask(FROM_HERE, |
| 57 base::Bind(&SocketAcceptor::StopWatching, base::Unretained(this), | 60 base::Bind(&SocketAcceptor::StopWatching, base::Unretained(this), |
| 58 watcher_.release())); | 61 watcher_.release())); |
| 62 stopped_watching_event_.Wait(); |
| 59 } | 63 } |
| 60 } | 64 } |
| 61 | 65 |
| 62 private: | 66 private: |
| 63 void StartWatching(int fd) { | 67 void StartWatching(int fd) { |
| 64 watcher_.reset(new base::MessageLoopForIO::FileDescriptorWatcher); | 68 watcher_.reset(new base::MessageLoopForIO::FileDescriptorWatcher); |
| 65 base::MessageLoopForIO::current()->WatchFileDescriptor( | 69 base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 66 fd, true, base::MessageLoopForIO::WATCH_READ, watcher_.get(), this); | 70 fd, true, base::MessageLoopForIO::WATCH_READ, watcher_.get(), this); |
| 67 started_watching_event_.Signal(); | 71 started_watching_event_.Signal(); |
| 68 } | 72 } |
| 69 void StopWatching(base::MessageLoopForIO::FileDescriptorWatcher* watcher) { | 73 void StopWatching(base::MessageLoopForIO::FileDescriptorWatcher* watcher) { |
| 70 watcher->StopWatchingFileDescriptor(); | 74 watcher->StopWatchingFileDescriptor(); |
| 71 delete watcher; | 75 delete watcher; |
| 76 stopped_watching_event_.Signal(); |
| 72 } | 77 } |
| 73 void OnFileCanReadWithoutBlocking(int fd) override { | 78 void OnFileCanReadWithoutBlocking(int fd) override { |
| 74 ASSERT_EQ(-1, server_fd_); | 79 ASSERT_EQ(-1, server_fd_); |
| 75 IPC::ServerOnConnect(fd, &server_fd_); | 80 IPC::ServerOnConnect(fd, &server_fd_); |
| 76 watcher_->StopWatchingFileDescriptor(); | 81 watcher_->StopWatchingFileDescriptor(); |
| 77 accepted_event_.Signal(); | 82 accepted_event_.Signal(); |
| 78 } | 83 } |
| 79 void OnFileCanWriteWithoutBlocking(int fd) override {} | 84 void OnFileCanWriteWithoutBlocking(int fd) override {} |
| 80 | 85 |
| 81 int server_fd_; | 86 int server_fd_; |
| 82 base::SingleThreadTaskRunner* target_thread_; | 87 base::SingleThreadTaskRunner* target_thread_; |
| 83 std::unique_ptr<base::MessageLoopForIO::FileDescriptorWatcher> watcher_; | 88 std::unique_ptr<base::MessageLoopForIO::FileDescriptorWatcher> watcher_; |
| 84 base::WaitableEvent started_watching_event_; | 89 base::WaitableEvent started_watching_event_; |
| 90 base::WaitableEvent stopped_watching_event_; |
| 85 base::WaitableEvent accepted_event_; | 91 base::WaitableEvent accepted_event_; |
| 86 | 92 |
| 87 DISALLOW_COPY_AND_ASSIGN(SocketAcceptor); | 93 DISALLOW_COPY_AND_ASSIGN(SocketAcceptor); |
| 88 }; | 94 }; |
| 89 | 95 |
| 90 const base::FilePath GetChannelDir() { | 96 const base::FilePath GetChannelDir() { |
| 91 base::FilePath tmp_dir; | 97 base::FilePath tmp_dir; |
| 92 PathService::Get(base::DIR_TEMP, &tmp_dir); | 98 PathService::Get(base::DIR_TEMP, &tmp_dir); |
| 93 return tmp_dir; | 99 return tmp_dir; |
| 94 } | 100 } |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 HANDLE_EINTR(send(connection.client_fd(), buffer, buf_len, 0)); | 178 HANDLE_EINTR(send(connection.client_fd(), buffer, buf_len, 0)); |
| 173 ASSERT_EQ(buf_len, sent_bytes); | 179 ASSERT_EQ(buf_len, sent_bytes); |
| 174 char recv_buf[sizeof(buffer)]; | 180 char recv_buf[sizeof(buffer)]; |
| 175 size_t received_bytes = | 181 size_t received_bytes = |
| 176 HANDLE_EINTR(recv(connection.server_fd(), recv_buf, buf_len, 0)); | 182 HANDLE_EINTR(recv(connection.server_fd(), recv_buf, buf_len, 0)); |
| 177 ASSERT_EQ(buf_len, received_bytes); | 183 ASSERT_EQ(buf_len, received_bytes); |
| 178 ASSERT_EQ(0, memcmp(recv_buf, buffer, buf_len)); | 184 ASSERT_EQ(0, memcmp(recv_buf, buffer, buf_len)); |
| 179 } | 185 } |
| 180 | 186 |
| 181 } // namespace | 187 } // namespace |
| OLD | NEW |