blob: efe0803320ebb212c67787966827556a49b24df5 [file] [log] [blame]
[email protected]a08ebea2011-02-13 17:50:201// Copyright (c) 2011 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]aabfa652011-07-13 23:28:4411#include "base/message_loop.h"
[email protected]c2932f5e2010-11-03 03:22:3312#include "ipc/ipc_message.h"
13#include "ipc/ipc_sync_channel.h"
[email protected]366ae242011-05-10 02:23:5814#include "base/debug/trace_event.h"
[email protected]c2932f5e2010-11-03 03:22:3315#include "ppapi/c/pp_errors.h"
[email protected]e8f07ac2012-01-03 17:43:3616#include "ppapi/c/ppp_instance.h"
[email protected]5c966022011-09-13 18:09:3717#include "ppapi/proxy/interface_list.h"
[email protected]c2932f5e2010-11-03 03:22:3318#include "ppapi/proxy/interface_proxy.h"
[email protected]2cc062242011-03-10 21:16:3419#include "ppapi/proxy/plugin_message_filter.h"
[email protected]6239d342011-05-06 22:55:4720#include "ppapi/proxy/plugin_resource_tracker.h"
[email protected]c2932f5e2010-11-03 03:22:3321#include "ppapi/proxy/plugin_var_serialization_rules.h"
22#include "ppapi/proxy/ppapi_messages.h"
[email protected]9815108e2011-05-27 21:50:2823#include "ppapi/proxy/ppb_cursor_control_proxy.h"
[email protected]d259a8e2011-05-18 22:31:0924#include "ppapi/proxy/ppb_font_proxy.h"
[email protected]ceadc392011-06-15 23:04:2425#include "ppapi/proxy/ppb_instance_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3326#include "ppapi/proxy/ppp_class_proxy.h"
[email protected]6239d342011-05-06 22:55:4727#include "ppapi/proxy/resource_creation_proxy.h"
[email protected]7f8b26b2011-08-18 15:41:0128#include "ppapi/shared_impl/resource.h"
[email protected]c2932f5e2010-11-03 03:22:3329
[email protected]a08ebea2011-02-13 17:50:2030#if defined(OS_POSIX)
31#include "base/eintr_wrapper.h"
32#include "ipc/ipc_channel_posix.h"
33#endif
34
[email protected]4d2efd22011-08-18 21:58:0235namespace ppapi {
[email protected]c2932f5e2010-11-03 03:22:3336namespace proxy {
37
38namespace {
39
[email protected]465faa22011-02-08 16:31:4640typedef std::map<PP_Instance, PluginDispatcher*> InstanceToDispatcherMap;
41InstanceToDispatcherMap* g_instance_to_dispatcher = NULL;
[email protected]c2932f5e2010-11-03 03:22:3342
[email protected]a9b16dd2012-01-31 05:00:2643typedef std::set<PluginDispatcher*> DispatcherSet;
44DispatcherSet* g_live_dispatchers = NULL;
45
[email protected]c2932f5e2010-11-03 03:22:3346} // namespace
47
[email protected]06e0a342011-09-27 04:24:3048InstanceData::InstanceData()
[email protected]e8f07ac2012-01-03 17:43:3649 : flash_fullscreen(PP_FALSE),
[email protected]0f41c012011-10-21 19:49:2050 mouse_lock_callback(PP_BlockUntilComplete()) {
[email protected]c7bf7452011-09-12 21:31:5051}
52
[email protected]0f41c012011-10-21 19:49:2053InstanceData::~InstanceData() {
54 // Run any pending mouse lock callback to prevent leaks.
55 if (mouse_lock_callback.func)
56 PP_RunAndClearCompletionCallback(&mouse_lock_callback, PP_ERROR_ABORTED);
57}
58
[email protected]5d84d012010-12-02 17:17:2159PluginDispatcher::PluginDispatcher(base::ProcessHandle remote_process_handle,
[email protected]a08ebea2011-02-13 17:50:2060 GetInterfaceFunc get_interface)
[email protected]d259a8e2011-05-18 22:31:0961 : Dispatcher(remote_process_handle, get_interface),
[email protected]208aad792011-05-26 19:05:2862 plugin_delegate_(NULL),
[email protected]373a95a2011-07-01 16:58:1463 received_preferences_(false),
64 plugin_dispatcher_id_(0) {
[email protected]4614f192011-01-21 00:26:4365 SetSerializationRules(new PluginVarSerializationRules);
[email protected]a9b16dd2012-01-31 05:00:2666
67 if (!g_live_dispatchers)
68 g_live_dispatchers = new DispatcherSet;
69 g_live_dispatchers->insert(this);
[email protected]c2932f5e2010-11-03 03:22:3370}
71
72PluginDispatcher::~PluginDispatcher() {
[email protected]373a95a2011-07-01 16:58:1473 if (plugin_delegate_)
74 plugin_delegate_->Unregister(plugin_dispatcher_id_);
[email protected]a9b16dd2012-01-31 05:00:2675
76 g_live_dispatchers->erase(this);
77 if (g_live_dispatchers->empty()) {
78 delete g_live_dispatchers;
79 g_live_dispatchers = NULL;
80 }
[email protected]c2932f5e2010-11-03 03:22:3381}
82
83// static
[email protected]4614f192011-01-21 00:26:4384PluginDispatcher* PluginDispatcher::GetForInstance(PP_Instance instance) {
[email protected]465faa22011-02-08 16:31:4685 if (!g_instance_to_dispatcher)
86 return NULL;
87 InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find(
88 instance);
89 if (found == g_instance_to_dispatcher->end())
90 return NULL;
91 return found->second;
[email protected]4614f192011-01-21 00:26:4392}
93
[email protected]a08ebea2011-02-13 17:50:2094// static
[email protected]7f8b26b2011-08-18 15:41:0195PluginDispatcher* PluginDispatcher::GetForResource(const Resource* resource) {
96 return GetForInstance(resource->pp_instance());
97}
98
99// static
[email protected]5c966022011-09-13 18:09:37100const void* PluginDispatcher::GetBrowserInterface(const char* interface_name) {
101 return InterfaceList::GetInstance()->GetInterfaceForPPB(interface_name);
102}
103
[email protected]a9b16dd2012-01-31 05:00:26104// static
105void PluginDispatcher::LogWithSource(PP_Instance instance,
106 PP_LogLevel_Dev level,
107 const std::string& source,
108 const std::string& value) {
109 if (!g_live_dispatchers || !g_instance_to_dispatcher)
110 return;
111
112 if (instance) {
113 InstanceToDispatcherMap::iterator found =
114 g_instance_to_dispatcher->find(instance);
115 if (found != g_instance_to_dispatcher->end()) {
116 // Send just to this specific dispatcher.
117 found->second->Send(new PpapiHostMsg_LogWithSource(
118 instance, static_cast<int>(level), source, value));
119 return;
120 }
121 }
122
123 // Instance 0 or invalid, send to all dispatchers.
124 for (DispatcherSet::iterator i = g_live_dispatchers->begin();
125 i != g_live_dispatchers->end(); ++i) {
126 (*i)->Send(new PpapiHostMsg_LogWithSource(
127 instance, static_cast<int>(level), source, value));
128 }
129}
130
[email protected]5c966022011-09-13 18:09:37131const void* PluginDispatcher::GetPluginInterface(
132 const std::string& interface_name) {
133 InterfaceMap::iterator found = plugin_interfaces_.find(interface_name);
134 if (found == plugin_interfaces_.end()) {
135 const void* ret = local_get_interface()(interface_name.c_str());
136 plugin_interfaces_.insert(std::make_pair(interface_name, ret));
137 return ret;
138 }
139 return found->second;
[email protected]a08ebea2011-02-13 17:50:20140}
141
[email protected]e2614c62011-04-16 22:12:45142bool PluginDispatcher::InitPluginWithChannel(
[email protected]d259a8e2011-05-18 22:31:09143 PluginDelegate* delegate,
[email protected]2cc062242011-03-10 21:16:34144 const IPC::ChannelHandle& channel_handle,
145 bool is_client) {
146 if (!Dispatcher::InitWithChannel(delegate, channel_handle, is_client))
147 return false;
[email protected]d259a8e2011-05-18 22:31:09148 plugin_delegate_ = delegate;
[email protected]373a95a2011-07-01 16:58:14149 plugin_dispatcher_id_ = plugin_delegate_->Register(this);
[email protected]2cc062242011-03-10 21:16:34150
151 // The message filter will intercept and process certain messages directly
152 // on the I/O thread.
153 channel()->AddFilter(
154 new PluginMessageFilter(delegate->GetGloballySeenInstanceIDSet()));
155 return true;
156}
157
[email protected]7cf40912010-12-09 18:25:03158bool PluginDispatcher::IsPlugin() const {
159 return true;
160}
161
[email protected]b00bbb32011-03-30 19:02:14162bool PluginDispatcher::Send(IPC::Message* msg) {
[email protected]366ae242011-05-10 02:23:58163 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::Send",
164 "Class", IPC_MESSAGE_ID_CLASS(msg->type()),
165 "Line", IPC_MESSAGE_ID_LINE(msg->type()));
[email protected]b00bbb32011-03-30 19:02:14166 // We always want plugin->renderer messages to arrive in-order. If some sync
167 // and some async messages are send in response to a synchronous
168 // renderer->plugin call, the sync reply will be processed before the async
169 // reply, and everything will be confused.
170 //
171 // Allowing all async messages to unblock the renderer means more reentrancy
172 // there but gives correct ordering.
173 msg->set_unblock(true);
174 return Dispatcher::Send(msg);
175}
176
[email protected]a95986a82010-12-24 06:19:28177bool PluginDispatcher::OnMessageReceived(const IPC::Message& msg) {
[email protected]366ae242011-05-10 02:23:58178 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::OnMessageReceived",
179 "Class", IPC_MESSAGE_ID_CLASS(msg.type()),
180 "Line", IPC_MESSAGE_ID_LINE(msg.type()));
[email protected]c2932f5e2010-11-03 03:22:33181 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
182 // Handle some plugin-specific control messages.
[email protected]a95986a82010-12-24 06:19:28183 bool handled = true;
[email protected]c2932f5e2010-11-03 03:22:33184 IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg)
[email protected]465faa22011-02-08 16:31:46185 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface)
[email protected]208aad792011-05-26 19:05:28186 IPC_MESSAGE_HANDLER(PpapiMsg_SetPreferences, OnMsgSetPreferences)
[email protected]5c966022011-09-13 18:09:37187 IPC_MESSAGE_UNHANDLED(handled = false);
[email protected]c2932f5e2010-11-03 03:22:33188 IPC_END_MESSAGE_MAP()
[email protected]5c966022011-09-13 18:09:37189 if (handled)
[email protected]37fe0362011-09-13 04:00:33190 return true;
[email protected]37fe0362011-09-13 04:00:33191 }
[email protected]5c966022011-09-13 18:09:37192 return Dispatcher::OnMessageReceived(msg);
[email protected]c2932f5e2010-11-03 03:22:33193}
194
[email protected]a08ebea2011-02-13 17:50:20195void PluginDispatcher::OnChannelError() {
[email protected]4f15d2842011-02-15 17:36:33196 Dispatcher::OnChannelError();
197
[email protected]4b417e52011-04-18 22:51:08198 // The renderer has crashed or exited. This channel and all instances
199 // associated with it are no longer valid.
[email protected]a08ebea2011-02-13 17:50:20200 ForceFreeAllInstances();
201 // TODO(brettw) free resources too!
202 delete this;
203}
204
[email protected]f56279c2011-02-02 18:12:31205void PluginDispatcher::DidCreateInstance(PP_Instance instance) {
[email protected]465faa22011-02-08 16:31:46206 if (!g_instance_to_dispatcher)
207 g_instance_to_dispatcher = new InstanceToDispatcherMap;
208 (*g_instance_to_dispatcher)[instance] = this;
209
[email protected]f56279c2011-02-02 18:12:31210 instance_map_[instance] = InstanceData();
211}
212
213void PluginDispatcher::DidDestroyInstance(PP_Instance instance) {
214 InstanceDataMap::iterator it = instance_map_.find(instance);
215 if (it != instance_map_.end())
216 instance_map_.erase(it);
[email protected]465faa22011-02-08 16:31:46217
218 if (g_instance_to_dispatcher) {
219 InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find(
220 instance);
221 if (found != g_instance_to_dispatcher->end()) {
222 DCHECK(found->second == this);
223 g_instance_to_dispatcher->erase(found);
224 } else {
225 NOTREACHED();
226 }
227 }
[email protected]f56279c2011-02-02 18:12:31228}
229
230InstanceData* PluginDispatcher::GetInstanceData(PP_Instance instance) {
231 InstanceDataMap::iterator it = instance_map_.find(instance);
232 return (it == instance_map_.end()) ? NULL : &it->second;
233}
234
[email protected]ac4b54d2011-10-20 23:09:28235FunctionGroupBase* PluginDispatcher::GetFunctionAPI(ApiID id) {
[email protected]5c966022011-09-13 18:09:37236 return GetInterfaceProxy(id);
[email protected]6239d342011-05-06 22:55:47237}
238
[email protected]a08ebea2011-02-13 17:50:20239void PluginDispatcher::ForceFreeAllInstances() {
[email protected]4f15d2842011-02-15 17:36:33240 if (!g_instance_to_dispatcher)
241 return;
242
243 // Iterating will remove each item from the map, so we need to make a copy
244 // to avoid things changing out from under is.
245 InstanceToDispatcherMap temp_map = *g_instance_to_dispatcher;
246 for (InstanceToDispatcherMap::iterator i = temp_map.begin();
247 i != temp_map.end(); ++i) {
248 if (i->second == this) {
249 // Synthesize an "instance destroyed" message, this will notify the
250 // plugin and also remove it from our list of tracked plugins.
[email protected]ac4b54d2011-10-20 23:09:28251 PpapiMsg_PPPInstance_DidDestroy msg(API_ID_PPP_INSTANCE, i->first);
[email protected]4585fbc2011-06-13 17:17:56252 OnMessageReceived(msg);
[email protected]4f15d2842011-02-15 17:36:33253 }
254 }
[email protected]176c73922010-12-03 17:32:19255}
256
[email protected]465faa22011-02-08 16:31:46257void PluginDispatcher::OnMsgSupportsInterface(
258 const std::string& interface_name,
259 bool* result) {
[email protected]5c966022011-09-13 18:09:37260 *result = !!GetPluginInterface(interface_name);
[email protected]e8f07ac2012-01-03 17:43:36261
262 // Do fallback for PPP_Instance. This is a hack here and if we have more
263 // cases like this it should be generalized. The PPP_Instance proxy always
264 // proxies the 1.1 interface, and then does fallback to 1.0 inside the
265 // plugin process (see PPP_Instance_Proxy). So here we return true for
266 // supporting the 1.1 interface if either 1.1 or 1.0 is supported.
267 if (!*result && interface_name == PPP_INSTANCE_INTERFACE)
268 *result = !!GetPluginInterface(PPP_INSTANCE_INTERFACE_1_0);
[email protected]465faa22011-02-08 16:31:46269}
270
[email protected]4d2efd22011-08-18 21:58:02271void PluginDispatcher::OnMsgSetPreferences(const Preferences& prefs) {
[email protected]208aad792011-05-26 19:05:28272 // The renderer may send us preferences more than once (currently this
273 // happens every time a new plugin instance is created). Since we don't have
274 // a way to signal to the plugin that the preferences have changed, changing
275 // the default fonts and such in the middle of a running plugin could be
276 // confusing to it. As a result, we never allow the preferences to be changed
277 // once they're set. The user will have to restart to get new font prefs
278 // propogated to plugins.
279 if (!received_preferences_) {
280 received_preferences_ = true;
281 preferences_ = prefs;
282 }
283}
284
[email protected]c2932f5e2010-11-03 03:22:33285} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02286} // namespace ppapi