Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1979)

Side by Side Diff: ppapi/proxy/plugin_main_nacl.cc

Issue 140573003: Connect PPAPI IPC channels for non-SFI mode. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« ppapi/proxy/plugin_main.cc ('K') | « ppapi/proxy/plugin_main.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 #include <map>
6 #include <set>
7
8 #include "build/build_config.h"
9 // Need to include this before most other files because it defines
10 // IPC_MESSAGE_LOG_ENABLED. We need to use it to define
11 // IPC_MESSAGE_MACROS_LOG_ENABLED so ppapi_messages.h will generate the
12 // ViewMsgLog et al. functions.
13
14 #include "base/command_line.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/synchronization/waitable_event.h"
18 #include "base/threading/thread.h"
19 #include "components/tracing/child_trace_message_filter.h"
20 #include "ipc/ipc_channel_handle.h"
21 #include "ipc/ipc_logging.h"
22 #include "ipc/ipc_message.h"
23 #include "native_client/src/shared/srpc/nacl_srpc.h"
24 #include "native_client/src/untrusted/irt/irt_ppapi.h"
25 #include "ppapi/c/ppp.h"
26 #include "ppapi/c/ppp_instance.h"
27 #include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h"
28 #include "ppapi/proxy/plugin_dispatcher.h"
29 #include "ppapi/proxy/plugin_globals.h"
30 #include "ppapi/proxy/plugin_message_filter.h"
31 #include "ppapi/proxy/plugin_proxy_delegate.h"
32 #include "ppapi/proxy/resource_reply_thread_registrar.h"
33 #include "ppapi/shared_impl/ppapi_switches.h"
34 #include "ppapi/shared_impl/ppb_audio_shared.h"
35
36 #if defined(IPC_MESSAGE_LOG_ENABLED)
37 #include "base/containers/hash_tables.h"
38
39 LogFunctionMap g_log_function_mapping;
40
41 #define IPC_MESSAGE_MACROS_LOG_ENABLED
42 #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \
43 g_log_function_mapping[msg_id] = logger
44
45 #endif
46 #include "ppapi/proxy/ppapi_messages.h"
47
48 // This value must be sync'ed with the one defined in the sel_main_chrome.h.
49 #define NACL_CHROME_DESC_BASE 6
50
51 using ppapi::proxy::PluginDispatcher;
52 using ppapi::proxy::PluginGlobals;
53 using ppapi::proxy::PluginProxyDelegate;
54 using ppapi::proxy::ProxyChannel;
55 using ppapi::proxy::SerializedHandle;
56
57 namespace {
58
59 // This class manages communication between the plugin and the browser, and
60 // manages the PluginDispatcher instances for communication between the plugin
61 // and the renderer.
62 class PpapiDispatcher : public ProxyChannel,
63 public PluginDispatcher::PluginDelegate,
64 public PluginProxyDelegate {
65 public:
66 explicit PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop);
67
68 // PluginDispatcher::PluginDelegate implementation.
69 virtual base::MessageLoopProxy* GetIPCMessageLoop() OVERRIDE;
70 virtual base::WaitableEvent* GetShutdownEvent() OVERRIDE;
71 virtual IPC::PlatformFileForTransit ShareHandleWithRemote(
72 base::PlatformFile handle,
73 base::ProcessId peer_pid,
74 bool should_close_source) OVERRIDE;
75 virtual std::set<PP_Instance>* GetGloballySeenInstanceIDSet() OVERRIDE;
76 virtual uint32 Register(PluginDispatcher* plugin_dispatcher) OVERRIDE;
77 virtual void Unregister(uint32 plugin_dispatcher_id) OVERRIDE;
78
79 // PluginProxyDelegate implementation.
80 virtual IPC::Sender* GetBrowserSender() OVERRIDE;
81 virtual std::string GetUILanguage() OVERRIDE;
82 virtual void PreCacheFont(const void* logfontw) OVERRIDE;
83 virtual void SetActiveURL(const std::string& url) OVERRIDE;
84 virtual PP_Resource CreateBrowserFont(
85 ppapi::proxy::Connection connection,
86 PP_Instance instance,
87 const PP_BrowserFont_Trusted_Description& desc,
88 const ppapi::Preferences& prefs) OVERRIDE;
89
90 // IPC::Listener implementation.
91 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
92
93 private:
94 void OnMsgInitializeNaClDispatcher(const ppapi::PpapiNaClPluginArgs& args);
95 void OnPluginDispatcherMessageReceived(const IPC::Message& msg);
96
97 void SetPpapiKeepAliveThrottleFromCommandLine();
98
99 std::set<PP_Instance> instances_;
100 std::map<uint32, PluginDispatcher*> plugin_dispatchers_;
101 uint32 next_plugin_dispatcher_id_;
102 scoped_refptr<base::MessageLoopProxy> message_loop_;
103 base::WaitableEvent shutdown_event_;
104 };
105
106 PpapiDispatcher::PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop)
107 : next_plugin_dispatcher_id_(0),
108 message_loop_(io_loop),
109 shutdown_event_(true, false) {
110 // The first FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the
111 // browser.
112 IPC::ChannelHandle channel_handle(
113 "NaCl IPC", base::FileDescriptor(NACL_CHROME_DESC_BASE, false));
114 // We don't have/need a PID since handle sharing happens outside of the
115 // NaCl sandbox.
116 InitWithChannel(this, base::kNullProcessId, channel_handle,
117 false); // Channel is server.
118 channel()->AddFilter(new ppapi::proxy::PluginMessageFilter(
119 NULL, PluginGlobals::Get()->resource_reply_thread_registrar()));
120 channel()->AddFilter(
121 new tracing::ChildTraceMessageFilter(message_loop_.get()));
122 }
123
124 base::MessageLoopProxy* PpapiDispatcher::GetIPCMessageLoop() {
125 return message_loop_.get();
126 }
127
128 base::WaitableEvent* PpapiDispatcher::GetShutdownEvent() {
129 return &shutdown_event_;
130 }
131
132 IPC::PlatformFileForTransit PpapiDispatcher::ShareHandleWithRemote(
133 base::PlatformFile handle,
134 base::ProcessId peer_pid,
135 bool should_close_source) {
136 return IPC::InvalidPlatformFileForTransit();
137 }
138
139 std::set<PP_Instance>* PpapiDispatcher::GetGloballySeenInstanceIDSet() {
140 return &instances_;
141 }
142
143 uint32 PpapiDispatcher::Register(PluginDispatcher* plugin_dispatcher) {
144 if (!plugin_dispatcher ||
145 plugin_dispatchers_.size() >= std::numeric_limits<uint32>::max()) {
146 return 0;
147 }
148
149 uint32 id = 0;
150 do {
151 // Although it is unlikely, make sure that we won't cause any trouble
152 // when the counter overflows.
153 id = next_plugin_dispatcher_id_++;
154 } while (id == 0 ||
155 plugin_dispatchers_.find(id) != plugin_dispatchers_.end());
156 plugin_dispatchers_[id] = plugin_dispatcher;
157 return id;
158 }
159
160 void PpapiDispatcher::Unregister(uint32 plugin_dispatcher_id) {
161 plugin_dispatchers_.erase(plugin_dispatcher_id);
162 }
163
164 IPC::Sender* PpapiDispatcher::GetBrowserSender() {
165 return this;
166 }
167
168 std::string PpapiDispatcher::GetUILanguage() {
169 NOTIMPLEMENTED();
170 return std::string();
171 }
172
173 void PpapiDispatcher::PreCacheFont(const void* logfontw) {
174 NOTIMPLEMENTED();
175 }
176
177 void PpapiDispatcher::SetActiveURL(const std::string& url) {
178 NOTIMPLEMENTED();
179 }
180
181 PP_Resource PpapiDispatcher::CreateBrowserFont(
182 ppapi::proxy::Connection connection,
183 PP_Instance instance,
184 const PP_BrowserFont_Trusted_Description& desc,
185 const ppapi::Preferences& prefs) {
186 NOTIMPLEMENTED();
187 return 0;
188 }
189
190 bool PpapiDispatcher::OnMessageReceived(const IPC::Message& msg) {
191 IPC_BEGIN_MESSAGE_MAP(PpapiDispatcher, msg)
192 IPC_MESSAGE_HANDLER(PpapiMsg_InitializeNaClDispatcher,
193 OnMsgInitializeNaClDispatcher)
194 // All other messages are simply forwarded to a PluginDispatcher.
195 IPC_MESSAGE_UNHANDLED(OnPluginDispatcherMessageReceived(msg))
196 IPC_END_MESSAGE_MAP()
197 return true;
198 }
199
200 void PpapiDispatcher::OnMsgInitializeNaClDispatcher(
201 const ppapi::PpapiNaClPluginArgs& args) {
202 static bool command_line_and_logging_initialized = false;
203 if (command_line_and_logging_initialized) {
204 LOG(FATAL) << "InitializeNaClDispatcher must be called once per plugin.";
205 return;
206 }
207
208 command_line_and_logging_initialized = true;
209 CommandLine::Init(0, NULL);
210 for (size_t i = 0; i < args.switch_names.size(); ++i) {
211 DCHECK(i < args.switch_values.size());
212 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
213 args.switch_names[i], args.switch_values[i]);
214 }
215 logging::LoggingSettings settings;
216 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
217 logging::InitLogging(settings);
218 SetPpapiKeepAliveThrottleFromCommandLine();
219
220 // Tell the process-global GetInterface which interfaces it can return to the
221 // plugin.
222 ppapi::proxy::InterfaceList::SetProcessGlobalPermissions(
223 args.permissions);
224
225 int32_t error = ::PPP_InitializeModule(
226 0 /* module */,
227 &ppapi::proxy::PluginDispatcher::GetBrowserInterface);
228 if (error)
229 ::exit(error);
230
231 PluginDispatcher* dispatcher =
232 new PluginDispatcher(::PPP_GetInterface, args.permissions,
233 args.off_the_record);
234 // The channel handle's true name is not revealed here.
235 // The second FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the
236 // renderer.
237 IPC::ChannelHandle channel_handle(
238 "nacl", base::FileDescriptor(NACL_CHROME_DESC_BASE + 1, false));
239 if (!dispatcher->InitPluginWithChannel(this, base::kNullProcessId,
240 channel_handle, false)) {
241 delete dispatcher;
242 return;
243 }
244 // From here, the dispatcher will manage its own lifetime according to the
245 // lifetime of the attached channel.
246 }
247
248 void PpapiDispatcher::OnPluginDispatcherMessageReceived(
249 const IPC::Message& msg) {
250 // The first parameter should be a plugin dispatcher ID.
251 PickleIterator iter(msg);
252 uint32 id = 0;
253 if (!msg.ReadUInt32(&iter, &id)) {
254 NOTREACHED();
255 return;
256 }
257 std::map<uint32, ppapi::proxy::PluginDispatcher*>::iterator dispatcher =
258 plugin_dispatchers_.find(id);
259 if (dispatcher != plugin_dispatchers_.end())
260 dispatcher->second->OnMessageReceived(msg);
261 }
262
263 void PpapiDispatcher::SetPpapiKeepAliveThrottleFromCommandLine() {
264 unsigned keepalive_throttle_interval_milliseconds = 0;
265 if (base::StringToUint(
266 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
267 switches::kPpapiKeepAliveThrottle),
268 &keepalive_throttle_interval_milliseconds)) {
269 ppapi::proxy::PluginGlobals::Get()->
270 set_keepalive_throttle_interval_milliseconds(
271 keepalive_throttle_interval_milliseconds);
272 }
273 }
274
275 } // namespace
276
277 void PpapiPluginRegisterThreadCreator(
278 const struct PP_ThreadFunctions* thread_functions) {
279 // Initialize all classes that need to create threads that call back into
280 // user code.
281 ppapi::PPB_Audio_Shared::SetThreadFunctions(thread_functions);
282 }
283
284 int PpapiPluginMain() {
285 // Though it isn't referenced here, we must instantiate an AtExitManager.
286 base::AtExitManager exit_manager;
287 base::MessageLoop loop;
288 IPC::Logging::set_log_function_map(&g_log_function_mapping);
289 ppapi::proxy::PluginGlobals plugin_globals;
290 base::Thread io_thread("Chrome_NaClIOThread");
291 base::Thread::Options options;
292 options.message_loop_type = base::MessageLoop::TYPE_IO;
293 io_thread.StartWithOptions(options);
294
295 // Start up the SRPC server on another thread. Otherwise, when it blocks
296 // on an RPC, the PPAPI proxy will hang. Do this before we initialize the
297 // module and start the PPAPI proxy so that the NaCl plugin can continue
298 // loading the app.
299 static struct NaClSrpcHandlerDesc srpc_methods[] = { { NULL, NULL } };
300 if (!NaClSrpcAcceptClientOnThread(srpc_methods)) {
301 return 1;
302 }
303
304 PpapiDispatcher ppapi_dispatcher(io_thread.message_loop_proxy());
305 plugin_globals.set_plugin_proxy_delegate(&ppapi_dispatcher);
306
307 loop.Run();
308
309 return 0;
310 }
OLDNEW
« ppapi/proxy/plugin_main.cc ('K') | « ppapi/proxy/plugin_main.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698