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