unix_domain_listen_socket_posix.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1// Copyright 2014 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_SOCKET_UNIX_DOMAIN_LISTEN_SOCKET_POSIX_H_
6#define NET_SOCKET_UNIX_DOMAIN_LISTEN_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/macros.h"
14#include "build/build_config.h"
15#include "net/base/net_export.h"
16#include "net/socket/stream_listen_socket.h"
17#include "net/socket/unix_domain_server_socket_posix.h"
18
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
23#endif
24
25namespace net {
26namespace deprecated {
27
28// Unix Domain Socket Implementation. Supports abstract namespaces on Linux.
29class NET_EXPORT UnixDomainListenSocket : public StreamListenSocket {
30 public:
31  typedef UnixDomainServerSocket::AuthCallback AuthCallback;
32
33  virtual ~UnixDomainListenSocket();
34
35  // Note that the returned UnixDomainListenSocket instance does not take
36  // ownership of |del|.
37  static scoped_ptr<UnixDomainListenSocket> CreateAndListen(
38      const std::string& path,
39      StreamListenSocket::Delegate* del,
40      const AuthCallback& auth_callback);
41
42#if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED)
43  // Same as above except that the created socket uses the abstract namespace
44  // which is a Linux-only feature. If |fallback_path| is not empty,
45  // make the second attempt with the provided fallback name.
46  static scoped_ptr<UnixDomainListenSocket>
47  CreateAndListenWithAbstractNamespace(
48      const std::string& path,
49      const std::string& fallback_path,
50      StreamListenSocket::Delegate* del,
51      const AuthCallback& auth_callback);
52#endif
53
54 private:
55  UnixDomainListenSocket(SocketDescriptor s,
56                         StreamListenSocket::Delegate* del,
57                         const AuthCallback& auth_callback);
58
59  static scoped_ptr<UnixDomainListenSocket> CreateAndListenInternal(
60      const std::string& path,
61      const std::string& fallback_path,
62      StreamListenSocket::Delegate* del,
63      const AuthCallback& auth_callback,
64      bool use_abstract_namespace);
65
66  // StreamListenSocket:
67  virtual void Accept() OVERRIDE;
68
69  AuthCallback auth_callback_;
70
71  DISALLOW_COPY_AND_ASSIGN(UnixDomainListenSocket);
72};
73
74// Factory that can be used to instantiate UnixDomainListenSocket.
75class NET_EXPORT UnixDomainListenSocketFactory
76    : public StreamListenSocketFactory {
77 public:
78  // Note that this class does not take ownership of the provided delegate.
79  UnixDomainListenSocketFactory(
80      const std::string& path,
81      const UnixDomainListenSocket::AuthCallback& auth_callback);
82  virtual ~UnixDomainListenSocketFactory();
83
84  // StreamListenSocketFactory:
85  virtual scoped_ptr<StreamListenSocket> CreateAndListen(
86      StreamListenSocket::Delegate* delegate) const OVERRIDE;
87
88 protected:
89  const std::string path_;
90  const UnixDomainListenSocket::AuthCallback auth_callback_;
91
92 private:
93  DISALLOW_COPY_AND_ASSIGN(UnixDomainListenSocketFactory);
94};
95
96#if defined(SOCKET_ABSTRACT_NAMESPACE_SUPPORTED)
97// Use this factory to instantiate UnixDomainListenSocket using the abstract
98// namespace feature (only supported on Linux).
99class NET_EXPORT UnixDomainListenSocketWithAbstractNamespaceFactory
100    : public UnixDomainListenSocketFactory {
101 public:
102  UnixDomainListenSocketWithAbstractNamespaceFactory(
103      const std::string& path,
104      const std::string& fallback_path,
105      const UnixDomainListenSocket::AuthCallback& auth_callback);
106  virtual ~UnixDomainListenSocketWithAbstractNamespaceFactory();
107
108  // UnixDomainListenSocketFactory:
109  virtual scoped_ptr<StreamListenSocket> CreateAndListen(
110      StreamListenSocket::Delegate* delegate) const OVERRIDE;
111
112 private:
113  std::string fallback_path_;
114
115  DISALLOW_COPY_AND_ASSIGN(UnixDomainListenSocketWithAbstractNamespaceFactory);
116};
117#endif
118
119}  // namespace deprecated
120}  // namespace net
121
122#endif  // NET_SOCKET_UNIX_DOMAIN_LISTEN_SOCKET_POSIX_H_
123