1 // Copyright 2014 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.
5 #include "ppapi/nacl_irt/ppapi_dispatcher.h"
10 #include "build/build_config.h"
11 // Need to include this before most other files because it defines
12 // IPC_MESSAGE_LOG_ENABLED. We need to use it to define
13 // IPC_MESSAGE_MACROS_LOG_ENABLED so ppapi_messages.h will generate the
14 // ViewMsgLog et al. functions.
16 #include "base/command_line.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/message_loop/message_loop.h"
19 #include "base/synchronization/waitable_event.h"
20 #include "components/tracing/child_trace_message_filter.h"
21 #include "ipc/ipc_channel_handle.h"
22 #include "ipc/ipc_logging.h"
23 #include "ipc/ipc_message.h"
24 #include "ppapi/c/ppp.h"
25 #include "ppapi/c/ppp_instance.h"
26 #include "ppapi/nacl_irt/manifest_service.h"
27 #include "ppapi/nacl_irt/plugin_startup.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"
34 #if defined(IPC_MESSAGE_LOG_ENABLED)
35 #include "base/containers/hash_tables.h"
37 LogFunctionMap g_log_function_mapping
;
39 #define IPC_MESSAGE_MACROS_LOG_ENABLED
40 #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \
41 g_log_function_mapping[msg_id] = logger
44 #include "ppapi/proxy/ppapi_messages.h"
48 PpapiDispatcher::PpapiDispatcher(scoped_refptr
<base::MessageLoopProxy
> io_loop
,
49 base::WaitableEvent
* shutdown_event
,
52 : next_plugin_dispatcher_id_(0),
53 message_loop_(io_loop
),
54 shutdown_event_(shutdown_event
),
55 renderer_ipc_fd_(renderer_ipc_fd
) {
56 #if defined(IPC_MESSAGE_LOG_ENABLED)
57 IPC::Logging::set_log_function_map(&g_log_function_mapping
);
60 IPC::ChannelHandle
channel_handle(
61 "NaCl IPC", base::FileDescriptor(browser_ipc_fd
, false));
63 proxy::PluginGlobals
* globals
= proxy::PluginGlobals::Get();
64 // Delay initializing the SyncChannel until after we add filters. This
65 // ensures that the filters won't miss any messages received by
68 IPC::SyncChannel::Create(this, GetIPCMessageLoop(), GetShutdownEvent());
69 scoped_refptr
<ppapi::proxy::PluginMessageFilter
> plugin_filter(
70 new ppapi::proxy::PluginMessageFilter(
71 NULL
, globals
->resource_reply_thread_registrar()));
72 channel_
->AddFilter(plugin_filter
.get());
73 globals
->RegisterResourceMessageFilters(plugin_filter
.get());
76 new tracing::ChildTraceMessageFilter(message_loop_
.get()));
77 channel_
->Init(channel_handle
, IPC::Channel::MODE_SERVER
, true);
80 base::MessageLoopProxy
* PpapiDispatcher::GetIPCMessageLoop() {
81 return message_loop_
.get();
84 base::WaitableEvent
* PpapiDispatcher::GetShutdownEvent() {
85 return shutdown_event_
;
88 IPC::PlatformFileForTransit
PpapiDispatcher::ShareHandleWithRemote(
89 base::PlatformFile handle
,
90 base::ProcessId peer_pid
,
91 bool should_close_source
) {
92 return IPC::InvalidPlatformFileForTransit();
95 std::set
<PP_Instance
>* PpapiDispatcher::GetGloballySeenInstanceIDSet() {
99 uint32
PpapiDispatcher::Register(proxy::PluginDispatcher
* plugin_dispatcher
) {
100 if (!plugin_dispatcher
||
101 plugin_dispatchers_
.size() >= std::numeric_limits
<uint32
>::max()) {
107 // Although it is unlikely, make sure that we won't cause any trouble
108 // when the counter overflows.
109 id
= next_plugin_dispatcher_id_
++;
111 plugin_dispatchers_
.find(id
) != plugin_dispatchers_
.end());
112 plugin_dispatchers_
[id
] = plugin_dispatcher
;
116 void PpapiDispatcher::Unregister(uint32 plugin_dispatcher_id
) {
117 plugin_dispatchers_
.erase(plugin_dispatcher_id
);
120 IPC::Sender
* PpapiDispatcher::GetBrowserSender() {
124 std::string
PpapiDispatcher::GetUILanguage() {
126 return std::string();
129 void PpapiDispatcher::PreCacheFont(const void* logfontw
) {
133 void PpapiDispatcher::SetActiveURL(const std::string
& url
) {
137 PP_Resource
PpapiDispatcher::CreateBrowserFont(
138 proxy::Connection connection
,
139 PP_Instance instance
,
140 const PP_BrowserFont_Trusted_Description
& desc
,
141 const Preferences
& prefs
) {
146 bool PpapiDispatcher::OnMessageReceived(const IPC::Message
& msg
) {
147 IPC_BEGIN_MESSAGE_MAP(PpapiDispatcher
, msg
)
148 IPC_MESSAGE_HANDLER(PpapiMsg_InitializeNaClDispatcher
,
149 OnMsgInitializeNaClDispatcher
)
150 // All other messages are simply forwarded to a PluginDispatcher.
151 IPC_MESSAGE_UNHANDLED(OnPluginDispatcherMessageReceived(msg
))
152 IPC_END_MESSAGE_MAP()
156 void PpapiDispatcher::OnChannelError() {
160 bool PpapiDispatcher::Send(IPC::Message
* msg
) {
161 return channel_
->Send(msg
);
164 void PpapiDispatcher::OnMsgInitializeNaClDispatcher(
165 const PpapiNaClPluginArgs
& args
) {
166 static bool command_line_and_logging_initialized
= false;
167 if (command_line_and_logging_initialized
) {
168 LOG(FATAL
) << "InitializeNaClDispatcher must be called once per plugin.";
172 command_line_and_logging_initialized
= true;
173 base::CommandLine::Init(0, NULL
);
174 for (size_t i
= 0; i
< args
.switch_names
.size(); ++i
) {
175 DCHECK(i
< args
.switch_values
.size());
176 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
177 args
.switch_names
[i
], args
.switch_values
[i
]);
179 logging::LoggingSettings settings
;
180 settings
.logging_dest
= logging::LOG_TO_SYSTEM_DEBUG_LOG
;
181 logging::InitLogging(settings
);
183 proxy::PluginGlobals::Get()->set_keepalive_throttle_interval_milliseconds(
184 args
.keepalive_throttle_interval_milliseconds
);
186 // Tell the process-global GetInterface which interfaces it can return to the
188 proxy::InterfaceList::SetProcessGlobalPermissions(args
.permissions
);
190 int32_t error
= ::PPP_InitializeModule(
192 &proxy::PluginDispatcher::GetBrowserInterface
);
196 proxy::PluginDispatcher
* dispatcher
=
197 new proxy::PluginDispatcher(::PPP_GetInterface
, args
.permissions
,
198 args
.off_the_record
);
199 IPC::ChannelHandle
channel_handle(
201 base::FileDescriptor(renderer_ipc_fd_
, false));
202 if (!dispatcher
->InitPluginWithChannel(this, base::kNullProcessId
,
203 channel_handle
, false)) {
207 // From here, the dispatcher will manage its own lifetime according to the
208 // lifetime of the attached channel.
210 // Notify the renderer process, if necessary.
211 ManifestService
* manifest_service
= GetManifestService();
212 if (manifest_service
)
213 manifest_service
->StartupInitializationComplete();
216 void PpapiDispatcher::OnPluginDispatcherMessageReceived(
217 const IPC::Message
& msg
) {
218 // The first parameter should be a plugin dispatcher ID.
219 PickleIterator
iter(msg
);
221 if (!iter
.ReadUInt32(&id
)) {
225 std::map
<uint32
, proxy::PluginDispatcher
*>::iterator dispatcher
=
226 plugin_dispatchers_
.find(id
);
227 if (dispatcher
!= plugin_dispatchers_
.end())
228 dispatcher
->second
->OnMessageReceived(msg
);