Make castv2 performance test work.
[chromium-blink-merge.git] / ppapi / proxy / plugin_dispatcher.cc
blobfb3c23e9e50e56e3fda1baba0587f3fbab15a8fd
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"
7 #include <map>
9 #include "base/compiler_specific.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/metrics/histogram_macros.h"
13 #include "base/trace_event/trace_event.h"
14 #include "ipc/ipc_message.h"
15 #include "ipc/ipc_sync_channel.h"
16 #include "ipc/ipc_sync_message_filter.h"
17 #include "ppapi/c/pp_errors.h"
18 #include "ppapi/c/ppp_instance.h"
19 #include "ppapi/proxy/flash_clipboard_resource.h"
20 #include "ppapi/proxy/flash_file_resource.h"
21 #include "ppapi/proxy/flash_resource.h"
22 #include "ppapi/proxy/gamepad_resource.h"
23 #include "ppapi/proxy/interface_list.h"
24 #include "ppapi/proxy/interface_proxy.h"
25 #include "ppapi/proxy/plugin_globals.h"
26 #include "ppapi/proxy/plugin_message_filter.h"
27 #include "ppapi/proxy/plugin_resource_tracker.h"
28 #include "ppapi/proxy/plugin_var_serialization_rules.h"
29 #include "ppapi/proxy/ppapi_messages.h"
30 #include "ppapi/proxy/ppb_instance_proxy.h"
31 #include "ppapi/proxy/ppp_class_proxy.h"
32 #include "ppapi/proxy/resource_creation_proxy.h"
33 #include "ppapi/proxy/resource_reply_thread_registrar.h"
34 #include "ppapi/shared_impl/ppapi_globals.h"
35 #include "ppapi/shared_impl/proxy_lock.h"
36 #include "ppapi/shared_impl/resource.h"
38 #if defined(OS_POSIX) && !defined(OS_NACL)
39 #include "ipc/ipc_channel_posix.h"
40 #endif
42 namespace ppapi {
43 namespace proxy {
45 namespace {
47 typedef std::map<PP_Instance, PluginDispatcher*> InstanceToDispatcherMap;
48 InstanceToDispatcherMap* g_instance_to_dispatcher = NULL;
50 typedef std::set<PluginDispatcher*> DispatcherSet;
51 DispatcherSet* g_live_dispatchers = NULL;
53 } // namespace
55 InstanceData::InstanceData()
56 : is_request_surrounding_text_pending(false),
57 should_do_request_surrounding_text(false) {
60 InstanceData::~InstanceData() {
61 // Run any pending mouse lock callback to prevent leaks.
62 if (mouse_lock_callback.get())
63 mouse_lock_callback->Abort();
66 PluginDispatcher::PluginDispatcher(PP_GetInterface_Func get_interface,
67 const PpapiPermissions& permissions,
68 bool incognito)
69 : Dispatcher(get_interface, permissions),
70 plugin_delegate_(NULL),
71 received_preferences_(false),
72 plugin_dispatcher_id_(0),
73 incognito_(incognito) {
74 SetSerializationRules(new PluginVarSerializationRules(AsWeakPtr()));
76 if (!g_live_dispatchers)
77 g_live_dispatchers = new DispatcherSet;
78 g_live_dispatchers->insert(this);
81 PluginDispatcher::~PluginDispatcher() {
82 PluginGlobals::Get()->plugin_var_tracker()->DidDeleteDispatcher(this);
84 if (plugin_delegate_)
85 plugin_delegate_->Unregister(plugin_dispatcher_id_);
87 g_live_dispatchers->erase(this);
88 if (g_live_dispatchers->empty()) {
89 delete g_live_dispatchers;
90 g_live_dispatchers = NULL;
94 // static
95 PluginDispatcher* PluginDispatcher::GetForInstance(PP_Instance instance) {
96 if (!g_instance_to_dispatcher)
97 return NULL;
98 InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find(
99 instance);
100 if (found == g_instance_to_dispatcher->end())
101 return NULL;
102 return found->second;
105 // static
106 PluginDispatcher* PluginDispatcher::GetForResource(const Resource* resource) {
107 return GetForInstance(resource->pp_instance());
110 // static
111 const void* PluginDispatcher::GetBrowserInterface(const char* interface_name) {
112 // CAUTION: This function is called directly from the plugin, but we *don't*
113 // lock the ProxyLock to avoid excessive locking from C++ wrappers.
114 return InterfaceList::GetInstance()->GetInterfaceForPPB(interface_name);
117 // static
118 void PluginDispatcher::LogWithSource(PP_Instance instance,
119 PP_LogLevel level,
120 const std::string& source,
121 const std::string& value) {
122 if (!g_live_dispatchers || !g_instance_to_dispatcher)
123 return;
125 if (instance) {
126 InstanceToDispatcherMap::iterator found =
127 g_instance_to_dispatcher->find(instance);
128 if (found != g_instance_to_dispatcher->end()) {
129 // Send just to this specific dispatcher.
130 found->second->Send(new PpapiHostMsg_LogWithSource(
131 instance, static_cast<int>(level), source, value));
132 return;
136 // Instance 0 or invalid, send to all dispatchers.
137 for (DispatcherSet::iterator i = g_live_dispatchers->begin();
138 i != g_live_dispatchers->end(); ++i) {
139 (*i)->Send(new PpapiHostMsg_LogWithSource(
140 instance, static_cast<int>(level), source, value));
144 const void* PluginDispatcher::GetPluginInterface(
145 const std::string& interface_name) {
146 InterfaceMap::iterator found = plugin_interfaces_.find(interface_name);
147 if (found == plugin_interfaces_.end()) {
148 const void* ret = local_get_interface()(interface_name.c_str());
149 plugin_interfaces_.insert(std::make_pair(interface_name, ret));
150 return ret;
152 return found->second;
155 bool PluginDispatcher::InitPluginWithChannel(
156 PluginDelegate* delegate,
157 base::ProcessId peer_pid,
158 const IPC::ChannelHandle& channel_handle,
159 bool is_client) {
160 if (!Dispatcher::InitWithChannel(delegate, peer_pid, channel_handle,
161 is_client))
162 return false;
163 plugin_delegate_ = delegate;
164 plugin_dispatcher_id_ = plugin_delegate_->Register(this);
166 sync_filter_ = new IPC::SyncMessageFilter(delegate->GetShutdownEvent());
167 channel()->AddFilter(sync_filter_.get());
169 // The message filter will intercept and process certain messages directly
170 // on the I/O thread.
171 channel()->AddFilter(
172 new PluginMessageFilter(
173 delegate->GetGloballySeenInstanceIDSet(),
174 PluginGlobals::Get()->resource_reply_thread_registrar()));
175 return true;
178 bool PluginDispatcher::IsPlugin() const {
179 return true;
182 bool PluginDispatcher::SendMessage(IPC::Message* msg) {
183 // Currently we need to choose between two different mechanisms for sending.
184 // On the main thread we use the regular dispatch Send() method, on another
185 // thread we use SyncMessageFilter.
186 if (PpapiGlobals::Get()->GetMainThreadMessageLoop()->BelongsToCurrentThread())
187 return Dispatcher::Send(msg);
188 return sync_filter_->Send(msg);
191 bool PluginDispatcher::Send(IPC::Message* msg) {
192 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::Send",
193 "Class", IPC_MESSAGE_ID_CLASS(msg->type()),
194 "Line", IPC_MESSAGE_ID_LINE(msg->type()));
195 // We always want plugin->renderer messages to arrive in-order. If some sync
196 // and some async messages are sent in response to a synchronous
197 // renderer->plugin call, the sync reply will be processed before the async
198 // reply, and everything will be confused.
200 // Allowing all async messages to unblock the renderer means more reentrancy
201 // there but gives correct ordering.
203 // We don't want reply messages to unblock however, as they will potentially
204 // end up on the wrong queue - see crbug.com/122443
205 if (!msg->is_reply())
206 msg->set_unblock(true);
207 if (msg->is_sync()) {
208 // Synchronous messages might be re-entrant, so we need to drop the lock.
209 ProxyAutoUnlock unlock;
210 SCOPED_UMA_HISTOGRAM_TIMER("Plugin.PpapiSyncIPCTime");
211 return SendMessage(msg);
213 return SendMessage(msg);
216 bool PluginDispatcher::SendAndStayLocked(IPC::Message* msg) {
217 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::SendAndStayLocked",
218 "Class", IPC_MESSAGE_ID_CLASS(msg->type()),
219 "Line", IPC_MESSAGE_ID_LINE(msg->type()));
220 if (!msg->is_reply())
221 msg->set_unblock(true);
222 return SendMessage(msg);
225 bool PluginDispatcher::OnMessageReceived(const IPC::Message& msg) {
226 // We need to grab the proxy lock to ensure that we don't collide with the
227 // plugin making pepper calls on a different thread.
228 ProxyAutoLock lock;
229 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::OnMessageReceived",
230 "Class", IPC_MESSAGE_ID_CLASS(msg.type()),
231 "Line", IPC_MESSAGE_ID_LINE(msg.type()));
233 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
234 // Handle some plugin-specific control messages.
235 bool handled = true;
236 IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg)
237 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface)
238 IPC_MESSAGE_HANDLER(PpapiMsg_SetPreferences, OnMsgSetPreferences)
239 IPC_MESSAGE_UNHANDLED(handled = false);
240 IPC_END_MESSAGE_MAP()
241 if (handled)
242 return true;
244 return Dispatcher::OnMessageReceived(msg);
247 void PluginDispatcher::OnChannelError() {
248 Dispatcher::OnChannelError();
250 // The renderer has crashed or exited. This channel and all instances
251 // associated with it are no longer valid.
252 ForceFreeAllInstances();
253 // TODO(brettw) free resources too!
254 delete this;
257 void PluginDispatcher::DidCreateInstance(PP_Instance instance) {
258 if (!g_instance_to_dispatcher)
259 g_instance_to_dispatcher = new InstanceToDispatcherMap;
260 (*g_instance_to_dispatcher)[instance] = this;
261 instance_map_.set(instance, scoped_ptr<InstanceData>(new InstanceData()));
264 void PluginDispatcher::DidDestroyInstance(PP_Instance instance) {
265 instance_map_.erase(instance);
267 if (g_instance_to_dispatcher) {
268 InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find(
269 instance);
270 if (found != g_instance_to_dispatcher->end()) {
271 DCHECK(found->second == this);
272 g_instance_to_dispatcher->erase(found);
273 } else {
274 NOTREACHED();
279 InstanceData* PluginDispatcher::GetInstanceData(PP_Instance instance) {
280 return instance_map_.get(instance);
283 thunk::PPB_Instance_API* PluginDispatcher::GetInstanceAPI() {
284 return static_cast<PPB_Instance_Proxy*>(
285 GetInterfaceProxy(API_ID_PPB_INSTANCE));
288 thunk::ResourceCreationAPI* PluginDispatcher::GetResourceCreationAPI() {
289 return static_cast<ResourceCreationProxy*>(
290 GetInterfaceProxy(API_ID_RESOURCE_CREATION));
293 void PluginDispatcher::ForceFreeAllInstances() {
294 if (!g_instance_to_dispatcher)
295 return;
297 // Iterating will remove each item from the map, so we need to make a copy
298 // to avoid things changing out from under is.
299 InstanceToDispatcherMap temp_map = *g_instance_to_dispatcher;
300 for (InstanceToDispatcherMap::iterator i = temp_map.begin();
301 i != temp_map.end(); ++i) {
302 if (i->second == this) {
303 // Synthesize an "instance destroyed" message, this will notify the
304 // plugin and also remove it from our list of tracked plugins.
305 PpapiMsg_PPPInstance_DidDestroy msg(API_ID_PPP_INSTANCE, i->first);
306 OnMessageReceived(msg);
311 void PluginDispatcher::OnMsgSupportsInterface(
312 const std::string& interface_name,
313 bool* result) {
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;
339 } // namespace proxy
340 } // namespace ppapi