blob: f3fbc066d3a3543f6f11063e651d012da28bfe92 [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]4f15d2842011-02-15 17:36:33154 : remote_process_handle_(remote_process_handle),
[email protected]f24448db2011-01-27 20:40:39155 test_sink_(NULL),
[email protected]5f2517612010-12-02 22:36:48156 disallow_trusted_interfaces_(false), // TODO(brettw) make this settable.
[email protected]c2932f5e2010-11-03 03:22:33157 local_get_interface_(local_get_interface),
[email protected]709a847e2010-11-10 01:16:11158 callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
[email protected]c2932f5e2010-11-03 03:22:33159}
160
161Dispatcher::~Dispatcher() {
162}
163
164bool Dispatcher::InitWithChannel(MessageLoop* ipc_message_loop,
[email protected]42ce94e2010-12-08 19:28:09165 const IPC::ChannelHandle& channel_handle,
[email protected]c2932f5e2010-11-03 03:22:33166 bool is_client,
167 base::WaitableEvent* shutdown_event) {
168 IPC::Channel::Mode mode = is_client ? IPC::Channel::MODE_CLIENT
169 : IPC::Channel::MODE_SERVER;
[email protected]42ce94e2010-12-08 19:28:09170 channel_.reset(new IPC::SyncChannel(channel_handle, mode, this,
[email protected]c2932f5e2010-11-03 03:22:33171 ipc_message_loop, false, shutdown_event));
172 return true;
173}
174
[email protected]f24448db2011-01-27 20:40:39175void Dispatcher::InitWithTestSink(IPC::TestSink* test_sink) {
176 DCHECK(!test_sink_);
177 test_sink_ = test_sink;
178}
179
[email protected]a95986a82010-12-24 06:19:28180bool Dispatcher::OnMessageReceived(const IPC::Message& msg) {
[email protected]c2932f5e2010-11-03 03:22:33181 // Control messages.
182 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
[email protected]a95986a82010-12-24 06:19:28183 bool handled = true;
[email protected]c2932f5e2010-11-03 03:22:33184 IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg)
[email protected]c2932f5e2010-11-03 03:22:33185 IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_,
186 CallbackTracker::ReceiveExecuteSerializedCallback)
[email protected]a95986a82010-12-24 06:19:28187 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]c2932f5e2010-11-03 03:22:33188 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28189 return handled;
[email protected]c2932f5e2010-11-03 03:22:33190 }
[email protected]a95986a82010-12-24 06:19:28191 return false;
[email protected]c2932f5e2010-11-03 03:22:33192}
193
[email protected]4f15d2842011-02-15 17:36:33194void Dispatcher::OnChannelError() {
195 channel_.reset();
196}
197
[email protected]465faa22011-02-08 16:31:46198// static
199const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo(
200 const std::string& name) {
201 const InterfaceList* list = InterfaceList::GetInstance();
202 InterfaceList::NameToInfo::const_iterator found =
203 list->name_to_browser_info_.find(name);
204 if (found == list->name_to_browser_info_.end())
205 return NULL;
206 return found->second;
207}
208
209// static
210const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo(InterfaceID id) {
211 if (id <= 0 || id >= INTERFACE_ID_COUNT)
212 return NULL;
213 const InterfaceList* list = InterfaceList::GetInstance();
214 return list->id_to_browser_info_[id];
215}
216
217// static
218const InterfaceProxy::Info* Dispatcher::GetPPPInterfaceInfo(
219 const std::string& name) {
220 const InterfaceList* list = InterfaceList::GetInstance();
221 InterfaceList::NameToInfo::const_iterator found =
222 list->name_to_plugin_info_.find(name);
223 if (found == list->name_to_plugin_info_.end())
224 return NULL;
225 return found->second;
226}
227
228// static
229const InterfaceProxy::Info* Dispatcher::GetPPPInterfaceInfo(InterfaceID id) {
230 if (id <= 0 || id >= INTERFACE_ID_COUNT)
231 return NULL;
232 const InterfaceList* list = InterfaceList::GetInstance();
233 return list->id_to_plugin_info_[id];
234}
235
[email protected]c2932f5e2010-11-03 03:22:33236void Dispatcher::SetSerializationRules(
237 VarSerializationRules* var_serialization_rules) {
238 serialization_rules_.reset(var_serialization_rules);
239}
240
[email protected]c2932f5e2010-11-03 03:22:33241const void* Dispatcher::GetLocalInterface(const char* interface) {
242 return local_get_interface_(interface);
243}
244
[email protected]c2932f5e2010-11-03 03:22:33245bool Dispatcher::Send(IPC::Message* msg) {
[email protected]f24448db2011-01-27 20:40:39246 if (test_sink_)
247 return test_sink_->Send(msg);
[email protected]4f15d2842011-02-15 17:36:33248 if (channel_.get())
249 return channel_->Send(msg);
250
251 // Remote side crashed, drop this message.
252 delete msg;
253 return false;
[email protected]c2932f5e2010-11-03 03:22:33254}
255
[email protected]c2932f5e2010-11-03 03:22:33256} // namespace proxy
257} // namespace pp
258