[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] | 7358d57 | 2011-02-15 18:44:40 | [diff] [blame] | 40 | #include "ppapi/c/private/ppb_flash_menu.h" |
[email protected] | 917a44b0 | 2011-01-13 17:07:29 | [diff] [blame] | 41 | #include "ppapi/c/private/ppb_pdf.h" |
[email protected] | 5f251761 | 2010-12-02 22:36:48 | [diff] [blame] | 42 | #include "ppapi/c/trusted/ppb_url_loader_trusted.h" |
[email protected] | 9ca952d | 2010-11-11 20:43:50 | [diff] [blame] | 43 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 44 | #include "ppapi/proxy/ppb_audio_config_proxy.h" |
| 45 | #include "ppapi/proxy/ppb_audio_proxy.h" |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 46 | #include "ppapi/proxy/ppb_buffer_proxy.h" |
[email protected] | 9ca952d | 2010-11-11 20:43:50 | [diff] [blame] | 47 | #include "ppapi/proxy/ppb_char_set_proxy.h" |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 48 | #include "ppapi/proxy/ppb_context_3d_proxy.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 49 | #include "ppapi/proxy/ppb_core_proxy.h" |
[email protected] | a301033 | 2010-11-12 07:09:35 | [diff] [blame] | 50 | #include "ppapi/proxy/ppb_cursor_control_proxy.h" |
[email protected] | 4deeb43 | 2011-02-17 23:59:39 | [diff] [blame^] | 51 | #include "ppapi/proxy/ppb_file_chooser_proxy.h" |
| 52 | #include "ppapi/proxy/ppb_file_ref_proxy.h" |
[email protected] | 5f251761 | 2010-12-02 22:36:48 | [diff] [blame] | 53 | #include "ppapi/proxy/ppb_flash_proxy.h" |
[email protected] | 7358d57 | 2011-02-15 18:44:40 | [diff] [blame] | 54 | #include "ppapi/proxy/ppb_flash_menu_proxy.h" |
[email protected] | 9ca952d | 2010-11-11 20:43:50 | [diff] [blame] | 55 | #include "ppapi/proxy/ppb_font_proxy.h" |
[email protected] | a301033 | 2010-11-12 07:09:35 | [diff] [blame] | 56 | #include "ppapi/proxy/ppb_fullscreen_proxy.h" |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 57 | #include "ppapi/proxy/ppb_gles_chromium_texture_mapping_proxy.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 58 | #include "ppapi/proxy/ppb_graphics_2d_proxy.h" |
| 59 | #include "ppapi/proxy/ppb_image_data_proxy.h" |
| 60 | #include "ppapi/proxy/ppb_instance_proxy.h" |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 61 | #include "ppapi/proxy/ppb_opengles2_proxy.h" |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 62 | #include "ppapi/proxy/ppb_pdf_proxy.h" |
[email protected] | f56279c | 2011-02-02 18:12:31 | [diff] [blame] | 63 | #include "ppapi/proxy/ppb_surface_3d_proxy.h" |
[email protected] | 799d1ab | 2010-11-09 17:16:28 | [diff] [blame] | 64 | #include "ppapi/proxy/ppb_testing_proxy.h" |
| 65 | #include "ppapi/proxy/ppb_url_loader_proxy.h" |
| 66 | #include "ppapi/proxy/ppb_url_request_info_proxy.h" |
| 67 | #include "ppapi/proxy/ppb_url_response_info_proxy.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 68 | #include "ppapi/proxy/ppb_var_deprecated_proxy.h" |
| 69 | #include "ppapi/proxy/ppp_class_proxy.h" |
| 70 | #include "ppapi/proxy/ppp_instance_proxy.h" |
| 71 | #include "ppapi/proxy/var_serialization_rules.h" |
| 72 | |
| 73 | namespace pp { |
| 74 | namespace proxy { |
| 75 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 76 | namespace { |
| 77 | |
| 78 | struct InterfaceList { |
| 79 | InterfaceList(); |
| 80 | |
| 81 | static InterfaceList* GetInstance(); |
| 82 | |
| 83 | void AddPPP(const InterfaceProxy::Info* info); |
| 84 | void AddPPB(const InterfaceProxy::Info* info); |
| 85 | |
| 86 | typedef std::map<std::string, const InterfaceProxy::Info*> NameToInfo; |
| 87 | NameToInfo name_to_plugin_info_; |
| 88 | NameToInfo name_to_browser_info_; |
| 89 | |
| 90 | const InterfaceProxy::Info* id_to_plugin_info_[INTERFACE_ID_COUNT]; |
| 91 | const InterfaceProxy::Info* id_to_browser_info_[INTERFACE_ID_COUNT]; |
| 92 | }; |
| 93 | |
| 94 | InterfaceList::InterfaceList() { |
| 95 | memset(id_to_plugin_info_, 0, |
| 96 | static_cast<int>(INTERFACE_ID_COUNT) * sizeof(InterfaceID)); |
| 97 | memset(id_to_browser_info_, 0, |
| 98 | static_cast<int>(INTERFACE_ID_COUNT) * sizeof(InterfaceID)); |
| 99 | |
| 100 | // PPB (browser) interfaces. |
| 101 | AddPPB(PPB_AudioConfig_Proxy::GetInfo()); |
| 102 | AddPPB(PPB_Audio_Proxy::GetInfo()); |
| 103 | AddPPB(PPB_Buffer_Proxy::GetInfo()); |
| 104 | AddPPB(PPB_CharSet_Proxy::GetInfo()); |
| 105 | AddPPB(PPB_Context3D_Proxy::GetInfo()); |
| 106 | AddPPB(PPB_Core_Proxy::GetInfo()); |
| 107 | AddPPB(PPB_CursorControl_Proxy::GetInfo()); |
[email protected] | 4deeb43 | 2011-02-17 23:59:39 | [diff] [blame^] | 108 | AddPPB(PPB_FileChooser_Proxy::GetInfo()); |
| 109 | AddPPB(PPB_FileRef_Proxy::GetInfo()); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 110 | AddPPB(PPB_Flash_Proxy::GetInfo()); |
[email protected] | 7358d57 | 2011-02-15 18:44:40 | [diff] [blame] | 111 | AddPPB(PPB_Flash_Menu_Proxy::GetInfo()); |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 112 | AddPPB(PPB_Font_Proxy::GetInfo()); |
| 113 | AddPPB(PPB_Fullscreen_Proxy::GetInfo()); |
| 114 | AddPPB(PPB_GLESChromiumTextureMapping_Proxy::GetInfo()); |
| 115 | AddPPB(PPB_Graphics2D_Proxy::GetInfo()); |
| 116 | AddPPB(PPB_ImageData_Proxy::GetInfo()); |
| 117 | AddPPB(PPB_Instance_Proxy::GetInfo()); |
| 118 | AddPPB(PPB_OpenGLES2_Proxy::GetInfo()); |
| 119 | AddPPB(PPB_PDF_Proxy::GetInfo()); |
| 120 | AddPPB(PPB_Surface3D_Proxy::GetInfo()); |
| 121 | AddPPB(PPB_Testing_Proxy::GetInfo()); |
| 122 | AddPPB(PPB_URLLoader_Proxy::GetInfo()); |
| 123 | AddPPB(PPB_URLLoaderTrusted_Proxy::GetInfo()); |
| 124 | AddPPB(PPB_URLRequestInfo_Proxy::GetInfo()); |
| 125 | AddPPB(PPB_URLResponseInfo_Proxy::GetInfo()); |
| 126 | AddPPB(PPB_Var_Deprecated_Proxy::GetInfo()); |
| 127 | |
| 128 | // PPP (plugin) interfaces. |
| 129 | AddPPP(PPP_Instance_Proxy::GetInfo()); |
| 130 | } |
| 131 | |
| 132 | void InterfaceList::AddPPP(const InterfaceProxy::Info* info) { |
| 133 | DCHECK(name_to_plugin_info_.find(info->name) == |
| 134 | name_to_plugin_info_.end()); |
| 135 | DCHECK(info->id > 0 && info->id < INTERFACE_ID_COUNT); |
| 136 | DCHECK(id_to_plugin_info_[info->id] == NULL); |
| 137 | |
| 138 | name_to_plugin_info_[info->name] = info; |
| 139 | id_to_plugin_info_[info->id] = info; |
| 140 | } |
| 141 | |
| 142 | void InterfaceList::AddPPB(const InterfaceProxy::Info* info) { |
| 143 | DCHECK(name_to_browser_info_.find(info->name) == |
| 144 | name_to_browser_info_.end()); |
| 145 | DCHECK(info->id > 0 && info->id < INTERFACE_ID_COUNT); |
| 146 | DCHECK(id_to_browser_info_[info->id] == NULL); |
| 147 | |
| 148 | name_to_browser_info_[std::string(info->name)] = info; |
| 149 | id_to_browser_info_[info->id] = info; |
| 150 | } |
| 151 | |
| 152 | // static |
| 153 | InterfaceList* InterfaceList::GetInstance() { |
| 154 | return Singleton<InterfaceList>::get(); |
| 155 | } |
| 156 | |
| 157 | } // namespace |
| 158 | |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 159 | Dispatcher::Dispatcher(base::ProcessHandle remote_process_handle, |
| 160 | GetInterfaceFunc local_get_interface) |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 161 | : remote_process_handle_(remote_process_handle), |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 162 | test_sink_(NULL), |
[email protected] | 5f251761 | 2010-12-02 22:36:48 | [diff] [blame] | 163 | disallow_trusted_interfaces_(false), // TODO(brettw) make this settable. |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 164 | local_get_interface_(local_get_interface), |
[email protected] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 165 | callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | Dispatcher::~Dispatcher() { |
| 169 | } |
| 170 | |
| 171 | bool Dispatcher::InitWithChannel(MessageLoop* ipc_message_loop, |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 172 | const IPC::ChannelHandle& channel_handle, |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 173 | bool is_client, |
| 174 | base::WaitableEvent* shutdown_event) { |
| 175 | IPC::Channel::Mode mode = is_client ? IPC::Channel::MODE_CLIENT |
| 176 | : IPC::Channel::MODE_SERVER; |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 177 | channel_.reset(new IPC::SyncChannel(channel_handle, mode, this, |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 178 | ipc_message_loop, false, shutdown_event)); |
| 179 | return true; |
| 180 | } |
| 181 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 182 | void Dispatcher::InitWithTestSink(IPC::TestSink* test_sink) { |
| 183 | DCHECK(!test_sink_); |
| 184 | test_sink_ = test_sink; |
| 185 | } |
| 186 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 187 | bool Dispatcher::OnMessageReceived(const IPC::Message& msg) { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 188 | // Control messages. |
| 189 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 190 | bool handled = true; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 191 | IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg) |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 192 | IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_, |
| 193 | CallbackTracker::ReceiveExecuteSerializedCallback) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 194 | IPC_MESSAGE_UNHANDLED(handled = false) |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 195 | IPC_END_MESSAGE_MAP() |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 196 | return handled; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 197 | } |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 198 | return false; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 199 | } |
| 200 | |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 201 | void Dispatcher::OnChannelError() { |
| 202 | channel_.reset(); |
| 203 | } |
| 204 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 205 | // static |
| 206 | const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo( |
| 207 | const std::string& name) { |
| 208 | const InterfaceList* list = InterfaceList::GetInstance(); |
| 209 | InterfaceList::NameToInfo::const_iterator found = |
| 210 | list->name_to_browser_info_.find(name); |
| 211 | if (found == list->name_to_browser_info_.end()) |
| 212 | return NULL; |
| 213 | return found->second; |
| 214 | } |
| 215 | |
| 216 | // static |
| 217 | const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo(InterfaceID id) { |
| 218 | if (id <= 0 || id >= INTERFACE_ID_COUNT) |
| 219 | return NULL; |
| 220 | const InterfaceList* list = InterfaceList::GetInstance(); |
| 221 | return list->id_to_browser_info_[id]; |
| 222 | } |
| 223 | |
| 224 | // static |
| 225 | const InterfaceProxy::Info* Dispatcher::GetPPPInterfaceInfo( |
| 226 | const std::string& name) { |
| 227 | const InterfaceList* list = InterfaceList::GetInstance(); |
| 228 | InterfaceList::NameToInfo::const_iterator found = |
| 229 | list->name_to_plugin_info_.find(name); |
| 230 | if (found == list->name_to_plugin_info_.end()) |
| 231 | return NULL; |
| 232 | return found->second; |
| 233 | } |
| 234 | |
| 235 | // static |
| 236 | const InterfaceProxy::Info* Dispatcher::GetPPPInterfaceInfo(InterfaceID id) { |
| 237 | if (id <= 0 || id >= INTERFACE_ID_COUNT) |
| 238 | return NULL; |
| 239 | const InterfaceList* list = InterfaceList::GetInstance(); |
| 240 | return list->id_to_plugin_info_[id]; |
| 241 | } |
| 242 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 243 | void Dispatcher::SetSerializationRules( |
| 244 | VarSerializationRules* var_serialization_rules) { |
| 245 | serialization_rules_.reset(var_serialization_rules); |
| 246 | } |
| 247 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 248 | const void* Dispatcher::GetLocalInterface(const char* interface) { |
| 249 | return local_get_interface_(interface); |
| 250 | } |
| 251 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 252 | bool Dispatcher::Send(IPC::Message* msg) { |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 253 | if (test_sink_) |
| 254 | return test_sink_->Send(msg); |
[email protected] | 4f15d284 | 2011-02-15 17:36:33 | [diff] [blame] | 255 | if (channel_.get()) |
| 256 | return channel_->Send(msg); |
| 257 | |
| 258 | // Remote side crashed, drop this message. |
| 259 | delete msg; |
| 260 | return false; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 261 | } |
| 262 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 263 | } // namespace proxy |
| 264 | } // namespace pp |
| 265 | |