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/ipc_channel_factory.h" | 5 #include "apps/app_shim/unix_domain_socket_acceptor.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/scoped_file.h" | 8 #include "base/files/scoped_file.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "ipc/unix_domain_socket_util.h" | 10 #include "ipc/unix_domain_socket_util.h" |
11 | 11 |
12 namespace IPC { | 12 namespace apps { |
13 | 13 |
14 ChannelFactory::ChannelFactory(const base::FilePath& path, Delegate* delegate) | 14 UnixDomainSocketAcceptor::UnixDomainSocketAcceptor(const base::FilePath& path, |
| 15 Delegate* delegate) |
15 : path_(path), delegate_(delegate), listen_fd_(-1) { | 16 : path_(path), delegate_(delegate), listen_fd_(-1) { |
16 DCHECK(delegate_); | 17 DCHECK(delegate_); |
17 CreateSocket(); | 18 CreateSocket(); |
18 } | 19 } |
19 | 20 |
20 ChannelFactory::~ChannelFactory() { | 21 UnixDomainSocketAcceptor::~UnixDomainSocketAcceptor() { |
21 Close(); | 22 Close(); |
22 } | 23 } |
23 | 24 |
24 bool ChannelFactory::CreateSocket() { | 25 bool UnixDomainSocketAcceptor::CreateSocket() { |
25 DCHECK(listen_fd_ < 0); | 26 DCHECK(listen_fd_ < 0); |
26 | 27 |
27 // Create the socket. | 28 // Create the socket. |
28 return CreateServerUnixDomainSocket(path_, &listen_fd_); | 29 return IPC::CreateServerUnixDomainSocket(path_, &listen_fd_); |
29 } | 30 } |
30 | 31 |
31 bool ChannelFactory::Listen() { | 32 bool UnixDomainSocketAcceptor::Listen() { |
32 if (listen_fd_ < 0) | 33 if (listen_fd_ < 0) |
33 return false; | 34 return false; |
34 | 35 |
35 // Watch the fd for connections, and turn any connections into | 36 // Watch the fd for connections, and turn any connections into |
36 // active sockets. | 37 // active sockets. |
37 base::MessageLoopForIO::current()->WatchFileDescriptor( | 38 base::MessageLoopForIO::current()->WatchFileDescriptor( |
38 listen_fd_, | 39 listen_fd_, |
39 true, | 40 true, |
40 base::MessageLoopForIO::WATCH_READ, | 41 base::MessageLoopForIO::WATCH_READ, |
41 &server_listen_connection_watcher_, | 42 &server_listen_connection_watcher_, |
42 this); | 43 this); |
43 return true; | 44 return true; |
44 } | 45 } |
45 | 46 |
46 // Called by libevent when we can read from the fd without blocking. | 47 // Called by libevent when we can read from the fd without blocking. |
47 void ChannelFactory::OnFileCanReadWithoutBlocking(int fd) { | 48 void UnixDomainSocketAcceptor::OnFileCanReadWithoutBlocking(int fd) { |
48 DCHECK(fd == listen_fd_); | 49 DCHECK(fd == listen_fd_); |
49 int new_fd = -1; | 50 int new_fd = -1; |
50 if (!ServerAcceptConnection(listen_fd_, &new_fd)) { | 51 if (!IPC::ServerAcceptConnection(listen_fd_, &new_fd)) { |
51 Close(); | 52 Close(); |
52 delegate_->OnListenError(); | 53 delegate_->OnListenError(); |
53 return; | 54 return; |
54 } | 55 } |
55 base::ScopedFD scoped_fd(new_fd); | 56 base::ScopedFD scoped_fd(new_fd); |
56 | 57 |
57 if (!scoped_fd.is_valid()) { | 58 if (!scoped_fd.is_valid()) { |
58 // The accept() failed, but not in such a way that the factory needs to be | 59 // The accept() failed, but not in such a way that the factory needs to be |
59 // shut down. | 60 // shut down. |
60 return; | 61 return; |
61 } | 62 } |
62 | 63 |
63 // Verify that the IPC channel peer is running as the same user. | 64 // Verify that the IPC channel peer is running as the same user. |
64 if (!IsPeerAuthorized(scoped_fd.get())) | 65 if (!IPC::IsPeerAuthorized(scoped_fd.get())) |
65 return; | 66 return; |
66 | 67 |
67 ChannelHandle handle(std::string(), | 68 IPC::ChannelHandle handle(std::string(), |
68 base::FileDescriptor(scoped_fd.release(), true)); | 69 base::FileDescriptor(scoped_fd.release(), true)); |
69 delegate_->OnClientConnected(handle); | 70 delegate_->OnClientConnected(handle); |
70 } | 71 } |
71 | 72 |
72 void ChannelFactory::OnFileCanWriteWithoutBlocking(int fd) { | 73 void UnixDomainSocketAcceptor::OnFileCanWriteWithoutBlocking(int fd) { |
73 NOTREACHED() << "Listen fd should never be writable."; | 74 NOTREACHED() << "Listen fd should never be writable."; |
74 } | 75 } |
75 | 76 |
76 void ChannelFactory::Close() { | 77 void UnixDomainSocketAcceptor::Close() { |
77 if (listen_fd_ < 0) | 78 if (listen_fd_ < 0) |
78 return; | 79 return; |
79 if (IGNORE_EINTR(close(listen_fd_)) < 0) | 80 if (IGNORE_EINTR(close(listen_fd_)) < 0) |
80 PLOG(ERROR) << "close"; | 81 PLOG(ERROR) << "close"; |
81 listen_fd_ = -1; | 82 listen_fd_ = -1; |
82 if (unlink(path_.value().c_str()) < 0) | 83 if (unlink(path_.value().c_str()) < 0) |
83 PLOG(ERROR) << "unlink"; | 84 PLOG(ERROR) << "unlink"; |
84 | 85 |
85 // Unregister libevent for the listening socket and close it. | 86 // Unregister libevent for the listening socket and close it. |
86 server_listen_connection_watcher_.StopWatchingFileDescriptor(); | 87 server_listen_connection_watcher_.StopWatchingFileDescriptor(); |
87 } | 88 } |
88 | 89 |
89 } // namespace IPC | 90 } // namespace apps |
OLD | NEW |