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