OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MOJO_SHELL_INCOMING_CONNECTION_LISTENER_H_ |
| 6 #define MOJO_SHELL_INCOMING_CONNECTION_LISTENER_H_ |
| 7 |
| 8 #include "base/callback_forward.h" |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/threading/thread_checker.h" |
| 12 #include "net/socket/socket_descriptor.h" |
| 13 #include "net/socket/unix_domain_server_socket_posix.h" |
| 14 |
| 15 namespace mojo { |
| 16 namespace shell { |
| 17 |
| 18 // Asynchronously listens for incoming connections on a unix domain |
| 19 // socket at the provided path. Expects the parent directory in the |
| 20 // path to exist. Must be run on an IO thread. |
| 21 class IncomingConnectionListener { |
| 22 public: |
| 23 class Delegate { |
| 24 public: |
| 25 Delegate(); |
| 26 virtual ~Delegate(); |
| 27 |
| 28 // Called when listening has started. rv is from net/base/net_error_list.h |
| 29 virtual void OnListening(int rv) = 0; |
| 30 |
| 31 // Called every time an incoming connection is accepted. The delegate |
| 32 // takes ownership of incoming. |
| 33 virtual void OnConnection(net::SocketDescriptor incoming) = 0; |
| 34 }; |
| 35 |
| 36 IncomingConnectionListener(const base::FilePath& socket_path, |
| 37 Delegate* delegate); |
| 38 virtual ~IncomingConnectionListener(); |
| 39 |
| 40 // Attempts to bind a unix domain socket, set up for listening, at |
| 41 // socket_path_. |
| 42 // Regardless of success or failure, calls delegate->OnListening() with a |
| 43 // status code. If the socket was successfully created, begins asynchronously |
| 44 // waiting to accept incoming connections. |
| 45 void StartListening(); |
| 46 |
| 47 private: |
| 48 // Tells listen_socket_ to perform a non-blocking accept(). It may succeed |
| 49 // or fail immediately, or asynchronously wait for a later connection attempt. |
| 50 // Regardless, when it returns a definitive result (OK or a failing error), |
| 51 // calls OnAccept(). |
| 52 void Accept(); |
| 53 |
| 54 // If rv indicates success, incoming_socket_ should be populated with a |
| 55 // connected FD. Hands this off to delegate->OnConnection() and goes |
| 56 // back to non-blocking accept(). |
| 57 // Upon error, logs the error and goes back to non-blocking accept(). |
| 58 void OnAccept(int rv); |
| 59 |
| 60 Delegate* const delegate_; |
| 61 |
| 62 const base::FilePath socket_path_; |
| 63 net::UnixDomainServerSocket listen_socket_; |
| 64 base::ThreadChecker listen_thread_checker_; |
| 65 |
| 66 net::SocketDescriptor incoming_socket_; |
| 67 |
| 68 base::WeakPtrFactory<IncomingConnectionListener> weak_ptr_factory_; |
| 69 }; |
| 70 |
| 71 } // namespace shell |
| 72 } // namespace mojo |
| 73 |
| 74 #endif // MOJO_SHELL_INCOMING_CONNECTION_LISTENER_H_ |
OLD | NEW |