OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ppapi/proxy/dispatcher.h" | 5 #include "ppapi/proxy/dispatcher.h" |
6 | 6 |
7 #include <string.h> // For memset. | 7 #include <string.h> // For memset. |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/singleton.h" |
13 #include "ipc/ipc_message.h" | 14 #include "ipc/ipc_message.h" |
14 #include "ipc/ipc_sync_channel.h" | 15 #include "ipc/ipc_sync_channel.h" |
15 #include "ipc/ipc_test_sink.h" | 16 #include "ipc/ipc_test_sink.h" |
16 #include "ppapi/c/dev/ppb_buffer_dev.h" | 17 #include "ppapi/c/dev/ppb_buffer_dev.h" |
17 #include "ppapi/c/dev/ppb_char_set_dev.h" | 18 #include "ppapi/c/dev/ppb_char_set_dev.h" |
18 #include "ppapi/c/dev/ppb_context_3d_dev.h" | 19 #include "ppapi/c/dev/ppb_context_3d_dev.h" |
19 #include "ppapi/c/dev/ppb_cursor_control_dev.h" | 20 #include "ppapi/c/dev/ppb_cursor_control_dev.h" |
20 #include "ppapi/c/dev/ppb_gles_chromium_texture_mapping_dev.h" | 21 #include "ppapi/c/dev/ppb_gles_chromium_texture_mapping_dev.h" |
21 #include "ppapi/c/dev/ppb_font_dev.h" | 22 #include "ppapi/c/dev/ppb_font_dev.h" |
22 #include "ppapi/c/dev/ppb_fullscreen_dev.h" | 23 #include "ppapi/c/dev/ppb_fullscreen_dev.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 #include "ppapi/proxy/ppb_url_request_info_proxy.h" | 62 #include "ppapi/proxy/ppb_url_request_info_proxy.h" |
62 #include "ppapi/proxy/ppb_url_response_info_proxy.h" | 63 #include "ppapi/proxy/ppb_url_response_info_proxy.h" |
63 #include "ppapi/proxy/ppb_var_deprecated_proxy.h" | 64 #include "ppapi/proxy/ppb_var_deprecated_proxy.h" |
64 #include "ppapi/proxy/ppp_class_proxy.h" | 65 #include "ppapi/proxy/ppp_class_proxy.h" |
65 #include "ppapi/proxy/ppp_instance_proxy.h" | 66 #include "ppapi/proxy/ppp_instance_proxy.h" |
66 #include "ppapi/proxy/var_serialization_rules.h" | 67 #include "ppapi/proxy/var_serialization_rules.h" |
67 | 68 |
68 namespace pp { | 69 namespace pp { |
69 namespace proxy { | 70 namespace proxy { |
70 | 71 |
| 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 |
71 Dispatcher::Dispatcher(base::ProcessHandle remote_process_handle, | 152 Dispatcher::Dispatcher(base::ProcessHandle remote_process_handle, |
72 GetInterfaceFunc local_get_interface) | 153 GetInterfaceFunc local_get_interface) |
73 : pp_module_(0), | 154 : pp_module_(0), |
74 remote_process_handle_(remote_process_handle), | 155 remote_process_handle_(remote_process_handle), |
75 test_sink_(NULL), | 156 test_sink_(NULL), |
76 disallow_trusted_interfaces_(false), // TODO(brettw) make this settable. | 157 disallow_trusted_interfaces_(false), // TODO(brettw) make this settable. |
77 local_get_interface_(local_get_interface), | 158 local_get_interface_(local_get_interface), |
78 declared_supported_remote_interfaces_(false), | |
79 callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 159 callback_tracker_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
80 memset(id_to_proxy_, 0, | |
81 static_cast<int>(INTERFACE_ID_COUNT) * sizeof(InterfaceProxy*)); | |
82 } | 160 } |
83 | 161 |
84 Dispatcher::~Dispatcher() { | 162 Dispatcher::~Dispatcher() { |
85 } | 163 } |
86 | 164 |
87 bool Dispatcher::InitWithChannel(MessageLoop* ipc_message_loop, | 165 bool Dispatcher::InitWithChannel(MessageLoop* ipc_message_loop, |
88 const IPC::ChannelHandle& channel_handle, | 166 const IPC::ChannelHandle& channel_handle, |
89 bool is_client, | 167 bool is_client, |
90 base::WaitableEvent* shutdown_event) { | 168 base::WaitableEvent* shutdown_event) { |
91 IPC::Channel::Mode mode = is_client ? IPC::Channel::MODE_CLIENT | 169 IPC::Channel::Mode mode = is_client ? IPC::Channel::MODE_CLIENT |
92 : IPC::Channel::MODE_SERVER; | 170 : IPC::Channel::MODE_SERVER; |
93 channel_.reset(new IPC::SyncChannel(channel_handle, mode, this, | 171 channel_.reset(new IPC::SyncChannel(channel_handle, mode, this, |
94 ipc_message_loop, false, shutdown_event)); | 172 ipc_message_loop, false, shutdown_event)); |
95 return true; | 173 return true; |
96 } | 174 } |
97 | 175 |
98 void Dispatcher::InitWithTestSink(IPC::TestSink* test_sink) { | 176 void Dispatcher::InitWithTestSink(IPC::TestSink* test_sink) { |
99 DCHECK(!test_sink_); | 177 DCHECK(!test_sink_); |
100 test_sink_ = test_sink; | 178 test_sink_ = test_sink; |
101 } | 179 } |
102 | 180 |
103 bool Dispatcher::OnMessageReceived(const IPC::Message& msg) { | 181 bool Dispatcher::OnMessageReceived(const IPC::Message& msg) { |
104 // Control messages. | 182 // Control messages. |
105 if (msg.routing_id() == MSG_ROUTING_CONTROL) { | 183 if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
106 bool handled = true; | 184 bool handled = true; |
107 IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg) | 185 IPC_BEGIN_MESSAGE_MAP(Dispatcher, msg) |
108 IPC_MESSAGE_HANDLER(PpapiMsg_DeclareInterfaces, | |
109 OnMsgDeclareInterfaces) | |
110 IPC_MESSAGE_HANDLER(PpapiMsg_SupportsInterface, OnMsgSupportsInterface) | |
111 IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_, | 186 IPC_MESSAGE_FORWARD(PpapiMsg_ExecuteCallback, &callback_tracker_, |
112 CallbackTracker::ReceiveExecuteSerializedCallback) | 187 CallbackTracker::ReceiveExecuteSerializedCallback) |
113 IPC_MESSAGE_UNHANDLED(handled = false) | 188 IPC_MESSAGE_UNHANDLED(handled = false) |
114 IPC_END_MESSAGE_MAP() | 189 IPC_END_MESSAGE_MAP() |
115 return handled; | 190 return handled; |
116 } | 191 } |
| 192 return false; |
| 193 } |
117 | 194 |
118 // Interface-specific messages. | 195 // static |
119 if (msg.routing_id() > 0 && msg.routing_id() < INTERFACE_ID_COUNT) { | 196 const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo( |
120 InterfaceProxy* proxy = id_to_proxy_[msg.routing_id()]; | 197 const std::string& name) { |
121 if (proxy) | 198 const InterfaceList* list = InterfaceList::GetInstance(); |
122 return proxy->OnMessageReceived(msg); | 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 } |
123 | 205 |
124 NOTREACHED(); | 206 // static |
125 // TODO(brettw): kill the plugin if it starts sending invalid messages? | 207 const InterfaceProxy::Info* Dispatcher::GetPPBInterfaceInfo(InterfaceID id) { |
126 } | 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 } |
127 | 213 |
128 return false; | 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]; |
129 } | 231 } |
130 | 232 |
131 void Dispatcher::SetSerializationRules( | 233 void Dispatcher::SetSerializationRules( |
132 VarSerializationRules* var_serialization_rules) { | 234 VarSerializationRules* var_serialization_rules) { |
133 serialization_rules_.reset(var_serialization_rules); | 235 serialization_rules_.reset(var_serialization_rules); |
134 } | 236 } |
135 | 237 |
136 void Dispatcher::InjectProxy(InterfaceID id, | |
137 const std::string& name, | |
138 InterfaceProxy* proxy) { | |
139 proxies_[name] = linked_ptr<InterfaceProxy>(proxy); | |
140 id_to_proxy_[id] = proxy; | |
141 } | |
142 | |
143 const void* Dispatcher::GetLocalInterface(const char* interface) { | 238 const void* Dispatcher::GetLocalInterface(const char* interface) { |
144 return local_get_interface_(interface); | 239 return local_get_interface_(interface); |
145 } | 240 } |
146 | 241 |
147 const void* Dispatcher::GetProxiedInterface(const std::string& interface) { | |
148 // See if we already know about this interface and have created a host. | |
149 ProxyMap::const_iterator found = proxies_.find(interface); | |
150 if (found != proxies_.end()) | |
151 return found->second->GetSourceInterface(); | |
152 | |
153 // When the remote side has sent us a declared list of all interfaces it | |
154 // supports and we don't have it in our list, we know the requested interface | |
155 // doesn't exist and we can return failure. | |
156 if (declared_supported_remote_interfaces_) | |
157 return NULL; | |
158 | |
159 if (!RemoteSupportsTargetInterface(interface)) | |
160 return NULL; | |
161 | |
162 linked_ptr<InterfaceProxy> proxy(CreateProxyForInterface(interface, NULL)); | |
163 if (!proxy.get()) | |
164 return NULL; // Don't know how to proxy this interface. | |
165 | |
166 // Save our proxy. | |
167 proxies_[interface] = proxy; | |
168 InterfaceID id = proxy->GetInterfaceId(); | |
169 if (id != INTERFACE_ID_NONE) | |
170 id_to_proxy_[id] = proxy.get(); | |
171 return proxy->GetSourceInterface(); | |
172 } | |
173 | |
174 bool Dispatcher::Send(IPC::Message* msg) { | 242 bool Dispatcher::Send(IPC::Message* msg) { |
175 if (test_sink_) | 243 if (test_sink_) |
176 return test_sink_->Send(msg); | 244 return test_sink_->Send(msg); |
177 return channel_->Send(msg); | 245 return channel_->Send(msg); |
178 } | 246 } |
179 | 247 |
180 bool Dispatcher::RemoteSupportsTargetInterface(const std::string& interface) { | |
181 bool result = false; | |
182 Send(new PpapiMsg_SupportsInterface(interface, &result)); | |
183 return result; | |
184 } | |
185 | |
186 bool Dispatcher::IsInterfaceTrusted(const std::string& interface) { | |
187 // FIXME(brettw) | |
188 (void)interface; | |
189 return false; | |
190 } | |
191 | |
192 bool Dispatcher::SetupProxyForTargetInterface(const std::string& interface) { | |
193 // If we already have a proxy that knows about the locally-implemented | |
194 // interface, we know it's supported and don't need to re-query. | |
195 ProxyMap::const_iterator found = proxies_.find(interface); | |
196 if (found != proxies_.end()) | |
197 return true; | |
198 | |
199 if (disallow_trusted_interfaces_ && IsInterfaceTrusted(interface)) | |
200 return false; | |
201 | |
202 // Create the proxy if it doesn't exist and set the local interface on it. | |
203 // This also handles the case where possibly an interface could be supported | |
204 // by both the local and remote side. | |
205 const void* interface_functions = local_get_interface_(interface.c_str()); | |
206 if (!interface_functions) | |
207 return false; | |
208 InterfaceProxy* proxy = CreateProxyForInterface(interface, | |
209 interface_functions); | |
210 if (!proxy) | |
211 return false; | |
212 | |
213 proxies_[interface] = linked_ptr<InterfaceProxy>(proxy); | |
214 id_to_proxy_[proxy->GetInterfaceId()] = proxy; | |
215 return true; | |
216 } | |
217 | |
218 void Dispatcher::OnMsgSupportsInterface(const std::string& interface_name, | |
219 bool* result) { | |
220 *result = SetupProxyForTargetInterface(interface_name); | |
221 } | |
222 | |
223 void Dispatcher::OnMsgDeclareInterfaces( | |
224 const std::vector<std::string>& interfaces) { | |
225 // Make proxies for all the interfaces it supports that we also support. | |
226 for (size_t i = 0; i < interfaces.size(); i++) { | |
227 // Possibly the plugin could request an interface before the "declare" | |
228 // message is received, so we could already have an entry for this | |
229 // interface. In this case, we can just skip to the next one. | |
230 if (proxies_.find(interfaces[i]) != proxies_.end()) | |
231 continue; | |
232 | |
233 linked_ptr<InterfaceProxy> proxy(CreateProxyForInterface(interfaces[i], | |
234 NULL)); | |
235 if (!proxy.get()) { | |
236 // Since only the browser declares supported interfaces, we should never | |
237 // get one we don't support. | |
238 //NOTREACHED() << "Remote side declaring an unsupported proxy."; | |
239 continue; | |
240 } | |
241 proxies_[interfaces[i]] = proxy; | |
242 id_to_proxy_[proxy->GetInterfaceId()] = proxy.get(); | |
243 } | |
244 } | |
245 | |
246 InterfaceProxy* Dispatcher::CreateProxyForInterface( | |
247 const std::string& interface_name, | |
248 const void* interface_functions) { | |
249 if (interface_name == PPB_AUDIO_CONFIG_INTERFACE) | |
250 return new PPB_AudioConfig_Proxy(this, interface_functions); | |
251 if (interface_name == PPB_AUDIO_INTERFACE) | |
252 return new PPB_Audio_Proxy(this, interface_functions); | |
253 if (interface_name == PPB_BUFFER_DEV_INTERFACE) | |
254 return new PPB_Buffer_Proxy(this, interface_functions); | |
255 if (interface_name == PPB_CHAR_SET_DEV_INTERFACE) | |
256 return new PPB_CharSet_Proxy(this, interface_functions); | |
257 if (interface_name == PPB_CONTEXT_3D_DEV_INTERFACE) | |
258 return new PPB_Context3D_Proxy(this, interface_functions); | |
259 if (interface_name == PPB_CORE_INTERFACE) | |
260 return new PPB_Core_Proxy(this, interface_functions); | |
261 if (interface_name == PPB_CURSOR_CONTROL_DEV_INTERFACE) | |
262 return new PPB_CursorControl_Proxy(this, interface_functions); | |
263 if (interface_name == PPB_FONT_DEV_INTERFACE) | |
264 return new PPB_Font_Proxy(this, interface_functions); | |
265 if (interface_name == PPB_FULLSCREEN_DEV_INTERFACE) | |
266 return new PPB_Fullscreen_Proxy(this, interface_functions); | |
267 if (interface_name == PPB_GLES_CHROMIUM_TEXTURE_MAPPING_DEV_INTERFACE) | |
268 return new PPB_GLESChromiumTextureMapping_Proxy(this, interface_functions); | |
269 if (interface_name == PPB_GRAPHICS_2D_INTERFACE) | |
270 return new PPB_Graphics2D_Proxy(this, interface_functions); | |
271 if (interface_name == PPB_IMAGEDATA_INTERFACE) | |
272 return new PPB_ImageData_Proxy(this, interface_functions); | |
273 if (interface_name == PPB_INSTANCE_INTERFACE) | |
274 return new PPB_Instance_Proxy(this, interface_functions); | |
275 if (interface_name == PPB_OPENGLES2_DEV_INTERFACE) | |
276 return new PPB_OpenGLES2_Proxy(this, interface_functions); | |
277 if (interface_name == PPB_SURFACE_3D_DEV_INTERFACE) | |
278 return new PPB_Surface3D_Proxy(this, interface_functions); | |
279 if (interface_name == PPB_TESTING_DEV_INTERFACE) | |
280 return new PPB_Testing_Proxy(this, interface_functions); | |
281 if (interface_name == PPB_URLLOADER_INTERFACE) | |
282 return new PPB_URLLoader_Proxy(this, interface_functions); | |
283 if (interface_name == PPB_URLREQUESTINFO_INTERFACE) | |
284 return new PPB_URLRequestInfo_Proxy(this, interface_functions); | |
285 if (interface_name == PPB_URLRESPONSEINFO_INTERFACE) | |
286 return new PPB_URLResponseInfo_Proxy(this, interface_functions); | |
287 if (interface_name == PPB_VAR_DEPRECATED_INTERFACE) | |
288 return new PPB_Var_Deprecated_Proxy(this, interface_functions); | |
289 if (interface_name == PPP_INSTANCE_INTERFACE) | |
290 return new PPP_Instance_Proxy(this, interface_functions); | |
291 | |
292 // Trusted interfaces. | |
293 if (!disallow_trusted_interfaces_) { | |
294 if (interface_name == PPB_FLASH_INTERFACE) | |
295 return new PPB_Flash_Proxy(this, interface_functions); | |
296 if (interface_name == PPB_PDF_INTERFACE) | |
297 return new PPB_PDF_Proxy(this, interface_functions); | |
298 if (interface_name == PPB_URLLOADERTRUSTED_INTERFACE) | |
299 return new PPB_URLLoaderTrusted_Proxy(this, interface_functions); | |
300 } | |
301 | |
302 return NULL; | |
303 } | |
304 | |
305 } // namespace proxy | 248 } // namespace proxy |
306 } // namespace pp | 249 } // namespace pp |
307 | 250 |
OLD | NEW |