| 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 <map> | 5 #include <map> |
| 6 #include <set> | 6 #include <set> |
| 7 | 7 |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 // Need to include this before most other files because it defines | 9 // Need to include this before most other files because it defines |
| 10 // IPC_MESSAGE_LOG_ENABLED. We need to use it to define | 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 | 11 // IPC_MESSAGE_MACROS_LOG_ENABLED so ppapi_messages.h will generate the |
| 12 // ViewMsgLog et al. functions. | 12 // ViewMsgLog et al. functions. |
| 13 | 13 |
| 14 #ifndef __native_client__ |
| 15 # if OS_LINUX |
| 16 # define NACL_LINUX 1 |
| 17 # else |
| 18 # error "non-SFI mode is currently supported only on Linux." |
| 19 # endif |
| 20 #endif |
| 21 |
| 22 #include "base/at_exit.h" |
| 14 #include "base/command_line.h" | 23 #include "base/command_line.h" |
| 24 #include "base/lazy_instance.h" |
| 15 #include "base/message_loop/message_loop.h" | 25 #include "base/message_loop/message_loop.h" |
| 16 #include "base/strings/string_number_conversions.h" | 26 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/synchronization/waitable_event.h" | 27 #include "base/synchronization/waitable_event.h" |
| 18 #include "base/threading/thread.h" | 28 #include "base/threading/thread.h" |
| 19 #include "components/tracing/child_trace_message_filter.h" | 29 #include "components/tracing/child_trace_message_filter.h" |
| 20 #include "ipc/ipc_channel_handle.h" | 30 #include "ipc/ipc_channel_handle.h" |
| 21 #include "ipc/ipc_logging.h" | 31 #include "ipc/ipc_logging.h" |
| 22 #include "ipc/ipc_message.h" | 32 #include "ipc/ipc_message.h" |
| 23 #include "native_client/src/public/chrome_main.h" | 33 #include "native_client/src/public/chrome_main.h" |
| 24 #include "native_client/src/shared/srpc/nacl_srpc.h" | |
| 25 #include "ppapi/c/ppp.h" | 34 #include "ppapi/c/ppp.h" |
| 26 #include "ppapi/c/ppp_instance.h" | 35 #include "ppapi/c/ppp_instance.h" |
| 27 #include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h" | 36 #include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h" |
| 28 #include "ppapi/proxy/plugin_dispatcher.h" | 37 #include "ppapi/proxy/plugin_dispatcher.h" |
| 29 #include "ppapi/proxy/plugin_globals.h" | 38 #include "ppapi/proxy/plugin_globals.h" |
| 30 #include "ppapi/proxy/plugin_message_filter.h" | 39 #include "ppapi/proxy/plugin_message_filter.h" |
| 31 #include "ppapi/proxy/plugin_proxy_delegate.h" | 40 #include "ppapi/proxy/plugin_proxy_delegate.h" |
| 32 #include "ppapi/proxy/resource_reply_thread_registrar.h" | 41 #include "ppapi/proxy/resource_reply_thread_registrar.h" |
| 33 #include "ppapi/shared_impl/ppapi_switches.h" | 42 #include "ppapi/shared_impl/ppapi_switches.h" |
| 34 #include "ppapi/shared_impl/ppb_audio_shared.h" | 43 #include "ppapi/shared_impl/ppb_audio_shared.h" |
| 35 | 44 |
| 45 #if defined(__native_client__) |
| 46 #include "native_client/src/shared/srpc/nacl_srpc.h" |
| 47 #endif |
| 48 |
| 36 #if defined(IPC_MESSAGE_LOG_ENABLED) | 49 #if defined(IPC_MESSAGE_LOG_ENABLED) |
| 37 #include "base/containers/hash_tables.h" | 50 #include "base/containers/hash_tables.h" |
| 38 | 51 |
| 39 LogFunctionMap g_log_function_mapping; | 52 LogFunctionMap g_log_function_mapping; |
| 40 | 53 |
| 41 #define IPC_MESSAGE_MACROS_LOG_ENABLED | 54 #define IPC_MESSAGE_MACROS_LOG_ENABLED |
| 42 #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \ | 55 #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \ |
| 43 g_log_function_mapping[msg_id] = logger | 56 g_log_function_mapping[msg_id] = logger |
| 44 | 57 |
| 45 #endif | 58 #endif |
| 46 #include "ppapi/proxy/ppapi_messages.h" | 59 #include "ppapi/proxy/ppapi_messages.h" |
| 47 | 60 |
| 48 using ppapi::proxy::PluginDispatcher; | 61 using ppapi::proxy::PluginDispatcher; |
| 49 using ppapi::proxy::PluginGlobals; | 62 using ppapi::proxy::PluginGlobals; |
| 50 using ppapi::proxy::PluginProxyDelegate; | 63 using ppapi::proxy::PluginProxyDelegate; |
| 51 using ppapi::proxy::ProxyChannel; | 64 using ppapi::proxy::ProxyChannel; |
| 52 using ppapi::proxy::SerializedHandle; | 65 using ppapi::proxy::SerializedHandle; |
| 53 | 66 |
| 67 namespace ppapi { |
| 68 namespace proxy { |
| 69 namespace { |
| 70 |
| 71 // On non-SFI mode, the FDs of IPC channels are different from the hard coded |
| 72 // ones. These values will be overwritten by SetIPCFileDescriptors() below. |
| 73 // The first FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the |
| 74 // browser, and the second one is to the renderer. |
| 75 int g_nacl_ipc_browser_fd = NACL_CHROME_DESC_BASE; |
| 76 int g_nacl_ipc_renderer_fd = NACL_CHROME_DESC_BASE + 1; |
| 77 |
| 78 // We cannot use the default traits for LazyInstance for the |
| 79 // base::WaitableEvent, because it doesn't have the default constructor. |
| 80 struct WaitableEventLazyInstanceTraits |
| 81 : public base::DefaultLazyInstanceTraits<base::WaitableEvent> { |
| 82 static base::WaitableEvent* New(void* instance) { |
| 83 return new (instance) base::WaitableEvent(true, false); |
| 84 } |
| 85 }; |
| 86 base::LazyInstance<base::WaitableEvent, WaitableEventLazyInstanceTraits> |
| 87 g_ppapi_start_event = LAZY_INSTANCE_INITIALIZER; |
| 88 |
| 89 } // namespace |
| 90 |
| 91 void SetIPCFileDescriptors(int ipc_browser_fd, int ipc_renderer_fd) { |
| 92 // For non-SFI mode, the browser_ipc_fd and renderer_ipc_fd are different |
| 93 // from the hard-coded numbers. So, this overwrites the values directly. |
| 94 g_nacl_ipc_browser_fd = ipc_browser_fd; |
| 95 g_nacl_ipc_renderer_fd = ipc_renderer_fd; |
| 96 } |
| 97 |
| 98 void WaitForPpapiStartEvent() { |
| 99 g_ppapi_start_event.Pointer()->Wait(); |
| 100 } |
| 101 |
| 102 } // namespace proxy |
| 103 } // namespace ppapi |
| 104 |
| 54 namespace { | 105 namespace { |
| 55 | 106 |
| 56 // This class manages communication between the plugin and the browser, and | 107 // This class manages communication between the plugin and the browser, and |
| 57 // manages the PluginDispatcher instances for communication between the plugin | 108 // manages the PluginDispatcher instances for communication between the plugin |
| 58 // and the renderer. | 109 // and the renderer. |
| 59 class PpapiDispatcher : public ProxyChannel, | 110 class PpapiDispatcher : public ProxyChannel, |
| 60 public PluginDispatcher::PluginDelegate, | 111 public PluginDispatcher::PluginDelegate, |
| 61 public PluginProxyDelegate { | 112 public PluginProxyDelegate { |
| 62 public: | 113 public: |
| 63 explicit PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop); | 114 explicit PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 base::WaitableEvent shutdown_event_; | 151 base::WaitableEvent shutdown_event_; |
| 101 }; | 152 }; |
| 102 | 153 |
| 103 PpapiDispatcher::PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop) | 154 PpapiDispatcher::PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop) |
| 104 : next_plugin_dispatcher_id_(0), | 155 : next_plugin_dispatcher_id_(0), |
| 105 message_loop_(io_loop), | 156 message_loop_(io_loop), |
| 106 shutdown_event_(true, false) { | 157 shutdown_event_(true, false) { |
| 107 // The first FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the | 158 // The first FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the |
| 108 // browser. | 159 // browser. |
| 109 IPC::ChannelHandle channel_handle( | 160 IPC::ChannelHandle channel_handle( |
| 110 "NaCl IPC", base::FileDescriptor(NACL_CHROME_DESC_BASE, false)); | 161 "NaCl IPC", |
| 162 base::FileDescriptor(ppapi::proxy::g_nacl_ipc_browser_fd, false)); |
| 111 // We don't have/need a PID since handle sharing happens outside of the | 163 // We don't have/need a PID since handle sharing happens outside of the |
| 112 // NaCl sandbox. | 164 // NaCl sandbox. |
| 113 InitWithChannel(this, base::kNullProcessId, channel_handle, | 165 InitWithChannel(this, base::kNullProcessId, channel_handle, |
| 114 false); // Channel is server. | 166 false); // Channel is server. |
| 115 channel()->AddFilter(new ppapi::proxy::PluginMessageFilter( | 167 channel()->AddFilter(new ppapi::proxy::PluginMessageFilter( |
| 116 NULL, PluginGlobals::Get()->resource_reply_thread_registrar())); | 168 NULL, PluginGlobals::Get()->resource_reply_thread_registrar())); |
| 117 channel()->AddFilter( | 169 channel()->AddFilter( |
| 118 new tracing::ChildTraceMessageFilter(message_loop_.get())); | 170 new tracing::ChildTraceMessageFilter(message_loop_.get())); |
| 119 } | 171 } |
| 120 | 172 |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 if (error) | 277 if (error) |
| 226 ::exit(error); | 278 ::exit(error); |
| 227 | 279 |
| 228 PluginDispatcher* dispatcher = | 280 PluginDispatcher* dispatcher = |
| 229 new PluginDispatcher(::PPP_GetInterface, args.permissions, | 281 new PluginDispatcher(::PPP_GetInterface, args.permissions, |
| 230 args.off_the_record); | 282 args.off_the_record); |
| 231 // The channel handle's true name is not revealed here. | 283 // 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 | 284 // The second FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the |
| 233 // renderer. | 285 // renderer. |
| 234 IPC::ChannelHandle channel_handle( | 286 IPC::ChannelHandle channel_handle( |
| 235 "nacl", base::FileDescriptor(NACL_CHROME_DESC_BASE + 1, false)); | 287 "nacl", |
| 288 base::FileDescriptor(ppapi::proxy::g_nacl_ipc_renderer_fd, false)); |
| 236 if (!dispatcher->InitPluginWithChannel(this, base::kNullProcessId, | 289 if (!dispatcher->InitPluginWithChannel(this, base::kNullProcessId, |
| 237 channel_handle, false)) { | 290 channel_handle, false)) { |
| 238 delete dispatcher; | 291 delete dispatcher; |
| 239 return; | 292 return; |
| 240 } | 293 } |
| 241 // From here, the dispatcher will manage its own lifetime according to the | 294 // From here, the dispatcher will manage its own lifetime according to the |
| 242 // lifetime of the attached channel. | 295 // lifetime of the attached channel. |
| 296 ppapi::proxy::g_ppapi_start_event.Pointer()->Signal(); |
| 243 } | 297 } |
| 244 | 298 |
| 245 void PpapiDispatcher::OnPluginDispatcherMessageReceived( | 299 void PpapiDispatcher::OnPluginDispatcherMessageReceived( |
| 246 const IPC::Message& msg) { | 300 const IPC::Message& msg) { |
| 247 // The first parameter should be a plugin dispatcher ID. | 301 // The first parameter should be a plugin dispatcher ID. |
| 248 PickleIterator iter(msg); | 302 PickleIterator iter(msg); |
| 249 uint32 id = 0; | 303 uint32 id = 0; |
| 250 if (!msg.ReadUInt32(&iter, &id)) { | 304 if (!msg.ReadUInt32(&iter, &id)) { |
| 251 NOTREACHED(); | 305 NOTREACHED(); |
| 252 return; | 306 return; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 266 ppapi::proxy::PluginGlobals::Get()-> | 320 ppapi::proxy::PluginGlobals::Get()-> |
| 267 set_keepalive_throttle_interval_milliseconds( | 321 set_keepalive_throttle_interval_milliseconds( |
| 268 keepalive_throttle_interval_milliseconds); | 322 keepalive_throttle_interval_milliseconds); |
| 269 } | 323 } |
| 270 } | 324 } |
| 271 | 325 |
| 272 } // namespace | 326 } // namespace |
| 273 | 327 |
| 274 void PpapiPluginRegisterThreadCreator( | 328 void PpapiPluginRegisterThreadCreator( |
| 275 const struct PP_ThreadFunctions* thread_functions) { | 329 const struct PP_ThreadFunctions* thread_functions) { |
| 330 #if defined(__native_client__) |
| 331 // TODO(hidehiko): The thread creation for the PPB_Audio is not yet |
| 332 // implemented on non-SFI mode. Support this. Now, this function invocation |
| 333 // is just ignored. |
| 334 |
| 276 // Initialize all classes that need to create threads that call back into | 335 // Initialize all classes that need to create threads that call back into |
| 277 // user code. | 336 // user code. |
| 278 ppapi::PPB_Audio_Shared::SetThreadFunctions(thread_functions); | 337 ppapi::PPB_Audio_Shared::SetThreadFunctions(thread_functions); |
| 338 #endif |
| 279 } | 339 } |
| 280 | 340 |
| 281 int PpapiPluginMain() { | 341 int PpapiPluginMain() { |
| 282 // Though it isn't referenced here, we must instantiate an AtExitManager. | 342 // Though it isn't referenced here, we must instantiate an AtExitManager. |
| 283 base::AtExitManager exit_manager; | 343 base::AtExitManager exit_manager; |
| 284 base::MessageLoop loop; | 344 base::MessageLoop loop; |
| 345 #if defined(IPC_MESSAGE_LOG_ENABLED) |
| 285 IPC::Logging::set_log_function_map(&g_log_function_mapping); | 346 IPC::Logging::set_log_function_map(&g_log_function_mapping); |
| 347 #endif |
| 286 ppapi::proxy::PluginGlobals plugin_globals; | 348 ppapi::proxy::PluginGlobals plugin_globals; |
| 287 base::Thread io_thread("Chrome_NaClIOThread"); | 349 base::Thread io_thread("Chrome_NaClIOThread"); |
| 288 base::Thread::Options options; | 350 base::Thread::Options options; |
| 289 options.message_loop_type = base::MessageLoop::TYPE_IO; | 351 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 290 io_thread.StartWithOptions(options); | 352 io_thread.StartWithOptions(options); |
| 291 | 353 |
| 354 #if defined(__native_client__) |
| 355 // Currently on non-SFI mode, we don't use SRPC server on plugin. |
| 356 // TODO(hidehiko): Make sure this SRPC is actually used on SFI-mode. |
| 357 |
| 292 // Start up the SRPC server on another thread. Otherwise, when it blocks | 358 // 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 | 359 // 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 | 360 // module and start the PPAPI proxy so that the NaCl plugin can continue |
| 295 // loading the app. | 361 // loading the app. |
| 296 static struct NaClSrpcHandlerDesc srpc_methods[] = { { NULL, NULL } }; | 362 static struct NaClSrpcHandlerDesc srpc_methods[] = { { NULL, NULL } }; |
| 297 if (!NaClSrpcAcceptClientOnThread(srpc_methods)) { | 363 if (!NaClSrpcAcceptClientOnThread(srpc_methods)) { |
| 298 return 1; | 364 return 1; |
| 299 } | 365 } |
| 366 #endif |
| 300 | 367 |
| 301 PpapiDispatcher ppapi_dispatcher(io_thread.message_loop_proxy()); | 368 PpapiDispatcher ppapi_dispatcher(io_thread.message_loop_proxy()); |
| 302 plugin_globals.set_plugin_proxy_delegate(&ppapi_dispatcher); | 369 plugin_globals.set_plugin_proxy_delegate(&ppapi_dispatcher); |
| 303 | 370 |
| 304 loop.Run(); | 371 loop.Run(); |
| 305 | 372 |
| 306 return 0; | 373 return 0; |
| 307 } | 374 } |
| OLD | NEW |