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.
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.
14 #include "base/command_line.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/threading/thread.h"
18 #include "components/tracing/child_trace_message_filter.h"
19 #include "ipc/ipc_channel_handle.h"
20 #include "ipc/ipc_logging.h"
21 #include "ipc/ipc_message.h"
22 #include "native_client/src/shared/srpc/nacl_srpc.h"
23 #include "native_client/src/untrusted/irt/irt_ppapi.h"
24 #include "ppapi/c/ppp.h"
25 #include "ppapi/c/ppp_instance.h"
26 #include "ppapi/native_client/src/shared/ppapi_proxy/ppruntime.h"
27 #include "ppapi/proxy/plugin_dispatcher.h"
28 #include "ppapi/proxy/plugin_globals.h"
29 #include "ppapi/proxy/plugin_proxy_delegate.h"
30 #include "ppapi/shared_impl/ppb_audio_shared.h"
32 #if defined(IPC_MESSAGE_LOG_ENABLED)
33 #include "base/containers/hash_tables.h"
35 LogFunctionMap g_log_function_mapping
;
37 #define IPC_MESSAGE_MACROS_LOG_ENABLED
38 #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \
39 g_log_function_mapping[msg_id] = logger
42 #include "ppapi/proxy/ppapi_messages.h"
44 // This must match up with NACL_CHROME_INITIAL_IPC_DESC,
45 // defined in sel_main_chrome.h
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
;
56 // This class manages communication between the plugin and the browser, and
57 // manages the PluginDispatcher instances for communication between the plugin
59 class PpapiDispatcher
: public ProxyChannel
,
60 public PluginDispatcher::PluginDelegate
,
61 public PluginProxyDelegate
{
63 explicit PpapiDispatcher(scoped_refptr
<base::MessageLoopProxy
> io_loop
);
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
;
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
,
84 const PP_BrowserFont_Trusted_Description
& desc
,
85 const ppapi::Preferences
& prefs
) OVERRIDE
;
87 // IPC::Listener implementation.
88 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
91 void OnMsgCreateNaClChannel(int renderer_id
,
92 const ppapi::PpapiNaClChannelArgs
& args
,
93 SerializedHandle handle
);
94 void OnMsgResourceReply(
95 const ppapi::proxy::ResourceMessageReplyParams
& reply_params
,
96 const IPC::Message
& nested_msg
);
97 void OnPluginDispatcherMessageReceived(const IPC::Message
& msg
);
99 std::set
<PP_Instance
> instances_
;
100 std::map
<uint32
, PluginDispatcher
*> plugin_dispatchers_
;
101 uint32 next_plugin_dispatcher_id_
;
102 scoped_refptr
<base::MessageLoopProxy
> message_loop_
;
103 base::WaitableEvent shutdown_event_
;
106 PpapiDispatcher::PpapiDispatcher(scoped_refptr
<base::MessageLoopProxy
> io_loop
)
107 : next_plugin_dispatcher_id_(0),
108 message_loop_(io_loop
),
109 shutdown_event_(true, false) {
110 IPC::ChannelHandle
channel_handle(
111 "NaCl IPC", base::FileDescriptor(NACL_IPC_FD
, false));
112 // We don't have/need a PID since handle sharing happens outside of the
114 InitWithChannel(this, base::kNullProcessId
, channel_handle
,
115 false); // Channel is server.
116 channel()->AddFilter(
117 new tracing::ChildTraceMessageFilter(message_loop_
.get()));
120 base::MessageLoopProxy
* PpapiDispatcher::GetIPCMessageLoop() {
121 return message_loop_
.get();
124 base::WaitableEvent
* PpapiDispatcher::GetShutdownEvent() {
125 return &shutdown_event_
;
128 IPC::PlatformFileForTransit
PpapiDispatcher::ShareHandleWithRemote(
129 base::PlatformFile handle
,
130 base::ProcessId peer_pid
,
131 bool should_close_source
) {
132 return IPC::InvalidPlatformFileForTransit();
135 std::set
<PP_Instance
>* PpapiDispatcher::GetGloballySeenInstanceIDSet() {
139 uint32
PpapiDispatcher::Register(PluginDispatcher
* plugin_dispatcher
) {
140 if (!plugin_dispatcher
||
141 plugin_dispatchers_
.size() >= std::numeric_limits
<uint32
>::max()) {
147 // Although it is unlikely, make sure that we won't cause any trouble
148 // when the counter overflows.
149 id
= next_plugin_dispatcher_id_
++;
151 plugin_dispatchers_
.find(id
) != plugin_dispatchers_
.end());
152 plugin_dispatchers_
[id
] = plugin_dispatcher
;
156 void PpapiDispatcher::Unregister(uint32 plugin_dispatcher_id
) {
157 plugin_dispatchers_
.erase(plugin_dispatcher_id
);
160 IPC::Sender
* PpapiDispatcher::GetBrowserSender() {
164 std::string
PpapiDispatcher::GetUILanguage() {
166 return std::string();
169 void PpapiDispatcher::PreCacheFont(const void* logfontw
) {
173 void PpapiDispatcher::SetActiveURL(const std::string
& url
) {
177 PP_Resource
PpapiDispatcher::CreateBrowserFont(
178 ppapi::proxy::Connection connection
,
179 PP_Instance instance
,
180 const PP_BrowserFont_Trusted_Description
& desc
,
181 const ppapi::Preferences
& prefs
) {
186 bool PpapiDispatcher::OnMessageReceived(const IPC::Message
& msg
) {
187 IPC_BEGIN_MESSAGE_MAP(PpapiDispatcher
, msg
)
188 IPC_MESSAGE_HANDLER(PpapiMsg_CreateNaClChannel
, OnMsgCreateNaClChannel
)
189 IPC_MESSAGE_HANDLER(PpapiPluginMsg_ResourceReply
, OnMsgResourceReply
)
190 // All other messages are simply forwarded to a PluginDispatcher.
191 IPC_MESSAGE_UNHANDLED(OnPluginDispatcherMessageReceived(msg
))
192 IPC_END_MESSAGE_MAP()
196 void PpapiDispatcher::OnMsgCreateNaClChannel(
198 const ppapi::PpapiNaClChannelArgs
& args
,
199 SerializedHandle handle
) {
200 static bool command_line_and_logging_initialized
= false;
201 if (!command_line_and_logging_initialized
) {
202 CommandLine::Init(0, NULL
);
203 for (size_t i
= 0; i
< args
.switch_names
.size(); ++i
) {
204 DCHECK(i
< args
.switch_values
.size());
205 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
206 args
.switch_names
[i
], args
.switch_values
[i
]);
208 logging::LoggingSettings settings
;
209 settings
.logging_dest
= logging::LOG_TO_SYSTEM_DEBUG_LOG
;
210 logging::InitLogging(settings
);
211 command_line_and_logging_initialized
= true;
213 // Tell the process-global GetInterface which interfaces it can return to the
215 ppapi::proxy::InterfaceList::SetProcessGlobalPermissions(
218 PluginDispatcher
* dispatcher
=
219 new PluginDispatcher(::PPP_GetInterface
, args
.permissions
,
220 args
.off_the_record
);
221 // The channel handle's true name is not revealed here.
222 IPC::ChannelHandle
channel_handle("nacl", handle
.descriptor());
223 if (!dispatcher
->InitPluginWithChannel(this, base::kNullProcessId
,
224 channel_handle
, false)) {
228 // From here, the dispatcher will manage its own lifetime according to the
229 // lifetime of the attached channel.
232 void PpapiDispatcher::OnMsgResourceReply(
233 const ppapi::proxy::ResourceMessageReplyParams
& reply_params
,
234 const IPC::Message
& nested_msg
) {
235 ppapi::proxy::PluginDispatcher::DispatchResourceReply(reply_params
,
239 void PpapiDispatcher::OnPluginDispatcherMessageReceived(
240 const IPC::Message
& msg
) {
241 // The first parameter should be a plugin dispatcher ID.
242 PickleIterator
iter(msg
);
244 if (!msg
.ReadUInt32(&iter
, &id
)) {
248 std::map
<uint32
, ppapi::proxy::PluginDispatcher
*>::iterator dispatcher
=
249 plugin_dispatchers_
.find(id
);
250 if (dispatcher
!= plugin_dispatchers_
.end())
251 dispatcher
->second
->OnMessageReceived(msg
);
256 void PpapiPluginRegisterThreadCreator(
257 const struct PP_ThreadFunctions
* thread_functions
) {
258 // Initialize all classes that need to create threads that call back into
260 ppapi::PPB_Audio_Shared::SetThreadFunctions(thread_functions
);
263 int PpapiPluginMain() {
264 // Though it isn't referenced here, we must instantiate an AtExitManager.
265 base::AtExitManager exit_manager
;
266 base::MessageLoop loop
;
267 IPC::Logging::set_log_function_map(&g_log_function_mapping
);
268 ppapi::proxy::PluginGlobals plugin_globals
;
269 base::Thread
io_thread("Chrome_NaClIOThread");
270 base::Thread::Options options
;
271 options
.message_loop_type
= base::MessageLoop::TYPE_IO
;
272 io_thread
.StartWithOptions(options
);
274 // Start up the SRPC server on another thread. Otherwise, when it blocks
275 // on an RPC, the PPAPI proxy will hang. Do this before we initialize the
276 // module and start the PPAPI proxy so that the NaCl plugin can continue
278 static struct NaClSrpcHandlerDesc srpc_methods
[] = { { NULL
, NULL
} };
279 if (!NaClSrpcAcceptClientOnThread(srpc_methods
)) {
283 int32_t error
= ::PPP_InitializeModule(
285 &ppapi::proxy::PluginDispatcher::GetBrowserInterface
);
286 // TODO(dmichael): Handle other error conditions, like failure to connect?
290 PpapiDispatcher
ppapi_dispatcher(io_thread
.message_loop_proxy());
291 plugin_globals
.set_plugin_proxy_delegate(&ppapi_dispatcher
);