blob: e9a189a4f0b2e2af666b89341d81c9281f536eae [file] [log] [blame]
[email protected]a08ebea2011-02-13 17:50:201// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]c2932f5e2010-11-03 03:22:332// 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]709a847e2010-11-10 01:16:119#include "base/compiler_specific.h"
[email protected]c2932f5e2010-11-03 03:22:3310#include "base/logging.h"
11#include "ipc/ipc_message.h"
12#include "ipc/ipc_sync_channel.h"
[email protected]366ae242011-05-10 02:23:5813#include "base/debug/trace_event.h"
[email protected]c2932f5e2010-11-03 03:22:3314#include "ppapi/c/pp_errors.h"
15#include "ppapi/proxy/interface_proxy.h"
[email protected]2cc062242011-03-10 21:16:3416#include "ppapi/proxy/plugin_message_filter.h"
[email protected]6239d342011-05-06 22:55:4717#include "ppapi/proxy/plugin_resource_tracker.h"
[email protected]c2932f5e2010-11-03 03:22:3318#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]6239d342011-05-06 22:55:4721#include "ppapi/proxy/resource_creation_proxy.h"
22#include "ppapi/shared_impl/tracker_base.h"
[email protected]c2932f5e2010-11-03 03:22:3323
[email protected]a08ebea2011-02-13 17:50:2024#if defined(OS_POSIX)
25#include "base/eintr_wrapper.h"
26#include "ipc/ipc_channel_posix.h"
27#endif
28
[email protected]c2932f5e2010-11-03 03:22:3329namespace pp {
30namespace proxy {
31
32namespace {
33
[email protected]465faa22011-02-08 16:31:4634typedef std::map<PP_Instance, PluginDispatcher*> InstanceToDispatcherMap;
35InstanceToDispatcherMap* g_instance_to_dispatcher = NULL;
[email protected]c2932f5e2010-11-03 03:22:3336
[email protected]c2932f5e2010-11-03 03:22:3337} // namespace
38
[email protected]5d84d012010-12-02 17:17:2139PluginDispatcher::PluginDispatcher(base::ProcessHandle remote_process_handle,
[email protected]a08ebea2011-02-13 17:50:2040 GetInterfaceFunc get_interface)
[email protected]b04dbbc2011-04-08 22:18:5641 : Dispatcher(remote_process_handle, get_interface) {
[email protected]4614f192011-01-21 00:26:4342 SetSerializationRules(new PluginVarSerializationRules);
[email protected]c2932f5e2010-11-03 03:22:3343
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]465faa22011-02-08 16:31:4646 target_proxies_[INTERFACE_ID_PPP_CLASS].reset(new PPP_Class_Proxy(this));
[email protected]6239d342011-05-06 22:55:4747
48 ::ppapi::shared_impl::TrackerBase::Init(
49 &PluginResourceTracker::GetTrackerBaseInstance);
[email protected]c2932f5e2010-11-03 03:22:3350}
51
52PluginDispatcher::~PluginDispatcher() {
[email protected]c2932f5e2010-11-03 03:22:3353}
54
55// static
[email protected]4614f192011-01-21 00:26:4356PluginDispatcher* PluginDispatcher::GetForInstance(PP_Instance instance) {
[email protected]465faa22011-02-08 16:31:4657 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]4614f192011-01-21 00:26:4364}
65
[email protected]a08ebea2011-02-13 17:50:2066// static
67const 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]84396dbc2011-04-14 06:33:4273 return info->interface_ptr;
[email protected]a08ebea2011-02-13 17:50:2074}
75
[email protected]e2614c62011-04-16 22:12:4576bool PluginDispatcher::InitPluginWithChannel(
77 PluginDispatcher::Delegate* delegate,
[email protected]2cc062242011-03-10 21:16:3478 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]7cf40912010-12-09 18:25:0390bool PluginDispatcher::IsPlugin() const {
91 return true;
92}
93
[email protected]b00bbb32011-03-30 19:02:1494bool PluginDispatcher::Send(IPC::Message* msg) {
[email protected]366ae242011-05-10 02:23:5895 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::Send",
96 "Class", IPC_MESSAGE_ID_CLASS(msg->type()),
97 "Line", IPC_MESSAGE_ID_LINE(msg->type()));
[email protected]b00bbb32011-03-30 19:02:1498 // 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]a95986a82010-12-24 06:19:28109bool PluginDispatcher::OnMessageReceived(const IPC::Message& msg) {
[email protected]366ae242011-05-10 02:23:58110 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::OnMessageReceived",
111 "Class", IPC_MESSAGE_ID_CLASS(msg.type()),
112 "Line", IPC_MESSAGE_ID_LINE(msg.type()));
[email protected]465faa22011-02-08 16:31:46113 // Handle common control messages.
114 if (Dispatcher::OnMessageReceived(msg))
115 return true;
116
[email protected]c2932f5e2010-11-03 03:22:33117 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
118 // Handle some plugin-specific control messages.
[email protected]a95986a82010-12-24 06:19:28119 bool handled = true;
[email protected]c2932f5e2010-11-03 03:22:33120 IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg)
[email protected]465faa22011-02-08 16:31:46121 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface)
[email protected]c2932f5e2010-11-03 03:22:33122 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28123 return handled;
[email protected]c2932f5e2010-11-03 03:22:33124 }
125
[email protected]465faa22011-02-08 16:31:46126 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]c2932f5e2010-11-03 03:22:33162}
163
[email protected]a08ebea2011-02-13 17:50:20164void PluginDispatcher::OnChannelError() {
[email protected]4f15d2842011-02-15 17:36:33165 Dispatcher::OnChannelError();
166
[email protected]4b417e52011-04-18 22:51:08167 // The renderer has crashed or exited. This channel and all instances
168 // associated with it are no longer valid.
[email protected]a08ebea2011-02-13 17:50:20169 ForceFreeAllInstances();
170 // TODO(brettw) free resources too!
171 delete this;
172}
173
[email protected]f56279c2011-02-02 18:12:31174void PluginDispatcher::DidCreateInstance(PP_Instance instance) {
[email protected]465faa22011-02-08 16:31:46175 if (!g_instance_to_dispatcher)
176 g_instance_to_dispatcher = new InstanceToDispatcherMap;
177 (*g_instance_to_dispatcher)[instance] = this;
178
[email protected]f56279c2011-02-02 18:12:31179 instance_map_[instance] = InstanceData();
180}
181
182void 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]465faa22011-02-08 16:31:46186
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]f56279c2011-02-02 18:12:31197}
198
199InstanceData* 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]6239d342011-05-06 22:55:47204::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]a08ebea2011-02-13 17:50:20215void PluginDispatcher::ForceFreeAllInstances() {
[email protected]4f15d2842011-02-15 17:36:33216 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]176c73922010-12-03 17:32:19231}
232
[email protected]465faa22011-02-08 16:31:46233void 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]c2932f5e2010-11-03 03:22:33258} // namespace proxy
259} // namespace pp
260