blob: dd4167c3d0469bf9ce173592fe576663600fb156 [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]465faa22011-02-08 16:31:4613#include "base/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]c2932f5e2010-11-03 03:22:3327#include "ppapi/c/dev/ppb_var_deprecated.h"
28#include "ppapi/c/pp_errors.h"
[email protected]b9a59842011-01-15 01:04:0029#include "ppapi/c/ppb_audio.h"
30#include "ppapi/c/ppb_audio_config.h"
[email protected]c2932f5e2010-11-03 03:22:3331#include "ppapi/c/ppb_core.h"
32#include "ppapi/c/ppb_graphics_2d.h"
33#include "ppapi/c/ppb_image_data.h"
34#include "ppapi/c/ppb_instance.h"
[email protected]5a3f62852010-11-10 21:43:0135#include "ppapi/c/ppb_url_loader.h"
36#include "ppapi/c/ppb_url_request_info.h"
37#include "ppapi/c/ppb_url_response_info.h"
[email protected]9ca952d2010-11-11 20:43:5038#include "ppapi/c/ppp_instance.h"
[email protected]b697e1a2011-01-06 22:20:2839#include "ppapi/c/private/ppb_flash.h"
[email protected]5cf8a5b2011-03-09 19:00:0540#include "ppapi/c/private/ppb_flash_clipboard.h"
[email protected]d07fa3792011-02-24 18:58:0141#include "ppapi/c/private/ppb_flash_file.h"
[email protected]7358d572011-02-15 18:44:4042#include "ppapi/c/private/ppb_flash_menu.h"
[email protected]917a44b02011-01-13 17:07:2943#include "ppapi/c/private/ppb_pdf.h"
[email protected]5f2517612010-12-02 22:36:4844#include "ppapi/c/trusted/ppb_url_loader_trusted.h"
[email protected]9ca952d2010-11-11 20:43:5045#include "ppapi/proxy/ppapi_messages.h"
[email protected]5d84d012010-12-02 17:17:2146#include "ppapi/proxy/ppb_audio_config_proxy.h"
47#include "ppapi/proxy/ppb_audio_proxy.h"
[email protected]43a40202010-11-12 16:25:0148#include "ppapi/proxy/ppb_buffer_proxy.h"
[email protected]9ca952d2010-11-11 20:43:5049#include "ppapi/proxy/ppb_char_set_proxy.h"
[email protected]f56279c2011-02-02 18:12:3150#include "ppapi/proxy/ppb_context_3d_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3351#include "ppapi/proxy/ppb_core_proxy.h"
[email protected]a3010332010-11-12 07:09:3552#include "ppapi/proxy/ppb_cursor_control_proxy.h"
[email protected]4deeb432011-02-17 23:59:3953#include "ppapi/proxy/ppb_file_chooser_proxy.h"
54#include "ppapi/proxy/ppb_file_ref_proxy.h"
[email protected]ce482df2011-02-21 23:49:1655#include "ppapi/proxy/ppb_file_system_proxy.h"
[email protected]5cf8a5b2011-03-09 19:00:0556#include "ppapi/proxy/ppb_flash_clipboard_proxy.h"
[email protected]d07fa3792011-02-24 18:58:0157#include "ppapi/proxy/ppb_flash_file_proxy.h"
[email protected]5f2517612010-12-02 22:36:4858#include "ppapi/proxy/ppb_flash_proxy.h"
[email protected]7358d572011-02-15 18:44:4059#include "ppapi/proxy/ppb_flash_menu_proxy.h"
[email protected]9ca952d2010-11-11 20:43:5060#include "ppapi/proxy/ppb_font_proxy.h"
[email protected]a3010332010-11-12 07:09:3561#include "ppapi/proxy/ppb_fullscreen_proxy.h"
[email protected]f56279c2011-02-02 18:12:3162#include "ppapi/proxy/ppb_gles_chromium_texture_mapping_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3363#include "ppapi/proxy/ppb_graphics_2d_proxy.h"
64#include "ppapi/proxy/ppb_image_data_proxy.h"
65#include "ppapi/proxy/ppb_instance_proxy.h"
[email protected]f56279c2011-02-02 18:12:3166#include "ppapi/proxy/ppb_opengles2_proxy.h"
[email protected]43a40202010-11-12 16:25:0167#include "ppapi/proxy/ppb_pdf_proxy.h"
[email protected]f56279c2011-02-02 18:12:3168#include "ppapi/proxy/ppb_surface_3d_proxy.h"
[email protected]799d1ab2010-11-09 17:16:2869#include "ppapi/proxy/ppb_testing_proxy.h"
70#include "ppapi/proxy/ppb_url_loader_proxy.h"
71#include "ppapi/proxy/ppb_url_request_info_proxy.h"
72#include "ppapi/proxy/ppb_url_response_info_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3373#include "ppapi/proxy/ppb_var_deprecated_proxy.h"
74#include "ppapi/proxy/ppp_class_proxy.h"
75#include "ppapi/proxy/ppp_instance_proxy.h"
76#include "ppapi/proxy/var_serialization_rules.h"
77
78namespace pp {
79namespace proxy {
80
[email protected]465faa22011-02-08 16:31:4681namespace {
82
83struct InterfaceList {
84 InterfaceList();
85
86 static InterfaceList* GetInstance();
87
88 void AddPPP(const InterfaceProxy::Info* info);
89 void AddPPB(const InterfaceProxy::Info* info);
90
91 typedef std::map<std::string, const InterfaceProxy::Info*> NameToInfo;
92 NameToInfo name_to_plugin_info_;
93 NameToInfo name_to_browser_info_;
94
95 const InterfaceProxy::Info* id_to_plugin_info_[INTERFACE_ID_COUNT];
96 const InterfaceProxy::Info* id_to_browser_info_[INTERFACE_ID_COUNT];
97};
98
99InterfaceList::InterfaceList() {
[email protected]ce482df2011-02-21 23:49:16100 memset(id_to_plugin_info_, 0, sizeof(id_to_plugin_info_));
101 memset(id_to_browser_info_, 0, sizeof(id_to_browser_info_));
[email protected]465faa22011-02-08 16:31:46102
103 // PPB (browser) interfaces.
104 AddPPB(PPB_AudioConfig_Proxy::GetInfo());
105 AddPPB(PPB_Audio_Proxy::GetInfo());
106 AddPPB(PPB_Buffer_Proxy::GetInfo());
107 AddPPB(PPB_CharSet_Proxy::GetInfo());
108 AddPPB(PPB_Context3D_Proxy::GetInfo());
109 AddPPB(PPB_Core_Proxy::GetInfo());
110 AddPPB(PPB_CursorControl_Proxy::GetInfo());
[email protected]4deeb432011-02-17 23:59:39111 AddPPB(PPB_FileChooser_Proxy::GetInfo());
112 AddPPB(PPB_FileRef_Proxy::GetInfo());
[email protected]ce482df2011-02-21 23:49:16113 AddPPB(PPB_FileSystem_Proxy::GetInfo());
[email protected]5cf8a5b2011-03-09 19:00:05114 AddPPB(PPB_Flash_Clipboard_Proxy::GetInfo());
[email protected]d062d962011-03-02 01:27:26115 AddPPB(PPB_Flash_File_FileRef_Proxy::GetInfo());
[email protected]d07fa3792011-02-24 18:58:01116 AddPPB(PPB_Flash_File_ModuleLocal_Proxy::GetInfo());
[email protected]465faa22011-02-08 16:31:46117 AddPPB(PPB_Flash_Proxy::GetInfo());
[email protected]7358d572011-02-15 18:44:40118 AddPPB(PPB_Flash_Menu_Proxy::GetInfo());
[email protected]465faa22011-02-08 16:31:46119 AddPPB(PPB_Font_Proxy::GetInfo());
120 AddPPB(PPB_Fullscreen_Proxy::GetInfo());
121 AddPPB(PPB_GLESChromiumTextureMapping_Proxy::GetInfo());
122 AddPPB(PPB_Graphics2D_Proxy::GetInfo());
123 AddPPB(PPB_ImageData_Proxy::GetInfo());
124 AddPPB(PPB_Instance_Proxy::GetInfo());
125 AddPPB(PPB_OpenGLES2_Proxy::GetInfo());
126 AddPPB(PPB_PDF_Proxy::GetInfo());
127 AddPPB(PPB_Surface3D_Proxy::GetInfo());
128 AddPPB(PPB_Testing_Proxy::GetInfo());
129 AddPPB(PPB_URLLoader_Proxy::GetInfo());
130 AddPPB(PPB_URLLoaderTrusted_Proxy::GetInfo());
131 AddPPB(PPB_URLRequestInfo_Proxy::GetInfo());
132 AddPPB(PPB_URLResponseInfo_Proxy::GetInfo());
133 AddPPB(PPB_Var_Deprecated_Proxy::GetInfo());
134
135 // PPP (plugin) interfaces.
136 AddPPP(PPP_Instance_Proxy::GetInfo());
137}
138
139void InterfaceList::AddPPP(const InterfaceProxy::Info* info) {
140 DCHECK(name_to_plugin_info_.find(info->name) ==
141 name_to_plugin_info_.end());
142 DCHECK(info->id > 0 && info->id < INTERFACE_ID_COUNT);
143 DCHECK(id_to_plugin_info_[info->id] == NULL);
144
145 name_to_plugin_info_[info->name] = info;
146 id_to_plugin_info_[info->id] = info;
147}
148
149void InterfaceList::AddPPB(const InterfaceProxy::Info* info) {
150 DCHECK(name_to_browser_info_.find(info->name) ==
151 name_to_browser_info_.end());
152 DCHECK(info->id > 0 && info->id < INTERFACE_ID_COUNT);
153 DCHECK(id_to_browser_info_[info->id] == NULL);
154
155 name_to_browser_info_[std::string(info->name)] = info;
156 id_to_browser_info_[info->id] = info;
157}
158
159// static
160InterfaceList* InterfaceList::GetInstance() {
161 return Singleton<InterfaceList>::get();
162}
163
164} // namespace
165
[email protected]5d84d012010-12-02 17:17:21166Dispatcher::Dispatcher(base::ProcessHandle remote_process_handle,
167 GetInterfaceFunc local_get_interface)
[email protected]4f15d2842011-02-15 17:36:33168 : remote_process_handle_(remote_process_handle),
[email protected]f24448db2011-01-27 20:40:39169 test_sink_(NULL),
[email protected]5f2517612010-12-02 22:36:48170 disallow_trusted_interfaces_(false), // TODO(brettw) make this settable.
[email protected]c2932f5e2010-11-03 03:22:33171 local_get_interface_(local_get_interface),
[email protected]709a847e2010-11-10 01:16:11172 callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
[email protected]c2932f5e2010-11-03 03:22:33173}
174
175Dispatcher::~Dispatcher() {
176}
177
178bool Dispatcher::InitWithChannel(MessageLoop* ipc_message_loop,
[email protected]42ce94e2010-12-08 19:28:09179 const IPC::ChannelHandle& channel_handle,
[email protected]c2932f5e2010-11-03 03:22:33180 bool is_client,
181 base::WaitableEvent* shutdown_event) {
182 IPC::Channel::Mode mode = is_client ? IPC::Channel::MODE_CLIENT
183 : IPC::Channel::MODE_SERVER;
[email protected]42ce94e2010-12-08 19:28:09184 channel_.reset(new IPC::SyncChannel(channel_handle, mode, this,
[email protected]c2932f5e2010-11-03 03:22:33185 ipc_message_loop, false, shutdown_event));
186 return true;
187}
188
[email protected]f24448db2011-01-27 20:40:39189void Dispatcher::InitWithTestSink(IPC::TestSink* test_sink) {
190 DCHECK(!test_sink_);
191 test_sink_ = test_sink;
192}
193
[email protected]a95986a82010-12-24 06:19:28194bool Dispatcher::OnMessageReceived(const IPC::Message& msg) {
[email protected]c2932f5e2010-11-03 03:22:33195 // Control messages.
196 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
[email protected]a95986a82010-12-24 06:19:28197 bool handled = true;
[email protected]c2932f5e2010-11-03 03:22:33198 IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg)
[email protected]c2932f5e2010-11-03 03:22:33199 IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_,
200 CallbackTracker::ReceiveExecuteSerializedCallback)
[email protected]a95986a82010-12-24 06:19:28201 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]c2932f5e2010-11-03 03:22:33202 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28203 return handled;
[email protected]c2932f5e2010-11-03 03:22:33204 }
[email protected]a95986a82010-12-24 06:19:28205 return false;
[email protected]c2932f5e2010-11-03 03:22:33206}
207
[email protected]4f15d2842011-02-15 17:36:33208void Dispatcher::OnChannelError() {
209 channel_.reset();
210}
211
[email protected]465faa22011-02-08 16:31:46212// static
213const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo(
214 const std::string& name) {
215 const InterfaceList* list = InterfaceList::GetInstance();
216 InterfaceList::NameToInfo::const_iterator found =
217 list->name_to_browser_info_.find(name);
218 if (found == list->name_to_browser_info_.end())
219 return NULL;
220 return found->second;
221}
222
223// static
224const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo(InterfaceID id) {
225 if (id <= 0 || id >= INTERFACE_ID_COUNT)
226 return NULL;
227 const InterfaceList* list = InterfaceList::GetInstance();
228 return list->id_to_browser_info_[id];
229}
230
231// static
232const InterfaceProxy::Info* Dispatcher::GetPPPInterfaceInfo(
233 const std::string& name) {
234 const InterfaceList* list = InterfaceList::GetInstance();
235 InterfaceList::NameToInfo::const_iterator found =
236 list->name_to_plugin_info_.find(name);
237 if (found == list->name_to_plugin_info_.end())
238 return NULL;
239 return found->second;
240}
241
242// static
243const InterfaceProxy::Info* Dispatcher::GetPPPInterfaceInfo(InterfaceID id) {
244 if (id <= 0 || id >= INTERFACE_ID_COUNT)
245 return NULL;
246 const InterfaceList* list = InterfaceList::GetInstance();
247 return list->id_to_plugin_info_[id];
248}
249
[email protected]c2932f5e2010-11-03 03:22:33250void Dispatcher::SetSerializationRules(
251 VarSerializationRules* var_serialization_rules) {
252 serialization_rules_.reset(var_serialization_rules);
253}
254
[email protected]c2932f5e2010-11-03 03:22:33255const void* Dispatcher::GetLocalInterface(const char* interface) {
256 return local_get_interface_(interface);
257}
258
[email protected]db0f57f52011-02-25 03:54:54259IPC::PlatformFileForTransit Dispatcher::ShareHandleWithRemote(
260 base::PlatformFile handle,
261 bool should_close_source) {
262 IPC::PlatformFileForTransit out_handle;
263#if defined(OS_WIN)
264 DWORD options = DUPLICATE_SAME_ACCESS;
265 if (should_close_source)
266 options |= DUPLICATE_CLOSE_SOURCE;
267 if (!::DuplicateHandle(::GetCurrentProcess(),
268 handle,
269 remote_process_handle_,
270 &out_handle,
271 0,
272 FALSE,
273 options))
274 out_handle = IPC::InvalidPlatformFileForTransit();
275#elif defined(OS_POSIX)
276 // If asked to close the source, we can simply re-use the source fd instead of
277 // dup()ing and close()ing.
278 int fd = should_close_source ? handle : ::dup(handle);
279 out_handle = base::FileDescriptor(fd, true);
280#else
281 #error Not implemented.
282#endif
283 return out_handle;
284}
285
[email protected]c2932f5e2010-11-03 03:22:33286bool Dispatcher::Send(IPC::Message* msg) {
[email protected]f24448db2011-01-27 20:40:39287 if (test_sink_)
288 return test_sink_->Send(msg);
[email protected]4f15d2842011-02-15 17:36:33289 if (channel_.get())
290 return channel_->Send(msg);
291
292 // Remote side crashed, drop this message.
293 delete msg;
294 return false;
[email protected]c2932f5e2010-11-03 03:22:33295}
296
[email protected]c2932f5e2010-11-03 03:22:33297} // namespace proxy
298} // namespace pp
299