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