Suppress memory leadk in message center unittests.
[chromium-blink-merge.git] / ipc / ipc_channel_nacl.h
bloba21730eb81346c918ef37bd5a98549484e176840
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 Posix version of ChannelImpl 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 Channel::ChannelImpl : public internal::ChannelReader {
35 public:
36 // Mirror methods of Channel, see ipc_channel.h for description.
37 ChannelImpl(const IPC::ChannelHandle& channel_handle,
38 Mode mode,
39 Listener* listener);
40 virtual ~ChannelImpl();
42 // Channel implementation.
43 bool Connect();
44 void Close();
45 bool Send(Message* message);
47 // Posted to the main thread by ReaderThreadRunner.
48 void DidRecvMsg(scoped_ptr<MessageContents> contents);
49 void ReadDidFail();
51 private:
52 class ReaderThreadRunner;
54 bool CreatePipe(const IPC::ChannelHandle& channel_handle);
55 bool ProcessOutgoingMessages();
57 // ChannelReader implementation.
58 virtual ReadState ReadData(char* buffer,
59 int buffer_len,
60 int* bytes_read) OVERRIDE;
61 virtual bool WillDispatchInputMessage(Message* msg) OVERRIDE;
62 virtual bool DidEmptyInputBuffers() OVERRIDE;
63 virtual void HandleInternalMessage(const Message& msg) OVERRIDE;
65 Mode mode_;
66 bool waiting_connect_;
68 // The pipe used for communication.
69 int pipe_;
71 // The "name" of our pipe. On Windows this is the global identifier for
72 // the pipe. On POSIX it's used as a key in a local map of file descriptors.
73 // For NaCl, we don't actually support looking up file descriptors by name,
74 // and it's only used for debug information.
75 std::string pipe_name_;
77 // We use a thread for reading, so that we can simply block on reading and
78 // post the received data back to the main thread to be properly interleaved
79 // with other tasks in the MessagePump.
81 // imc_recvmsg supports non-blocking reads, but there's no easy way to be
82 // informed when a write or read can be done without blocking (this is handled
83 // by libevent in Posix).
84 scoped_ptr<ReaderThreadRunner> reader_thread_runner_;
85 scoped_ptr<base::DelegateSimpleThread> reader_thread_;
87 // IPC::ChannelReader expects to be able to call ReadData on us to
88 // synchronously read data waiting in the pipe's buffer without blocking.
89 // Since we can't do that (see 1 and 2 above), the reader thread does blocking
90 // reads and posts the data over to the main thread in MessageContents. Each
91 // MessageContents object is the result of one call to "imc_recvmsg".
92 // DidRecvMsg breaks the MessageContents out in to the data and the file
93 // descriptors, and puts them on these two queues.
94 // TODO(dmichael): There's probably a more efficient way to emulate this with
95 // a circular buffer or something, so we don't have to do so
96 // many heap allocations. But it maybe isn't worth
97 // the trouble given that we probably want to implement 1 and
98 // 2 above in NaCl eventually.
99 // When ReadData is called, it pulls the bytes out of this queue in order.
100 std::deque<linked_ptr<std::vector<char> > > read_queue_;
101 // Queue of file descriptors extracted from imc_recvmsg messages.
102 // NOTE: The implementation assumes underlying storage here is contiguous, so
103 // don't change to something like std::deque<> without changing the
104 // implementation!
105 std::vector<int> input_fds_;
107 // This queue is used when a message is sent prior to Connect having been
108 // called. Normally after we're connected, the queue is empty.
109 std::deque<linked_ptr<Message> > output_queue_;
111 base::WeakPtrFactory<ChannelImpl> weak_ptr_factory_;
113 DISALLOW_IMPLICIT_CONSTRUCTORS(ChannelImpl);
116 } // namespace IPC
118 #endif // IPC_IPC_CHANNEL_NACL_H_