blob: f852c495d2b9bfd318fc3918234e2892c81ae254 [file] [log] [blame]
[email protected]b697e1a2011-01-06 22:20:281// 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/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]f24448db2011-01-27 20:40:3915#include "ipc/ipc_test_sink.h"
[email protected]43a40202010-11-12 16:25:0116#include "ppapi/c/dev/ppb_buffer_dev.h"
[email protected]9ca952d2010-11-11 20:43:5017#include "ppapi/c/dev/ppb_char_set_dev.h"
[email protected]a3010332010-11-12 07:09:3518#include "ppapi/c/dev/ppb_cursor_control_dev.h"
[email protected]43a40202010-11-12 16:25:0119#include "ppapi/c/dev/ppb_cursor_control_dev.h"
20#include "ppapi/c/dev/ppb_font_dev.h"
21#include "ppapi/c/dev/ppb_fullscreen_dev.h"
[email protected]a3010332010-11-12 07:09:3522#include "ppapi/c/dev/ppb_fullscreen_dev.h"
[email protected]799d1ab2010-11-09 17:16:2823#include "ppapi/c/dev/ppb_testing_dev.h"
[email protected]c2932f5e2010-11-03 03:22:3324#include "ppapi/c/dev/ppb_var_deprecated.h"
25#include "ppapi/c/pp_errors.h"
[email protected]b9a59842011-01-15 01:04:0026#include "ppapi/c/ppb_audio.h"
27#include "ppapi/c/ppb_audio_config.h"
[email protected]c2932f5e2010-11-03 03:22:3328#include "ppapi/c/ppb_core.h"
29#include "ppapi/c/ppb_graphics_2d.h"
30#include "ppapi/c/ppb_image_data.h"
31#include "ppapi/c/ppb_instance.h"
[email protected]5a3f62852010-11-10 21:43:0132#include "ppapi/c/ppb_url_loader.h"
33#include "ppapi/c/ppb_url_request_info.h"
34#include "ppapi/c/ppb_url_response_info.h"
[email protected]9ca952d2010-11-11 20:43:5035#include "ppapi/c/ppp_instance.h"
[email protected]b697e1a2011-01-06 22:20:2836#include "ppapi/c/private/ppb_flash.h"
[email protected]917a44b02011-01-13 17:07:2937#include "ppapi/c/private/ppb_pdf.h"
[email protected]5f2517612010-12-02 22:36:4838#include "ppapi/c/trusted/ppb_url_loader_trusted.h"
[email protected]9ca952d2010-11-11 20:43:5039#include "ppapi/proxy/ppapi_messages.h"
[email protected]5d84d012010-12-02 17:17:2140#include "ppapi/proxy/ppb_audio_config_proxy.h"
41#include "ppapi/proxy/ppb_audio_proxy.h"
[email protected]43a40202010-11-12 16:25:0142#include "ppapi/proxy/ppb_buffer_proxy.h"
[email protected]9ca952d2010-11-11 20:43:5043#include "ppapi/proxy/ppb_char_set_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3344#include "ppapi/proxy/ppb_core_proxy.h"
[email protected]a3010332010-11-12 07:09:3545#include "ppapi/proxy/ppb_cursor_control_proxy.h"
[email protected]5f2517612010-12-02 22:36:4846#include "ppapi/proxy/ppb_flash_proxy.h"
[email protected]9ca952d2010-11-11 20:43:5047#include "ppapi/proxy/ppb_font_proxy.h"
[email protected]a3010332010-11-12 07:09:3548#include "ppapi/proxy/ppb_fullscreen_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3349#include "ppapi/proxy/ppb_graphics_2d_proxy.h"
50#include "ppapi/proxy/ppb_image_data_proxy.h"
51#include "ppapi/proxy/ppb_instance_proxy.h"
[email protected]43a40202010-11-12 16:25:0152#include "ppapi/proxy/ppb_pdf_proxy.h"
[email protected]799d1ab2010-11-09 17:16:2853#include "ppapi/proxy/ppb_testing_proxy.h"
54#include "ppapi/proxy/ppb_url_loader_proxy.h"
55#include "ppapi/proxy/ppb_url_request_info_proxy.h"
56#include "ppapi/proxy/ppb_url_response_info_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3357#include "ppapi/proxy/ppb_var_deprecated_proxy.h"
58#include "ppapi/proxy/ppp_class_proxy.h"
59#include "ppapi/proxy/ppp_instance_proxy.h"
60#include "ppapi/proxy/var_serialization_rules.h"
[email protected]c2932f5e2010-11-03 03:22:3361
62namespace pp {
63namespace proxy {
64
[email protected]5d84d012010-12-02 17:17:2165Dispatcher::Dispatcher(base::ProcessHandle remote_process_handle,
66 GetInterfaceFunc local_get_interface)
[email protected]c2932f5e2010-11-03 03:22:3367 : pp_module_(0),
[email protected]5d84d012010-12-02 17:17:2168 remote_process_handle_(remote_process_handle),
[email protected]f24448db2011-01-27 20:40:3969 test_sink_(NULL),
[email protected]5f2517612010-12-02 22:36:4870 disallow_trusted_interfaces_(false), // TODO(brettw) make this settable.
[email protected]c2932f5e2010-11-03 03:22:3371 local_get_interface_(local_get_interface),
72 declared_supported_remote_interfaces_(false),
[email protected]709a847e2010-11-10 01:16:1173 callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
[email protected]c2932f5e2010-11-03 03:22:3374 memset(id_to_proxy_, 0,
75 static_cast<int>(INTERFACE_ID_COUNT) * sizeof(InterfaceProxy*));
76}
77
78Dispatcher::~Dispatcher() {
79}
80
81bool Dispatcher::InitWithChannel(MessageLoop* ipc_message_loop,
[email protected]42ce94e2010-12-08 19:28:0982 const IPC::ChannelHandle& channel_handle,
[email protected]c2932f5e2010-11-03 03:22:3383 bool is_client,
84 base::WaitableEvent* shutdown_event) {
85 IPC::Channel::Mode mode = is_client ? IPC::Channel::MODE_CLIENT
86 : IPC::Channel::MODE_SERVER;
[email protected]42ce94e2010-12-08 19:28:0987 channel_.reset(new IPC::SyncChannel(channel_handle, mode, this,
[email protected]c2932f5e2010-11-03 03:22:3388 ipc_message_loop, false, shutdown_event));
89 return true;
90}
91
[email protected]f24448db2011-01-27 20:40:3992void Dispatcher::InitWithTestSink(IPC::TestSink* test_sink) {
93 DCHECK(!test_sink_);
94 test_sink_ = test_sink;
95}
96
[email protected]a95986a82010-12-24 06:19:2897bool Dispatcher::OnMessageReceived(const IPC::Message& msg) {
[email protected]c2932f5e2010-11-03 03:22:3398 // Control messages.
99 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
[email protected]a95986a82010-12-24 06:19:28100 bool handled = true;
[email protected]c2932f5e2010-11-03 03:22:33101 IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg)
102 IPC_MESSAGE_HANDLER(PpapiMsg_DeclareInterfaces,
103 OnMsgDeclareInterfaces)
104 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface)
105 IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_,
106 CallbackTracker::ReceiveExecuteSerializedCallback)
[email protected]a95986a82010-12-24 06:19:28107 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]c2932f5e2010-11-03 03:22:33108 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28109 return handled;
[email protected]c2932f5e2010-11-03 03:22:33110 }
111
112 // Interface-specific messages.
113 if (msg.routing_id() > 0 && msg.routing_id() < INTERFACE_ID_COUNT) {
114 InterfaceProxy* proxy = id_to_proxy_[msg.routing_id()];
115 if (proxy)
[email protected]a95986a82010-12-24 06:19:28116 return proxy->OnMessageReceived(msg);
117
118 NOTREACHED();
[email protected]c2932f5e2010-11-03 03:22:33119 // TODO(brettw): kill the plugin if it starts sending invalid messages?
120 }
[email protected]a95986a82010-12-24 06:19:28121
122 return false;
[email protected]c2932f5e2010-11-03 03:22:33123}
124
125void Dispatcher::SetSerializationRules(
126 VarSerializationRules* var_serialization_rules) {
127 serialization_rules_.reset(var_serialization_rules);
128}
129
130void Dispatcher::InjectProxy(InterfaceID id,
131 const std::string& name,
132 InterfaceProxy* proxy) {
133 proxies_[name] = linked_ptr<InterfaceProxy>(proxy);
134 id_to_proxy_[id] = proxy;
135}
136
137const void* Dispatcher::GetLocalInterface(const char* interface) {
138 return local_get_interface_(interface);
139}
140
141const void* Dispatcher::GetProxiedInterface(const std::string& interface) {
142 // See if we already know about this interface and have created a host.
143 ProxyMap::const_iterator found = proxies_.find(interface);
144 if (found != proxies_.end())
145 return found->second->GetSourceInterface();
146
147 // When the remote side has sent us a declared list of all interfaces it
148 // supports and we don't have it in our list, we know the requested interface
149 // doesn't exist and we can return failure.
150 if (declared_supported_remote_interfaces_)
151 return NULL;
152
153 if (!RemoteSupportsTargetInterface(interface))
154 return NULL;
155
156 linked_ptr<InterfaceProxy> proxy(CreateProxyForInterface(interface, NULL));
157 if (!proxy.get())
158 return NULL; // Don't know how to proxy this interface.
159
160 // Save our proxy.
161 proxies_[interface] = proxy;
162 id_to_proxy_[proxy->GetInterfaceId()] = proxy.get();
163 return proxy->GetSourceInterface();
164}
165
166bool Dispatcher::Send(IPC::Message* msg) {
[email protected]f24448db2011-01-27 20:40:39167 if (test_sink_)
168 return test_sink_->Send(msg);
[email protected]c2932f5e2010-11-03 03:22:33169 return channel_->Send(msg);
170}
171
172bool Dispatcher::RemoteSupportsTargetInterface(const std::string& interface) {
173 bool result = false;
174 Send(new PpapiMsg_SupportsInterface(interface, &result));
175 return result;
176}
177
178bool Dispatcher::IsInterfaceTrusted(const std::string& interface) {
179 // FIXME(brettw)
180 (void)interface;
181 return false;
182}
183
184bool Dispatcher::SetupProxyForTargetInterface(const std::string& interface) {
185 // If we already have a proxy that knows about the locally-implemented
186 // interface, we know it's supported and don't need to re-query.
187 ProxyMap::const_iterator found = proxies_.find(interface);
188 if (found != proxies_.end())
189 return true;
190
191 if (disallow_trusted_interfaces_ && IsInterfaceTrusted(interface))
192 return false;
193
194 // Create the proxy if it doesn't exist and set the local interface on it.
195 // This also handles the case where possibly an interface could be supported
196 // by both the local and remote side.
197 const void* interface_functions = local_get_interface_(interface.c_str());
198 if (!interface_functions)
199 return false;
200 InterfaceProxy* proxy = CreateProxyForInterface(interface,
201 interface_functions);
202 if (!proxy)
203 return false;
204
205 proxies_[interface] = linked_ptr<InterfaceProxy>(proxy);
206 id_to_proxy_[proxy->GetInterfaceId()] = proxy;
207 return true;
208}
209
210void Dispatcher::OnMsgSupportsInterface(const std::string& interface_name,
211 bool* result) {
212 *result = SetupProxyForTargetInterface(interface_name);
213}
214
215void Dispatcher::OnMsgDeclareInterfaces(
216 const std::vector<std::string>& interfaces) {
217 // Make proxies for all the interfaces it supports that we also support.
218 for (size_t i = 0; i < interfaces.size(); i++) {
219 // Possibly the plugin could request an interface before the "declare"
220 // message is received, so we could already have an entry for this
221 // interface. In this case, we can just skip to the next one.
222 if (proxies_.find(interfaces[i]) != proxies_.end())
223 continue;
224
225 linked_ptr<InterfaceProxy> proxy(CreateProxyForInterface(interfaces[i],
226 NULL));
227 if (!proxy.get()) {
228 // Since only the browser declares supported interfaces, we should never
229 // get one we don't support.
230 //NOTREACHED() << "Remote side declaring an unsupported proxy.";
231 continue;
232 }
233 proxies_[interfaces[i]] = proxy;
234 id_to_proxy_[proxy->GetInterfaceId()] = proxy.get();
235 }
236}
237
238InterfaceProxy* Dispatcher::CreateProxyForInterface(
239 const std::string& interface_name,
240 const void* interface_functions) {
[email protected]b9a59842011-01-15 01:04:00241 if (interface_name == PPB_AUDIO_CONFIG_INTERFACE)
[email protected]5d84d012010-12-02 17:17:21242 return new PPB_AudioConfig_Proxy(this, interface_functions);
[email protected]b9a59842011-01-15 01:04:00243 if (interface_name == PPB_AUDIO_INTERFACE)
[email protected]5d84d012010-12-02 17:17:21244 return new PPB_Audio_Proxy(this, interface_functions);
[email protected]43a40202010-11-12 16:25:01245 if (interface_name == PPB_BUFFER_DEV_INTERFACE)
246 return new PPB_Buffer_Proxy(this, interface_functions);
[email protected]9ca952d2010-11-11 20:43:50247 if (interface_name == PPB_CHAR_SET_DEV_INTERFACE)
248 return new PPB_CharSet_Proxy(this, interface_functions);
[email protected]c2932f5e2010-11-03 03:22:33249 if (interface_name == PPB_CORE_INTERFACE)
250 return new PPB_Core_Proxy(this, interface_functions);
[email protected]a3010332010-11-12 07:09:35251 if (interface_name == PPB_CURSOR_CONTROL_DEV_INTERFACE)
252 return new PPB_CursorControl_Proxy(this, interface_functions);
[email protected]9ca952d2010-11-11 20:43:50253 if (interface_name == PPB_FONT_DEV_INTERFACE)
254 return new PPB_Font_Proxy(this, interface_functions);
[email protected]a3010332010-11-12 07:09:35255 if (interface_name == PPB_FULLSCREEN_DEV_INTERFACE)
256 return new PPB_Fullscreen_Proxy(this, interface_functions);
[email protected]c2932f5e2010-11-03 03:22:33257 if (interface_name == PPB_GRAPHICS_2D_INTERFACE)
258 return new PPB_Graphics2D_Proxy(this, interface_functions);
259 if (interface_name == PPB_IMAGEDATA_INTERFACE)
260 return new PPB_ImageData_Proxy(this, interface_functions);
261 if (interface_name == PPB_INSTANCE_INTERFACE)
262 return new PPB_Instance_Proxy(this, interface_functions);
[email protected]799d1ab2010-11-09 17:16:28263 if (interface_name == PPB_TESTING_DEV_INTERFACE)
264 return new PPB_Testing_Proxy(this, interface_functions);
[email protected]5a3f62852010-11-10 21:43:01265 if (interface_name == PPB_URLLOADER_INTERFACE)
[email protected]799d1ab2010-11-09 17:16:28266 return new PPB_URLLoader_Proxy(this, interface_functions);
[email protected]5a3f62852010-11-10 21:43:01267 if (interface_name == PPB_URLREQUESTINFO_INTERFACE)
[email protected]799d1ab2010-11-09 17:16:28268 return new PPB_URLRequestInfo_Proxy(this, interface_functions);
[email protected]5a3f62852010-11-10 21:43:01269 if (interface_name == PPB_URLRESPONSEINFO_INTERFACE)
[email protected]799d1ab2010-11-09 17:16:28270 return new PPB_URLResponseInfo_Proxy(this, interface_functions);
[email protected]c2932f5e2010-11-03 03:22:33271 if (interface_name == PPB_VAR_DEPRECATED_INTERFACE)
272 return new PPB_Var_Deprecated_Proxy(this, interface_functions);
273 if (interface_name == PPP_INSTANCE_INTERFACE)
274 return new PPP_Instance_Proxy(this, interface_functions);
275
[email protected]5f2517612010-12-02 22:36:48276 // Trusted interfaces.
277 if (!disallow_trusted_interfaces_) {
[email protected]0bd753682010-12-16 18:15:52278 if (interface_name == PPB_FLASH_INTERFACE)
[email protected]5f2517612010-12-02 22:36:48279 return new PPB_Flash_Proxy(this, interface_functions);
[email protected]0bd753682010-12-16 18:15:52280 if (interface_name == PPB_PDF_INTERFACE)
281 return new PPB_PDF_Proxy(this, interface_functions);
[email protected]5f2517612010-12-02 22:36:48282 if (interface_name == PPB_URLLOADERTRUSTED_INTERFACE)
283 return new PPB_URLLoaderTrusted_Proxy(this, interface_functions);
284 }
285
[email protected]c2932f5e2010-11-03 03:22:33286 return NULL;
287}
288
289} // namespace proxy
290} // namespace pp
291