blob: a4d9798596e2b911ca2ceb406ca714ec12e1796c [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"
[email protected]8c3bd1d2011-04-12 20:01:4280#include "ppapi/proxy/ppp_graphics_3d_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3381#include "ppapi/proxy/ppp_instance_proxy.h"
82#include "ppapi/proxy/var_serialization_rules.h"
83
84namespace pp {
85namespace proxy {
86
[email protected]465faa22011-02-08 16:31:4687namespace {
88
89struct InterfaceList {
90 InterfaceList();
91
92 static InterfaceList* GetInstance();
93
94 void AddPPP(const InterfaceProxy::Info* info);
95 void AddPPB(const InterfaceProxy::Info* info);
96
97 typedef std::map<std::string, const InterfaceProxy::Info*> NameToInfo;
98 NameToInfo name_to_plugin_info_;
99 NameToInfo name_to_browser_info_;
100
101 const InterfaceProxy::Info* id_to_plugin_info_[INTERFACE_ID_COUNT];
102 const InterfaceProxy::Info* id_to_browser_info_[INTERFACE_ID_COUNT];
103};
104
105InterfaceList::InterfaceList() {
[email protected]ce482df2011-02-21 23:49:16106 memset(id_to_plugin_info_, 0, sizeof(id_to_plugin_info_));
107 memset(id_to_browser_info_, 0, sizeof(id_to_browser_info_));
[email protected]465faa22011-02-08 16:31:46108
109 // PPB (browser) interfaces.
110 AddPPB(PPB_AudioConfig_Proxy::GetInfo());
111 AddPPB(PPB_Audio_Proxy::GetInfo());
112 AddPPB(PPB_Buffer_Proxy::GetInfo());
113 AddPPB(PPB_CharSet_Proxy::GetInfo());
[email protected]d38c57402011-03-11 20:20:56114 AddPPB(PPB_Console_Proxy::GetInfo());
[email protected]465faa22011-02-08 16:31:46115 AddPPB(PPB_Context3D_Proxy::GetInfo());
116 AddPPB(PPB_Core_Proxy::GetInfo());
117 AddPPB(PPB_CursorControl_Proxy::GetInfo());
[email protected]4deeb432011-02-17 23:59:39118 AddPPB(PPB_FileChooser_Proxy::GetInfo());
119 AddPPB(PPB_FileRef_Proxy::GetInfo());
[email protected]ce482df2011-02-21 23:49:16120 AddPPB(PPB_FileSystem_Proxy::GetInfo());
[email protected]5cf8a5b2011-03-09 19:00:05121 AddPPB(PPB_Flash_Clipboard_Proxy::GetInfo());
[email protected]d062d962011-03-02 01:27:26122 AddPPB(PPB_Flash_File_FileRef_Proxy::GetInfo());
[email protected]d07fa3792011-02-24 18:58:01123 AddPPB(PPB_Flash_File_ModuleLocal_Proxy::GetInfo());
[email protected]465faa22011-02-08 16:31:46124 AddPPB(PPB_Flash_Proxy::GetInfo());
[email protected]7358d572011-02-15 18:44:40125 AddPPB(PPB_Flash_Menu_Proxy::GetInfo());
[email protected]465faa22011-02-08 16:31:46126 AddPPB(PPB_Font_Proxy::GetInfo());
127 AddPPB(PPB_Fullscreen_Proxy::GetInfo());
128 AddPPB(PPB_GLESChromiumTextureMapping_Proxy::GetInfo());
129 AddPPB(PPB_Graphics2D_Proxy::GetInfo());
130 AddPPB(PPB_ImageData_Proxy::GetInfo());
131 AddPPB(PPB_Instance_Proxy::GetInfo());
132 AddPPB(PPB_OpenGLES2_Proxy::GetInfo());
133 AddPPB(PPB_PDF_Proxy::GetInfo());
134 AddPPB(PPB_Surface3D_Proxy::GetInfo());
135 AddPPB(PPB_Testing_Proxy::GetInfo());
136 AddPPB(PPB_URLLoader_Proxy::GetInfo());
137 AddPPB(PPB_URLLoaderTrusted_Proxy::GetInfo());
138 AddPPB(PPB_URLRequestInfo_Proxy::GetInfo());
139 AddPPB(PPB_URLResponseInfo_Proxy::GetInfo());
[email protected]9ca245e2011-03-18 01:50:31140 AddPPB(PPB_URLUtil_Proxy::GetInfo());
[email protected]465faa22011-02-08 16:31:46141 AddPPB(PPB_Var_Deprecated_Proxy::GetInfo());
142
[email protected]7d3782f2011-03-29 19:16:23143#ifdef ENABLE_FLAPPER_HACKS
144 AddPPB(PPB_Flash_NetConnector_Proxy::GetInfo());
145#endif
146
[email protected]465faa22011-02-08 16:31:46147 // PPP (plugin) interfaces.
[email protected]8c3bd1d2011-04-12 20:01:42148 AddPPP(PPP_Graphics3D_Proxy::GetInfo());
[email protected]465faa22011-02-08 16:31:46149 AddPPP(PPP_Instance_Proxy::GetInfo());
150}
151
152void InterfaceList::AddPPP(const InterfaceProxy::Info* info) {
153 DCHECK(name_to_plugin_info_.find(info->name) ==
154 name_to_plugin_info_.end());
155 DCHECK(info->id > 0 && info->id < INTERFACE_ID_COUNT);
156 DCHECK(id_to_plugin_info_[info->id] == NULL);
157
158 name_to_plugin_info_[info->name] = info;
159 id_to_plugin_info_[info->id] = info;
160}
161
162void InterfaceList::AddPPB(const InterfaceProxy::Info* info) {
163 DCHECK(name_to_browser_info_.find(info->name) ==
164 name_to_browser_info_.end());
165 DCHECK(info->id > 0 && info->id < INTERFACE_ID_COUNT);
166 DCHECK(id_to_browser_info_[info->id] == NULL);
167
168 name_to_browser_info_[std::string(info->name)] = info;
169 id_to_browser_info_[info->id] = info;
170}
171
172// static
173InterfaceList* InterfaceList::GetInstance() {
174 return Singleton<InterfaceList>::get();
175}
176
177} // namespace
178
[email protected]5d84d012010-12-02 17:17:21179Dispatcher::Dispatcher(base::ProcessHandle remote_process_handle,
180 GetInterfaceFunc local_get_interface)
[email protected]2cc062242011-03-10 21:16:34181 : delegate_(NULL),
182 remote_process_handle_(remote_process_handle),
[email protected]f24448db2011-01-27 20:40:39183 test_sink_(NULL),
[email protected]5f2517612010-12-02 22:36:48184 disallow_trusted_interfaces_(false), // TODO(brettw) make this settable.
[email protected]c2932f5e2010-11-03 03:22:33185 local_get_interface_(local_get_interface),
[email protected]709a847e2010-11-10 01:16:11186 callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
[email protected]c2932f5e2010-11-03 03:22:33187}
188
189Dispatcher::~Dispatcher() {
190}
191
[email protected]2cc062242011-03-10 21:16:34192bool Dispatcher::InitWithChannel(Delegate* delegate,
[email protected]42ce94e2010-12-08 19:28:09193 const IPC::ChannelHandle& channel_handle,
[email protected]2cc062242011-03-10 21:16:34194 bool is_client) {
[email protected]1609d0d2011-04-15 22:06:21195 delegate_ = delegate;
[email protected]c2932f5e2010-11-03 03:22:33196 IPC::Channel::Mode mode = is_client ? IPC::Channel::MODE_CLIENT
197 : IPC::Channel::MODE_SERVER;
[email protected]42ce94e2010-12-08 19:28:09198 channel_.reset(new IPC::SyncChannel(channel_handle, mode, this,
[email protected]2cc062242011-03-10 21:16:34199 delegate->GetIPCMessageLoop(), false,
200 delegate->GetShutdownEvent()));
[email protected]c2932f5e2010-11-03 03:22:33201 return true;
202}
203
[email protected]f24448db2011-01-27 20:40:39204void Dispatcher::InitWithTestSink(IPC::TestSink* test_sink) {
205 DCHECK(!test_sink_);
206 test_sink_ = test_sink;
207}
208
[email protected]a95986a82010-12-24 06:19:28209bool Dispatcher::OnMessageReceived(const IPC::Message& msg) {
[email protected]c2932f5e2010-11-03 03:22:33210 // Control messages.
211 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
[email protected]a95986a82010-12-24 06:19:28212 bool handled = true;
[email protected]c2932f5e2010-11-03 03:22:33213 IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg)
[email protected]c2932f5e2010-11-03 03:22:33214 IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_,
215 CallbackTracker::ReceiveExecuteSerializedCallback)
[email protected]a95986a82010-12-24 06:19:28216 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]c2932f5e2010-11-03 03:22:33217 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28218 return handled;
[email protected]c2932f5e2010-11-03 03:22:33219 }
[email protected]a95986a82010-12-24 06:19:28220 return false;
[email protected]c2932f5e2010-11-03 03:22:33221}
222
[email protected]4f15d2842011-02-15 17:36:33223void Dispatcher::OnChannelError() {
224 channel_.reset();
225}
226
[email protected]465faa22011-02-08 16:31:46227// static
228const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo(
229 const std::string& name) {
230 const InterfaceList* list = InterfaceList::GetInstance();
231 InterfaceList::NameToInfo::const_iterator found =
232 list->name_to_browser_info_.find(name);
233 if (found == list->name_to_browser_info_.end())
234 return NULL;
235 return found->second;
236}
237
238// static
239const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo(InterfaceID id) {
240 if (id <= 0 || id >= INTERFACE_ID_COUNT)
241 return NULL;
242 const InterfaceList* list = InterfaceList::GetInstance();
243 return list->id_to_browser_info_[id];
244}
245
246// static
247const InterfaceProxy::Info* Dispatcher::GetPPPInterfaceInfo(
248 const std::string& name) {
249 const InterfaceList* list = InterfaceList::GetInstance();
250 InterfaceList::NameToInfo::const_iterator found =
251 list->name_to_plugin_info_.find(name);
252 if (found == list->name_to_plugin_info_.end())
253 return NULL;
254 return found->second;
255}
256
257// static
258const InterfaceProxy::Info* Dispatcher::GetPPPInterfaceInfo(InterfaceID id) {
259 if (id <= 0 || id >= INTERFACE_ID_COUNT)
260 return NULL;
261 const InterfaceList* list = InterfaceList::GetInstance();
262 return list->id_to_plugin_info_[id];
263}
264
[email protected]c2932f5e2010-11-03 03:22:33265void Dispatcher::SetSerializationRules(
266 VarSerializationRules* var_serialization_rules) {
267 serialization_rules_.reset(var_serialization_rules);
268}
269
[email protected]84396dbc2011-04-14 06:33:42270const void* Dispatcher::GetLocalInterface(const char* interface_name) {
271 return local_get_interface_(interface_name);
[email protected]c2932f5e2010-11-03 03:22:33272}
273
[email protected]db0f57f52011-02-25 03:54:54274IPC::PlatformFileForTransit Dispatcher::ShareHandleWithRemote(
275 base::PlatformFile handle,
276 bool should_close_source) {
277 IPC::PlatformFileForTransit out_handle;
278#if defined(OS_WIN)
279 DWORD options = DUPLICATE_SAME_ACCESS;
280 if (should_close_source)
281 options |= DUPLICATE_CLOSE_SOURCE;
282 if (!::DuplicateHandle(::GetCurrentProcess(),
283 handle,
284 remote_process_handle_,
285 &out_handle,
286 0,
287 FALSE,
288 options))
289 out_handle = IPC::InvalidPlatformFileForTransit();
290#elif defined(OS_POSIX)
291 // If asked to close the source, we can simply re-use the source fd instead of
292 // dup()ing and close()ing.
293 int fd = should_close_source ? handle : ::dup(handle);
294 out_handle = base::FileDescriptor(fd, true);
295#else
296 #error Not implemented.
297#endif
298 return out_handle;
299}
300
[email protected]1609d0d2011-04-15 22:06:21301MessageLoop* Dispatcher::GetIPCMessageLoop() {
302 return delegate_->GetIPCMessageLoop();
303}
304
305void Dispatcher::AddIOThreadMessageFilter(
306 IPC::ChannelProxy::MessageFilter* filter) {
307 channel_->AddFilter(filter);
308}
309
[email protected]c2932f5e2010-11-03 03:22:33310bool Dispatcher::Send(IPC::Message* msg) {
[email protected]f24448db2011-01-27 20:40:39311 if (test_sink_)
312 return test_sink_->Send(msg);
[email protected]4f15d2842011-02-15 17:36:33313 if (channel_.get())
314 return channel_->Send(msg);
315
316 // Remote side crashed, drop this message.
317 delete msg;
318 return false;
[email protected]c2932f5e2010-11-03 03:22:33319}
320
[email protected]c2932f5e2010-11-03 03:22:33321} // namespace proxy
322} // namespace pp
323