blob: 93130940afa4a56fb0470b53c26c804ff95db2e2 [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]917a44b02011-01-13 17:07:2944#include "ppapi/c/private/ppb_pdf.h"
[email protected]5f2517612010-12-02 22:36:4845#include "ppapi/c/trusted/ppb_url_loader_trusted.h"
[email protected]9ca952d2010-11-11 20:43:5046#include "ppapi/proxy/ppapi_messages.h"
[email protected]5d84d012010-12-02 17:17:2147#include "ppapi/proxy/ppb_audio_config_proxy.h"
48#include "ppapi/proxy/ppb_audio_proxy.h"
[email protected]43a40202010-11-12 16:25:0149#include "ppapi/proxy/ppb_buffer_proxy.h"
[email protected]9ca952d2010-11-11 20:43:5050#include "ppapi/proxy/ppb_char_set_proxy.h"
[email protected]d38c57402011-03-11 20:20:5651#include "ppapi/proxy/ppb_console_proxy.h"
[email protected]f56279c2011-02-02 18:12:3152#include "ppapi/proxy/ppb_context_3d_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3353#include "ppapi/proxy/ppb_core_proxy.h"
[email protected]a3010332010-11-12 07:09:3554#include "ppapi/proxy/ppb_cursor_control_proxy.h"
[email protected]4deeb432011-02-17 23:59:3955#include "ppapi/proxy/ppb_file_chooser_proxy.h"
56#include "ppapi/proxy/ppb_file_ref_proxy.h"
[email protected]ce482df2011-02-21 23:49:1657#include "ppapi/proxy/ppb_file_system_proxy.h"
[email protected]5cf8a5b2011-03-09 19:00:0558#include "ppapi/proxy/ppb_flash_clipboard_proxy.h"
[email protected]d07fa3792011-02-24 18:58:0159#include "ppapi/proxy/ppb_flash_file_proxy.h"
[email protected]5f2517612010-12-02 22:36:4860#include "ppapi/proxy/ppb_flash_proxy.h"
[email protected]7358d572011-02-15 18:44:4061#include "ppapi/proxy/ppb_flash_menu_proxy.h"
[email protected]9ca952d2010-11-11 20:43:5062#include "ppapi/proxy/ppb_font_proxy.h"
[email protected]a3010332010-11-12 07:09:3563#include "ppapi/proxy/ppb_fullscreen_proxy.h"
[email protected]f56279c2011-02-02 18:12:3164#include "ppapi/proxy/ppb_gles_chromium_texture_mapping_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3365#include "ppapi/proxy/ppb_graphics_2d_proxy.h"
66#include "ppapi/proxy/ppb_image_data_proxy.h"
67#include "ppapi/proxy/ppb_instance_proxy.h"
[email protected]f56279c2011-02-02 18:12:3168#include "ppapi/proxy/ppb_opengles2_proxy.h"
[email protected]43a40202010-11-12 16:25:0169#include "ppapi/proxy/ppb_pdf_proxy.h"
[email protected]f56279c2011-02-02 18:12:3170#include "ppapi/proxy/ppb_surface_3d_proxy.h"
[email protected]799d1ab2010-11-09 17:16:2871#include "ppapi/proxy/ppb_testing_proxy.h"
72#include "ppapi/proxy/ppb_url_loader_proxy.h"
73#include "ppapi/proxy/ppb_url_request_info_proxy.h"
74#include "ppapi/proxy/ppb_url_response_info_proxy.h"
[email protected]9ca245e2011-03-18 01:50:3175#include "ppapi/proxy/ppb_url_util_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3376#include "ppapi/proxy/ppb_var_deprecated_proxy.h"
77#include "ppapi/proxy/ppp_class_proxy.h"
78#include "ppapi/proxy/ppp_instance_proxy.h"
79#include "ppapi/proxy/var_serialization_rules.h"
80
81namespace pp {
82namespace proxy {
83
[email protected]465faa22011-02-08 16:31:4684namespace {
85
86struct InterfaceList {
87 InterfaceList();
88
89 static InterfaceList* GetInstance();
90
91 void AddPPP(const InterfaceProxy::Info* info);
92 void AddPPB(const InterfaceProxy::Info* info);
93
94 typedef std::map<std::string, const InterfaceProxy::Info*> NameToInfo;
95 NameToInfo name_to_plugin_info_;
96 NameToInfo name_to_browser_info_;
97
98 const InterfaceProxy::Info* id_to_plugin_info_[INTERFACE_ID_COUNT];
99 const InterfaceProxy::Info* id_to_browser_info_[INTERFACE_ID_COUNT];
100};
101
102InterfaceList::InterfaceList() {
[email protected]ce482df2011-02-21 23:49:16103 memset(id_to_plugin_info_, 0, sizeof(id_to_plugin_info_));
104 memset(id_to_browser_info_, 0, sizeof(id_to_browser_info_));
[email protected]465faa22011-02-08 16:31:46105
106 // PPB (browser) interfaces.
107 AddPPB(PPB_AudioConfig_Proxy::GetInfo());
108 AddPPB(PPB_Audio_Proxy::GetInfo());
109 AddPPB(PPB_Buffer_Proxy::GetInfo());
110 AddPPB(PPB_CharSet_Proxy::GetInfo());
[email protected]d38c57402011-03-11 20:20:56111 AddPPB(PPB_Console_Proxy::GetInfo());
[email protected]465faa22011-02-08 16:31:46112 AddPPB(PPB_Context3D_Proxy::GetInfo());
113 AddPPB(PPB_Core_Proxy::GetInfo());
114 AddPPB(PPB_CursorControl_Proxy::GetInfo());
[email protected]4deeb432011-02-17 23:59:39115 AddPPB(PPB_FileChooser_Proxy::GetInfo());
116 AddPPB(PPB_FileRef_Proxy::GetInfo());
[email protected]ce482df2011-02-21 23:49:16117 AddPPB(PPB_FileSystem_Proxy::GetInfo());
[email protected]5cf8a5b2011-03-09 19:00:05118 AddPPB(PPB_Flash_Clipboard_Proxy::GetInfo());
[email protected]d062d962011-03-02 01:27:26119 AddPPB(PPB_Flash_File_FileRef_Proxy::GetInfo());
[email protected]d07fa3792011-02-24 18:58:01120 AddPPB(PPB_Flash_File_ModuleLocal_Proxy::GetInfo());
[email protected]465faa22011-02-08 16:31:46121 AddPPB(PPB_Flash_Proxy::GetInfo());
[email protected]7358d572011-02-15 18:44:40122 AddPPB(PPB_Flash_Menu_Proxy::GetInfo());
[email protected]465faa22011-02-08 16:31:46123 AddPPB(PPB_Font_Proxy::GetInfo());
124 AddPPB(PPB_Fullscreen_Proxy::GetInfo());
125 AddPPB(PPB_GLESChromiumTextureMapping_Proxy::GetInfo());
126 AddPPB(PPB_Graphics2D_Proxy::GetInfo());
127 AddPPB(PPB_ImageData_Proxy::GetInfo());
128 AddPPB(PPB_Instance_Proxy::GetInfo());
129 AddPPB(PPB_OpenGLES2_Proxy::GetInfo());
130 AddPPB(PPB_PDF_Proxy::GetInfo());
131 AddPPB(PPB_Surface3D_Proxy::GetInfo());
132 AddPPB(PPB_Testing_Proxy::GetInfo());
133 AddPPB(PPB_URLLoader_Proxy::GetInfo());
134 AddPPB(PPB_URLLoaderTrusted_Proxy::GetInfo());
135 AddPPB(PPB_URLRequestInfo_Proxy::GetInfo());
136 AddPPB(PPB_URLResponseInfo_Proxy::GetInfo());
[email protected]9ca245e2011-03-18 01:50:31137 AddPPB(PPB_URLUtil_Proxy::GetInfo());
[email protected]465faa22011-02-08 16:31:46138 AddPPB(PPB_Var_Deprecated_Proxy::GetInfo());
139
140 // PPP (plugin) interfaces.
141 AddPPP(PPP_Instance_Proxy::GetInfo());
142}
143
144void InterfaceList::AddPPP(const InterfaceProxy::Info* info) {
145 DCHECK(name_to_plugin_info_.find(info->name) ==
146 name_to_plugin_info_.end());
147 DCHECK(info->id > 0 && info->id < INTERFACE_ID_COUNT);
148 DCHECK(id_to_plugin_info_[info->id] == NULL);
149
150 name_to_plugin_info_[info->name] = info;
151 id_to_plugin_info_[info->id] = info;
152}
153
154void InterfaceList::AddPPB(const InterfaceProxy::Info* info) {
155 DCHECK(name_to_browser_info_.find(info->name) ==
156 name_to_browser_info_.end());
157 DCHECK(info->id > 0 && info->id < INTERFACE_ID_COUNT);
158 DCHECK(id_to_browser_info_[info->id] == NULL);
159
160 name_to_browser_info_[std::string(info->name)] = info;
161 id_to_browser_info_[info->id] = info;
162}
163
164// static
165InterfaceList* InterfaceList::GetInstance() {
166 return Singleton<InterfaceList>::get();
167}
168
169} // namespace
170
[email protected]5d84d012010-12-02 17:17:21171Dispatcher::Dispatcher(base::ProcessHandle remote_process_handle,
172 GetInterfaceFunc local_get_interface)
[email protected]2cc062242011-03-10 21:16:34173 : delegate_(NULL),
174 remote_process_handle_(remote_process_handle),
[email protected]f24448db2011-01-27 20:40:39175 test_sink_(NULL),
[email protected]5f2517612010-12-02 22:36:48176 disallow_trusted_interfaces_(false), // TODO(brettw) make this settable.
[email protected]c2932f5e2010-11-03 03:22:33177 local_get_interface_(local_get_interface),
[email protected]709a847e2010-11-10 01:16:11178 callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
[email protected]c2932f5e2010-11-03 03:22:33179}
180
181Dispatcher::~Dispatcher() {
182}
183
[email protected]2cc062242011-03-10 21:16:34184bool Dispatcher::InitWithChannel(Delegate* delegate,
[email protected]42ce94e2010-12-08 19:28:09185 const IPC::ChannelHandle& channel_handle,
[email protected]2cc062242011-03-10 21:16:34186 bool is_client) {
[email protected]c2932f5e2010-11-03 03:22:33187 IPC::Channel::Mode mode = is_client ? IPC::Channel::MODE_CLIENT
188 : IPC::Channel::MODE_SERVER;
[email protected]42ce94e2010-12-08 19:28:09189 channel_.reset(new IPC::SyncChannel(channel_handle, mode, this,
[email protected]2cc062242011-03-10 21:16:34190 delegate->GetIPCMessageLoop(), false,
191 delegate->GetShutdownEvent()));
[email protected]c2932f5e2010-11-03 03:22:33192 return true;
193}
194
[email protected]f24448db2011-01-27 20:40:39195void Dispatcher::InitWithTestSink(IPC::TestSink* test_sink) {
196 DCHECK(!test_sink_);
197 test_sink_ = test_sink;
198}
199
[email protected]a95986a82010-12-24 06:19:28200bool Dispatcher::OnMessageReceived(const IPC::Message& msg) {
[email protected]c2932f5e2010-11-03 03:22:33201 // Control messages.
202 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
[email protected]a95986a82010-12-24 06:19:28203 bool handled = true;
[email protected]c2932f5e2010-11-03 03:22:33204 IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg)
[email protected]c2932f5e2010-11-03 03:22:33205 IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_,
206 CallbackTracker::ReceiveExecuteSerializedCallback)
[email protected]a95986a82010-12-24 06:19:28207 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]c2932f5e2010-11-03 03:22:33208 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28209 return handled;
[email protected]c2932f5e2010-11-03 03:22:33210 }
[email protected]a95986a82010-12-24 06:19:28211 return false;
[email protected]c2932f5e2010-11-03 03:22:33212}
213
[email protected]4f15d2842011-02-15 17:36:33214void Dispatcher::OnChannelError() {
215 channel_.reset();
216}
217
[email protected]465faa22011-02-08 16:31:46218// static
219const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo(
220 const std::string& name) {
221 const InterfaceList* list = InterfaceList::GetInstance();
222 InterfaceList::NameToInfo::const_iterator found =
223 list->name_to_browser_info_.find(name);
224 if (found == list->name_to_browser_info_.end())
225 return NULL;
226 return found->second;
227}
228
229// static
230const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo(InterfaceID id) {
231 if (id <= 0 || id >= INTERFACE_ID_COUNT)
232 return NULL;
233 const InterfaceList* list = InterfaceList::GetInstance();
234 return list->id_to_browser_info_[id];
235}
236
237// static
238const InterfaceProxy::Info* Dispatcher::GetPPPInterfaceInfo(
239 const std::string& name) {
240 const InterfaceList* list = InterfaceList::GetInstance();
241 InterfaceList::NameToInfo::const_iterator found =
242 list->name_to_plugin_info_.find(name);
243 if (found == list->name_to_plugin_info_.end())
244 return NULL;
245 return found->second;
246}
247
248// static
249const InterfaceProxy::Info* Dispatcher::GetPPPInterfaceInfo(InterfaceID id) {
250 if (id <= 0 || id >= INTERFACE_ID_COUNT)
251 return NULL;
252 const InterfaceList* list = InterfaceList::GetInstance();
253 return list->id_to_plugin_info_[id];
254}
255
[email protected]c2932f5e2010-11-03 03:22:33256void Dispatcher::SetSerializationRules(
257 VarSerializationRules* var_serialization_rules) {
258 serialization_rules_.reset(var_serialization_rules);
259}
260
[email protected]c2932f5e2010-11-03 03:22:33261const void* Dispatcher::GetLocalInterface(const char* interface) {
262 return local_get_interface_(interface);
263}
264
[email protected]db0f57f52011-02-25 03:54:54265IPC::PlatformFileForTransit Dispatcher::ShareHandleWithRemote(
266 base::PlatformFile handle,
267 bool should_close_source) {
268 IPC::PlatformFileForTransit out_handle;
269#if defined(OS_WIN)
270 DWORD options = DUPLICATE_SAME_ACCESS;
271 if (should_close_source)
272 options |= DUPLICATE_CLOSE_SOURCE;
273 if (!::DuplicateHandle(::GetCurrentProcess(),
274 handle,
275 remote_process_handle_,
276 &out_handle,
277 0,
278 FALSE,
279 options))
280 out_handle = IPC::InvalidPlatformFileForTransit();
281#elif defined(OS_POSIX)
282 // If asked to close the source, we can simply re-use the source fd instead of
283 // dup()ing and close()ing.
284 int fd = should_close_source ? handle : ::dup(handle);
285 out_handle = base::FileDescriptor(fd, true);
286#else
287 #error Not implemented.
288#endif
289 return out_handle;
290}
291
[email protected]c2932f5e2010-11-03 03:22:33292bool Dispatcher::Send(IPC::Message* msg) {
[email protected]f24448db2011-01-27 20:40:39293 if (test_sink_)
294 return test_sink_->Send(msg);
[email protected]4f15d2842011-02-15 17:36:33295 if (channel_.get())
296 return channel_->Send(msg);
297
298 // Remote side crashed, drop this message.
299 delete msg;
300 return false;
[email protected]c2932f5e2010-11-03 03:22:33301}
302
[email protected]c2932f5e2010-11-03 03:22:33303} // namespace proxy
304} // namespace pp
305