xref: /aosp_15_r20/external/cronet/ipc/ipc_channel_nacl.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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 IPC_IPC_CHANNEL_NACL_H_
6 #define IPC_IPC_CHANNEL_NACL_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/containers/circular_deque.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/process/process.h"
14 #include "base/threading/simple_thread.h"
15 #include "ipc/ipc_channel.h"
16 #include "ipc/ipc_channel_reader.h"
17 
18 namespace IPC {
19 
20 class MessageAttachment;
21 
22 // Contains the results from one call to imc_recvmsg (data and file
23 // descriptors).
24 struct MessageContents;
25 
26 // Similar to the ChannelPosix but for Native Client code.
27 // This is somewhat different because sendmsg/recvmsg here do not follow POSIX
28 // semantics. Instead, they are implemented by a custom embedding of
29 // NaClDescCustom. See NaClIPCAdapter for the trusted-side implementation.
30 //
31 // We don't need to worry about complicated set up and READWRITE mode for
32 // sharing handles. We also currently do not support passing file descriptors or
33 // named pipes, and we use background threads to emulate signaling when we can
34 // read or write without blocking.
35 class ChannelNacl : public Channel,
36                     public internal::ChannelReader {
37  public:
38   ChannelNacl() = delete;
39 
40   // Mirror methods of Channel, see ipc_channel.h for description.
41   ChannelNacl(const IPC::ChannelHandle& channel_handle,
42               Mode mode,
43               Listener* listener);
44 
45   ChannelNacl(const ChannelNacl&) = delete;
46   ChannelNacl& operator=(const ChannelNacl&) = delete;
47 
48   ~ChannelNacl() override;
49 
50   // Channel implementation.
51   bool Connect() override;
52   void Close() override;
53   bool Send(Message* message) override;
54 
55   // Posted to the main thread by ReaderThreadRunner.
56   void DidRecvMsg(std::unique_ptr<MessageContents> contents);
57   void ReadDidFail();
58 
59  private:
60   class ReaderThreadRunner;
61 
62   bool CreatePipe(const IPC::ChannelHandle& channel_handle);
63   bool ProcessOutgoingMessages();
64   void CallOnChannelConnected();
65 
66   // ChannelReader implementation.
67   ReadState ReadData(char* buffer,
68                      int buffer_len,
69                      int* bytes_read) override;
70   bool ShouldDispatchInputMessage(Message* msg) override;
71   bool GetAttachments(Message* msg) override;
72   bool DidEmptyInputBuffers() override;
73   void HandleInternalMessage(const Message& msg) override;
74 
75   Mode mode_;
76   bool waiting_connect_;
77 
78   // The pipe used for communication.
79   int pipe_;
80 
81   // We use a thread for reading, so that we can simply block on reading and
82   // post the received data back to the main thread to be properly interleaved
83   // with other tasks in the MessagePump.
84   //
85   // imc_recvmsg supports non-blocking reads, but there's no easy way to be
86   // informed when a write or read can be done without blocking (this is handled
87   // by libevent in Posix).
88   std::unique_ptr<ReaderThreadRunner> reader_thread_runner_;
89   std::unique_ptr<base::DelegateSimpleThread> reader_thread_;
90 
91   // IPC::ChannelReader expects to be able to call ReadData on us to
92   // synchronously read data waiting in the pipe's buffer without blocking.
93   // Since we can't do that (see 1 and 2 above), the reader thread does blocking
94   // reads and posts the data over to the main thread in MessageContents. Each
95   // MessageContents object is the result of one call to "imc_recvmsg".
96   // DidRecvMsg breaks the MessageContents out in to the data and the file
97   // descriptors, and puts them on these two queues.
98   // TODO(dmichael): There's probably a more efficient way to emulate this with
99   //                 a circular buffer or something, so we don't have to do so
100   //                 many heap allocations. But it maybe isn't worth
101   //                 the trouble given that we probably want to implement 1 and
102   //                 2 above in NaCl eventually.
103   // When ReadData is called, it pulls the bytes out of this queue in order.
104   base::circular_deque<std::unique_ptr<std::vector<char>>> read_queue_;
105   // Queue of file descriptor attachments extracted from imc_recvmsg messages.
106   std::vector<scoped_refptr<MessageAttachment>> input_attachments_;
107 
108   // This queue is used when a message is sent prior to Connect having been
109   // called. Normally after we're connected, the queue is empty.
110   base::circular_deque<std::unique_ptr<Message>> output_queue_;
111 
112   base::WeakPtrFactory<ChannelNacl> weak_ptr_factory_;
113 };
114 
115 }  // namespace IPC
116 
117 #endif  // IPC_IPC_CHANNEL_NACL_H_
118