Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / ppapi / proxy / interface_list.cc
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.
4
5 #include "ppapi/proxy/interface_list.h"
6
7 #include "base/hash.h"
8 #include "base/lazy_instance.h"
9 #include "base/memory/singleton.h"
10 #include "ppapi/c/dev/ppb_audio_input_dev.h"
11 #include "ppapi/c/dev/ppb_buffer_dev.h"
12 #include "ppapi/c/dev/ppb_char_set_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_ime_input_event_dev.h"
19 #include "ppapi/c/dev/ppb_memory_dev.h"
20 #include "ppapi/c/dev/ppb_messaging_deprecated.h"
21 #include "ppapi/c/dev/ppb_opengles2ext_dev.h"
22 #include "ppapi/c/dev/ppb_printing_dev.h"
23 #include "ppapi/c/dev/ppb_scrollbar_dev.h"
24 #include "ppapi/c/dev/ppb_text_input_dev.h"
25 #include "ppapi/c/dev/ppb_trace_event_dev.h"
26 #include "ppapi/c/dev/ppb_truetype_font_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/dev/ppb_widget_dev.h"
32 #include "ppapi/c/dev/ppb_zoom_dev.h"
33 #include "ppapi/c/ppb_audio.h"
34 #include "ppapi/c/ppb_audio_buffer.h"
35 #include "ppapi/c/ppb_audio_config.h"
36 #include "ppapi/c/ppb_compositor.h"
37 #include "ppapi/c/ppb_compositor_layer.h"
38 #include "ppapi/c/ppb_console.h"
39 #include "ppapi/c/ppb_core.h"
40 #include "ppapi/c/ppb_file_io.h"
41 #include "ppapi/c/ppb_file_mapping.h"
42 #include "ppapi/c/ppb_file_ref.h"
43 #include "ppapi/c/ppb_file_system.h"
44 #include "ppapi/c/ppb_fullscreen.h"
45 #include "ppapi/c/ppb_graphics_2d.h"
46 #include "ppapi/c/ppb_host_resolver.h"
47 #include "ppapi/c/ppb_image_data.h"
48 #include "ppapi/c/ppb_input_event.h"
49 #include "ppapi/c/ppb_instance.h"
50 #include "ppapi/c/ppb_media_stream_audio_track.h"
51 #include "ppapi/c/ppb_media_stream_video_track.h"
52 #include "ppapi/c/ppb_message_loop.h"
53 #include "ppapi/c/ppb_messaging.h"
54 #include "ppapi/c/ppb_mouse_lock.h"
55 #include "ppapi/c/ppb_net_address.h"
56 #include "ppapi/c/ppb_network_list.h"
57 #include "ppapi/c/ppb_network_monitor.h"
58 #include "ppapi/c/ppb_network_proxy.h"
59 #include "ppapi/c/ppb_opengles2.h"
60 #include "ppapi/c/ppb_tcp_socket.h"
61 #include "ppapi/c/ppb_text_input_controller.h"
62 #include "ppapi/c/ppb_udp_socket.h"
63 #include "ppapi/c/ppb_url_loader.h"
64 #include "ppapi/c/ppb_url_request_info.h"
65 #include "ppapi/c/ppb_url_response_info.h"
66 #include "ppapi/c/ppb_var.h"
67 #include "ppapi/c/ppb_var_array.h"
68 #include "ppapi/c/ppb_var_array_buffer.h"
69 #include "ppapi/c/ppb_var_dictionary.h"
70 #include "ppapi/c/ppb_video_decoder.h"
71 #include "ppapi/c/ppb_video_frame.h"
72 #include "ppapi/c/ppb_view.h"
73 #include "ppapi/c/pp_errors.h"
74 #include "ppapi/c/ppp_instance.h"
75 #include "ppapi/c/private/ppb_content_decryptor_private.h"
76 #include "ppapi/c/private/ppb_ext_crx_file_system_private.h"
77 #include "ppapi/c/private/ppb_file_io_private.h"
78 #include "ppapi/c/private/ppb_file_ref_private.h"
79 #include "ppapi/c/private/ppb_find_private.h"
80 #include "ppapi/c/private/ppb_flash_clipboard.h"
81 #include "ppapi/c/private/ppb_flash_file.h"
82 #include "ppapi/c/private/ppb_flash_font_file.h"
83 #include "ppapi/c/private/ppb_flash_fullscreen.h"
84 #include "ppapi/c/private/ppb_flash.h"
85 #include "ppapi/c/private/ppb_flash_device_id.h"
86 #include "ppapi/c/private/ppb_flash_drm.h"
87 #include "ppapi/c/private/ppb_flash_menu.h"
88 #include "ppapi/c/private/ppb_flash_message_loop.h"
89 #include "ppapi/c/private/ppb_flash_print.h"
90 #include "ppapi/c/private/ppb_host_resolver_private.h"
91 #include "ppapi/c/private/ppb_input_event_private.h"
92 #include "ppapi/c/private/ppb_isolated_file_system_private.h"
93 #include "ppapi/c/private/ppb_net_address_private.h"
94 #include "ppapi/c/private/ppb_output_protection_private.h"
95 #include "ppapi/c/private/ppb_pdf.h"
96 #include "ppapi/c/private/ppb_platform_verification_private.h"
97 #include "ppapi/c/private/ppb_talk_private.h"
98 #include "ppapi/c/private/ppb_tcp_server_socket_private.h"
99 #include "ppapi/c/private/ppb_tcp_socket_private.h"
100 #include "ppapi/c/private/ppb_testing_private.h"
101 #include "ppapi/c/private/ppb_udp_socket_private.h"
102 #include "ppapi/c/private/ppb_uma_private.h"
103 #include "ppapi/c/private/ppb_video_destination_private.h"
104 #include "ppapi/c/private/ppb_video_source_private.h"
105 #include "ppapi/c/private/ppb_x509_certificate_private.h"
106 #include "ppapi/c/private/ppp_content_decryptor_private.h"
107 #include "ppapi/c/trusted/ppb_broker_trusted.h"
108 #include "ppapi/c/trusted/ppb_browser_font_trusted.h"
109 #include "ppapi/c/trusted/ppb_char_set_trusted.h"
110 #include "ppapi/c/trusted/ppb_file_chooser_trusted.h"
111 #include "ppapi/c/trusted/ppb_url_loader_trusted.h"
112 #include "ppapi/proxy/interface_proxy.h"
113 #include "ppapi/proxy/plugin_globals.h"
114 #include "ppapi/proxy/ppapi_messages.h"
115 #include "ppapi/proxy/ppb_audio_proxy.h"
116 #include "ppapi/proxy/ppb_broker_proxy.h"
117 #include "ppapi/proxy/ppb_buffer_proxy.h"
118 #include "ppapi/proxy/ppb_core_proxy.h"
119 #include "ppapi/proxy/ppb_flash_message_loop_proxy.h"
120 #include "ppapi/proxy/ppb_graphics_3d_proxy.h"
121 #include "ppapi/proxy/ppb_image_data_proxy.h"
122 #include "ppapi/proxy/ppb_instance_proxy.h"
123 #include "ppapi/proxy/ppb_message_loop_proxy.h"
124 #include "ppapi/proxy/ppb_testing_proxy.h"
125 #include "ppapi/proxy/ppb_var_deprecated_proxy.h"
126 #include "ppapi/proxy/ppb_video_decoder_proxy.h"
127 #include "ppapi/proxy/ppb_x509_certificate_private_proxy.h"
128 #include "ppapi/proxy/ppp_class_proxy.h"
129 #include "ppapi/proxy/ppp_content_decryptor_private_proxy.h"
130 #include "ppapi/proxy/ppp_find_proxy.h"
131 #include "ppapi/proxy/ppp_graphics_3d_proxy.h"
132 #include "ppapi/proxy/ppp_input_event_proxy.h"
133 #include "ppapi/proxy/ppp_instance_private_proxy.h"
134 #include "ppapi/proxy/ppp_instance_proxy.h"
135 #include "ppapi/proxy/ppp_messaging_proxy.h"
136 #include "ppapi/proxy/ppp_mouse_lock_proxy.h"
137 #include "ppapi/proxy/ppp_pdf_proxy.h"
138 #include "ppapi/proxy/ppp_printing_proxy.h"
139 #include "ppapi/proxy/ppp_text_input_proxy.h"
140 #include "ppapi/proxy/ppp_video_decoder_proxy.h"
141 #include "ppapi/proxy/resource_creation_proxy.h"
142 #include "ppapi/shared_impl/ppb_opengles2_shared.h"
143 #include "ppapi/shared_impl/ppb_var_shared.h"
144 #include "ppapi/thunk/thunk.h"
145
146 // Helper to get the proxy name PPB_Foo_Proxy given the API name PPB_Foo.
147 #define PROXY_CLASS_NAME(api_name) api_name##_Proxy
148
149 // Helper to get the interface ID PPB_Foo_Proxy::kApiID given the API
150 // name PPB_Foo.
151 #define PROXY_API_ID(api_name) PROXY_CLASS_NAME(api_name)::kApiID
152
153 // Helper to get the name of the templatized factory function.
154 #define PROXY_FACTORY_NAME(api_name) ProxyFactory<PROXY_CLASS_NAME(api_name)>
155
156 // Helper to get the name of the thunk GetPPB_Foo_1_0_Thunk given the interface
157 // struct name PPB_Foo_1_0.
158 #define INTERFACE_THUNK_NAME(iface_struct) thunk::Get##iface_struct##_Thunk
159
160 namespace ppapi {
161 namespace proxy {
162
163 namespace {
164
165 template<typename ProxyClass>
166 InterfaceProxy* ProxyFactory(Dispatcher* dispatcher) {
167   return new ProxyClass(dispatcher);
168 }
169
170 base::LazyInstance<PpapiPermissions> g_process_global_permissions;
171
172 }  // namespace
173
174 InterfaceList::InterfaceList() {
175   memset(id_to_factory_, 0, sizeof(id_to_factory_));
176
177   // Register the API factories for each of the API types. This calls AddProxy
178   // for each InterfaceProxy type we support.
179   #define PROXIED_API(api_name) \
180       AddProxy(PROXY_API_ID(api_name), &PROXY_FACTORY_NAME(api_name));
181
182   // Register each proxied interface by calling AddPPB for each supported
183   // interface. Set current_required_permission to the appropriate value for
184   // the value you want expanded by this macro.
185   #define PROXIED_IFACE(iface_str, iface_struct) \
186       AddPPB(iface_str, \
187              INTERFACE_THUNK_NAME(iface_struct)(), \
188              current_required_permission);
189
190   {
191     Permission current_required_permission = PERMISSION_NONE;
192     #include "ppapi/thunk/interfaces_ppb_private_no_permissions.h"
193     #include "ppapi/thunk/interfaces_ppb_public_stable.h"
194   }
195   {
196     Permission current_required_permission = PERMISSION_DEV;
197     #include "ppapi/thunk/interfaces_ppb_public_dev.h"
198   }
199   {
200     Permission current_required_permission = PERMISSION_PRIVATE;
201     #include "ppapi/thunk/interfaces_ppb_private.h"
202   }
203   {
204 #if !defined(OS_NACL)
205     Permission current_required_permission = PERMISSION_FLASH;
206     #include "ppapi/thunk/interfaces_ppb_private_flash.h"
207 #endif  // !defined(OS_NACL)
208   }
209   {
210     Permission current_required_permission = PERMISSION_DEV_CHANNEL;
211     #include "ppapi/thunk/interfaces_ppb_public_dev_channel.h"
212   }
213
214   #undef PROXIED_API
215   #undef PROXIED_IFACE
216
217   // Manually add some special proxies. Some of these don't have interfaces
218   // that they support, so aren't covered by the macros above, but have proxies
219   // for message routing. Others have different implementations between the
220   // proxy and the impl and there's no obvious message routing.
221   AddProxy(API_ID_RESOURCE_CREATION, &ResourceCreationProxy::Create);
222   AddProxy(API_ID_PPP_CLASS, &PPP_Class_Proxy::Create);
223   AddPPB(PPB_CORE_INTERFACE_1_0,
224          PPB_Core_Proxy::GetPPB_Core_Interface(), PERMISSION_NONE);
225   AddPPB(PPB_MESSAGELOOP_INTERFACE_1_0,
226          PPB_MessageLoop_Proxy::GetInterface(), PERMISSION_NONE);
227   AddPPB(PPB_OPENGLES2_INTERFACE_1_0,
228          PPB_OpenGLES2_Shared::GetInterface(), PERMISSION_NONE);
229   AddPPB(PPB_OPENGLES2_INSTANCEDARRAYS_INTERFACE_1_0,
230          PPB_OpenGLES2_Shared::GetInstancedArraysInterface(), PERMISSION_NONE);
231   AddPPB(PPB_OPENGLES2_FRAMEBUFFERBLIT_INTERFACE_1_0,
232          PPB_OpenGLES2_Shared::GetFramebufferBlitInterface(), PERMISSION_NONE);
233   AddPPB(PPB_OPENGLES2_FRAMEBUFFERMULTISAMPLE_INTERFACE_1_0,
234          PPB_OpenGLES2_Shared::GetFramebufferMultisampleInterface(),
235          PERMISSION_NONE);
236   AddPPB(PPB_OPENGLES2_CHROMIUMENABLEFEATURE_INTERFACE_1_0,
237          PPB_OpenGLES2_Shared::GetChromiumEnableFeatureInterface(),
238          PERMISSION_NONE);
239   AddPPB(PPB_OPENGLES2_CHROMIUMMAPSUB_INTERFACE_1_0,
240          PPB_OpenGLES2_Shared::GetChromiumMapSubInterface(), PERMISSION_NONE);
241   AddPPB(PPB_OPENGLES2_CHROMIUMMAPSUB_DEV_INTERFACE_1_0,
242          PPB_OpenGLES2_Shared::GetChromiumMapSubInterface(), PERMISSION_NONE);
243   AddPPB(PPB_OPENGLES2_QUERY_INTERFACE_1_0,
244          PPB_OpenGLES2_Shared::GetQueryInterface(), PERMISSION_NONE);
245   AddPPB(PPB_OPENGLES2_VERTEXARRAYOBJECT_INTERFACE_1_0,
246          PPB_OpenGLES2_Shared::GetVertexArrayObjectInterface(),
247          PERMISSION_NONE);
248   AddPPB(PPB_OPENGLES2_DRAWBUFFERS_DEV_INTERFACE_1_0,
249          PPB_OpenGLES2_Shared::GetDrawBuffersInterface(),
250          PERMISSION_DEV);
251   AddPPB(PPB_VAR_ARRAY_BUFFER_INTERFACE_1_0,
252          PPB_Var_Shared::GetVarArrayBufferInterface1_0(),
253          PERMISSION_NONE);
254   AddPPB(PPB_VAR_INTERFACE_1_2,
255          PPB_Var_Shared::GetVarInterface1_2(), PERMISSION_NONE);
256   AddPPB(PPB_VAR_INTERFACE_1_1,
257          PPB_Var_Shared::GetVarInterface1_1(), PERMISSION_NONE);
258   AddPPB(PPB_VAR_INTERFACE_1_0,
259          PPB_Var_Shared::GetVarInterface1_0(), PERMISSION_NONE);
260
261 #if !defined(OS_NACL)
262   // PPB (browser) interfaces.
263   // Do not add more stuff here, they should be added to interface_list*.h
264   // TODO(brettw) remove these.
265   AddProxy(API_ID_PPB_INSTANCE_PRIVATE, &ProxyFactory<PPB_Instance_Proxy>);
266   AddPPB(PPB_INSTANCE_PRIVATE_INTERFACE_0_1,
267          thunk::GetPPB_Instance_Private_0_1_Thunk(),
268          PERMISSION_PRIVATE);
269
270   AddProxy(API_ID_PPB_VAR_DEPRECATED, &ProxyFactory<PPB_Var_Deprecated_Proxy>);
271   AddPPB(PPB_VAR_DEPRECATED_INTERFACE,
272          PPB_Var_Deprecated_Proxy::GetProxyInterface(), PERMISSION_DEV);
273
274   // TODO(tomfinegan): Figure out where to put these once we refactor things
275   // to load the PPP interface struct from the PPB interface.
276   AddProxy(API_ID_PPP_CONTENT_DECRYPTOR_PRIVATE,
277            &ProxyFactory<PPP_ContentDecryptor_Private_Proxy>);
278   AddPPP(PPP_CONTENTDECRYPTOR_PRIVATE_INTERFACE,
279          PPP_ContentDecryptor_Private_Proxy::GetProxyInterface());
280 #endif
281   AddProxy(API_ID_PPB_TESTING, &ProxyFactory<PPB_Testing_Proxy>);
282   AddPPB(PPB_TESTING_PRIVATE_INTERFACE,
283          PPB_Testing_Proxy::GetProxyInterface(), PERMISSION_TESTING);
284
285   // PPP (plugin) interfaces.
286   // TODO(brettw) move these to interface_list*.h
287   AddProxy(API_ID_PPP_GRAPHICS_3D, &ProxyFactory<PPP_Graphics3D_Proxy>);
288   AddPPP(PPP_GRAPHICS_3D_INTERFACE, PPP_Graphics3D_Proxy::GetProxyInterface());
289   AddProxy(API_ID_PPP_INPUT_EVENT, &ProxyFactory<PPP_InputEvent_Proxy>);
290   AddPPP(PPP_INPUT_EVENT_INTERFACE, PPP_InputEvent_Proxy::GetProxyInterface());
291   AddProxy(API_ID_PPP_INSTANCE, &ProxyFactory<PPP_Instance_Proxy>);
292 #if !defined(OS_NACL)
293   AddPPP(PPP_INSTANCE_INTERFACE_1_1,
294          PPP_Instance_Proxy::GetInstanceInterface());
295   AddProxy(API_ID_PPP_INSTANCE_PRIVATE,
296            &ProxyFactory<PPP_Instance_Private_Proxy>);
297   AddPPP(PPP_INSTANCE_PRIVATE_INTERFACE,
298          PPP_Instance_Private_Proxy::GetProxyInterface());
299 #endif
300   AddProxy(API_ID_PPP_MESSAGING, &ProxyFactory<PPP_Messaging_Proxy>);
301   AddProxy(API_ID_PPP_MOUSE_LOCK, &ProxyFactory<PPP_MouseLock_Proxy>);
302   AddPPP(PPP_MOUSELOCK_INTERFACE, PPP_MouseLock_Proxy::GetProxyInterface());
303   AddProxy(API_ID_PPP_PRINTING, &ProxyFactory<PPP_Printing_Proxy>);
304   AddPPP(PPP_PRINTING_DEV_INTERFACE, PPP_Printing_Proxy::GetProxyInterface());
305   AddProxy(API_ID_PPP_TEXT_INPUT, &ProxyFactory<PPP_TextInput_Proxy>);
306   AddPPP(PPP_TEXTINPUT_DEV_INTERFACE, PPP_TextInput_Proxy::GetProxyInterface());
307 #if !defined(OS_NACL)
308   AddProxy(API_ID_PPP_PDF, &ProxyFactory<PPP_Pdf_Proxy>);
309   AddPPP(PPP_PDF_INTERFACE, PPP_Pdf_Proxy::GetProxyInterface());
310   AddProxy(API_ID_PPP_FIND_PRIVATE, &ProxyFactory<PPP_Find_Proxy>);
311   AddPPP(PPP_FIND_PRIVATE_INTERFACE, PPP_Find_Proxy::GetProxyInterface());
312   AddProxy(API_ID_PPP_VIDEO_DECODER_DEV, &ProxyFactory<PPP_VideoDecoder_Proxy>);
313   AddPPP(PPP_VIDEODECODER_DEV_INTERFACE,
314          PPP_VideoDecoder_Proxy::GetProxyInterface());
315 #endif
316 }
317
318 InterfaceList::~InterfaceList() {
319 }
320
321 // static
322 InterfaceList* InterfaceList::GetInstance() {
323   // CAUTION: This function is called without the ProxyLock to avoid excessive
324   // excessive locking from C++ wrappers. (See also GetBrowserInterface.)
325   return Singleton<InterfaceList>::get();
326 }
327
328 // static
329 void InterfaceList::SetProcessGlobalPermissions(
330     const PpapiPermissions& permissions) {
331   g_process_global_permissions.Get() = permissions;
332 }
333
334 InterfaceProxy::Factory InterfaceList::GetFactoryForID(ApiID id) const {
335   int index = static_cast<int>(id);
336   COMPILE_ASSERT(API_ID_NONE == 0, none_must_be_zero);
337   if (id <= 0 || id >= API_ID_COUNT)
338     return NULL;
339   return id_to_factory_[index];
340 }
341
342 const void* InterfaceList::GetInterfaceForPPB(const std::string& name) {
343   // CAUTION: This function is called without the ProxyLock to avoid excessive
344   // excessive locking from C++ wrappers. (See also GetBrowserInterface.)
345   NameToInterfaceInfoMap::iterator found =
346       name_to_browser_info_.find(name);
347   if (found == name_to_browser_info_.end())
348     return NULL;
349
350   if (g_process_global_permissions.Get().HasPermission(
351           found->second->required_permission())) {
352     // Only log interface use once per plugin.
353     found->second->LogWithUmaOnce(
354         PluginGlobals::Get()->GetBrowserSender(), name);
355     return found->second->iface();
356   }
357   return NULL;
358 }
359
360 const void* InterfaceList::GetInterfaceForPPP(const std::string& name) const {
361   NameToInterfaceInfoMap::const_iterator found =
362       name_to_plugin_info_.find(name);
363   if (found == name_to_plugin_info_.end())
364     return NULL;
365   return found->second->iface();
366 }
367
368 void InterfaceList::InterfaceInfo::LogWithUmaOnce(
369     IPC::Sender* sender, const std::string& name) {
370   {
371     base::AutoLock acquire(sent_to_uma_lock_);
372     if (sent_to_uma_)
373       return;
374     sent_to_uma_ = true;
375   }
376   int hash = InterfaceList::HashInterfaceName(name);
377   PluginGlobals::Get()->GetBrowserSender()->Send(
378       new PpapiHostMsg_LogInterfaceUsage(hash));
379 }
380
381 void InterfaceList::AddProxy(ApiID id,
382                              InterfaceProxy::Factory factory) {
383   // For interfaces with no corresponding _Proxy objects, the macros will
384   // generate calls to this function with API_ID_NONE. This means we
385   // should just skip adding a factory for these functions.
386   if (id == API_ID_NONE)
387     return;
388
389   // The factory should be an exact dupe of the one we already have if it
390   // has already been registered before.
391   int index = static_cast<int>(id);
392   DCHECK(!id_to_factory_[index] || id_to_factory_[index] == factory);
393
394   id_to_factory_[index] = factory;
395 }
396
397 void InterfaceList::AddPPB(const char* name,
398                            const void* iface,
399                            Permission perm) {
400   DCHECK(name_to_browser_info_.find(name) == name_to_browser_info_.end());
401   name_to_browser_info_.add(
402       name, scoped_ptr<InterfaceInfo>(new InterfaceInfo(iface, perm)));
403 }
404
405 void InterfaceList::AddPPP(const char* name,
406                            const void* iface) {
407   DCHECK(name_to_plugin_info_.find(name) == name_to_plugin_info_.end());
408   name_to_plugin_info_.add(
409       name,
410       scoped_ptr<InterfaceInfo>(new InterfaceInfo(iface, PERMISSION_NONE)));
411 }
412
413 int InterfaceList::HashInterfaceName(const std::string& name) {
414   uint32 data = base::Hash(name.c_str(), name.size());
415   // Strip off the signed bit because UMA doesn't support negative values,
416   // but takes a signed int as input.
417   return static_cast<int>(data & 0x7fffffff);
418 }
419
420 }  // namespace proxy
421 }  // namespace ppapi