Revert 285871 "Revert 285867 "Revert 285832 "Disable accessible ..."
[chromium-blink-merge.git] / ipc / ipc_channel_nacl.h
blobc47892d25c46d7b3b026b56b61057f6dddeb5dd7
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.
5 #ifndef IPC_IPC_CHANNEL_NACL_H_
6 #define IPC_IPC_CHANNEL_NACL_H_
8 #include <deque>
9 #include <string>
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/scoped_ptr.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"
19 namespace IPC {
21 // Contains the results from one call to imc_recvmsg (data and file
22 // descriptors).
23 struct MessageContents;
25 // Similar to the ChannelPosix but for Native Client code.
26 // This is somewhat different because sendmsg/recvmsg here do not follow POSIX
27 // semantics. Instead, they are implemented by a custom embedding of
28 // NaClDescCustom. See NaClIPCAdapter for the trusted-side implementation.
30 // We don't need to worry about complicated set up and READWRITE mode for
31 // sharing handles. We also currently do not support passing file descriptors or
32 // named pipes, and we use background threads to emulate signaling when we can
33 // read or write without blocking.
34 class ChannelNacl : public Channel,
35 public internal::ChannelReader {
36 public:
37 // Mirror methods of Channel, see ipc_channel.h for description.
38 ChannelNacl(const IPC::ChannelHandle& channel_handle,
39 Mode mode,
40 Listener* listener);
41 virtual ~ChannelNacl();
43 // Channel implementation.
44 virtual base::ProcessId GetPeerPID() const OVERRIDE;
45 virtual bool Connect() OVERRIDE;
46 virtual void Close() OVERRIDE;
47 virtual bool Send(Message* message) OVERRIDE;
49 // Posted to the main thread by ReaderThreadRunner.
50 void DidRecvMsg(scoped_ptr<MessageContents> contents);
51 void ReadDidFail();
53 private:
54 class ReaderThreadRunner;
56 bool CreatePipe(const IPC::ChannelHandle& channel_handle);
57 bool ProcessOutgoingMessages();
58 void CallOnChannelConnected();
60 // ChannelReader implementation.
61 virtual ReadState ReadData(char* buffer,
62 int buffer_len,
63 int* bytes_read) OVERRIDE;
64 virtual bool WillDispatchInputMessage(Message* msg) OVERRIDE;
65 virtual bool DidEmptyInputBuffers() OVERRIDE;
66 virtual void HandleInternalMessage(const Message& msg) OVERRIDE;
68 Mode mode_;
69 bool waiting_connect_;
71 // The pipe used for communication.
72 int pipe_;
74 // The "name" of our pipe. On Windows this is the global identifier for
75 // the pipe. On POSIX it's used as a key in a local map of file descriptors.
76 // For NaCl, we don't actually support looking up file descriptors by name,
77 // and it's only used for debug information.
78 std::string pipe_name_;
80 // We use a thread for reading, so that we can simply block on reading and
81 // post the received data back to the main thread to be properly interleaved
82 // with other tasks in the MessagePump.
84 // imc_recvmsg supports non-blocking reads, but there's no easy way to be
85 // informed when a write or read can be done without blocking (this is handled
86 // by libevent in Posix).
87 scoped_ptr<ReaderThreadRunner> reader_thread_runner_;
88 scoped_ptr<base::DelegateSimpleThread> reader_thread_;
90 // IPC::ChannelReader expects to be able to call ReadData on us to
91 // synchronously read data waiting in the pipe's buffer without blocking.
92 // Since we can't do that (see 1 and 2 above), the reader thread does blocking
93 // reads and posts the data over to the main thread in MessageContents. Each
94 // MessageContents object is the result of one call to "imc_recvmsg".
95 // DidRecvMsg breaks the MessageContents out in to the data and the file
96 // descriptors, and puts them on these two queues.
97 // TODO(dmichael): There's probably a more efficient way to emulate this with
98 // a circular buffer or something, so we don't have to do so
99 // many heap allocations. But it maybe isn't worth
100 // the trouble given that we probably want to implement 1 and
101 // 2 above in NaCl eventually.
102 // When ReadData is called, it pulls the bytes out of this queue in order.
103 std::deque<linked_ptr<std::vector<char> > > read_queue_;
104 // Queue of file descriptors extracted from imc_recvmsg messages.
105 // NOTE: The implementation assumes underlying storage here is contiguous, so
106 // don't change to something like std::deque<> without changing the
107 // implementation!
108 std::vector<int> input_fds_;
110 // This queue is used when a message is sent prior to Connect having been
111 // called. Normally after we're connected, the queue is empty.
112 std::deque<linked_ptr<Message> > output_queue_;
114 base::WeakPtrFactory<ChannelNacl> weak_ptr_factory_;
116 DISALLOW_IMPLICIT_CONSTRUCTORS(ChannelNacl);
119 } // namespace IPC
121 #endif // IPC_IPC_CHANNEL_NACL_H_