xref: /aosp_15_r20/external/libchrome/ipc/ipc_channel.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 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 IPC_IPC_CHANNEL_H_
6*635a8641SAndroid Build Coastguard Worker #define IPC_IPC_CHANNEL_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
9*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker #include <memory>
12*635a8641SAndroid Build Coastguard Worker #include <string>
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker #include "base/compiler_specific.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/component_export.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/files/scoped_file.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/memory/ref_counted.h"
18*635a8641SAndroid Build Coastguard Worker #include "base/process/process.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/single_thread_task_runner.h"
20*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_task_runner_handle.h"
21*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
22*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc.mojom.h"
23*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_channel_handle.h"
24*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_message.h"
25*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_sender.h"
26*635a8641SAndroid Build Coastguard Worker #include "mojo/public/cpp/bindings/associated_interface_ptr.h"
27*635a8641SAndroid Build Coastguard Worker #include "mojo/public/cpp/bindings/associated_interface_request.h"
28*635a8641SAndroid Build Coastguard Worker #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
29*635a8641SAndroid Build Coastguard Worker #include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h"
30*635a8641SAndroid Build Coastguard Worker 
31*635a8641SAndroid Build Coastguard Worker #if defined(OS_POSIX)
32*635a8641SAndroid Build Coastguard Worker #include <sys/types.h>
33*635a8641SAndroid Build Coastguard Worker #endif
34*635a8641SAndroid Build Coastguard Worker 
35*635a8641SAndroid Build Coastguard Worker namespace IPC {
36*635a8641SAndroid Build Coastguard Worker 
37*635a8641SAndroid Build Coastguard Worker class Listener;
38*635a8641SAndroid Build Coastguard Worker 
39*635a8641SAndroid Build Coastguard Worker //------------------------------------------------------------------------------
40*635a8641SAndroid Build Coastguard Worker // See
41*635a8641SAndroid Build Coastguard Worker // http://www.chromium.org/developers/design-documents/inter-process-communication
42*635a8641SAndroid Build Coastguard Worker // for overview of IPC in Chromium.
43*635a8641SAndroid Build Coastguard Worker 
44*635a8641SAndroid Build Coastguard Worker // Channels are implemented using mojo message pipes on all platforms other
45*635a8641SAndroid Build Coastguard Worker // than NaCl.
46*635a8641SAndroid Build Coastguard Worker 
COMPONENT_EXPORT(IPC)47*635a8641SAndroid Build Coastguard Worker class COMPONENT_EXPORT(IPC) Channel : public Sender {
48*635a8641SAndroid Build Coastguard Worker   // Security tests need access to the pipe handle.
49*635a8641SAndroid Build Coastguard Worker   friend class ChannelTest;
50*635a8641SAndroid Build Coastguard Worker 
51*635a8641SAndroid Build Coastguard Worker  public:
52*635a8641SAndroid Build Coastguard Worker   // Flags to test modes
53*635a8641SAndroid Build Coastguard Worker   enum ModeFlags {
54*635a8641SAndroid Build Coastguard Worker     MODE_NO_FLAG = 0x0,
55*635a8641SAndroid Build Coastguard Worker     MODE_SERVER_FLAG = 0x1,
56*635a8641SAndroid Build Coastguard Worker     MODE_CLIENT_FLAG = 0x2,
57*635a8641SAndroid Build Coastguard Worker   };
58*635a8641SAndroid Build Coastguard Worker 
59*635a8641SAndroid Build Coastguard Worker   // Some Standard Modes
60*635a8641SAndroid Build Coastguard Worker   // TODO(morrita): These are under deprecation work. You should use Create*()
61*635a8641SAndroid Build Coastguard Worker   // functions instead.
62*635a8641SAndroid Build Coastguard Worker   enum Mode {
63*635a8641SAndroid Build Coastguard Worker     MODE_NONE = MODE_NO_FLAG,
64*635a8641SAndroid Build Coastguard Worker     MODE_SERVER = MODE_SERVER_FLAG,
65*635a8641SAndroid Build Coastguard Worker     MODE_CLIENT = MODE_CLIENT_FLAG,
66*635a8641SAndroid Build Coastguard Worker   };
67*635a8641SAndroid Build Coastguard Worker 
68*635a8641SAndroid Build Coastguard Worker   // Messages internal to the IPC implementation are defined here.
69*635a8641SAndroid Build Coastguard Worker   // Uses Maximum value of message type (uint16_t), to avoid conflicting
70*635a8641SAndroid Build Coastguard Worker   // with normal message types, which are enumeration constants starting from 0.
71*635a8641SAndroid Build Coastguard Worker   enum {
72*635a8641SAndroid Build Coastguard Worker     // The Hello message is sent by the peer when the channel is connected.
73*635a8641SAndroid Build Coastguard Worker     // The message contains just the process id (pid).
74*635a8641SAndroid Build Coastguard Worker     // The message has a special routing_id (MSG_ROUTING_NONE)
75*635a8641SAndroid Build Coastguard Worker     // and type (HELLO_MESSAGE_TYPE).
76*635a8641SAndroid Build Coastguard Worker     HELLO_MESSAGE_TYPE = UINT16_MAX,
77*635a8641SAndroid Build Coastguard Worker     // The CLOSE_FD_MESSAGE_TYPE is used in the IPC class to
78*635a8641SAndroid Build Coastguard Worker     // work around a bug in sendmsg() on Mac. When an FD is sent
79*635a8641SAndroid Build Coastguard Worker     // over the socket, a CLOSE_FD_MESSAGE is sent with hops = 2.
80*635a8641SAndroid Build Coastguard Worker     // The client will return the message with hops = 1, *after* it
81*635a8641SAndroid Build Coastguard Worker     // has received the message that contains the FD. When we
82*635a8641SAndroid Build Coastguard Worker     // receive it again on the sender side, we close the FD.
83*635a8641SAndroid Build Coastguard Worker     CLOSE_FD_MESSAGE_TYPE = HELLO_MESSAGE_TYPE - 1
84*635a8641SAndroid Build Coastguard Worker   };
85*635a8641SAndroid Build Coastguard Worker 
86*635a8641SAndroid Build Coastguard Worker   // Helper interface a Channel may implement to expose support for associated
87*635a8641SAndroid Build Coastguard Worker   // Mojo interfaces.
88*635a8641SAndroid Build Coastguard Worker   class COMPONENT_EXPORT(IPC) AssociatedInterfaceSupport {
89*635a8641SAndroid Build Coastguard Worker    public:
90*635a8641SAndroid Build Coastguard Worker     using GenericAssociatedInterfaceFactory =
91*635a8641SAndroid Build Coastguard Worker         base::Callback<void(mojo::ScopedInterfaceEndpointHandle)>;
92*635a8641SAndroid Build Coastguard Worker 
93*635a8641SAndroid Build Coastguard Worker     virtual ~AssociatedInterfaceSupport() {}
94*635a8641SAndroid Build Coastguard Worker 
95*635a8641SAndroid Build Coastguard Worker     // Returns a ThreadSafeForwarded for this channel which can be used to
96*635a8641SAndroid Build Coastguard Worker     // safely send mojom::Channel requests from arbitrary threads.
97*635a8641SAndroid Build Coastguard Worker     virtual std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>>
98*635a8641SAndroid Build Coastguard Worker     CreateThreadSafeChannel() = 0;
99*635a8641SAndroid Build Coastguard Worker 
100*635a8641SAndroid Build Coastguard Worker     // Adds an interface factory to this channel for interface |name|. Must be
101*635a8641SAndroid Build Coastguard Worker     // safe to call from any thread.
102*635a8641SAndroid Build Coastguard Worker     virtual void AddGenericAssociatedInterface(
103*635a8641SAndroid Build Coastguard Worker         const std::string& name,
104*635a8641SAndroid Build Coastguard Worker         const GenericAssociatedInterfaceFactory& factory) = 0;
105*635a8641SAndroid Build Coastguard Worker 
106*635a8641SAndroid Build Coastguard Worker     // Requests an associated interface from the remote endpoint.
107*635a8641SAndroid Build Coastguard Worker     virtual void GetGenericRemoteAssociatedInterface(
108*635a8641SAndroid Build Coastguard Worker         const std::string& name,
109*635a8641SAndroid Build Coastguard Worker         mojo::ScopedInterfaceEndpointHandle handle) = 0;
110*635a8641SAndroid Build Coastguard Worker 
111*635a8641SAndroid Build Coastguard Worker     // Template helper to add an interface factory to this channel.
112*635a8641SAndroid Build Coastguard Worker     template <typename Interface>
113*635a8641SAndroid Build Coastguard Worker     using AssociatedInterfaceFactory =
114*635a8641SAndroid Build Coastguard Worker         base::Callback<void(mojo::AssociatedInterfaceRequest<Interface>)>;
115*635a8641SAndroid Build Coastguard Worker     template <typename Interface>
116*635a8641SAndroid Build Coastguard Worker     void AddAssociatedInterface(
117*635a8641SAndroid Build Coastguard Worker         const AssociatedInterfaceFactory<Interface>& factory) {
118*635a8641SAndroid Build Coastguard Worker       AddGenericAssociatedInterface(
119*635a8641SAndroid Build Coastguard Worker           Interface::Name_,
120*635a8641SAndroid Build Coastguard Worker           base::Bind(&BindAssociatedInterfaceRequest<Interface>, factory));
121*635a8641SAndroid Build Coastguard Worker     }
122*635a8641SAndroid Build Coastguard Worker 
123*635a8641SAndroid Build Coastguard Worker     // Template helper to request a remote associated interface.
124*635a8641SAndroid Build Coastguard Worker     template <typename Interface>
125*635a8641SAndroid Build Coastguard Worker     void GetRemoteAssociatedInterface(
126*635a8641SAndroid Build Coastguard Worker         mojo::AssociatedInterfacePtr<Interface>* proxy) {
127*635a8641SAndroid Build Coastguard Worker       auto request = mojo::MakeRequest(proxy);
128*635a8641SAndroid Build Coastguard Worker       GetGenericRemoteAssociatedInterface(
129*635a8641SAndroid Build Coastguard Worker           Interface::Name_, request.PassHandle());
130*635a8641SAndroid Build Coastguard Worker     }
131*635a8641SAndroid Build Coastguard Worker 
132*635a8641SAndroid Build Coastguard Worker    private:
133*635a8641SAndroid Build Coastguard Worker     template <typename Interface>
134*635a8641SAndroid Build Coastguard Worker     static void BindAssociatedInterfaceRequest(
135*635a8641SAndroid Build Coastguard Worker         const AssociatedInterfaceFactory<Interface>& factory,
136*635a8641SAndroid Build Coastguard Worker         mojo::ScopedInterfaceEndpointHandle handle) {
137*635a8641SAndroid Build Coastguard Worker       factory.Run(
138*635a8641SAndroid Build Coastguard Worker           mojo::AssociatedInterfaceRequest<Interface>(std::move(handle)));
139*635a8641SAndroid Build Coastguard Worker     }
140*635a8641SAndroid Build Coastguard Worker   };
141*635a8641SAndroid Build Coastguard Worker 
142*635a8641SAndroid Build Coastguard Worker   // The maximum message size in bytes. Attempting to receive a message of this
143*635a8641SAndroid Build Coastguard Worker   // size or bigger results in a channel error.
144*635a8641SAndroid Build Coastguard Worker   static constexpr size_t kMaximumMessageSize = 128 * 1024 * 1024;
145*635a8641SAndroid Build Coastguard Worker 
146*635a8641SAndroid Build Coastguard Worker   // Amount of data to read at once from the pipe.
147*635a8641SAndroid Build Coastguard Worker   static const size_t kReadBufferSize = 4 * 1024;
148*635a8641SAndroid Build Coastguard Worker 
149*635a8641SAndroid Build Coastguard Worker   // Maximum persistent read buffer size. Read buffer can grow larger to
150*635a8641SAndroid Build Coastguard Worker   // accommodate large messages, but it's recommended to shrink back to this
151*635a8641SAndroid Build Coastguard Worker   // value because it fits 99.9% of all messages (see issue 529940 for data).
152*635a8641SAndroid Build Coastguard Worker   static const size_t kMaximumReadBufferSize = 64 * 1024;
153*635a8641SAndroid Build Coastguard Worker 
154*635a8641SAndroid Build Coastguard Worker   // Initialize a Channel.
155*635a8641SAndroid Build Coastguard Worker   //
156*635a8641SAndroid Build Coastguard Worker   // |channel_handle| identifies the communication Channel. For POSIX, if
157*635a8641SAndroid Build Coastguard Worker   // the file descriptor in the channel handle is != -1, the channel takes
158*635a8641SAndroid Build Coastguard Worker   // ownership of the file descriptor and will close it appropriately, otherwise
159*635a8641SAndroid Build Coastguard Worker   // it will create a new descriptor internally.
160*635a8641SAndroid Build Coastguard Worker   // |listener| receives a callback on the current thread for each newly
161*635a8641SAndroid Build Coastguard Worker   // received message.
162*635a8641SAndroid Build Coastguard Worker   //
163*635a8641SAndroid Build Coastguard Worker   // There are four type of modes how channels operate:
164*635a8641SAndroid Build Coastguard Worker   //
165*635a8641SAndroid Build Coastguard Worker   // - Server and named server: In these modes, the Channel is
166*635a8641SAndroid Build Coastguard Worker   //   responsible for settingb up the IPC object
167*635a8641SAndroid Build Coastguard Worker   // - An "open" named server: It accepts connections from ANY client.
168*635a8641SAndroid Build Coastguard Worker   //   The caller must then implement their own access-control based on the
169*635a8641SAndroid Build Coastguard Worker   //   client process' user Id.
170*635a8641SAndroid Build Coastguard Worker   // - Client and named client: In these mode, the Channel merely
171*635a8641SAndroid Build Coastguard Worker   //   connects to the already established IPC object.
172*635a8641SAndroid Build Coastguard Worker   //
173*635a8641SAndroid Build Coastguard Worker   // Each mode has its own Create*() API to create the Channel object.
174*635a8641SAndroid Build Coastguard Worker   static std::unique_ptr<Channel> Create(
175*635a8641SAndroid Build Coastguard Worker       const IPC::ChannelHandle& channel_handle,
176*635a8641SAndroid Build Coastguard Worker       Mode mode,
177*635a8641SAndroid Build Coastguard Worker       Listener* listener);
178*635a8641SAndroid Build Coastguard Worker 
179*635a8641SAndroid Build Coastguard Worker   static std::unique_ptr<Channel> CreateClient(
180*635a8641SAndroid Build Coastguard Worker       const IPC::ChannelHandle& channel_handle,
181*635a8641SAndroid Build Coastguard Worker       Listener* listener,
182*635a8641SAndroid Build Coastguard Worker       const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
183*635a8641SAndroid Build Coastguard Worker 
184*635a8641SAndroid Build Coastguard Worker   static std::unique_ptr<Channel> CreateServer(
185*635a8641SAndroid Build Coastguard Worker       const IPC::ChannelHandle& channel_handle,
186*635a8641SAndroid Build Coastguard Worker       Listener* listener,
187*635a8641SAndroid Build Coastguard Worker       const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
188*635a8641SAndroid Build Coastguard Worker 
189*635a8641SAndroid Build Coastguard Worker   ~Channel() override;
190*635a8641SAndroid Build Coastguard Worker 
191*635a8641SAndroid Build Coastguard Worker   // Connect the pipe.  On the server side, this will initiate
192*635a8641SAndroid Build Coastguard Worker   // waiting for connections.  On the client, it attempts to
193*635a8641SAndroid Build Coastguard Worker   // connect to a pre-existing pipe.  Note, calling Connect()
194*635a8641SAndroid Build Coastguard Worker   // will not block the calling thread and may complete
195*635a8641SAndroid Build Coastguard Worker   // asynchronously.
196*635a8641SAndroid Build Coastguard Worker   //
197*635a8641SAndroid Build Coastguard Worker   // The subclass implementation must call WillConnect() at the beginning of its
198*635a8641SAndroid Build Coastguard Worker   // implementation.
199*635a8641SAndroid Build Coastguard Worker   virtual bool Connect() WARN_UNUSED_RESULT = 0;
200*635a8641SAndroid Build Coastguard Worker 
201*635a8641SAndroid Build Coastguard Worker   // Pause the channel. Subsequent sends will be queued internally until
202*635a8641SAndroid Build Coastguard Worker   // Unpause() is called and the channel is flushed either by Unpause() or a
203*635a8641SAndroid Build Coastguard Worker   // subsequent call to Flush().
204*635a8641SAndroid Build Coastguard Worker   virtual void Pause();
205*635a8641SAndroid Build Coastguard Worker 
206*635a8641SAndroid Build Coastguard Worker   // Unpause the channel. This allows subsequent Send() calls to transmit
207*635a8641SAndroid Build Coastguard Worker   // messages immediately, without queueing. If |flush| is true, any messages
208*635a8641SAndroid Build Coastguard Worker   // queued while paused will be flushed immediately upon unpausing. Otherwise
209*635a8641SAndroid Build Coastguard Worker   // you must call Flush() explicitly.
210*635a8641SAndroid Build Coastguard Worker   //
211*635a8641SAndroid Build Coastguard Worker   // Not all implementations support Unpause(). See ConnectPaused() above for
212*635a8641SAndroid Build Coastguard Worker   // details.
213*635a8641SAndroid Build Coastguard Worker   virtual void Unpause(bool flush);
214*635a8641SAndroid Build Coastguard Worker 
215*635a8641SAndroid Build Coastguard Worker   // Manually flush the pipe. This is only useful exactly once, and only after
216*635a8641SAndroid Build Coastguard Worker   // a call to Unpause(false), in order to explicitly flush out any
217*635a8641SAndroid Build Coastguard Worker   // messages which were queued prior to unpausing.
218*635a8641SAndroid Build Coastguard Worker   //
219*635a8641SAndroid Build Coastguard Worker   // Not all implementations support Flush(). See ConnectPaused() above for
220*635a8641SAndroid Build Coastguard Worker   // details.
221*635a8641SAndroid Build Coastguard Worker   virtual void Flush();
222*635a8641SAndroid Build Coastguard Worker 
223*635a8641SAndroid Build Coastguard Worker   // Close this Channel explicitly.  May be called multiple times.
224*635a8641SAndroid Build Coastguard Worker   // On POSIX calling close on an IPC channel that listens for connections will
225*635a8641SAndroid Build Coastguard Worker   // cause it to close any accepted connections, and it will stop listening for
226*635a8641SAndroid Build Coastguard Worker   // new connections. If you just want to close the currently accepted
227*635a8641SAndroid Build Coastguard Worker   // connection and listen for new ones, use ResetToAcceptingConnectionState.
228*635a8641SAndroid Build Coastguard Worker   virtual void Close() = 0;
229*635a8641SAndroid Build Coastguard Worker 
230*635a8641SAndroid Build Coastguard Worker   // Gets a helper for associating Mojo interfaces with this Channel.
231*635a8641SAndroid Build Coastguard Worker   //
232*635a8641SAndroid Build Coastguard Worker   // NOTE: Not all implementations support this.
233*635a8641SAndroid Build Coastguard Worker   virtual AssociatedInterfaceSupport* GetAssociatedInterfaceSupport();
234*635a8641SAndroid Build Coastguard Worker 
235*635a8641SAndroid Build Coastguard Worker   // Overridden from ipc::Sender.
236*635a8641SAndroid Build Coastguard Worker   // Send a message over the Channel to the listener on the other end.
237*635a8641SAndroid Build Coastguard Worker   //
238*635a8641SAndroid Build Coastguard Worker   // |message| must be allocated using operator new.  This object will be
239*635a8641SAndroid Build Coastguard Worker   // deleted once the contents of the Message have been sent.
240*635a8641SAndroid Build Coastguard Worker   bool Send(Message* message) override = 0;
241*635a8641SAndroid Build Coastguard Worker 
242*635a8641SAndroid Build Coastguard Worker #if !defined(OS_NACL_SFI)
243*635a8641SAndroid Build Coastguard Worker   // Generates a channel ID that's non-predictable and unique.
244*635a8641SAndroid Build Coastguard Worker   static std::string GenerateUniqueRandomChannelID();
245*635a8641SAndroid Build Coastguard Worker #endif
246*635a8641SAndroid Build Coastguard Worker 
247*635a8641SAndroid Build Coastguard Worker #if defined(OS_LINUX)
248*635a8641SAndroid Build Coastguard Worker   // Sandboxed processes live in a PID namespace, so when sending the IPC hello
249*635a8641SAndroid Build Coastguard Worker   // message from client to server we need to send the PID from the global
250*635a8641SAndroid Build Coastguard Worker   // PID namespace.
251*635a8641SAndroid Build Coastguard Worker   static void SetGlobalPid(int pid);
252*635a8641SAndroid Build Coastguard Worker   static int GetGlobalPid();
253*635a8641SAndroid Build Coastguard Worker #endif
254*635a8641SAndroid Build Coastguard Worker 
255*635a8641SAndroid Build Coastguard Worker  protected:
256*635a8641SAndroid Build Coastguard Worker   // Subclasses must call this method at the beginning of their implementation
257*635a8641SAndroid Build Coastguard Worker   // of Connect().
258*635a8641SAndroid Build Coastguard Worker   void WillConnect();
259*635a8641SAndroid Build Coastguard Worker 
260*635a8641SAndroid Build Coastguard Worker  private:
261*635a8641SAndroid Build Coastguard Worker   bool did_start_connect_ = false;
262*635a8641SAndroid Build Coastguard Worker };
263*635a8641SAndroid Build Coastguard Worker 
264*635a8641SAndroid Build Coastguard Worker }  // namespace IPC
265*635a8641SAndroid Build Coastguard Worker 
266*635a8641SAndroid Build Coastguard Worker #endif  // IPC_IPC_CHANNEL_H_
267