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 const IPC::ChannelHandle
& channel_handle
,
160 if (!Dispatcher::InitWithChannel(delegate
, channel_handle
, is_client
))
162 plugin_delegate_
= delegate
;
163 plugin_dispatcher_id_
= plugin_delegate_
->Register(this);
165 // The message filter will intercept and process certain messages directly
166 // on the I/O thread.
167 channel()->AddFilter(
168 new PluginMessageFilter(delegate
->GetGloballySeenInstanceIDSet()));
172 bool PluginDispatcher::IsPlugin() const {
176 bool PluginDispatcher::Send(IPC::Message
* msg
) {
177 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::Send",
178 "Class", IPC_MESSAGE_ID_CLASS(msg
->type()),
179 "Line", IPC_MESSAGE_ID_LINE(msg
->type()));
180 // We always want plugin->renderer messages to arrive in-order. If some sync
181 // and some async messages are sent in response to a synchronous
182 // renderer->plugin call, the sync reply will be processed before the async
183 // reply, and everything will be confused.
185 // Allowing all async messages to unblock the renderer means more reentrancy
186 // there but gives correct ordering.
188 // We don't want reply messages to unblock however, as they will potentially
189 // end up on the wrong queue - see crbug.com/122443
190 if (!msg
->is_reply())
191 msg
->set_unblock(true);
192 if (msg
->is_sync()) {
193 // Synchronous messages might be re-entrant, so we need to drop the lock.
194 ProxyAutoUnlock unlock
;
196 // TODO(yzshen): Make sending message thread-safe. It may be accessed from
197 // non-main threads. Moreover, since the proxy lock has been released, it
198 // may be accessed by multiple threads at the same time.
199 return Dispatcher::Send(msg
);
201 return Dispatcher::Send(msg
);
204 bool PluginDispatcher::OnMessageReceived(const IPC::Message
& msg
) {
205 // We need to grab the proxy lock to ensure that we don't collide with the
206 // plugin making pepper calls on a different thread.
208 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::OnMessageReceived",
209 "Class", IPC_MESSAGE_ID_CLASS(msg
.type()),
210 "Line", IPC_MESSAGE_ID_LINE(msg
.type()));
212 if (msg
.routing_id() == MSG_ROUTING_CONTROL
) {
213 // Handle some plugin-specific control messages.
215 IPC_BEGIN_MESSAGE_MAP(PluginDispatcher
, msg
)
216 IPC_MESSAGE_HANDLER(PpapiPluginMsg_ResourceReply
, OnMsgResourceReply
)
217 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface
, OnMsgSupportsInterface
)
218 IPC_MESSAGE_HANDLER(PpapiMsg_SetPreferences
, OnMsgSetPreferences
)
219 IPC_MESSAGE_UNHANDLED(handled
= false);
220 IPC_END_MESSAGE_MAP()
224 return Dispatcher::OnMessageReceived(msg
);
227 void PluginDispatcher::OnChannelError() {
228 Dispatcher::OnChannelError();
230 // The renderer has crashed or exited. This channel and all instances
231 // associated with it are no longer valid.
232 ForceFreeAllInstances();
233 // TODO(brettw) free resources too!
237 void PluginDispatcher::DidCreateInstance(PP_Instance instance
) {
238 if (!g_instance_to_dispatcher
)
239 g_instance_to_dispatcher
= new InstanceToDispatcherMap
;
240 (*g_instance_to_dispatcher
)[instance
] = this;
242 instance_map_
[instance
] = InstanceData();
245 void PluginDispatcher::DidDestroyInstance(PP_Instance instance
) {
246 InstanceDataMap::iterator it
= instance_map_
.find(instance
);
247 if (it
!= instance_map_
.end())
248 instance_map_
.erase(it
);
250 if (g_instance_to_dispatcher
) {
251 InstanceToDispatcherMap::iterator found
= g_instance_to_dispatcher
->find(
253 if (found
!= g_instance_to_dispatcher
->end()) {
254 DCHECK(found
->second
== this);
255 g_instance_to_dispatcher
->erase(found
);
262 InstanceData
* PluginDispatcher::GetInstanceData(PP_Instance instance
) {
263 InstanceDataMap::iterator it
= instance_map_
.find(instance
);
264 return (it
== instance_map_
.end()) ? NULL
: &it
->second
;
267 thunk::PPB_Instance_API
* PluginDispatcher::GetInstanceAPI() {
268 return static_cast<PPB_Instance_Proxy
*>(
269 GetInterfaceProxy(API_ID_PPB_INSTANCE
));
272 thunk::ResourceCreationAPI
* PluginDispatcher::GetResourceCreationAPI() {
273 return static_cast<ResourceCreationProxy
*>(
274 GetInterfaceProxy(API_ID_RESOURCE_CREATION
));
278 void PluginDispatcher::DispatchResourceReply(
279 const ppapi::proxy::ResourceMessageReplyParams
& reply_params
,
280 const IPC::Message
& nested_msg
) {
281 // We need to grab the proxy lock to ensure that we don't collide with the
282 // plugin making pepper calls on a different thread.
284 LockedDispatchResourceReply(reply_params
, nested_msg
);
287 void PluginDispatcher::ForceFreeAllInstances() {
288 if (!g_instance_to_dispatcher
)
291 // Iterating will remove each item from the map, so we need to make a copy
292 // to avoid things changing out from under is.
293 InstanceToDispatcherMap temp_map
= *g_instance_to_dispatcher
;
294 for (InstanceToDispatcherMap::iterator i
= temp_map
.begin();
295 i
!= temp_map
.end(); ++i
) {
296 if (i
->second
== this) {
297 // Synthesize an "instance destroyed" message, this will notify the
298 // plugin and also remove it from our list of tracked plugins.
299 PpapiMsg_PPPInstance_DidDestroy
msg(API_ID_PPP_INSTANCE
, i
->first
);
300 OnMessageReceived(msg
);
305 void PluginDispatcher::OnMsgResourceReply(
306 const ppapi::proxy::ResourceMessageReplyParams
& reply_params
,
307 const IPC::Message
& nested_msg
) {
308 LockedDispatchResourceReply(reply_params
, nested_msg
);
311 void PluginDispatcher::OnMsgSupportsInterface(
312 const std::string
& interface_name
,
314 *result
= !!GetPluginInterface(interface_name
);
316 // Do fallback for PPP_Instance. This is a hack here and if we have more
317 // cases like this it should be generalized. The PPP_Instance proxy always
318 // proxies the 1.1 interface, and then does fallback to 1.0 inside the
319 // plugin process (see PPP_Instance_Proxy). So here we return true for
320 // supporting the 1.1 interface if either 1.1 or 1.0 is supported.
321 if (!*result
&& interface_name
== PPP_INSTANCE_INTERFACE
)
322 *result
= !!GetPluginInterface(PPP_INSTANCE_INTERFACE_1_0
);
325 void PluginDispatcher::OnMsgSetPreferences(const Preferences
& prefs
) {
326 // The renderer may send us preferences more than once (currently this
327 // happens every time a new plugin instance is created). Since we don't have
328 // a way to signal to the plugin that the preferences have changed, changing
329 // the default fonts and such in the middle of a running plugin could be
330 // confusing to it. As a result, we never allow the preferences to be changed
331 // once they're set. The user will have to restart to get new font prefs
332 // propogated to plugins.
333 if (!received_preferences_
) {
334 received_preferences_
= true;
335 preferences_
= prefs
;
340 void PluginDispatcher::LockedDispatchResourceReply(
341 const ppapi::proxy::ResourceMessageReplyParams
& reply_params
,
342 const IPC::Message
& nested_msg
) {
343 Resource
* resource
= PpapiGlobals::Get()->GetResourceTracker()->GetResource(
344 reply_params
.pp_resource());
346 DLOG_IF(INFO
, reply_params
.sequence() != 0)
347 << "Pepper resource reply message received but the resource doesn't "
348 "exist (probably has been destroyed).";
351 resource
->OnReplyReceived(reply_params
, nested_msg
);