blob: 4ce47c6f44e7125bde6941634463c05f567eb7af [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"
[email protected]d259a8e2011-05-18 22:31:0920#include "ppapi/proxy/ppb_font_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3321#include "ppapi/proxy/ppp_class_proxy.h"
[email protected]6239d342011-05-06 22:55:4722#include "ppapi/proxy/resource_creation_proxy.h"
23#include "ppapi/shared_impl/tracker_base.h"
[email protected]c2932f5e2010-11-03 03:22:3324
[email protected]a08ebea2011-02-13 17:50:2025#if defined(OS_POSIX)
26#include "base/eintr_wrapper.h"
27#include "ipc/ipc_channel_posix.h"
28#endif
29
[email protected]c2932f5e2010-11-03 03:22:3330namespace pp {
31namespace proxy {
32
33namespace {
34
[email protected]465faa22011-02-08 16:31:4635typedef std::map<PP_Instance, PluginDispatcher*> InstanceToDispatcherMap;
36InstanceToDispatcherMap* g_instance_to_dispatcher = NULL;
[email protected]c2932f5e2010-11-03 03:22:3337
[email protected]c2932f5e2010-11-03 03:22:3338} // namespace
39
[email protected]5d84d012010-12-02 17:17:2140PluginDispatcher::PluginDispatcher(base::ProcessHandle remote_process_handle,
[email protected]a08ebea2011-02-13 17:50:2041 GetInterfaceFunc get_interface)
[email protected]d259a8e2011-05-18 22:31:0942 : Dispatcher(remote_process_handle, get_interface),
[email protected]208aad792011-05-26 19:05:2843 plugin_delegate_(NULL),
44 received_preferences_(false) {
[email protected]4614f192011-01-21 00:26:4345 SetSerializationRules(new PluginVarSerializationRules);
[email protected]c2932f5e2010-11-03 03:22:3346
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]465faa22011-02-08 16:31:4649 target_proxies_[INTERFACE_ID_PPP_CLASS].reset(new PPP_Class_Proxy(this));
[email protected]6239d342011-05-06 22:55:4750
[email protected]55cdf6052011-05-13 19:22:5351 ::ppapi::TrackerBase::Init(
[email protected]6239d342011-05-06 22:55:4752 &PluginResourceTracker::GetTrackerBaseInstance);
[email protected]c2932f5e2010-11-03 03:22:3353}
54
55PluginDispatcher::~PluginDispatcher() {
[email protected]c2932f5e2010-11-03 03:22:3356}
57
58// static
[email protected]4614f192011-01-21 00:26:4359PluginDispatcher* PluginDispatcher::GetForInstance(PP_Instance instance) {
[email protected]465faa22011-02-08 16:31:4660 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]4614f192011-01-21 00:26:4367}
68
[email protected]a08ebea2011-02-13 17:50:2069// static
70const 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]84396dbc2011-04-14 06:33:4276 return info->interface_ptr;
[email protected]a08ebea2011-02-13 17:50:2077}
78
[email protected]e2614c62011-04-16 22:12:4579bool PluginDispatcher::InitPluginWithChannel(
[email protected]d259a8e2011-05-18 22:31:0980 PluginDelegate* delegate,
[email protected]2cc062242011-03-10 21:16:3481 const IPC::ChannelHandle& channel_handle,
82 bool is_client) {
83 if (!Dispatcher::InitWithChannel(delegate, channel_handle, is_client))
84 return false;
[email protected]d259a8e2011-05-18 22:31:0985 plugin_delegate_ = delegate;
[email protected]2cc062242011-03-10 21:16:3486
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]7cf40912010-12-09 18:25:0394bool PluginDispatcher::IsPlugin() const {
95 return true;
96}
97
[email protected]b00bbb32011-03-30 19:02:1498bool PluginDispatcher::Send(IPC::Message* msg) {
[email protected]366ae242011-05-10 02:23:5899 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::Send",
100 "Class", IPC_MESSAGE_ID_CLASS(msg->type()),
101 "Line", IPC_MESSAGE_ID_LINE(msg->type()));
[email protected]b00bbb32011-03-30 19:02:14102 // 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]a95986a82010-12-24 06:19:28113bool PluginDispatcher::OnMessageReceived(const IPC::Message& msg) {
[email protected]366ae242011-05-10 02:23:58114 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::OnMessageReceived",
115 "Class", IPC_MESSAGE_ID_CLASS(msg.type()),
116 "Line", IPC_MESSAGE_ID_LINE(msg.type()));
[email protected]465faa22011-02-08 16:31:46117 // Handle common control messages.
118 if (Dispatcher::OnMessageReceived(msg))
119 return true;
120
[email protected]c2932f5e2010-11-03 03:22:33121 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
122 // Handle some plugin-specific control messages.
[email protected]a95986a82010-12-24 06:19:28123 bool handled = true;
[email protected]c2932f5e2010-11-03 03:22:33124 IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg)
[email protected]465faa22011-02-08 16:31:46125 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface)
[email protected]208aad792011-05-26 19:05:28126 IPC_MESSAGE_HANDLER(PpapiMsg_SetPreferences, OnMsgSetPreferences)
[email protected]c2932f5e2010-11-03 03:22:33127 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28128 return handled;
[email protected]c2932f5e2010-11-03 03:22:33129 }
130
[email protected]465faa22011-02-08 16:31:46131 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]c2932f5e2010-11-03 03:22:33167}
168
[email protected]a08ebea2011-02-13 17:50:20169void PluginDispatcher::OnChannelError() {
[email protected]4f15d2842011-02-15 17:36:33170 Dispatcher::OnChannelError();
171
[email protected]4b417e52011-04-18 22:51:08172 // The renderer has crashed or exited. This channel and all instances
173 // associated with it are no longer valid.
[email protected]a08ebea2011-02-13 17:50:20174 ForceFreeAllInstances();
175 // TODO(brettw) free resources too!
176 delete this;
177}
178
[email protected]f56279c2011-02-02 18:12:31179void PluginDispatcher::DidCreateInstance(PP_Instance instance) {
[email protected]465faa22011-02-08 16:31:46180 if (!g_instance_to_dispatcher)
181 g_instance_to_dispatcher = new InstanceToDispatcherMap;
182 (*g_instance_to_dispatcher)[instance] = this;
183
[email protected]f56279c2011-02-02 18:12:31184 instance_map_[instance] = InstanceData();
185}
186
187void 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]465faa22011-02-08 16:31:46191
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]f56279c2011-02-02 18:12:31202}
203
204InstanceData* 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]7a1f7c6f2011-05-10 21:17:48209void PluginDispatcher::PostToWebKitThread(
210 const tracked_objects::Location& from_here,
211 const base::Closure& task) {
[email protected]d259a8e2011-05-18 22:31:09212 return plugin_delegate_->PostToWebKitThread(from_here, task);
213}
214
215bool PluginDispatcher::SendToBrowser(IPC::Message* msg) {
216 return plugin_delegate_->SendToBrowser(msg);
[email protected]7a1f7c6f2011-05-10 21:17:48217}
218
[email protected]55cdf6052011-05-13 19:22:53219ppapi::WebKitForwarding* PluginDispatcher::GetWebKitForwarding() {
[email protected]d259a8e2011-05-18 22:31:09220 return plugin_delegate_->GetWebKitForwarding();
[email protected]7a1f7c6f2011-05-10 21:17:48221}
222
[email protected]55cdf6052011-05-13 19:22:53223::ppapi::FunctionGroupBase* PluginDispatcher::GetFunctionAPI(
[email protected]6239d342011-05-06 22:55:47224 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]d259a8e2011-05-18 22:31:09230 if (id == INTERFACE_ID_PPB_FONT)
231 function_proxies_[id].reset(new PPB_Font_Proxy(this, NULL));
[email protected]6239d342011-05-06 22:55:47232
233 return function_proxies_[id].get();
234}
235
[email protected]a08ebea2011-02-13 17:50:20236void PluginDispatcher::ForceFreeAllInstances() {
[email protected]4f15d2842011-02-15 17:36:33237 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]176c73922010-12-03 17:32:19252}
253
[email protected]465faa22011-02-08 16:31:46254void 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]208aad792011-05-26 19:05:28279void 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]c2932f5e2010-11-03 03:22:33293} // namespace proxy
294} // namespace pp
295