blob: fa74e8796b50074927ae463ad11729be1ef3d6ee [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>
Gyuyoung Kimb480aba2018-01-27 07:00:048#include <memory>
[email protected]c2932f5e2010-11-03 03:22:339
Hans Wennborg708fa822020-04-27 17:23:1510#include "base/check.h"
[email protected]709a847e2010-11-10 01:16:1111#include "base/compiler_specific.h"
gab751416e1c2015-03-26 17:20:5812#include "base/metrics/histogram_macros.h"
Hans Wennborg708fa822020-04-27 17:23:1513#include "base/notreached.h"
David Sanders527f0ce92022-03-23 17:42:3014#include "base/threading/thread_task_runner_handle.h"
primiano3ca22962015-01-30 17:08:3715#include "base/trace_event/trace_event.h"
avie029c4132015-12-23 06:45:2216#include "build/build_config.h"
[email protected]c2932f5e2010-11-03 03:22:3317#include "ipc/ipc_message.h"
18#include "ipc/ipc_sync_channel.h"
[email protected]83ad1c42013-05-01 16:56:2719#include "ipc/ipc_sync_message_filter.h"
[email protected]c2932f5e2010-11-03 03:22:3320#include "ppapi/c/pp_errors.h"
[email protected]e8f07ac2012-01-03 17:43:3621#include "ppapi/c/ppp_instance.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]6239d342011-05-06 22:55:4731#include "ppapi/proxy/resource_creation_proxy.h"
[email protected]511c58e2013-12-12 12:25:3332#include "ppapi/proxy/resource_reply_thread_registrar.h"
[email protected]eccf80312012-07-14 15:43:4233#include "ppapi/shared_impl/ppapi_globals.h"
[email protected]4c41d3f2012-02-15 01:44:4734#include "ppapi/shared_impl/proxy_lock.h"
[email protected]7f8b26b2011-08-18 15:41:0135#include "ppapi/shared_impl/resource.h"
[email protected]c2932f5e2010-11-03 03:22:3336
[email protected]4d2efd22011-08-18 21:58:0237namespace ppapi {
[email protected]c2932f5e2010-11-03 03:22:3338namespace proxy {
39
40namespace {
41
Daniel Bratelld5850cc2018-10-25 08:40:5542typedef std::map<PP_Instance, PluginDispatcher*> InstanceToPluginDispatcherMap;
43InstanceToPluginDispatcherMap* g_instance_to_plugin_dispatcher = NULL;
[email protected]c2932f5e2010-11-03 03:22:3344
[email protected]a9b16dd2012-01-31 05:00:2645typedef std::set<PluginDispatcher*> DispatcherSet;
46DispatcherSet* g_live_dispatchers = NULL;
47
[email protected]c2932f5e2010-11-03 03:22:3348} // namespace
49
[email protected]06e0a342011-09-27 04:24:3050InstanceData::InstanceData()
[email protected]22fdaa62012-11-30 01:55:4451 : is_request_surrounding_text_pending(false),
[email protected]a8e72142012-08-21 17:24:2052 should_do_request_surrounding_text(false) {
[email protected]c7bf7452011-09-12 21:31:5053}
54
[email protected]0f41c012011-10-21 19:49:2055InstanceData::~InstanceData() {
56 // Run any pending mouse lock callback to prevent leaks.
[email protected]f0c86242013-06-02 21:25:4357 if (mouse_lock_callback.get())
[email protected]aed96532012-06-23 14:27:4258 mouse_lock_callback->Abort();
[email protected]0f41c012011-10-21 19:49:2059}
60
penghuang3634509a2015-06-15 15:06:4961InstanceData::FlushInfo::FlushInfo()
62 : flush_pending(false),
63 put_offset(0) {
64}
65
66InstanceData::FlushInfo::~FlushInfo() {
67}
68
Raymes Khoury982b7eb22017-07-26 09:40:2469PluginDispatcher::Sender::Sender(
70 base::WeakPtr<PluginDispatcher> plugin_dispatcher,
71 scoped_refptr<IPC::SyncMessageFilter> sync_filter)
72 : plugin_dispatcher_(plugin_dispatcher), sync_filter_(sync_filter) {}
73
74PluginDispatcher::Sender::~Sender() {}
75
76bool PluginDispatcher::Sender::SendMessage(IPC::Message* msg) {
77 // Currently we need to choose between two different mechanisms for sending.
78 // On the main thread we use the regular dispatch Send() method, on another
79 // thread we use SyncMessageFilter.
80 if (PpapiGlobals::Get()
81 ->GetMainThreadMessageLoop()
82 ->BelongsToCurrentThread()) {
83 // The PluginDispatcher may have been destroyed if the channel is gone, but
84 // resources are leaked and may still send messages. We ignore those
85 // messages. See crbug.com/725033.
86 if (plugin_dispatcher_) {
87 return plugin_dispatcher_.get()->Dispatcher::Send(msg);
88 } else {
89 delete msg;
90 return false;
91 }
92 }
93 return sync_filter_->Send(msg);
94}
95
96bool PluginDispatcher::Sender::Send(IPC::Message* msg) {
Sami Kyostila0f5c1549e2021-02-08 20:17:5297 TRACE_EVENT2("ppapi_proxy", "PluginDispatcher::Send", "Class",
Raymes Khoury982b7eb22017-07-26 09:40:2498 IPC_MESSAGE_ID_CLASS(msg->type()), "Line",
99 IPC_MESSAGE_ID_LINE(msg->type()));
100 // We always want plugin->renderer messages to arrive in-order. If some sync
101 // and some async messages are sent in response to a synchronous
102 // renderer->plugin call, the sync reply will be processed before the async
103 // reply, and everything will be confused.
104 //
105 // Allowing all async messages to unblock the renderer means more reentrancy
106 // there but gives correct ordering.
107 //
108 // We don't want reply messages to unblock however, as they will potentially
109 // end up on the wrong queue - see crbug.com/122443
110 if (!msg->is_reply())
111 msg->set_unblock(true);
112 if (msg->is_sync()) {
113 // Synchronous messages might be re-entrant, so we need to drop the lock.
114 ProxyAutoUnlock unlock;
115 SCOPED_UMA_HISTOGRAM_TIMER("Plugin.PpapiSyncIPCTime");
116 return SendMessage(msg);
117 }
118 return SendMessage(msg);
119}
120
[email protected]f0ecb552012-05-11 22:09:11121PluginDispatcher::PluginDispatcher(PP_GetInterface_Func get_interface,
[email protected]195d4cde2012-10-02 18:12:41122 const PpapiPermissions& permissions,
[email protected]bc2eeb42012-05-02 22:35:53123 bool incognito)
[email protected]195d4cde2012-10-02 18:12:41124 : Dispatcher(get_interface, permissions),
[email protected]208aad792011-05-26 19:05:28125 plugin_delegate_(NULL),
[email protected]373a95a2011-07-01 16:58:14126 received_preferences_(false),
[email protected]bc2eeb42012-05-02 22:35:53127 plugin_dispatcher_id_(0),
Raymes Khoury982b7eb22017-07-26 09:40:24128 incognito_(incognito),
129 sender_(
130 new Sender(AsWeakPtr(), scoped_refptr<IPC::SyncMessageFilter>())) {
[email protected]67600b92012-03-10 06:51:48131 SetSerializationRules(new PluginVarSerializationRules(AsWeakPtr()));
[email protected]a9b16dd2012-01-31 05:00:26132
133 if (!g_live_dispatchers)
134 g_live_dispatchers = new DispatcherSet;
135 g_live_dispatchers->insert(this);
[email protected]c2932f5e2010-11-03 03:22:33136}
137
138PluginDispatcher::~PluginDispatcher() {
[email protected]60181162013-04-26 22:02:53139 PluginGlobals::Get()->plugin_var_tracker()->DidDeleteDispatcher(this);
140
[email protected]373a95a2011-07-01 16:58:14141 if (plugin_delegate_)
142 plugin_delegate_->Unregister(plugin_dispatcher_id_);
[email protected]a9b16dd2012-01-31 05:00:26143
144 g_live_dispatchers->erase(this);
145 if (g_live_dispatchers->empty()) {
146 delete g_live_dispatchers;
147 g_live_dispatchers = NULL;
148 }
[email protected]c2932f5e2010-11-03 03:22:33149}
150
151// static
[email protected]4614f192011-01-21 00:26:43152PluginDispatcher* PluginDispatcher::GetForInstance(PP_Instance instance) {
Daniel Bratelld5850cc2018-10-25 08:40:55153 if (!g_instance_to_plugin_dispatcher)
[email protected]465faa22011-02-08 16:31:46154 return NULL;
Daniel Bratelld5850cc2018-10-25 08:40:55155 InstanceToPluginDispatcherMap::iterator found =
156 g_instance_to_plugin_dispatcher->find(instance);
157 if (found == g_instance_to_plugin_dispatcher->end())
[email protected]465faa22011-02-08 16:31:46158 return NULL;
159 return found->second;
[email protected]4614f192011-01-21 00:26:43160}
161
[email protected]a08ebea2011-02-13 17:50:20162// static
[email protected]7f8b26b2011-08-18 15:41:01163PluginDispatcher* PluginDispatcher::GetForResource(const Resource* resource) {
164 return GetForInstance(resource->pp_instance());
165}
166
167// static
[email protected]5c966022011-09-13 18:09:37168const void* PluginDispatcher::GetBrowserInterface(const char* interface_name) {
dmichaelfee3a512014-09-18 21:32:13169 // CAUTION: This function is called directly from the plugin, but we *don't*
170 // lock the ProxyLock to avoid excessive locking from C++ wrappers.
[email protected]5c966022011-09-13 18:09:37171 return InterfaceList::GetInstance()->GetInterfaceForPPB(interface_name);
172}
173
[email protected]a9b16dd2012-01-31 05:00:26174// static
175void PluginDispatcher::LogWithSource(PP_Instance instance,
[email protected]598816ad2012-12-13 01:34:32176 PP_LogLevel level,
[email protected]a9b16dd2012-01-31 05:00:26177 const std::string& source,
178 const std::string& value) {
Daniel Bratelld5850cc2018-10-25 08:40:55179 if (!g_live_dispatchers || !g_instance_to_plugin_dispatcher)
[email protected]a9b16dd2012-01-31 05:00:26180 return;
181
182 if (instance) {
Daniel Bratelld5850cc2018-10-25 08:40:55183 InstanceToPluginDispatcherMap::iterator found =
184 g_instance_to_plugin_dispatcher->find(instance);
185 if (found != g_instance_to_plugin_dispatcher->end()) {
[email protected]a9b16dd2012-01-31 05:00:26186 // Send just to this specific dispatcher.
187 found->second->Send(new PpapiHostMsg_LogWithSource(
188 instance, static_cast<int>(level), source, value));
189 return;
190 }
191 }
192
193 // Instance 0 or invalid, send to all dispatchers.
194 for (DispatcherSet::iterator i = g_live_dispatchers->begin();
195 i != g_live_dispatchers->end(); ++i) {
196 (*i)->Send(new PpapiHostMsg_LogWithSource(
197 instance, static_cast<int>(level), source, value));
198 }
199}
200
[email protected]5c966022011-09-13 18:09:37201const void* PluginDispatcher::GetPluginInterface(
202 const std::string& interface_name) {
203 InterfaceMap::iterator found = plugin_interfaces_.find(interface_name);
204 if (found == plugin_interfaces_.end()) {
205 const void* ret = local_get_interface()(interface_name.c_str());
206 plugin_interfaces_.insert(std::make_pair(interface_name, ret));
207 return ret;
208 }
209 return found->second;
[email protected]a08ebea2011-02-13 17:50:20210}
211
[email protected]e2614c62011-04-16 22:12:45212bool PluginDispatcher::InitPluginWithChannel(
[email protected]d259a8e2011-05-18 22:31:09213 PluginDelegate* delegate,
[email protected]108fd342013-01-04 20:46:54214 base::ProcessId peer_pid,
[email protected]2cc062242011-03-10 21:16:34215 const IPC::ChannelHandle& channel_handle,
216 bool is_client) {
[email protected]f7b7eb7c2014-02-27 23:54:15217 if (!Dispatcher::InitWithChannel(delegate, peer_pid, channel_handle,
Hajime Hoshi5959c54f2019-01-09 01:42:12218 is_client,
219 base::ThreadTaskRunnerHandle::Get()))
[email protected]f7b7eb7c2014-02-27 23:54:15220 return false;
[email protected]d259a8e2011-05-18 22:31:09221 plugin_delegate_ = delegate;
[email protected]373a95a2011-07-01 16:58:14222 plugin_dispatcher_id_ = plugin_delegate_->Register(this);
[email protected]2cc062242011-03-10 21:16:34223
Raymes Khoury982b7eb22017-07-26 09:40:24224 sender_ = new Sender(AsWeakPtr(), channel()->CreateSyncMessageFilter());
[email protected]83ad1c42013-05-01 16:56:27225
[email protected]2cc062242011-03-10 21:16:34226 // The message filter will intercept and process certain messages directly
227 // on the I/O thread.
228 channel()->AddFilter(
[email protected]511c58e2013-12-12 12:25:33229 new PluginMessageFilter(
230 delegate->GetGloballySeenInstanceIDSet(),
231 PluginGlobals::Get()->resource_reply_thread_registrar()));
[email protected]2cc062242011-03-10 21:16:34232 return true;
233}
234
[email protected]7cf40912010-12-09 18:25:03235bool PluginDispatcher::IsPlugin() const {
236 return true;
237}
238
[email protected]b00bbb32011-03-30 19:02:14239bool PluginDispatcher::Send(IPC::Message* msg) {
Raymes Khoury982b7eb22017-07-26 09:40:24240 return sender_->Send(msg);
[email protected]b00bbb32011-03-30 19:02:14241}
242
dmichael06b3b7f2015-04-03 05:22:26243bool PluginDispatcher::SendAndStayLocked(IPC::Message* msg) {
Sami Kyostila0f5c1549e2021-02-08 20:17:52244 TRACE_EVENT2("ppapi_proxy", "PluginDispatcher::SendAndStayLocked", "Class",
245 IPC_MESSAGE_ID_CLASS(msg->type()), "Line",
246 IPC_MESSAGE_ID_LINE(msg->type()));
dmichael06b3b7f2015-04-03 05:22:26247 if (!msg->is_reply())
248 msg->set_unblock(true);
Raymes Khoury982b7eb22017-07-26 09:40:24249 return sender_->SendMessage(msg);
dmichael06b3b7f2015-04-03 05:22:26250}
251
[email protected]a95986a82010-12-24 06:19:28252bool PluginDispatcher::OnMessageReceived(const IPC::Message& msg) {
[email protected]4c41d3f2012-02-15 01:44:47253 // We need to grab the proxy lock to ensure that we don't collide with the
254 // plugin making pepper calls on a different thread.
255 ProxyAutoLock lock;
Sami Kyostila0f5c1549e2021-02-08 20:17:52256 TRACE_EVENT2("ppapi_proxy", "PluginDispatcher::OnMessageReceived", "Class",
257 IPC_MESSAGE_ID_CLASS(msg.type()), "Line",
258 IPC_MESSAGE_ID_LINE(msg.type()));
[email protected]eccf80312012-07-14 15:43:42259
[email protected]c2932f5e2010-11-03 03:22:33260 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
261 // Handle some plugin-specific control messages.
[email protected]a95986a82010-12-24 06:19:28262 bool handled = true;
[email protected]c2932f5e2010-11-03 03:22:33263 IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg)
kareng1c62eeb2014-11-08 16:35:03264 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface)
[email protected]208aad792011-05-26 19:05:28265 IPC_MESSAGE_HANDLER(PpapiMsg_SetPreferences, OnMsgSetPreferences)
philipjc842a082016-01-27 03:36:11266 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]c2932f5e2010-11-03 03:22:33267 IPC_END_MESSAGE_MAP()
[email protected]5c966022011-09-13 18:09:37268 if (handled)
[email protected]37fe0362011-09-13 04:00:33269 return true;
[email protected]37fe0362011-09-13 04:00:33270 }
[email protected]5c966022011-09-13 18:09:37271 return Dispatcher::OnMessageReceived(msg);
[email protected]c2932f5e2010-11-03 03:22:33272}
273
[email protected]a08ebea2011-02-13 17:50:20274void PluginDispatcher::OnChannelError() {
[email protected]4f15d2842011-02-15 17:36:33275 Dispatcher::OnChannelError();
276
[email protected]4b417e52011-04-18 22:51:08277 // The renderer has crashed or exited. This channel and all instances
278 // associated with it are no longer valid.
[email protected]a08ebea2011-02-13 17:50:20279 ForceFreeAllInstances();
280 // TODO(brettw) free resources too!
281 delete this;
282}
283
[email protected]f56279c2011-02-02 18:12:31284void PluginDispatcher::DidCreateInstance(PP_Instance instance) {
Daniel Bratelld5850cc2018-10-25 08:40:55285 if (!g_instance_to_plugin_dispatcher)
286 g_instance_to_plugin_dispatcher = new InstanceToPluginDispatcherMap;
287 (*g_instance_to_plugin_dispatcher)[instance] = this;
Gyuyoung Kimb480aba2018-01-27 07:00:04288 instance_map_[instance] = std::make_unique<InstanceData>();
[email protected]f56279c2011-02-02 18:12:31289}
290
291void PluginDispatcher::DidDestroyInstance(PP_Instance instance) {
[email protected]4111b992014-05-15 17:11:20292 instance_map_.erase(instance);
[email protected]465faa22011-02-08 16:31:46293
Daniel Bratelld5850cc2018-10-25 08:40:55294 if (g_instance_to_plugin_dispatcher) {
295 InstanceToPluginDispatcherMap::iterator found =
296 g_instance_to_plugin_dispatcher->find(instance);
297 if (found != g_instance_to_plugin_dispatcher->end()) {
[email protected]465faa22011-02-08 16:31:46298 DCHECK(found->second == this);
Daniel Bratelld5850cc2018-10-25 08:40:55299 g_instance_to_plugin_dispatcher->erase(found);
[email protected]465faa22011-02-08 16:31:46300 } else {
301 NOTREACHED();
302 }
303 }
[email protected]f56279c2011-02-02 18:12:31304}
305
306InstanceData* PluginDispatcher::GetInstanceData(PP_Instance instance) {
avi6e1b4e72016-12-29 22:02:57307 auto it = instance_map_.find(instance);
308 if (it == instance_map_.end())
309 return nullptr;
310 return it->second.get();
[email protected]f56279c2011-02-02 18:12:31311}
312
[email protected]4f2006122012-04-30 05:13:17313thunk::PPB_Instance_API* PluginDispatcher::GetInstanceAPI() {
314 return static_cast<PPB_Instance_Proxy*>(
315 GetInterfaceProxy(API_ID_PPB_INSTANCE));
316}
317
318thunk::ResourceCreationAPI* PluginDispatcher::GetResourceCreationAPI() {
319 return static_cast<ResourceCreationProxy*>(
320 GetInterfaceProxy(API_ID_RESOURCE_CREATION));
[email protected]6239d342011-05-06 22:55:47321}
322
[email protected]a08ebea2011-02-13 17:50:20323void PluginDispatcher::ForceFreeAllInstances() {
Daniel Bratelld5850cc2018-10-25 08:40:55324 if (!g_instance_to_plugin_dispatcher)
[email protected]4f15d2842011-02-15 17:36:33325 return;
326
327 // Iterating will remove each item from the map, so we need to make a copy
328 // to avoid things changing out from under is.
Daniel Bratelld5850cc2018-10-25 08:40:55329 InstanceToPluginDispatcherMap temp_map = *g_instance_to_plugin_dispatcher;
330 for (InstanceToPluginDispatcherMap::iterator i = temp_map.begin();
[email protected]4f15d2842011-02-15 17:36:33331 i != temp_map.end(); ++i) {
332 if (i->second == this) {
333 // Synthesize an "instance destroyed" message, this will notify the
334 // plugin and also remove it from our list of tracked plugins.
[email protected]ac4b54d2011-10-20 23:09:28335 PpapiMsg_PPPInstance_DidDestroy msg(API_ID_PPP_INSTANCE, i->first);
[email protected]4585fbc2011-06-13 17:17:56336 OnMessageReceived(msg);
[email protected]4f15d2842011-02-15 17:36:33337 }
338 }
[email protected]176c73922010-12-03 17:32:19339}
340
kareng1c62eeb2014-11-08 16:35:03341void PluginDispatcher::OnMsgSupportsInterface(
[email protected]465faa22011-02-08 16:31:46342 const std::string& interface_name,
343 bool* result) {
[email protected]5c966022011-09-13 18:09:37344 *result = !!GetPluginInterface(interface_name);
[email protected]e8f07ac2012-01-03 17:43:36345
346 // Do fallback for PPP_Instance. This is a hack here and if we have more
347 // cases like this it should be generalized. The PPP_Instance proxy always
348 // proxies the 1.1 interface, and then does fallback to 1.0 inside the
349 // plugin process (see PPP_Instance_Proxy). So here we return true for
350 // supporting the 1.1 interface if either 1.1 or 1.0 is supported.
351 if (!*result && interface_name == PPP_INSTANCE_INTERFACE)
352 *result = !!GetPluginInterface(PPP_INSTANCE_INTERFACE_1_0);
[email protected]465faa22011-02-08 16:31:46353}
354
[email protected]4d2efd22011-08-18 21:58:02355void PluginDispatcher::OnMsgSetPreferences(const Preferences& prefs) {
[email protected]208aad792011-05-26 19:05:28356 // The renderer may send us preferences more than once (currently this
357 // happens every time a new plugin instance is created). Since we don't have
358 // a way to signal to the plugin that the preferences have changed, changing
359 // the default fonts and such in the middle of a running plugin could be
360 // confusing to it. As a result, we never allow the preferences to be changed
361 // once they're set. The user will have to restart to get new font prefs
362 // propogated to plugins.
363 if (!received_preferences_) {
364 received_preferences_ = true;
365 preferences_ = prefs;
366 }
367}
368
[email protected]c2932f5e2010-11-03 03:22:33369} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02370} // namespace ppapi