blob: a7019472ef36eabb79c42eb9e6bf7e8b27f973ec [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]9815108e2011-05-27 21:50:2820#include "ppapi/proxy/ppb_char_set_proxy.h"
21#include "ppapi/proxy/ppb_cursor_control_proxy.h"
[email protected]d259a8e2011-05-18 22:31:0922#include "ppapi/proxy/ppb_font_proxy.h"
[email protected]ceadc392011-06-15 23:04:2423#include "ppapi/proxy/ppb_instance_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3324#include "ppapi/proxy/ppp_class_proxy.h"
[email protected]6239d342011-05-06 22:55:4725#include "ppapi/proxy/resource_creation_proxy.h"
26#include "ppapi/shared_impl/tracker_base.h"
[email protected]c2932f5e2010-11-03 03:22:3327
[email protected]a08ebea2011-02-13 17:50:2028#if defined(OS_POSIX)
29#include "base/eintr_wrapper.h"
30#include "ipc/ipc_channel_posix.h"
31#endif
32
[email protected]c2932f5e2010-11-03 03:22:3333namespace pp {
34namespace proxy {
35
36namespace {
37
[email protected]465faa22011-02-08 16:31:4638typedef std::map<PP_Instance, PluginDispatcher*> InstanceToDispatcherMap;
39InstanceToDispatcherMap* g_instance_to_dispatcher = NULL;
[email protected]c2932f5e2010-11-03 03:22:3340
[email protected]c2932f5e2010-11-03 03:22:3341} // namespace
42
[email protected]5d84d012010-12-02 17:17:2143PluginDispatcher::PluginDispatcher(base::ProcessHandle remote_process_handle,
[email protected]a08ebea2011-02-13 17:50:2044 GetInterfaceFunc get_interface)
[email protected]d259a8e2011-05-18 22:31:0945 : Dispatcher(remote_process_handle, get_interface),
[email protected]208aad792011-05-26 19:05:2846 plugin_delegate_(NULL),
47 received_preferences_(false) {
[email protected]4614f192011-01-21 00:26:4348 SetSerializationRules(new PluginVarSerializationRules);
[email protected]c2932f5e2010-11-03 03:22:3349
50 // As a plugin, we always support the PPP_Class interface. There's no
51 // GetInterface call or name for it, so we insert it into our table now.
[email protected]465faa22011-02-08 16:31:4652 target_proxies_[INTERFACE_ID_PPP_CLASS].reset(new PPP_Class_Proxy(this));
[email protected]6239d342011-05-06 22:55:4753
[email protected]55cdf6052011-05-13 19:22:5354 ::ppapi::TrackerBase::Init(
[email protected]6239d342011-05-06 22:55:4755 &PluginResourceTracker::GetTrackerBaseInstance);
[email protected]c2932f5e2010-11-03 03:22:3356}
57
58PluginDispatcher::~PluginDispatcher() {
[email protected]c2932f5e2010-11-03 03:22:3359}
60
61// static
[email protected]4614f192011-01-21 00:26:4362PluginDispatcher* PluginDispatcher::GetForInstance(PP_Instance instance) {
[email protected]465faa22011-02-08 16:31:4663 if (!g_instance_to_dispatcher)
64 return NULL;
65 InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find(
66 instance);
67 if (found == g_instance_to_dispatcher->end())
68 return NULL;
69 return found->second;
[email protected]4614f192011-01-21 00:26:4370}
71
[email protected]a08ebea2011-02-13 17:50:2072// static
73const void* PluginDispatcher::GetInterfaceFromDispatcher(
74 const char* interface) {
75 // All interfaces the plugin requests of the browser are "PPB".
76 const InterfaceProxy::Info* info = GetPPBInterfaceInfo(interface);
77 if (!info)
78 return NULL;
[email protected]84396dbc2011-04-14 06:33:4279 return info->interface_ptr;
[email protected]a08ebea2011-02-13 17:50:2080}
81
[email protected]e2614c62011-04-16 22:12:4582bool PluginDispatcher::InitPluginWithChannel(
[email protected]d259a8e2011-05-18 22:31:0983 PluginDelegate* delegate,
[email protected]2cc062242011-03-10 21:16:3484 const IPC::ChannelHandle& channel_handle,
85 bool is_client) {
86 if (!Dispatcher::InitWithChannel(delegate, channel_handle, is_client))
87 return false;
[email protected]d259a8e2011-05-18 22:31:0988 plugin_delegate_ = delegate;
[email protected]2cc062242011-03-10 21:16:3489
90 // The message filter will intercept and process certain messages directly
91 // on the I/O thread.
92 channel()->AddFilter(
93 new PluginMessageFilter(delegate->GetGloballySeenInstanceIDSet()));
94 return true;
95}
96
[email protected]7cf40912010-12-09 18:25:0397bool PluginDispatcher::IsPlugin() const {
98 return true;
99}
100
[email protected]b00bbb32011-03-30 19:02:14101bool PluginDispatcher::Send(IPC::Message* msg) {
[email protected]366ae242011-05-10 02:23:58102 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::Send",
103 "Class", IPC_MESSAGE_ID_CLASS(msg->type()),
104 "Line", IPC_MESSAGE_ID_LINE(msg->type()));
[email protected]b00bbb32011-03-30 19:02:14105 // We always want plugin->renderer messages to arrive in-order. If some sync
106 // and some async messages are send in response to a synchronous
107 // renderer->plugin call, the sync reply will be processed before the async
108 // reply, and everything will be confused.
109 //
110 // Allowing all async messages to unblock the renderer means more reentrancy
111 // there but gives correct ordering.
112 msg->set_unblock(true);
113 return Dispatcher::Send(msg);
114}
115
[email protected]a95986a82010-12-24 06:19:28116bool PluginDispatcher::OnMessageReceived(const IPC::Message& msg) {
[email protected]366ae242011-05-10 02:23:58117 TRACE_EVENT2("ppapi proxy", "PluginDispatcher::OnMessageReceived",
118 "Class", IPC_MESSAGE_ID_CLASS(msg.type()),
119 "Line", IPC_MESSAGE_ID_LINE(msg.type()));
[email protected]465faa22011-02-08 16:31:46120 // Handle common control messages.
121 if (Dispatcher::OnMessageReceived(msg))
122 return true;
123
[email protected]c2932f5e2010-11-03 03:22:33124 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
125 // Handle some plugin-specific control messages.
[email protected]a95986a82010-12-24 06:19:28126 bool handled = true;
[email protected]c2932f5e2010-11-03 03:22:33127 IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg)
[email protected]465faa22011-02-08 16:31:46128 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface)
[email protected]208aad792011-05-26 19:05:28129 IPC_MESSAGE_HANDLER(PpapiMsg_SetPreferences, OnMsgSetPreferences)
[email protected]c2932f5e2010-11-03 03:22:33130 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28131 return handled;
[email protected]c2932f5e2010-11-03 03:22:33132 }
133
[email protected]465faa22011-02-08 16:31:46134 if (msg.routing_id() <= 0 && msg.routing_id() >= INTERFACE_ID_COUNT) {
135 // Host is sending us garbage. Since it's supposed to be trusted, this
136 // isn't supposed to happen. Crash here in all builds in case the renderer
137 // is compromised.
138 CHECK(false);
139 return true;
140 }
141
142 // There are two cases:
143 //
144 // * The first case is that the host is calling a PPP interface. It will
145 // always do a check for the interface before sending messages, and this
146 // will create the necessary interface proxy at that time. So when we
147 // actually receive a message, we know such a proxy will exist.
148 //
149 // * The second case is that the host is sending a response to the plugin
150 // side of a PPB interface (some, like the URL loader, have complex
151 // response messages). Since the host is trusted and not supposed to be
152 // doing silly things, we can just create a PPB proxy project on demand the
153 // first time it's needed.
154
155 InterfaceProxy* proxy = target_proxies_[msg.routing_id()].get();
156 if (!proxy) {
157 // Handle the first time the host calls a PPB reply interface by
158 // autocreating it.
159 const InterfaceProxy::Info* info = GetPPBInterfaceInfo(
160 static_cast<InterfaceID>(msg.routing_id()));
161 if (!info) {
162 NOTREACHED();
163 return true;
164 }
165 proxy = info->create_proxy(this, NULL);
166 target_proxies_[info->id].reset(proxy);
167 }
168
169 return proxy->OnMessageReceived(msg);
[email protected]c2932f5e2010-11-03 03:22:33170}
171
[email protected]a08ebea2011-02-13 17:50:20172void PluginDispatcher::OnChannelError() {
[email protected]4f15d2842011-02-15 17:36:33173 Dispatcher::OnChannelError();
174
[email protected]4b417e52011-04-18 22:51:08175 // The renderer has crashed or exited. This channel and all instances
176 // associated with it are no longer valid.
[email protected]a08ebea2011-02-13 17:50:20177 ForceFreeAllInstances();
178 // TODO(brettw) free resources too!
179 delete this;
180}
181
[email protected]f56279c2011-02-02 18:12:31182void PluginDispatcher::DidCreateInstance(PP_Instance instance) {
[email protected]465faa22011-02-08 16:31:46183 if (!g_instance_to_dispatcher)
184 g_instance_to_dispatcher = new InstanceToDispatcherMap;
185 (*g_instance_to_dispatcher)[instance] = this;
186
[email protected]f56279c2011-02-02 18:12:31187 instance_map_[instance] = InstanceData();
188}
189
190void PluginDispatcher::DidDestroyInstance(PP_Instance instance) {
191 InstanceDataMap::iterator it = instance_map_.find(instance);
192 if (it != instance_map_.end())
193 instance_map_.erase(it);
[email protected]465faa22011-02-08 16:31:46194
195 if (g_instance_to_dispatcher) {
196 InstanceToDispatcherMap::iterator found = g_instance_to_dispatcher->find(
197 instance);
198 if (found != g_instance_to_dispatcher->end()) {
199 DCHECK(found->second == this);
200 g_instance_to_dispatcher->erase(found);
201 } else {
202 NOTREACHED();
203 }
204 }
[email protected]f56279c2011-02-02 18:12:31205}
206
207InstanceData* PluginDispatcher::GetInstanceData(PP_Instance instance) {
208 InstanceDataMap::iterator it = instance_map_.find(instance);
209 return (it == instance_map_.end()) ? NULL : &it->second;
210}
211
[email protected]7a1f7c6f2011-05-10 21:17:48212void PluginDispatcher::PostToWebKitThread(
213 const tracked_objects::Location& from_here,
214 const base::Closure& task) {
[email protected]d259a8e2011-05-18 22:31:09215 return plugin_delegate_->PostToWebKitThread(from_here, task);
216}
217
218bool PluginDispatcher::SendToBrowser(IPC::Message* msg) {
219 return plugin_delegate_->SendToBrowser(msg);
[email protected]7a1f7c6f2011-05-10 21:17:48220}
221
[email protected]55cdf6052011-05-13 19:22:53222ppapi::WebKitForwarding* PluginDispatcher::GetWebKitForwarding() {
[email protected]d259a8e2011-05-18 22:31:09223 return plugin_delegate_->GetWebKitForwarding();
[email protected]7a1f7c6f2011-05-10 21:17:48224}
225
[email protected]55cdf6052011-05-13 19:22:53226::ppapi::FunctionGroupBase* PluginDispatcher::GetFunctionAPI(
[email protected]6239d342011-05-06 22:55:47227 pp::proxy::InterfaceID id) {
[email protected]9815108e2011-05-27 21:50:28228 scoped_ptr< ::ppapi::FunctionGroupBase >& proxy = function_proxies_[id];
[email protected]6239d342011-05-06 22:55:47229
[email protected]9815108e2011-05-27 21:50:28230 if (proxy.get())
231 return proxy.get();
[email protected]6239d342011-05-06 22:55:47232
[email protected]9815108e2011-05-27 21:50:28233 if (id == INTERFACE_ID_PPB_CHAR_SET)
234 proxy.reset(new PPB_CharSet_Proxy(this, NULL));
235 else if(id == INTERFACE_ID_PPB_CURSORCONTROL)
236 proxy.reset(new PPB_CursorControl_Proxy(this, NULL));
237 else if (id == INTERFACE_ID_PPB_FONT)
238 proxy.reset(new PPB_Font_Proxy(this, NULL));
[email protected]ceadc392011-06-15 23:04:24239 else if (id == INTERFACE_ID_PPB_INSTANCE)
240 proxy.reset(new PPB_Instance_Proxy(this, NULL));
[email protected]9815108e2011-05-27 21:50:28241 else if (id == INTERFACE_ID_RESOURCE_CREATION)
242 proxy.reset(new ResourceCreationProxy(this));
243
244 return proxy.get();
[email protected]6239d342011-05-06 22:55:47245}
246
[email protected]a08ebea2011-02-13 17:50:20247void PluginDispatcher::ForceFreeAllInstances() {
[email protected]4f15d2842011-02-15 17:36:33248 if (!g_instance_to_dispatcher)
249 return;
250
251 // Iterating will remove each item from the map, so we need to make a copy
252 // to avoid things changing out from under is.
253 InstanceToDispatcherMap temp_map = *g_instance_to_dispatcher;
254 for (InstanceToDispatcherMap::iterator i = temp_map.begin();
255 i != temp_map.end(); ++i) {
256 if (i->second == this) {
257 // Synthesize an "instance destroyed" message, this will notify the
258 // plugin and also remove it from our list of tracked plugins.
[email protected]4585fbc2011-06-13 17:17:56259 PpapiMsg_PPPInstance_DidDestroy msg(INTERFACE_ID_PPP_INSTANCE, i->first);
260 OnMessageReceived(msg);
261 delete msg.GetReplyDeserializer();
[email protected]4f15d2842011-02-15 17:36:33262 }
263 }
[email protected]176c73922010-12-03 17:32:19264}
265
[email protected]465faa22011-02-08 16:31:46266void PluginDispatcher::OnMsgSupportsInterface(
267 const std::string& interface_name,
268 bool* result) {
269 *result = false;
270
271 // Setup a proxy for receiving the messages from this interface.
272 const InterfaceProxy::Info* info = GetPPPInterfaceInfo(interface_name);
273 if (!info)
274 return; // Interface not supported by proxy.
275
276 // Check for a cached result.
277 if (target_proxies_[info->id].get()) {
278 *result = true;
279 return;
280 }
281
282 // Query the plugin & cache the result.
283 const void* interface_functions = GetLocalInterface(interface_name.c_str());
284 if (!interface_functions)
285 return;
286 target_proxies_[info->id].reset(
287 info->create_proxy(this, interface_functions));
288 *result = true;
289}
290
[email protected]208aad792011-05-26 19:05:28291void PluginDispatcher::OnMsgSetPreferences(const ::ppapi::Preferences& prefs) {
292 // The renderer may send us preferences more than once (currently this
293 // happens every time a new plugin instance is created). Since we don't have
294 // a way to signal to the plugin that the preferences have changed, changing
295 // the default fonts and such in the middle of a running plugin could be
296 // confusing to it. As a result, we never allow the preferences to be changed
297 // once they're set. The user will have to restart to get new font prefs
298 // propogated to plugins.
299 if (!received_preferences_) {
300 received_preferences_ = true;
301 preferences_ = prefs;
302 }
303}
304
[email protected]c2932f5e2010-11-03 03:22:33305} // namespace proxy
306} // namespace pp