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/message_loop.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "base/threading/thread.h"
17 #include "content/components/tracing/child_trace_message_filter.h"
18 #include "ipc/ipc_channel_handle.h"
19 #include "ipc/ipc_logging.h"
20 #include "ipc/ipc_message.h"
21 #include "native_client/src/shared/ppapi_proxy/ppruntime.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/proxy/plugin_dispatcher.h"
27 #include "ppapi/proxy/plugin_globals.h"
28 #include "ppapi/proxy/plugin_proxy_delegate.h"
29 #include "ppapi/shared_impl/ppb_audio_shared.h"
31 #if defined(IPC_MESSAGE_LOG_ENABLED)
32 #include "base/hash_tables.h"
34 LogFunctionMap g_log_function_mapping
;
36 #define IPC_MESSAGE_MACROS_LOG_ENABLED
37 #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \
38 g_log_function_mapping[msg_id] = logger
41 #include "ppapi/proxy/ppapi_messages.h"
43 // This must match up with NACL_CHROME_INITIAL_IPC_DESC,
44 // defined in sel_main_chrome.h
47 using ppapi::proxy::PluginDispatcher
;
48 using ppapi::proxy::PluginGlobals
;
49 using ppapi::proxy::PluginProxyDelegate
;
50 using ppapi::proxy::ProxyChannel
;
51 using ppapi::proxy::SerializedHandle
;
55 // This class manages communication between the plugin and the browser, and
56 // manages the PluginDispatcher instances for communication between the plugin
58 class PpapiDispatcher
: public ProxyChannel
,
59 public PluginDispatcher::PluginDelegate
,
60 public PluginProxyDelegate
{
62 explicit PpapiDispatcher(scoped_refptr
<base::MessageLoopProxy
> io_loop
);
64 // PluginDispatcher::PluginDelegate implementation.
65 virtual base::MessageLoopProxy
* GetIPCMessageLoop() OVERRIDE
;
66 virtual base::WaitableEvent
* GetShutdownEvent() OVERRIDE
;
67 virtual IPC::PlatformFileForTransit
ShareHandleWithRemote(
68 base::PlatformFile handle
,
69 const IPC::SyncChannel
& channel
,
70 bool should_close_source
) OVERRIDE
;
71 virtual std::set
<PP_Instance
>* GetGloballySeenInstanceIDSet() OVERRIDE
;
72 virtual uint32
Register(PluginDispatcher
* plugin_dispatcher
) OVERRIDE
;
73 virtual void Unregister(uint32 plugin_dispatcher_id
) OVERRIDE
;
75 // PluginProxyDelegate implementation.
76 virtual IPC::Sender
* GetBrowserSender() OVERRIDE
;
77 virtual std::string
GetUILanguage() OVERRIDE
;
78 virtual void PreCacheFont(const void* logfontw
) OVERRIDE
;
79 virtual void SetActiveURL(const std::string
& url
) OVERRIDE
;
81 // IPC::Listener implementation.
82 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
85 void OnMsgCreateNaClChannel(int renderer_id
,
86 const ppapi::PpapiPermissions
& permissions
,
88 SerializedHandle handle
);
89 void OnMsgResourceReply(
90 const ppapi::proxy::ResourceMessageReplyParams
& reply_params
,
91 const IPC::Message
& nested_msg
);
92 void OnPluginDispatcherMessageReceived(const IPC::Message
& msg
);
94 std::set
<PP_Instance
> instances_
;
95 std::map
<uint32
, PluginDispatcher
*> plugin_dispatchers_
;
96 uint32 next_plugin_dispatcher_id_
;
97 scoped_refptr
<base::MessageLoopProxy
> message_loop_
;
98 base::WaitableEvent shutdown_event_
;
101 PpapiDispatcher::PpapiDispatcher(scoped_refptr
<base::MessageLoopProxy
> io_loop
)
102 : next_plugin_dispatcher_id_(0),
103 message_loop_(io_loop
),
104 shutdown_event_(true, false) {
105 IPC::ChannelHandle
channel_handle(
106 "NaCl IPC", base::FileDescriptor(NACL_IPC_FD
, false));
107 InitWithChannel(this, channel_handle
, false); // Channel is server.
108 channel()->AddFilter(
109 new content::ChildTraceMessageFilter(message_loop_
));
112 base::MessageLoopProxy
* PpapiDispatcher::GetIPCMessageLoop() {
113 return message_loop_
.get();
116 base::WaitableEvent
* PpapiDispatcher::GetShutdownEvent() {
117 return &shutdown_event_
;
120 IPC::PlatformFileForTransit
PpapiDispatcher::ShareHandleWithRemote(
121 base::PlatformFile handle
,
122 const IPC::SyncChannel
& channel
,
123 bool should_close_source
) {
124 return IPC::InvalidPlatformFileForTransit();
127 std::set
<PP_Instance
>* PpapiDispatcher::GetGloballySeenInstanceIDSet() {
131 uint32
PpapiDispatcher::Register(PluginDispatcher
* plugin_dispatcher
) {
132 if (!plugin_dispatcher
||
133 plugin_dispatchers_
.size() >= std::numeric_limits
<uint32
>::max()) {
139 // Although it is unlikely, make sure that we won't cause any trouble
140 // when the counter overflows.
141 id
= next_plugin_dispatcher_id_
++;
143 plugin_dispatchers_
.find(id
) != plugin_dispatchers_
.end());
144 plugin_dispatchers_
[id
] = plugin_dispatcher
;
148 void PpapiDispatcher::Unregister(uint32 plugin_dispatcher_id
) {
149 plugin_dispatchers_
.erase(plugin_dispatcher_id
);
152 IPC::Sender
* PpapiDispatcher::GetBrowserSender() {
156 std::string
PpapiDispatcher::GetUILanguage() {
158 return std::string();
161 void PpapiDispatcher::PreCacheFont(const void* logfontw
) {
165 void PpapiDispatcher::SetActiveURL(const std::string
& url
) {
169 bool PpapiDispatcher::OnMessageReceived(const IPC::Message
& msg
) {
170 IPC_BEGIN_MESSAGE_MAP(PpapiDispatcher
, msg
)
171 IPC_MESSAGE_HANDLER(PpapiMsg_CreateNaClChannel
, OnMsgCreateNaClChannel
)
172 IPC_MESSAGE_HANDLER(PpapiPluginMsg_ResourceReply
, OnMsgResourceReply
)
173 // All other messages are simply forwarded to a PluginDispatcher.
174 IPC_MESSAGE_UNHANDLED(OnPluginDispatcherMessageReceived(msg
))
175 IPC_END_MESSAGE_MAP()
179 void PpapiDispatcher::OnMsgCreateNaClChannel(
181 const ppapi::PpapiPermissions
& permissions
,
183 SerializedHandle handle
) {
184 // Tell the process-global GetInterface which interfaces it can return to the
186 ppapi::proxy::InterfaceList::SetProcessGlobalPermissions(
189 PluginDispatcher
* dispatcher
=
190 new PluginDispatcher(::PPP_GetInterface
, permissions
, incognito
);
191 // The channel handle's true name is not revealed here.
192 IPC::ChannelHandle
channel_handle("nacl", handle
.descriptor());
193 if (!dispatcher
->InitPluginWithChannel(this, channel_handle
, false)) {
197 // From here, the dispatcher will manage its own lifetime according to the
198 // lifetime of the attached channel.
201 void PpapiDispatcher::OnMsgResourceReply(
202 const ppapi::proxy::ResourceMessageReplyParams
& reply_params
,
203 const IPC::Message
& nested_msg
) {
204 ppapi::proxy::PluginDispatcher::DispatchResourceReply(reply_params
,
208 void PpapiDispatcher::OnPluginDispatcherMessageReceived(
209 const IPC::Message
& msg
) {
210 // The first parameter should be a plugin dispatcher ID.
211 PickleIterator
iter(msg
);
213 if (!msg
.ReadUInt32(&iter
, &id
)) {
217 std::map
<uint32
, ppapi::proxy::PluginDispatcher
*>::iterator dispatcher
=
218 plugin_dispatchers_
.find(id
);
219 if (dispatcher
!= plugin_dispatchers_
.end())
220 dispatcher
->second
->OnMessageReceived(msg
);
225 void PpapiPluginRegisterThreadCreator(
226 const struct PP_ThreadFunctions
* thread_functions
) {
227 // Initialize all classes that need to create threads that call back into
229 ppapi::PPB_Audio_Shared::SetThreadFunctions(thread_functions
);
233 static int initialized
= 0;
237 if (!NaClSrpcModuleInit()) {
244 int PpapiPluginMain() {
247 // Though it isn't referenced here, we must instantiate an AtExitManager.
248 base::AtExitManager exit_manager
;
250 IPC::Logging::set_log_function_map(&g_log_function_mapping
);
251 ppapi::proxy::PluginGlobals plugin_globals
;
252 base::Thread
io_thread("Chrome_NaClIOThread");
253 base::Thread::Options options
;
254 options
.message_loop_type
= MessageLoop::TYPE_IO
;
255 io_thread
.StartWithOptions(options
);
257 // Start up the SRPC server on another thread. Otherwise, when it blocks
258 // on an RPC, the PPAPI proxy will hang. Do this before we initialize the
259 // module and start the PPAPI proxy so that the NaCl plugin can continue
261 static struct NaClSrpcHandlerDesc srpc_methods
[] = { { NULL
, NULL
} };
262 if (!NaClSrpcAcceptClientOnThread(srpc_methods
)) {
266 int32_t error
= ::PPP_InitializeModule(
268 &ppapi::proxy::PluginDispatcher::GetBrowserInterface
);
269 // TODO(dmichael): Handle other error conditions, like failure to connect?
273 PpapiDispatcher
ppapi_dispatcher(io_thread
.message_loop_proxy());
274 plugin_globals
.set_plugin_proxy_delegate(&ppapi_dispatcher
);
278 NaClSrpcModuleFini();