Use IsRecording() instead of did_initiate_recording_ in TraceHandler::RequestMemoryDump
[chromium-blink-merge.git] / ppapi / proxy / plugin_dispatcher.cc
blob80fec1f60fcbd08f982e549e5af5abe7f8f9d754
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 InstanceData::FlushInfo::FlushInfo()
67 : flush_pending(false),
68 put_offset(0) {
71 InstanceData::FlushInfo::~FlushInfo() {
74 PluginDispatcher::PluginDispatcher(PP_GetInterface_Func get_interface,
75 const PpapiPermissions& permissions,
76 bool incognito)
77 : Dispatcher(get_interface, permissions),
78 plugin_delegate_(NULL),
79 received_preferences_(false),
80 plugin_dispatcher_id_(0),
81 incognito_(incognito) {
82 SetSerializationRules(new PluginVarSerializationRules(AsWeakPtr()));
84 if (!g_live_dispatchers)
85 g_live_dispatchers = new DispatcherSet;
86 g_live_dispatchers->insert(this);
89 PluginDispatcher::~PluginDispatcher() {
90 PluginGlobals::Get()->plugin_var_tracker()->DidDeleteDispatcher(this);
92 if (plugin_delegate_)
93 plugin_delegate_->Unregister(plugin_dispatcher_id_);
95 g_live_dispatchers->erase(this);
96 if (g_live_dispatchers->empty()) {
97 delete g_live_dispatchers;
98 g_live_dispatchers = NULL;
102 // static
103 PluginDispatcher* PluginDispatcher::GetForInstance(PP_Instance instance) {
104 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;
113 // static
114 PluginDispatcher* PluginDispatcher::GetForResource(const Resource* resource) {
115 return GetForInstance(resource->pp_instance());
118 // static
119 const void* PluginDispatcher::GetBrowserInterface(const char* interface_name) {
120 // CAUTION: This function is called directly from the plugin, but we *don't*
121 // lock the ProxyLock to avoid excessive locking from C++ wrappers.
122 return InterfaceList::GetInstance()->GetInterfaceForPPB(interface_name);
125 // static
126 void PluginDispatcher::LogWithSource(PP_Instance instance,
127 PP_LogLevel level,
128 const std::string& source,
129 const std::string& value) {
130 if (!g_live_dispatchers || !g_instance_to_dispatcher)
131 return;
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;
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));
152 const 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;
160 return found->second;
163 bool PluginDispatcher::InitPluginWithChannel(
164 PluginDelegate* delegate,
165 base::ProcessId peer_pid,
166 const IPC::ChannelHandle& channel_handle,
167 bool is_client) {
168 if (!Dispatcher::InitWithChannel(delegate, peer_pid, channel_handle,
169 is_client))
170 return false;
171 plugin_delegate_ = delegate;
172 plugin_dispatcher_id_ = plugin_delegate_->Register(this);
174 sync_filter_ = channel()->CreateSyncMessageFilter();
176 // The message filter will intercept and process certain messages directly
177 // on the I/O thread.
178 channel()->AddFilter(
179 new PluginMessageFilter(
180 delegate->GetGloballySeenInstanceIDSet(),
181 PluginGlobals::Get()->resource_reply_thread_registrar()));
182 return true;
185 bool PluginDispatcher::IsPlugin() const {
186 return true;
189 bool PluginDispatcher::SendMessage(IPC::Message* msg) {
190 // Currently we need to choose between two different mechanisms for sending.
191 // On the main thread we use the regular dispatch Send() method, on another
192 // thread we use SyncMessageFilter.
193 if (PpapiGlobals::Get()->GetMainThreadMessageLoop()->BelongsToCurrentThread())
194 return Dispatcher::Send(msg);
195 return sync_filter_->Send(msg);
198 bool PluginDispatcher::Send(IPC::Message* msg) {
199 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::Send",
200 "Class", IPC_MESSAGE_ID_CLASS(msg->type()),
201 "Line", IPC_MESSAGE_ID_LINE(msg->type()));
202 // We always want plugin->renderer messages to arrive in-order. If some sync
203 // and some async messages are sent in response to a synchronous
204 // renderer->plugin call, the sync reply will be processed before the async
205 // reply, and everything will be confused.
207 // Allowing all async messages to unblock the renderer means more reentrancy
208 // there but gives correct ordering.
210 // We don't want reply messages to unblock however, as they will potentially
211 // end up on the wrong queue - see crbug.com/122443
212 if (!msg->is_reply())
213 msg->set_unblock(true);
214 if (msg->is_sync()) {
215 // Synchronous messages might be re-entrant, so we need to drop the lock.
216 ProxyAutoUnlock unlock;
217 SCOPED_UMA_HISTOGRAM_TIMER("Plugin.PpapiSyncIPCTime");
218 return SendMessage(msg);
220 return SendMessage(msg);
223 bool PluginDispatcher::SendAndStayLocked(IPC::Message* msg) {
224 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::SendAndStayLocked",
225 "Class", IPC_MESSAGE_ID_CLASS(msg->type()),
226 "Line", IPC_MESSAGE_ID_LINE(msg->type()));
227 if (!msg->is_reply())
228 msg->set_unblock(true);
229 return SendMessage(msg);
232 bool PluginDispatcher::OnMessageReceived(const IPC::Message& msg) {
233 // We need to grab the proxy lock to ensure that we don't collide with the
234 // plugin making pepper calls on a different thread.
235 ProxyAutoLock lock;
236 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::OnMessageReceived",
237 "Class", IPC_MESSAGE_ID_CLASS(msg.type()),
238 "Line", IPC_MESSAGE_ID_LINE(msg.type()));
240 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
241 // Handle some plugin-specific control messages.
242 bool handled = true;
243 IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg)
244 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface)
245 IPC_MESSAGE_HANDLER(PpapiMsg_SetPreferences, OnMsgSetPreferences)
246 IPC_MESSAGE_UNHANDLED(handled = false);
247 IPC_END_MESSAGE_MAP()
248 if (handled)
249 return true;
251 return Dispatcher::OnMessageReceived(msg);
254 void PluginDispatcher::OnChannelError() {
255 Dispatcher::OnChannelError();
257 // The renderer has crashed or exited. This channel and all instances
258 // associated with it are no longer valid.
259 ForceFreeAllInstances();
260 // TODO(brettw) free resources too!
261 delete this;
264 void PluginDispatcher::DidCreateInstance(PP_Instance instance) {
265 if (!g_instance_to_dispatcher)
266 g_instance_to_dispatcher = new InstanceToDispatcherMap;
267 (*g_instance_to_dispatcher)[instance] = this;
268 instance_map_.set(instance, scoped_ptr<InstanceData>(new InstanceData()));
271 void PluginDispatcher::DidDestroyInstance(PP_Instance instance) {
272 instance_map_.erase(instance);
274 if (g_instance_to_dispatcher) {
275 InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find(
276 instance);
277 if (found != g_instance_to_dispatcher->end()) {
278 DCHECK(found->second == this);
279 g_instance_to_dispatcher->erase(found);
280 } else {
281 NOTREACHED();
286 InstanceData* PluginDispatcher::GetInstanceData(PP_Instance instance) {
287 return instance_map_.get(instance);
290 thunk::PPB_Instance_API* PluginDispatcher::GetInstanceAPI() {
291 return static_cast<PPB_Instance_Proxy*>(
292 GetInterfaceProxy(API_ID_PPB_INSTANCE));
295 thunk::ResourceCreationAPI* PluginDispatcher::GetResourceCreationAPI() {
296 return static_cast<ResourceCreationProxy*>(
297 GetInterfaceProxy(API_ID_RESOURCE_CREATION));
300 void PluginDispatcher::ForceFreeAllInstances() {
301 if (!g_instance_to_dispatcher)
302 return;
304 // Iterating will remove each item from the map, so we need to make a copy
305 // to avoid things changing out from under is.
306 InstanceToDispatcherMap temp_map = *g_instance_to_dispatcher;
307 for (InstanceToDispatcherMap::iterator i = temp_map.begin();
308 i != temp_map.end(); ++i) {
309 if (i->second == this) {
310 // Synthesize an "instance destroyed" message, this will notify the
311 // plugin and also remove it from our list of tracked plugins.
312 PpapiMsg_PPPInstance_DidDestroy msg(API_ID_PPP_INSTANCE, i->first);
313 OnMessageReceived(msg);
318 void PluginDispatcher::OnMsgSupportsInterface(
319 const std::string& interface_name,
320 bool* result) {
321 *result = !!GetPluginInterface(interface_name);
323 // Do fallback for PPP_Instance. This is a hack here and if we have more
324 // cases like this it should be generalized. The PPP_Instance proxy always
325 // proxies the 1.1 interface, and then does fallback to 1.0 inside the
326 // plugin process (see PPP_Instance_Proxy). So here we return true for
327 // supporting the 1.1 interface if either 1.1 or 1.0 is supported.
328 if (!*result && interface_name == PPP_INSTANCE_INTERFACE)
329 *result = !!GetPluginInterface(PPP_INSTANCE_INTERFACE_1_0);
332 void PluginDispatcher::OnMsgSetPreferences(const Preferences& prefs) {
333 // The renderer may send us preferences more than once (currently this
334 // happens every time a new plugin instance is created). Since we don't have
335 // a way to signal to the plugin that the preferences have changed, changing
336 // the default fonts and such in the middle of a running plugin could be
337 // confusing to it. As a result, we never allow the preferences to be changed
338 // once they're set. The user will have to restart to get new font prefs
339 // propogated to plugins.
340 if (!received_preferences_) {
341 received_preferences_ = true;
342 preferences_ = prefs;
346 } // namespace proxy
347 } // namespace ppapi