blob: c0b71c658087efa8944bfe90e01bd92546ff7c61 [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"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/singleton.h"
[email protected]c2932f5e2010-11-03 03:22:3314#include "ipc/ipc_message.h"
15#include "ipc/ipc_sync_channel.h"
[email protected]f24448db2011-01-27 20:40:3916#include "ipc/ipc_test_sink.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]f56279c2011-02-02 18:12:3119#include "ppapi/c/dev/ppb_context_3d_dev.h"
[email protected]a3010332010-11-12 07:09:3520#include "ppapi/c/dev/ppb_cursor_control_dev.h"
[email protected]f56279c2011-02-02 18:12:3121#include "ppapi/c/dev/ppb_gles_chromium_texture_mapping_dev.h"
[email protected]43a40202010-11-12 16:25:0122#include "ppapi/c/dev/ppb_font_dev.h"
23#include "ppapi/c/dev/ppb_fullscreen_dev.h"
[email protected]f56279c2011-02-02 18:12:3124#include "ppapi/c/dev/ppb_opengles_dev.h"
25#include "ppapi/c/dev/ppb_surface_3d_dev.h"
[email protected]799d1ab2010-11-09 17:16:2826#include "ppapi/c/dev/ppb_testing_dev.h"
[email protected]9ca245e2011-03-18 01:50:3127#include "ppapi/c/dev/ppb_url_util_dev.h"
[email protected]c2932f5e2010-11-03 03:22:3328#include "ppapi/c/dev/ppb_var_deprecated.h"
29#include "ppapi/c/pp_errors.h"
[email protected]b9a59842011-01-15 01:04:0030#include "ppapi/c/ppb_audio.h"
31#include "ppapi/c/ppb_audio_config.h"
[email protected]c2932f5e2010-11-03 03:22:3332#include "ppapi/c/ppb_core.h"
33#include "ppapi/c/ppb_graphics_2d.h"
34#include "ppapi/c/ppb_image_data.h"
35#include "ppapi/c/ppb_instance.h"
[email protected]5a3f62852010-11-10 21:43:0136#include "ppapi/c/ppb_url_loader.h"
37#include "ppapi/c/ppb_url_request_info.h"
38#include "ppapi/c/ppb_url_response_info.h"
[email protected]9ca952d2010-11-11 20:43:5039#include "ppapi/c/ppp_instance.h"
[email protected]b697e1a2011-01-06 22:20:2840#include "ppapi/c/private/ppb_flash.h"
[email protected]5cf8a5b2011-03-09 19:00:0541#include "ppapi/c/private/ppb_flash_clipboard.h"
[email protected]d07fa3792011-02-24 18:58:0142#include "ppapi/c/private/ppb_flash_file.h"
[email protected]7358d572011-02-15 18:44:4043#include "ppapi/c/private/ppb_flash_menu.h"
[email protected]7d3782f2011-03-29 19:16:2344#include "ppapi/c/private/ppb_flash_net_connector.h"
[email protected]917a44b02011-01-13 17:07:2945#include "ppapi/c/private/ppb_pdf.h"
[email protected]5f2517612010-12-02 22:36:4846#include "ppapi/c/trusted/ppb_url_loader_trusted.h"
[email protected]9ca952d2010-11-11 20:43:5047#include "ppapi/proxy/ppapi_messages.h"
[email protected]5d84d012010-12-02 17:17:2148#include "ppapi/proxy/ppb_audio_config_proxy.h"
49#include "ppapi/proxy/ppb_audio_proxy.h"
[email protected]43a40202010-11-12 16:25:0150#include "ppapi/proxy/ppb_buffer_proxy.h"
[email protected]9ca952d2010-11-11 20:43:5051#include "ppapi/proxy/ppb_char_set_proxy.h"
[email protected]d38c57402011-03-11 20:20:5652#include "ppapi/proxy/ppb_console_proxy.h"
[email protected]f56279c2011-02-02 18:12:3153#include "ppapi/proxy/ppb_context_3d_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3354#include "ppapi/proxy/ppb_core_proxy.h"
[email protected]a3010332010-11-12 07:09:3555#include "ppapi/proxy/ppb_cursor_control_proxy.h"
[email protected]4deeb432011-02-17 23:59:3956#include "ppapi/proxy/ppb_file_chooser_proxy.h"
57#include "ppapi/proxy/ppb_file_ref_proxy.h"
[email protected]ce482df2011-02-21 23:49:1658#include "ppapi/proxy/ppb_file_system_proxy.h"
[email protected]5cf8a5b2011-03-09 19:00:0559#include "ppapi/proxy/ppb_flash_clipboard_proxy.h"
[email protected]d07fa3792011-02-24 18:58:0160#include "ppapi/proxy/ppb_flash_file_proxy.h"
[email protected]5f2517612010-12-02 22:36:4861#include "ppapi/proxy/ppb_flash_proxy.h"
[email protected]7358d572011-02-15 18:44:4062#include "ppapi/proxy/ppb_flash_menu_proxy.h"
[email protected]7d3782f2011-03-29 19:16:2363#include "ppapi/proxy/ppb_flash_net_connector_proxy.h"
[email protected]9ca952d2010-11-11 20:43:5064#include "ppapi/proxy/ppb_font_proxy.h"
[email protected]a3010332010-11-12 07:09:3565#include "ppapi/proxy/ppb_fullscreen_proxy.h"
[email protected]f56279c2011-02-02 18:12:3166#include "ppapi/proxy/ppb_gles_chromium_texture_mapping_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3367#include "ppapi/proxy/ppb_graphics_2d_proxy.h"
68#include "ppapi/proxy/ppb_image_data_proxy.h"
69#include "ppapi/proxy/ppb_instance_proxy.h"
[email protected]f56279c2011-02-02 18:12:3170#include "ppapi/proxy/ppb_opengles2_proxy.h"
[email protected]43a40202010-11-12 16:25:0171#include "ppapi/proxy/ppb_pdf_proxy.h"
[email protected]f56279c2011-02-02 18:12:3172#include "ppapi/proxy/ppb_surface_3d_proxy.h"
[email protected]799d1ab2010-11-09 17:16:2873#include "ppapi/proxy/ppb_testing_proxy.h"
74#include "ppapi/proxy/ppb_url_loader_proxy.h"
75#include "ppapi/proxy/ppb_url_request_info_proxy.h"
76#include "ppapi/proxy/ppb_url_response_info_proxy.h"
[email protected]9ca245e2011-03-18 01:50:3177#include "ppapi/proxy/ppb_url_util_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3378#include "ppapi/proxy/ppb_var_deprecated_proxy.h"
79#include "ppapi/proxy/ppp_class_proxy.h"
80#include "ppapi/proxy/ppp_instance_proxy.h"
81#include "ppapi/proxy/var_serialization_rules.h"
82
83namespace pp {
84namespace proxy {
85
[email protected]465faa22011-02-08 16:31:4686namespace {
87
88struct InterfaceList {
89 InterfaceList();
90
91 static InterfaceList* GetInstance();
92
93 void AddPPP(const InterfaceProxy::Info* info);
94 void AddPPB(const InterfaceProxy::Info* info);
95
96 typedef std::map<std::string, const InterfaceProxy::Info*> NameToInfo;
97 NameToInfo name_to_plugin_info_;
98 NameToInfo name_to_browser_info_;
99
100 const InterfaceProxy::Info* id_to_plugin_info_[INTERFACE_ID_COUNT];
101 const InterfaceProxy::Info* id_to_browser_info_[INTERFACE_ID_COUNT];
102};
103
104InterfaceList::InterfaceList() {
[email protected]ce482df2011-02-21 23:49:16105 memset(id_to_plugin_info_, 0, sizeof(id_to_plugin_info_));
106 memset(id_to_browser_info_, 0, sizeof(id_to_browser_info_));
[email protected]465faa22011-02-08 16:31:46107
108 // PPB (browser) interfaces.
109 AddPPB(PPB_AudioConfig_Proxy::GetInfo());
110 AddPPB(PPB_Audio_Proxy::GetInfo());
111 AddPPB(PPB_Buffer_Proxy::GetInfo());
112 AddPPB(PPB_CharSet_Proxy::GetInfo());
[email protected]d38c57402011-03-11 20:20:56113 AddPPB(PPB_Console_Proxy::GetInfo());
[email protected]465faa22011-02-08 16:31:46114 AddPPB(PPB_Context3D_Proxy::GetInfo());
115 AddPPB(PPB_Core_Proxy::GetInfo());
116 AddPPB(PPB_CursorControl_Proxy::GetInfo());
[email protected]4deeb432011-02-17 23:59:39117 AddPPB(PPB_FileChooser_Proxy::GetInfo());
118 AddPPB(PPB_FileRef_Proxy::GetInfo());
[email protected]ce482df2011-02-21 23:49:16119 AddPPB(PPB_FileSystem_Proxy::GetInfo());
[email protected]5cf8a5b2011-03-09 19:00:05120 AddPPB(PPB_Flash_Clipboard_Proxy::GetInfo());
[email protected]d062d962011-03-02 01:27:26121 AddPPB(PPB_Flash_File_FileRef_Proxy::GetInfo());
[email protected]d07fa3792011-02-24 18:58:01122 AddPPB(PPB_Flash_File_ModuleLocal_Proxy::GetInfo());
[email protected]465faa22011-02-08 16:31:46123 AddPPB(PPB_Flash_Proxy::GetInfo());
[email protected]7358d572011-02-15 18:44:40124 AddPPB(PPB_Flash_Menu_Proxy::GetInfo());
[email protected]465faa22011-02-08 16:31:46125 AddPPB(PPB_Font_Proxy::GetInfo());
126 AddPPB(PPB_Fullscreen_Proxy::GetInfo());
127 AddPPB(PPB_GLESChromiumTextureMapping_Proxy::GetInfo());
128 AddPPB(PPB_Graphics2D_Proxy::GetInfo());
129 AddPPB(PPB_ImageData_Proxy::GetInfo());
130 AddPPB(PPB_Instance_Proxy::GetInfo());
131 AddPPB(PPB_OpenGLES2_Proxy::GetInfo());
132 AddPPB(PPB_PDF_Proxy::GetInfo());
133 AddPPB(PPB_Surface3D_Proxy::GetInfo());
134 AddPPB(PPB_Testing_Proxy::GetInfo());
135 AddPPB(PPB_URLLoader_Proxy::GetInfo());
136 AddPPB(PPB_URLLoaderTrusted_Proxy::GetInfo());
137 AddPPB(PPB_URLRequestInfo_Proxy::GetInfo());
138 AddPPB(PPB_URLResponseInfo_Proxy::GetInfo());
[email protected]9ca245e2011-03-18 01:50:31139 AddPPB(PPB_URLUtil_Proxy::GetInfo());
[email protected]465faa22011-02-08 16:31:46140 AddPPB(PPB_Var_Deprecated_Proxy::GetInfo());
141
[email protected]7d3782f2011-03-29 19:16:23142#ifdef ENABLE_FLAPPER_HACKS
143 AddPPB(PPB_Flash_NetConnector_Proxy::GetInfo());
144#endif
145
[email protected]465faa22011-02-08 16:31:46146 // PPP (plugin) interfaces.
147 AddPPP(PPP_Instance_Proxy::GetInfo());
148}
149
150void InterfaceList::AddPPP(const InterfaceProxy::Info* info) {
151 DCHECK(name_to_plugin_info_.find(info->name) ==
152 name_to_plugin_info_.end());
153 DCHECK(info->id > 0 && info->id < INTERFACE_ID_COUNT);
154 DCHECK(id_to_plugin_info_[info->id] == NULL);
155
156 name_to_plugin_info_[info->name] = info;
157 id_to_plugin_info_[info->id] = info;
158}
159
160void InterfaceList::AddPPB(const InterfaceProxy::Info* info) {
161 DCHECK(name_to_browser_info_.find(info->name) ==
162 name_to_browser_info_.end());
163 DCHECK(info->id > 0 && info->id < INTERFACE_ID_COUNT);
164 DCHECK(id_to_browser_info_[info->id] == NULL);
165
166 name_to_browser_info_[std::string(info->name)] = info;
167 id_to_browser_info_[info->id] = info;
168}
169
170// static
171InterfaceList* InterfaceList::GetInstance() {
172 return Singleton<InterfaceList>::get();
173}
174
175} // namespace
176
[email protected]5d84d012010-12-02 17:17:21177Dispatcher::Dispatcher(base::ProcessHandle remote_process_handle,
178 GetInterfaceFunc local_get_interface)
[email protected]2cc062242011-03-10 21:16:34179 : delegate_(NULL),
180 remote_process_handle_(remote_process_handle),
[email protected]f24448db2011-01-27 20:40:39181 test_sink_(NULL),
[email protected]5f2517612010-12-02 22:36:48182 disallow_trusted_interfaces_(false), // TODO(brettw) make this settable.
[email protected]c2932f5e2010-11-03 03:22:33183 local_get_interface_(local_get_interface),
[email protected]709a847e2010-11-10 01:16:11184 callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
[email protected]c2932f5e2010-11-03 03:22:33185}
186
187Dispatcher::~Dispatcher() {
188}
189
[email protected]2cc062242011-03-10 21:16:34190bool Dispatcher::InitWithChannel(Delegate* delegate,
[email protected]42ce94e2010-12-08 19:28:09191 const IPC::ChannelHandle& channel_handle,
[email protected]2cc062242011-03-10 21:16:34192 bool is_client) {
[email protected]c2932f5e2010-11-03 03:22:33193 IPC::Channel::Mode mode = is_client ? IPC::Channel::MODE_CLIENT
194 : IPC::Channel::MODE_SERVER;
[email protected]42ce94e2010-12-08 19:28:09195 channel_.reset(new IPC::SyncChannel(channel_handle, mode, this,
[email protected]2cc062242011-03-10 21:16:34196 delegate->GetIPCMessageLoop(), false,
197 delegate->GetShutdownEvent()));
[email protected]c2932f5e2010-11-03 03:22:33198 return true;
199}
200
[email protected]f24448db2011-01-27 20:40:39201void Dispatcher::InitWithTestSink(IPC::TestSink* test_sink) {
202 DCHECK(!test_sink_);
203 test_sink_ = test_sink;
204}
205
[email protected]a95986a82010-12-24 06:19:28206bool Dispatcher::OnMessageReceived(const IPC::Message& msg) {
[email protected]c2932f5e2010-11-03 03:22:33207 // Control messages.
208 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
[email protected]a95986a82010-12-24 06:19:28209 bool handled = true;
[email protected]c2932f5e2010-11-03 03:22:33210 IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg)
[email protected]c2932f5e2010-11-03 03:22:33211 IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_,
212 CallbackTracker::ReceiveExecuteSerializedCallback)
[email protected]a95986a82010-12-24 06:19:28213 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]c2932f5e2010-11-03 03:22:33214 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28215 return handled;
[email protected]c2932f5e2010-11-03 03:22:33216 }
[email protected]a95986a82010-12-24 06:19:28217 return false;
[email protected]c2932f5e2010-11-03 03:22:33218}
219
[email protected]4f15d2842011-02-15 17:36:33220void Dispatcher::OnChannelError() {
221 channel_.reset();
222}
223
[email protected]465faa22011-02-08 16:31:46224// static
225const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo(
226 const std::string& name) {
227 const InterfaceList* list = InterfaceList::GetInstance();
228 InterfaceList::NameToInfo::const_iterator found =
229 list->name_to_browser_info_.find(name);
230 if (found == list->name_to_browser_info_.end())
231 return NULL;
232 return found->second;
233}
234
235// static
236const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo(InterfaceID id) {
237 if (id <= 0 || id >= INTERFACE_ID_COUNT)
238 return NULL;
239 const InterfaceList* list = InterfaceList::GetInstance();
240 return list->id_to_browser_info_[id];
241}
242
243// static
244const InterfaceProxy::Info* Dispatcher::GetPPPInterfaceInfo(
245 const std::string& name) {
246 const InterfaceList* list = InterfaceList::GetInstance();
247 InterfaceList::NameToInfo::const_iterator found =
248 list->name_to_plugin_info_.find(name);
249 if (found == list->name_to_plugin_info_.end())
250 return NULL;
251 return found->second;
252}
253
254// static
255const InterfaceProxy::Info* Dispatcher::GetPPPInterfaceInfo(InterfaceID id) {
256 if (id <= 0 || id >= INTERFACE_ID_COUNT)
257 return NULL;
258 const InterfaceList* list = InterfaceList::GetInstance();
259 return list->id_to_plugin_info_[id];
260}
261
[email protected]c2932f5e2010-11-03 03:22:33262void Dispatcher::SetSerializationRules(
263 VarSerializationRules* var_serialization_rules) {
264 serialization_rules_.reset(var_serialization_rules);
265}
266
[email protected]c2932f5e2010-11-03 03:22:33267const void* Dispatcher::GetLocalInterface(const char* interface) {
268 return local_get_interface_(interface);
269}
270
[email protected]db0f57f52011-02-25 03:54:54271IPC::PlatformFileForTransit Dispatcher::ShareHandleWithRemote(
272 base::PlatformFile handle,
273 bool should_close_source) {
274 IPC::PlatformFileForTransit out_handle;
275#if defined(OS_WIN)
276 DWORD options = DUPLICATE_SAME_ACCESS;
277 if (should_close_source)
278 options |= DUPLICATE_CLOSE_SOURCE;
279 if (!::DuplicateHandle(::GetCurrentProcess(),
280 handle,
281 remote_process_handle_,
282 &out_handle,
283 0,
284 FALSE,
285 options))
286 out_handle = IPC::InvalidPlatformFileForTransit();
287#elif defined(OS_POSIX)
288 // If asked to close the source, we can simply re-use the source fd instead of
289 // dup()ing and close()ing.
290 int fd = should_close_source ? handle : ::dup(handle);
291 out_handle = base::FileDescriptor(fd, true);
292#else
293 #error Not implemented.
294#endif
295 return out_handle;
296}
297
[email protected]c2932f5e2010-11-03 03:22:33298bool Dispatcher::Send(IPC::Message* msg) {
[email protected]f24448db2011-01-27 20:40:39299 if (test_sink_)
300 return test_sink_->Send(msg);
[email protected]4f15d2842011-02-15 17:36:33301 if (channel_.get())
302 return channel_->Send(msg);
303
304 // Remote side crashed, drop this message.
305 delete msg;
306 return false;
[email protected]c2932f5e2010-11-03 03:22:33307}
308
[email protected]c2932f5e2010-11-03 03:22:33309} // namespace proxy
310} // namespace pp
311