blob: efd4f054093834e09b7015c6a4dfd4287ecdbed9 [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"
13#include "ppapi/c/pp_errors.h"
14#include "ppapi/proxy/interface_proxy.h"
[email protected]2cc062242011-03-10 21:16:3415#include "ppapi/proxy/plugin_message_filter.h"
[email protected]c2932f5e2010-11-03 03:22:3316#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]a08ebea2011-02-13 17:50:2020#if defined(OS_POSIX)
21#include "base/eintr_wrapper.h"
22#include "ipc/ipc_channel_posix.h"
23#endif
24
[email protected]c2932f5e2010-11-03 03:22:3325namespace pp {
26namespace proxy {
27
28namespace {
29
[email protected]465faa22011-02-08 16:31:4630typedef std::map<PP_Instance, PluginDispatcher*> InstanceToDispatcherMap;
31InstanceToDispatcherMap* g_instance_to_dispatcher = NULL;
[email protected]c2932f5e2010-11-03 03:22:3332
[email protected]c2932f5e2010-11-03 03:22:3333} // namespace
34
[email protected]5d84d012010-12-02 17:17:2135PluginDispatcher::PluginDispatcher(base::ProcessHandle remote_process_handle,
[email protected]a08ebea2011-02-13 17:50:2036 GetInterfaceFunc get_interface)
37 : Dispatcher(remote_process_handle, get_interface)
38#if defined(OS_POSIX)
39 , renderer_fd_(-1)
40#endif
41 {
[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]c2932f5e2010-11-03 03:22:3347}
48
49PluginDispatcher::~PluginDispatcher() {
[email protected]a08ebea2011-02-13 17:50:2050#if defined(OS_POSIX)
51 CloseRendererFD();
52#endif
[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;
73 return info->interface;
74}
75
[email protected]2cc062242011-03-10 21:16:3476bool 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]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) {
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]a95986a82010-12-24 06:19:28106bool PluginDispatcher::OnMessageReceived(const IPC::Message& msg) {
[email protected]465faa22011-02-08 16:31:46107 // Handle common control messages.
108 if (Dispatcher::OnMessageReceived(msg))
109 return true;
110
[email protected]c2932f5e2010-11-03 03:22:33111 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
112 // Handle some plugin-specific control messages.
[email protected]a95986a82010-12-24 06:19:28113 bool handled = true;
[email protected]c2932f5e2010-11-03 03:22:33114 IPC_BEGIN_MESSAGE_MAP(PluginDispatcher, msg)
[email protected]465faa22011-02-08 16:31:46115 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface)
[email protected]c2932f5e2010-11-03 03:22:33116 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28117 return handled;
[email protected]c2932f5e2010-11-03 03:22:33118 }
119
[email protected]465faa22011-02-08 16:31:46120 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]c2932f5e2010-11-03 03:22:33156}
157
[email protected]a08ebea2011-02-13 17:50:20158void PluginDispatcher::OnChannelError() {
[email protected]4f15d2842011-02-15 17:36:33159 Dispatcher::OnChannelError();
160
[email protected]a08ebea2011-02-13 17:50:20161 // 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]f56279c2011-02-02 18:12:31168void PluginDispatcher::DidCreateInstance(PP_Instance instance) {
[email protected]465faa22011-02-08 16:31:46169 if (!g_instance_to_dispatcher)
170 g_instance_to_dispatcher = new InstanceToDispatcherMap;
171 (*g_instance_to_dispatcher)[instance] = this;
172
[email protected]f56279c2011-02-02 18:12:31173 instance_map_[instance] = InstanceData();
174}
175
176void 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]465faa22011-02-08 16:31:46180
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]f56279c2011-02-02 18:12:31191}
192
193InstanceData* 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]a08ebea2011-02-13 17:50:20198#if defined(OS_POSIX)
199int PluginDispatcher::GetRendererFD() {
[email protected]4f15d2842011-02-15 17:36:33200 if (renderer_fd_ == -1 && channel())
[email protected]a08ebea2011-02-13 17:50:20201 renderer_fd_ = channel()->GetClientFileDescriptor();
202 return renderer_fd_;
[email protected]c2932f5e2010-11-03 03:22:33203}
204
[email protected]a08ebea2011-02-13 17:50:20205void 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
214void PluginDispatcher::ForceFreeAllInstances() {
[email protected]4f15d2842011-02-15 17:36:33215 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]176c73922010-12-03 17:32:19230}
231
[email protected]465faa22011-02-08 16:31:46232void 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]c2932f5e2010-11-03 03:22:33257} // namespace proxy
258} // namespace pp
259