| 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 #include "base/at_exit.h" |
| 14 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 15 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
| 18 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
| 19 #include "components/tracing/child_trace_message_filter.h" | |
| 20 #include "ipc/ipc_channel_handle.h" | 20 #include "ipc/ipc_channel_handle.h" |
| 21 #include "ipc/ipc_logging.h" | 21 #include "ipc/ipc_logging.h" |
| 22 #include "ipc/ipc_message.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" | 23 #include "native_client/src/untrusted/irt/irt_ppapi.h" |
| 25 #include "ppapi/c/ppp.h" | 24 #include "ppapi/c/ppp.h" |
| 26 #include "ppapi/c/ppp_instance.h" | 25 #include "ppapi/c/ppp_instance.h" |
| 27 #include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h" | 26 #include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h" |
| 28 #include "ppapi/proxy/plugin_dispatcher.h" | 27 #include "ppapi/proxy/plugin_dispatcher.h" |
| 29 #include "ppapi/proxy/plugin_globals.h" | 28 #include "ppapi/proxy/plugin_globals.h" |
| 30 #include "ppapi/proxy/plugin_message_filter.h" | 29 #include "ppapi/proxy/plugin_message_filter.h" |
| 31 #include "ppapi/proxy/plugin_proxy_delegate.h" | 30 #include "ppapi/proxy/plugin_proxy_delegate.h" |
| 32 #include "ppapi/proxy/resource_reply_thread_registrar.h" | 31 #include "ppapi/proxy/resource_reply_thread_registrar.h" |
| 33 #include "ppapi/shared_impl/ppapi_switches.h" | 32 #include "ppapi/shared_impl/ppapi_switches.h" |
| 34 #include "ppapi/shared_impl/ppb_audio_shared.h" | 33 #include "ppapi/shared_impl/ppb_audio_shared.h" |
| 35 | 34 |
| 35 #if defined(__native_client__) |
| 36 #include "components/tracing/child_trace_message_filter.h" |
| 37 #include "native_client/src/shared/srpc/nacl_srpc.h" |
| 38 #endif |
| 39 |
| 36 #if defined(IPC_MESSAGE_LOG_ENABLED) | 40 #if defined(IPC_MESSAGE_LOG_ENABLED) |
| 37 #include "base/containers/hash_tables.h" | 41 #include "base/containers/hash_tables.h" |
| 38 | 42 |
| 39 LogFunctionMap g_log_function_mapping; | 43 LogFunctionMap g_log_function_mapping; |
| 40 | 44 |
| 41 #define IPC_MESSAGE_MACROS_LOG_ENABLED | 45 #define IPC_MESSAGE_MACROS_LOG_ENABLED |
| 42 #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \ | 46 #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \ |
| 43 g_log_function_mapping[msg_id] = logger | 47 g_log_function_mapping[msg_id] = logger |
| 44 | 48 |
| 45 #endif | 49 #endif |
| 46 #include "ppapi/proxy/ppapi_messages.h" | 50 #include "ppapi/proxy/ppapi_messages.h" |
| 47 | 51 |
| 48 // This must match up with NACL_CHROME_INITIAL_IPC_DESC, | |
| 49 // defined in sel_main_chrome.h | |
| 50 #define NACL_IPC_FD 6 | |
| 51 | |
| 52 using ppapi::proxy::PluginDispatcher; | 52 using ppapi::proxy::PluginDispatcher; |
| 53 using ppapi::proxy::PluginGlobals; | 53 using ppapi::proxy::PluginGlobals; |
| 54 using ppapi::proxy::PluginProxyDelegate; | 54 using ppapi::proxy::PluginProxyDelegate; |
| 55 using ppapi::proxy::ProxyChannel; | 55 using ppapi::proxy::ProxyChannel; |
| 56 using ppapi::proxy::SerializedHandle; | 56 using ppapi::proxy::SerializedHandle; |
| 57 | 57 |
| 58 namespace ppapi { |
| 59 namespace proxy { |
| 60 // This must match up with NACL_CHROME_INITIAL_IPC_DESC, |
| 61 // defined in sel_main_chrome.h |
| 62 int g_nacl_browser_ipc_fd = 6; |
| 63 |
| 64 // This must match up with NACL_CHROME_RENDERER_INITIAL_IPC_DESC, |
| 65 // defined in sel_main_chrome.h |
| 66 int g_nacl_renderer_ipc_fd = 7; |
| 67 |
| 68 base::WaitableEvent* g_ppapi_start_event = NULL; |
| 69 |
| 70 // For non-SFI mode, the browser_ipc_fd and renderer_ipc_fd are different from |
| 71 // the hard-coded numbers. So, this overwrites the values directly. |
| 72 void SetIPCFileDescriptors(int browser_ipc_fd, int renderer_ipc_fd) { |
| 73 g_nacl_browser_ipc_fd = browser_ipc_fd; |
| 74 g_nacl_renderer_ipc_fd = renderer_ipc_fd; |
| 75 } |
| 76 |
| 77 // For non-SFI mode, we need to wait returning LoadModule SRPC until the |
| 78 // MessageLoop on the plugin's main thread starts to run. This is the signal |
| 79 // of the synchronization. Note that this setter must be called before the |
| 80 // main thread starts, otherwise it would have a timing issue. |
| 81 void SetPpapiStartEvent(base::WaitableEvent* ppapi_start_event) { |
| 82 DCHECK(g_ppapi_start_event == NULL); |
| 83 g_ppapi_start_event = ppapi_start_event; |
| 84 } |
| 85 |
| 86 } // namespace proxy |
| 87 } // namespace ppapi |
| 88 |
| 58 namespace { | 89 namespace { |
| 59 | 90 |
| 60 // This class manages communication between the plugin and the browser, and | 91 // This class manages communication between the plugin and the browser, and |
| 61 // manages the PluginDispatcher instances for communication between the plugin | 92 // manages the PluginDispatcher instances for communication between the plugin |
| 62 // and the renderer. | 93 // and the renderer. |
| 63 class PpapiDispatcher : public ProxyChannel, | 94 class PpapiDispatcher : public ProxyChannel, |
| 64 public PluginDispatcher::PluginDelegate, | 95 public PluginDispatcher::PluginDelegate, |
| 65 public PluginProxyDelegate { | 96 public PluginProxyDelegate { |
| 66 public: | 97 public: |
| 67 explicit PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop); | 98 explicit PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 uint32 next_plugin_dispatcher_id_; | 134 uint32 next_plugin_dispatcher_id_; |
| 104 scoped_refptr<base::MessageLoopProxy> message_loop_; | 135 scoped_refptr<base::MessageLoopProxy> message_loop_; |
| 105 base::WaitableEvent shutdown_event_; | 136 base::WaitableEvent shutdown_event_; |
| 106 }; | 137 }; |
| 107 | 138 |
| 108 PpapiDispatcher::PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop) | 139 PpapiDispatcher::PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop) |
| 109 : next_plugin_dispatcher_id_(0), | 140 : next_plugin_dispatcher_id_(0), |
| 110 message_loop_(io_loop), | 141 message_loop_(io_loop), |
| 111 shutdown_event_(true, false) { | 142 shutdown_event_(true, false) { |
| 112 IPC::ChannelHandle channel_handle( | 143 IPC::ChannelHandle channel_handle( |
| 113 "NaCl IPC", base::FileDescriptor(NACL_IPC_FD, false)); | 144 "NaCl IPC", |
| 145 base::FileDescriptor(ppapi::proxy::g_nacl_browser_ipc_fd, false)); |
| 114 // We don't have/need a PID since handle sharing happens outside of the | 146 // We don't have/need a PID since handle sharing happens outside of the |
| 115 // NaCl sandbox. | 147 // NaCl sandbox. |
| 116 InitWithChannel(this, base::kNullProcessId, channel_handle, | 148 InitWithChannel(this, base::kNullProcessId, channel_handle, |
| 117 false); // Channel is server. | 149 false); // Channel is server. |
| 118 channel()->AddFilter(new ppapi::proxy::PluginMessageFilter( | 150 channel()->AddFilter(new ppapi::proxy::PluginMessageFilter( |
| 119 NULL, PluginGlobals::Get()->resource_reply_thread_registrar())); | 151 NULL, PluginGlobals::Get()->resource_reply_thread_registrar())); |
| 152 #if defined(__native_client__) |
| 120 channel()->AddFilter( | 153 channel()->AddFilter( |
| 121 new tracing::ChildTraceMessageFilter(message_loop_.get())); | 154 new tracing::ChildTraceMessageFilter(message_loop_.get())); |
| 155 #endif |
| 122 } | 156 } |
| 123 | 157 |
| 124 base::MessageLoopProxy* PpapiDispatcher::GetIPCMessageLoop() { | 158 base::MessageLoopProxy* PpapiDispatcher::GetIPCMessageLoop() { |
| 125 return message_loop_.get(); | 159 return message_loop_.get(); |
| 126 } | 160 } |
| 127 | 161 |
| 128 base::WaitableEvent* PpapiDispatcher::GetShutdownEvent() { | 162 base::WaitableEvent* PpapiDispatcher::GetShutdownEvent() { |
| 129 return &shutdown_event_; | 163 return &shutdown_event_; |
| 130 } | 164 } |
| 131 | 165 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 IPC_MESSAGE_HANDLER(PpapiMsg_CreateNaClChannel, OnMsgCreateNaClChannel) | 226 IPC_MESSAGE_HANDLER(PpapiMsg_CreateNaClChannel, OnMsgCreateNaClChannel) |
| 193 // All other messages are simply forwarded to a PluginDispatcher. | 227 // All other messages are simply forwarded to a PluginDispatcher. |
| 194 IPC_MESSAGE_UNHANDLED(OnPluginDispatcherMessageReceived(msg)) | 228 IPC_MESSAGE_UNHANDLED(OnPluginDispatcherMessageReceived(msg)) |
| 195 IPC_END_MESSAGE_MAP() | 229 IPC_END_MESSAGE_MAP() |
| 196 return true; | 230 return true; |
| 197 } | 231 } |
| 198 | 232 |
| 199 void PpapiDispatcher::OnMsgCreateNaClChannel( | 233 void PpapiDispatcher::OnMsgCreateNaClChannel( |
| 200 const ppapi::PpapiNaClChannelArgs& args, | 234 const ppapi::PpapiNaClChannelArgs& args, |
| 201 SerializedHandle handle) { | 235 SerializedHandle handle) { |
| 202 static bool command_line_and_logging_initialized = false; | 236 LOG(FATAL) << "OnMsgCreateNaClChannel"; |
| 203 if (!command_line_and_logging_initialized) { | |
| 204 CommandLine::Init(0, NULL); | |
| 205 for (size_t i = 0; i < args.switch_names.size(); ++i) { | |
| 206 DCHECK(i < args.switch_values.size()); | |
| 207 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 208 args.switch_names[i], args.switch_values[i]); | |
| 209 } | |
| 210 logging::LoggingSettings settings; | |
| 211 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | |
| 212 logging::InitLogging(settings); | |
| 213 SetPpapiKeepAliveThrottleFromCommandLine(); | |
| 214 command_line_and_logging_initialized = true; | |
| 215 } | |
| 216 // Tell the process-global GetInterface which interfaces it can return to the | |
| 217 // plugin. | |
| 218 ppapi::proxy::InterfaceList::SetProcessGlobalPermissions( | |
| 219 args.permissions); | |
| 220 | |
| 221 int32_t error = ::PPP_InitializeModule( | |
| 222 0 /* module */, | |
| 223 &ppapi::proxy::PluginDispatcher::GetBrowserInterface); | |
| 224 if (error) | |
| 225 ::exit(error); | |
| 226 | |
| 227 PluginDispatcher* dispatcher = | |
| 228 new PluginDispatcher(::PPP_GetInterface, args.permissions, | |
| 229 args.off_the_record); | |
| 230 // The channel handle's true name is not revealed here. | |
| 231 IPC::ChannelHandle channel_handle("nacl", handle.descriptor()); | |
| 232 if (!dispatcher->InitPluginWithChannel(this, base::kNullProcessId, | |
| 233 channel_handle, false)) { | |
| 234 delete dispatcher; | |
| 235 return; | |
| 236 } | |
| 237 // From here, the dispatcher will manage its own lifetime according to the | |
| 238 // lifetime of the attached channel. | |
| 239 } | 237 } |
| 240 | 238 |
| 241 void PpapiDispatcher::OnPluginDispatcherMessageReceived( | 239 void PpapiDispatcher::OnPluginDispatcherMessageReceived( |
| 242 const IPC::Message& msg) { | 240 const IPC::Message& msg) { |
| 243 // The first parameter should be a plugin dispatcher ID. | 241 // The first parameter should be a plugin dispatcher ID. |
| 244 PickleIterator iter(msg); | 242 PickleIterator iter(msg); |
| 245 uint32 id = 0; | 243 uint32 id = 0; |
| 246 if (!msg.ReadUInt32(&iter, &id)) { | 244 if (!msg.ReadUInt32(&iter, &id)) { |
| 247 NOTREACHED(); | 245 NOTREACHED(); |
| 248 return; | 246 return; |
| 249 } | 247 } |
| 250 std::map<uint32, ppapi::proxy::PluginDispatcher*>::iterator dispatcher = | 248 std::map<uint32, ppapi::proxy::PluginDispatcher*>::iterator dispatcher = |
| 251 plugin_dispatchers_.find(id); | 249 plugin_dispatchers_.find(id); |
| 252 if (dispatcher != plugin_dispatchers_.end()) | 250 if (dispatcher != plugin_dispatchers_.end()) |
| 253 dispatcher->second->OnMessageReceived(msg); | 251 dispatcher->second->OnMessageReceived(msg); |
| 254 } | 252 } |
| 255 | 253 |
| 256 void PpapiDispatcher::SetPpapiKeepAliveThrottleFromCommandLine() { | 254 void PpapiDispatcher::SetPpapiKeepAliveThrottleFromCommandLine() { |
| 257 unsigned keepalive_throttle_interval_milliseconds = 0; | 255 unsigned keepalive_throttle_interval_milliseconds = 0; |
| 258 if (base::StringToUint( | 256 if (base::StringToUint( |
| 259 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 257 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 260 switches::kPpapiKeepAliveThrottle), | 258 switches::kPpapiKeepAliveThrottle), |
| 261 &keepalive_throttle_interval_milliseconds)) { | 259 &keepalive_throttle_interval_milliseconds)) { |
| 262 ppapi::proxy::PluginGlobals::Get()-> | 260 ppapi::proxy::PluginGlobals::Get()-> |
| 263 set_keepalive_throttle_interval_milliseconds( | 261 set_keepalive_throttle_interval_milliseconds( |
| 264 keepalive_throttle_interval_milliseconds); | 262 keepalive_throttle_interval_milliseconds); |
| 265 } | 263 } |
| 266 } | 264 } |
| 267 | 265 |
| 266 void SetPpapiKeepAliveThrottleFromCommandLine() { |
| 267 unsigned keepalive_throttle_interval_milliseconds = 0; |
| 268 if (base::StringToUint( |
| 269 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 270 switches::kPpapiKeepAliveThrottle), |
| 271 &keepalive_throttle_interval_milliseconds)) { |
| 272 ppapi::proxy::PluginGlobals::Get()-> |
| 273 set_keepalive_throttle_interval_milliseconds( |
| 274 keepalive_throttle_interval_milliseconds); |
| 275 } |
| 276 } |
| 277 |
| 268 } // namespace | 278 } // namespace |
| 269 | 279 |
| 270 void PpapiPluginRegisterThreadCreator( | 280 void PpapiPluginRegisterThreadCreator( |
| 271 const struct PP_ThreadFunctions* thread_functions) { | 281 const struct PP_ThreadFunctions* thread_functions) { |
| 282 #if defined(__native_client__) |
| 283 // Note: Currently, PPB_Audio_Shared::SetThreadFunctions is not available |
| 284 // on non-SFI mode, just because it is still under development. |
| 285 |
| 272 // Initialize all classes that need to create threads that call back into | 286 // Initialize all classes that need to create threads that call back into |
| 273 // user code. | 287 // user code. |
| 274 ppapi::PPB_Audio_Shared::SetThreadFunctions(thread_functions); | 288 ppapi::PPB_Audio_Shared::SetThreadFunctions(thread_functions); |
| 289 #endif |
| 275 } | 290 } |
| 276 | 291 |
| 277 int PpapiPluginMain() { | 292 int PpapiPluginMain() { |
| 293 LOG(ERROR) << "PpapiPluginMain()"; |
| 278 // Though it isn't referenced here, we must instantiate an AtExitManager. | 294 // Though it isn't referenced here, we must instantiate an AtExitManager. |
| 279 base::AtExitManager exit_manager; | 295 base::AtExitManager exit_manager; |
| 280 base::MessageLoop loop; | 296 base::MessageLoop loop; |
| 297 #if defined(IPC_MESSAGE_LOG_ENABLED) |
| 281 IPC::Logging::set_log_function_map(&g_log_function_mapping); | 298 IPC::Logging::set_log_function_map(&g_log_function_mapping); |
| 299 #endif |
| 282 ppapi::proxy::PluginGlobals plugin_globals; | 300 ppapi::proxy::PluginGlobals plugin_globals; |
| 283 base::Thread io_thread("Chrome_NaClIOThread"); | 301 base::Thread io_thread("Chrome_NaClIOThread"); |
| 284 base::Thread::Options options; | 302 base::Thread::Options options; |
| 285 options.message_loop_type = base::MessageLoop::TYPE_IO; | 303 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 286 io_thread.StartWithOptions(options); | 304 io_thread.StartWithOptions(options); |
| 287 | 305 |
| 306 #if defined(__native_client__) |
| 288 // Start up the SRPC server on another thread. Otherwise, when it blocks | 307 // Start up the SRPC server on another thread. Otherwise, when it blocks |
| 289 // on an RPC, the PPAPI proxy will hang. Do this before we initialize the | 308 // on an RPC, the PPAPI proxy will hang. Do this before we initialize the |
| 290 // module and start the PPAPI proxy so that the NaCl plugin can continue | 309 // module and start the PPAPI proxy so that the NaCl plugin can continue |
| 291 // loading the app. | 310 // loading the app. |
| 292 static struct NaClSrpcHandlerDesc srpc_methods[] = { { NULL, NULL } }; | 311 static struct NaClSrpcHandlerDesc srpc_methods[] = { { NULL, NULL } }; |
| 293 if (!NaClSrpcAcceptClientOnThread(srpc_methods)) { | 312 if (!NaClSrpcAcceptClientOnThread(srpc_methods)) { |
| 294 return 1; | 313 return 1; |
| 295 } | 314 } |
| 315 #endif |
| 316 |
| 317 // TODO fix me. |
| 318 ppapi::PpapiNaClChannelArgs args; |
| 319 args.permissions = ppapi::PpapiPermissions(33); |
| 320 |
| 321 static bool command_line_and_logging_initialized = false; |
| 322 if (!command_line_and_logging_initialized) { |
| 323 CommandLine::Init(0, NULL); |
| 324 for (size_t i = 0; i < args.switch_names.size(); ++i) { |
| 325 DCHECK(i < args.switch_values.size()); |
| 326 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 327 args.switch_names[i], args.switch_values[i]); |
| 328 } |
| 329 logging::LoggingSettings settings; |
| 330 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; |
| 331 logging::InitLogging(settings); |
| 332 SetPpapiKeepAliveThrottleFromCommandLine(); |
| 333 command_line_and_logging_initialized = true; |
| 334 } |
| 335 |
| 336 // Tell the process-global GetInterface which interfaces it can return to the |
| 337 // plugin. |
| 338 ppapi::proxy::InterfaceList::SetProcessGlobalPermissions( |
| 339 args.permissions); |
| 296 | 340 |
| 297 PpapiDispatcher ppapi_dispatcher(io_thread.message_loop_proxy()); | 341 PpapiDispatcher ppapi_dispatcher(io_thread.message_loop_proxy()); |
| 298 plugin_globals.set_plugin_proxy_delegate(&ppapi_dispatcher); | 342 plugin_globals.set_plugin_proxy_delegate(&ppapi_dispatcher); |
| 299 | 343 |
| 344 PluginDispatcher* plugin_dispatcher = |
| 345 new PluginDispatcher(::PPP_GetInterface, args.permissions, |
| 346 args.off_the_record); |
| 347 |
| 348 // The channel handle's true name is not revealed here. |
| 349 IPC::ChannelHandle channel_handle( |
| 350 "nacl", |
| 351 base::FileDescriptor(ppapi::proxy::g_nacl_renderer_ipc_fd, false)); |
| 352 if (!plugin_dispatcher->InitPluginWithChannel( |
| 353 &ppapi_dispatcher, base::kNullProcessId, |
| 354 channel_handle, false)) { |
| 355 delete plugin_dispatcher; |
| 356 return 1; |
| 357 } |
| 358 |
| 359 // From here, the dispatcher will manage its own lifetime according to the |
| 360 // lifetime of the attached channel. |
| 361 |
| 362 int32_t error = ::PPP_InitializeModule( |
| 363 0 /* module */, |
| 364 &ppapi::proxy::PluginDispatcher::GetBrowserInterface); |
| 365 if (error) |
| 366 ::exit(error); |
| 367 |
| 368 if (ppapi::proxy::g_ppapi_start_event) { |
| 369 ppapi::proxy::g_ppapi_start_event->Signal(); |
| 370 ppapi::proxy::g_ppapi_start_event = NULL; |
| 371 } |
| 372 |
| 300 loop.Run(); | 373 loop.Run(); |
| 301 | 374 |
| 302 return 0; | 375 return 0; |
| 303 } | 376 } |
| OLD | NEW |