blob: 9772a7c1e5e8525a6c4c4a999c2ff05ec77d0767 [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
[email protected]709a847e2010-11-10 01:16:1110#include "base/compiler_specific.h"
[email protected]c2932f5e2010-11-03 03:22:3311#include "base/logging.h"
[email protected]a76295972013-07-18 00:42:3212#include "base/message_loop/message_loop.h"
gab751416e1c2015-03-26 17:20:5813#include "base/metrics/histogram_macros.h"
primiano3ca22962015-01-30 17:08:3714#include "base/trace_event/trace_event.h"
avie029c4132015-12-23 06:45:2215#include "build/build_config.h"
[email protected]c2932f5e2010-11-03 03:22:3316#include "ipc/ipc_message.h"
17#include "ipc/ipc_sync_channel.h"
[email protected]83ad1c42013-05-01 16:56:2718#include "ipc/ipc_sync_message_filter.h"
[email protected]c2932f5e2010-11-03 03:22:3319#include "ppapi/c/pp_errors.h"
[email protected]e8f07ac2012-01-03 17:43:3620#include "ppapi/c/ppp_instance.h"
[email protected]a1686e272012-11-01 23:39:3421#include "ppapi/proxy/flash_clipboard_resource.h"
[email protected]0c92b0d2012-12-08 00:46:2322#include "ppapi/proxy/flash_file_resource.h"
[email protected]a76295972013-07-18 00:42:3223#include "ppapi/proxy/flash_resource.h"
[email protected]f5118812012-08-24 19:54:3024#include "ppapi/proxy/gamepad_resource.h"
[email protected]5c966022011-09-13 18:09:3725#include "ppapi/proxy/interface_list.h"
[email protected]c2932f5e2010-11-03 03:22:3326#include "ppapi/proxy/interface_proxy.h"
[email protected]60181162013-04-26 22:02:5327#include "ppapi/proxy/plugin_globals.h"
[email protected]2cc062242011-03-10 21:16:3428#include "ppapi/proxy/plugin_message_filter.h"
[email protected]6239d342011-05-06 22:55:4729#include "ppapi/proxy/plugin_resource_tracker.h"
[email protected]c2932f5e2010-11-03 03:22:3330#include "ppapi/proxy/plugin_var_serialization_rules.h"
31#include "ppapi/proxy/ppapi_messages.h"
[email protected]ceadc392011-06-15 23:04:2432#include "ppapi/proxy/ppb_instance_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3333#include "ppapi/proxy/ppp_class_proxy.h"
[email protected]6239d342011-05-06 22:55:4734#include "ppapi/proxy/resource_creation_proxy.h"
[email protected]511c58e2013-12-12 12:25:3335#include "ppapi/proxy/resource_reply_thread_registrar.h"
[email protected]eccf80312012-07-14 15:43:4236#include "ppapi/shared_impl/ppapi_globals.h"
[email protected]4c41d3f2012-02-15 01:44:4737#include "ppapi/shared_impl/proxy_lock.h"
[email protected]7f8b26b2011-08-18 15:41:0138#include "ppapi/shared_impl/resource.h"
[email protected]c2932f5e2010-11-03 03:22:3339
[email protected]4d2efd22011-08-18 21:58:0240namespace ppapi {
[email protected]c2932f5e2010-11-03 03:22:3341namespace proxy {
42
43namespace {
44
[email protected]465faa22011-02-08 16:31:4645typedef std::map<PP_Instance, PluginDispatcher*> InstanceToDispatcherMap;
46InstanceToDispatcherMap* g_instance_to_dispatcher = NULL;
[email protected]c2932f5e2010-11-03 03:22:3347
[email protected]a9b16dd2012-01-31 05:00:2648typedef std::set<PluginDispatcher*> DispatcherSet;
49DispatcherSet* g_live_dispatchers = NULL;
50
[email protected]c2932f5e2010-11-03 03:22:3351} // namespace
52
[email protected]06e0a342011-09-27 04:24:3053InstanceData::InstanceData()
[email protected]22fdaa62012-11-30 01:55:4454 : is_request_surrounding_text_pending(false),
[email protected]a8e72142012-08-21 17:24:2055 should_do_request_surrounding_text(false) {
[email protected]c7bf7452011-09-12 21:31:5056}
57
[email protected]0f41c012011-10-21 19:49:2058InstanceData::~InstanceData() {
59 // Run any pending mouse lock callback to prevent leaks.
[email protected]f0c86242013-06-02 21:25:4360 if (mouse_lock_callback.get())
[email protected]aed96532012-06-23 14:27:4261 mouse_lock_callback->Abort();
[email protected]0f41c012011-10-21 19:49:2062}
63
penghuang3634509a2015-06-15 15:06:4964InstanceData::FlushInfo::FlushInfo()
65 : flush_pending(false),
66 put_offset(0) {
67}
68
69InstanceData::FlushInfo::~FlushInfo() {
70}
71
Raymes Khoury982b7eb22017-07-26 09:40:2472PluginDispatcher::Sender::Sender(
73 base::WeakPtr<PluginDispatcher> plugin_dispatcher,
74 scoped_refptr<IPC::SyncMessageFilter> sync_filter)
75 : plugin_dispatcher_(plugin_dispatcher), sync_filter_(sync_filter) {}
76
77PluginDispatcher::Sender::~Sender() {}
78
79bool PluginDispatcher::Sender::SendMessage(IPC::Message* msg) {
80 // Currently we need to choose between two different mechanisms for sending.
81 // On the main thread we use the regular dispatch Send() method, on another
82 // thread we use SyncMessageFilter.
83 if (PpapiGlobals::Get()
84 ->GetMainThreadMessageLoop()
85 ->BelongsToCurrentThread()) {
86 // The PluginDispatcher may have been destroyed if the channel is gone, but
87 // resources are leaked and may still send messages. We ignore those
88 // messages. See crbug.com/725033.
89 if (plugin_dispatcher_) {
90 return plugin_dispatcher_.get()->Dispatcher::Send(msg);
91 } else {
92 delete msg;
93 return false;
94 }
95 }
96 return sync_filter_->Send(msg);
97}
98
99bool PluginDispatcher::Sender::Send(IPC::Message* msg) {
100 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::Send", "Class",
101 IPC_MESSAGE_ID_CLASS(msg->type()), "Line",
102 IPC_MESSAGE_ID_LINE(msg->type()));
103 // We always want plugin->renderer messages to arrive in-order. If some sync
104 // and some async messages are sent in response to a synchronous
105 // renderer->plugin call, the sync reply will be processed before the async
106 // reply, and everything will be confused.
107 //
108 // Allowing all async messages to unblock the renderer means more reentrancy
109 // there but gives correct ordering.
110 //
111 // We don't want reply messages to unblock however, as they will potentially
112 // end up on the wrong queue - see crbug.com/122443
113 if (!msg->is_reply())
114 msg->set_unblock(true);
115 if (msg->is_sync()) {
116 // Synchronous messages might be re-entrant, so we need to drop the lock.
117 ProxyAutoUnlock unlock;
118 SCOPED_UMA_HISTOGRAM_TIMER("Plugin.PpapiSyncIPCTime");
119 return SendMessage(msg);
120 }
121 return SendMessage(msg);
122}
123
[email protected]f0ecb552012-05-11 22:09:11124PluginDispatcher::PluginDispatcher(PP_GetInterface_Func get_interface,
[email protected]195d4cde2012-10-02 18:12:41125 const PpapiPermissions& permissions,
[email protected]bc2eeb42012-05-02 22:35:53126 bool incognito)
[email protected]195d4cde2012-10-02 18:12:41127 : Dispatcher(get_interface, permissions),
[email protected]208aad792011-05-26 19:05:28128 plugin_delegate_(NULL),
[email protected]373a95a2011-07-01 16:58:14129 received_preferences_(false),
[email protected]bc2eeb42012-05-02 22:35:53130 plugin_dispatcher_id_(0),
Raymes Khoury982b7eb22017-07-26 09:40:24131 incognito_(incognito),
132 sender_(
133 new Sender(AsWeakPtr(), scoped_refptr<IPC::SyncMessageFilter>())) {
[email protected]67600b92012-03-10 06:51:48134 SetSerializationRules(new PluginVarSerializationRules(AsWeakPtr()));
[email protected]a9b16dd2012-01-31 05:00:26135
136 if (!g_live_dispatchers)
137 g_live_dispatchers = new DispatcherSet;
138 g_live_dispatchers->insert(this);
[email protected]c2932f5e2010-11-03 03:22:33139}
140
141PluginDispatcher::~PluginDispatcher() {
[email protected]60181162013-04-26 22:02:53142 PluginGlobals::Get()->plugin_var_tracker()->DidDeleteDispatcher(this);
143
[email protected]373a95a2011-07-01 16:58:14144 if (plugin_delegate_)
145 plugin_delegate_->Unregister(plugin_dispatcher_id_);
[email protected]a9b16dd2012-01-31 05:00:26146
147 g_live_dispatchers->erase(this);
148 if (g_live_dispatchers->empty()) {
149 delete g_live_dispatchers;
150 g_live_dispatchers = NULL;
151 }
[email protected]c2932f5e2010-11-03 03:22:33152}
153
154// static
[email protected]4614f192011-01-21 00:26:43155PluginDispatcher* PluginDispatcher::GetForInstance(PP_Instance instance) {
[email protected]465faa22011-02-08 16:31:46156 if (!g_instance_to_dispatcher)
157 return NULL;
158 InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find(
159 instance);
160 if (found == g_instance_to_dispatcher->end())
161 return NULL;
162 return found->second;
[email protected]4614f192011-01-21 00:26:43163}
164
[email protected]a08ebea2011-02-13 17:50:20165// static
[email protected]7f8b26b2011-08-18 15:41:01166PluginDispatcher* PluginDispatcher::GetForResource(const Resource* resource) {
167 return GetForInstance(resource->pp_instance());
168}
169
170// static
[email protected]5c966022011-09-13 18:09:37171const void* PluginDispatcher::GetBrowserInterface(const char* interface_name) {
dmichaelfee3a512014-09-18 21:32:13172 // CAUTION: This function is called directly from the plugin, but we *don't*
173 // lock the ProxyLock to avoid excessive locking from C++ wrappers.
[email protected]5c966022011-09-13 18:09:37174 return InterfaceList::GetInstance()->GetInterfaceForPPB(interface_name);
175}
176
[email protected]a9b16dd2012-01-31 05:00:26177// static
178void PluginDispatcher::LogWithSource(PP_Instance instance,
[email protected]598816ad2012-12-13 01:34:32179 PP_LogLevel level,
[email protected]a9b16dd2012-01-31 05:00:26180 const std::string& source,
181 const std::string& value) {
182 if (!g_live_dispatchers || !g_instance_to_dispatcher)
183 return;
184
185 if (instance) {
186 InstanceToDispatcherMap::iterator found =
187 g_instance_to_dispatcher->find(instance);
188 if (found != g_instance_to_dispatcher->end()) {
189 // Send just to this specific dispatcher.
190 found->second->Send(new PpapiHostMsg_LogWithSource(
191 instance, static_cast<int>(level), source, value));
192 return;
193 }
194 }
195
196 // Instance 0 or invalid, send to all dispatchers.
197 for (DispatcherSet::iterator i = g_live_dispatchers->begin();
198 i != g_live_dispatchers->end(); ++i) {
199 (*i)->Send(new PpapiHostMsg_LogWithSource(
200 instance, static_cast<int>(level), source, value));
201 }
202}
203
[email protected]5c966022011-09-13 18:09:37204const void* PluginDispatcher::GetPluginInterface(
205 const std::string& interface_name) {
206 InterfaceMap::iterator found = plugin_interfaces_.find(interface_name);
207 if (found == plugin_interfaces_.end()) {
208 const void* ret = local_get_interface()(interface_name.c_str());
209 plugin_interfaces_.insert(std::make_pair(interface_name, ret));
210 return ret;
211 }
212 return found->second;
[email protected]a08ebea2011-02-13 17:50:20213}
214
[email protected]e2614c62011-04-16 22:12:45215bool PluginDispatcher::InitPluginWithChannel(
[email protected]d259a8e2011-05-18 22:31:09216 PluginDelegate* delegate,
[email protected]108fd342013-01-04 20:46:54217 base::ProcessId peer_pid,
[email protected]2cc062242011-03-10 21:16:34218 const IPC::ChannelHandle& channel_handle,
219 bool is_client) {
[email protected]f7b7eb7c2014-02-27 23:54:15220 if (!Dispatcher::InitWithChannel(delegate, peer_pid, channel_handle,
221 is_client))
222 return false;
[email protected]d259a8e2011-05-18 22:31:09223 plugin_delegate_ = delegate;
[email protected]373a95a2011-07-01 16:58:14224 plugin_dispatcher_id_ = plugin_delegate_->Register(this);
[email protected]2cc062242011-03-10 21:16:34225
Raymes Khoury982b7eb22017-07-26 09:40:24226 sender_ = new Sender(AsWeakPtr(), channel()->CreateSyncMessageFilter());
[email protected]83ad1c42013-05-01 16:56:27227
[email protected]2cc062242011-03-10 21:16:34228 // The message filter will intercept and process certain messages directly
229 // on the I/O thread.
230 channel()->AddFilter(
[email protected]511c58e2013-12-12 12:25:33231 new PluginMessageFilter(
232 delegate->GetGloballySeenInstanceIDSet(),
233 PluginGlobals::Get()->resource_reply_thread_registrar()));
[email protected]2cc062242011-03-10 21:16:34234 return true;
235}
236
[email protected]7cf40912010-12-09 18:25:03237bool PluginDispatcher::IsPlugin() const {
238 return true;
239}
240
[email protected]b00bbb32011-03-30 19:02:14241bool PluginDispatcher::Send(IPC::Message* msg) {
Raymes Khoury982b7eb22017-07-26 09:40:24242 return sender_->Send(msg);
[email protected]b00bbb32011-03-30 19:02:14243}
244
dmichael06b3b7f2015-04-03 05:22:26245bool PluginDispatcher::SendAndStayLocked(IPC::Message* msg) {
246 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::SendAndStayLocked",
247 "Class", IPC_MESSAGE_ID_CLASS(msg->type()),
248 "Line", IPC_MESSAGE_ID_LINE(msg->type()));
249 if (!msg->is_reply())
250 msg->set_unblock(true);
Raymes Khoury982b7eb22017-07-26 09:40:24251 return sender_->SendMessage(msg);
dmichael06b3b7f2015-04-03 05:22:26252}
253
[email protected]a95986a82010-12-24 06:19:28254bool PluginDispatcher::OnMessageReceived(const IPC::Message& msg) {
[email protected]4c41d3f2012-02-15 01:44:47255 // We need to grab the proxy lock to ensure that we don't collide with the
256 // plugin making pepper calls on a different thread.
257 ProxyAutoLock lock;
[email protected]366ae242011-05-10 02:23:58258 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::OnMessageReceived",
259 "Class", IPC_MESSAGE_ID_CLASS(msg.type()),
260 "Line", IPC_MESSAGE_ID_LINE(msg.type()));
[email protected]eccf80312012-07-14 15:43:42261
[email protected]c2932f5e2010-11-03 03:22:33262 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
263 // Handle some plugin-specific control messages.
[email protected]a95986a82010-12-24 06:19:28264 bool handled = true;
[email protected]c2932f5e2010-11-03 03:22:33265 IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg)
kareng1c62eeb2014-11-08 16:35:03266 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface)
[email protected]208aad792011-05-26 19:05:28267 IPC_MESSAGE_HANDLER(PpapiMsg_SetPreferences, OnMsgSetPreferences)
philipjc842a082016-01-27 03:36:11268 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]c2932f5e2010-11-03 03:22:33269 IPC_END_MESSAGE_MAP()
[email protected]5c966022011-09-13 18:09:37270 if (handled)
[email protected]37fe0362011-09-13 04:00:33271 return true;
[email protected]37fe0362011-09-13 04:00:33272 }
[email protected]5c966022011-09-13 18:09:37273 return Dispatcher::OnMessageReceived(msg);
[email protected]c2932f5e2010-11-03 03:22:33274}
275
[email protected]a08ebea2011-02-13 17:50:20276void PluginDispatcher::OnChannelError() {
[email protected]4f15d2842011-02-15 17:36:33277 Dispatcher::OnChannelError();
278
[email protected]4b417e52011-04-18 22:51:08279 // The renderer has crashed or exited. This channel and all instances
280 // associated with it are no longer valid.
[email protected]a08ebea2011-02-13 17:50:20281 ForceFreeAllInstances();
282 // TODO(brettw) free resources too!
283 delete this;
284}
285
[email protected]f56279c2011-02-02 18:12:31286void PluginDispatcher::DidCreateInstance(PP_Instance instance) {
[email protected]465faa22011-02-08 16:31:46287 if (!g_instance_to_dispatcher)
288 g_instance_to_dispatcher = new InstanceToDispatcherMap;
289 (*g_instance_to_dispatcher)[instance] = this;
Gyuyoung Kimb480aba2018-01-27 07:00:04290 instance_map_[instance] = std::make_unique<InstanceData>();
[email protected]f56279c2011-02-02 18:12:31291}
292
293void PluginDispatcher::DidDestroyInstance(PP_Instance instance) {
[email protected]4111b992014-05-15 17:11:20294 instance_map_.erase(instance);
[email protected]465faa22011-02-08 16:31:46295
296 if (g_instance_to_dispatcher) {
297 InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find(
298 instance);
299 if (found != g_instance_to_dispatcher->end()) {
300 DCHECK(found->second == this);
301 g_instance_to_dispatcher->erase(found);
302 } else {
303 NOTREACHED();
304 }
305 }
[email protected]f56279c2011-02-02 18:12:31306}
307
308InstanceData* PluginDispatcher::GetInstanceData(PP_Instance instance) {
avi6e1b4e72016-12-29 22:02:57309 auto it = instance_map_.find(instance);
310 if (it == instance_map_.end())
311 return nullptr;
312 return it->second.get();
[email protected]f56279c2011-02-02 18:12:31313}
314
[email protected]4f2006122012-04-30 05:13:17315thunk::PPB_Instance_API* PluginDispatcher::GetInstanceAPI() {
316 return static_cast<PPB_Instance_Proxy*>(
317 GetInterfaceProxy(API_ID_PPB_INSTANCE));
318}
319
320thunk::ResourceCreationAPI* PluginDispatcher::GetResourceCreationAPI() {
321 return static_cast<ResourceCreationProxy*>(
322 GetInterfaceProxy(API_ID_RESOURCE_CREATION));
[email protected]6239d342011-05-06 22:55:47323}
324
[email protected]a08ebea2011-02-13 17:50:20325void PluginDispatcher::ForceFreeAllInstances() {
[email protected]4f15d2842011-02-15 17:36:33326 if (!g_instance_to_dispatcher)
327 return;
328
329 // Iterating will remove each item from the map, so we need to make a copy
330 // to avoid things changing out from under is.
331 InstanceToDispatcherMap temp_map = *g_instance_to_dispatcher;
332 for (InstanceToDispatcherMap::iterator i = temp_map.begin();
333 i != temp_map.end(); ++i) {
334 if (i->second == this) {
335 // Synthesize an "instance destroyed" message, this will notify the
336 // plugin and also remove it from our list of tracked plugins.
[email protected]ac4b54d2011-10-20 23:09:28337 PpapiMsg_PPPInstance_DidDestroy msg(API_ID_PPP_INSTANCE, i->first);
[email protected]4585fbc2011-06-13 17:17:56338 OnMessageReceived(msg);
[email protected]4f15d2842011-02-15 17:36:33339 }
340 }
[email protected]176c73922010-12-03 17:32:19341}
342
kareng1c62eeb2014-11-08 16:35:03343void PluginDispatcher::OnMsgSupportsInterface(
[email protected]465faa22011-02-08 16:31:46344 const std::string& interface_name,
345 bool* result) {
[email protected]5c966022011-09-13 18:09:37346 *result = !!GetPluginInterface(interface_name);
[email protected]e8f07ac2012-01-03 17:43:36347
348 // Do fallback for PPP_Instance. This is a hack here and if we have more
349 // cases like this it should be generalized. The PPP_Instance proxy always
350 // proxies the 1.1 interface, and then does fallback to 1.0 inside the
351 // plugin process (see PPP_Instance_Proxy). So here we return true for
352 // supporting the 1.1 interface if either 1.1 or 1.0 is supported.
353 if (!*result && interface_name == PPP_INSTANCE_INTERFACE)
354 *result = !!GetPluginInterface(PPP_INSTANCE_INTERFACE_1_0);
[email protected]465faa22011-02-08 16:31:46355}
356
[email protected]4d2efd22011-08-18 21:58:02357void PluginDispatcher::OnMsgSetPreferences(const Preferences& prefs) {
[email protected]208aad792011-05-26 19:05:28358 // The renderer may send us preferences more than once (currently this
359 // happens every time a new plugin instance is created). Since we don't have
360 // a way to signal to the plugin that the preferences have changed, changing
361 // the default fonts and such in the middle of a running plugin could be
362 // confusing to it. As a result, we never allow the preferences to be changed
363 // once they're set. The user will have to restart to get new font prefs
364 // propogated to plugins.
365 if (!received_preferences_) {
366 received_preferences_ = true;
367 preferences_ = prefs;
368 }
369}
370
[email protected]c2932f5e2010-11-03 03:22:33371} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02372} // namespace ppapi