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