[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" |
| 16 | #include "ppapi/proxy/interface_proxy.h" |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 17 | #include "ppapi/proxy/plugin_message_filter.h" |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 18 | #include "ppapi/proxy/plugin_resource_tracker.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 19 | #include "ppapi/proxy/plugin_var_serialization_rules.h" |
| 20 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | 9815108e | 2011-05-27 21:50:28 | [diff] [blame] | 21 | #include "ppapi/proxy/ppb_char_set_proxy.h" |
| 22 | #include "ppapi/proxy/ppb_cursor_control_proxy.h" |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 23 | #include "ppapi/proxy/ppb_font_proxy.h" |
[email protected] | ceadc39 | 2011-06-15 23:04:24 | [diff] [blame] | 24 | #include "ppapi/proxy/ppb_instance_proxy.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 25 | #include "ppapi/proxy/ppp_class_proxy.h" |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 26 | #include "ppapi/proxy/resource_creation_proxy.h" |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 27 | #include "ppapi/shared_impl/resource.h" |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 28 | #include "ppapi/shared_impl/tracker_base.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] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 43 | } // namespace |
| 44 | |
[email protected] | c7bf745 | 2011-09-12 21:31:50 | [diff] [blame^] | 45 | InstanceData::InstanceData() : fullscreen(PP_FALSE) { |
| 46 | memset(&position, 0, sizeof(position)); |
| 47 | } |
| 48 | |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 49 | PluginDispatcher::PluginDispatcher(base::ProcessHandle remote_process_handle, |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 50 | GetInterfaceFunc get_interface) |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 51 | : Dispatcher(remote_process_handle, get_interface), |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 52 | plugin_delegate_(NULL), |
[email protected] | 373a95a | 2011-07-01 16:58:14 | [diff] [blame] | 53 | received_preferences_(false), |
| 54 | plugin_dispatcher_id_(0) { |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 55 | SetSerializationRules(new PluginVarSerializationRules); |
[email protected] | c77a8dd4 | 2011-09-12 20:37:04 | [diff] [blame] | 56 | |
| 57 | // As a plugin, we always support the PPP_Class interface. There's no |
| 58 | // GetInterface call or name for it, so we insert it into our table now. |
| 59 | target_proxies_[INTERFACE_ID_PPP_CLASS].reset(new PPP_Class_Proxy(this)); |
| 60 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 61 | TrackerBase::Init(&PluginResourceTracker::GetTrackerBaseInstance); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | PluginDispatcher::~PluginDispatcher() { |
[email protected] | 373a95a | 2011-07-01 16:58:14 | [diff] [blame] | 65 | if (plugin_delegate_) |
| 66 | plugin_delegate_->Unregister(plugin_dispatcher_id_); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // static |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 70 | PluginDispatcher* PluginDispatcher::GetForInstance(PP_Instance instance) { |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 71 | if (!g_instance_to_dispatcher) |
| 72 | return NULL; |
| 73 | InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find( |
| 74 | instance); |
| 75 | if (found == g_instance_to_dispatcher->end()) |
| 76 | return NULL; |
| 77 | return found->second; |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 78 | } |
| 79 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 80 | // static |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 81 | PluginDispatcher* PluginDispatcher::GetForResource(const Resource* resource) { |
| 82 | return GetForInstance(resource->pp_instance()); |
| 83 | } |
| 84 | |
| 85 | // static |
[email protected] | c77a8dd4 | 2011-09-12 20:37:04 | [diff] [blame] | 86 | const void* PluginDispatcher::GetInterfaceFromDispatcher( |
| 87 | const char* dispatcher_interface) { |
| 88 | // All interfaces the plugin requests of the browser are "PPB". |
| 89 | const InterfaceProxy::Info* info = GetPPBInterfaceInfo(dispatcher_interface); |
| 90 | if (!info) |
| 91 | return NULL; |
| 92 | return info->interface_ptr; |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 95 | bool PluginDispatcher::InitPluginWithChannel( |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 96 | PluginDelegate* delegate, |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 97 | const IPC::ChannelHandle& channel_handle, |
| 98 | bool is_client) { |
| 99 | if (!Dispatcher::InitWithChannel(delegate, channel_handle, is_client)) |
| 100 | return false; |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 101 | plugin_delegate_ = delegate; |
[email protected] | 373a95a | 2011-07-01 16:58:14 | [diff] [blame] | 102 | plugin_dispatcher_id_ = plugin_delegate_->Register(this); |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 103 | |
| 104 | // The message filter will intercept and process certain messages directly |
| 105 | // on the I/O thread. |
| 106 | channel()->AddFilter( |
| 107 | new PluginMessageFilter(delegate->GetGloballySeenInstanceIDSet())); |
| 108 | return true; |
| 109 | } |
| 110 | |
[email protected] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 111 | bool PluginDispatcher::IsPlugin() const { |
| 112 | return true; |
| 113 | } |
| 114 | |
[email protected] | b00bbb3 | 2011-03-30 19:02:14 | [diff] [blame] | 115 | bool PluginDispatcher::Send(IPC::Message* msg) { |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 116 | TRACE_EVENT2("ppapi proxy", "PluginDispatcher::Send", |
| 117 | "Class", IPC_MESSAGE_ID_CLASS(msg->type()), |
| 118 | "Line", IPC_MESSAGE_ID_LINE(msg->type())); |
[email protected] | b00bbb3 | 2011-03-30 19:02:14 | [diff] [blame] | 119 | // We always want plugin->renderer messages to arrive in-order. If some sync |
| 120 | // and some async messages are send in response to a synchronous |
| 121 | // renderer->plugin call, the sync reply will be processed before the async |
| 122 | // reply, and everything will be confused. |
| 123 | // |
| 124 | // Allowing all async messages to unblock the renderer means more reentrancy |
| 125 | // there but gives correct ordering. |
| 126 | msg->set_unblock(true); |
| 127 | return Dispatcher::Send(msg); |
| 128 | } |
| 129 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 130 | bool PluginDispatcher::OnMessageReceived(const IPC::Message& msg) { |
[email protected] | 366ae24 | 2011-05-10 02:23:58 | [diff] [blame] | 131 | TRACE_EVENT2("ppapi proxy", "PluginDispatcher::OnMessageReceived", |
| 132 | "Class", IPC_MESSAGE_ID_CLASS(msg.type()), |
| 133 | "Line", IPC_MESSAGE_ID_LINE(msg.type())); |
[email protected] | c77a8dd4 | 2011-09-12 20:37:04 | [diff] [blame] | 134 | // Handle common control messages. |
| 135 | if (Dispatcher::OnMessageReceived(msg)) |
| 136 | return true; |
| 137 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 138 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
| 139 | // Handle some plugin-specific control messages. |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 140 | bool handled = true; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 141 | IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg) |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 142 | IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface) |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 143 | IPC_MESSAGE_HANDLER(PpapiMsg_SetPreferences, OnMsgSetPreferences) |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 144 | IPC_END_MESSAGE_MAP() |
[email protected] | c77a8dd4 | 2011-09-12 20:37:04 | [diff] [blame] | 145 | return handled; |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 146 | } |
[email protected] | c77a8dd4 | 2011-09-12 20:37:04 | [diff] [blame] | 147 | |
| 148 | if (msg.routing_id() <= 0 || msg.routing_id() >= INTERFACE_ID_COUNT) { |
| 149 | // Host is sending us garbage. Since it's supposed to be trusted, this |
| 150 | // isn't supposed to happen. Crash here in all builds in case the renderer |
| 151 | // is compromised. |
| 152 | CHECK(false); |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | // There are two cases: |
| 157 | // |
| 158 | // * The first case is that the host is calling a PPP interface. It will |
| 159 | // always do a check for the interface before sending messages, and this |
| 160 | // will create the necessary interface proxy at that time. So when we |
| 161 | // actually receive a message, we know such a proxy will exist. |
| 162 | // |
| 163 | // * The second case is that the host is sending a response to the plugin |
| 164 | // side of a PPB interface (some, like the URL loader, have complex |
| 165 | // response messages). Since the host is trusted and not supposed to be |
| 166 | // doing silly things, we can just create a PPB proxy project on demand the |
| 167 | // first time it's needed. |
| 168 | |
| 169 | InterfaceProxy* proxy = target_proxies_[msg.routing_id()].get(); |
| 170 | if (!proxy) { |
| 171 | // Handle the first time the host calls a PPB reply interface by |
| 172 | // autocreating it. |
| 173 | const InterfaceProxy::Info* info = GetPPBInterfaceInfo( |
| 174 | static_cast<InterfaceID>(msg.routing_id())); |
| 175 | if (!info) { |
| 176 | NOTREACHED(); |
| 177 | return true; |
| 178 | } |
| 179 | proxy = info->create_proxy(this, NULL); |
| 180 | target_proxies_[info->id].reset(proxy); |
| 181 | } |
| 182 | |
| 183 | return proxy->OnMessageReceived(msg); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 186 | void PluginDispatcher::OnChannelError() { |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 187 | Dispatcher::OnChannelError(); |
| 188 | |
[email protected] | 4b417e5 | 2011-04-18 22:51:08 | [diff] [blame] | 189 | // The renderer has crashed or exited. This channel and all instances |
| 190 | // associated with it are no longer valid. |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 191 | ForceFreeAllInstances(); |
| 192 | // TODO(brettw) free resources too! |
| 193 | delete this; |
| 194 | } |
| 195 | |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 196 | void PluginDispatcher::DidCreateInstance(PP_Instance instance) { |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 197 | if (!g_instance_to_dispatcher) |
| 198 | g_instance_to_dispatcher = new InstanceToDispatcherMap; |
| 199 | (*g_instance_to_dispatcher)[instance] = this; |
| 200 | |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 201 | instance_map_[instance] = InstanceData(); |
| 202 | } |
| 203 | |
| 204 | void PluginDispatcher::DidDestroyInstance(PP_Instance instance) { |
| 205 | InstanceDataMap::iterator it = instance_map_.find(instance); |
| 206 | if (it != instance_map_.end()) |
| 207 | instance_map_.erase(it); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 208 | |
| 209 | if (g_instance_to_dispatcher) { |
| 210 | InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find( |
| 211 | instance); |
| 212 | if (found != g_instance_to_dispatcher->end()) { |
| 213 | DCHECK(found->second == this); |
| 214 | g_instance_to_dispatcher->erase(found); |
| 215 | } else { |
| 216 | NOTREACHED(); |
| 217 | } |
| 218 | } |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | InstanceData* PluginDispatcher::GetInstanceData(PP_Instance instance) { |
| 222 | InstanceDataMap::iterator it = instance_map_.find(instance); |
| 223 | return (it == instance_map_.end()) ? NULL : &it->second; |
| 224 | } |
| 225 | |
[email protected] | 7a1f7c6f | 2011-05-10 21:17:48 | [diff] [blame] | 226 | void PluginDispatcher::PostToWebKitThread( |
| 227 | const tracked_objects::Location& from_here, |
| 228 | const base::Closure& task) { |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 229 | return plugin_delegate_->PostToWebKitThread(from_here, task); |
| 230 | } |
| 231 | |
| 232 | bool PluginDispatcher::SendToBrowser(IPC::Message* msg) { |
| 233 | return plugin_delegate_->SendToBrowser(msg); |
[email protected] | 7a1f7c6f | 2011-05-10 21:17:48 | [diff] [blame] | 234 | } |
| 235 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 236 | WebKitForwarding* PluginDispatcher::GetWebKitForwarding() { |
[email protected] | d259a8e | 2011-05-18 22:31:09 | [diff] [blame] | 237 | return plugin_delegate_->GetWebKitForwarding(); |
[email protected] | 7a1f7c6f | 2011-05-10 21:17:48 | [diff] [blame] | 238 | } |
| 239 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 240 | FunctionGroupBase* PluginDispatcher::GetFunctionAPI(InterfaceID id) { |
[email protected] | c77a8dd4 | 2011-09-12 20:37:04 | [diff] [blame] | 241 | scoped_ptr<FunctionGroupBase >& proxy = function_proxies_[id]; |
| 242 | |
| 243 | if (proxy.get()) |
| 244 | return proxy.get(); |
| 245 | |
| 246 | if (id == INTERFACE_ID_PPB_CHAR_SET) |
| 247 | proxy.reset(new PPB_CharSet_Proxy(this, NULL)); |
| 248 | else if(id == INTERFACE_ID_PPB_CURSORCONTROL) |
| 249 | proxy.reset(new PPB_CursorControl_Proxy(this, NULL)); |
| 250 | else if (id == INTERFACE_ID_PPB_FONT) |
| 251 | proxy.reset(new PPB_Font_Proxy(this, NULL)); |
| 252 | else if (id == INTERFACE_ID_PPB_INSTANCE) |
| 253 | proxy.reset(new PPB_Instance_Proxy(this, NULL)); |
| 254 | else if (id == INTERFACE_ID_RESOURCE_CREATION) |
| 255 | proxy.reset(new ResourceCreationProxy(this)); |
| 256 | |
| 257 | return proxy.get(); |
[email protected] | 6239d34 | 2011-05-06 22:55:47 | [diff] [blame] | 258 | } |
| 259 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 260 | void PluginDispatcher::ForceFreeAllInstances() { |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 261 | if (!g_instance_to_dispatcher) |
| 262 | return; |
| 263 | |
| 264 | // Iterating will remove each item from the map, so we need to make a copy |
| 265 | // to avoid things changing out from under is. |
| 266 | InstanceToDispatcherMap temp_map = *g_instance_to_dispatcher; |
| 267 | for (InstanceToDispatcherMap::iterator i = temp_map.begin(); |
| 268 | i != temp_map.end(); ++i) { |
| 269 | if (i->second == this) { |
| 270 | // Synthesize an "instance destroyed" message, this will notify the |
| 271 | // plugin and also remove it from our list of tracked plugins. |
[email protected] | 4585fbc | 2011-06-13 17:17:56 | [diff] [blame] | 272 | PpapiMsg_PPPInstance_DidDestroy msg(INTERFACE_ID_PPP_INSTANCE, i->first); |
| 273 | OnMessageReceived(msg); |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 274 | } |
| 275 | } |
[email protected] | 176c7392 | 2010-12-03 17:32:19 | [diff] [blame] | 276 | } |
| 277 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 278 | void PluginDispatcher::OnMsgSupportsInterface( |
| 279 | const std::string& interface_name, |
| 280 | bool* result) { |
[email protected] | c77a8dd4 | 2011-09-12 20:37:04 | [diff] [blame] | 281 | *result = false; |
| 282 | |
| 283 | // Setup a proxy for receiving the messages from this interface. |
| 284 | const InterfaceProxy::Info* info = GetPPPInterfaceInfo(interface_name); |
| 285 | if (!info) |
| 286 | return; // Interface not supported by proxy. |
| 287 | |
| 288 | // Check for a cached result. |
| 289 | if (target_proxies_[info->id].get()) { |
| 290 | *result = true; |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | // Query the plugin & cache the result. |
| 295 | const void* interface_functions = GetLocalInterface(interface_name.c_str()); |
| 296 | if (!interface_functions) |
| 297 | return; |
| 298 | target_proxies_[info->id].reset( |
| 299 | info->create_proxy(this, interface_functions)); |
| 300 | *result = true; |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 301 | } |
| 302 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 303 | void PluginDispatcher::OnMsgSetPreferences(const Preferences& prefs) { |
[email protected] | 208aad79 | 2011-05-26 19:05:28 | [diff] [blame] | 304 | // The renderer may send us preferences more than once (currently this |
| 305 | // happens every time a new plugin instance is created). Since we don't have |
| 306 | // a way to signal to the plugin that the preferences have changed, changing |
| 307 | // the default fonts and such in the middle of a running plugin could be |
| 308 | // confusing to it. As a result, we never allow the preferences to be changed |
| 309 | // once they're set. The user will have to restart to get new font prefs |
| 310 | // propogated to plugins. |
| 311 | if (!received_preferences_) { |
| 312 | received_preferences_ = true; |
| 313 | preferences_ = prefs; |
| 314 | } |
| 315 | } |
| 316 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 317 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 318 | } // namespace ppapi |