[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" |
| 13 | #include "ppapi/c/pp_errors.h" |
| 14 | #include "ppapi/proxy/interface_proxy.h" |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 15 | #include "ppapi/proxy/plugin_message_filter.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 16 | #include "ppapi/proxy/plugin_var_serialization_rules.h" |
| 17 | #include "ppapi/proxy/ppapi_messages.h" |
| 18 | #include "ppapi/proxy/ppp_class_proxy.h" |
| 19 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 20 | #if defined(OS_POSIX) |
| 21 | #include "base/eintr_wrapper.h" |
| 22 | #include "ipc/ipc_channel_posix.h" |
| 23 | #endif |
| 24 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 25 | namespace pp { |
| 26 | namespace proxy { |
| 27 | |
| 28 | namespace { |
| 29 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 30 | typedef std::map<PP_Instance, PluginDispatcher*> InstanceToDispatcherMap; |
| 31 | InstanceToDispatcherMap* g_instance_to_dispatcher = NULL; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 32 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 33 | } // namespace |
| 34 | |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 35 | PluginDispatcher::PluginDispatcher(base::ProcessHandle remote_process_handle, |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 36 | GetInterfaceFunc get_interface) |
| 37 | : Dispatcher(remote_process_handle, get_interface) |
| 38 | #if defined(OS_POSIX) |
| 39 | , renderer_fd_(-1) |
| 40 | #endif |
| 41 | { |
[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] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | PluginDispatcher::~PluginDispatcher() { |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 50 | #if defined(OS_POSIX) |
| 51 | CloseRendererFD(); |
| 52 | #endif |
[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; |
| 73 | return info->interface; |
| 74 | } |
| 75 | |
[email protected] | 2cc06224 | 2011-03-10 21:16:34 | [diff] [blame] | 76 | bool PluginDispatcher::InitWithChannel( |
| 77 | Delegate* delegate, |
| 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) { |
| 95 | // We always want plugin->renderer messages to arrive in-order. If some sync |
| 96 | // and some async messages are send in response to a synchronous |
| 97 | // renderer->plugin call, the sync reply will be processed before the async |
| 98 | // reply, and everything will be confused. |
| 99 | // |
| 100 | // Allowing all async messages to unblock the renderer means more reentrancy |
| 101 | // there but gives correct ordering. |
| 102 | msg->set_unblock(true); |
| 103 | return Dispatcher::Send(msg); |
| 104 | } |
| 105 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 106 | bool PluginDispatcher::OnMessageReceived(const IPC::Message& msg) { |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 107 | // Handle common control messages. |
| 108 | if (Dispatcher::OnMessageReceived(msg)) |
| 109 | return true; |
| 110 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 111 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
| 112 | // Handle some plugin-specific control messages. |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 113 | bool handled = true; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 114 | IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg) |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 115 | IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface) |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 116 | IPC_END_MESSAGE_MAP() |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 117 | return handled; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 120 | if (msg.routing_id() <= 0 && msg.routing_id() >= INTERFACE_ID_COUNT) { |
| 121 | // Host is sending us garbage. Since it's supposed to be trusted, this |
| 122 | // isn't supposed to happen. Crash here in all builds in case the renderer |
| 123 | // is compromised. |
| 124 | CHECK(false); |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | // There are two cases: |
| 129 | // |
| 130 | // * The first case is that the host is calling a PPP interface. It will |
| 131 | // always do a check for the interface before sending messages, and this |
| 132 | // will create the necessary interface proxy at that time. So when we |
| 133 | // actually receive a message, we know such a proxy will exist. |
| 134 | // |
| 135 | // * The second case is that the host is sending a response to the plugin |
| 136 | // side of a PPB interface (some, like the URL loader, have complex |
| 137 | // response messages). Since the host is trusted and not supposed to be |
| 138 | // doing silly things, we can just create a PPB proxy project on demand the |
| 139 | // first time it's needed. |
| 140 | |
| 141 | InterfaceProxy* proxy = target_proxies_[msg.routing_id()].get(); |
| 142 | if (!proxy) { |
| 143 | // Handle the first time the host calls a PPB reply interface by |
| 144 | // autocreating it. |
| 145 | const InterfaceProxy::Info* info = GetPPBInterfaceInfo( |
| 146 | static_cast<InterfaceID>(msg.routing_id())); |
| 147 | if (!info) { |
| 148 | NOTREACHED(); |
| 149 | return true; |
| 150 | } |
| 151 | proxy = info->create_proxy(this, NULL); |
| 152 | target_proxies_[info->id].reset(proxy); |
| 153 | } |
| 154 | |
| 155 | return proxy->OnMessageReceived(msg); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 158 | void PluginDispatcher::OnChannelError() { |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 159 | Dispatcher::OnChannelError(); |
| 160 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 161 | // The renderer has crashed. This channel and all instances associated with |
| 162 | // it are no longer valid. |
| 163 | ForceFreeAllInstances(); |
| 164 | // TODO(brettw) free resources too! |
| 165 | delete this; |
| 166 | } |
| 167 | |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 168 | void PluginDispatcher::DidCreateInstance(PP_Instance instance) { |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 169 | if (!g_instance_to_dispatcher) |
| 170 | g_instance_to_dispatcher = new InstanceToDispatcherMap; |
| 171 | (*g_instance_to_dispatcher)[instance] = this; |
| 172 | |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 173 | instance_map_[instance] = InstanceData(); |
| 174 | } |
| 175 | |
| 176 | void PluginDispatcher::DidDestroyInstance(PP_Instance instance) { |
| 177 | InstanceDataMap::iterator it = instance_map_.find(instance); |
| 178 | if (it != instance_map_.end()) |
| 179 | instance_map_.erase(it); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 180 | |
| 181 | if (g_instance_to_dispatcher) { |
| 182 | InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find( |
| 183 | instance); |
| 184 | if (found != g_instance_to_dispatcher->end()) { |
| 185 | DCHECK(found->second == this); |
| 186 | g_instance_to_dispatcher->erase(found); |
| 187 | } else { |
| 188 | NOTREACHED(); |
| 189 | } |
| 190 | } |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | InstanceData* PluginDispatcher::GetInstanceData(PP_Instance instance) { |
| 194 | InstanceDataMap::iterator it = instance_map_.find(instance); |
| 195 | return (it == instance_map_.end()) ? NULL : &it->second; |
| 196 | } |
| 197 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 198 | #if defined(OS_POSIX) |
| 199 | int PluginDispatcher::GetRendererFD() { |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 200 | if (renderer_fd_ == -1 && channel()) |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 201 | renderer_fd_ = channel()->GetClientFileDescriptor(); |
| 202 | return renderer_fd_; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 203 | } |
| 204 | |
[email protected] | a08ebea | 2011-02-13 17:50:20 | [diff] [blame] | 205 | void PluginDispatcher::CloseRendererFD() { |
| 206 | if (renderer_fd_ != -1) { |
| 207 | if (HANDLE_EINTR(close(renderer_fd_)) < 0) |
| 208 | PLOG(ERROR) << "close"; |
| 209 | renderer_fd_ = -1; |
| 210 | } |
| 211 | } |
| 212 | #endif |
| 213 | |
| 214 | void PluginDispatcher::ForceFreeAllInstances() { |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 215 | if (!g_instance_to_dispatcher) |
| 216 | return; |
| 217 | |
| 218 | // Iterating will remove each item from the map, so we need to make a copy |
| 219 | // to avoid things changing out from under is. |
| 220 | InstanceToDispatcherMap temp_map = *g_instance_to_dispatcher; |
| 221 | for (InstanceToDispatcherMap::iterator i = temp_map.begin(); |
| 222 | i != temp_map.end(); ++i) { |
| 223 | if (i->second == this) { |
| 224 | // Synthesize an "instance destroyed" message, this will notify the |
| 225 | // plugin and also remove it from our list of tracked plugins. |
| 226 | OnMessageReceived( |
| 227 | PpapiMsg_PPPInstance_DidDestroy(INTERFACE_ID_PPP_INSTANCE, i->first)); |
| 228 | } |
| 229 | } |
[email protected] | 176c7392 | 2010-12-03 17:32:19 | [diff] [blame] | 230 | } |
| 231 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 232 | void PluginDispatcher::OnMsgSupportsInterface( |
| 233 | const std::string& interface_name, |
| 234 | bool* result) { |
| 235 | *result = false; |
| 236 | |
| 237 | // Setup a proxy for receiving the messages from this interface. |
| 238 | const InterfaceProxy::Info* info = GetPPPInterfaceInfo(interface_name); |
| 239 | if (!info) |
| 240 | return; // Interface not supported by proxy. |
| 241 | |
| 242 | // Check for a cached result. |
| 243 | if (target_proxies_[info->id].get()) { |
| 244 | *result = true; |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | // Query the plugin & cache the result. |
| 249 | const void* interface_functions = GetLocalInterface(interface_name.c_str()); |
| 250 | if (!interface_functions) |
| 251 | return; |
| 252 | target_proxies_[info->id].reset( |
| 253 | info->create_proxy(this, interface_functions)); |
| 254 | *result = true; |
| 255 | } |
| 256 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 257 | } // namespace proxy |
| 258 | } // namespace pp |
| 259 | |