1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ppapi/proxy/interface_list.h"
7 #include "base/lazy_instance.h"
8 #include "base/memory/singleton.h"
9 #include "ppapi/c/dev/ppb_audio_input_dev.h"
10 #include "ppapi/c/dev/ppb_buffer_dev.h"
11 #include "ppapi/c/dev/ppb_char_set_dev.h"
12 #include "ppapi/c/dev/ppb_console_dev.h"
13 #include "ppapi/c/dev/ppb_crypto_dev.h"
14 #include "ppapi/c/dev/ppb_cursor_control_dev.h"
15 #include "ppapi/c/dev/ppb_device_ref_dev.h"
16 #include "ppapi/c/dev/ppb_font_dev.h"
17 #include "ppapi/c/dev/ppb_gles_chromium_texture_mapping_dev.h"
18 #include "ppapi/c/dev/ppb_graphics_2d_dev.h"
19 #include "ppapi/c/dev/ppb_ime_input_event_dev.h"
20 #include "ppapi/c/dev/ppb_keyboard_input_event_dev.h"
21 #include "ppapi/c/dev/ppb_memory_dev.h"
22 #include "ppapi/c/dev/ppb_opengles2ext_dev.h"
23 #include "ppapi/c/dev/ppb_printing_dev.h"
24 #include "ppapi/c/dev/ppb_resource_array_dev.h"
25 #include "ppapi/c/dev/ppb_testing_dev.h"
26 #include "ppapi/c/dev/ppb_text_input_dev.h"
27 #include "ppapi/c/dev/ppb_url_util_dev.h"
28 #include "ppapi/c/dev/ppb_var_deprecated.h"
29 #include "ppapi/c/dev/ppb_video_capture_dev.h"
30 #include "ppapi/c/dev/ppb_view_dev.h"
31 #include "ppapi/c/ppb_audio_config.h"
32 #include "ppapi/c/ppb_audio.h"
33 #include "ppapi/c/ppb_core.h"
34 #include "ppapi/c/ppb_file_io.h"
35 #include "ppapi/c/ppb_file_ref.h"
36 #include "ppapi/c/ppb_file_system.h"
37 #include "ppapi/c/ppb_fullscreen.h"
38 #include "ppapi/c/ppb_graphics_2d.h"
39 #include "ppapi/c/ppb_image_data.h"
40 #include "ppapi/c/ppb_input_event.h"
41 #include "ppapi/c/ppb_instance.h"
42 #include "ppapi/c/ppb_message_loop.h"
43 #include "ppapi/c/ppb_messaging.h"
44 #include "ppapi/c/ppb_mouse_lock.h"
45 #include "ppapi/c/ppb_opengles2.h"
46 #include "ppapi/c/ppb_url_loader.h"
47 #include "ppapi/c/ppb_url_request_info.h"
48 #include "ppapi/c/ppb_url_response_info.h"
49 #include "ppapi/c/ppb_var.h"
50 #include "ppapi/c/ppb_var_array_buffer.h"
51 #include "ppapi/c/ppb_view.h"
52 #include "ppapi/c/pp_errors.h"
53 #include "ppapi/c/ppp_instance.h"
54 #include "ppapi/c/private/ppb_content_decryptor_private.h"
55 #include "ppapi/c/private/ppb_file_ref_private.h"
56 #include "ppapi/c/private/ppb_flash_clipboard.h"
57 #include "ppapi/c/private/ppb_flash_file.h"
58 #include "ppapi/c/private/ppb_flash_font_file.h"
59 #include "ppapi/c/private/ppb_flash_fullscreen.h"
60 #include "ppapi/c/private/ppb_flash.h"
61 #include "ppapi/c/private/ppb_flash_device_id.h"
62 #include "ppapi/c/private/ppb_flash_menu.h"
63 #include "ppapi/c/private/ppb_flash_message_loop.h"
64 #include "ppapi/c/private/ppb_flash_print.h"
65 #include "ppapi/c/private/ppb_net_address_private.h"
66 #include "ppapi/c/private/ppb_network_list_private.h"
67 #include "ppapi/c/private/ppb_network_monitor_private.h"
68 #include "ppapi/c/private/ppb_pdf.h"
69 #include "ppapi/c/private/ppb_talk_private.h"
70 #include "ppapi/c/private/ppb_tcp_socket_private.h"
71 #include "ppapi/c/private/ppb_udp_socket_private.h"
72 #include "ppapi/c/private/ppb_x509_certificate_private.h"
73 #include "ppapi/c/private/ppp_content_decryptor_private.h"
74 #include "ppapi/c/trusted/ppb_broker_trusted.h"
75 #include "ppapi/c/trusted/ppb_browser_font_trusted.h"
76 #include "ppapi/c/trusted/ppb_char_set_trusted.h"
77 #include "ppapi/c/trusted/ppb_file_io_trusted.h"
78 #include "ppapi/c/trusted/ppb_url_loader_trusted.h"
79 #include "ppapi/proxy/interface_proxy.h"
80 #include "ppapi/proxy/ppb_audio_proxy.h"
81 #include "ppapi/proxy/ppb_broker_proxy.h"
82 #include "ppapi/proxy/ppb_buffer_proxy.h"
83 #include "ppapi/proxy/ppb_core_proxy.h"
84 #include "ppapi/proxy/ppb_file_io_proxy.h"
85 #include "ppapi/proxy/ppb_file_ref_proxy.h"
86 #include "ppapi/proxy/ppb_file_system_proxy.h"
87 #include "ppapi/proxy/ppb_flash_message_loop_proxy.h"
88 #include "ppapi/proxy/ppb_flash_proxy.h"
89 #include "ppapi/proxy/ppb_graphics_2d_proxy.h"
90 #include "ppapi/proxy/ppb_graphics_3d_proxy.h"
91 #include "ppapi/proxy/ppb_host_resolver_private_proxy.h"
92 #include "ppapi/proxy/ppb_image_data_proxy.h"
93 #include "ppapi/proxy/ppb_instance_proxy.h"
94 #include "ppapi/proxy/ppb_message_loop_proxy.h"
95 #include "ppapi/proxy/ppb_network_monitor_private_proxy.h"
96 #include "ppapi/proxy/ppb_pdf_proxy.h"
97 #include "ppapi/proxy/ppb_talk_private_proxy.h"
98 #include "ppapi/proxy/ppb_tcp_server_socket_private_proxy.h"
99 #include "ppapi/proxy/ppb_tcp_socket_private_proxy.h"
100 #include "ppapi/proxy/ppb_testing_proxy.h"
101 #include "ppapi/proxy/ppb_udp_socket_private_proxy.h"
102 #include "ppapi/proxy/ppb_url_loader_proxy.h"
103 #include "ppapi/proxy/ppb_url_response_info_proxy.h"
104 #include "ppapi/proxy/ppb_var_deprecated_proxy.h"
105 #include "ppapi/proxy/ppb_video_decoder_proxy.h"
106 #include "ppapi/proxy/ppb_x509_certificate_private_proxy.h"
107 #include "ppapi/proxy/ppp_class_proxy.h"
108 #include "ppapi/proxy/ppp_content_decryptor_private_proxy.h"
109 #include "ppapi/proxy/ppp_graphics_3d_proxy.h"
110 #include "ppapi/proxy/ppp_input_event_proxy.h"
111 #include "ppapi/proxy/ppp_instance_private_proxy.h"
112 #include "ppapi/proxy/ppp_instance_proxy.h"
113 #include "ppapi/proxy/ppp_messaging_proxy.h"
114 #include "ppapi/proxy/ppp_mouse_lock_proxy.h"
115 #include "ppapi/proxy/ppp_printing_proxy.h"
116 #include "ppapi/proxy/ppp_text_input_proxy.h"
117 #include "ppapi/proxy/ppp_video_decoder_proxy.h"
118 #include "ppapi/proxy/resource_creation_proxy.h"
119 #include "ppapi/shared_impl/ppb_opengles2_shared.h"
120 #include "ppapi/shared_impl/ppb_var_shared.h"
121 #include "ppapi/thunk/thunk.h"
123 // Helper to get the proxy name PPB_Foo_Proxy given the API name PPB_Foo.
124 #define PROXY_CLASS_NAME(api_name) api_name##_Proxy
126 // Helper to get the interface ID PPB_Foo_Proxy::kApiID given the API
128 #define PROXY_API_ID(api_name) PROXY_CLASS_NAME(api_name)::kApiID
130 // Helper to get the name of the templatized factory function.
131 #define PROXY_FACTORY_NAME(api_name) ProxyFactory<PROXY_CLASS_NAME(api_name)>
133 // Helper to get the name of the thunk GetPPB_Foo_1_0_Thunk given the interface
134 // struct name PPB_Foo_1_0.
135 #define INTERFACE_THUNK_NAME(iface_struct) thunk::Get##iface_struct##_Thunk
142 // The interface list has interfaces with no ID listed as "NoAPIName" which
143 // means there's no corresponding _Proxy object. Our macros expand this to
144 // NoAPIName_Proxy, and then they look for kApiID inside it.
146 // This dummy class provides the correct definition for that interface ID,
148 class NoAPIName_Proxy
{
150 static const ApiID kApiID
= API_ID_NONE
;
153 template<typename ProxyClass
>
154 InterfaceProxy
* ProxyFactory(Dispatcher
* dispatcher
) {
155 return new ProxyClass(dispatcher
);
158 base::LazyInstance
<PpapiPermissions
> g_process_global_permissions
;
162 InterfaceList::InterfaceList() {
163 memset(id_to_factory_
, 0, sizeof(id_to_factory_
));
165 // Register the API factories for each of the API types. This calls AddProxy
166 // for each InterfaceProxy type we support.
167 #define PROXIED_API(api_name) \
168 AddProxy(PROXY_API_ID(api_name), &PROXY_FACTORY_NAME(api_name));
170 // Register each proxied interface by calling AddPPB for each supported
171 // interface. Set current_required_permission to the appropriate value for
172 // the value you want expanded by this macro.
173 #define PROXIED_IFACE(api_name, iface_str, iface_struct) \
174 AddPPB(iface_str, PROXY_API_ID(api_name), \
175 INTERFACE_THUNK_NAME(iface_struct)(), \
176 current_required_permission);
179 Permission current_required_permission
= PERMISSION_NONE
;
180 #include "ppapi/thunk/interfaces_ppb_private_no_permissions.h"
181 #include "ppapi/thunk/interfaces_ppb_public_stable.h"
185 Permission current_required_permission
= PERMISSION_DEV
;
186 #include "ppapi/thunk/interfaces_ppb_public_dev.h"
189 Permission current_required_permission
= PERMISSION_PRIVATE
;
190 #include "ppapi/thunk/interfaces_ppb_private.h"
193 #if !defined(OS_NACL)
194 Permission current_required_permission
= PERMISSION_FLASH
;
195 #include "ppapi/thunk/interfaces_ppb_private_flash.h"
196 #endif // !defined(OS_NACL)
202 // Manually add some special proxies. Some of these don't have interfaces
203 // that they support, so aren't covered by the macros above, but have proxies
204 // for message routing. Others have different implementations between the
205 // proxy and the impl and there's no obvious message routing.
206 AddProxy(API_ID_RESOURCE_CREATION
, &ResourceCreationProxy::Create
);
207 AddProxy(API_ID_PPP_CLASS
, &PPP_Class_Proxy::Create
);
208 AddPPB(PPB_CORE_INTERFACE_1_0
, API_ID_PPB_CORE
,
209 PPB_Core_Proxy::GetPPB_Core_Interface(), PERMISSION_NONE
);
210 AddPPB(PPB_MESSAGELOOP_INTERFACE_1_0
, API_ID_NONE
,
211 PPB_MessageLoop_Proxy::GetInterface(), PERMISSION_NONE
);
212 AddPPB(PPB_OPENGLES2_INTERFACE_1_0
, API_ID_NONE
,
213 PPB_OpenGLES2_Shared::GetInterface(), PERMISSION_NONE
);
214 AddPPB(PPB_OPENGLES2_INSTANCEDARRAYS_INTERFACE_1_0
, API_ID_NONE
,
215 PPB_OpenGLES2_Shared::GetInstancedArraysInterface(), PERMISSION_NONE
);
216 AddPPB(PPB_OPENGLES2_FRAMEBUFFERBLIT_INTERFACE_1_0
, API_ID_NONE
,
217 PPB_OpenGLES2_Shared::GetFramebufferBlitInterface(), PERMISSION_NONE
);
218 AddPPB(PPB_OPENGLES2_FRAMEBUFFERMULTISAMPLE_INTERFACE_1_0
, API_ID_NONE
,
219 PPB_OpenGLES2_Shared::GetFramebufferMultisampleInterface(),
221 AddPPB(PPB_OPENGLES2_CHROMIUMENABLEFEATURE_INTERFACE_1_0
, API_ID_NONE
,
222 PPB_OpenGLES2_Shared::GetChromiumEnableFeatureInterface(),
224 AddPPB(PPB_OPENGLES2_CHROMIUMMAPSUB_INTERFACE_1_0
, API_ID_NONE
,
225 PPB_OpenGLES2_Shared::GetChromiumMapSubInterface(), PERMISSION_NONE
);
226 AddPPB(PPB_OPENGLES2_CHROMIUMMAPSUB_DEV_INTERFACE_1_0
, API_ID_NONE
,
227 PPB_OpenGLES2_Shared::GetChromiumMapSubInterface(), PERMISSION_NONE
);
228 AddPPB(PPB_OPENGLES2_QUERY_INTERFACE_1_0
, API_ID_NONE
,
229 PPB_OpenGLES2_Shared::GetQueryInterface(), PERMISSION_NONE
);
230 #if !defined(OS_NACL)
231 AddPPB(PPB_FLASH_PRINT_INTERFACE_1_0
, API_ID_PPB_FLASH
,
232 PPB_Flash_Proxy::GetFlashPrintInterface(),
235 AddPPB(PPB_VAR_ARRAY_BUFFER_INTERFACE_1_0
, API_ID_NONE
,
236 PPB_Var_Shared::GetVarArrayBufferInterface1_0(),
238 AddPPB(PPB_VAR_INTERFACE_1_1
, API_ID_NONE
,
239 PPB_Var_Shared::GetVarInterface1_1(), PERMISSION_NONE
);
240 AddPPB(PPB_VAR_INTERFACE_1_0
, API_ID_NONE
,
241 PPB_Var_Shared::GetVarInterface1_0(), PERMISSION_NONE
);
243 #if !defined(OS_NACL)
244 // PPB (browser) interfaces.
245 // Do not add more stuff here, they should be added to interface_list*.h
246 // TODO(brettw) remove these.
247 AddPPB(PPB_Instance_Proxy::GetInfoPrivate(), PERMISSION_PRIVATE
);
248 AddPPB(PPB_PDF_Proxy::GetInfo(), PERMISSION_PRIVATE
);
249 AddPPB(PPB_URLLoader_Proxy::GetTrustedInfo(), PERMISSION_PRIVATE
);
250 AddPPB(PPB_Var_Deprecated_Proxy::GetInfo(), PERMISSION_DEV
);
252 // TODO(tomfinegan): Figure out where to put these once we refactor things
253 // to load the PPP interface struct from the PPB interface.
254 AddProxy(API_ID_PPP_CONTENT_DECRYPTOR_PRIVATE
,
255 &ProxyFactory
<PPP_ContentDecryptor_Private_Proxy
>);
256 AddPPP(PPP_CONTENTDECRYPTOR_PRIVATE_INTERFACE
,
257 API_ID_PPP_CONTENT_DECRYPTOR_PRIVATE
,
258 PPP_ContentDecryptor_Private_Proxy::GetProxyInterface());
260 AddPPB(PPB_Testing_Proxy::GetInfo(), PERMISSION_TESTING
);
262 // PPP (plugin) interfaces.
263 // TODO(brettw) move these to interface_list*.h
264 AddProxy(API_ID_PPP_INSTANCE
, &ProxyFactory
<PPP_Instance_Proxy
>);
265 #if !defined(OS_NACL)
266 AddPPP(PPP_INSTANCE_INTERFACE_1_1
, API_ID_PPP_INSTANCE
,
267 PPP_Instance_Proxy::GetInstanceInterface());
269 AddProxy(API_ID_PPP_PRINTING
, &ProxyFactory
<PPP_Printing_Proxy
>);
270 AddPPP(PPP_PRINTING_DEV_INTERFACE
, API_ID_PPP_PRINTING
,
271 PPP_Printing_Proxy::GetProxyInterface());
272 AddProxy(API_ID_PPP_TEXT_INPUT
, &ProxyFactory
<PPP_TextInput_Proxy
>);
273 AddPPP(PPP_TEXTINPUT_DEV_INTERFACE
, API_ID_PPP_TEXT_INPUT
,
274 PPP_TextInput_Proxy::GetProxyInterface());
276 // Old-style GetInfo PPP interfaces.
277 // Do not add more stuff here, they should be added to interface_list*.h
278 // TODO(brettw) remove these.
279 AddPPP(PPP_InputEvent_Proxy::GetInfo());
280 AddPPP(PPP_Messaging_Proxy::GetInfo());
281 AddPPP(PPP_MouseLock_Proxy::GetInfo());
282 AddPPP(PPP_Graphics3D_Proxy::GetInfo());
283 #if !defined(OS_NACL)
284 AddPPP(PPP_Instance_Private_Proxy::GetInfo());
285 AddPPP(PPP_VideoDecoder_Proxy::GetInfo());
289 InterfaceList::~InterfaceList() {
293 InterfaceList
* InterfaceList::GetInstance() {
294 return Singleton
<InterfaceList
>::get();
298 void InterfaceList::SetProcessGlobalPermissions(
299 const PpapiPermissions
& permissions
) {
300 g_process_global_permissions
.Get() = permissions
;
303 ApiID
InterfaceList::GetIDForPPBInterface(const std::string
& name
) const {
304 NameToInterfaceInfoMap::const_iterator found
=
305 name_to_browser_info_
.find(name
);
306 if (found
== name_to_browser_info_
.end())
308 return found
->second
.id
;
311 ApiID
InterfaceList::GetIDForPPPInterface(const std::string
& name
) const {
312 NameToInterfaceInfoMap::const_iterator found
=
313 name_to_plugin_info_
.find(name
);
314 if (found
== name_to_plugin_info_
.end())
316 return found
->second
.id
;
319 InterfaceProxy::Factory
InterfaceList::GetFactoryForID(ApiID id
) const {
320 int index
= static_cast<int>(id
);
321 COMPILE_ASSERT(API_ID_NONE
== 0, none_must_be_zero
);
322 if (id
<= 0 || id
>= API_ID_COUNT
)
324 return id_to_factory_
[index
];
327 const void* InterfaceList::GetInterfaceForPPB(const std::string
& name
) const {
328 NameToInterfaceInfoMap::const_iterator found
=
329 name_to_browser_info_
.find(name
);
330 if (found
== name_to_browser_info_
.end())
333 if (g_process_global_permissions
.Get().HasPermission(
334 found
->second
.required_permission
))
335 return found
->second
.iface
;
339 const void* InterfaceList::GetInterfaceForPPP(const std::string
& name
) const {
340 NameToInterfaceInfoMap::const_iterator found
=
341 name_to_plugin_info_
.find(name
);
342 if (found
== name_to_plugin_info_
.end())
344 return found
->second
.iface
;
347 void InterfaceList::AddProxy(ApiID id
,
348 InterfaceProxy::Factory factory
) {
349 // For interfaces with no corresponding _Proxy objects, the macros will
350 // generate calls to this function with API_ID_NONE. This means we
351 // should just skip adding a factory for these functions.
352 if (id
== API_ID_NONE
)
355 // The factory should be an exact dupe of the one we already have if it
356 // has already been registered before.
357 int index
= static_cast<int>(id
);
358 DCHECK(!id_to_factory_
[index
] || id_to_factory_
[index
] == factory
);
360 id_to_factory_
[index
] = factory
;
363 void InterfaceList::AddPPB(const char* name
,
367 DCHECK(name_to_browser_info_
.find(name
) == name_to_browser_info_
.end());
368 name_to_browser_info_
[name
] = InterfaceInfo(id
, iface
, perm
);
371 void InterfaceList::AddPPP(const char* name
,
374 DCHECK(name_to_plugin_info_
.find(name
) == name_to_plugin_info_
.end());
375 name_to_plugin_info_
[name
] = InterfaceInfo(id
, iface
, PERMISSION_NONE
);
378 void InterfaceList::AddPPB(const InterfaceProxy::Info
* info
, Permission perm
) {
379 AddProxy(info
->id
, info
->create_proxy
);
380 AddPPB(info
->name
, info
->id
, info
->interface_ptr
, perm
);
383 void InterfaceList::AddPPP(const InterfaceProxy::Info
* info
) {
384 AddProxy(info
->id
, info
->create_proxy
);
385 AddPPP(info
->name
, info
->id
, info
->interface_ptr
);