blob: 9b7958c4ca0af88f7c03a64f0e7b27d3bbf9edc5 [file] [log] [blame]
[email protected]c2932f5e2010-11-03 03:22:331// Copyright (c) 2010 The Chromium Authors. All rights reserved.
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/dispatcher.h"
6
7#include <string.h> // For memset.
8
9#include <map>
10
[email protected]709a847e2010-11-10 01:16:1111#include "base/compiler_specific.h"
[email protected]c2932f5e2010-11-03 03:22:3312#include "base/logging.h"
13#include "ipc/ipc_message.h"
14#include "ipc/ipc_sync_channel.h"
[email protected]5d84d012010-12-02 17:17:2115#include "ppapi/c/dev/ppb_audio_config_dev.h"
16#include "ppapi/c/dev/ppb_audio_dev.h"
[email protected]43a40202010-11-12 16:25:0117#include "ppapi/c/dev/ppb_buffer_dev.h"
[email protected]9ca952d2010-11-11 20:43:5018#include "ppapi/c/dev/ppb_char_set_dev.h"
[email protected]a3010332010-11-12 07:09:3519#include "ppapi/c/dev/ppb_cursor_control_dev.h"
[email protected]43a40202010-11-12 16:25:0120#include "ppapi/c/dev/ppb_cursor_control_dev.h"
21#include "ppapi/c/dev/ppb_font_dev.h"
22#include "ppapi/c/dev/ppb_fullscreen_dev.h"
[email protected]a3010332010-11-12 07:09:3523#include "ppapi/c/dev/ppb_fullscreen_dev.h"
[email protected]799d1ab2010-11-09 17:16:2824#include "ppapi/c/dev/ppb_testing_dev.h"
[email protected]c2932f5e2010-11-03 03:22:3325#include "ppapi/c/dev/ppb_var_deprecated.h"
26#include "ppapi/c/pp_errors.h"
27#include "ppapi/c/ppb_core.h"
28#include "ppapi/c/ppb_graphics_2d.h"
29#include "ppapi/c/ppb_image_data.h"
30#include "ppapi/c/ppb_instance.h"
[email protected]5a3f62852010-11-10 21:43:0131#include "ppapi/c/ppb_url_loader.h"
32#include "ppapi/c/ppb_url_request_info.h"
33#include "ppapi/c/ppb_url_response_info.h"
[email protected]9ca952d2010-11-11 20:43:5034#include "ppapi/c/ppp_instance.h"
[email protected]5f2517612010-12-02 22:36:4835#include "ppapi/c/trusted/ppb_url_loader_trusted.h"
[email protected]9ca952d2010-11-11 20:43:5036#include "ppapi/proxy/ppapi_messages.h"
[email protected]5d84d012010-12-02 17:17:2137#include "ppapi/proxy/ppb_audio_config_proxy.h"
38#include "ppapi/proxy/ppb_audio_proxy.h"
[email protected]43a40202010-11-12 16:25:0139#include "ppapi/proxy/ppb_buffer_proxy.h"
[email protected]9ca952d2010-11-11 20:43:5040#include "ppapi/proxy/ppb_char_set_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3341#include "ppapi/proxy/ppb_core_proxy.h"
[email protected]a3010332010-11-12 07:09:3542#include "ppapi/proxy/ppb_cursor_control_proxy.h"
[email protected]5f2517612010-12-02 22:36:4843#include "ppapi/proxy/ppb_flash_proxy.h"
[email protected]9ca952d2010-11-11 20:43:5044#include "ppapi/proxy/ppb_font_proxy.h"
[email protected]a3010332010-11-12 07:09:3545#include "ppapi/proxy/ppb_fullscreen_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3346#include "ppapi/proxy/ppb_graphics_2d_proxy.h"
47#include "ppapi/proxy/ppb_image_data_proxy.h"
48#include "ppapi/proxy/ppb_instance_proxy.h"
[email protected]43a40202010-11-12 16:25:0149#include "ppapi/proxy/ppb_pdf_proxy.h"
[email protected]799d1ab2010-11-09 17:16:2850#include "ppapi/proxy/ppb_testing_proxy.h"
51#include "ppapi/proxy/ppb_url_loader_proxy.h"
52#include "ppapi/proxy/ppb_url_request_info_proxy.h"
53#include "ppapi/proxy/ppb_url_response_info_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3354#include "ppapi/proxy/ppb_var_deprecated_proxy.h"
55#include "ppapi/proxy/ppp_class_proxy.h"
56#include "ppapi/proxy/ppp_instance_proxy.h"
57#include "ppapi/proxy/var_serialization_rules.h"
[email protected]0bd753682010-12-16 18:15:5258#include "webkit/plugins/ppapi/ppb_pdf.h"
59#include "webkit/plugins/ppapi/ppb_flash.h"
[email protected]c2932f5e2010-11-03 03:22:3360
61namespace pp {
62namespace proxy {
63
[email protected]5d84d012010-12-02 17:17:2164Dispatcher::Dispatcher(base::ProcessHandle remote_process_handle,
65 GetInterfaceFunc local_get_interface)
[email protected]c2932f5e2010-11-03 03:22:3366 : pp_module_(0),
[email protected]5d84d012010-12-02 17:17:2167 remote_process_handle_(remote_process_handle),
[email protected]5f2517612010-12-02 22:36:4868 disallow_trusted_interfaces_(false), // TODO(brettw) make this settable.
[email protected]c2932f5e2010-11-03 03:22:3369 local_get_interface_(local_get_interface),
70 declared_supported_remote_interfaces_(false),
[email protected]709a847e2010-11-10 01:16:1171 callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
[email protected]c2932f5e2010-11-03 03:22:3372 memset(id_to_proxy_, 0,
73 static_cast<int>(INTERFACE_ID_COUNT) * sizeof(InterfaceProxy*));
74}
75
76Dispatcher::~Dispatcher() {
77}
78
79bool Dispatcher::InitWithChannel(MessageLoop* ipc_message_loop,
[email protected]42ce94e2010-12-08 19:28:0980 const IPC::ChannelHandle& channel_handle,
[email protected]c2932f5e2010-11-03 03:22:3381 bool is_client,
82 base::WaitableEvent* shutdown_event) {
83 IPC::Channel::Mode mode = is_client ? IPC::Channel::MODE_CLIENT
84 : IPC::Channel::MODE_SERVER;
[email protected]42ce94e2010-12-08 19:28:0985 channel_.reset(new IPC::SyncChannel(channel_handle, mode, this,
[email protected]c2932f5e2010-11-03 03:22:3386 ipc_message_loop, false, shutdown_event));
87 return true;
88}
89
90void Dispatcher::OnMessageReceived(const IPC::Message& msg) {
91 // Control messages.
92 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
93 IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg)
94 IPC_MESSAGE_HANDLER(PpapiMsg_DeclareInterfaces,
95 OnMsgDeclareInterfaces)
96 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface)
97 IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_,
98 CallbackTracker::ReceiveExecuteSerializedCallback)
99 IPC_END_MESSAGE_MAP()
100 return;
101 }
102
103 // Interface-specific messages.
104 if (msg.routing_id() > 0 && msg.routing_id() < INTERFACE_ID_COUNT) {
105 InterfaceProxy* proxy = id_to_proxy_[msg.routing_id()];
106 if (proxy)
107 proxy->OnMessageReceived(msg);
108 else
109 NOTREACHED();
110 // TODO(brettw): kill the plugin if it starts sending invalid messages?
111 }
112}
113
114void Dispatcher::SetSerializationRules(
115 VarSerializationRules* var_serialization_rules) {
116 serialization_rules_.reset(var_serialization_rules);
117}
118
119void Dispatcher::InjectProxy(InterfaceID id,
120 const std::string& name,
121 InterfaceProxy* proxy) {
122 proxies_[name] = linked_ptr<InterfaceProxy>(proxy);
123 id_to_proxy_[id] = proxy;
124}
125
126const void* Dispatcher::GetLocalInterface(const char* interface) {
127 return local_get_interface_(interface);
128}
129
130const void* Dispatcher::GetProxiedInterface(const std::string& interface) {
131 // See if we already know about this interface and have created a host.
132 ProxyMap::const_iterator found = proxies_.find(interface);
133 if (found != proxies_.end())
134 return found->second->GetSourceInterface();
135
136 // When the remote side has sent us a declared list of all interfaces it
137 // supports and we don't have it in our list, we know the requested interface
138 // doesn't exist and we can return failure.
139 if (declared_supported_remote_interfaces_)
140 return NULL;
141
142 if (!RemoteSupportsTargetInterface(interface))
143 return NULL;
144
145 linked_ptr<InterfaceProxy> proxy(CreateProxyForInterface(interface, NULL));
146 if (!proxy.get())
147 return NULL; // Don't know how to proxy this interface.
148
149 // Save our proxy.
150 proxies_[interface] = proxy;
151 id_to_proxy_[proxy->GetInterfaceId()] = proxy.get();
152 return proxy->GetSourceInterface();
153}
154
155bool Dispatcher::Send(IPC::Message* msg) {
156 return channel_->Send(msg);
157}
158
159bool Dispatcher::RemoteSupportsTargetInterface(const std::string& interface) {
160 bool result = false;
161 Send(new PpapiMsg_SupportsInterface(interface, &result));
162 return result;
163}
164
165bool Dispatcher::IsInterfaceTrusted(const std::string& interface) {
166 // FIXME(brettw)
167 (void)interface;
168 return false;
169}
170
171bool Dispatcher::SetupProxyForTargetInterface(const std::string& interface) {
172 // If we already have a proxy that knows about the locally-implemented
173 // interface, we know it's supported and don't need to re-query.
174 ProxyMap::const_iterator found = proxies_.find(interface);
175 if (found != proxies_.end())
176 return true;
177
178 if (disallow_trusted_interfaces_ && IsInterfaceTrusted(interface))
179 return false;
180
181 // Create the proxy if it doesn't exist and set the local interface on it.
182 // This also handles the case where possibly an interface could be supported
183 // by both the local and remote side.
184 const void* interface_functions = local_get_interface_(interface.c_str());
185 if (!interface_functions)
186 return false;
187 InterfaceProxy* proxy = CreateProxyForInterface(interface,
188 interface_functions);
189 if (!proxy)
190 return false;
191
192 proxies_[interface] = linked_ptr<InterfaceProxy>(proxy);
193 id_to_proxy_[proxy->GetInterfaceId()] = proxy;
194 return true;
195}
196
197void Dispatcher::OnMsgSupportsInterface(const std::string& interface_name,
198 bool* result) {
199 *result = SetupProxyForTargetInterface(interface_name);
200}
201
202void Dispatcher::OnMsgDeclareInterfaces(
203 const std::vector<std::string>& interfaces) {
204 // Make proxies for all the interfaces it supports that we also support.
205 for (size_t i = 0; i < interfaces.size(); i++) {
206 // Possibly the plugin could request an interface before the "declare"
207 // message is received, so we could already have an entry for this
208 // interface. In this case, we can just skip to the next one.
209 if (proxies_.find(interfaces[i]) != proxies_.end())
210 continue;
211
212 linked_ptr<InterfaceProxy> proxy(CreateProxyForInterface(interfaces[i],
213 NULL));
214 if (!proxy.get()) {
215 // Since only the browser declares supported interfaces, we should never
216 // get one we don't support.
217 //NOTREACHED() << "Remote side declaring an unsupported proxy.";
218 continue;
219 }
220 proxies_[interfaces[i]] = proxy;
221 id_to_proxy_[proxy->GetInterfaceId()] = proxy.get();
222 }
223}
224
225InterfaceProxy* Dispatcher::CreateProxyForInterface(
226 const std::string& interface_name,
227 const void* interface_functions) {
[email protected]5d84d012010-12-02 17:17:21228 if (interface_name == PPB_AUDIO_CONFIG_DEV_INTERFACE)
229 return new PPB_AudioConfig_Proxy(this, interface_functions);
230 if (interface_name == PPB_AUDIO_DEV_INTERFACE)
231 return new PPB_Audio_Proxy(this, interface_functions);
[email protected]43a40202010-11-12 16:25:01232 if (interface_name == PPB_BUFFER_DEV_INTERFACE)
233 return new PPB_Buffer_Proxy(this, interface_functions);
[email protected]9ca952d2010-11-11 20:43:50234 if (interface_name == PPB_CHAR_SET_DEV_INTERFACE)
235 return new PPB_CharSet_Proxy(this, interface_functions);
[email protected]c2932f5e2010-11-03 03:22:33236 if (interface_name == PPB_CORE_INTERFACE)
237 return new PPB_Core_Proxy(this, interface_functions);
[email protected]a3010332010-11-12 07:09:35238 if (interface_name == PPB_CURSOR_CONTROL_DEV_INTERFACE)
239 return new PPB_CursorControl_Proxy(this, interface_functions);
[email protected]9ca952d2010-11-11 20:43:50240 if (interface_name == PPB_FONT_DEV_INTERFACE)
241 return new PPB_Font_Proxy(this, interface_functions);
[email protected]a3010332010-11-12 07:09:35242 if (interface_name == PPB_FULLSCREEN_DEV_INTERFACE)
243 return new PPB_Fullscreen_Proxy(this, interface_functions);
[email protected]c2932f5e2010-11-03 03:22:33244 if (interface_name == PPB_GRAPHICS_2D_INTERFACE)
245 return new PPB_Graphics2D_Proxy(this, interface_functions);
246 if (interface_name == PPB_IMAGEDATA_INTERFACE)
247 return new PPB_ImageData_Proxy(this, interface_functions);
248 if (interface_name == PPB_INSTANCE_INTERFACE)
249 return new PPB_Instance_Proxy(this, interface_functions);
[email protected]799d1ab2010-11-09 17:16:28250 if (interface_name == PPB_TESTING_DEV_INTERFACE)
251 return new PPB_Testing_Proxy(this, interface_functions);
[email protected]5a3f62852010-11-10 21:43:01252 if (interface_name == PPB_URLLOADER_INTERFACE)
[email protected]799d1ab2010-11-09 17:16:28253 return new PPB_URLLoader_Proxy(this, interface_functions);
[email protected]5a3f62852010-11-10 21:43:01254 if (interface_name == PPB_URLREQUESTINFO_INTERFACE)
[email protected]799d1ab2010-11-09 17:16:28255 return new PPB_URLRequestInfo_Proxy(this, interface_functions);
[email protected]5a3f62852010-11-10 21:43:01256 if (interface_name == PPB_URLRESPONSEINFO_INTERFACE)
[email protected]799d1ab2010-11-09 17:16:28257 return new PPB_URLResponseInfo_Proxy(this, interface_functions);
[email protected]c2932f5e2010-11-03 03:22:33258 if (interface_name == PPB_VAR_DEPRECATED_INTERFACE)
259 return new PPB_Var_Deprecated_Proxy(this, interface_functions);
260 if (interface_name == PPP_INSTANCE_INTERFACE)
261 return new PPP_Instance_Proxy(this, interface_functions);
262
[email protected]5f2517612010-12-02 22:36:48263 // Trusted interfaces.
264 if (!disallow_trusted_interfaces_) {
[email protected]0bd753682010-12-16 18:15:52265 if (interface_name == PPB_FLASH_INTERFACE)
[email protected]5f2517612010-12-02 22:36:48266 return new PPB_Flash_Proxy(this, interface_functions);
[email protected]0bd753682010-12-16 18:15:52267 if (interface_name == PPB_PDF_INTERFACE)
268 return new PPB_PDF_Proxy(this, interface_functions);
[email protected]5f2517612010-12-02 22:36:48269 if (interface_name == PPB_URLLOADERTRUSTED_INTERFACE)
270 return new PPB_URLLoaderTrusted_Proxy(this, interface_functions);
271 }
272
[email protected]c2932f5e2010-11-03 03:22:33273 return NULL;
274}
275
276} // namespace proxy
277} // namespace pp
278