[email protected] | b697e1a | 2011-01-06 22:20:28 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 2 | // 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] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame^] | 13 | #include "base/singleton.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 14 | #include "ipc/ipc_message.h" |
| 15 | #include "ipc/ipc_sync_channel.h" |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 16 | #include "ipc/ipc_test_sink.h" |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 17 | #include "ppapi/c/dev/ppb_buffer_dev.h" |
[email protected] | 9ca952d | 2010-11-11 20:43:50 | [diff] [blame] | 18 | #include "ppapi/c/dev/ppb_char_set_dev.h" |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 19 | #include "ppapi/c/dev/ppb_context_3d_dev.h" |
[email protected] | a301033 | 2010-11-12 07:09:35 | [diff] [blame] | 20 | #include "ppapi/c/dev/ppb_cursor_control_dev.h" |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 21 | #include "ppapi/c/dev/ppb_gles_chromium_texture_mapping_dev.h" |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 22 | #include "ppapi/c/dev/ppb_font_dev.h" |
| 23 | #include "ppapi/c/dev/ppb_fullscreen_dev.h" |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 24 | #include "ppapi/c/dev/ppb_opengles_dev.h" |
| 25 | #include "ppapi/c/dev/ppb_surface_3d_dev.h" |
[email protected] | 799d1ab | 2010-11-09 17:16:28 | [diff] [blame] | 26 | #include "ppapi/c/dev/ppb_testing_dev.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 27 | #include "ppapi/c/dev/ppb_var_deprecated.h" |
| 28 | #include "ppapi/c/pp_errors.h" |
[email protected] | b9a5984 | 2011-01-15 01:04:00 | [diff] [blame] | 29 | #include "ppapi/c/ppb_audio.h" |
| 30 | #include "ppapi/c/ppb_audio_config.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 31 | #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] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 35 | #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] | 9ca952d | 2010-11-11 20:43:50 | [diff] [blame] | 38 | #include "ppapi/c/ppp_instance.h" |
[email protected] | b697e1a | 2011-01-06 22:20:28 | [diff] [blame] | 39 | #include "ppapi/c/private/ppb_flash.h" |
[email protected] | 917a44b0 | 2011-01-13 17:07:29 | [diff] [blame] | 40 | #include "ppapi/c/private/ppb_pdf.h" |
[email protected] | 5f251761 | 2010-12-02 22:36:48 | [diff] [blame] | 41 | #include "ppapi/c/trusted/ppb_url_loader_trusted.h" |
[email protected] | 9ca952d | 2010-11-11 20:43:50 | [diff] [blame] | 42 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 43 | #include "ppapi/proxy/ppb_audio_config_proxy.h" |
| 44 | #include "ppapi/proxy/ppb_audio_proxy.h" |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 45 | #include "ppapi/proxy/ppb_buffer_proxy.h" |
[email protected] | 9ca952d | 2010-11-11 20:43:50 | [diff] [blame] | 46 | #include "ppapi/proxy/ppb_char_set_proxy.h" |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 47 | #include "ppapi/proxy/ppb_context_3d_proxy.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 48 | #include "ppapi/proxy/ppb_core_proxy.h" |
[email protected] | a301033 | 2010-11-12 07:09:35 | [diff] [blame] | 49 | #include "ppapi/proxy/ppb_cursor_control_proxy.h" |
[email protected] | 5f251761 | 2010-12-02 22:36:48 | [diff] [blame] | 50 | #include "ppapi/proxy/ppb_flash_proxy.h" |
[email protected] | 9ca952d | 2010-11-11 20:43:50 | [diff] [blame] | 51 | #include "ppapi/proxy/ppb_font_proxy.h" |
[email protected] | a301033 | 2010-11-12 07:09:35 | [diff] [blame] | 52 | #include "ppapi/proxy/ppb_fullscreen_proxy.h" |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 53 | #include "ppapi/proxy/ppb_gles_chromium_texture_mapping_proxy.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 54 | #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] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 57 | #include "ppapi/proxy/ppb_opengles2_proxy.h" |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 58 | #include "ppapi/proxy/ppb_pdf_proxy.h" |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 59 | #include "ppapi/proxy/ppb_surface_3d_proxy.h" |
[email protected] | 799d1ab | 2010-11-09 17:16:28 | [diff] [blame] | 60 | #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] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 64 | #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 | |
| 69 | namespace pp { |
| 70 | namespace proxy { |
| 71 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame^] | 72 | namespace { |
| 73 | |
| 74 | struct 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 | |
| 90 | InterfaceList::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 | |
| 125 | void 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 | |
| 135 | void 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 |
| 146 | InterfaceList* InterfaceList::GetInstance() { |
| 147 | return Singleton<InterfaceList>::get(); |
| 148 | } |
| 149 | |
| 150 | } // namespace |
| 151 | |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 152 | Dispatcher::Dispatcher(base::ProcessHandle remote_process_handle, |
| 153 | GetInterfaceFunc local_get_interface) |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 154 | : pp_module_(0), |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 155 | remote_process_handle_(remote_process_handle), |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 156 | test_sink_(NULL), |
[email protected] | 5f251761 | 2010-12-02 22:36:48 | [diff] [blame] | 157 | disallow_trusted_interfaces_(false), // TODO(brettw) make this settable. |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 158 | local_get_interface_(local_get_interface), |
[email protected] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 159 | callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | Dispatcher::~Dispatcher() { |
| 163 | } |
| 164 | |
| 165 | bool Dispatcher::InitWithChannel(MessageLoop* ipc_message_loop, |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 166 | const IPC::ChannelHandle& channel_handle, |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 167 | 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] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 171 | channel_.reset(new IPC::SyncChannel(channel_handle, mode, this, |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 172 | ipc_message_loop, false, shutdown_event)); |
| 173 | return true; |
| 174 | } |
| 175 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 176 | void Dispatcher::InitWithTestSink(IPC::TestSink* test_sink) { |
| 177 | DCHECK(!test_sink_); |
| 178 | test_sink_ = test_sink; |
| 179 | } |
| 180 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 181 | bool Dispatcher::OnMessageReceived(const IPC::Message& msg) { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 182 | // Control messages. |
| 183 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 184 | bool handled = true; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 185 | IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg) |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 186 | IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_, |
| 187 | CallbackTracker::ReceiveExecuteSerializedCallback) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 188 | IPC_MESSAGE_UNHANDLED(handled = false) |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 189 | IPC_END_MESSAGE_MAP() |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 190 | return handled; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 191 | } |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 192 | return false; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame^] | 195 | // static |
| 196 | const 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 |
| 207 | const 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 |
| 215 | const 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 |
| 226 | const 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] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 233 | void Dispatcher::SetSerializationRules( |
| 234 | VarSerializationRules* var_serialization_rules) { |
| 235 | serialization_rules_.reset(var_serialization_rules); |
| 236 | } |
| 237 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 238 | const void* Dispatcher::GetLocalInterface(const char* interface) { |
| 239 | return local_get_interface_(interface); |
| 240 | } |
| 241 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 242 | bool Dispatcher::Send(IPC::Message* msg) { |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 243 | if (test_sink_) |
| 244 | return test_sink_->Send(msg); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 245 | return channel_->Send(msg); |
| 246 | } |
| 247 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 248 | } // namespace proxy |
| 249 | } // namespace pp |
| 250 | |