| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef NET_SOCKET_UNIX_DOMAIN_SOCKET_POSIX_H_ | 5 #ifndef NET_SOCKET_UNIX_DOMAIN_LISTEN_SOCKET_POSIX_H_ |
| 6 #define NET_SOCKET_UNIX_DOMAIN_SOCKET_POSIX_H_ | 6 #define NET_SOCKET_UNIX_DOMAIN_LISTEN_SOCKET_POSIX_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/macros.h" |
| 13 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 14 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
| 15 #include "net/socket/stream_listen_socket.h" | 16 #include "net/socket/stream_listen_socket.h" |
| 17 #include "net/socket/unix_domain_server_socket_posix.h" |
| 16 | 18 |
| 17 #if defined(OS_ANDROID) || defined(OS_LINUX) | 19 #if defined(OS_ANDROID) || defined(OS_LINUX) |
| 18 // Feature only supported on Linux currently. This lets the Unix Domain Socket | 20 // Feature only supported on Linux currently. This lets the Unix Domain Socket |
| 19 // not be backed by the file system. | 21 // not be backed by the file system. |
| 20 #define SOCKET_ABSTRACT_NAMESPACE_SUPPORTED | 22 #define SOCKET_ABSTRACT_NAMESPACE_SUPPORTED |
| 21 #endif | 23 #endif |
| 22 | 24 |
| 23 namespace net { | 25 namespace net { |
| 24 | 26 |
| 25 // Unix Domain Socket Implementation. Supports abstract namespaces on Linux. | 27 // Unix Domain Socket Implementation. Supports abstract namespaces on Linux. |
| 26 class NET_EXPORT UnixDomainSocket : public StreamListenSocket { | 28 class NET_EXPORT UnixDomainListenSocket : public StreamListenSocket { |
| 27 public: | 29 public: |
| 28 virtual ~UnixDomainSocket(); | 30 typedef UnixDomainServerSocket::AuthCallback AuthCallback; |
| 29 | 31 |
| 30 // Callback that returns whether the already connected client, identified by | 32 virtual ~UnixDomainListenSocket(); |
| 31 // its process |user_id| and |group_id|, is allowed to keep the connection | |
| 32 // open. Note that the socket is closed immediately in case the callback | |
| 33 // returns false. | |
| 34 typedef base::Callback<bool (uid_t user_id, gid_t group_id)> AuthCallback; | |
| 35 | 33 |
| 36 // Returns an authentication callback that always grants access for | 34 // Note that the returned UnixDomainListenSocket instance does not take |
| 37 // convenience in case you don't want to use authentication. | 35 // ownership of |del|. |
| 38 static AuthCallback NoAuthentication(); | 36 static scoped_ptr<UnixDomainListenSocket> CreateAndListen( |
| 39 | |
| 40 // Note that the returned UnixDomainSocket instance does not take ownership of | |
| 41 // |del|. | |
| 42 static scoped_ptr<UnixDomainSocket> CreateAndListen( | |
| 43 const std::string& path, | 37 const std::string& path, |
| 44 StreamListenSocket::Delegate* del, | 38 StreamListenSocket::Delegate* del, |
| 45 const AuthCallback& auth_callback); | 39 const AuthCallback& auth_callback); |
| 46 | 40 |
| 47 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED) | 41 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED) |
| 48 // Same as above except that the created socket uses the abstract namespace | 42 // Same as above except that the created socket uses the abstract namespace |
| 49 // which is a Linux-only feature. If |fallback_path| is not empty, | 43 // which is a Linux-only feature. If |fallback_path| is not empty, |
| 50 // make the second attempt with the provided fallback name. | 44 // make the second attempt with the provided fallback name. |
| 51 static scoped_ptr<UnixDomainSocket> CreateAndListenWithAbstractNamespace( | 45 static scoped_ptr<UnixDomainListenSocket> |
| 46 CreateAndListenWithAbstractNamespace( |
| 52 const std::string& path, | 47 const std::string& path, |
| 53 const std::string& fallback_path, | 48 const std::string& fallback_path, |
| 54 StreamListenSocket::Delegate* del, | 49 StreamListenSocket::Delegate* del, |
| 55 const AuthCallback& auth_callback); | 50 const AuthCallback& auth_callback); |
| 56 #endif | 51 #endif |
| 57 | 52 |
| 58 private: | 53 private: |
| 59 UnixDomainSocket(SocketDescriptor s, | 54 UnixDomainListenSocket(SocketDescriptor s, |
| 60 StreamListenSocket::Delegate* del, | 55 StreamListenSocket::Delegate* del, |
| 61 const AuthCallback& auth_callback); | 56 const AuthCallback& auth_callback); |
| 62 | 57 |
| 63 static scoped_ptr<UnixDomainSocket> CreateAndListenInternal( | 58 static scoped_ptr<UnixDomainListenSocket> CreateAndListenInternal( |
| 64 const std::string& path, | 59 const std::string& path, |
| 65 const std::string& fallback_path, | 60 const std::string& fallback_path, |
| 66 StreamListenSocket::Delegate* del, | 61 StreamListenSocket::Delegate* del, |
| 67 const AuthCallback& auth_callback, | 62 const AuthCallback& auth_callback, |
| 68 bool use_abstract_namespace); | 63 bool use_abstract_namespace); |
| 69 | 64 |
| 70 static SocketDescriptor CreateAndBind(const std::string& path, | |
| 71 bool use_abstract_namespace); | |
| 72 | |
| 73 // StreamListenSocket: | 65 // StreamListenSocket: |
| 74 virtual void Accept() OVERRIDE; | 66 virtual void Accept() OVERRIDE; |
| 75 | 67 |
| 76 AuthCallback auth_callback_; | 68 AuthCallback auth_callback_; |
| 77 | 69 |
| 78 DISALLOW_COPY_AND_ASSIGN(UnixDomainSocket); | 70 DISALLOW_COPY_AND_ASSIGN(UnixDomainListenSocket); |
| 79 }; | 71 }; |
| 80 | 72 |
| 81 // Factory that can be used to instantiate UnixDomainSocket. | 73 // Factory that can be used to instantiate UnixDomainListenSocket. |
| 82 class NET_EXPORT UnixDomainSocketFactory : public StreamListenSocketFactory { | 74 class NET_EXPORT UnixDomainListenSocketFactory |
| 75 : public StreamListenSocketFactory { |
| 83 public: | 76 public: |
| 84 // Note that this class does not take ownership of the provided delegate. | 77 // Note that this class does not take ownership of the provided delegate. |
| 85 UnixDomainSocketFactory(const std::string& path, | 78 UnixDomainListenSocketFactory( |
| 86 const UnixDomainSocket::AuthCallback& auth_callback); | 79 const std::string& path, |
| 87 virtual ~UnixDomainSocketFactory(); | 80 const UnixDomainListenSocket::AuthCallback& auth_callback); |
| 81 virtual ~UnixDomainListenSocketFactory(); |
| 88 | 82 |
| 89 // StreamListenSocketFactory: | 83 // StreamListenSocketFactory: |
| 90 virtual scoped_ptr<StreamListenSocket> CreateAndListen( | 84 virtual scoped_ptr<StreamListenSocket> CreateAndListen( |
| 91 StreamListenSocket::Delegate* delegate) const OVERRIDE; | 85 StreamListenSocket::Delegate* delegate) const OVERRIDE; |
| 92 | 86 |
| 93 protected: | 87 protected: |
| 94 const std::string path_; | 88 const std::string path_; |
| 95 const UnixDomainSocket::AuthCallback auth_callback_; | 89 const UnixDomainListenSocket::AuthCallback auth_callback_; |
| 96 | 90 |
| 97 private: | 91 private: |
| 98 DISALLOW_COPY_AND_ASSIGN(UnixDomainSocketFactory); | 92 DISALLOW_COPY_AND_ASSIGN(UnixDomainListenSocketFactory); |
| 99 }; | 93 }; |
| 100 | 94 |
| 101 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED) | 95 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED) |
| 102 // Use this factory to instantiate UnixDomainSocket using the abstract | 96 // Use this factory to instantiate UnixDomainListenSocket using the abstract |
| 103 // namespace feature (only supported on Linux). | 97 // namespace feature (only supported on Linux). |
| 104 class NET_EXPORT UnixDomainSocketWithAbstractNamespaceFactory | 98 class NET_EXPORT UnixDomainListenSocketWithAbstractNamespaceFactory |
| 105 : public UnixDomainSocketFactory { | 99 : public UnixDomainListenSocketFactory { |
| 106 public: | 100 public: |
| 107 UnixDomainSocketWithAbstractNamespaceFactory( | 101 UnixDomainListenSocketWithAbstractNamespaceFactory( |
| 108 const std::string& path, | 102 const std::string& path, |
| 109 const std::string& fallback_path, | 103 const std::string& fallback_path, |
| 110 const UnixDomainSocket::AuthCallback& auth_callback); | 104 const UnixDomainListenSocket::AuthCallback& auth_callback); |
| 111 virtual ~UnixDomainSocketWithAbstractNamespaceFactory(); | 105 virtual ~UnixDomainListenSocketWithAbstractNamespaceFactory(); |
| 112 | 106 |
| 113 // UnixDomainSocketFactory: | 107 // UnixDomainListenSocketFactory: |
| 114 virtual scoped_ptr<StreamListenSocket> CreateAndListen( | 108 virtual scoped_ptr<StreamListenSocket> CreateAndListen( |
| 115 StreamListenSocket::Delegate* delegate) const OVERRIDE; | 109 StreamListenSocket::Delegate* delegate) const OVERRIDE; |
| 116 | 110 |
| 117 private: | 111 private: |
| 118 std::string fallback_path_; | 112 std::string fallback_path_; |
| 119 | 113 |
| 120 DISALLOW_COPY_AND_ASSIGN(UnixDomainSocketWithAbstractNamespaceFactory); | 114 DISALLOW_COPY_AND_ASSIGN(UnixDomainListenSocketWithAbstractNamespaceFactory); |
| 121 }; | 115 }; |
| 122 #endif | 116 #endif |
| 123 | 117 |
| 124 } // namespace net | 118 } // namespace net |
| 125 | 119 |
| 126 #endif // NET_SOCKET_UNIX_DOMAIN_SOCKET_POSIX_H_ | 120 #endif // NET_SOCKET_UNIX_DOMAIN_LISTEN_SOCKET_POSIX_H_ |
| OLD | NEW |