blob: eb9726be93120ccebcb89eae9e6e352e7a35a15e [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]917a44b02011-01-13 17:07:2940#include "ppapi/c/private/ppb_pdf.h"
[email protected]5f2517612010-12-02 22:36:4841#include "ppapi/c/trusted/ppb_url_loader_trusted.h"
[email protected]9ca952d2010-11-11 20:43:5042#include "ppapi/proxy/ppapi_messages.h"
[email protected]5d84d012010-12-02 17:17:2143#include "ppapi/proxy/ppb_audio_config_proxy.h"
44#include "ppapi/proxy/ppb_audio_proxy.h"
[email protected]43a40202010-11-12 16:25:0145#include "ppapi/proxy/ppb_buffer_proxy.h"
[email protected]9ca952d2010-11-11 20:43:5046#include "ppapi/proxy/ppb_char_set_proxy.h"
[email protected]f56279c2011-02-02 18:12:3147#include "ppapi/proxy/ppb_context_3d_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3348#include "ppapi/proxy/ppb_core_proxy.h"
[email protected]a3010332010-11-12 07:09:3549#include "ppapi/proxy/ppb_cursor_control_proxy.h"
[email protected]5f2517612010-12-02 22:36:4850#include "ppapi/proxy/ppb_flash_proxy.h"
[email protected]9ca952d2010-11-11 20:43:5051#include "ppapi/proxy/ppb_font_proxy.h"
[email protected]a3010332010-11-12 07:09:3552#include "ppapi/proxy/ppb_fullscreen_proxy.h"
[email protected]f56279c2011-02-02 18:12:3153#include "ppapi/proxy/ppb_gles_chromium_texture_mapping_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3354#include "ppapi/proxy/ppb_graphics_2d_proxy.h"
55#include "ppapi/proxy/ppb_image_data_proxy.h"
56#include "ppapi/proxy/ppb_instance_proxy.h"
[email protected]f56279c2011-02-02 18:12:3157#include "ppapi/proxy/ppb_opengles2_proxy.h"
[email protected]43a40202010-11-12 16:25:0158#include "ppapi/proxy/ppb_pdf_proxy.h"
[email protected]f56279c2011-02-02 18:12:3159#include "ppapi/proxy/ppb_surface_3d_proxy.h"
[email protected]799d1ab2010-11-09 17:16:2860#include "ppapi/proxy/ppb_testing_proxy.h"
61#include "ppapi/proxy/ppb_url_loader_proxy.h"
62#include "ppapi/proxy/ppb_url_request_info_proxy.h"
63#include "ppapi/proxy/ppb_url_response_info_proxy.h"
[email protected]c2932f5e2010-11-03 03:22:3364#include "ppapi/proxy/ppb_var_deprecated_proxy.h"
65#include "ppapi/proxy/ppp_class_proxy.h"
66#include "ppapi/proxy/ppp_instance_proxy.h"
67#include "ppapi/proxy/var_serialization_rules.h"
68
69namespace pp {
70namespace proxy {
71
[email protected]465faa22011-02-08 16:31:4672namespace {
73
74struct InterfaceList {
75 InterfaceList();
76
77 static InterfaceList* GetInstance();
78
79 void AddPPP(const InterfaceProxy::Info* info);
80 void AddPPB(const InterfaceProxy::Info* info);
81
82 typedef std::map<std::string, const InterfaceProxy::Info*> NameToInfo;
83 NameToInfo name_to_plugin_info_;
84 NameToInfo name_to_browser_info_;
85
86 const InterfaceProxy::Info* id_to_plugin_info_[INTERFACE_ID_COUNT];
87 const InterfaceProxy::Info* id_to_browser_info_[INTERFACE_ID_COUNT];
88};
89
90InterfaceList::InterfaceList() {
91 memset(id_to_plugin_info_, 0,
92 static_cast<int>(INTERFACE_ID_COUNT) * sizeof(InterfaceID));
93 memset(id_to_browser_info_, 0,
94 static_cast<int>(INTERFACE_ID_COUNT) * sizeof(InterfaceID));
95
96 // PPB (browser) interfaces.
97 AddPPB(PPB_AudioConfig_Proxy::GetInfo());
98 AddPPB(PPB_Audio_Proxy::GetInfo());
99 AddPPB(PPB_Buffer_Proxy::GetInfo());
100 AddPPB(PPB_CharSet_Proxy::GetInfo());
101 AddPPB(PPB_Context3D_Proxy::GetInfo());
102 AddPPB(PPB_Core_Proxy::GetInfo());
103 AddPPB(PPB_CursorControl_Proxy::GetInfo());
104 AddPPB(PPB_Flash_Proxy::GetInfo());
105 AddPPB(PPB_Font_Proxy::GetInfo());
106 AddPPB(PPB_Fullscreen_Proxy::GetInfo());
107 AddPPB(PPB_GLESChromiumTextureMapping_Proxy::GetInfo());
108 AddPPB(PPB_Graphics2D_Proxy::GetInfo());
109 AddPPB(PPB_ImageData_Proxy::GetInfo());
110 AddPPB(PPB_Instance_Proxy::GetInfo());
111 AddPPB(PPB_OpenGLES2_Proxy::GetInfo());
112 AddPPB(PPB_PDF_Proxy::GetInfo());
113 AddPPB(PPB_Surface3D_Proxy::GetInfo());
114 AddPPB(PPB_Testing_Proxy::GetInfo());
115 AddPPB(PPB_URLLoader_Proxy::GetInfo());
116 AddPPB(PPB_URLLoaderTrusted_Proxy::GetInfo());
117 AddPPB(PPB_URLRequestInfo_Proxy::GetInfo());
118 AddPPB(PPB_URLResponseInfo_Proxy::GetInfo());
119 AddPPB(PPB_Var_Deprecated_Proxy::GetInfo());
120
121 // PPP (plugin) interfaces.
122 AddPPP(PPP_Instance_Proxy::GetInfo());
123}
124
125void InterfaceList::AddPPP(const InterfaceProxy::Info* info) {
126 DCHECK(name_to_plugin_info_.find(info->name) ==
127 name_to_plugin_info_.end());
128 DCHECK(info->id > 0 && info->id < INTERFACE_ID_COUNT);
129 DCHECK(id_to_plugin_info_[info->id] == NULL);
130
131 name_to_plugin_info_[info->name] = info;
132 id_to_plugin_info_[info->id] = info;
133}
134
135void InterfaceList::AddPPB(const InterfaceProxy::Info* info) {
136 DCHECK(name_to_browser_info_.find(info->name) ==
137 name_to_browser_info_.end());
138 DCHECK(info->id > 0 && info->id < INTERFACE_ID_COUNT);
139 DCHECK(id_to_browser_info_[info->id] == NULL);
140
141 name_to_browser_info_[std::string(info->name)] = info;
142 id_to_browser_info_[info->id] = info;
143}
144
145// static
146InterfaceList* InterfaceList::GetInstance() {
147 return Singleton<InterfaceList>::get();
148}
149
150} // namespace
151
[email protected]5d84d012010-12-02 17:17:21152Dispatcher::Dispatcher(base::ProcessHandle remote_process_handle,
153 GetInterfaceFunc local_get_interface)
[email protected]c2932f5e2010-11-03 03:22:33154 : pp_module_(0),
[email protected]5d84d012010-12-02 17:17:21155 remote_process_handle_(remote_process_handle),
[email protected]f24448db2011-01-27 20:40:39156 test_sink_(NULL),
[email protected]5f2517612010-12-02 22:36:48157 disallow_trusted_interfaces_(false), // TODO(brettw) make this settable.
[email protected]c2932f5e2010-11-03 03:22:33158 local_get_interface_(local_get_interface),
[email protected]709a847e2010-11-10 01:16:11159 callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
[email protected]c2932f5e2010-11-03 03:22:33160}
161
162Dispatcher::~Dispatcher() {
163}
164
165bool Dispatcher::InitWithChannel(MessageLoop* ipc_message_loop,
[email protected]42ce94e2010-12-08 19:28:09166 const IPC::ChannelHandle& channel_handle,
[email protected]c2932f5e2010-11-03 03:22:33167 bool is_client,
168 base::WaitableEvent* shutdown_event) {
169 IPC::Channel::Mode mode = is_client ? IPC::Channel::MODE_CLIENT
170 : IPC::Channel::MODE_SERVER;
[email protected]42ce94e2010-12-08 19:28:09171 channel_.reset(new IPC::SyncChannel(channel_handle, mode, this,
[email protected]c2932f5e2010-11-03 03:22:33172 ipc_message_loop, false, shutdown_event));
173 return true;
174}
175
[email protected]f24448db2011-01-27 20:40:39176void Dispatcher::InitWithTestSink(IPC::TestSink* test_sink) {
177 DCHECK(!test_sink_);
178 test_sink_ = test_sink;
179}
180
[email protected]a95986a82010-12-24 06:19:28181bool Dispatcher::OnMessageReceived(const IPC::Message& msg) {
[email protected]c2932f5e2010-11-03 03:22:33182 // Control messages.
183 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
[email protected]a95986a82010-12-24 06:19:28184 bool handled = true;
[email protected]c2932f5e2010-11-03 03:22:33185 IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg)
[email protected]c2932f5e2010-11-03 03:22:33186 IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_,
187 CallbackTracker::ReceiveExecuteSerializedCallback)
[email protected]a95986a82010-12-24 06:19:28188 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]c2932f5e2010-11-03 03:22:33189 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28190 return handled;
[email protected]c2932f5e2010-11-03 03:22:33191 }
[email protected]a95986a82010-12-24 06:19:28192 return false;
[email protected]c2932f5e2010-11-03 03:22:33193}
194
[email protected]465faa22011-02-08 16:31:46195// static
196const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo(
197 const std::string& name) {
198 const InterfaceList* list = InterfaceList::GetInstance();
199 InterfaceList::NameToInfo::const_iterator found =
200 list->name_to_browser_info_.find(name);
201 if (found == list->name_to_browser_info_.end())
202 return NULL;
203 return found->second;
204}
205
206// static
207const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo(InterfaceID id) {
208 if (id <= 0 || id >= INTERFACE_ID_COUNT)
209 return NULL;
210 const InterfaceList* list = InterfaceList::GetInstance();
211 return list->id_to_browser_info_[id];
212}
213
214// static
215const InterfaceProxy::Info* Dispatcher::GetPPPInterfaceInfo(
216 const std::string& name) {
217 const InterfaceList* list = InterfaceList::GetInstance();
218 InterfaceList::NameToInfo::const_iterator found =
219 list->name_to_plugin_info_.find(name);
220 if (found == list->name_to_plugin_info_.end())
221 return NULL;
222 return found->second;
223}
224
225// static
226const InterfaceProxy::Info* Dispatcher::GetPPPInterfaceInfo(InterfaceID id) {
227 if (id <= 0 || id >= INTERFACE_ID_COUNT)
228 return NULL;
229 const InterfaceList* list = InterfaceList::GetInstance();
230 return list->id_to_plugin_info_[id];
231}
232
[email protected]c2932f5e2010-11-03 03:22:33233void Dispatcher::SetSerializationRules(
234 VarSerializationRules* var_serialization_rules) {
235 serialization_rules_.reset(var_serialization_rules);
236}
237
[email protected]c2932f5e2010-11-03 03:22:33238const void* Dispatcher::GetLocalInterface(const char* interface) {
239 return local_get_interface_(interface);
240}
241
[email protected]c2932f5e2010-11-03 03:22:33242bool Dispatcher::Send(IPC::Message* msg) {
[email protected]f24448db2011-01-27 20:40:39243 if (test_sink_)
244 return test_sink_->Send(msg);
[email protected]c2932f5e2010-11-03 03:22:33245 return channel_->Send(msg);
246}
247
[email protected]c2932f5e2010-11-03 03:22:33248} // namespace proxy
249} // namespace pp
250