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