1 // Copyright 2018 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 MOJO_PUBLIC_CPP_PLATFORM_NAMED_PLATFORM_CHANNEL_H_
6 #define MOJO_PUBLIC_CPP_PLATFORM_NAMED_PLATFORM_CHANNEL_H_
7
8 #include "base/command_line.h"
9 #include "base/component_export.h"
10 #include "base/macros.h"
11 #include "base/strings/string_piece.h"
12 #include "build/build_config.h"
13 #include "mojo/public/cpp/platform/platform_channel_endpoint.h"
14 #include "mojo/public/cpp/platform/platform_channel_server_endpoint.h"
15
16 #if defined(OS_WIN)
17 #include "base/strings/string16.h"
18 #elif defined(OS_POSIX)
19 #include "base/files/file_path.h"
20 #endif
21
22 namespace mojo {
23
24 // NamedPlatformChannel encapsulates a Mojo invitation transport channel which
25 // can listen for inbound connections established by clients connecting to
26 // a named system resource (i.e. a named pipe server on Windows, a named
27 // Unix domain socket on POSIX; other platforms not supported).
28 //
29 // This can be especially useful when the local process has no way to transfer
30 // handles to the remote process, e.g. it does not control process launch or
31 // have any pre-existing communication channel to the process.
COMPONENT_EXPORT(MOJO_CPP_PLATFORM)32 class COMPONENT_EXPORT(MOJO_CPP_PLATFORM) NamedPlatformChannel {
33 public:
34 static const char kNamedHandleSwitch[];
35
36 #if defined(OS_WIN)
37 using ServerName = base::string16;
38 #else
39 using ServerName = std::string;
40 #endif
41
42 struct COMPONENT_EXPORT(MOJO_CPP_PLATFORM) Options {
43 // Specifies the name to use for the server. If empty, a random name is
44 // generated.
45 ServerName server_name;
46
47 #if defined(OS_WIN)
48 // If non-empty, a security descriptor to use when creating the pipe. If
49 // empty, a default security descriptor will be used. See
50 // |kDefaultSecurityDescriptor|.
51 base::string16 security_descriptor;
52
53 // If |true|, only a server endpoint will be allowed with the given name and
54 // only one client will be able to connect. Otherwise many
55 // NamedPlatformChannel instances can be created with the same name and
56 // a different client can connect to each one.
57 bool enforce_uniqueness = true;
58 #elif defined(OS_POSIX)
59 // On POSIX, every new unnamed NamedPlatformChannel creates a server socket
60 // with a random name. This controls the directory where that happens.
61 // Ignored if |server_name| was set explicitly.
62 base::FilePath socket_dir;
63 #endif
64 };
65
66 NamedPlatformChannel(const Options& options);
67 NamedPlatformChannel(NamedPlatformChannel&& other);
68 ~NamedPlatformChannel();
69
70 NamedPlatformChannel& operator=(NamedPlatformChannel&& other);
71
72 const PlatformChannelServerEndpoint& server_endpoint() const {
73 return server_endpoint_;
74 }
75
76 // Helper to create a ServerName from a UTF8 string regardless of platform.
77 static ServerName ServerNameFromUTF8(base::StringPiece name);
78
79 // Passes the local server endpoint for the channel. On Windows, this is a
80 // named pipe server; on POSIX it's a bound, listening domain socket. In each
81 // case it should accept a single new connection.
82 //
83 // Use the handle to send or receive an invitation, with the endpoint type as
84 // |MOJO_INVITATION_TRANSPORT_TYPE_CHANNEL_SERVER|.
85 PlatformChannelServerEndpoint TakeServerEndpoint() WARN_UNUSED_RESULT {
86 return std::move(server_endpoint_);
87 }
88
89 // Returns a name that can be used a remote process to connect to the server
90 // endpoint.
91 const ServerName& GetServerName() const { return server_name_; }
92
93 // Passes the server name on |*command_line| using the common
94 // |kNamedHandleSwitch| flag.
95 void PassServerNameOnCommandLine(base::CommandLine* command_line);
96
97 // Recovers a functioning client endpoint handle by creating a new endpoint
98 // and connecting it to |server_name| if possible.
99 static PlatformChannelEndpoint ConnectToServer(const ServerName& server_name)
100 WARN_UNUSED_RESULT;
101
102 // Like above, but extracts the server name from |command_line| using the
103 // common |kNamedHandleSwitch| flag.
104 static PlatformChannelEndpoint ConnectToServer(
105 const base::CommandLine& command_line) WARN_UNUSED_RESULT;
106
107 private:
108 static PlatformChannelServerEndpoint CreateServerEndpoint(
109 const Options& options,
110 ServerName* server_name);
111 static PlatformChannelEndpoint CreateClientEndpoint(
112 const ServerName& server_name);
113
114 ServerName server_name_;
115 PlatformChannelServerEndpoint server_endpoint_;
116
117 DISALLOW_COPY_AND_ASSIGN(NamedPlatformChannel);
118 };
119
120 } // namespace mojo
121
122 #endif // MOJO_PUBLIC_CPP_PLATFORM_NAMED_PLATFORM_CHANNEL_H_
123