| OLD | NEW |
| (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/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_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 using ppapi::proxy::PluginDispatcher; | |
| 49 using ppapi::proxy::PluginGlobals; | |
| 50 using ppapi::proxy::PluginProxyDelegate; | |
| 51 using ppapi::proxy::ProxyChannel; | |
| 52 using ppapi::proxy::SerializedHandle; | |
| 53 | |
| 54 namespace { | |
| 55 | |
| 56 // This class manages communication between the plugin and the browser, and | |
| 57 // manages the PluginDispatcher instances for communication between the plugin | |
| 58 // and the renderer. | |
| 59 class PpapiDispatcher : public ProxyChannel, | |
| 60 public PluginDispatcher::PluginDelegate, | |
| 61 public PluginProxyDelegate { | |
| 62 public: | |
| 63 explicit PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop); | |
| 64 | |
| 65 // PluginDispatcher::PluginDelegate implementation. | |
| 66 virtual base::MessageLoopProxy* GetIPCMessageLoop() OVERRIDE; | |
| 67 virtual base::WaitableEvent* GetShutdownEvent() OVERRIDE; | |
| 68 virtual IPC::PlatformFileForTransit ShareHandleWithRemote( | |
| 69 base::PlatformFile handle, | |
| 70 base::ProcessId peer_pid, | |
| 71 bool should_close_source) OVERRIDE; | |
| 72 virtual std::set<PP_Instance>* GetGloballySeenInstanceIDSet() OVERRIDE; | |
| 73 virtual uint32 Register(PluginDispatcher* plugin_dispatcher) OVERRIDE; | |
| 74 virtual void Unregister(uint32 plugin_dispatcher_id) OVERRIDE; | |
| 75 | |
| 76 // PluginProxyDelegate implementation. | |
| 77 virtual IPC::Sender* GetBrowserSender() OVERRIDE; | |
| 78 virtual std::string GetUILanguage() OVERRIDE; | |
| 79 virtual void PreCacheFont(const void* logfontw) OVERRIDE; | |
| 80 virtual void SetActiveURL(const std::string& url) OVERRIDE; | |
| 81 virtual PP_Resource CreateBrowserFont( | |
| 82 ppapi::proxy::Connection connection, | |
| 83 PP_Instance instance, | |
| 84 const PP_BrowserFont_Trusted_Description& desc, | |
| 85 const ppapi::Preferences& prefs) OVERRIDE; | |
| 86 | |
| 87 // IPC::Listener implementation. | |
| 88 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
| 89 | |
| 90 private: | |
| 91 void OnMsgInitializeNaClDispatcher(const ppapi::PpapiNaClPluginArgs& args); | |
| 92 void OnPluginDispatcherMessageReceived(const IPC::Message& msg); | |
| 93 | |
| 94 void SetPpapiKeepAliveThrottleFromCommandLine(); | |
| 95 | |
| 96 std::set<PP_Instance> instances_; | |
| 97 std::map<uint32, PluginDispatcher*> plugin_dispatchers_; | |
| 98 uint32 next_plugin_dispatcher_id_; | |
| 99 scoped_refptr<base::MessageLoopProxy> message_loop_; | |
| 100 base::WaitableEvent shutdown_event_; | |
| 101 }; | |
| 102 | |
| 103 PpapiDispatcher::PpapiDispatcher(scoped_refptr<base::MessageLoopProxy> io_loop) | |
| 104 : next_plugin_dispatcher_id_(0), | |
| 105 message_loop_(io_loop), | |
| 106 shutdown_event_(true, false) { | |
| 107 // The first FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the | |
| 108 // browser. | |
| 109 IPC::ChannelHandle channel_handle( | |
| 110 "NaCl IPC", base::FileDescriptor(NACL_CHROME_DESC_BASE, false)); | |
| 111 // We don't have/need a PID since handle sharing happens outside of the | |
| 112 // NaCl sandbox. | |
| 113 InitWithChannel(this, base::kNullProcessId, channel_handle, | |
| 114 false); // Channel is server. | |
| 115 channel()->AddFilter(new ppapi::proxy::PluginMessageFilter( | |
| 116 NULL, PluginGlobals::Get()->resource_reply_thread_registrar())); | |
| 117 channel()->AddFilter( | |
| 118 new tracing::ChildTraceMessageFilter(message_loop_.get())); | |
| 119 } | |
| 120 | |
| 121 base::MessageLoopProxy* PpapiDispatcher::GetIPCMessageLoop() { | |
| 122 return message_loop_.get(); | |
| 123 } | |
| 124 | |
| 125 base::WaitableEvent* PpapiDispatcher::GetShutdownEvent() { | |
| 126 return &shutdown_event_; | |
| 127 } | |
| 128 | |
| 129 IPC::PlatformFileForTransit PpapiDispatcher::ShareHandleWithRemote( | |
| 130 base::PlatformFile handle, | |
| 131 base::ProcessId peer_pid, | |
| 132 bool should_close_source) { | |
| 133 return IPC::InvalidPlatformFileForTransit(); | |
| 134 } | |
| 135 | |
| 136 std::set<PP_Instance>* PpapiDispatcher::GetGloballySeenInstanceIDSet() { | |
| 137 return &instances_; | |
| 138 } | |
| 139 | |
| 140 uint32 PpapiDispatcher::Register(PluginDispatcher* plugin_dispatcher) { | |
| 141 if (!plugin_dispatcher || | |
| 142 plugin_dispatchers_.size() >= std::numeric_limits<uint32>::max()) { | |
| 143 return 0; | |
| 144 } | |
| 145 | |
| 146 uint32 id = 0; | |
| 147 do { | |
| 148 // Although it is unlikely, make sure that we won't cause any trouble | |
| 149 // when the counter overflows. | |
| 150 id = next_plugin_dispatcher_id_++; | |
| 151 } while (id == 0 || | |
| 152 plugin_dispatchers_.find(id) != plugin_dispatchers_.end()); | |
| 153 plugin_dispatchers_[id] = plugin_dispatcher; | |
| 154 return id; | |
| 155 } | |
| 156 | |
| 157 void PpapiDispatcher::Unregister(uint32 plugin_dispatcher_id) { | |
| 158 plugin_dispatchers_.erase(plugin_dispatcher_id); | |
| 159 } | |
| 160 | |
| 161 IPC::Sender* PpapiDispatcher::GetBrowserSender() { | |
| 162 return this; | |
| 163 } | |
| 164 | |
| 165 std::string PpapiDispatcher::GetUILanguage() { | |
| 166 NOTIMPLEMENTED(); | |
| 167 return std::string(); | |
| 168 } | |
| 169 | |
| 170 void PpapiDispatcher::PreCacheFont(const void* logfontw) { | |
| 171 NOTIMPLEMENTED(); | |
| 172 } | |
| 173 | |
| 174 void PpapiDispatcher::SetActiveURL(const std::string& url) { | |
| 175 NOTIMPLEMENTED(); | |
| 176 } | |
| 177 | |
| 178 PP_Resource PpapiDispatcher::CreateBrowserFont( | |
| 179 ppapi::proxy::Connection connection, | |
| 180 PP_Instance instance, | |
| 181 const PP_BrowserFont_Trusted_Description& desc, | |
| 182 const ppapi::Preferences& prefs) { | |
| 183 NOTIMPLEMENTED(); | |
| 184 return 0; | |
| 185 } | |
| 186 | |
| 187 bool PpapiDispatcher::OnMessageReceived(const IPC::Message& msg) { | |
| 188 IPC_BEGIN_MESSAGE_MAP(PpapiDispatcher, msg) | |
| 189 IPC_MESSAGE_HANDLER(PpapiMsg_InitializeNaClDispatcher, | |
| 190 OnMsgInitializeNaClDispatcher) | |
| 191 // All other messages are simply forwarded to a PluginDispatcher. | |
| 192 IPC_MESSAGE_UNHANDLED(OnPluginDispatcherMessageReceived(msg)) | |
| 193 IPC_END_MESSAGE_MAP() | |
| 194 return true; | |
| 195 } | |
| 196 | |
| 197 void PpapiDispatcher::OnMsgInitializeNaClDispatcher( | |
| 198 const ppapi::PpapiNaClPluginArgs& args) { | |
| 199 static bool command_line_and_logging_initialized = false; | |
| 200 if (command_line_and_logging_initialized) { | |
| 201 LOG(FATAL) << "InitializeNaClDispatcher must be called once per plugin."; | |
| 202 return; | |
| 203 } | |
| 204 | |
| 205 command_line_and_logging_initialized = true; | |
| 206 CommandLine::Init(0, NULL); | |
| 207 for (size_t i = 0; i < args.switch_names.size(); ++i) { | |
| 208 DCHECK(i < args.switch_values.size()); | |
| 209 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
| 210 args.switch_names[i], args.switch_values[i]); | |
| 211 } | |
| 212 logging::LoggingSettings settings; | |
| 213 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | |
| 214 logging::InitLogging(settings); | |
| 215 SetPpapiKeepAliveThrottleFromCommandLine(); | |
| 216 | |
| 217 // Tell the process-global GetInterface which interfaces it can return to the | |
| 218 // plugin. | |
| 219 ppapi::proxy::InterfaceList::SetProcessGlobalPermissions( | |
| 220 args.permissions); | |
| 221 | |
| 222 int32_t error = ::PPP_InitializeModule( | |
| 223 0 /* module */, | |
| 224 &ppapi::proxy::PluginDispatcher::GetBrowserInterface); | |
| 225 if (error) | |
| 226 ::exit(error); | |
| 227 | |
| 228 PluginDispatcher* dispatcher = | |
| 229 new PluginDispatcher(::PPP_GetInterface, args.permissions, | |
| 230 args.off_the_record); | |
| 231 // 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 | |
| 233 // renderer. | |
| 234 IPC::ChannelHandle channel_handle( | |
| 235 "nacl", base::FileDescriptor(NACL_CHROME_DESC_BASE + 1, false)); | |
| 236 if (!dispatcher->InitPluginWithChannel(this, base::kNullProcessId, | |
| 237 channel_handle, false)) { | |
| 238 delete dispatcher; | |
| 239 return; | |
| 240 } | |
| 241 // From here, the dispatcher will manage its own lifetime according to the | |
| 242 // lifetime of the attached channel. | |
| 243 } | |
| 244 | |
| 245 void PpapiDispatcher::OnPluginDispatcherMessageReceived( | |
| 246 const IPC::Message& msg) { | |
| 247 // The first parameter should be a plugin dispatcher ID. | |
| 248 PickleIterator iter(msg); | |
| 249 uint32 id = 0; | |
| 250 if (!msg.ReadUInt32(&iter, &id)) { | |
| 251 NOTREACHED(); | |
| 252 return; | |
| 253 } | |
| 254 std::map<uint32, ppapi::proxy::PluginDispatcher*>::iterator dispatcher = | |
| 255 plugin_dispatchers_.find(id); | |
| 256 if (dispatcher != plugin_dispatchers_.end()) | |
| 257 dispatcher->second->OnMessageReceived(msg); | |
| 258 } | |
| 259 | |
| 260 void PpapiDispatcher::SetPpapiKeepAliveThrottleFromCommandLine() { | |
| 261 unsigned keepalive_throttle_interval_milliseconds = 0; | |
| 262 if (base::StringToUint( | |
| 263 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 264 switches::kPpapiKeepAliveThrottle), | |
| 265 &keepalive_throttle_interval_milliseconds)) { | |
| 266 ppapi::proxy::PluginGlobals::Get()-> | |
| 267 set_keepalive_throttle_interval_milliseconds( | |
| 268 keepalive_throttle_interval_milliseconds); | |
| 269 } | |
| 270 } | |
| 271 | |
| 272 } // namespace | |
| 273 | |
| 274 void PpapiPluginRegisterThreadCreator( | |
| 275 const struct PP_ThreadFunctions* thread_functions) { | |
| 276 // Initialize all classes that need to create threads that call back into | |
| 277 // user code. | |
| 278 ppapi::PPB_Audio_Shared::SetThreadFunctions(thread_functions); | |
| 279 } | |
| 280 | |
| 281 int PpapiPluginMain() { | |
| 282 // Though it isn't referenced here, we must instantiate an AtExitManager. | |
| 283 base::AtExitManager exit_manager; | |
| 284 base::MessageLoop loop; | |
| 285 IPC::Logging::set_log_function_map(&g_log_function_mapping); | |
| 286 ppapi::proxy::PluginGlobals plugin_globals; | |
| 287 base::Thread io_thread("Chrome_NaClIOThread"); | |
| 288 base::Thread::Options options; | |
| 289 options.message_loop_type = base::MessageLoop::TYPE_IO; | |
| 290 io_thread.StartWithOptions(options); | |
| 291 | |
| 292 // 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 | |
| 294 // module and start the PPAPI proxy so that the NaCl plugin can continue | |
| 295 // loading the app. | |
| 296 static struct NaClSrpcHandlerDesc srpc_methods[] = { { NULL, NULL } }; | |
| 297 if (!NaClSrpcAcceptClientOnThread(srpc_methods)) { | |
| 298 return 1; | |
| 299 } | |
| 300 | |
| 301 PpapiDispatcher ppapi_dispatcher(io_thread.message_loop_proxy()); | |
| 302 plugin_globals.set_plugin_proxy_delegate(&ppapi_dispatcher); | |
| 303 | |
| 304 loop.Run(); | |
| 305 | |
| 306 return 0; | |
| 307 } | |
| OLD | NEW |