blob: 15c521cd2e8e3dc54458f48cbf55236ce9065e69 [file] [log] [blame]
[email protected]4c41d3f2012-02-15 01:44:471// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c2932f5e2010-11-03 03:22:332// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ppapi/proxy/plugin_dispatcher.h"
6
7#include <map>
8
[email protected]709a847e2010-11-10 01:16:119#include "base/compiler_specific.h"
[email protected]c2932f5e2010-11-03 03:22:3310#include "base/logging.h"
[email protected]a76295972013-07-18 00:42:3211#include "base/message_loop/message_loop.h"
gab751416e1c2015-03-26 17:20:5812#include "base/metrics/histogram_macros.h"
primiano3ca22962015-01-30 17:08:3713#include "base/trace_event/trace_event.h"
[email protected]c2932f5e2010-11-03 03:22:3314#include "ipc/ipc_message.h"
15#include "ipc/ipc_sync_channel.h"
[email protected]83ad1c42013-05-01 16:56:2716#include "ipc/ipc_sync_message_filter.h"
[email protected]c2932f5e2010-11-03 03:22:3317#include "ppapi/c/pp_errors.h"
[email protected]e8f07ac2012-01-03 17:43:3618#include "ppapi/c/ppp_instance.h"
[email protected]a1686e272012-11-01 23:39:3419#include "ppapi/proxy/flash_clipboard_resource.h"
[email protected]0c92b0d2012-12-08 00:46:2320#include "ppapi/proxy/flash_file_resource.h"
[email protected]a76295972013-07-18 00:42:3221#include "ppapi/proxy/flash_resource.h"
[email protected]f5118812012-08-24 19:54:3022#include "ppapi/proxy/gamepad_resource.h"
[email protected]5c966022011-09-13 18:09:3723#include "ppapi/proxy/interface_list.h"
[email protected]c2932f5e2010-11-03 03:22:3324#include "ppapi/proxy/interface_proxy.h"
[email protected]60181162013-04-26 22:02:5325#include "ppapi/proxy/plugin_globals.h"
[email protected]2cc062242011-03-10 21:16:3426#include "ppapi/proxy/plugin_message_filter.h"
[email protected]6239d342011-05-06 22:55:4727#include "ppapi/proxy/plugin_resource_tracker.h"
[email protected]c2932f5e2010-11-03 03:22:3328#include "ppapi/proxy/plugin_var_serialization_rules.h"
29#include "ppapi/proxy/ppapi_messages.h"
[email protected]ceadc392011-06-15 23:04:2430#include "ppapi/proxy/ppb_instance_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3331#include "ppapi/proxy/ppp_class_proxy.h"
[email protected]6239d342011-05-06 22:55:4732#include "ppapi/proxy/resource_creation_proxy.h"
[email protected]511c58e2013-12-12 12:25:3333#include "ppapi/proxy/resource_reply_thread_registrar.h"
[email protected]eccf80312012-07-14 15:43:4234#include "ppapi/shared_impl/ppapi_globals.h"
[email protected]4c41d3f2012-02-15 01:44:4735#include "ppapi/shared_impl/proxy_lock.h"
[email protected]7f8b26b2011-08-18 15:41:0136#include "ppapi/shared_impl/resource.h"
[email protected]c2932f5e2010-11-03 03:22:3337
[email protected]339fbcff2012-02-29 16:10:3238#if defined(OS_POSIX) && !defined(OS_NACL)
[email protected]a08ebea2011-02-13 17:50:2039#include "ipc/ipc_channel_posix.h"
40#endif
41
[email protected]4d2efd22011-08-18 21:58:0242namespace ppapi {
[email protected]c2932f5e2010-11-03 03:22:3343namespace proxy {
44
45namespace {
46
[email protected]465faa22011-02-08 16:31:4647typedef std::map<PP_Instance, PluginDispatcher*> InstanceToDispatcherMap;
48InstanceToDispatcherMap* g_instance_to_dispatcher = NULL;
[email protected]c2932f5e2010-11-03 03:22:3349
[email protected]a9b16dd2012-01-31 05:00:2650typedef std::set<PluginDispatcher*> DispatcherSet;
51DispatcherSet* g_live_dispatchers = NULL;
52
[email protected]c2932f5e2010-11-03 03:22:3353} // namespace
54
[email protected]06e0a342011-09-27 04:24:3055InstanceData::InstanceData()
[email protected]22fdaa62012-11-30 01:55:4456 : is_request_surrounding_text_pending(false),
[email protected]a8e72142012-08-21 17:24:2057 should_do_request_surrounding_text(false) {
[email protected]c7bf7452011-09-12 21:31:5058}
59
[email protected]0f41c012011-10-21 19:49:2060InstanceData::~InstanceData() {
61 // Run any pending mouse lock callback to prevent leaks.
[email protected]f0c86242013-06-02 21:25:4362 if (mouse_lock_callback.get())
[email protected]aed96532012-06-23 14:27:4263 mouse_lock_callback->Abort();
[email protected]0f41c012011-10-21 19:49:2064}
65
penghuang3634509a2015-06-15 15:06:4966InstanceData::FlushInfo::FlushInfo()
67 : flush_pending(false),
68 put_offset(0) {
69}
70
71InstanceData::FlushInfo::~FlushInfo() {
72}
73
[email protected]f0ecb552012-05-11 22:09:1174PluginDispatcher::PluginDispatcher(PP_GetInterface_Func get_interface,
[email protected]195d4cde2012-10-02 18:12:4175 const PpapiPermissions& permissions,
[email protected]bc2eeb42012-05-02 22:35:5376 bool incognito)
[email protected]195d4cde2012-10-02 18:12:4177 : Dispatcher(get_interface, permissions),
[email protected]208aad792011-05-26 19:05:2878 plugin_delegate_(NULL),
[email protected]373a95a2011-07-01 16:58:1479 received_preferences_(false),
[email protected]bc2eeb42012-05-02 22:35:5380 plugin_dispatcher_id_(0),
81 incognito_(incognito) {
[email protected]67600b92012-03-10 06:51:4882 SetSerializationRules(new PluginVarSerializationRules(AsWeakPtr()));
[email protected]a9b16dd2012-01-31 05:00:2683
84 if (!g_live_dispatchers)
85 g_live_dispatchers = new DispatcherSet;
86 g_live_dispatchers->insert(this);
[email protected]c2932f5e2010-11-03 03:22:3387}
88
89PluginDispatcher::~PluginDispatcher() {
[email protected]60181162013-04-26 22:02:5390 PluginGlobals::Get()->plugin_var_tracker()->DidDeleteDispatcher(this);
91
[email protected]373a95a2011-07-01 16:58:1492 if (plugin_delegate_)
93 plugin_delegate_->Unregister(plugin_dispatcher_id_);
[email protected]a9b16dd2012-01-31 05:00:2694
95 g_live_dispatchers->erase(this);
96 if (g_live_dispatchers->empty()) {
97 delete g_live_dispatchers;
98 g_live_dispatchers = NULL;
99 }
[email protected]c2932f5e2010-11-03 03:22:33100}
101
102// static
[email protected]4614f192011-01-21 00:26:43103PluginDispatcher* PluginDispatcher::GetForInstance(PP_Instance instance) {
[email protected]465faa22011-02-08 16:31:46104 if (!g_instance_to_dispatcher)
105 return NULL;
106 InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find(
107 instance);
108 if (found == g_instance_to_dispatcher->end())
109 return NULL;
110 return found->second;
[email protected]4614f192011-01-21 00:26:43111}
112
[email protected]a08ebea2011-02-13 17:50:20113// static
[email protected]7f8b26b2011-08-18 15:41:01114PluginDispatcher* PluginDispatcher::GetForResource(const Resource* resource) {
115 return GetForInstance(resource->pp_instance());
116}
117
118// static
[email protected]5c966022011-09-13 18:09:37119const void* PluginDispatcher::GetBrowserInterface(const char* interface_name) {
dmichaelfee3a512014-09-18 21:32:13120 // CAUTION: This function is called directly from the plugin, but we *don't*
121 // lock the ProxyLock to avoid excessive locking from C++ wrappers.
[email protected]5c966022011-09-13 18:09:37122 return InterfaceList::GetInstance()->GetInterfaceForPPB(interface_name);
123}
124
[email protected]a9b16dd2012-01-31 05:00:26125// static
126void PluginDispatcher::LogWithSource(PP_Instance instance,
[email protected]598816ad2012-12-13 01:34:32127 PP_LogLevel level,
[email protected]a9b16dd2012-01-31 05:00:26128 const std::string& source,
129 const std::string& value) {
130 if (!g_live_dispatchers || !g_instance_to_dispatcher)
131 return;
132
133 if (instance) {
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));
140 return;
141 }
142 }
143
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));
149 }
150}
151
[email protected]5c966022011-09-13 18:09:37152const 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));
158 return ret;
159 }
160 return found->second;
[email protected]a08ebea2011-02-13 17:50:20161}
162
[email protected]e2614c62011-04-16 22:12:45163bool PluginDispatcher::InitPluginWithChannel(
[email protected]d259a8e2011-05-18 22:31:09164 PluginDelegate* delegate,
[email protected]108fd342013-01-04 20:46:54165 base::ProcessId peer_pid,
[email protected]2cc062242011-03-10 21:16:34166 const IPC::ChannelHandle& channel_handle,
167 bool is_client) {
[email protected]f7b7eb7c2014-02-27 23:54:15168 if (!Dispatcher::InitWithChannel(delegate, peer_pid, channel_handle,
169 is_client))
170 return false;
[email protected]d259a8e2011-05-18 22:31:09171 plugin_delegate_ = delegate;
[email protected]373a95a2011-07-01 16:58:14172 plugin_dispatcher_id_ = plugin_delegate_->Register(this);
[email protected]2cc062242011-03-10 21:16:34173
[email protected]83ad1c42013-05-01 16:56:27174 sync_filter_ = new IPC::SyncMessageFilter(delegate->GetShutdownEvent());
175 channel()->AddFilter(sync_filter_.get());
176
[email protected]2cc062242011-03-10 21:16:34177 // The message filter will intercept and process certain messages directly
178 // on the I/O thread.
179 channel()->AddFilter(
[email protected]511c58e2013-12-12 12:25:33180 new PluginMessageFilter(
181 delegate->GetGloballySeenInstanceIDSet(),
182 PluginGlobals::Get()->resource_reply_thread_registrar()));
[email protected]2cc062242011-03-10 21:16:34183 return true;
184}
185
[email protected]7cf40912010-12-09 18:25:03186bool PluginDispatcher::IsPlugin() const {
187 return true;
188}
189
[email protected]83ad1c42013-05-01 16:56:27190bool 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);
197}
198
[email protected]b00bbb32011-03-30 19:02:14199bool PluginDispatcher::Send(IPC::Message* msg) {
[email protected]366ae242011-05-10 02:23:58200 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::Send",
201 "Class", IPC_MESSAGE_ID_CLASS(msg->type()),
202 "Line", IPC_MESSAGE_ID_LINE(msg->type()));
[email protected]b00bbb32011-03-30 19:02:14203 // We always want plugin->renderer messages to arrive in-order. If some sync
[email protected]4c41d3f2012-02-15 01:44:47204 // and some async messages are sent in response to a synchronous
[email protected]b00bbb32011-03-30 19:02:14205 // renderer->plugin call, the sync reply will be processed before the async
206 // reply, and everything will be confused.
207 //
208 // Allowing all async messages to unblock the renderer means more reentrancy
209 // there but gives correct ordering.
[email protected]80f86a92012-04-09 23:58:14210 //
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);
[email protected]4c41d3f2012-02-15 01:44:47215 if (msg->is_sync()) {
216 // Synchronous messages might be re-entrant, so we need to drop the lock.
217 ProxyAutoUnlock unlock;
gab751416e1c2015-03-26 17:20:58218 SCOPED_UMA_HISTOGRAM_TIMER("Plugin.PpapiSyncIPCTime");
[email protected]83ad1c42013-05-01 16:56:27219 return SendMessage(msg);
[email protected]4c41d3f2012-02-15 01:44:47220 }
[email protected]83ad1c42013-05-01 16:56:27221 return SendMessage(msg);
[email protected]b00bbb32011-03-30 19:02:14222}
223
dmichael06b3b7f2015-04-03 05:22:26224bool 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);
231}
232
[email protected]a95986a82010-12-24 06:19:28233bool PluginDispatcher::OnMessageReceived(const IPC::Message& msg) {
[email protected]4c41d3f2012-02-15 01:44:47234 // 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.
236 ProxyAutoLock lock;
[email protected]366ae242011-05-10 02:23:58237 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::OnMessageReceived",
238 "Class", IPC_MESSAGE_ID_CLASS(msg.type()),
239 "Line", IPC_MESSAGE_ID_LINE(msg.type()));
[email protected]eccf80312012-07-14 15:43:42240
[email protected]c2932f5e2010-11-03 03:22:33241 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
242 // Handle some plugin-specific control messages.
[email protected]a95986a82010-12-24 06:19:28243 bool handled = true;
[email protected]c2932f5e2010-11-03 03:22:33244 IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg)
kareng1c62eeb2014-11-08 16:35:03245 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface)
[email protected]208aad792011-05-26 19:05:28246 IPC_MESSAGE_HANDLER(PpapiMsg_SetPreferences, OnMsgSetPreferences)
[email protected]5c966022011-09-13 18:09:37247 IPC_MESSAGE_UNHANDLED(handled = false);
[email protected]c2932f5e2010-11-03 03:22:33248 IPC_END_MESSAGE_MAP()
[email protected]5c966022011-09-13 18:09:37249 if (handled)
[email protected]37fe0362011-09-13 04:00:33250 return true;
[email protected]37fe0362011-09-13 04:00:33251 }
[email protected]5c966022011-09-13 18:09:37252 return Dispatcher::OnMessageReceived(msg);
[email protected]c2932f5e2010-11-03 03:22:33253}
254
[email protected]a08ebea2011-02-13 17:50:20255void PluginDispatcher::OnChannelError() {
[email protected]4f15d2842011-02-15 17:36:33256 Dispatcher::OnChannelError();
257
[email protected]4b417e52011-04-18 22:51:08258 // The renderer has crashed or exited. This channel and all instances
259 // associated with it are no longer valid.
[email protected]a08ebea2011-02-13 17:50:20260 ForceFreeAllInstances();
261 // TODO(brettw) free resources too!
262 delete this;
263}
264
[email protected]f56279c2011-02-02 18:12:31265void PluginDispatcher::DidCreateInstance(PP_Instance instance) {
[email protected]465faa22011-02-08 16:31:46266 if (!g_instance_to_dispatcher)
267 g_instance_to_dispatcher = new InstanceToDispatcherMap;
268 (*g_instance_to_dispatcher)[instance] = this;
[email protected]4111b992014-05-15 17:11:20269 instance_map_.set(instance, scoped_ptr<InstanceData>(new InstanceData()));
[email protected]f56279c2011-02-02 18:12:31270}
271
272void PluginDispatcher::DidDestroyInstance(PP_Instance instance) {
[email protected]4111b992014-05-15 17:11:20273 instance_map_.erase(instance);
[email protected]465faa22011-02-08 16:31:46274
275 if (g_instance_to_dispatcher) {
276 InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find(
277 instance);
278 if (found != g_instance_to_dispatcher->end()) {
279 DCHECK(found->second == this);
280 g_instance_to_dispatcher->erase(found);
281 } else {
282 NOTREACHED();
283 }
284 }
[email protected]f56279c2011-02-02 18:12:31285}
286
287InstanceData* PluginDispatcher::GetInstanceData(PP_Instance instance) {
[email protected]4111b992014-05-15 17:11:20288 return instance_map_.get(instance);
[email protected]f56279c2011-02-02 18:12:31289}
290
[email protected]4f2006122012-04-30 05:13:17291thunk::PPB_Instance_API* PluginDispatcher::GetInstanceAPI() {
292 return static_cast<PPB_Instance_Proxy*>(
293 GetInterfaceProxy(API_ID_PPB_INSTANCE));
294}
295
296thunk::ResourceCreationAPI* PluginDispatcher::GetResourceCreationAPI() {
297 return static_cast<ResourceCreationProxy*>(
298 GetInterfaceProxy(API_ID_RESOURCE_CREATION));
[email protected]6239d342011-05-06 22:55:47299}
300
[email protected]a08ebea2011-02-13 17:50:20301void PluginDispatcher::ForceFreeAllInstances() {
[email protected]4f15d2842011-02-15 17:36:33302 if (!g_instance_to_dispatcher)
303 return;
304
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.
[email protected]ac4b54d2011-10-20 23:09:28313 PpapiMsg_PPPInstance_DidDestroy msg(API_ID_PPP_INSTANCE, i->first);
[email protected]4585fbc2011-06-13 17:17:56314 OnMessageReceived(msg);
[email protected]4f15d2842011-02-15 17:36:33315 }
316 }
[email protected]176c73922010-12-03 17:32:19317}
318
kareng1c62eeb2014-11-08 16:35:03319void PluginDispatcher::OnMsgSupportsInterface(
[email protected]465faa22011-02-08 16:31:46320 const std::string& interface_name,
321 bool* result) {
[email protected]5c966022011-09-13 18:09:37322 *result = !!GetPluginInterface(interface_name);
[email protected]e8f07ac2012-01-03 17:43:36323
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);
[email protected]465faa22011-02-08 16:31:46331}
332
[email protected]4d2efd22011-08-18 21:58:02333void PluginDispatcher::OnMsgSetPreferences(const Preferences& prefs) {
[email protected]208aad792011-05-26 19:05:28334 // 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;
344 }
345}
346
[email protected]c2932f5e2010-11-03 03:22:33347} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02348} // namespace ppapi