Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / nacl / loader / nacl_ipc_adapter.h
blob4a0a4b77aa09899df3e3cfd66cdef01ab5bf0aab
1 // Copyright 2013 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 CHROME_NACL_NACL_IPC_ADAPTER_H_
6 #define CHROME_NACL_NACL_IPC_ADAPTER_H_
8 #include <map>
9 #include <queue>
10 #include <string>
11 #include <vector>
13 #include "base/basictypes.h"
14 #include "base/callback.h"
15 #include "base/files/scoped_file.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/scoped_vector.h"
19 #include "base/memory/shared_memory.h"
20 #include "base/pickle.h"
21 #include "base/synchronization/condition_variable.h"
22 #include "base/synchronization/lock.h"
23 #include "base/task_runner.h"
24 #include "ipc/ipc_listener.h"
25 #include "ipc/ipc_platform_file.h"
26 #include "ppapi/c/pp_stdint.h"
27 #include "ppapi/proxy/nacl_message_scanner.h"
29 struct NaClDesc;
30 struct NaClImcTypedMsgHdr;
31 struct PP_Size;
33 namespace IPC {
34 class Channel;
35 struct ChannelHandle;
38 namespace ppapi {
39 class HostResource;
42 // Adapts a Chrome IPC channel to an IPC channel that we expose to Native
43 // Client. This provides a mapping in both directions, so when IPC messages
44 // come in from another process, we rewrite them and allow them to be received
45 // via a recvmsg-like interface in the NaCl code. When NaCl code calls sendmsg,
46 // we implement that as sending IPC messages on the channel.
48 // This object also provides the necessary logic for rewriting IPC messages.
49 // NaCl code is platform-independent and runs in a Posix-like enviroment, but
50 // some formatting in the message and the way handles are transferred varies
51 // by platform. This class bridges that gap to provide what looks like a
52 // normal platform-specific IPC implementation to Chrome, and a Posix-like
53 // version on every platform to NaCl.
55 // This object must be threadsafe since the nacl environment determines which
56 // thread every function is called on.
57 class NaClIPCAdapter : public base::RefCountedThreadSafe<NaClIPCAdapter>,
58 public IPC::Listener {
59 public:
60 // Chrome's IPC message format varies by platform, NaCl's does not. In
61 // particular, the header has some extra fields on Posix platforms. Since
62 // NaCl is a Posix environment, it gets that version of the header. This
63 // header is duplicated here so we have a cross-platform definition of the
64 // header we're exposing to NaCl.
65 #pragma pack(push, 4)
66 struct NaClMessageHeader : public Pickle::Header {
67 int32 routing;
68 uint32 type;
69 uint32 flags;
70 uint16 num_fds;
71 uint16 pad;
73 #pragma pack(pop)
75 // Creates an adapter, using the thread associated with the given task
76 // runner for posting messages. In normal use, the task runner will post to
77 // the I/O thread of the process.
79 // If you use this constructor, you MUST call ConnectChannel after the
80 // NaClIPCAdapter is constructed, or the NaClIPCAdapter's channel will not be
81 // connected.
82 NaClIPCAdapter(const IPC::ChannelHandle& handle, base::TaskRunner* runner);
84 // Initializes with a given channel that's already created for testing
85 // purposes. This function will take ownership of the given channel.
86 NaClIPCAdapter(scoped_ptr<IPC::Channel> channel, base::TaskRunner* runner);
88 // Connect the channel. This must be called after the constructor that accepts
89 // an IPC::ChannelHandle, and causes the Channel to be connected on the IO
90 // thread.
91 void ConnectChannel();
93 // Implementation of sendmsg. Returns the number of bytes written or -1 on
94 // failure.
95 int Send(const NaClImcTypedMsgHdr* msg);
97 // Implementation of recvmsg. Returns the number of bytes read or -1 on
98 // failure. This will block until there's an error or there is data to
99 // read.
100 int BlockingReceive(NaClImcTypedMsgHdr* msg);
102 // Closes the IPC channel.
103 void CloseChannel();
105 // Make a NaClDesc that refers to this NaClIPCAdapter. Note that the returned
106 // NaClDesc is reference-counted, and a reference is returned.
107 NaClDesc* MakeNaClDesc();
109 #if defined(OS_POSIX)
110 base::ScopedFD TakeClientFileDescriptor();
111 #endif
113 // Listener implementation.
114 bool OnMessageReceived(const IPC::Message& message) override;
115 void OnChannelConnected(int32 peer_pid) override;
116 void OnChannelError() override;
118 typedef base::Callback<void(IPC::PlatformFileForTransit, base::FilePath)>
119 ResolveFileTokenReplyCallback;
121 typedef base::Callback<void(uint64_t, // file_token_lo
122 uint64_t, // file_token_hi
123 ResolveFileTokenReplyCallback)>
124 ResolveFileTokenCallback;
126 // Sets a callback to be invoked for resolving file tokens received from the
127 // renderer. When the file token is resolved, the
128 // ResolveFileTokenReplyCallback passed inside the ResolveFileTokenCallback
129 // will be invoked.
130 void set_resolve_file_token_callback(ResolveFileTokenCallback cb) {
131 resolve_file_token_cb_ = cb;
134 private:
135 friend class base::RefCountedThreadSafe<NaClIPCAdapter>;
137 class RewrittenMessage;
139 // This is the data that must only be accessed inside the lock. This struct
140 // just separates it so it's easier to see.
141 struct LockedData {
142 LockedData();
143 ~LockedData();
145 // Messages that we have read off of the Chrome IPC channel that are waiting
146 // to be received by the plugin.
147 std::queue< scoped_refptr<RewrittenMessage> > to_be_received_;
149 ppapi::proxy::NaClMessageScanner nacl_msg_scanner_;
151 // Data that we've queued from the plugin to send, but doesn't consist of a
152 // full message yet. The calling code can break apart the message into
153 // smaller pieces, and we need to send the message to the other process in
154 // one chunk.
156 // The IPC channel always starts a new send() at the beginning of each
157 // message, so we don't need to worry about arbitrary message boundaries.
158 std::string to_be_sent_;
160 bool channel_closed_;
163 // This is the data that must only be accessed on the I/O thread (as defined
164 // by TaskRunner). This struct just separates it so it's easier to see.
165 struct IOThreadData {
166 IOThreadData();
167 ~IOThreadData();
169 scoped_ptr<IPC::Channel> channel_;
171 // When we send a synchronous message (from untrusted to trusted), we store
172 // its type here, so that later we can associate the reply with its type
173 // for scanning.
174 typedef std::map<int, uint32> PendingSyncMsgMap;
175 PendingSyncMsgMap pending_sync_msgs_;
178 ~NaClIPCAdapter() override;
180 void OnFileTokenResolved(const IPC::Message& orig_msg,
181 IPC::PlatformFileForTransit ipc_fd,
182 base::FilePath file_path);
184 bool RewriteMessage(const IPC::Message& msg, uint32_t type);
186 // Returns 0 if nothing is waiting.
187 int LockedReceive(NaClImcTypedMsgHdr* msg);
189 // Sends a message that we know has been completed to the Chrome process.
190 bool SendCompleteMessage(const char* buffer, size_t buffer_len);
192 // Clears the LockedData.to_be_sent_ structure in a way to make sure that
193 // the memory is deleted. std::string can sometimes hold onto the buffer
194 // for future use which we don't want.
195 void ClearToBeSent();
197 void ConnectChannelOnIOThread();
198 void CloseChannelOnIOThread();
199 void SendMessageOnIOThread(scoped_ptr<IPC::Message> message);
201 // Saves the message to forward to NaCl. This method assumes that the caller
202 // holds the lock for locked_data_.
203 void SaveMessage(const IPC::Message& message,
204 RewrittenMessage* rewritten_message);
206 base::Lock lock_;
207 base::ConditionVariable cond_var_;
209 scoped_refptr<base::TaskRunner> task_runner_;
211 ResolveFileTokenCallback resolve_file_token_cb_;
213 // To be accessed inside of lock_ only.
214 LockedData locked_data_;
216 // To be accessed on the I/O thread (via task runner) only.
217 IOThreadData io_thread_data_;
219 DISALLOW_COPY_AND_ASSIGN(NaClIPCAdapter);
222 // Export TranslatePepperFileReadWriteOpenFlags for testing.
223 int TranslatePepperFileReadWriteOpenFlagsForTesting(int32_t pp_open_flags);
225 #endif // CHROME_NACL_NACL_IPC_ADAPTER_H_