From 48977dbb85422ade0a5a488f3b9656d3c5d0bc80 Mon Sep 17 00:00:00 2001 From: "loislo@chromium.org" Date: Fri, 21 Feb 2014 06:07:53 +0000 Subject: Revert "Connect PPAPI IPC channels for non-SFI mode." This reverts commit 3d41a2a63bbd307fcee6e6c5547ce5fe29e2f4a5. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@252506 0039d316-1c4b-4281-b951-d872f2087c98 --- components/nacl.gyp | 5 - components/nacl/loader/DEPS | 3 - components/nacl/loader/nacl_listener.cc | 52 +-- components/nacl/loader/nonsfi/DEPS | 3 - components/nacl/loader/nonsfi/irt_interfaces.cc | 1 - components/nacl/loader/nonsfi/irt_interfaces.h | 1 - components/nacl/loader/nonsfi/irt_ppapi.cc | 44 --- components/nacl/loader/nonsfi/nonsfi_main.cc | 61 +--- ipc/ipc_channel.h | 6 - .../src/shared/ppapi_proxy/ppruntime.h | 3 + ppapi/ppapi_proxy.gypi | 3 +- ppapi/proxy/irt_ppapi.c | 2 +- ppapi/proxy/plugin_main_irt.cc | 362 --------------------- ppapi/proxy/plugin_main_irt.h | 31 -- ppapi/proxy/plugin_main_nacl.cc | 327 +++++++++++++++++++ 15 files changed, 355 insertions(+), 549 deletions(-) delete mode 100644 components/nacl/loader/nonsfi/DEPS delete mode 100644 components/nacl/loader/nonsfi/irt_ppapi.cc delete mode 100644 ppapi/proxy/plugin_main_irt.cc delete mode 100644 ppapi/proxy/plugin_main_irt.h create mode 100644 ppapi/proxy/plugin_main_nacl.cc diff --git a/components/nacl.gyp b/components/nacl.gyp index 3f67983..8f0ff77f 100644 --- a/components/nacl.gyp +++ b/components/nacl.gyp @@ -181,11 +181,9 @@ 'dependencies': [ 'nacl', 'nacl_common', - '../components/tracing.gyp:tracing', '../crypto/crypto.gyp:crypto', '../sandbox/sandbox.gyp:libc_urandom_override', '../sandbox/sandbox.gyp:sandbox', - '../ppapi/ppapi_internal.gyp:ppapi_proxy', ], 'defines': [ '<@(nacl_defines)', @@ -208,7 +206,6 @@ 'nacl/loader/nonsfi/irt_interfaces.cc', 'nacl/loader/nonsfi/irt_interfaces.h', 'nacl/loader/nonsfi/irt_memory.cc', - 'nacl/loader/nonsfi/irt_ppapi.cc', 'nacl/loader/nonsfi/irt_thread.cc', 'nacl/loader/nonsfi/irt_util.h', 'nacl/loader/nonsfi/nonsfi_main.cc', @@ -219,8 +216,6 @@ '../content/common/sandbox_linux/sandbox_init_linux.cc', '../content/common/sandbox_linux/sandbox_seccomp_bpf_linux.cc', '../content/public/common/content_switches.cc', - '../ppapi/proxy/plugin_main_irt.cc', - '../ppapi/proxy/plugin_main_irt.h', ], 'conditions': [ ['toolkit_uses_gtk == 1', { diff --git a/components/nacl/loader/DEPS b/components/nacl/loader/DEPS index 6167777..8345fef 100644 --- a/components/nacl/loader/DEPS +++ b/components/nacl/loader/DEPS @@ -22,9 +22,6 @@ include_rules = [ "+ppapi/proxy/nacl_message_scanner.h", "+ppapi/proxy/serialized_handle.h", - # For setting FDs in non-SFI mode. - "+ppapi/proxy/plugin_main_irt.h", - # For sending PpapiHostMsg_ChannelCreated in nacl_ipc_adapter.cc: "+ppapi/proxy/ppapi_messages.h" ] diff --git a/components/nacl/loader/nacl_listener.cc b/components/nacl/loader/nacl_listener.cc index 4bafc8c..c35d946 100644 --- a/components/nacl/loader/nacl_listener.cc +++ b/components/nacl/loader/nacl_listener.cc @@ -35,7 +35,6 @@ #if defined(OS_LINUX) #include "components/nacl/loader/nonsfi/nonsfi_main.h" #include "content/public/common/child_process_sandbox_support_linux.h" -#include "ppapi/proxy/plugin_main_irt.h" #endif #if defined(OS_WIN) @@ -280,54 +279,19 @@ void NaClListener::OnStart(const nacl::NaClStartParams& params) { } if (params.enable_ipc_proxy) { + // Create the PPAPI IPC channels between the NaCl IRT and the hosts + // (browser/renderer) processes. The IRT uses these channels to communicate + // with the host and to initialize the IPC dispatchers. IPC::ChannelHandle browser_handle = IPC::Channel::GenerateVerifiedChannelID("nacl"); + SetUpIPCAdapter(&browser_handle, io_thread_.message_loop_proxy(), + nap, NACL_CHROME_DESC_BASE); + IPC::ChannelHandle renderer_handle = IPC::Channel::GenerateVerifiedChannelID("nacl"); + SetUpIPCAdapter(&renderer_handle, io_thread_.message_loop_proxy(), + nap, NACL_CHROME_DESC_BASE + 1); -#if defined(OS_LINUX) - if (params.enable_nonsfi_mode) { - // In non-SFI mode, we neither intercept nor rewrite the message using - // NaClIPCAdapter, and the channels are connected between the plugin and - // the hosts directly. So, the IPC::Channel instances will be created in - // the plugin side, because the IPC::Listener needs to live on the - // plugin's main thread. However, on initialization (i.e. before loading - // the plugin binary), the FD needs to be passed to the hosts. So, here - // we create raw FD pairs, and pass the client side FDs to the hosts, - // and the server side FDs to the plugin. - int browser_server_ppapi_fd; - int browser_client_ppapi_fd; - int renderer_server_ppapi_fd; - int renderer_client_ppapi_fd; - if (!IPC::SocketPair( - &browser_server_ppapi_fd, &browser_client_ppapi_fd) || - !IPC::SocketPair( - &renderer_server_ppapi_fd, &renderer_client_ppapi_fd)) { - LOG(ERROR) << "Failed to create sockets for IPC."; - return; - } - - // Set the plugin IPC channel FDs. - SetIPCFileDescriptors( - browser_server_ppapi_fd, renderer_server_ppapi_fd); - - // Send back to the client side IPC channel FD to the host. - browser_handle.socket = - base::FileDescriptor(browser_client_ppapi_fd, true); - renderer_handle.socket = - base::FileDescriptor(renderer_client_ppapi_fd, true); - } else { -#endif - // Create the PPAPI IPC channels between the NaCl IRT and the host - // (browser/renderer) processes. The IRT uses these channels to - // communicate with the host and to initialize the IPC dispatchers. - SetUpIPCAdapter(&browser_handle, io_thread_.message_loop_proxy(), - nap, NACL_CHROME_DESC_BASE); - SetUpIPCAdapter(&renderer_handle, io_thread_.message_loop_proxy(), - nap, NACL_CHROME_DESC_BASE + 1); -#if defined(OS_LINUX) - } -#endif if (!Send(new NaClProcessHostMsg_PpapiChannelsCreated( browser_handle, renderer_handle))) LOG(ERROR) << "Failed to send IPC channel handle to NaClProcessHost."; diff --git a/components/nacl/loader/nonsfi/DEPS b/components/nacl/loader/nonsfi/DEPS deleted file mode 100644 index fa6de5d..0000000 --- a/components/nacl/loader/nonsfi/DEPS +++ /dev/null @@ -1,3 +0,0 @@ -include_rules = [ - "+ppapi/nacl_irt", -] diff --git a/components/nacl/loader/nonsfi/irt_interfaces.cc b/components/nacl/loader/nonsfi/irt_interfaces.cc index 4364bb8..c53c795 100644 --- a/components/nacl/loader/nonsfi/irt_interfaces.cc +++ b/components/nacl/loader/nonsfi/irt_interfaces.cc @@ -29,7 +29,6 @@ const NaClInterfaceTable kIrtInterfaces[] = { NACL_INTERFACE_TABLE(NACL_IRT_FUTEX_v0_1, kIrtFutex), NACL_INTERFACE_TABLE(NACL_IRT_TLS_v0_1, kIrtTls), NACL_INTERFACE_TABLE(NACL_IRT_CLOCK_v0_1, kIrtClock), - NACL_INTERFACE_TABLE(NACL_IRT_PPAPIHOOK_v0_1, kIrtPpapiHook), }; #undef NACL_INTERFACE_TABLE diff --git a/components/nacl/loader/nonsfi/irt_interfaces.h b/components/nacl/loader/nonsfi/irt_interfaces.h index a2bf468..78c5bf0 100644 --- a/components/nacl/loader/nonsfi/irt_interfaces.h +++ b/components/nacl/loader/nonsfi/irt_interfaces.h @@ -21,7 +21,6 @@ extern const struct nacl_irt_thread kIrtThread; extern const struct nacl_irt_futex kIrtFutex; extern const struct nacl_irt_tls kIrtTls; extern const struct nacl_irt_clock kIrtClock; -extern const struct nacl_irt_ppapihook kIrtPpapiHook; } // namespace nonsfi } // namespace nacl diff --git a/components/nacl/loader/nonsfi/irt_ppapi.cc b/components/nacl/loader/nonsfi/irt_ppapi.cc deleted file mode 100644 index ebe2893..0000000 --- a/components/nacl/loader/nonsfi/irt_ppapi.cc +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/logging.h" -#include "components/nacl/loader/nonsfi/irt_interfaces.h" -#include "ppapi/c/ppp.h" -#include "ppapi/nacl_irt/irt_ppapi.h" -#include "ppapi/proxy/plugin_main_irt.h" - -namespace nacl { -namespace nonsfi { -namespace { - -struct PP_StartFunctions g_pp_functions; - -int IrtPpapiStart(const struct PP_StartFunctions* funcs) { - g_pp_functions = *funcs; - return PpapiPluginMain(); -} - -} // namespace - -const struct nacl_irt_ppapihook kIrtPpapiHook = { - IrtPpapiStart, - PpapiPluginRegisterThreadCreator, -}; - -} // namespace nonsfi -} // namespace nacl - -int32_t PPP_InitializeModule(PP_Module module_id, - PPB_GetInterface get_browser_interface) { - return nacl::nonsfi::g_pp_functions.PPP_InitializeModule( - module_id, get_browser_interface); -} - -void PPP_ShutdownModule(void) { - nacl::nonsfi::g_pp_functions.PPP_ShutdownModule(); -} - -const void *PPP_GetInterface(const char *interface_name) { - return nacl::nonsfi::g_pp_functions.PPP_GetInterface(interface_name); -} diff --git a/components/nacl/loader/nonsfi/nonsfi_main.cc b/components/nacl/loader/nonsfi/nonsfi_main.cc index 87fbc1b..d9847c52 100644 --- a/components/nacl/loader/nonsfi/nonsfi_main.cc +++ b/components/nacl/loader/nonsfi/nonsfi_main.cc @@ -4,10 +4,8 @@ #include "components/nacl/loader/nonsfi/nonsfi_main.h" -#include "base/debug/leak_annotations.h" #include "base/logging.h" #include "base/memory/scoped_ptr.h" -#include "base/threading/platform_thread.h" #include "components/nacl/loader/nonsfi/elf_loader.h" #include "components/nacl/loader/nonsfi/irt_interfaces.h" #include "native_client/src/include/elf_auxv.h" @@ -24,41 +22,6 @@ namespace nacl { namespace nonsfi { namespace { -typedef void (*EntryPointType)(uintptr_t*); - -class PluginMainDelegate : public base::PlatformThread::Delegate { - public: - explicit PluginMainDelegate(EntryPointType entry_point) - : entry_point_(entry_point) { - } - - virtual ~PluginMainDelegate() { - } - - virtual void ThreadMain() OVERRIDE { - base::PlatformThread::SetName("NaClMainThread"); - - uintptr_t info[] = { - 0, // Do not use fini. - 0, // envc. - 0, // argc. - 0, // Null terminate for argv. - 0, // Null terminate for envv. - AT_SYSINFO, - reinterpret_cast(&NaClIrtInterface), - AT_NULL, - 0, // Null terminate for auxv. - }; - entry_point_(info); - } - - private: - EntryPointType entry_point_; -}; - -// Default stack size of the plugin main thread. We heuristically chose 16M. -const size_t kStackSize = (16 << 20); - struct NaClDescUnrefer { void operator()(struct NaClDesc* desc) const { NaClDescUnref(desc); @@ -83,16 +46,22 @@ void LoadModuleRpc(struct NaClSrpcRpc* rpc, return; } - EntryPointType entry_point = - reinterpret_cast(image.entry_point()); - if (!base::PlatformThread::CreateNonJoinable( - kStackSize, new PluginMainDelegate(entry_point))) { - LOG(ERROR) << "LoadModuleRpc: Failed to create plugin main thread."; - return; - } - + uintptr_t entry_point = image.entry_point(); rpc->result = NACL_SRPC_RESULT_OK; - (*done_cls->Run)(done_cls); + + // Run for testing. TODO(hidehiko): Remove this. + uintptr_t info[] = { + 0, // Do not use fini. + 0, // envc. + 0, // argc. + 0, // Null terminate for argv. + 0, // Null terminate for envv. + AT_SYSINFO, + reinterpret_cast(&NaClIrtInterface), + AT_NULL, + 0, // Null terminate for auxv. + }; + reinterpret_cast(entry_point)(info); } const static struct NaClSrpcHandlerDesc kNonSfiServiceHandlers[] = { diff --git a/ipc/ipc_channel.h b/ipc/ipc_channel.h index bfa5ec6..7e09a80 100644 --- a/ipc/ipc_channel.h +++ b/ipc/ipc_channel.h @@ -217,12 +217,6 @@ class IPC_EXPORT Channel : public Sender { ChannelImpl *channel_impl_; }; -#if defined(OS_POSIX) -// SocketPair() creates a pair of socket FDs suitable for using with -// IPC::Channel. -IPC_EXPORT bool SocketPair(int* fd1, int* fd2); -#endif - } // namespace IPC #endif // IPC_IPC_CHANNEL_H_ diff --git a/ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h b/ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h index ac23a34..c7fe8a0 100644 --- a/ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h +++ b/ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h @@ -15,6 +15,9 @@ EXTERN_C_BEGIN // The entry point for the main thread of the PPAPI plugin process. int PpapiPluginMain(void); +void PpapiPluginRegisterThreadCreator( + const struct PP_ThreadFunctions* new_funcs); + EXTERN_C_END #endif // NATIVE_CLIENT_SRC_SHARED_PPAPI_PROXY_PPRUNTIME_H_ diff --git a/ppapi/ppapi_proxy.gypi b/ppapi/ppapi_proxy.gypi index d6fc604..f345414 100644 --- a/ppapi/ppapi_proxy.gypi +++ b/ppapi/ppapi_proxy.gypi @@ -112,6 +112,7 @@ 'proxy/plugin_dispatcher.h', 'proxy/plugin_globals.cc', 'proxy/plugin_globals.h', + 'proxy/plugin_main_nacl.cc', 'proxy/plugin_message_filter.cc', 'proxy/plugin_message_filter.h', 'proxy/plugin_resource.cc', @@ -243,8 +244,6 @@ ['>(nacl_untrusted_build)==1', { 'sources': [ 'proxy/irt_ppapi.c', - 'proxy/plugin_main_irt.cc', - 'proxy/plugin_main_irt.h', ], 'sources!': [ 'proxy/audio_input_resource.cc', diff --git a/ppapi/proxy/irt_ppapi.c b/ppapi/proxy/irt_ppapi.c index 24dafdb..be6a216 100644 --- a/ppapi/proxy/irt_ppapi.c +++ b/ppapi/proxy/irt_ppapi.c @@ -8,7 +8,7 @@ #include "native_client/src/untrusted/irt/irt.h" #include "native_client/src/untrusted/irt/irt_private.h" #include "ppapi/nacl_irt/irt_ppapi.h" -#include "ppapi/proxy/plugin_main_irt.h" +#include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h" struct PP_StartFunctions g_pp_functions; diff --git a/ppapi/proxy/plugin_main_irt.cc b/ppapi/proxy/plugin_main_irt.cc deleted file mode 100644 index a3ea6c1..0000000 --- a/ppapi/proxy/plugin_main_irt.cc +++ /dev/null @@ -1,362 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "ppapi/proxy/plugin_main_irt.h" - -#include -#include -#include - -#include "build/build_config.h" -// Need to include this before most other files because it defines -// IPC_MESSAGE_LOG_ENABLED. We need to use it to define -// IPC_MESSAGE_MACROS_LOG_ENABLED so ppapi_messages.h will generate the -// ViewMsgLog et al. functions. - -#include "base/at_exit.h" -#include "base/command_line.h" -#include "base/memory/scoped_ptr.h" -#include "base/message_loop/message_loop.h" -#include "base/strings/string_number_conversions.h" -#include "base/synchronization/waitable_event.h" -#include "base/threading/thread.h" -#include "components/tracing/child_trace_message_filter.h" -#include "ipc/ipc_channel_handle.h" -#include "ipc/ipc_logging.h" -#include "ipc/ipc_message.h" -#include "ppapi/c/ppp.h" -#include "ppapi/c/ppp_instance.h" -#include "ppapi/proxy/plugin_dispatcher.h" -#include "ppapi/proxy/plugin_globals.h" -#include "ppapi/proxy/plugin_message_filter.h" -#include "ppapi/proxy/plugin_proxy_delegate.h" -#include "ppapi/proxy/resource_reply_thread_registrar.h" -#include "ppapi/shared_impl/ppapi_switches.h" -#include "ppapi/shared_impl/ppb_audio_shared.h" - -#if defined(__native_client__) -#include "native_client/src/public/chrome_main.h" -#include "native_client/src/shared/srpc/nacl_srpc.h" -#endif - -#if defined(IPC_MESSAGE_LOG_ENABLED) -#include "base/containers/hash_tables.h" - -LogFunctionMap g_log_function_mapping; - -#define IPC_MESSAGE_MACROS_LOG_ENABLED -#define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \ - g_log_function_mapping[msg_id] = logger - -#endif -#include "ppapi/proxy/ppapi_messages.h" - -using ppapi::proxy::PluginDispatcher; -using ppapi::proxy::PluginGlobals; -using ppapi::proxy::PluginProxyDelegate; -using ppapi::proxy::ProxyChannel; -using ppapi::proxy::SerializedHandle; - -namespace { - -#if defined(__native_client__) -// In SFI mode, the FDs of IPC channels are NACL_CHROME_DESC_BASE and its -// successor, which is set in nacl_listener.cc. -int g_nacl_ipc_browser_fd = NACL_CHROME_DESC_BASE; -int g_nacl_ipc_renderer_fd = NACL_CHROME_DESC_BASE + 1; -#else -// In non-SFI mode, the FDs of IPC channels are different from the hard coded -// ones. These values will be set by SetIPCFileDescriptors() below. -// At first, both are initialized to invalid FD number (-1). -int g_nacl_ipc_browser_fd = -1; -int g_nacl_ipc_renderer_fd = -1; -#endif - -// This class manages communication between the plugin and the browser, and -// manages the PluginDispatcher instances for communication between the plugin -// and the renderer. -class PpapiDispatcher : public PluginDispatcher::PluginDelegate, - public PluginProxyDelegate, - public IPC::Listener, - public IPC::Sender { - public: - explicit PpapiDispatcher(scoped_refptr io_loop); - - // PluginDispatcher::PluginDelegate implementation. - virtual base::MessageLoopProxy* GetIPCMessageLoop() OVERRIDE; - virtual base::WaitableEvent* GetShutdownEvent() OVERRIDE; - virtual IPC::PlatformFileForTransit ShareHandleWithRemote( - base::PlatformFile handle, - base::ProcessId peer_pid, - bool should_close_source) OVERRIDE; - virtual std::set* GetGloballySeenInstanceIDSet() OVERRIDE; - virtual uint32 Register(PluginDispatcher* plugin_dispatcher) OVERRIDE; - virtual void Unregister(uint32 plugin_dispatcher_id) OVERRIDE; - - // PluginProxyDelegate implementation. - virtual IPC::Sender* GetBrowserSender() OVERRIDE; - virtual std::string GetUILanguage() OVERRIDE; - virtual void PreCacheFont(const void* logfontw) OVERRIDE; - virtual void SetActiveURL(const std::string& url) OVERRIDE; - virtual PP_Resource CreateBrowserFont( - ppapi::proxy::Connection connection, - PP_Instance instance, - const PP_BrowserFont_Trusted_Description& desc, - const ppapi::Preferences& prefs) OVERRIDE; - - // IPC::Listener implementation. - virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; - virtual void OnChannelError() OVERRIDE; - - // IPC::Sender implementation - virtual bool Send(IPC::Message* message) OVERRIDE; - - private: - void OnMsgInitializeNaClDispatcher(const ppapi::PpapiNaClPluginArgs& args); - void OnPluginDispatcherMessageReceived(const IPC::Message& msg); - - void SetPpapiKeepAliveThrottleFromCommandLine(); - - std::set instances_; - std::map plugin_dispatchers_; - uint32 next_plugin_dispatcher_id_; - scoped_refptr message_loop_; - base::WaitableEvent shutdown_event_; - scoped_ptr channel_; -}; - -PpapiDispatcher::PpapiDispatcher(scoped_refptr io_loop) - : next_plugin_dispatcher_id_(0), - message_loop_(io_loop), - shutdown_event_(true, false) { - DCHECK_NE(g_nacl_ipc_browser_fd, -1) - << "g_nacl_ipc_browser_fd must be initialized before the plugin starts"; - IPC::ChannelHandle channel_handle( - "NaCl IPC", base::FileDescriptor(g_nacl_ipc_browser_fd, false)); - - // Delay initializing the SyncChannel until after we add filters. This - // ensures that the filters won't miss any messages received by - // the channel. - channel_.reset(new IPC::SyncChannel( - this, GetIPCMessageLoop(), GetShutdownEvent())); - channel_->AddFilter(new ppapi::proxy::PluginMessageFilter( - NULL, PluginGlobals::Get()->resource_reply_thread_registrar())); - channel_->AddFilter( - new tracing::ChildTraceMessageFilter(message_loop_.get())); - channel_->Init(channel_handle, IPC::Channel::MODE_SERVER, true); -} - -base::MessageLoopProxy* PpapiDispatcher::GetIPCMessageLoop() { - return message_loop_.get(); -} - -base::WaitableEvent* PpapiDispatcher::GetShutdownEvent() { - return &shutdown_event_; -} - -IPC::PlatformFileForTransit PpapiDispatcher::ShareHandleWithRemote( - base::PlatformFile handle, - base::ProcessId peer_pid, - bool should_close_source) { - return IPC::InvalidPlatformFileForTransit(); -} - -std::set* PpapiDispatcher::GetGloballySeenInstanceIDSet() { - return &instances_; -} - -uint32 PpapiDispatcher::Register(PluginDispatcher* plugin_dispatcher) { - if (!plugin_dispatcher || - plugin_dispatchers_.size() >= std::numeric_limits::max()) { - return 0; - } - - uint32 id = 0; - do { - // Although it is unlikely, make sure that we won't cause any trouble - // when the counter overflows. - id = next_plugin_dispatcher_id_++; - } while (id == 0 || - plugin_dispatchers_.find(id) != plugin_dispatchers_.end()); - plugin_dispatchers_[id] = plugin_dispatcher; - return id; -} - -void PpapiDispatcher::Unregister(uint32 plugin_dispatcher_id) { - plugin_dispatchers_.erase(plugin_dispatcher_id); -} - -IPC::Sender* PpapiDispatcher::GetBrowserSender() { - return this; -} - -std::string PpapiDispatcher::GetUILanguage() { - NOTIMPLEMENTED(); - return std::string(); -} - -void PpapiDispatcher::PreCacheFont(const void* logfontw) { - NOTIMPLEMENTED(); -} - -void PpapiDispatcher::SetActiveURL(const std::string& url) { - NOTIMPLEMENTED(); -} - -PP_Resource PpapiDispatcher::CreateBrowserFont( - ppapi::proxy::Connection connection, - PP_Instance instance, - const PP_BrowserFont_Trusted_Description& desc, - const ppapi::Preferences& prefs) { - NOTIMPLEMENTED(); - return 0; -} - -bool PpapiDispatcher::OnMessageReceived(const IPC::Message& msg) { - IPC_BEGIN_MESSAGE_MAP(PpapiDispatcher, msg) - IPC_MESSAGE_HANDLER(PpapiMsg_InitializeNaClDispatcher, - OnMsgInitializeNaClDispatcher) - // All other messages are simply forwarded to a PluginDispatcher. - IPC_MESSAGE_UNHANDLED(OnPluginDispatcherMessageReceived(msg)) - IPC_END_MESSAGE_MAP() - return true; -} - -void PpapiDispatcher::OnChannelError() { - exit(1); -} - -bool PpapiDispatcher::Send(IPC::Message* msg) { - return channel_->Send(msg); -} - -void PpapiDispatcher::OnMsgInitializeNaClDispatcher( - const ppapi::PpapiNaClPluginArgs& args) { - static bool command_line_and_logging_initialized = false; - if (command_line_and_logging_initialized) { - LOG(FATAL) << "InitializeNaClDispatcher must be called once per plugin."; - return; - } - - command_line_and_logging_initialized = true; - CommandLine::Init(0, NULL); - for (size_t i = 0; i < args.switch_names.size(); ++i) { - DCHECK(i < args.switch_values.size()); - CommandLine::ForCurrentProcess()->AppendSwitchASCII( - args.switch_names[i], args.switch_values[i]); - } - logging::LoggingSettings settings; - settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; - logging::InitLogging(settings); - SetPpapiKeepAliveThrottleFromCommandLine(); - - // Tell the process-global GetInterface which interfaces it can return to the - // plugin. - ppapi::proxy::InterfaceList::SetProcessGlobalPermissions( - args.permissions); - - int32_t error = ::PPP_InitializeModule( - 0 /* module */, - &ppapi::proxy::PluginDispatcher::GetBrowserInterface); - if (error) - ::exit(error); - - PluginDispatcher* dispatcher = - new PluginDispatcher(::PPP_GetInterface, args.permissions, - args.off_the_record); - // The channel handle's true name is not revealed here. - DCHECK_NE(g_nacl_ipc_renderer_fd, -1) - << "g_nacl_ipc_renderer_fd must be initialized before the plugin starts"; - IPC::ChannelHandle channel_handle( - "nacl", base::FileDescriptor(g_nacl_ipc_renderer_fd, false)); - if (!dispatcher->InitPluginWithChannel(this, base::kNullProcessId, - channel_handle, false)) { - delete dispatcher; - return; - } - // From here, the dispatcher will manage its own lifetime according to the - // lifetime of the attached channel. -} - -void PpapiDispatcher::OnPluginDispatcherMessageReceived( - const IPC::Message& msg) { - // The first parameter should be a plugin dispatcher ID. - PickleIterator iter(msg); - uint32 id = 0; - if (!msg.ReadUInt32(&iter, &id)) { - NOTREACHED(); - return; - } - std::map::iterator dispatcher = - plugin_dispatchers_.find(id); - if (dispatcher != plugin_dispatchers_.end()) - dispatcher->second->OnMessageReceived(msg); -} - -void PpapiDispatcher::SetPpapiKeepAliveThrottleFromCommandLine() { - unsigned keepalive_throttle_interval_milliseconds = 0; - if (base::StringToUint( - CommandLine::ForCurrentProcess()->GetSwitchValueASCII( - switches::kPpapiKeepAliveThrottle), - &keepalive_throttle_interval_milliseconds)) { - ppapi::proxy::PluginGlobals::Get()-> - set_keepalive_throttle_interval_milliseconds( - keepalive_throttle_interval_milliseconds); - } -} - -} // namespace - -void SetIPCFileDescriptors(int ipc_browser_fd, int ipc_renderer_fd) { - g_nacl_ipc_browser_fd = ipc_browser_fd; - g_nacl_ipc_renderer_fd = ipc_renderer_fd; -} - -void PpapiPluginRegisterThreadCreator( - const struct PP_ThreadFunctions* thread_functions) { -#if defined(__native_client__) - // TODO(hidehiko): The thread creation for the PPB_Audio is not yet - // implemented on non-SFI mode. Support this. Now, this function invocation - // is just ignored. - - // Initialize all classes that need to create threads that call back into - // user code. - ppapi::PPB_Audio_Shared::SetThreadFunctions(thread_functions); -#endif -} - -int PpapiPluginMain() { - // Though it isn't referenced here, we must instantiate an AtExitManager. - base::AtExitManager exit_manager; - base::MessageLoop loop; -#if defined(IPC_MESSAGE_LOG_ENABLED) - IPC::Logging::set_log_function_map(&g_log_function_mapping); -#endif - ppapi::proxy::PluginGlobals plugin_globals; - base::Thread io_thread("Chrome_NaClIOThread"); - base::Thread::Options options; - options.message_loop_type = base::MessageLoop::TYPE_IO; - io_thread.StartWithOptions(options); - -#if defined(__native_client__) - // Currently on non-SFI mode, we don't use SRPC server on plugin. - // TODO(hidehiko): Make sure this SRPC is actually used on SFI-mode. - - // Start up the SRPC server on another thread. Otherwise, when it blocks - // on an RPC, the PPAPI proxy will hang. Do this before we initialize the - // module and start the PPAPI proxy so that the NaCl plugin can continue - // loading the app. - static struct NaClSrpcHandlerDesc srpc_methods[] = { { NULL, NULL } }; - if (!NaClSrpcAcceptClientOnThread(srpc_methods)) { - return 1; - } -#endif - - PpapiDispatcher ppapi_dispatcher(io_thread.message_loop_proxy()); - plugin_globals.set_plugin_proxy_delegate(&ppapi_dispatcher); - - loop.Run(); - - return 0; -} diff --git a/ppapi/proxy/plugin_main_irt.h b/ppapi/proxy/plugin_main_irt.h deleted file mode 100644 index f9a24cb..0000000 --- a/ppapi/proxy/plugin_main_irt.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef PPAPI_PROXY_PLUGIN_MAIN_IRT_H_ -#define PPAPI_PROXY_PLUGIN_MAIN_IRT_H_ - -#include "ppapi/nacl_irt/irt_ppapi.h" -#include "ppapi/proxy/ppapi_proxy_export.h" - -#ifdef __cplusplus -extern "C" { -#endif - -// The entry point for the main thread of the PPAPI plugin process. -PPAPI_PROXY_EXPORT int PpapiPluginMain(void); - -PPAPI_PROXY_EXPORT void PpapiPluginRegisterThreadCreator( - const struct PP_ThreadFunctions* new_funcs); - -// Sets the IPC channels for the browser and the renderer by the given FD -// numbers. This will be used for non-SFI mode. Must be called before -// PpapiPluginMain is called. -PPAPI_PROXY_EXPORT void SetIPCFileDescriptors( - int browser_ipc_fd, int renderer_ipc_fd); - -#ifdef __cplusplus -} -#endif - -#endif // PPAPI_PROXY_PLUGIN_MAIN_IRT_H_ diff --git a/ppapi/proxy/plugin_main_nacl.cc b/ppapi/proxy/plugin_main_nacl.cc new file mode 100644 index 0000000..c84828d --- /dev/null +++ b/ppapi/proxy/plugin_main_nacl.cc @@ -0,0 +1,327 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include + +#include +#include + +#include "build/build_config.h" +// Need to include this before most other files because it defines +// IPC_MESSAGE_LOG_ENABLED. We need to use it to define +// IPC_MESSAGE_MACROS_LOG_ENABLED so ppapi_messages.h will generate the +// ViewMsgLog et al. functions. + +#include "base/command_line.h" +#include "base/memory/scoped_ptr.h" +#include "base/message_loop/message_loop.h" +#include "base/strings/string_number_conversions.h" +#include "base/synchronization/waitable_event.h" +#include "base/threading/thread.h" +#include "components/tracing/child_trace_message_filter.h" +#include "ipc/ipc_channel_handle.h" +#include "ipc/ipc_logging.h" +#include "ipc/ipc_message.h" +#include "native_client/src/public/chrome_main.h" +#include "native_client/src/shared/srpc/nacl_srpc.h" +#include "ppapi/c/ppp.h" +#include "ppapi/c/ppp_instance.h" +#include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h" +#include "ppapi/proxy/plugin_dispatcher.h" +#include "ppapi/proxy/plugin_globals.h" +#include "ppapi/proxy/plugin_message_filter.h" +#include "ppapi/proxy/plugin_proxy_delegate.h" +#include "ppapi/proxy/resource_reply_thread_registrar.h" +#include "ppapi/shared_impl/ppapi_switches.h" +#include "ppapi/shared_impl/ppb_audio_shared.h" + +#if defined(IPC_MESSAGE_LOG_ENABLED) +#include "base/containers/hash_tables.h" + +LogFunctionMap g_log_function_mapping; + +#define IPC_MESSAGE_MACROS_LOG_ENABLED +#define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \ + g_log_function_mapping[msg_id] = logger + +#endif +#include "ppapi/proxy/ppapi_messages.h" + +using ppapi::proxy::PluginDispatcher; +using ppapi::proxy::PluginGlobals; +using ppapi::proxy::PluginProxyDelegate; +using ppapi::proxy::ProxyChannel; +using ppapi::proxy::SerializedHandle; + +namespace { + +// This class manages communication between the plugin and the browser, and +// manages the PluginDispatcher instances for communication between the plugin +// and the renderer. +class PpapiDispatcher : public PluginDispatcher::PluginDelegate, + public PluginProxyDelegate, + public IPC::Listener, + public IPC::Sender { + public: + explicit PpapiDispatcher(scoped_refptr io_loop); + + // PluginDispatcher::PluginDelegate implementation. + virtual base::MessageLoopProxy* GetIPCMessageLoop() OVERRIDE; + virtual base::WaitableEvent* GetShutdownEvent() OVERRIDE; + virtual IPC::PlatformFileForTransit ShareHandleWithRemote( + base::PlatformFile handle, + base::ProcessId peer_pid, + bool should_close_source) OVERRIDE; + virtual std::set* GetGloballySeenInstanceIDSet() OVERRIDE; + virtual uint32 Register(PluginDispatcher* plugin_dispatcher) OVERRIDE; + virtual void Unregister(uint32 plugin_dispatcher_id) OVERRIDE; + + // PluginProxyDelegate implementation. + virtual IPC::Sender* GetBrowserSender() OVERRIDE; + virtual std::string GetUILanguage() OVERRIDE; + virtual void PreCacheFont(const void* logfontw) OVERRIDE; + virtual void SetActiveURL(const std::string& url) OVERRIDE; + virtual PP_Resource CreateBrowserFont( + ppapi::proxy::Connection connection, + PP_Instance instance, + const PP_BrowserFont_Trusted_Description& desc, + const ppapi::Preferences& prefs) OVERRIDE; + + // IPC::Listener implementation. + virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; + virtual void OnChannelError() OVERRIDE; + + // IPC::Sender implementation + virtual bool Send(IPC::Message* message) OVERRIDE; + + private: + void OnMsgInitializeNaClDispatcher(const ppapi::PpapiNaClPluginArgs& args); + void OnPluginDispatcherMessageReceived(const IPC::Message& msg); + + void SetPpapiKeepAliveThrottleFromCommandLine(); + + std::set instances_; + std::map plugin_dispatchers_; + uint32 next_plugin_dispatcher_id_; + scoped_refptr message_loop_; + base::WaitableEvent shutdown_event_; + scoped_ptr channel_; +}; + +PpapiDispatcher::PpapiDispatcher(scoped_refptr io_loop) + : next_plugin_dispatcher_id_(0), + message_loop_(io_loop), + shutdown_event_(true, false) { + // The first FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the + // browser. + IPC::ChannelHandle channel_handle( + "NaCl IPC", base::FileDescriptor(NACL_CHROME_DESC_BASE, false)); + + // Delay initializing the SyncChannel until after we add filters. This + // ensures that the filters won't miss any messages received by + // the channel. + channel_.reset(new IPC::SyncChannel( + this, GetIPCMessageLoop(), GetShutdownEvent())); + channel_->AddFilter(new ppapi::proxy::PluginMessageFilter( + NULL, PluginGlobals::Get()->resource_reply_thread_registrar())); + channel_->AddFilter( + new tracing::ChildTraceMessageFilter(message_loop_.get())); + channel_->Init(channel_handle, IPC::Channel::MODE_SERVER, true); +} + +base::MessageLoopProxy* PpapiDispatcher::GetIPCMessageLoop() { + return message_loop_.get(); +} + +base::WaitableEvent* PpapiDispatcher::GetShutdownEvent() { + return &shutdown_event_; +} + +IPC::PlatformFileForTransit PpapiDispatcher::ShareHandleWithRemote( + base::PlatformFile handle, + base::ProcessId peer_pid, + bool should_close_source) { + return IPC::InvalidPlatformFileForTransit(); +} + +std::set* PpapiDispatcher::GetGloballySeenInstanceIDSet() { + return &instances_; +} + +uint32 PpapiDispatcher::Register(PluginDispatcher* plugin_dispatcher) { + if (!plugin_dispatcher || + plugin_dispatchers_.size() >= std::numeric_limits::max()) { + return 0; + } + + uint32 id = 0; + do { + // Although it is unlikely, make sure that we won't cause any trouble + // when the counter overflows. + id = next_plugin_dispatcher_id_++; + } while (id == 0 || + plugin_dispatchers_.find(id) != plugin_dispatchers_.end()); + plugin_dispatchers_[id] = plugin_dispatcher; + return id; +} + +void PpapiDispatcher::Unregister(uint32 plugin_dispatcher_id) { + plugin_dispatchers_.erase(plugin_dispatcher_id); +} + +IPC::Sender* PpapiDispatcher::GetBrowserSender() { + return this; +} + +std::string PpapiDispatcher::GetUILanguage() { + NOTIMPLEMENTED(); + return std::string(); +} + +void PpapiDispatcher::PreCacheFont(const void* logfontw) { + NOTIMPLEMENTED(); +} + +void PpapiDispatcher::SetActiveURL(const std::string& url) { + NOTIMPLEMENTED(); +} + +PP_Resource PpapiDispatcher::CreateBrowserFont( + ppapi::proxy::Connection connection, + PP_Instance instance, + const PP_BrowserFont_Trusted_Description& desc, + const ppapi::Preferences& prefs) { + NOTIMPLEMENTED(); + return 0; +} + +bool PpapiDispatcher::OnMessageReceived(const IPC::Message& msg) { + IPC_BEGIN_MESSAGE_MAP(PpapiDispatcher, msg) + IPC_MESSAGE_HANDLER(PpapiMsg_InitializeNaClDispatcher, + OnMsgInitializeNaClDispatcher) + // All other messages are simply forwarded to a PluginDispatcher. + IPC_MESSAGE_UNHANDLED(OnPluginDispatcherMessageReceived(msg)) + IPC_END_MESSAGE_MAP() + return true; +} + +void PpapiDispatcher::OnChannelError() { + exit(1); +} + +bool PpapiDispatcher::Send(IPC::Message* msg) { + return channel_->Send(msg); +} + +void PpapiDispatcher::OnMsgInitializeNaClDispatcher( + const ppapi::PpapiNaClPluginArgs& args) { + static bool command_line_and_logging_initialized = false; + if (command_line_and_logging_initialized) { + LOG(FATAL) << "InitializeNaClDispatcher must be called once per plugin."; + return; + } + + command_line_and_logging_initialized = true; + CommandLine::Init(0, NULL); + for (size_t i = 0; i < args.switch_names.size(); ++i) { + DCHECK(i < args.switch_values.size()); + CommandLine::ForCurrentProcess()->AppendSwitchASCII( + args.switch_names[i], args.switch_values[i]); + } + logging::LoggingSettings settings; + settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; + logging::InitLogging(settings); + SetPpapiKeepAliveThrottleFromCommandLine(); + + // Tell the process-global GetInterface which interfaces it can return to the + // plugin. + ppapi::proxy::InterfaceList::SetProcessGlobalPermissions( + args.permissions); + + int32_t error = ::PPP_InitializeModule( + 0 /* module */, + &ppapi::proxy::PluginDispatcher::GetBrowserInterface); + if (error) + ::exit(error); + + PluginDispatcher* dispatcher = + new PluginDispatcher(::PPP_GetInterface, args.permissions, + args.off_the_record); + // The channel handle's true name is not revealed here. + // The second FD (based on NACL_CHROME_DESC_BASE) is the IPC channel to the + // renderer. + IPC::ChannelHandle channel_handle( + "nacl", base::FileDescriptor(NACL_CHROME_DESC_BASE + 1, false)); + if (!dispatcher->InitPluginWithChannel(this, base::kNullProcessId, + channel_handle, false)) { + delete dispatcher; + return; + } + // From here, the dispatcher will manage its own lifetime according to the + // lifetime of the attached channel. +} + +void PpapiDispatcher::OnPluginDispatcherMessageReceived( + const IPC::Message& msg) { + // The first parameter should be a plugin dispatcher ID. + PickleIterator iter(msg); + uint32 id = 0; + if (!msg.ReadUInt32(&iter, &id)) { + NOTREACHED(); + return; + } + std::map::iterator dispatcher = + plugin_dispatchers_.find(id); + if (dispatcher != plugin_dispatchers_.end()) + dispatcher->second->OnMessageReceived(msg); +} + +void PpapiDispatcher::SetPpapiKeepAliveThrottleFromCommandLine() { + unsigned keepalive_throttle_interval_milliseconds = 0; + if (base::StringToUint( + CommandLine::ForCurrentProcess()->GetSwitchValueASCII( + switches::kPpapiKeepAliveThrottle), + &keepalive_throttle_interval_milliseconds)) { + ppapi::proxy::PluginGlobals::Get()-> + set_keepalive_throttle_interval_milliseconds( + keepalive_throttle_interval_milliseconds); + } +} + +} // namespace + +void PpapiPluginRegisterThreadCreator( + const struct PP_ThreadFunctions* thread_functions) { + // Initialize all classes that need to create threads that call back into + // user code. + ppapi::PPB_Audio_Shared::SetThreadFunctions(thread_functions); +} + +int PpapiPluginMain() { + // Though it isn't referenced here, we must instantiate an AtExitManager. + base::AtExitManager exit_manager; + base::MessageLoop loop; + IPC::Logging::set_log_function_map(&g_log_function_mapping); + ppapi::proxy::PluginGlobals plugin_globals; + base::Thread io_thread("Chrome_NaClIOThread"); + base::Thread::Options options; + options.message_loop_type = base::MessageLoop::TYPE_IO; + io_thread.StartWithOptions(options); + + // Start up the SRPC server on another thread. Otherwise, when it blocks + // on an RPC, the PPAPI proxy will hang. Do this before we initialize the + // module and start the PPAPI proxy so that the NaCl plugin can continue + // loading the app. + static struct NaClSrpcHandlerDesc srpc_methods[] = { { NULL, NULL } }; + if (!NaClSrpcAcceptClientOnThread(srpc_methods)) { + return 1; + } + + PpapiDispatcher ppapi_dispatcher(io_thread.message_loop_proxy()); + plugin_globals.set_plugin_proxy_delegate(&ppapi_dispatcher); + + loop.Run(); + + return 0; +} -- cgit v1.1