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.h"
12 #include "ipc/ipc_message.h"
13 #include "ipc/ipc_sync_channel.h"
14 #include "base/debug/trace_event.h"
15 #include "ppapi/c/pp_errors.h"
16 #include "ppapi/c/ppp_instance.h"
17 #include "ppapi/proxy/flash_resource.h"
18 #include "ppapi/proxy/flash_clipboard_resource.h"
19 #include "ppapi/proxy/flash_file_resource.h"
20 #include "ppapi/proxy/gamepad_resource.h"
21 #include "ppapi/proxy/interface_list.h"
22 #include "ppapi/proxy/interface_proxy.h"
23 #include "ppapi/proxy/plugin_message_filter.h"
24 #include "ppapi/proxy/plugin_resource_tracker.h"
25 #include "ppapi/proxy/plugin_var_serialization_rules.h"
26 #include "ppapi/proxy/ppapi_messages.h"
27 #include "ppapi/proxy/ppb_instance_proxy.h"
28 #include "ppapi/proxy/ppp_class_proxy.h"
29 #include "ppapi/proxy/resource_creation_proxy.h"
30 #include "ppapi/proxy/resource_message_params.h"
31 #include "ppapi/shared_impl/ppapi_globals.h"
32 #include "ppapi/shared_impl/proxy_lock.h"
33 #include "ppapi/shared_impl/resource.h"
35 #if defined(OS_POSIX) && !defined(OS_NACL)
36 #include "base/posix/eintr_wrapper.h"
37 #include "ipc/ipc_channel_posix.h"
45 typedef std::map
<PP_Instance
, PluginDispatcher
*> InstanceToDispatcherMap
;
46 InstanceToDispatcherMap
* g_instance_to_dispatcher
= NULL
;
48 typedef std::set
<PluginDispatcher
*> DispatcherSet
;
49 DispatcherSet
* g_live_dispatchers
= NULL
;
53 InstanceData::InstanceData()
54 : is_request_surrounding_text_pending(false),
55 should_do_request_surrounding_text(false) {
58 InstanceData::~InstanceData() {
59 // Run any pending mouse lock callback to prevent leaks.
60 if (mouse_lock_callback
)
61 mouse_lock_callback
->Abort();
64 PluginDispatcher::PluginDispatcher(PP_GetInterface_Func get_interface
,
65 const PpapiPermissions
& permissions
,
67 : Dispatcher(get_interface
, permissions
),
68 plugin_delegate_(NULL
),
69 received_preferences_(false),
70 plugin_dispatcher_id_(0),
71 incognito_(incognito
) {
72 SetSerializationRules(new PluginVarSerializationRules(AsWeakPtr()));
74 if (!g_live_dispatchers
)
75 g_live_dispatchers
= new DispatcherSet
;
76 g_live_dispatchers
->insert(this);
79 PluginDispatcher::~PluginDispatcher() {
81 plugin_delegate_
->Unregister(plugin_dispatcher_id_
);
83 g_live_dispatchers
->erase(this);
84 if (g_live_dispatchers
->empty()) {
85 delete g_live_dispatchers
;
86 g_live_dispatchers
= NULL
;
91 PluginDispatcher
* PluginDispatcher::GetForInstance(PP_Instance instance
) {
92 if (!g_instance_to_dispatcher
)
94 InstanceToDispatcherMap::iterator found
= g_instance_to_dispatcher
->find(
96 if (found
== g_instance_to_dispatcher
->end())
102 PluginDispatcher
* PluginDispatcher::GetForResource(const Resource
* resource
) {
103 return GetForInstance(resource
->pp_instance());
107 const void* PluginDispatcher::GetBrowserInterface(const char* interface_name
) {
108 if (!interface_name
) {
109 DLOG(WARNING
) << "|interface_name| is null. Did you forget to add "
110 "the |interface_name()| template function to the interface's C++ "
115 return InterfaceList::GetInstance()->GetInterfaceForPPB(interface_name
);
119 void PluginDispatcher::LogWithSource(PP_Instance instance
,
121 const std::string
& source
,
122 const std::string
& value
) {
123 if (!g_live_dispatchers
|| !g_instance_to_dispatcher
)
127 InstanceToDispatcherMap::iterator found
=
128 g_instance_to_dispatcher
->find(instance
);
129 if (found
!= g_instance_to_dispatcher
->end()) {
130 // Send just to this specific dispatcher.
131 found
->second
->Send(new PpapiHostMsg_LogWithSource(
132 instance
, static_cast<int>(level
), source
, value
));
137 // Instance 0 or invalid, send to all dispatchers.
138 for (DispatcherSet::iterator i
= g_live_dispatchers
->begin();
139 i
!= g_live_dispatchers
->end(); ++i
) {
140 (*i
)->Send(new PpapiHostMsg_LogWithSource(
141 instance
, static_cast<int>(level
), source
, value
));
145 const void* PluginDispatcher::GetPluginInterface(
146 const std::string
& interface_name
) {
147 InterfaceMap::iterator found
= plugin_interfaces_
.find(interface_name
);
148 if (found
== plugin_interfaces_
.end()) {
149 const void* ret
= local_get_interface()(interface_name
.c_str());
150 plugin_interfaces_
.insert(std::make_pair(interface_name
, ret
));
153 return found
->second
;
156 bool PluginDispatcher::InitPluginWithChannel(
157 PluginDelegate
* delegate
,
158 base::ProcessId peer_pid
,
159 const IPC::ChannelHandle
& channel_handle
,
161 if (!Dispatcher::InitWithChannel(delegate
, peer_pid
, channel_handle
,
164 plugin_delegate_
= delegate
;
165 plugin_dispatcher_id_
= plugin_delegate_
->Register(this);
167 // The message filter will intercept and process certain messages directly
168 // on the I/O thread.
169 channel()->AddFilter(
170 new PluginMessageFilter(delegate
->GetGloballySeenInstanceIDSet()));
174 bool PluginDispatcher::IsPlugin() const {
178 bool PluginDispatcher::Send(IPC::Message
* msg
) {
179 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::Send",
180 "Class", IPC_MESSAGE_ID_CLASS(msg
->type()),
181 "Line", IPC_MESSAGE_ID_LINE(msg
->type()));
182 // We always want plugin->renderer messages to arrive in-order. If some sync
183 // and some async messages are sent in response to a synchronous
184 // renderer->plugin call, the sync reply will be processed before the async
185 // reply, and everything will be confused.
187 // Allowing all async messages to unblock the renderer means more reentrancy
188 // there but gives correct ordering.
190 // We don't want reply messages to unblock however, as they will potentially
191 // end up on the wrong queue - see crbug.com/122443
192 if (!msg
->is_reply())
193 msg
->set_unblock(true);
194 if (msg
->is_sync()) {
195 // Synchronous messages might be re-entrant, so we need to drop the lock.
196 ProxyAutoUnlock unlock
;
198 // TODO(yzshen): Make sending message thread-safe. It may be accessed from
199 // non-main threads. Moreover, since the proxy lock has been released, it
200 // may be accessed by multiple threads at the same time.
201 return Dispatcher::Send(msg
);
203 return Dispatcher::Send(msg
);
206 bool PluginDispatcher::OnMessageReceived(const IPC::Message
& msg
) {
207 // We need to grab the proxy lock to ensure that we don't collide with the
208 // plugin making pepper calls on a different thread.
210 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::OnMessageReceived",
211 "Class", IPC_MESSAGE_ID_CLASS(msg
.type()),
212 "Line", IPC_MESSAGE_ID_LINE(msg
.type()));
214 if (msg
.routing_id() == MSG_ROUTING_CONTROL
) {
215 // Handle some plugin-specific control messages.
217 IPC_BEGIN_MESSAGE_MAP(PluginDispatcher
, msg
)
218 IPC_MESSAGE_HANDLER(PpapiPluginMsg_ResourceReply
, OnMsgResourceReply
)
219 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface
, OnMsgSupportsInterface
)
220 IPC_MESSAGE_HANDLER(PpapiMsg_SetPreferences
, OnMsgSetPreferences
)
221 IPC_MESSAGE_UNHANDLED(handled
= false);
222 IPC_END_MESSAGE_MAP()
226 return Dispatcher::OnMessageReceived(msg
);
229 void PluginDispatcher::OnChannelError() {
230 Dispatcher::OnChannelError();
232 // The renderer has crashed or exited. This channel and all instances
233 // associated with it are no longer valid.
234 ForceFreeAllInstances();
235 // TODO(brettw) free resources too!
239 void PluginDispatcher::DidCreateInstance(PP_Instance instance
) {
240 if (!g_instance_to_dispatcher
)
241 g_instance_to_dispatcher
= new InstanceToDispatcherMap
;
242 (*g_instance_to_dispatcher
)[instance
] = this;
244 instance_map_
[instance
] = InstanceData();
247 void PluginDispatcher::DidDestroyInstance(PP_Instance instance
) {
248 InstanceDataMap::iterator it
= instance_map_
.find(instance
);
249 if (it
!= instance_map_
.end())
250 instance_map_
.erase(it
);
252 if (g_instance_to_dispatcher
) {
253 InstanceToDispatcherMap::iterator found
= g_instance_to_dispatcher
->find(
255 if (found
!= g_instance_to_dispatcher
->end()) {
256 DCHECK(found
->second
== this);
257 g_instance_to_dispatcher
->erase(found
);
264 InstanceData
* PluginDispatcher::GetInstanceData(PP_Instance instance
) {
265 InstanceDataMap::iterator it
= instance_map_
.find(instance
);
266 return (it
== instance_map_
.end()) ? NULL
: &it
->second
;
269 thunk::PPB_Instance_API
* PluginDispatcher::GetInstanceAPI() {
270 return static_cast<PPB_Instance_Proxy
*>(
271 GetInterfaceProxy(API_ID_PPB_INSTANCE
));
274 thunk::ResourceCreationAPI
* PluginDispatcher::GetResourceCreationAPI() {
275 return static_cast<ResourceCreationProxy
*>(
276 GetInterfaceProxy(API_ID_RESOURCE_CREATION
));
280 void PluginDispatcher::DispatchResourceReply(
281 const ppapi::proxy::ResourceMessageReplyParams
& reply_params
,
282 const IPC::Message
& nested_msg
) {
283 // We need to grab the proxy lock to ensure that we don't collide with the
284 // plugin making pepper calls on a different thread.
286 LockedDispatchResourceReply(reply_params
, nested_msg
);
289 void PluginDispatcher::ForceFreeAllInstances() {
290 if (!g_instance_to_dispatcher
)
293 // Iterating will remove each item from the map, so we need to make a copy
294 // to avoid things changing out from under is.
295 InstanceToDispatcherMap temp_map
= *g_instance_to_dispatcher
;
296 for (InstanceToDispatcherMap::iterator i
= temp_map
.begin();
297 i
!= temp_map
.end(); ++i
) {
298 if (i
->second
== this) {
299 // Synthesize an "instance destroyed" message, this will notify the
300 // plugin and also remove it from our list of tracked plugins.
301 PpapiMsg_PPPInstance_DidDestroy
msg(API_ID_PPP_INSTANCE
, i
->first
);
302 OnMessageReceived(msg
);
307 void PluginDispatcher::OnMsgResourceReply(
308 const ppapi::proxy::ResourceMessageReplyParams
& reply_params
,
309 const IPC::Message
& nested_msg
) {
310 LockedDispatchResourceReply(reply_params
, nested_msg
);
313 void PluginDispatcher::OnMsgSupportsInterface(
314 const std::string
& interface_name
,
316 *result
= !!GetPluginInterface(interface_name
);
318 // Do fallback for PPP_Instance. This is a hack here and if we have more
319 // cases like this it should be generalized. The PPP_Instance proxy always
320 // proxies the 1.1 interface, and then does fallback to 1.0 inside the
321 // plugin process (see PPP_Instance_Proxy). So here we return true for
322 // supporting the 1.1 interface if either 1.1 or 1.0 is supported.
323 if (!*result
&& interface_name
== PPP_INSTANCE_INTERFACE
)
324 *result
= !!GetPluginInterface(PPP_INSTANCE_INTERFACE_1_0
);
327 void PluginDispatcher::OnMsgSetPreferences(const Preferences
& prefs
) {
328 // The renderer may send us preferences more than once (currently this
329 // happens every time a new plugin instance is created). Since we don't have
330 // a way to signal to the plugin that the preferences have changed, changing
331 // the default fonts and such in the middle of a running plugin could be
332 // confusing to it. As a result, we never allow the preferences to be changed
333 // once they're set. The user will have to restart to get new font prefs
334 // propogated to plugins.
335 if (!received_preferences_
) {
336 received_preferences_
= true;
337 preferences_
= prefs
;
342 void PluginDispatcher::LockedDispatchResourceReply(
343 const ppapi::proxy::ResourceMessageReplyParams
& reply_params
,
344 const IPC::Message
& nested_msg
) {
345 Resource
* resource
= PpapiGlobals::Get()->GetResourceTracker()->GetResource(
346 reply_params
.pp_resource());
348 DLOG_IF(INFO
, reply_params
.sequence() != 0)
349 << "Pepper resource reply message received but the resource doesn't "
350 "exist (probably has been destroyed).";
353 resource
->OnReplyReceived(reply_params
, nested_msg
);