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