xref: /aosp_15_r20/external/libchrome/mojo/public/cpp/system/invitation.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2018 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef MOJO_PUBLIC_CPP_SYSTEM_INVITATION_H_
6*635a8641SAndroid Build Coastguard Worker #define MOJO_PUBLIC_CPP_SYSTEM_INVITATION_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <cstdint>
9*635a8641SAndroid Build Coastguard Worker #include <string>
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker #include "base/callback.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/component_export.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/process/process_handle.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_piece.h"
16*635a8641SAndroid Build Coastguard Worker #include "mojo/public/cpp/platform/platform_channel_endpoint.h"
17*635a8641SAndroid Build Coastguard Worker #include "mojo/public/cpp/platform/platform_channel_server_endpoint.h"
18*635a8641SAndroid Build Coastguard Worker #include "mojo/public/cpp/system/handle.h"
19*635a8641SAndroid Build Coastguard Worker #include "mojo/public/cpp/system/message_pipe.h"
20*635a8641SAndroid Build Coastguard Worker #include "mojo/public/cpp/system/system_export.h"
21*635a8641SAndroid Build Coastguard Worker 
22*635a8641SAndroid Build Coastguard Worker namespace mojo {
23*635a8641SAndroid Build Coastguard Worker 
24*635a8641SAndroid Build Coastguard Worker // A callback which may be provided when sending an invitation to another
25*635a8641SAndroid Build Coastguard Worker // process. In the event of any validation errors regarding messages from that
26*635a8641SAndroid Build Coastguard Worker // process (reported via MojoNotifyBadMessage etc and related helpers), the
27*635a8641SAndroid Build Coastguard Worker // callback will be invoked.
28*635a8641SAndroid Build Coastguard Worker using ProcessErrorCallback = base::RepeatingCallback<void(const std::string&)>;
29*635a8641SAndroid Build Coastguard Worker 
30*635a8641SAndroid Build Coastguard Worker // A strongly-typed representation of a |MojoHandle| for an invitation.
31*635a8641SAndroid Build Coastguard Worker class InvitationHandle : public Handle {
32*635a8641SAndroid Build Coastguard Worker  public:
InvitationHandle()33*635a8641SAndroid Build Coastguard Worker   InvitationHandle() {}
InvitationHandle(MojoHandle value)34*635a8641SAndroid Build Coastguard Worker   explicit InvitationHandle(MojoHandle value) : Handle(value) {}
35*635a8641SAndroid Build Coastguard Worker 
36*635a8641SAndroid Build Coastguard Worker   // Copying and assignment allowed.
37*635a8641SAndroid Build Coastguard Worker };
38*635a8641SAndroid Build Coastguard Worker 
39*635a8641SAndroid Build Coastguard Worker static_assert(sizeof(InvitationHandle) == sizeof(Handle),
40*635a8641SAndroid Build Coastguard Worker               "Bad size for C++ InvitationHandle");
41*635a8641SAndroid Build Coastguard Worker 
42*635a8641SAndroid Build Coastguard Worker using ScopedInvitationHandle = ScopedHandleBase<InvitationHandle>;
43*635a8641SAndroid Build Coastguard Worker static_assert(sizeof(ScopedInvitationHandle) == sizeof(InvitationHandle),
44*635a8641SAndroid Build Coastguard Worker               "Bad size for C++ ScopedInvitationHandle");
45*635a8641SAndroid Build Coastguard Worker 
46*635a8641SAndroid Build Coastguard Worker // An OutgoingInvitation is used to invite another process to join the calling
47*635a8641SAndroid Build Coastguard Worker // process's IPC network.
48*635a8641SAndroid Build Coastguard Worker //
49*635a8641SAndroid Build Coastguard Worker // Typical use involves constructing a |PlatformChannel| and using one end to
50*635a8641SAndroid Build Coastguard Worker // send the invitation (see |Send()| below) while passing the other to a child
51*635a8641SAndroid Build Coastguard Worker // process.
52*635a8641SAndroid Build Coastguard Worker //
53*635a8641SAndroid Build Coastguard Worker // This may also be used with the server endpoint of a |NamedPlatformChannel|.
54*635a8641SAndroid Build Coastguard Worker class MOJO_CPP_SYSTEM_EXPORT OutgoingInvitation {
55*635a8641SAndroid Build Coastguard Worker  public:
56*635a8641SAndroid Build Coastguard Worker   OutgoingInvitation();
57*635a8641SAndroid Build Coastguard Worker   OutgoingInvitation(OutgoingInvitation&& other);
58*635a8641SAndroid Build Coastguard Worker   ~OutgoingInvitation();
59*635a8641SAndroid Build Coastguard Worker 
60*635a8641SAndroid Build Coastguard Worker   OutgoingInvitation& operator=(OutgoingInvitation&& other);
61*635a8641SAndroid Build Coastguard Worker 
62*635a8641SAndroid Build Coastguard Worker   // Creates a new message pipe, attaching one end to this invitation and
63*635a8641SAndroid Build Coastguard Worker   // returning the other end to the caller. The invitee can extract the
64*635a8641SAndroid Build Coastguard Worker   // attached endpoint (see |IncomingInvitation|) thus establishing end-to-end
65*635a8641SAndroid Build Coastguard Worker   // Mojo communication.
66*635a8641SAndroid Build Coastguard Worker   //
67*635a8641SAndroid Build Coastguard Worker   // |name| is an arbitrary value that must be used by the invitee to extract
68*635a8641SAndroid Build Coastguard Worker   // the corresponding attached endpoint.
69*635a8641SAndroid Build Coastguard Worker   ScopedMessagePipeHandle AttachMessagePipe(base::StringPiece name);
70*635a8641SAndroid Build Coastguard Worker 
71*635a8641SAndroid Build Coastguard Worker   // Same as above but allows use of an integer name for convenience.
72*635a8641SAndroid Build Coastguard Worker   ScopedMessagePipeHandle AttachMessagePipe(uint64_t name);
73*635a8641SAndroid Build Coastguard Worker 
74*635a8641SAndroid Build Coastguard Worker   // Extracts an attached pipe. Note that this is not typically useful, but it
75*635a8641SAndroid Build Coastguard Worker   // is potentially necessary in cases where a caller wants to, e.g., abort
76*635a8641SAndroid Build Coastguard Worker   // launching another process and recover a pipe endpoint they had previously
77*635a8641SAndroid Build Coastguard Worker   // attached.
78*635a8641SAndroid Build Coastguard Worker   ScopedMessagePipeHandle ExtractMessagePipe(base::StringPiece name);
79*635a8641SAndroid Build Coastguard Worker 
80*635a8641SAndroid Build Coastguard Worker   // Same as above but allows use of an integer name for convenience.
81*635a8641SAndroid Build Coastguard Worker   ScopedMessagePipeHandle ExtractMessagePipe(uint64_t name);
82*635a8641SAndroid Build Coastguard Worker 
83*635a8641SAndroid Build Coastguard Worker   // Sends |invitation| to another process via |channel_endpoint|, which should
84*635a8641SAndroid Build Coastguard Worker   // correspond to the local endpoint taken from a |PlatformChannel|.
85*635a8641SAndroid Build Coastguard Worker   //
86*635a8641SAndroid Build Coastguard Worker   // |process_handle| is a handle to the destination process if known. If not
87*635a8641SAndroid Build Coastguard Worker   // provided, IPC may be limited on some platforms (namely Mac and Windows) due
88*635a8641SAndroid Build Coastguard Worker   // to an inability to transfer system handles across the boundary.
89*635a8641SAndroid Build Coastguard Worker   static void Send(OutgoingInvitation invitation,
90*635a8641SAndroid Build Coastguard Worker                    base::ProcessHandle target_process,
91*635a8641SAndroid Build Coastguard Worker                    PlatformChannelEndpoint channel_endpoint,
92*635a8641SAndroid Build Coastguard Worker                    const ProcessErrorCallback& error_callback = {});
93*635a8641SAndroid Build Coastguard Worker 
94*635a8641SAndroid Build Coastguard Worker   // Similar to above, but sends |invitation| via |server_endpoint|, which
95*635a8641SAndroid Build Coastguard Worker   // should correspond to a |PlatformChannelServerEndpoint| taken from a
96*635a8641SAndroid Build Coastguard Worker   // |NamedPlatformChannel|.
97*635a8641SAndroid Build Coastguard Worker   static void Send(OutgoingInvitation invitation,
98*635a8641SAndroid Build Coastguard Worker                    base::ProcessHandle target_process,
99*635a8641SAndroid Build Coastguard Worker                    PlatformChannelServerEndpoint server_endpoint,
100*635a8641SAndroid Build Coastguard Worker                    const ProcessErrorCallback& error_callback = {});
101*635a8641SAndroid Build Coastguard Worker 
102*635a8641SAndroid Build Coastguard Worker   // Sends an isolated invitation over |endpoint|. The process at the other
103*635a8641SAndroid Build Coastguard Worker   // endpoint must use |IncomingInvitation::AcceptIsolated()| to accept the
104*635a8641SAndroid Build Coastguard Worker   // invitation.
105*635a8641SAndroid Build Coastguard Worker   //
106*635a8641SAndroid Build Coastguard Worker   // Isolated invitations must be used in lieu of regular invitations in cases
107*635a8641SAndroid Build Coastguard Worker   // where both of the processes being connected already belong to independent
108*635a8641SAndroid Build Coastguard Worker   // multiprocess graphs.
109*635a8641SAndroid Build Coastguard Worker   //
110*635a8641SAndroid Build Coastguard Worker   // Such connections are limited in functionality:
111*635a8641SAndroid Build Coastguard Worker   //
112*635a8641SAndroid Build Coastguard Worker   //   * Platform handles may not be transferrable between the processes
113*635a8641SAndroid Build Coastguard Worker   //
114*635a8641SAndroid Build Coastguard Worker   //   * Pipes sent between the processes may not be subsequently transferred to
115*635a8641SAndroid Build Coastguard Worker   //     other processes in each others' process graph.
116*635a8641SAndroid Build Coastguard Worker   //
117*635a8641SAndroid Build Coastguard Worker   // Only one concurrent isolated connection is supported between any two
118*635a8641SAndroid Build Coastguard Worker   // processes.
119*635a8641SAndroid Build Coastguard Worker   //
120*635a8641SAndroid Build Coastguard Worker   // Unlike |Send()| above, isolated invitations automatically have a single
121*635a8641SAndroid Build Coastguard Worker   // message pipe attached and this is the only attachment allowed. The local
122*635a8641SAndroid Build Coastguard Worker   // end of the attached pipe is returned here.
123*635a8641SAndroid Build Coastguard Worker   //
124*635a8641SAndroid Build Coastguard Worker   // If |connection_name| is non-empty, any previously established isolated
125*635a8641SAndroid Build Coastguard Worker   // connection using the same name will be disconnected.
126*635a8641SAndroid Build Coastguard Worker   static ScopedMessagePipeHandle SendIsolated(
127*635a8641SAndroid Build Coastguard Worker       PlatformChannelEndpoint channel_endpoint,
128*635a8641SAndroid Build Coastguard Worker       base::StringPiece connection_name = {});
129*635a8641SAndroid Build Coastguard Worker 
130*635a8641SAndroid Build Coastguard Worker   // Similar to above but sends |invitation| via |server_endpoint|, which should
131*635a8641SAndroid Build Coastguard Worker   // correspond to a |PlatformChannelServerEndpoint| taken from a
132*635a8641SAndroid Build Coastguard Worker   // |NamedPlatformChannel|.
133*635a8641SAndroid Build Coastguard Worker   //
134*635a8641SAndroid Build Coastguard Worker   // If |connection_name| is non-empty, any previously established isolated
135*635a8641SAndroid Build Coastguard Worker   // connection using the same name will be disconnected.
136*635a8641SAndroid Build Coastguard Worker   static ScopedMessagePipeHandle SendIsolated(
137*635a8641SAndroid Build Coastguard Worker       PlatformChannelServerEndpoint server_endpoint,
138*635a8641SAndroid Build Coastguard Worker       base::StringPiece connection_name = {});
139*635a8641SAndroid Build Coastguard Worker 
140*635a8641SAndroid Build Coastguard Worker  private:
141*635a8641SAndroid Build Coastguard Worker   ScopedInvitationHandle handle_;
142*635a8641SAndroid Build Coastguard Worker 
143*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(OutgoingInvitation);
144*635a8641SAndroid Build Coastguard Worker };
145*635a8641SAndroid Build Coastguard Worker 
146*635a8641SAndroid Build Coastguard Worker // An IncomingInvitation can be accepted by an invited process by calling
147*635a8641SAndroid Build Coastguard Worker // |IncomingInvitation::Accept()|. Once accepted, the invitation can be used
148*635a8641SAndroid Build Coastguard Worker // to extract attached message pipes by name.
149*635a8641SAndroid Build Coastguard Worker class MOJO_CPP_SYSTEM_EXPORT IncomingInvitation {
150*635a8641SAndroid Build Coastguard Worker  public:
151*635a8641SAndroid Build Coastguard Worker   IncomingInvitation();
152*635a8641SAndroid Build Coastguard Worker   IncomingInvitation(IncomingInvitation&& other);
153*635a8641SAndroid Build Coastguard Worker   explicit IncomingInvitation(ScopedInvitationHandle handle);
154*635a8641SAndroid Build Coastguard Worker   ~IncomingInvitation();
155*635a8641SAndroid Build Coastguard Worker 
156*635a8641SAndroid Build Coastguard Worker   IncomingInvitation& operator=(IncomingInvitation&& other);
157*635a8641SAndroid Build Coastguard Worker 
158*635a8641SAndroid Build Coastguard Worker   // Accepts an incoming invitation from |channel_endpoint|. If the invitation
159*635a8641SAndroid Build Coastguard Worker   // was sent using one end of a |PlatformChannel|, |channel_endpoint| should be
160*635a8641SAndroid Build Coastguard Worker   // the other end of that channel. If the invitation was sent using a
161*635a8641SAndroid Build Coastguard Worker   // |PlatformChannelServerEndpoint|, then |channel_endpoint| should be created
162*635a8641SAndroid Build Coastguard Worker   // by |NamedPlatformChannel::ConnectToServer|.
163*635a8641SAndroid Build Coastguard Worker   static IncomingInvitation Accept(PlatformChannelEndpoint channel_endpoint);
164*635a8641SAndroid Build Coastguard Worker 
165*635a8641SAndroid Build Coastguard Worker   // Accepts an incoming isolated invitation from |channel_endpoint|. See
166*635a8641SAndroid Build Coastguard Worker   // notes on |OutgoingInvitation::SendIsolated()|.
167*635a8641SAndroid Build Coastguard Worker   static ScopedMessagePipeHandle AcceptIsolated(
168*635a8641SAndroid Build Coastguard Worker       PlatformChannelEndpoint channel_endpoint);
169*635a8641SAndroid Build Coastguard Worker 
170*635a8641SAndroid Build Coastguard Worker   // Extracts an attached message pipe from this invitation. This may succeed
171*635a8641SAndroid Build Coastguard Worker   // even if no such pipe was attached, though the extracted pipe will
172*635a8641SAndroid Build Coastguard Worker   // eventually observe peer closure.
173*635a8641SAndroid Build Coastguard Worker   ScopedMessagePipeHandle ExtractMessagePipe(base::StringPiece name);
174*635a8641SAndroid Build Coastguard Worker 
175*635a8641SAndroid Build Coastguard Worker   // Same as above but allows use of an integer name for convenience.
176*635a8641SAndroid Build Coastguard Worker   ScopedMessagePipeHandle ExtractMessagePipe(uint64_t name);
177*635a8641SAndroid Build Coastguard Worker 
178*635a8641SAndroid Build Coastguard Worker  private:
179*635a8641SAndroid Build Coastguard Worker   ScopedInvitationHandle handle_;
180*635a8641SAndroid Build Coastguard Worker 
181*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(IncomingInvitation);
182*635a8641SAndroid Build Coastguard Worker };
183*635a8641SAndroid Build Coastguard Worker 
184*635a8641SAndroid Build Coastguard Worker }  // namespace mojo
185*635a8641SAndroid Build Coastguard Worker 
186*635a8641SAndroid Build Coastguard Worker #endif  // MOJO_PUBLIC_CPP_SYSTEM_INVITATION_H_
187