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