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. 4 5 #include "ppapi/proxy/plugin_dispatcher.h" 6 7 #include <map> 8 9 #include "base/compiler_specific.h" 10 #include "base/debug/trace_event.h" 11 #include "base/logging.h" 12 #include "base/message_loop/message_loop.h" 13 #include "ipc/ipc_message.h" 14 #include "ipc/ipc_sync_channel.h" 15 #include "ipc/ipc_sync_message_filter.h" 16 #include "ppapi/c/pp_errors.h" 17 #include "ppapi/c/ppp_instance.h" 18 #include "ppapi/proxy/flash_clipboard_resource.h" 19 #include "ppapi/proxy/flash_file_resource.h" 20 #include "ppapi/proxy/flash_resource.h" 21 #include "ppapi/proxy/gamepad_resource.h" 22 #include "ppapi/proxy/interface_list.h" 23 #include "ppapi/proxy/interface_proxy.h" 24 #include "ppapi/proxy/plugin_globals.h" 25 #include "ppapi/proxy/plugin_message_filter.h" 26 #include "ppapi/proxy/plugin_resource_tracker.h" 27 #include "ppapi/proxy/plugin_var_serialization_rules.h" 28 #include "ppapi/proxy/ppapi_messages.h" 29 #include "ppapi/proxy/ppb_instance_proxy.h" 30 #include "ppapi/proxy/ppp_class_proxy.h" 31 #include "ppapi/proxy/resource_creation_proxy.h" 32 #include "ppapi/proxy/resource_reply_thread_registrar.h" 33 #include "ppapi/shared_impl/ppapi_globals.h" 34 #include "ppapi/shared_impl/proxy_lock.h" 35 #include "ppapi/shared_impl/resource.h" 36 37 #if defined(OS_POSIX) && !defined(OS_NACL) 38 #include "ipc/ipc_channel_posix.h" 39 #endif 40 41 namespace ppapi { 42 namespace proxy { 43 44 namespace { 45 46 typedef std::map<PP_Instance, PluginDispatcher*> InstanceToDispatcherMap; 47 InstanceToDispatcherMap* g_instance_to_dispatcher = NULL; 48 49 typedef std::set<PluginDispatcher*> DispatcherSet; 50 DispatcherSet* g_live_dispatchers = NULL; 51 52 } // namespace 53 54 InstanceData::InstanceData() 55 : is_request_surrounding_text_pending(false), 56 should_do_request_surrounding_text(false) { 57 } 58 59 InstanceData::~InstanceData() { 60 // Run any pending mouse lock callback to prevent leaks. 61 if (mouse_lock_callback.get()) 62 mouse_lock_callback->Abort(); 63 } 64 65 PluginDispatcher::PluginDispatcher(PP_GetInterface_Func get_interface, 66 const PpapiPermissions& permissions, 67 bool incognito) 68 : Dispatcher(get_interface, permissions), 69 plugin_delegate_(NULL), 70 received_preferences_(false), 71 plugin_dispatcher_id_(0), 72 incognito_(incognito) { 73 SetSerializationRules(new PluginVarSerializationRules(AsWeakPtr())); 74 75 if (!g_live_dispatchers) 76 g_live_dispatchers = new DispatcherSet; 77 g_live_dispatchers->insert(this); 78 } 79 80 PluginDispatcher::~PluginDispatcher() { 81 PluginGlobals::Get()->plugin_var_tracker()->DidDeleteDispatcher(this); 82 83 if (plugin_delegate_) 84 plugin_delegate_->Unregister(plugin_dispatcher_id_); 85 86 g_live_dispatchers->erase(this); 87 if (g_live_dispatchers->empty()) { 88 delete g_live_dispatchers; 89 g_live_dispatchers = NULL; 90 } 91 } 92 93 // static 94 PluginDispatcher* PluginDispatcher::GetForInstance(PP_Instance instance) { 95 if (!g_instance_to_dispatcher) 96 return NULL; 97 InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find( 98 instance); 99 if (found == g_instance_to_dispatcher->end()) 100 return NULL; 101 return found->second; 102 } 103 104 // static 105 PluginDispatcher* PluginDispatcher::GetForResource(const Resource* resource) { 106 return GetForInstance(resource->pp_instance()); 107 } 108 109 // static 110 const void* PluginDispatcher::GetBrowserInterface(const char* interface_name) { 111 // CAUTION: This function is called directly from the plugin, but we *don't* 112 // lock the ProxyLock to avoid excessive locking from C++ wrappers. 113 return InterfaceList::GetInstance()->GetInterfaceForPPB(interface_name); 114 } 115 116 // static 117 void PluginDispatcher::LogWithSource(PP_Instance instance, 118 PP_LogLevel level, 119 const std::string& source, 120 const std::string& value) { 121 if (!g_live_dispatchers || !g_instance_to_dispatcher) 122 return; 123 124 if (instance) { 125 InstanceToDispatcherMap::iterator found = 126 g_instance_to_dispatcher->find(instance); 127 if (found != g_instance_to_dispatcher->end()) { 128 // Send just to this specific dispatcher. 129 found->second->Send(new PpapiHostMsg_LogWithSource( 130 instance, static_cast<int>(level), source, value)); 131 return; 132 } 133 } 134 135 // Instance 0 or invalid, send to all dispatchers. 136 for (DispatcherSet::iterator i = g_live_dispatchers->begin(); 137 i != g_live_dispatchers->end(); ++i) { 138 (*i)->Send(new PpapiHostMsg_LogWithSource( 139 instance, static_cast<int>(level), source, value)); 140 } 141 } 142 143 const void* PluginDispatcher::GetPluginInterface( 144 const std::string& interface_name) { 145 InterfaceMap::iterator found = plugin_interfaces_.find(interface_name); 146 if (found == plugin_interfaces_.end()) { 147 const void* ret = local_get_interface()(interface_name.c_str()); 148 plugin_interfaces_.insert(std::make_pair(interface_name, ret)); 149 return ret; 150 } 151 return found->second; 152 } 153 154 bool PluginDispatcher::InitPluginWithChannel( 155 PluginDelegate* delegate, 156 base::ProcessId peer_pid, 157 const IPC::ChannelHandle& channel_handle, 158 bool is_client) { 159 if (!Dispatcher::InitWithChannel(delegate, peer_pid, channel_handle, 160 is_client)) 161 return false; 162 plugin_delegate_ = delegate; 163 plugin_dispatcher_id_ = plugin_delegate_->Register(this); 164 165 sync_filter_ = new IPC::SyncMessageFilter(delegate->GetShutdownEvent()); 166 channel()->AddFilter(sync_filter_.get()); 167 168 // The message filter will intercept and process certain messages directly 169 // on the I/O thread. 170 channel()->AddFilter( 171 new PluginMessageFilter( 172 delegate->GetGloballySeenInstanceIDSet(), 173 PluginGlobals::Get()->resource_reply_thread_registrar())); 174 return true; 175 } 176 177 bool PluginDispatcher::IsPlugin() const { 178 return true; 179 } 180 181 bool PluginDispatcher::SendMessage(IPC::Message* msg) { 182 // Currently we need to choose between two different mechanisms for sending. 183 // On the main thread we use the regular dispatch Send() method, on another 184 // thread we use SyncMessageFilter. 185 if (PpapiGlobals::Get()->GetMainThreadMessageLoop()->BelongsToCurrentThread()) 186 return Dispatcher::Send(msg); 187 return sync_filter_->Send(msg); 188 } 189 190 bool PluginDispatcher::Send(IPC::Message* msg) { 191 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::Send", 192 "Class", IPC_MESSAGE_ID_CLASS(msg->type()), 193 "Line", IPC_MESSAGE_ID_LINE(msg->type())); 194 // We always want plugin->renderer messages to arrive in-order. If some sync 195 // and some async messages are sent in response to a synchronous 196 // renderer->plugin call, the sync reply will be processed before the async 197 // reply, and everything will be confused. 198 // 199 // Allowing all async messages to unblock the renderer means more reentrancy 200 // there but gives correct ordering. 201 // 202 // We don't want reply messages to unblock however, as they will potentially 203 // end up on the wrong queue - see crbug.com/122443 204 if (!msg->is_reply()) 205 msg->set_unblock(true); 206 if (msg->is_sync()) { 207 // Synchronous messages might be re-entrant, so we need to drop the lock. 208 ProxyAutoUnlock unlock; 209 return SendMessage(msg); 210 } 211 return SendMessage(msg); 212 } 213 214 bool PluginDispatcher::OnMessageReceived(const IPC::Message& msg) { 215 // We need to grab the proxy lock to ensure that we don't collide with the 216 // plugin making pepper calls on a different thread. 217 ProxyAutoLock lock; 218 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::OnMessageReceived", 219 "Class", IPC_MESSAGE_ID_CLASS(msg.type()), 220 "Line", IPC_MESSAGE_ID_LINE(msg.type())); 221 222 if (msg.routing_id() == MSG_ROUTING_CONTROL) { 223 // Handle some plugin-specific control messages. 224 bool handled = true; 225 IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg) 226 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface) 227 IPC_MESSAGE_HANDLER(PpapiMsg_SetPreferences, OnMsgSetPreferences) 228 IPC_MESSAGE_UNHANDLED(handled = false); 229 IPC_END_MESSAGE_MAP() 230 if (handled) 231 return true; 232 } 233 return Dispatcher::OnMessageReceived(msg); 234 } 235 236 void PluginDispatcher::OnChannelError() { 237 Dispatcher::OnChannelError(); 238 239 // The renderer has crashed or exited. This channel and all instances 240 // associated with it are no longer valid. 241 ForceFreeAllInstances(); 242 // TODO(brettw) free resources too! 243 delete this; 244 } 245 246 void PluginDispatcher::DidCreateInstance(PP_Instance instance) { 247 if (!g_instance_to_dispatcher) 248 g_instance_to_dispatcher = new InstanceToDispatcherMap; 249 (*g_instance_to_dispatcher)[instance] = this; 250 instance_map_.set(instance, scoped_ptr<InstanceData>(new InstanceData())); 251 } 252 253 void PluginDispatcher::DidDestroyInstance(PP_Instance instance) { 254 instance_map_.erase(instance); 255 256 if (g_instance_to_dispatcher) { 257 InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find( 258 instance); 259 if (found != g_instance_to_dispatcher->end()) { 260 DCHECK(found->second == this); 261 g_instance_to_dispatcher->erase(found); 262 } else { 263 NOTREACHED(); 264 } 265 } 266 } 267 268 InstanceData* PluginDispatcher::GetInstanceData(PP_Instance instance) { 269 return instance_map_.get(instance); 270 } 271 272 thunk::PPB_Instance_API* PluginDispatcher::GetInstanceAPI() { 273 return static_cast<PPB_Instance_Proxy*>( 274 GetInterfaceProxy(API_ID_PPB_INSTANCE)); 275 } 276 277 thunk::ResourceCreationAPI* PluginDispatcher::GetResourceCreationAPI() { 278 return static_cast<ResourceCreationProxy*>( 279 GetInterfaceProxy(API_ID_RESOURCE_CREATION)); 280 } 281 282 void PluginDispatcher::ForceFreeAllInstances() { 283 if (!g_instance_to_dispatcher) 284 return; 285 286 // Iterating will remove each item from the map, so we need to make a copy 287 // to avoid things changing out from under is. 288 InstanceToDispatcherMap temp_map = *g_instance_to_dispatcher; 289 for (InstanceToDispatcherMap::iterator i = temp_map.begin(); 290 i != temp_map.end(); ++i) { 291 if (i->second == this) { 292 // Synthesize an "instance destroyed" message, this will notify the 293 // plugin and also remove it from our list of tracked plugins. 294 PpapiMsg_PPPInstance_DidDestroy msg(API_ID_PPP_INSTANCE, i->first); 295 OnMessageReceived(msg); 296 } 297 } 298 } 299 300 void PluginDispatcher::OnMsgSupportsInterface( 301 const std::string& interface_name, 302 bool* result) { 303 *result = !!GetPluginInterface(interface_name); 304 305 // Do fallback for PPP_Instance. This is a hack here and if we have more 306 // cases like this it should be generalized. The PPP_Instance proxy always 307 // proxies the 1.1 interface, and then does fallback to 1.0 inside the 308 // plugin process (see PPP_Instance_Proxy). So here we return true for 309 // supporting the 1.1 interface if either 1.1 or 1.0 is supported. 310 if (!*result && interface_name == PPP_INSTANCE_INTERFACE) 311 *result = !!GetPluginInterface(PPP_INSTANCE_INTERFACE_1_0); 312 } 313 314 void PluginDispatcher::OnMsgSetPreferences(const Preferences& prefs) { 315 // The renderer may send us preferences more than once (currently this 316 // happens every time a new plugin instance is created). Since we don't have 317 // a way to signal to the plugin that the preferences have changed, changing 318 // the default fonts and such in the middle of a running plugin could be 319 // confusing to it. As a result, we never allow the preferences to be changed 320 // once they're set. The user will have to restart to get new font prefs 321 // propogated to plugins. 322 if (!received_preferences_) { 323 received_preferences_ = true; 324 preferences_ = prefs; 325 } 326 } 327 328 } // namespace proxy 329 } // namespace ppapi 330