[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 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 | |
[email protected] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 9 | #include "base/compiler_specific.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | aabfa65 | 2011-07-13 23:28:44 | [diff] [blame] | 11 | #include "base/message_loop.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 12 | #include "ipc/ipc_message.h" |
| 13 | #include "ipc/ipc_sync_channel.h" |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 14 | #include "base/debug/trace_event.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 15 | #include "ppapi/c/pp_errors.h" |
[email protected] | e8f07ac | 2012-01-03 17:43:36 | [diff] [blame] | 16 | #include "ppapi/c/ppp_instance.h" |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 17 | #include "ppapi/proxy/interface_list.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 18 | #include "ppapi/proxy/interface_proxy.h" |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 19 | #include "ppapi/proxy/plugin_message_filter.h" |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 20 | #include "ppapi/proxy/plugin_resource_tracker.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 21 | #include "ppapi/proxy/plugin_var_serialization_rules.h" |
| 22 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | 9815108e | 2011-05-27 21:50:28 | [diff] [blame] | 23 | #include "ppapi/proxy/ppb_cursor_control_proxy.h" |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 24 | #include "ppapi/proxy/ppb_font_proxy.h" |
[email protected] | ceadc39 | 2011-06-15 23:04:24 | [diff] [blame] | 25 | #include "ppapi/proxy/ppb_instance_proxy.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 26 | #include "ppapi/proxy/ppp_class_proxy.h" |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 27 | #include "ppapi/proxy/resource_creation_proxy.h" |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 28 | #include "ppapi/shared_impl/resource.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 29 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 30 | #if defined(OS_POSIX) |
| 31 | #include "base/eintr_wrapper.h" |
| 32 | #include "ipc/ipc_channel_posix.h" |
| 33 | #endif |
| 34 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 35 | namespace ppapi { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 36 | namespace proxy { |
| 37 | |
| 38 | namespace { |
| 39 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 40 | typedef std::map<PP_Instance, PluginDispatcher*> InstanceToDispatcherMap; |
| 41 | InstanceToDispatcherMap* g_instance_to_dispatcher = NULL; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 42 | |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame^] | 43 | typedef std::set<PluginDispatcher*> DispatcherSet; |
| 44 | DispatcherSet* g_live_dispatchers = NULL; |
| 45 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 46 | } // namespace |
| 47 | |
[email protected] | 06e0a34 | 2011-09-27 04:24:30 | [diff] [blame] | 48 | InstanceData::InstanceData() |
[email protected] | e8f07ac | 2012-01-03 17:43:36 | [diff] [blame] | 49 | : flash_fullscreen(PP_FALSE), |
[email protected] | 0f41c01 | 2011-10-21 19:49:20 | [diff] [blame] | 50 | mouse_lock_callback(PP_BlockUntilComplete()) { |
[email protected] | c7bf745 | 2011-09-12 21:31:50 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | 0f41c01 | 2011-10-21 19:49:20 | [diff] [blame] | 53 | InstanceData::~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] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 59 | PluginDispatcher::PluginDispatcher(base::ProcessHandle remote_process_handle, |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 60 | GetInterfaceFunc get_interface) |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 61 | : Dispatcher(remote_process_handle, get_interface), |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 62 | plugin_delegate_(NULL), |
[email protected] | 373a95a | 2011-07-01 16:58:14 | [diff] [blame] | 63 | received_preferences_(false), |
| 64 | plugin_dispatcher_id_(0) { |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 65 | SetSerializationRules(new PluginVarSerializationRules); |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame^] | 66 | |
| 67 | if (!g_live_dispatchers) |
| 68 | g_live_dispatchers = new DispatcherSet; |
| 69 | g_live_dispatchers->insert(this); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | PluginDispatcher::~PluginDispatcher() { |
[email protected] | 373a95a | 2011-07-01 16:58:14 | [diff] [blame] | 73 | if (plugin_delegate_) |
| 74 | plugin_delegate_->Unregister(plugin_dispatcher_id_); |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame^] | 75 | |
| 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] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // static |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 84 | PluginDispatcher* PluginDispatcher::GetForInstance(PP_Instance instance) { |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 85 | 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] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 92 | } |
| 93 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 94 | // static |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 95 | PluginDispatcher* PluginDispatcher::GetForResource(const Resource* resource) { |
| 96 | return GetForInstance(resource->pp_instance()); |
| 97 | } |
| 98 | |
| 99 | // static |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 100 | const void* PluginDispatcher::GetBrowserInterface(const char* interface_name) { |
| 101 | return InterfaceList::GetInstance()->GetInterfaceForPPB(interface_name); |
| 102 | } |
| 103 | |
[email protected] | a9b16dd | 2012-01-31 05:00:26 | [diff] [blame^] | 104 | // static |
| 105 | void 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] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 131 | const 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] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 142 | bool PluginDispatcher::InitPluginWithChannel( |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 143 | PluginDelegate* delegate, |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 144 | const IPC::ChannelHandle& channel_handle, |
| 145 | bool is_client) { |
| 146 | if (!Dispatcher::InitWithChannel(delegate, channel_handle, is_client)) |
| 147 | return false; |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 148 | plugin_delegate_ = delegate; |
[email protected] | 373a95a | 2011-07-01 16:58:14 | [diff] [blame] | 149 | plugin_dispatcher_id_ = plugin_delegate_->Register(this); |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 150 | |
| 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] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 158 | bool PluginDispatcher::IsPlugin() const { |
| 159 | return true; |
| 160 | } |
| 161 | |
[email protected] | b00bbb3 | 2011-03-30 19:02:14 | [diff] [blame] | 162 | bool PluginDispatcher::Send(IPC::Message* msg) { |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 163 | TRACE_EVENT2("ppapi proxy", "PluginDispatcher::Send", |
| 164 | "Class", IPC_MESSAGE_ID_CLASS(msg->type()), |
| 165 | "Line", IPC_MESSAGE_ID_LINE(msg->type())); |
[email protected] | b00bbb3 | 2011-03-30 19:02:14 | [diff] [blame] | 166 | // 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] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 177 | bool PluginDispatcher::OnMessageReceived(const IPC::Message& msg) { |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 178 | TRACE_EVENT2("ppapi proxy", "PluginDispatcher::OnMessageReceived", |
| 179 | "Class", IPC_MESSAGE_ID_CLASS(msg.type()), |
| 180 | "Line", IPC_MESSAGE_ID_LINE(msg.type())); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 181 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
| 182 | // Handle some plugin-specific control messages. |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 183 | bool handled = true; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 184 | IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg) |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 185 | IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface) |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 186 | IPC_MESSAGE_HANDLER(PpapiMsg_SetPreferences, OnMsgSetPreferences) |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 187 | IPC_MESSAGE_UNHANDLED(handled = false); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 188 | IPC_END_MESSAGE_MAP() |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 189 | if (handled) |
[email protected] | 37fe036 | 2011-09-13 04:00:33 | [diff] [blame] | 190 | return true; |
[email protected] | 37fe036 | 2011-09-13 04:00:33 | [diff] [blame] | 191 | } |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 192 | return Dispatcher::OnMessageReceived(msg); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 195 | void PluginDispatcher::OnChannelError() { |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 196 | Dispatcher::OnChannelError(); |
| 197 | |
[email protected] | 4b417e5 | 2011-04-18 22:51:08 | [diff] [blame] | 198 | // The renderer has crashed or exited. This channel and all instances |
| 199 | // associated with it are no longer valid. |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 200 | ForceFreeAllInstances(); |
| 201 | // TODO(brettw) free resources too! |
| 202 | delete this; |
| 203 | } |
| 204 | |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 205 | void PluginDispatcher::DidCreateInstance(PP_Instance instance) { |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 206 | if (!g_instance_to_dispatcher) |
| 207 | g_instance_to_dispatcher = new InstanceToDispatcherMap; |
| 208 | (*g_instance_to_dispatcher)[instance] = this; |
| 209 | |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 210 | instance_map_[instance] = InstanceData(); |
| 211 | } |
| 212 | |
| 213 | void 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] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 217 | |
| 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] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | InstanceData* 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] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 235 | FunctionGroupBase* PluginDispatcher::GetFunctionAPI(ApiID id) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 236 | return GetInterfaceProxy(id); |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 237 | } |
| 238 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 239 | void PluginDispatcher::ForceFreeAllInstances() { |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 240 | 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] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 251 | PpapiMsg_PPPInstance_DidDestroy msg(API_ID_PPP_INSTANCE, i->first); |
[email protected] | 4585fbc | 2011-06-13 17:17:56 | [diff] [blame] | 252 | OnMessageReceived(msg); |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 253 | } |
| 254 | } |
[email protected] | 176c7392 | 2010-12-03 17:32:19 | [diff] [blame] | 255 | } |
| 256 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 257 | void PluginDispatcher::OnMsgSupportsInterface( |
| 258 | const std::string& interface_name, |
| 259 | bool* result) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 260 | *result = !!GetPluginInterface(interface_name); |
[email protected] | e8f07ac | 2012-01-03 17:43:36 | [diff] [blame] | 261 | |
| 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] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 269 | } |
| 270 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 271 | void PluginDispatcher::OnMsgSetPreferences(const Preferences& prefs) { |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 272 | // 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] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 285 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 286 | } // namespace ppapi |