OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/proxy/plugin_main_irt.h" | |
6 | |
5 #include <map> | 7 #include <map> |
6 #include <set> | 8 #include <set> |
7 | 9 |
8 #include "build/build_config.h" | 10 #include "build/build_config.h" |
9 // Need to include this before most other files because it defines | 11 // Need to include this before most other files because it defines |
10 // IPC_MESSAGE_LOG_ENABLED. We need to use it to define | 12 // IPC_MESSAGE_LOG_ENABLED. We need to use it to define |
11 // IPC_MESSAGE_MACROS_LOG_ENABLED so ppapi_messages.h will generate the | 13 // IPC_MESSAGE_MACROS_LOG_ENABLED so ppapi_messages.h will generate the |
12 // ViewMsgLog et al. functions. | 14 // ViewMsgLog et al. functions. |
13 | 15 |
16 #include "base/at_exit.h" | |
14 #include "base/command_line.h" | 17 #include "base/command_line.h" |
15 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
16 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
17 #include "base/synchronization/waitable_event.h" | 20 #include "base/synchronization/waitable_event.h" |
18 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
19 #include "components/tracing/child_trace_message_filter.h" | 22 #include "components/tracing/child_trace_message_filter.h" |
20 #include "ipc/ipc_channel_handle.h" | 23 #include "ipc/ipc_channel_handle.h" |
21 #include "ipc/ipc_logging.h" | 24 #include "ipc/ipc_logging.h" |
22 #include "ipc/ipc_message.h" | 25 #include "ipc/ipc_message.h" |
23 #include "native_client/src/public/chrome_main.h" | |
24 #include "native_client/src/shared/srpc/nacl_srpc.h" | |
25 #include "ppapi/c/ppp.h" | 26 #include "ppapi/c/ppp.h" |
26 #include "ppapi/c/ppp_instance.h" | 27 #include "ppapi/c/ppp_instance.h" |
27 #include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h" | |
28 #include "ppapi/proxy/plugin_dispatcher.h" | 28 #include "ppapi/proxy/plugin_dispatcher.h" |
29 #include "ppapi/proxy/plugin_globals.h" | 29 #include "ppapi/proxy/plugin_globals.h" |
30 #include "ppapi/proxy/plugin_message_filter.h" | 30 #include "ppapi/proxy/plugin_message_filter.h" |
31 #include "ppapi/proxy/plugin_proxy_delegate.h" | 31 #include "ppapi/proxy/plugin_proxy_delegate.h" |
32 #include "ppapi/proxy/resource_reply_thread_registrar.h" | 32 #include "ppapi/proxy/resource_reply_thread_registrar.h" |
33 #include "ppapi/shared_impl/ppapi_switches.h" | 33 #include "ppapi/shared_impl/ppapi_switches.h" |
34 #include "ppapi/shared_impl/ppb_audio_shared.h" | 34 #include "ppapi/shared_impl/ppb_audio_shared.h" |
35 | 35 |
36 #if defined(__native_client__) | |
37 #include "native_client/src/public/chrome_main.h" | |
38 #include "native_client/src/shared/srpc/nacl_srpc.h" | |
39 #endif | |
40 | |
36 #if defined(IPC_MESSAGE_LOG_ENABLED) | 41 #if defined(IPC_MESSAGE_LOG_ENABLED) |
37 #include "base/containers/hash_tables.h" | 42 #include "base/containers/hash_tables.h" |
38 | 43 |
39 LogFunctionMap g_log_function_mapping; | 44 LogFunctionMap g_log_function_mapping; |
40 | 45 |
41 #define IPC_MESSAGE_MACROS_LOG_ENABLED | 46 #define IPC_MESSAGE_MACROS_LOG_ENABLED |
42 #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \ | 47 #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \ |
43 g_log_function_mapping[msg_id] = logger | 48 g_log_function_mapping[msg_id] = logger |
44 | 49 |
45 #endif | 50 #endif |
46 #include "ppapi/proxy/ppapi_messages.h" | 51 #include "ppapi/proxy/ppapi_messages.h" |
47 | 52 |
48 using ppapi::proxy::PluginDispatcher; | 53 using ppapi::proxy::PluginDispatcher; |
49 using ppapi::proxy::PluginGlobals; | 54 using ppapi::proxy::PluginGlobals; |
50 using ppapi::proxy::PluginProxyDelegate; | 55 using ppapi::proxy::PluginProxyDelegate; |
51 using ppapi::proxy::ProxyChannel; | 56 using ppapi::proxy::ProxyChannel; |
52 using ppapi::proxy::SerializedHandle; | 57 using ppapi::proxy::SerializedHandle; |
53 | 58 |
54 namespace { | 59 namespace { |
55 | 60 |
61 #if defined(__native_client__) | |
62 // On SFI mode, the FDs of IPC channels are NACL_CHROME_DESC_BASE and its | |
Mark Seaborn
2014/02/19 17:00:30
"On" -> "In"
hidehiko
2014/02/20 18:50:01
Done.
| |
63 // successor, which is set in nacl_listener.cc. | |
64 int g_nacl_ipc_browser_fd = NACL_CHROME_DESC_BASE; | |
65 int g_nacl_ipc_renderer_fd = NACL_CHROME_DESC_BASE + 1; | |
66 #else | |
67 // On non-SFI mode, the FDs of IPC channels are different from the hard coded | |
Mark Seaborn
2014/02/19 17:00:30
"On" -> "In"
hidehiko
2014/02/20 18:50:01
Done.
| |
68 // ones. These values will be overwritten by SetIPCFileDescriptors() below. | |
69 // The first FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the | |
Mark Seaborn
2014/02/19 17:00:30
This reference to NACL_CHROME_DESC_BASE is incorre
hidehiko
2014/02/20 18:50:01
oops, fixed.
| |
70 // browser, and the second one is to the renderer. | |
71 int g_nacl_ipc_browser_fd = -1; | |
72 int g_nacl_ipc_renderer_fd = -1; | |
73 #endif | |
74 | |
56 // This class manages communication between the plugin and the browser, and | 75 // This class manages communication between the plugin and the browser, and |
57 // manages the PluginDispatcher instances for communication between the plugin | 76 // manages the PluginDispatcher instances for communication between the plugin |
58 // and the renderer. | 77 // and the renderer. |
59 class PpapiDispatcher : public ProxyChannel, | 78 class PpapiDispatcher : public ProxyChannel, |
60 public PluginDispatcher::PluginDelegate, | 79 public PluginDispatcher::PluginDelegate, |
61 public PluginProxyDelegate { | 80 public PluginProxyDelegate { |
62 public: | 81 public: |
63 explicit PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop); | 82 explicit PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop); |
64 | 83 |
65 // PluginDispatcher::PluginDelegate implementation. | 84 // PluginDispatcher::PluginDelegate implementation. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 std::map<uint32, PluginDispatcher*> plugin_dispatchers_; | 116 std::map<uint32, PluginDispatcher*> plugin_dispatchers_; |
98 uint32 next_plugin_dispatcher_id_; | 117 uint32 next_plugin_dispatcher_id_; |
99 scoped_refptr<base::MessageLoopProxy> message_loop_; | 118 scoped_refptr<base::MessageLoopProxy> message_loop_; |
100 base::WaitableEvent shutdown_event_; | 119 base::WaitableEvent shutdown_event_; |
101 }; | 120 }; |
102 | 121 |
103 PpapiDispatcher::PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop) | 122 PpapiDispatcher::PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop) |
104 : next_plugin_dispatcher_id_(0), | 123 : next_plugin_dispatcher_id_(0), |
105 message_loop_(io_loop), | 124 message_loop_(io_loop), |
106 shutdown_event_(true, false) { | 125 shutdown_event_(true, false) { |
107 // The first FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the | 126 // The first FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the |
Mark Seaborn
2014/02/19 17:00:30
Please remove this comment now since the reference
hidehiko
2014/02/20 18:50:01
Done.
| |
108 // browser. | 127 // browser. |
128 DCHECK_NE(g_nacl_ipc_browser_fd, -1) | |
129 << "g_nacl_ipc_browser_fd must be initialized before the plugin starts"; | |
109 IPC::ChannelHandle channel_handle( | 130 IPC::ChannelHandle channel_handle( |
110 "NaCl IPC", base::FileDescriptor(NACL_CHROME_DESC_BASE, false)); | 131 "NaCl IPC", base::FileDescriptor(g_nacl_ipc_browser_fd, false)); |
111 // We don't have/need a PID since handle sharing happens outside of the | 132 // We don't have/need a PID since handle sharing happens outside of the |
112 // NaCl sandbox. | 133 // NaCl sandbox. |
113 InitWithChannel(this, base::kNullProcessId, channel_handle, | 134 InitWithChannel(this, base::kNullProcessId, channel_handle, |
114 false); // Channel is server. | 135 false); // Channel is server. |
115 channel()->AddFilter(new ppapi::proxy::PluginMessageFilter( | 136 channel()->AddFilter(new ppapi::proxy::PluginMessageFilter( |
116 NULL, PluginGlobals::Get()->resource_reply_thread_registrar())); | 137 NULL, PluginGlobals::Get()->resource_reply_thread_registrar())); |
117 channel()->AddFilter( | 138 channel()->AddFilter( |
118 new tracing::ChildTraceMessageFilter(message_loop_.get())); | 139 new tracing::ChildTraceMessageFilter(message_loop_.get())); |
119 } | 140 } |
120 | 141 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
222 int32_t error = ::PPP_InitializeModule( | 243 int32_t error = ::PPP_InitializeModule( |
223 0 /* module */, | 244 0 /* module */, |
224 &ppapi::proxy::PluginDispatcher::GetBrowserInterface); | 245 &ppapi::proxy::PluginDispatcher::GetBrowserInterface); |
225 if (error) | 246 if (error) |
226 ::exit(error); | 247 ::exit(error); |
227 | 248 |
228 PluginDispatcher* dispatcher = | 249 PluginDispatcher* dispatcher = |
229 new PluginDispatcher(::PPP_GetInterface, args.permissions, | 250 new PluginDispatcher(::PPP_GetInterface, args.permissions, |
230 args.off_the_record); | 251 args.off_the_record); |
231 // The channel handle's true name is not revealed here. | 252 // The channel handle's true name is not revealed here. |
232 // The second FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the | 253 // The second FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the |
Mark Seaborn
2014/02/19 17:00:30
Same for this sentence
hidehiko
2014/02/20 18:50:01
Done.
| |
233 // renderer. | 254 // renderer. |
255 DCHECK_NE(g_nacl_ipc_renderer_fd, -1) | |
256 << "g_nacl_ipc_renderer_fd must be initialized before the plugin starts"; | |
234 IPC::ChannelHandle channel_handle( | 257 IPC::ChannelHandle channel_handle( |
235 "nacl", base::FileDescriptor(NACL_CHROME_DESC_BASE + 1, false)); | 258 "nacl", base::FileDescriptor(g_nacl_ipc_renderer_fd, false)); |
236 if (!dispatcher->InitPluginWithChannel(this, base::kNullProcessId, | 259 if (!dispatcher->InitPluginWithChannel(this, base::kNullProcessId, |
237 channel_handle, false)) { | 260 channel_handle, false)) { |
238 delete dispatcher; | 261 delete dispatcher; |
239 return; | 262 return; |
240 } | 263 } |
241 // From here, the dispatcher will manage its own lifetime according to the | |
Mark Seaborn
2014/02/19 17:00:30
Why remove this?
hidehiko
2014/02/20 18:50:01
Oops.. I somehow accidentally removed this. Revert
| |
242 // lifetime of the attached channel. | |
243 } | 264 } |
244 | 265 |
245 void PpapiDispatcher::OnPluginDispatcherMessageReceived( | 266 void PpapiDispatcher::OnPluginDispatcherMessageReceived( |
246 const IPC::Message& msg) { | 267 const IPC::Message& msg) { |
247 // The first parameter should be a plugin dispatcher ID. | 268 // The first parameter should be a plugin dispatcher ID. |
248 PickleIterator iter(msg); | 269 PickleIterator iter(msg); |
249 uint32 id = 0; | 270 uint32 id = 0; |
250 if (!msg.ReadUInt32(&iter, &id)) { | 271 if (!msg.ReadUInt32(&iter, &id)) { |
251 NOTREACHED(); | 272 NOTREACHED(); |
252 return; | 273 return; |
(...skipping 11 matching lines...) Expand all Loading... | |
264 switches::kPpapiKeepAliveThrottle), | 285 switches::kPpapiKeepAliveThrottle), |
265 &keepalive_throttle_interval_milliseconds)) { | 286 &keepalive_throttle_interval_milliseconds)) { |
266 ppapi::proxy::PluginGlobals::Get()-> | 287 ppapi::proxy::PluginGlobals::Get()-> |
267 set_keepalive_throttle_interval_milliseconds( | 288 set_keepalive_throttle_interval_milliseconds( |
268 keepalive_throttle_interval_milliseconds); | 289 keepalive_throttle_interval_milliseconds); |
269 } | 290 } |
270 } | 291 } |
271 | 292 |
272 } // namespace | 293 } // namespace |
273 | 294 |
295 void SetIPCFileDescriptors(int ipc_browser_fd, int ipc_renderer_fd) { | |
296 // For non-SFI mode, the browser_ipc_fd and renderer_ipc_fd are different | |
297 // from the hard-coded numbers. So, this overwrites the values directly. | |
Mark Seaborn
2014/02/19 17:00:30
With the "#if defined(__native_client__)" above, t
hidehiko
2014/02/20 18:50:01
Done.
| |
298 g_nacl_ipc_browser_fd = ipc_browser_fd; | |
299 g_nacl_ipc_renderer_fd = ipc_renderer_fd; | |
300 } | |
301 | |
274 void PpapiPluginRegisterThreadCreator( | 302 void PpapiPluginRegisterThreadCreator( |
275 const struct PP_ThreadFunctions* thread_functions) { | 303 const struct PP_ThreadFunctions* thread_functions) { |
304 #if defined(__native_client__) | |
305 // TODO(hidehiko): The thread creation for the PPB_Audio is not yet | |
306 // implemented on non-SFI mode. Support this. Now, this function invocation | |
307 // is just ignored. | |
308 | |
276 // Initialize all classes that need to create threads that call back into | 309 // Initialize all classes that need to create threads that call back into |
277 // user code. | 310 // user code. |
278 ppapi::PPB_Audio_Shared::SetThreadFunctions(thread_functions); | 311 ppapi::PPB_Audio_Shared::SetThreadFunctions(thread_functions); |
312 #endif | |
279 } | 313 } |
280 | 314 |
281 int PpapiPluginMain() { | 315 int PpapiPluginMain() { |
282 // Though it isn't referenced here, we must instantiate an AtExitManager. | 316 // Though it isn't referenced here, we must instantiate an AtExitManager. |
283 base::AtExitManager exit_manager; | 317 base::AtExitManager exit_manager; |
284 base::MessageLoop loop; | 318 base::MessageLoop loop; |
319 #if defined(IPC_MESSAGE_LOG_ENABLED) | |
285 IPC::Logging::set_log_function_map(&g_log_function_mapping); | 320 IPC::Logging::set_log_function_map(&g_log_function_mapping); |
321 #endif | |
286 ppapi::proxy::PluginGlobals plugin_globals; | 322 ppapi::proxy::PluginGlobals plugin_globals; |
287 base::Thread io_thread("Chrome_NaClIOThread"); | 323 base::Thread io_thread("Chrome_NaClIOThread"); |
288 base::Thread::Options options; | 324 base::Thread::Options options; |
289 options.message_loop_type = base::MessageLoop::TYPE_IO; | 325 options.message_loop_type = base::MessageLoop::TYPE_IO; |
290 io_thread.StartWithOptions(options); | 326 io_thread.StartWithOptions(options); |
291 | 327 |
328 #if defined(__native_client__) | |
329 // Currently on non-SFI mode, we don't use SRPC server on plugin. | |
330 // TODO(hidehiko): Make sure this SRPC is actually used on SFI-mode. | |
331 | |
292 // Start up the SRPC server on another thread. Otherwise, when it blocks | 332 // Start up the SRPC server on another thread. Otherwise, when it blocks |
293 // on an RPC, the PPAPI proxy will hang. Do this before we initialize the | 333 // on an RPC, the PPAPI proxy will hang. Do this before we initialize the |
294 // module and start the PPAPI proxy so that the NaCl plugin can continue | 334 // module and start the PPAPI proxy so that the NaCl plugin can continue |
295 // loading the app. | 335 // loading the app. |
296 static struct NaClSrpcHandlerDesc srpc_methods[] = { { NULL, NULL } }; | 336 static struct NaClSrpcHandlerDesc srpc_methods[] = { { NULL, NULL } }; |
297 if (!NaClSrpcAcceptClientOnThread(srpc_methods)) { | 337 if (!NaClSrpcAcceptClientOnThread(srpc_methods)) { |
298 return 1; | 338 return 1; |
299 } | 339 } |
340 #endif | |
300 | 341 |
301 PpapiDispatcher ppapi_dispatcher(io_thread.message_loop_proxy()); | 342 PpapiDispatcher ppapi_dispatcher(io_thread.message_loop_proxy()); |
302 plugin_globals.set_plugin_proxy_delegate(&ppapi_dispatcher); | 343 plugin_globals.set_plugin_proxy_delegate(&ppapi_dispatcher); |
303 | 344 |
304 loop.Run(); | 345 loop.Run(); |
305 | 346 |
306 return 0; | 347 return 0; |
307 } | 348 } |
OLD | NEW |