| 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 <unistd.h> | 7 #include <unistd.h> |
| 6 | |
| 7 #include <map> | 8 #include <map> |
| 8 #include <set> | 9 #include <set> |
| 9 | 10 |
| 10 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 11 // Need to include this before most other files because it defines | 12 // Need to include this before most other files because it defines |
| 12 // IPC_MESSAGE_LOG_ENABLED. We need to use it to define | 13 // IPC_MESSAGE_LOG_ENABLED. We need to use it to define |
| 13 // IPC_MESSAGE_MACROS_LOG_ENABLED so ppapi_messages.h will generate the | 14 // IPC_MESSAGE_MACROS_LOG_ENABLED so ppapi_messages.h will generate the |
| 14 // ViewMsgLog et al. functions. | 15 // ViewMsgLog et al. functions. |
| 15 | 16 |
| 17 #include "base/at_exit.h" |
| 16 #include "base/command_line.h" | 18 #include "base/command_line.h" |
| 17 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
| 19 #include "base/strings/string_number_conversions.h" | 21 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/synchronization/waitable_event.h" | 22 #include "base/synchronization/waitable_event.h" |
| 21 #include "base/threading/thread.h" | 23 #include "base/threading/thread.h" |
| 22 #include "components/tracing/child_trace_message_filter.h" | 24 #include "components/tracing/child_trace_message_filter.h" |
| 23 #include "ipc/ipc_channel_handle.h" | 25 #include "ipc/ipc_channel_handle.h" |
| 24 #include "ipc/ipc_logging.h" | 26 #include "ipc/ipc_logging.h" |
| 25 #include "ipc/ipc_message.h" | 27 #include "ipc/ipc_message.h" |
| 26 #include "native_client/src/public/chrome_main.h" | |
| 27 #include "native_client/src/shared/srpc/nacl_srpc.h" | |
| 28 #include "ppapi/c/ppp.h" | 28 #include "ppapi/c/ppp.h" |
| 29 #include "ppapi/c/ppp_instance.h" | 29 #include "ppapi/c/ppp_instance.h" |
| 30 #include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h" | |
| 31 #include "ppapi/proxy/plugin_dispatcher.h" | 30 #include "ppapi/proxy/plugin_dispatcher.h" |
| 32 #include "ppapi/proxy/plugin_globals.h" | 31 #include "ppapi/proxy/plugin_globals.h" |
| 33 #include "ppapi/proxy/plugin_message_filter.h" | 32 #include "ppapi/proxy/plugin_message_filter.h" |
| 34 #include "ppapi/proxy/plugin_proxy_delegate.h" | 33 #include "ppapi/proxy/plugin_proxy_delegate.h" |
| 35 #include "ppapi/proxy/resource_reply_thread_registrar.h" | 34 #include "ppapi/proxy/resource_reply_thread_registrar.h" |
| 36 #include "ppapi/shared_impl/ppapi_switches.h" | 35 #include "ppapi/shared_impl/ppapi_switches.h" |
| 37 #include "ppapi/shared_impl/ppb_audio_shared.h" | 36 #include "ppapi/shared_impl/ppb_audio_shared.h" |
| 38 | 37 |
| 38 #if defined(__native_client__) |
| 39 #include "native_client/src/public/chrome_main.h" |
| 40 #include "native_client/src/shared/srpc/nacl_srpc.h" |
| 41 #endif |
| 42 |
| 39 #if defined(IPC_MESSAGE_LOG_ENABLED) | 43 #if defined(IPC_MESSAGE_LOG_ENABLED) |
| 40 #include "base/containers/hash_tables.h" | 44 #include "base/containers/hash_tables.h" |
| 41 | 45 |
| 42 LogFunctionMap g_log_function_mapping; | 46 LogFunctionMap g_log_function_mapping; |
| 43 | 47 |
| 44 #define IPC_MESSAGE_MACROS_LOG_ENABLED | 48 #define IPC_MESSAGE_MACROS_LOG_ENABLED |
| 45 #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \ | 49 #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \ |
| 46 g_log_function_mapping[msg_id] = logger | 50 g_log_function_mapping[msg_id] = logger |
| 47 | 51 |
| 48 #endif | 52 #endif |
| 49 #include "ppapi/proxy/ppapi_messages.h" | 53 #include "ppapi/proxy/ppapi_messages.h" |
| 50 | 54 |
| 51 using ppapi::proxy::PluginDispatcher; | 55 using ppapi::proxy::PluginDispatcher; |
| 52 using ppapi::proxy::PluginGlobals; | 56 using ppapi::proxy::PluginGlobals; |
| 53 using ppapi::proxy::PluginProxyDelegate; | 57 using ppapi::proxy::PluginProxyDelegate; |
| 54 using ppapi::proxy::ProxyChannel; | 58 using ppapi::proxy::ProxyChannel; |
| 55 using ppapi::proxy::SerializedHandle; | 59 using ppapi::proxy::SerializedHandle; |
| 56 | 60 |
| 57 namespace { | 61 namespace { |
| 58 | 62 |
| 63 #if defined(__native_client__) |
| 64 // In SFI mode, the FDs of IPC channels are NACL_CHROME_DESC_BASE and its |
| 65 // successor, which is set in nacl_listener.cc. |
| 66 int g_nacl_ipc_browser_fd = NACL_CHROME_DESC_BASE; |
| 67 int g_nacl_ipc_renderer_fd = NACL_CHROME_DESC_BASE + 1; |
| 68 #else |
| 69 // In non-SFI mode, the FDs of IPC channels are different from the hard coded |
| 70 // ones. These values will be set by SetIPCFileDescriptors() below. |
| 71 // At first, both are initialized to invalid FD number (-1). |
| 72 int g_nacl_ipc_browser_fd = -1; |
| 73 int g_nacl_ipc_renderer_fd = -1; |
| 74 #endif |
| 75 |
| 59 // This class manages communication between the plugin and the browser, and | 76 // This class manages communication between the plugin and the browser, and |
| 60 // manages the PluginDispatcher instances for communication between the plugin | 77 // manages the PluginDispatcher instances for communication between the plugin |
| 61 // and the renderer. | 78 // and the renderer. |
| 62 class PpapiDispatcher : public PluginDispatcher::PluginDelegate, | 79 class PpapiDispatcher : public PluginDispatcher::PluginDelegate, |
| 63 public PluginProxyDelegate, | 80 public PluginProxyDelegate, |
| 64 public IPC::Listener, | 81 public IPC::Listener, |
| 65 public IPC::Sender { | 82 public IPC::Sender { |
| 66 public: | 83 public: |
| 67 explicit PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop); | 84 explicit PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop); |
| 68 | 85 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 uint32 next_plugin_dispatcher_id_; | 123 uint32 next_plugin_dispatcher_id_; |
| 107 scoped_refptr<base::MessageLoopProxy> message_loop_; | 124 scoped_refptr<base::MessageLoopProxy> message_loop_; |
| 108 base::WaitableEvent shutdown_event_; | 125 base::WaitableEvent shutdown_event_; |
| 109 scoped_ptr<IPC::SyncChannel> channel_; | 126 scoped_ptr<IPC::SyncChannel> channel_; |
| 110 }; | 127 }; |
| 111 | 128 |
| 112 PpapiDispatcher::PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop) | 129 PpapiDispatcher::PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop) |
| 113 : next_plugin_dispatcher_id_(0), | 130 : next_plugin_dispatcher_id_(0), |
| 114 message_loop_(io_loop), | 131 message_loop_(io_loop), |
| 115 shutdown_event_(true, false) { | 132 shutdown_event_(true, false) { |
| 116 // The first FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the | 133 DCHECK_NE(g_nacl_ipc_browser_fd, -1) |
| 117 // browser. | 134 << "g_nacl_ipc_browser_fd must be initialized before the plugin starts"; |
| 118 IPC::ChannelHandle channel_handle( | 135 IPC::ChannelHandle channel_handle( |
| 119 "NaCl IPC", base::FileDescriptor(NACL_CHROME_DESC_BASE, false)); | 136 "NaCl IPC", base::FileDescriptor(g_nacl_ipc_browser_fd, false)); |
| 120 | 137 |
| 121 // Delay initializing the SyncChannel until after we add filters. This | 138 // Delay initializing the SyncChannel until after we add filters. This |
| 122 // ensures that the filters won't miss any messages received by | 139 // ensures that the filters won't miss any messages received by |
| 123 // the channel. | 140 // the channel. |
| 124 channel_.reset(new IPC::SyncChannel( | 141 channel_.reset(new IPC::SyncChannel( |
| 125 this, GetIPCMessageLoop(), GetShutdownEvent())); | 142 this, GetIPCMessageLoop(), GetShutdownEvent())); |
| 126 channel_->AddFilter(new ppapi::proxy::PluginMessageFilter( | 143 channel_->AddFilter(new ppapi::proxy::PluginMessageFilter( |
| 127 NULL, PluginGlobals::Get()->resource_reply_thread_registrar())); | 144 NULL, PluginGlobals::Get()->resource_reply_thread_registrar())); |
| 128 channel_->AddFilter( | 145 channel_->AddFilter( |
| 129 new tracing::ChildTraceMessageFilter(message_loop_.get())); | 146 new tracing::ChildTraceMessageFilter(message_loop_.get())); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 int32_t error = ::PPP_InitializeModule( | 259 int32_t error = ::PPP_InitializeModule( |
| 243 0 /* module */, | 260 0 /* module */, |
| 244 &ppapi::proxy::PluginDispatcher::GetBrowserInterface); | 261 &ppapi::proxy::PluginDispatcher::GetBrowserInterface); |
| 245 if (error) | 262 if (error) |
| 246 ::exit(error); | 263 ::exit(error); |
| 247 | 264 |
| 248 PluginDispatcher* dispatcher = | 265 PluginDispatcher* dispatcher = |
| 249 new PluginDispatcher(::PPP_GetInterface, args.permissions, | 266 new PluginDispatcher(::PPP_GetInterface, args.permissions, |
| 250 args.off_the_record); | 267 args.off_the_record); |
| 251 // The channel handle's true name is not revealed here. | 268 // The channel handle's true name is not revealed here. |
| 252 // The second FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the | 269 DCHECK_NE(g_nacl_ipc_renderer_fd, -1) |
| 253 // renderer. | 270 << "g_nacl_ipc_renderer_fd must be initialized before the plugin starts"; |
| 254 IPC::ChannelHandle channel_handle( | 271 IPC::ChannelHandle channel_handle( |
| 255 "nacl", base::FileDescriptor(NACL_CHROME_DESC_BASE + 1, false)); | 272 "nacl", base::FileDescriptor(g_nacl_ipc_renderer_fd, false)); |
| 256 if (!dispatcher->InitPluginWithChannel(this, base::kNullProcessId, | 273 if (!dispatcher->InitPluginWithChannel(this, base::kNullProcessId, |
| 257 channel_handle, false)) { | 274 channel_handle, false)) { |
| 258 delete dispatcher; | 275 delete dispatcher; |
| 259 return; | 276 return; |
| 260 } | 277 } |
| 261 // From here, the dispatcher will manage its own lifetime according to the | 278 // From here, the dispatcher will manage its own lifetime according to the |
| 262 // lifetime of the attached channel. | 279 // lifetime of the attached channel. |
| 263 } | 280 } |
| 264 | 281 |
| 265 void PpapiDispatcher::OnPluginDispatcherMessageReceived( | 282 void PpapiDispatcher::OnPluginDispatcherMessageReceived( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 284 switches::kPpapiKeepAliveThrottle), | 301 switches::kPpapiKeepAliveThrottle), |
| 285 &keepalive_throttle_interval_milliseconds)) { | 302 &keepalive_throttle_interval_milliseconds)) { |
| 286 ppapi::proxy::PluginGlobals::Get()-> | 303 ppapi::proxy::PluginGlobals::Get()-> |
| 287 set_keepalive_throttle_interval_milliseconds( | 304 set_keepalive_throttle_interval_milliseconds( |
| 288 keepalive_throttle_interval_milliseconds); | 305 keepalive_throttle_interval_milliseconds); |
| 289 } | 306 } |
| 290 } | 307 } |
| 291 | 308 |
| 292 } // namespace | 309 } // namespace |
| 293 | 310 |
| 311 void SetIPCFileDescriptors(int ipc_browser_fd, int ipc_renderer_fd) { |
| 312 g_nacl_ipc_browser_fd = ipc_browser_fd; |
| 313 g_nacl_ipc_renderer_fd = ipc_renderer_fd; |
| 314 } |
| 315 |
| 294 void PpapiPluginRegisterThreadCreator( | 316 void PpapiPluginRegisterThreadCreator( |
| 295 const struct PP_ThreadFunctions* thread_functions) { | 317 const struct PP_ThreadFunctions* thread_functions) { |
| 318 #if defined(__native_client__) |
| 319 // TODO(hidehiko): The thread creation for the PPB_Audio is not yet |
| 320 // implemented on non-SFI mode. Support this. Now, this function invocation |
| 321 // is just ignored. |
| 322 |
| 296 // Initialize all classes that need to create threads that call back into | 323 // Initialize all classes that need to create threads that call back into |
| 297 // user code. | 324 // user code. |
| 298 ppapi::PPB_Audio_Shared::SetThreadFunctions(thread_functions); | 325 ppapi::PPB_Audio_Shared::SetThreadFunctions(thread_functions); |
| 326 #endif |
| 299 } | 327 } |
| 300 | 328 |
| 301 int PpapiPluginMain() { | 329 int PpapiPluginMain() { |
| 302 // Though it isn't referenced here, we must instantiate an AtExitManager. | 330 // Though it isn't referenced here, we must instantiate an AtExitManager. |
| 303 base::AtExitManager exit_manager; | 331 base::AtExitManager exit_manager; |
| 304 base::MessageLoop loop; | 332 base::MessageLoop loop; |
| 333 #if defined(IPC_MESSAGE_LOG_ENABLED) |
| 305 IPC::Logging::set_log_function_map(&g_log_function_mapping); | 334 IPC::Logging::set_log_function_map(&g_log_function_mapping); |
| 335 #endif |
| 306 ppapi::proxy::PluginGlobals plugin_globals; | 336 ppapi::proxy::PluginGlobals plugin_globals; |
| 307 base::Thread io_thread("Chrome_NaClIOThread"); | 337 base::Thread io_thread("Chrome_NaClIOThread"); |
| 308 base::Thread::Options options; | 338 base::Thread::Options options; |
| 309 options.message_loop_type = base::MessageLoop::TYPE_IO; | 339 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 310 io_thread.StartWithOptions(options); | 340 io_thread.StartWithOptions(options); |
| 311 | 341 |
| 342 #if defined(__native_client__) |
| 343 // Currently on non-SFI mode, we don't use SRPC server on plugin. |
| 344 // TODO(hidehiko): Make sure this SRPC is actually used on SFI-mode. |
| 345 |
| 312 // Start up the SRPC server on another thread. Otherwise, when it blocks | 346 // Start up the SRPC server on another thread. Otherwise, when it blocks |
| 313 // on an RPC, the PPAPI proxy will hang. Do this before we initialize the | 347 // on an RPC, the PPAPI proxy will hang. Do this before we initialize the |
| 314 // module and start the PPAPI proxy so that the NaCl plugin can continue | 348 // module and start the PPAPI proxy so that the NaCl plugin can continue |
| 315 // loading the app. | 349 // loading the app. |
| 316 static struct NaClSrpcHandlerDesc srpc_methods[] = { { NULL, NULL } }; | 350 static struct NaClSrpcHandlerDesc srpc_methods[] = { { NULL, NULL } }; |
| 317 if (!NaClSrpcAcceptClientOnThread(srpc_methods)) { | 351 if (!NaClSrpcAcceptClientOnThread(srpc_methods)) { |
| 318 return 1; | 352 return 1; |
| 319 } | 353 } |
| 354 #endif |
| 320 | 355 |
| 321 PpapiDispatcher ppapi_dispatcher(io_thread.message_loop_proxy()); | 356 PpapiDispatcher ppapi_dispatcher(io_thread.message_loop_proxy()); |
| 322 plugin_globals.set_plugin_proxy_delegate(&ppapi_dispatcher); | 357 plugin_globals.set_plugin_proxy_delegate(&ppapi_dispatcher); |
| 323 | 358 |
| 324 loop.Run(); | 359 loop.Run(); |
| 325 | 360 |
| 326 return 0; | 361 return 0; |
| 327 } | 362 } |
| OLD | NEW |