Index: mojo/shell/incoming_connection_listener.h |
diff --git a/mojo/shell/incoming_connection_listener.h b/mojo/shell/incoming_connection_listener.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2b280e18d9b920ae2bad972f3d1e312667d30ad0 |
--- /dev/null |
+++ b/mojo/shell/incoming_connection_listener.h |
@@ -0,0 +1,74 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef MOJO_SHELL_INCOMING_CONNECTION_LISTENER_H_ |
+#define MOJO_SHELL_INCOMING_CONNECTION_LISTENER_H_ |
+ |
+#include "base/callback_forward.h" |
+#include "base/files/file_path.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/threading/thread_checker.h" |
+#include "net/socket/socket_descriptor.h" |
+#include "net/socket/unix_domain_server_socket_posix.h" |
+ |
+namespace mojo { |
+namespace shell { |
+ |
+// Asynchronously listens for incoming connections on a unix domain |
+// socket at the provided path. Expects the parent directory in the |
+// path to exist. Must be run on an IO thread. |
+class IncomingConnectionListener { |
+ public: |
+ class Delegate { |
+ public: |
+ Delegate(); |
+ virtual ~Delegate(); |
+ |
+ // Called when listening has started. rv is from net/base/net_error_list.h |
+ virtual void OnListening(int rv) = 0; |
+ |
+ // Called every time an incoming connection is accepted. The delegate |
+ // takes ownership of incoming. |
+ virtual void OnConnection(net::SocketDescriptor incoming) = 0; |
+ }; |
+ |
+ IncomingConnectionListener(const base::FilePath& socket_path, |
+ Delegate* delegate); |
+ virtual ~IncomingConnectionListener(); |
+ |
+ // Attempts to bind a unix domain socket, set up for listening, at |
+ // socket_path_. |
+ // Regardless of success or failure, calls delegate->OnListening() with a |
+ // status code. If the socket was successfully created, begins asynchronously |
+ // waiting to accept incoming connections. |
+ void StartListening(); |
+ |
+ private: |
+ // Tells listen_socket_ to perform a non-blocking accept(). It may succeed |
+ // or fail immediately, or asynchronously wait for a later connection attempt. |
+ // Regardless, when it returns a definitive result (OK or a failing error), |
+ // calls OnAccept(). |
+ void Accept(); |
+ |
+ // If rv indicates success, incoming_socket_ should be populated with a |
+ // connected FD. Hands this off to delegate->OnConnection() and goes |
+ // back to non-blocking accept(). |
+ // Upon error, logs the error and goes back to non-blocking accept(). |
+ void OnAccept(int rv); |
+ |
+ Delegate* const delegate_; |
+ |
+ const base::FilePath socket_path_; |
+ net::UnixDomainServerSocket listen_socket_; |
+ base::ThreadChecker listen_thread_checker_; |
+ |
+ net::SocketDescriptor incoming_socket_; |
+ |
+ base::WeakPtrFactory<IncomingConnectionListener> weak_ptr_factory_; |
+}; |
+ |
+} // namespace shell |
+} // namespace mojo |
+ |
+#endif // MOJO_SHELL_INCOMING_CONNECTION_LISTENER_H_ |