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.
5 #ifndef NET_BASE_UNIX_DOMAIN_SOCKET_POSIX_H_
6 #define NET_BASE_UNIX_DOMAIN_SOCKET_POSIX_H_
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "build/build_config.h"
16 #include "net/base/net_export.h"
17 #include "net/base/stream_listen_socket.h"
19 #if defined(OS_ANDROID) || defined(OS_LINUX)
20 // Feature only supported on Linux currently. This lets the Unix Domain Socket
21 // not be backed by the file system.
22 #define SOCKET_ABSTRACT_NAMESPACE_SUPPORTED
27 // Unix Domain Socket Implementation. Supports abstract namespaces on Linux.
28 class NET_EXPORT UnixDomainSocket
: public StreamListenSocket
{
30 // Callback that returns whether the already connected client, identified by
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
34 typedef base::Callback
<bool (uid_t user_id
, gid_t group_id
)> AuthCallback
;
36 // Returns an authentication callback that always grants access for
37 // convenience in case you don't want to use authentication.
38 static AuthCallback
NoAuthentication();
40 // Note that the returned UnixDomainSocket instance does not take ownership of
42 static scoped_refptr
<UnixDomainSocket
> CreateAndListen(
43 const std::string
& path
,
44 StreamListenSocket::Delegate
* del
,
45 const AuthCallback
& auth_callback
);
47 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED)
48 // Same as above except that the created socket uses the abstract namespace
49 // which is a Linux-only feature.
50 static scoped_refptr
<UnixDomainSocket
> CreateAndListenWithAbstractNamespace(
51 const std::string
& path
,
52 StreamListenSocket::Delegate
* del
,
53 const AuthCallback
& auth_callback
);
57 UnixDomainSocket(SocketDescriptor s
,
58 StreamListenSocket::Delegate
* del
,
59 const AuthCallback
& auth_callback
);
60 virtual ~UnixDomainSocket();
62 static UnixDomainSocket
* CreateAndListenInternal(
63 const std::string
& path
,
64 StreamListenSocket::Delegate
* del
,
65 const AuthCallback
& auth_callback
,
66 bool use_abstract_namespace
);
68 static SocketDescriptor
CreateAndBind(const std::string
& path
,
69 bool use_abstract_namespace
);
71 // StreamListenSocket:
72 virtual void Accept() OVERRIDE
;
74 AuthCallback auth_callback_
;
76 DISALLOW_COPY_AND_ASSIGN(UnixDomainSocket
);
79 // Factory that can be used to instantiate UnixDomainSocket.
80 class NET_EXPORT UnixDomainSocketFactory
: public StreamListenSocketFactory
{
82 // Note that this class does not take ownership of the provided delegate.
83 UnixDomainSocketFactory(const std::string
& path
,
84 const UnixDomainSocket::AuthCallback
& auth_callback
);
85 virtual ~UnixDomainSocketFactory();
87 // StreamListenSocketFactory:
88 virtual scoped_refptr
<StreamListenSocket
> CreateAndListen(
89 StreamListenSocket::Delegate
* delegate
) const OVERRIDE
;
92 const std::string path_
;
93 const UnixDomainSocket::AuthCallback auth_callback_
;
96 DISALLOW_COPY_AND_ASSIGN(UnixDomainSocketFactory
);
99 #if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED)
100 // Use this factory to instantiate UnixDomainSocket using the abstract
101 // namespace feature (only supported on Linux).
102 class NET_EXPORT UnixDomainSocketWithAbstractNamespaceFactory
103 : public UnixDomainSocketFactory
{
105 UnixDomainSocketWithAbstractNamespaceFactory(
106 const std::string
& path
,
107 const UnixDomainSocket::AuthCallback
& auth_callback
);
108 virtual ~UnixDomainSocketWithAbstractNamespaceFactory();
110 // UnixDomainSocketFactory:
111 virtual scoped_refptr
<StreamListenSocket
> CreateAndListen(
112 StreamListenSocket::Delegate
* delegate
) const OVERRIDE
;
115 DISALLOW_COPY_AND_ASSIGN(UnixDomainSocketWithAbstractNamespaceFactory
);
121 #endif // NET_BASE_UNIX_DOMAIN_SOCKET_POSIX_H_