[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" |
| 13 | #include "ipc/ipc_message.h" |
| 14 | #include "ipc/ipc_sync_channel.h" |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 15 | #include "ipc/ipc_test_sink.h" |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 16 | #include "ppapi/c/dev/ppb_buffer_dev.h" |
[email protected] | 9ca952d | 2010-11-11 20:43:50 | [diff] [blame] | 17 | #include "ppapi/c/dev/ppb_char_set_dev.h" |
[email protected] | a301033 | 2010-11-12 07:09:35 | [diff] [blame] | 18 | #include "ppapi/c/dev/ppb_cursor_control_dev.h" |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 19 | #include "ppapi/c/dev/ppb_cursor_control_dev.h" |
| 20 | #include "ppapi/c/dev/ppb_font_dev.h" |
| 21 | #include "ppapi/c/dev/ppb_fullscreen_dev.h" |
[email protected] | a301033 | 2010-11-12 07:09:35 | [diff] [blame] | 22 | #include "ppapi/c/dev/ppb_fullscreen_dev.h" |
[email protected] | 799d1ab | 2010-11-09 17:16:28 | [diff] [blame] | 23 | #include "ppapi/c/dev/ppb_testing_dev.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 24 | #include "ppapi/c/dev/ppb_var_deprecated.h" |
| 25 | #include "ppapi/c/pp_errors.h" |
[email protected] | b9a5984 | 2011-01-15 01:04:00 | [diff] [blame] | 26 | #include "ppapi/c/ppb_audio.h" |
| 27 | #include "ppapi/c/ppb_audio_config.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 28 | #include "ppapi/c/ppb_core.h" |
| 29 | #include "ppapi/c/ppb_graphics_2d.h" |
| 30 | #include "ppapi/c/ppb_image_data.h" |
| 31 | #include "ppapi/c/ppb_instance.h" |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 32 | #include "ppapi/c/ppb_url_loader.h" |
| 33 | #include "ppapi/c/ppb_url_request_info.h" |
| 34 | #include "ppapi/c/ppb_url_response_info.h" |
[email protected] | 9ca952d | 2010-11-11 20:43:50 | [diff] [blame] | 35 | #include "ppapi/c/ppp_instance.h" |
[email protected] | b697e1a | 2011-01-06 22:20:28 | [diff] [blame] | 36 | #include "ppapi/c/private/ppb_flash.h" |
[email protected] | 917a44b0 | 2011-01-13 17:07:29 | [diff] [blame] | 37 | #include "ppapi/c/private/ppb_pdf.h" |
[email protected] | 5f251761 | 2010-12-02 22:36:48 | [diff] [blame] | 38 | #include "ppapi/c/trusted/ppb_url_loader_trusted.h" |
[email protected] | 9ca952d | 2010-11-11 20:43:50 | [diff] [blame] | 39 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 40 | #include "ppapi/proxy/ppb_audio_config_proxy.h" |
| 41 | #include "ppapi/proxy/ppb_audio_proxy.h" |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 42 | #include "ppapi/proxy/ppb_buffer_proxy.h" |
[email protected] | 9ca952d | 2010-11-11 20:43:50 | [diff] [blame] | 43 | #include "ppapi/proxy/ppb_char_set_proxy.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 44 | #include "ppapi/proxy/ppb_core_proxy.h" |
[email protected] | a301033 | 2010-11-12 07:09:35 | [diff] [blame] | 45 | #include "ppapi/proxy/ppb_cursor_control_proxy.h" |
[email protected] | 5f251761 | 2010-12-02 22:36:48 | [diff] [blame] | 46 | #include "ppapi/proxy/ppb_flash_proxy.h" |
[email protected] | 9ca952d | 2010-11-11 20:43:50 | [diff] [blame] | 47 | #include "ppapi/proxy/ppb_font_proxy.h" |
[email protected] | a301033 | 2010-11-12 07:09:35 | [diff] [blame] | 48 | #include "ppapi/proxy/ppb_fullscreen_proxy.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 49 | #include "ppapi/proxy/ppb_graphics_2d_proxy.h" |
| 50 | #include "ppapi/proxy/ppb_image_data_proxy.h" |
| 51 | #include "ppapi/proxy/ppb_instance_proxy.h" |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 52 | #include "ppapi/proxy/ppb_pdf_proxy.h" |
[email protected] | 799d1ab | 2010-11-09 17:16:28 | [diff] [blame] | 53 | #include "ppapi/proxy/ppb_testing_proxy.h" |
| 54 | #include "ppapi/proxy/ppb_url_loader_proxy.h" |
| 55 | #include "ppapi/proxy/ppb_url_request_info_proxy.h" |
| 56 | #include "ppapi/proxy/ppb_url_response_info_proxy.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 57 | #include "ppapi/proxy/ppb_var_deprecated_proxy.h" |
| 58 | #include "ppapi/proxy/ppp_class_proxy.h" |
| 59 | #include "ppapi/proxy/ppp_instance_proxy.h" |
| 60 | #include "ppapi/proxy/var_serialization_rules.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 61 | |
| 62 | namespace pp { |
| 63 | namespace proxy { |
| 64 | |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 65 | Dispatcher::Dispatcher(base::ProcessHandle remote_process_handle, |
| 66 | GetInterfaceFunc local_get_interface) |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 67 | : pp_module_(0), |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 68 | remote_process_handle_(remote_process_handle), |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 69 | test_sink_(NULL), |
[email protected] | 5f251761 | 2010-12-02 22:36:48 | [diff] [blame] | 70 | disallow_trusted_interfaces_(false), // TODO(brettw) make this settable. |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 71 | local_get_interface_(local_get_interface), |
| 72 | declared_supported_remote_interfaces_(false), |
[email protected] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 73 | callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 74 | memset(id_to_proxy_, 0, |
| 75 | static_cast<int>(INTERFACE_ID_COUNT) * sizeof(InterfaceProxy*)); |
| 76 | } |
| 77 | |
| 78 | Dispatcher::~Dispatcher() { |
| 79 | } |
| 80 | |
| 81 | bool Dispatcher::InitWithChannel(MessageLoop* ipc_message_loop, |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 82 | const IPC::ChannelHandle& channel_handle, |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 83 | bool is_client, |
| 84 | base::WaitableEvent* shutdown_event) { |
| 85 | IPC::Channel::Mode mode = is_client ? IPC::Channel::MODE_CLIENT |
| 86 | : IPC::Channel::MODE_SERVER; |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 87 | channel_.reset(new IPC::SyncChannel(channel_handle, mode, this, |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 88 | ipc_message_loop, false, shutdown_event)); |
| 89 | return true; |
| 90 | } |
| 91 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 92 | void Dispatcher::InitWithTestSink(IPC::TestSink* test_sink) { |
| 93 | DCHECK(!test_sink_); |
| 94 | test_sink_ = test_sink; |
| 95 | } |
| 96 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 97 | bool Dispatcher::OnMessageReceived(const IPC::Message& msg) { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 98 | // Control messages. |
| 99 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 100 | bool handled = true; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 101 | IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg) |
| 102 | IPC_MESSAGE_HANDLER(PpapiMsg_DeclareInterfaces, |
| 103 | OnMsgDeclareInterfaces) |
| 104 | IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface) |
| 105 | IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_, |
| 106 | CallbackTracker::ReceiveExecuteSerializedCallback) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 107 | IPC_MESSAGE_UNHANDLED(handled = false) |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 108 | IPC_END_MESSAGE_MAP() |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 109 | return handled; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // Interface-specific messages. |
| 113 | if (msg.routing_id() > 0 && msg.routing_id() < INTERFACE_ID_COUNT) { |
| 114 | InterfaceProxy* proxy = id_to_proxy_[msg.routing_id()]; |
| 115 | if (proxy) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 116 | return proxy->OnMessageReceived(msg); |
| 117 | |
| 118 | NOTREACHED(); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 119 | // TODO(brettw): kill the plugin if it starts sending invalid messages? |
| 120 | } |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 121 | |
| 122 | return false; |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void Dispatcher::SetSerializationRules( |
| 126 | VarSerializationRules* var_serialization_rules) { |
| 127 | serialization_rules_.reset(var_serialization_rules); |
| 128 | } |
| 129 | |
| 130 | void Dispatcher::InjectProxy(InterfaceID id, |
| 131 | const std::string& name, |
| 132 | InterfaceProxy* proxy) { |
| 133 | proxies_[name] = linked_ptr<InterfaceProxy>(proxy); |
| 134 | id_to_proxy_[id] = proxy; |
| 135 | } |
| 136 | |
| 137 | const void* Dispatcher::GetLocalInterface(const char* interface) { |
| 138 | return local_get_interface_(interface); |
| 139 | } |
| 140 | |
| 141 | const void* Dispatcher::GetProxiedInterface(const std::string& interface) { |
| 142 | // See if we already know about this interface and have created a host. |
| 143 | ProxyMap::const_iterator found = proxies_.find(interface); |
| 144 | if (found != proxies_.end()) |
| 145 | return found->second->GetSourceInterface(); |
| 146 | |
| 147 | // When the remote side has sent us a declared list of all interfaces it |
| 148 | // supports and we don't have it in our list, we know the requested interface |
| 149 | // doesn't exist and we can return failure. |
| 150 | if (declared_supported_remote_interfaces_) |
| 151 | return NULL; |
| 152 | |
| 153 | if (!RemoteSupportsTargetInterface(interface)) |
| 154 | return NULL; |
| 155 | |
| 156 | linked_ptr<InterfaceProxy> proxy(CreateProxyForInterface(interface, NULL)); |
| 157 | if (!proxy.get()) |
| 158 | return NULL; // Don't know how to proxy this interface. |
| 159 | |
| 160 | // Save our proxy. |
| 161 | proxies_[interface] = proxy; |
| 162 | id_to_proxy_[proxy->GetInterfaceId()] = proxy.get(); |
| 163 | return proxy->GetSourceInterface(); |
| 164 | } |
| 165 | |
| 166 | bool Dispatcher::Send(IPC::Message* msg) { |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame^] | 167 | if (test_sink_) |
| 168 | return test_sink_->Send(msg); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 169 | return channel_->Send(msg); |
| 170 | } |
| 171 | |
| 172 | bool Dispatcher::RemoteSupportsTargetInterface(const std::string& interface) { |
| 173 | bool result = false; |
| 174 | Send(new PpapiMsg_SupportsInterface(interface, &result)); |
| 175 | return result; |
| 176 | } |
| 177 | |
| 178 | bool Dispatcher::IsInterfaceTrusted(const std::string& interface) { |
| 179 | // FIXME(brettw) |
| 180 | (void)interface; |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | bool Dispatcher::SetupProxyForTargetInterface(const std::string& interface) { |
| 185 | // If we already have a proxy that knows about the locally-implemented |
| 186 | // interface, we know it's supported and don't need to re-query. |
| 187 | ProxyMap::const_iterator found = proxies_.find(interface); |
| 188 | if (found != proxies_.end()) |
| 189 | return true; |
| 190 | |
| 191 | if (disallow_trusted_interfaces_ && IsInterfaceTrusted(interface)) |
| 192 | return false; |
| 193 | |
| 194 | // Create the proxy if it doesn't exist and set the local interface on it. |
| 195 | // This also handles the case where possibly an interface could be supported |
| 196 | // by both the local and remote side. |
| 197 | const void* interface_functions = local_get_interface_(interface.c_str()); |
| 198 | if (!interface_functions) |
| 199 | return false; |
| 200 | InterfaceProxy* proxy = CreateProxyForInterface(interface, |
| 201 | interface_functions); |
| 202 | if (!proxy) |
| 203 | return false; |
| 204 | |
| 205 | proxies_[interface] = linked_ptr<InterfaceProxy>(proxy); |
| 206 | id_to_proxy_[proxy->GetInterfaceId()] = proxy; |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | void Dispatcher::OnMsgSupportsInterface(const std::string& interface_name, |
| 211 | bool* result) { |
| 212 | *result = SetupProxyForTargetInterface(interface_name); |
| 213 | } |
| 214 | |
| 215 | void Dispatcher::OnMsgDeclareInterfaces( |
| 216 | const std::vector<std::string>& interfaces) { |
| 217 | // Make proxies for all the interfaces it supports that we also support. |
| 218 | for (size_t i = 0; i < interfaces.size(); i++) { |
| 219 | // Possibly the plugin could request an interface before the "declare" |
| 220 | // message is received, so we could already have an entry for this |
| 221 | // interface. In this case, we can just skip to the next one. |
| 222 | if (proxies_.find(interfaces[i]) != proxies_.end()) |
| 223 | continue; |
| 224 | |
| 225 | linked_ptr<InterfaceProxy> proxy(CreateProxyForInterface(interfaces[i], |
| 226 | NULL)); |
| 227 | if (!proxy.get()) { |
| 228 | // Since only the browser declares supported interfaces, we should never |
| 229 | // get one we don't support. |
| 230 | //NOTREACHED() << "Remote side declaring an unsupported proxy."; |
| 231 | continue; |
| 232 | } |
| 233 | proxies_[interfaces[i]] = proxy; |
| 234 | id_to_proxy_[proxy->GetInterfaceId()] = proxy.get(); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | InterfaceProxy* Dispatcher::CreateProxyForInterface( |
| 239 | const std::string& interface_name, |
| 240 | const void* interface_functions) { |
[email protected] | b9a5984 | 2011-01-15 01:04:00 | [diff] [blame] | 241 | if (interface_name == PPB_AUDIO_CONFIG_INTERFACE) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 242 | return new PPB_AudioConfig_Proxy(this, interface_functions); |
[email protected] | b9a5984 | 2011-01-15 01:04:00 | [diff] [blame] | 243 | if (interface_name == PPB_AUDIO_INTERFACE) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 244 | return new PPB_Audio_Proxy(this, interface_functions); |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 245 | if (interface_name == PPB_BUFFER_DEV_INTERFACE) |
| 246 | return new PPB_Buffer_Proxy(this, interface_functions); |
[email protected] | 9ca952d | 2010-11-11 20:43:50 | [diff] [blame] | 247 | if (interface_name == PPB_CHAR_SET_DEV_INTERFACE) |
| 248 | return new PPB_CharSet_Proxy(this, interface_functions); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 249 | if (interface_name == PPB_CORE_INTERFACE) |
| 250 | return new PPB_Core_Proxy(this, interface_functions); |
[email protected] | a301033 | 2010-11-12 07:09:35 | [diff] [blame] | 251 | if (interface_name == PPB_CURSOR_CONTROL_DEV_INTERFACE) |
| 252 | return new PPB_CursorControl_Proxy(this, interface_functions); |
[email protected] | 9ca952d | 2010-11-11 20:43:50 | [diff] [blame] | 253 | if (interface_name == PPB_FONT_DEV_INTERFACE) |
| 254 | return new PPB_Font_Proxy(this, interface_functions); |
[email protected] | a301033 | 2010-11-12 07:09:35 | [diff] [blame] | 255 | if (interface_name == PPB_FULLSCREEN_DEV_INTERFACE) |
| 256 | return new PPB_Fullscreen_Proxy(this, interface_functions); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 257 | if (interface_name == PPB_GRAPHICS_2D_INTERFACE) |
| 258 | return new PPB_Graphics2D_Proxy(this, interface_functions); |
| 259 | if (interface_name == PPB_IMAGEDATA_INTERFACE) |
| 260 | return new PPB_ImageData_Proxy(this, interface_functions); |
| 261 | if (interface_name == PPB_INSTANCE_INTERFACE) |
| 262 | return new PPB_Instance_Proxy(this, interface_functions); |
[email protected] | 799d1ab | 2010-11-09 17:16:28 | [diff] [blame] | 263 | if (interface_name == PPB_TESTING_DEV_INTERFACE) |
| 264 | return new PPB_Testing_Proxy(this, interface_functions); |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 265 | if (interface_name == PPB_URLLOADER_INTERFACE) |
[email protected] | 799d1ab | 2010-11-09 17:16:28 | [diff] [blame] | 266 | return new PPB_URLLoader_Proxy(this, interface_functions); |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 267 | if (interface_name == PPB_URLREQUESTINFO_INTERFACE) |
[email protected] | 799d1ab | 2010-11-09 17:16:28 | [diff] [blame] | 268 | return new PPB_URLRequestInfo_Proxy(this, interface_functions); |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 269 | if (interface_name == PPB_URLRESPONSEINFO_INTERFACE) |
[email protected] | 799d1ab | 2010-11-09 17:16:28 | [diff] [blame] | 270 | return new PPB_URLResponseInfo_Proxy(this, interface_functions); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 271 | if (interface_name == PPB_VAR_DEPRECATED_INTERFACE) |
| 272 | return new PPB_Var_Deprecated_Proxy(this, interface_functions); |
| 273 | if (interface_name == PPP_INSTANCE_INTERFACE) |
| 274 | return new PPP_Instance_Proxy(this, interface_functions); |
| 275 | |
[email protected] | 5f251761 | 2010-12-02 22:36:48 | [diff] [blame] | 276 | // Trusted interfaces. |
| 277 | if (!disallow_trusted_interfaces_) { |
[email protected] | 0bd75368 | 2010-12-16 18:15:52 | [diff] [blame] | 278 | if (interface_name == PPB_FLASH_INTERFACE) |
[email protected] | 5f251761 | 2010-12-02 22:36:48 | [diff] [blame] | 279 | return new PPB_Flash_Proxy(this, interface_functions); |
[email protected] | 0bd75368 | 2010-12-16 18:15:52 | [diff] [blame] | 280 | if (interface_name == PPB_PDF_INTERFACE) |
| 281 | return new PPB_PDF_Proxy(this, interface_functions); |
[email protected] | 5f251761 | 2010-12-02 22:36:48 | [diff] [blame] | 282 | if (interface_name == PPB_URLLOADERTRUSTED_INTERFACE) |
| 283 | return new PPB_URLLoaderTrusted_Proxy(this, interface_functions); |
| 284 | } |
| 285 | |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 286 | return NULL; |
| 287 | } |
| 288 | |
| 289 | } // namespace proxy |
| 290 | } // namespace pp |
| 291 | |