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 // Delay initializing the SyncChannel until after we add filters. This
64 // ensures that the filters won't miss any messages received by
67 IPC::SyncChannel::Create(this, GetIPCMessageLoop(), GetShutdownEvent());
68 channel_
->AddFilter(new proxy::PluginMessageFilter(
69 NULL
, proxy::PluginGlobals::Get()->resource_reply_thread_registrar()));
71 new tracing::ChildTraceMessageFilter(message_loop_
.get()));
72 channel_
->Init(channel_handle
, IPC::Channel::MODE_SERVER
, true);
75 base::MessageLoopProxy
* PpapiDispatcher::GetIPCMessageLoop() {
76 return message_loop_
.get();
79 base::WaitableEvent
* PpapiDispatcher::GetShutdownEvent() {
80 return shutdown_event_
;
83 IPC::PlatformFileForTransit
PpapiDispatcher::ShareHandleWithRemote(
84 base::PlatformFile handle
,
85 base::ProcessId peer_pid
,
86 bool should_close_source
) {
87 return IPC::InvalidPlatformFileForTransit();
90 std::set
<PP_Instance
>* PpapiDispatcher::GetGloballySeenInstanceIDSet() {
94 uint32
PpapiDispatcher::Register(proxy::PluginDispatcher
* plugin_dispatcher
) {
95 if (!plugin_dispatcher
||
96 plugin_dispatchers_
.size() >= std::numeric_limits
<uint32
>::max()) {
102 // Although it is unlikely, make sure that we won't cause any trouble
103 // when the counter overflows.
104 id
= next_plugin_dispatcher_id_
++;
106 plugin_dispatchers_
.find(id
) != plugin_dispatchers_
.end());
107 plugin_dispatchers_
[id
] = plugin_dispatcher
;
111 void PpapiDispatcher::Unregister(uint32 plugin_dispatcher_id
) {
112 plugin_dispatchers_
.erase(plugin_dispatcher_id
);
115 IPC::Sender
* PpapiDispatcher::GetBrowserSender() {
119 std::string
PpapiDispatcher::GetUILanguage() {
121 return std::string();
124 void PpapiDispatcher::PreCacheFont(const void* logfontw
) {
128 void PpapiDispatcher::SetActiveURL(const std::string
& url
) {
132 PP_Resource
PpapiDispatcher::CreateBrowserFont(
133 proxy::Connection connection
,
134 PP_Instance instance
,
135 const PP_BrowserFont_Trusted_Description
& desc
,
136 const Preferences
& prefs
) {
141 bool PpapiDispatcher::OnMessageReceived(const IPC::Message
& msg
) {
142 IPC_BEGIN_MESSAGE_MAP(PpapiDispatcher
, msg
)
143 IPC_MESSAGE_HANDLER(PpapiMsg_InitializeNaClDispatcher
,
144 OnMsgInitializeNaClDispatcher
)
145 // All other messages are simply forwarded to a PluginDispatcher.
146 IPC_MESSAGE_UNHANDLED(OnPluginDispatcherMessageReceived(msg
))
147 IPC_END_MESSAGE_MAP()
151 void PpapiDispatcher::OnChannelError() {
155 bool PpapiDispatcher::Send(IPC::Message
* msg
) {
156 return channel_
->Send(msg
);
159 void PpapiDispatcher::OnMsgInitializeNaClDispatcher(
160 const PpapiNaClPluginArgs
& args
) {
161 static bool command_line_and_logging_initialized
= false;
162 if (command_line_and_logging_initialized
) {
163 LOG(FATAL
) << "InitializeNaClDispatcher must be called once per plugin.";
167 command_line_and_logging_initialized
= true;
168 base::CommandLine::Init(0, NULL
);
169 for (size_t i
= 0; i
< args
.switch_names
.size(); ++i
) {
170 DCHECK(i
< args
.switch_values
.size());
171 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
172 args
.switch_names
[i
], args
.switch_values
[i
]);
174 logging::LoggingSettings settings
;
175 settings
.logging_dest
= logging::LOG_TO_SYSTEM_DEBUG_LOG
;
176 logging::InitLogging(settings
);
178 proxy::PluginGlobals::Get()->set_keepalive_throttle_interval_milliseconds(
179 args
.keepalive_throttle_interval_milliseconds
);
181 // Tell the process-global GetInterface which interfaces it can return to the
183 proxy::InterfaceList::SetProcessGlobalPermissions(args
.permissions
);
185 int32_t error
= ::PPP_InitializeModule(
187 &proxy::PluginDispatcher::GetBrowserInterface
);
191 proxy::PluginDispatcher
* dispatcher
=
192 new proxy::PluginDispatcher(::PPP_GetInterface
, args
.permissions
,
193 args
.off_the_record
);
194 IPC::ChannelHandle
channel_handle(
196 base::FileDescriptor(renderer_ipc_fd_
, false));
197 if (!dispatcher
->InitPluginWithChannel(this, base::kNullProcessId
,
198 channel_handle
, false)) {
202 // From here, the dispatcher will manage its own lifetime according to the
203 // lifetime of the attached channel.
205 // Notify the renderer process, if necessary.
206 ManifestService
* manifest_service
= GetManifestService();
207 if (manifest_service
)
208 manifest_service
->StartupInitializationComplete();
211 void PpapiDispatcher::OnPluginDispatcherMessageReceived(
212 const IPC::Message
& msg
) {
213 // The first parameter should be a plugin dispatcher ID.
214 PickleIterator
iter(msg
);
216 if (!iter
.ReadUInt32(&id
)) {
220 std::map
<uint32
, proxy::PluginDispatcher
*>::iterator dispatcher
=
221 plugin_dispatchers_
.find(id
);
222 if (dispatcher
!= plugin_dispatchers_
.end())
223 dispatcher
->second
->OnMessageReceived(msg
);