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.
5 #include "ppapi/proxy/plugin_dispatcher.h"
9 #include "base/compiler_specific.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/metrics/histogram_macros.h"
13 #include "base/trace_event/trace_event.h"
14 #include "ipc/ipc_message.h"
15 #include "ipc/ipc_sync_channel.h"
16 #include "ipc/ipc_sync_message_filter.h"
17 #include "ppapi/c/pp_errors.h"
18 #include "ppapi/c/ppp_instance.h"
19 #include "ppapi/proxy/flash_clipboard_resource.h"
20 #include "ppapi/proxy/flash_file_resource.h"
21 #include "ppapi/proxy/flash_resource.h"
22 #include "ppapi/proxy/gamepad_resource.h"
23 #include "ppapi/proxy/interface_list.h"
24 #include "ppapi/proxy/interface_proxy.h"
25 #include "ppapi/proxy/plugin_globals.h"
26 #include "ppapi/proxy/plugin_message_filter.h"
27 #include "ppapi/proxy/plugin_resource_tracker.h"
28 #include "ppapi/proxy/plugin_var_serialization_rules.h"
29 #include "ppapi/proxy/ppapi_messages.h"
30 #include "ppapi/proxy/ppb_instance_proxy.h"
31 #include "ppapi/proxy/ppp_class_proxy.h"
32 #include "ppapi/proxy/resource_creation_proxy.h"
33 #include "ppapi/proxy/resource_reply_thread_registrar.h"
34 #include "ppapi/shared_impl/ppapi_globals.h"
35 #include "ppapi/shared_impl/proxy_lock.h"
36 #include "ppapi/shared_impl/resource.h"
38 #if defined(OS_POSIX) && !defined(OS_NACL)
39 #include "ipc/ipc_channel_posix.h"
47 typedef std::map
<PP_Instance
, PluginDispatcher
*> InstanceToDispatcherMap
;
48 InstanceToDispatcherMap
* g_instance_to_dispatcher
= NULL
;
50 typedef std::set
<PluginDispatcher
*> DispatcherSet
;
51 DispatcherSet
* g_live_dispatchers
= NULL
;
55 InstanceData::InstanceData()
56 : is_request_surrounding_text_pending(false),
57 should_do_request_surrounding_text(false) {
60 InstanceData::~InstanceData() {
61 // Run any pending mouse lock callback to prevent leaks.
62 if (mouse_lock_callback
.get())
63 mouse_lock_callback
->Abort();
66 InstanceData::FlushInfo::FlushInfo()
67 : flush_pending(false),
71 InstanceData::FlushInfo::~FlushInfo() {
74 PluginDispatcher::PluginDispatcher(PP_GetInterface_Func get_interface
,
75 const PpapiPermissions
& permissions
,
77 : Dispatcher(get_interface
, permissions
),
78 plugin_delegate_(NULL
),
79 received_preferences_(false),
80 plugin_dispatcher_id_(0),
81 incognito_(incognito
) {
82 SetSerializationRules(new PluginVarSerializationRules(AsWeakPtr()));
84 if (!g_live_dispatchers
)
85 g_live_dispatchers
= new DispatcherSet
;
86 g_live_dispatchers
->insert(this);
89 PluginDispatcher::~PluginDispatcher() {
90 PluginGlobals::Get()->plugin_var_tracker()->DidDeleteDispatcher(this);
93 plugin_delegate_
->Unregister(plugin_dispatcher_id_
);
95 g_live_dispatchers
->erase(this);
96 if (g_live_dispatchers
->empty()) {
97 delete g_live_dispatchers
;
98 g_live_dispatchers
= NULL
;
103 PluginDispatcher
* PluginDispatcher::GetForInstance(PP_Instance instance
) {
104 if (!g_instance_to_dispatcher
)
106 InstanceToDispatcherMap::iterator found
= g_instance_to_dispatcher
->find(
108 if (found
== g_instance_to_dispatcher
->end())
110 return found
->second
;
114 PluginDispatcher
* PluginDispatcher::GetForResource(const Resource
* resource
) {
115 return GetForInstance(resource
->pp_instance());
119 const void* PluginDispatcher::GetBrowserInterface(const char* interface_name
) {
120 // CAUTION: This function is called directly from the plugin, but we *don't*
121 // lock the ProxyLock to avoid excessive locking from C++ wrappers.
122 return InterfaceList::GetInstance()->GetInterfaceForPPB(interface_name
);
126 void PluginDispatcher::LogWithSource(PP_Instance instance
,
128 const std::string
& source
,
129 const std::string
& value
) {
130 if (!g_live_dispatchers
|| !g_instance_to_dispatcher
)
134 InstanceToDispatcherMap::iterator found
=
135 g_instance_to_dispatcher
->find(instance
);
136 if (found
!= g_instance_to_dispatcher
->end()) {
137 // Send just to this specific dispatcher.
138 found
->second
->Send(new PpapiHostMsg_LogWithSource(
139 instance
, static_cast<int>(level
), source
, value
));
144 // Instance 0 or invalid, send to all dispatchers.
145 for (DispatcherSet::iterator i
= g_live_dispatchers
->begin();
146 i
!= g_live_dispatchers
->end(); ++i
) {
147 (*i
)->Send(new PpapiHostMsg_LogWithSource(
148 instance
, static_cast<int>(level
), source
, value
));
152 const void* PluginDispatcher::GetPluginInterface(
153 const std::string
& interface_name
) {
154 InterfaceMap::iterator found
= plugin_interfaces_
.find(interface_name
);
155 if (found
== plugin_interfaces_
.end()) {
156 const void* ret
= local_get_interface()(interface_name
.c_str());
157 plugin_interfaces_
.insert(std::make_pair(interface_name
, ret
));
160 return found
->second
;
163 bool PluginDispatcher::InitPluginWithChannel(
164 PluginDelegate
* delegate
,
165 base::ProcessId peer_pid
,
166 const IPC::ChannelHandle
& channel_handle
,
168 if (!Dispatcher::InitWithChannel(delegate
, peer_pid
, channel_handle
,
171 plugin_delegate_
= delegate
;
172 plugin_dispatcher_id_
= plugin_delegate_
->Register(this);
174 sync_filter_
= new IPC::SyncMessageFilter(delegate
->GetShutdownEvent());
175 channel()->AddFilter(sync_filter_
.get());
177 // The message filter will intercept and process certain messages directly
178 // on the I/O thread.
179 channel()->AddFilter(
180 new PluginMessageFilter(
181 delegate
->GetGloballySeenInstanceIDSet(),
182 PluginGlobals::Get()->resource_reply_thread_registrar()));
186 bool PluginDispatcher::IsPlugin() const {
190 bool PluginDispatcher::SendMessage(IPC::Message
* msg
) {
191 // Currently we need to choose between two different mechanisms for sending.
192 // On the main thread we use the regular dispatch Send() method, on another
193 // thread we use SyncMessageFilter.
194 if (PpapiGlobals::Get()->GetMainThreadMessageLoop()->BelongsToCurrentThread())
195 return Dispatcher::Send(msg
);
196 return sync_filter_
->Send(msg
);
199 bool PluginDispatcher::Send(IPC::Message
* msg
) {
200 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::Send",
201 "Class", IPC_MESSAGE_ID_CLASS(msg
->type()),
202 "Line", IPC_MESSAGE_ID_LINE(msg
->type()));
203 // We always want plugin->renderer messages to arrive in-order. If some sync
204 // and some async messages are sent in response to a synchronous
205 // renderer->plugin call, the sync reply will be processed before the async
206 // reply, and everything will be confused.
208 // Allowing all async messages to unblock the renderer means more reentrancy
209 // there but gives correct ordering.
211 // We don't want reply messages to unblock however, as they will potentially
212 // end up on the wrong queue - see crbug.com/122443
213 if (!msg
->is_reply())
214 msg
->set_unblock(true);
215 if (msg
->is_sync()) {
216 // Synchronous messages might be re-entrant, so we need to drop the lock.
217 ProxyAutoUnlock unlock
;
218 SCOPED_UMA_HISTOGRAM_TIMER("Plugin.PpapiSyncIPCTime");
219 return SendMessage(msg
);
221 return SendMessage(msg
);
224 bool PluginDispatcher::SendAndStayLocked(IPC::Message
* msg
) {
225 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::SendAndStayLocked",
226 "Class", IPC_MESSAGE_ID_CLASS(msg
->type()),
227 "Line", IPC_MESSAGE_ID_LINE(msg
->type()));
228 if (!msg
->is_reply())
229 msg
->set_unblock(true);
230 return SendMessage(msg
);
233 bool PluginDispatcher::OnMessageReceived(const IPC::Message
& msg
) {
234 // We need to grab the proxy lock to ensure that we don't collide with the
235 // plugin making pepper calls on a different thread.
237 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::OnMessageReceived",
238 "Class", IPC_MESSAGE_ID_CLASS(msg
.type()),
239 "Line", IPC_MESSAGE_ID_LINE(msg
.type()));
241 if (msg
.routing_id() == MSG_ROUTING_CONTROL
) {
242 // Handle some plugin-specific control messages.
244 IPC_BEGIN_MESSAGE_MAP(PluginDispatcher
, msg
)
245 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface
, OnMsgSupportsInterface
)
246 IPC_MESSAGE_HANDLER(PpapiMsg_SetPreferences
, OnMsgSetPreferences
)
247 IPC_MESSAGE_UNHANDLED(handled
= false);
248 IPC_END_MESSAGE_MAP()
252 return Dispatcher::OnMessageReceived(msg
);
255 void PluginDispatcher::OnChannelError() {
256 Dispatcher::OnChannelError();
258 // The renderer has crashed or exited. This channel and all instances
259 // associated with it are no longer valid.
260 ForceFreeAllInstances();
261 // TODO(brettw) free resources too!
265 void PluginDispatcher::DidCreateInstance(PP_Instance instance
) {
266 if (!g_instance_to_dispatcher
)
267 g_instance_to_dispatcher
= new InstanceToDispatcherMap
;
268 (*g_instance_to_dispatcher
)[instance
] = this;
269 instance_map_
.set(instance
, scoped_ptr
<InstanceData
>(new InstanceData()));
272 void PluginDispatcher::DidDestroyInstance(PP_Instance instance
) {
273 instance_map_
.erase(instance
);
275 if (g_instance_to_dispatcher
) {
276 InstanceToDispatcherMap::iterator found
= g_instance_to_dispatcher
->find(
278 if (found
!= g_instance_to_dispatcher
->end()) {
279 DCHECK(found
->second
== this);
280 g_instance_to_dispatcher
->erase(found
);
287 InstanceData
* PluginDispatcher::GetInstanceData(PP_Instance instance
) {
288 return instance_map_
.get(instance
);
291 thunk::PPB_Instance_API
* PluginDispatcher::GetInstanceAPI() {
292 return static_cast<PPB_Instance_Proxy
*>(
293 GetInterfaceProxy(API_ID_PPB_INSTANCE
));
296 thunk::ResourceCreationAPI
* PluginDispatcher::GetResourceCreationAPI() {
297 return static_cast<ResourceCreationProxy
*>(
298 GetInterfaceProxy(API_ID_RESOURCE_CREATION
));
301 void PluginDispatcher::ForceFreeAllInstances() {
302 if (!g_instance_to_dispatcher
)
305 // Iterating will remove each item from the map, so we need to make a copy
306 // to avoid things changing out from under is.
307 InstanceToDispatcherMap temp_map
= *g_instance_to_dispatcher
;
308 for (InstanceToDispatcherMap::iterator i
= temp_map
.begin();
309 i
!= temp_map
.end(); ++i
) {
310 if (i
->second
== this) {
311 // Synthesize an "instance destroyed" message, this will notify the
312 // plugin and also remove it from our list of tracked plugins.
313 PpapiMsg_PPPInstance_DidDestroy
msg(API_ID_PPP_INSTANCE
, i
->first
);
314 OnMessageReceived(msg
);
319 void PluginDispatcher::OnMsgSupportsInterface(
320 const std::string
& interface_name
,
322 *result
= !!GetPluginInterface(interface_name
);
324 // Do fallback for PPP_Instance. This is a hack here and if we have more
325 // cases like this it should be generalized. The PPP_Instance proxy always
326 // proxies the 1.1 interface, and then does fallback to 1.0 inside the
327 // plugin process (see PPP_Instance_Proxy). So here we return true for
328 // supporting the 1.1 interface if either 1.1 or 1.0 is supported.
329 if (!*result
&& interface_name
== PPP_INSTANCE_INTERFACE
)
330 *result
= !!GetPluginInterface(PPP_INSTANCE_INTERFACE_1_0
);
333 void PluginDispatcher::OnMsgSetPreferences(const Preferences
& prefs
) {
334 // The renderer may send us preferences more than once (currently this
335 // happens every time a new plugin instance is created). Since we don't have
336 // a way to signal to the plugin that the preferences have changed, changing
337 // the default fonts and such in the middle of a running plugin could be
338 // confusing to it. As a result, we never allow the preferences to be changed
339 // once they're set. The user will have to restart to get new font prefs
340 // propogated to plugins.
341 if (!received_preferences_
) {
342 received_preferences_
= true;
343 preferences_
= prefs
;