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