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
3 // found in the LICENSE file.
5 #include "ppapi/proxy/resource_creation_proxy.h"
7 #include "ppapi/c/pp_errors.h"
8 #include "ppapi/c/pp_size.h"
9 #include "ppapi/c/trusted/ppb_image_data_trusted.h"
10 #include "ppapi/proxy/plugin_dispatcher.h"
11 #include "ppapi/proxy/plugin_resource_tracker.h"
12 #include "ppapi/proxy/ppapi_messages.h"
13 #include "ppapi/proxy/ppb_audio_input_proxy.h"
14 #include "ppapi/proxy/ppb_audio_proxy.h"
15 #include "ppapi/proxy/ppb_buffer_proxy.h"
16 #include "ppapi/proxy/ppb_broker_proxy.h"
17 #include "ppapi/proxy/ppb_file_chooser_proxy.h"
18 #include "ppapi/proxy/ppb_file_ref_proxy.h"
19 #include "ppapi/proxy/ppb_file_system_proxy.h"
20 #include "ppapi/proxy/ppb_flash_menu_proxy.h"
21 #include "ppapi/proxy/ppb_flash_net_connector_proxy.h"
22 #include "ppapi/proxy/ppb_font_proxy.h"
23 #include "ppapi/proxy/ppb_graphics_2d_proxy.h"
24 #include "ppapi/proxy/ppb_graphics_3d_proxy.h"
25 #include "ppapi/proxy/ppb_image_data_proxy.h"
26 #include "ppapi/proxy/ppb_tcp_socket_private_proxy.h"
27 #include "ppapi/proxy/ppb_udp_socket_private_proxy.h"
28 #include "ppapi/proxy/ppb_url_loader_proxy.h"
29 #include "ppapi/proxy/ppb_video_capture_proxy.h"
30 #include "ppapi/proxy/ppb_video_decoder_proxy.h"
31 #include "ppapi/shared_impl/api_id.h"
32 #include "ppapi/shared_impl/audio_config_impl.h"
33 #include "ppapi/shared_impl/font_impl.h"
34 #include "ppapi/shared_impl/function_group_base.h"
35 #include "ppapi/shared_impl/host_resource.h"
36 #include "ppapi/shared_impl/input_event_impl.h"
37 #include "ppapi/shared_impl/url_request_info_impl.h"
38 #include "ppapi/shared_impl/var.h"
39 #include "ppapi/thunk/enter.h"
40 #include "ppapi/thunk/ppb_image_data_api.h"
42 using ppapi::thunk::ResourceCreationAPI
;
47 ResourceCreationProxy::ResourceCreationProxy(Dispatcher
* dispatcher
)
48 : InterfaceProxy(dispatcher
) {
51 ResourceCreationProxy::~ResourceCreationProxy() {
55 InterfaceProxy
* ResourceCreationProxy::Create(Dispatcher
* dispatcher
) {
56 return new ResourceCreationProxy(dispatcher
);
59 ResourceCreationAPI
* ResourceCreationProxy::AsResourceCreationAPI() {
63 PP_Resource
ResourceCreationProxy::CreateAudio(
65 PP_Resource config_id
,
66 PPB_Audio_Callback audio_callback
,
68 return PPB_Audio_Proxy::CreateProxyResource(instance
, config_id
,
69 audio_callback
, user_data
);
72 PP_Resource
ResourceCreationProxy::CreateAudioConfig(
74 PP_AudioSampleRate sample_rate
,
75 uint32_t sample_frame_count
) {
76 return AudioConfigImpl::CreateAsProxy(
77 instance
, sample_rate
, sample_frame_count
);
80 PP_Resource
ResourceCreationProxy::CreateAudioTrusted(PP_Instance instance
) {
81 // Proxied plugins can't created trusted audio devices.
85 PP_Resource
ResourceCreationProxy::CreateAudioInput(
87 PP_Resource config_id
,
88 PPB_AudioInput_Callback audio_input_callback
,
90 return PPB_AudioInput_Proxy::CreateProxyResource(instance
, config_id
,
95 PP_Resource
ResourceCreationProxy::CreateAudioInputTrusted(
96 PP_Instance instance
) {
97 // Proxied plugins can't created trusted audio input devices.
101 PP_Resource
ResourceCreationProxy::CreateBroker(PP_Instance instance
) {
102 return PPB_Broker_Proxy::CreateProxyResource(instance
);
105 PP_Resource
ResourceCreationProxy::CreateBuffer(PP_Instance instance
,
107 return PPB_Buffer_Proxy::CreateProxyResource(instance
, size
);
110 PP_Resource
ResourceCreationProxy::CreateDirectoryReader(
111 PP_Resource directory_ref
) {
112 NOTIMPLEMENTED(); // Not proxied yet.
116 PP_Resource
ResourceCreationProxy::CreateFileChooser(
117 PP_Instance instance
,
118 PP_FileChooserMode_Dev mode
,
119 const char* accept_mime_types
) {
120 return PPB_FileChooser_Proxy::CreateProxyResource(instance
, mode
,
124 PP_Resource
ResourceCreationProxy::CreateFileIO(PP_Instance instance
) {
125 NOTIMPLEMENTED(); // Not proxied yet.
129 PP_Resource
ResourceCreationProxy::CreateFileRef(PP_Resource file_system
,
131 return PPB_FileRef_Proxy::CreateProxyResource(file_system
, path
);
134 PP_Resource
ResourceCreationProxy::CreateFileSystem(
135 PP_Instance instance
,
136 PP_FileSystemType type
) {
137 return PPB_FileSystem_Proxy::CreateProxyResource(instance
, type
);
140 PP_Resource
ResourceCreationProxy::CreateFlashMenu(
141 PP_Instance instance
,
142 const PP_Flash_Menu
* menu_data
) {
143 return PPB_Flash_Menu_Proxy::CreateProxyResource(instance
, menu_data
);
146 PP_Resource
ResourceCreationProxy::CreateFlashNetConnector(
147 PP_Instance instance
) {
148 return PPB_Flash_NetConnector_Proxy::CreateProxyResource(instance
);
151 PP_Resource
ResourceCreationProxy::CreateFontObject(
152 PP_Instance instance
,
153 const PP_FontDescription_Dev
* description
) {
154 if (!ppapi::FontImpl::IsPPFontDescriptionValid(*description
))
156 return (new Font(HostResource::MakeInstanceOnly(instance
), *description
))->
160 PP_Resource
ResourceCreationProxy::CreateGraphics2D(PP_Instance instance
,
162 PP_Bool is_always_opaque
) {
163 return PPB_Graphics2D_Proxy::CreateProxyResource(instance
, size
,
167 PP_Resource
ResourceCreationProxy::CreateImageData(PP_Instance instance
,
168 PP_ImageDataFormat format
,
170 PP_Bool init_to_zero
) {
171 PluginDispatcher
* dispatcher
= PluginDispatcher::GetForInstance(instance
);
176 std::string image_data_desc
;
177 ImageHandle image_handle
= ImageData::NullHandle
;
178 dispatcher
->Send(new PpapiHostMsg_ResourceCreation_ImageData(
179 API_ID_RESOURCE_CREATION
, instance
, format
, size
, init_to_zero
,
180 &result
, &image_data_desc
, &image_handle
));
182 if (result
.is_null() || image_data_desc
.size() != sizeof(PP_ImageDataDesc
))
185 // We serialize the PP_ImageDataDesc just by copying to a string.
186 PP_ImageDataDesc desc
;
187 memcpy(&desc
, image_data_desc
.data(), sizeof(PP_ImageDataDesc
));
189 return (new ImageData(result
, desc
, image_handle
))->GetReference();
192 PP_Resource
ResourceCreationProxy::CreateKeyboardInputEvent(
193 PP_Instance instance
,
194 PP_InputEvent_Type type
,
195 PP_TimeTicks time_stamp
,
198 struct PP_Var character_text
) {
199 if (type
!= PP_INPUTEVENT_TYPE_RAWKEYDOWN
&&
200 type
!= PP_INPUTEVENT_TYPE_KEYDOWN
&&
201 type
!= PP_INPUTEVENT_TYPE_KEYUP
&&
202 type
!= PP_INPUTEVENT_TYPE_CHAR
)
204 ppapi::InputEventData data
;
205 data
.event_type
= type
;
206 data
.event_time_stamp
= time_stamp
;
207 data
.event_modifiers
= modifiers
;
208 data
.key_code
= key_code
;
209 if (character_text
.type
== PP_VARTYPE_STRING
) {
210 StringVar
* text_str
= StringVar::FromPPVar(character_text
);
213 data
.character_text
= text_str
->value();
216 return (new InputEventImpl(InputEventImpl::InitAsProxy(),
217 instance
, data
))->GetReference();
220 PP_Resource
ResourceCreationProxy::CreateMouseInputEvent(
221 PP_Instance instance
,
222 PP_InputEvent_Type type
,
223 PP_TimeTicks time_stamp
,
225 PP_InputEvent_MouseButton mouse_button
,
226 const PP_Point
* mouse_position
,
228 const PP_Point
* mouse_movement
) {
229 if (type
!= PP_INPUTEVENT_TYPE_MOUSEDOWN
&&
230 type
!= PP_INPUTEVENT_TYPE_MOUSEUP
&&
231 type
!= PP_INPUTEVENT_TYPE_MOUSEMOVE
&&
232 type
!= PP_INPUTEVENT_TYPE_MOUSEENTER
&&
233 type
!= PP_INPUTEVENT_TYPE_MOUSELEAVE
)
236 ppapi::InputEventData data
;
237 data
.event_type
= type
;
238 data
.event_time_stamp
= time_stamp
;
239 data
.event_modifiers
= modifiers
;
240 data
.mouse_button
= mouse_button
;
241 data
.mouse_position
= *mouse_position
;
242 data
.mouse_click_count
= click_count
;
243 data
.mouse_movement
= *mouse_movement
;
245 return (new InputEventImpl(InputEventImpl::InitAsProxy(),
246 instance
, data
))->GetReference();
249 PP_Resource
ResourceCreationProxy::CreateGraphics3D(
250 PP_Instance instance
,
251 PP_Resource share_context
,
252 const int32_t* attrib_list
) {
253 return PPB_Graphics3D_Proxy::CreateProxyResource(
254 instance
, share_context
, attrib_list
);
257 PP_Resource
ResourceCreationProxy::CreateGraphics3DRaw(
258 PP_Instance instance
,
259 PP_Resource share_context
,
260 const int32_t* attrib_list
) {
261 // Not proxied. The raw creation function is used only in the implementation
262 // of the proxy on the host side.
266 PP_Resource
ResourceCreationProxy::CreateScrollbar(PP_Instance instance
,
268 NOTIMPLEMENTED(); // Not proxied yet.
272 PP_Resource
ResourceCreationProxy::CreateTCPSocketPrivate(
273 PP_Instance instance
) {
274 return PPB_TCPSocket_Private_Proxy::CreateProxyResource(instance
);
277 PP_Resource
ResourceCreationProxy::CreateTransport(PP_Instance instance
,
279 PP_TransportType type
) {
280 NOTIMPLEMENTED(); // Not proxied yet.
284 PP_Resource
ResourceCreationProxy::CreateUDPSocketPrivate(
285 PP_Instance instance
) {
286 return PPB_UDPSocket_Private_Proxy::CreateProxyResource(instance
);
289 PP_Resource
ResourceCreationProxy::CreateURLLoader(PP_Instance instance
) {
290 return PPB_URLLoader_Proxy::CreateProxyResource(instance
);
293 PP_Resource
ResourceCreationProxy::CreateURLRequestInfo(
294 PP_Instance instance
,
295 const PPB_URLRequestInfo_Data
& data
) {
296 return (new URLRequestInfoImpl(
297 HostResource::MakeInstanceOnly(instance
), data
))->GetReference();
300 PP_Resource
ResourceCreationProxy::CreateVideoCapture(PP_Instance instance
) {
301 return PPB_VideoCapture_Proxy::CreateProxyResource(instance
);
304 PP_Resource
ResourceCreationProxy::CreateVideoDecoder(
305 PP_Instance instance
,
306 PP_Resource context3d_id
,
307 PP_VideoDecoder_Profile profile
) {
308 return PPB_VideoDecoder_Proxy::CreateProxyResource(
309 instance
, context3d_id
, profile
);
312 PP_Resource
ResourceCreationProxy::CreateVideoLayer(
313 PP_Instance instance
,
314 PP_VideoLayerMode_Dev mode
) {
319 PP_Resource
ResourceCreationProxy::CreateWebSocket(PP_Instance instance
) {
324 PP_Resource
ResourceCreationProxy::CreateWheelInputEvent(
325 PP_Instance instance
,
326 PP_TimeTicks time_stamp
,
328 const PP_FloatPoint
* wheel_delta
,
329 const PP_FloatPoint
* wheel_ticks
,
330 PP_Bool scroll_by_page
) {
331 ppapi::InputEventData data
;
332 data
.event_type
= PP_INPUTEVENT_TYPE_WHEEL
;
333 data
.event_time_stamp
= time_stamp
;
334 data
.event_modifiers
= modifiers
;
335 data
.wheel_delta
= *wheel_delta
;
336 data
.wheel_ticks
= *wheel_ticks
;
337 data
.wheel_scroll_by_page
= PP_ToBool(scroll_by_page
);
339 return (new InputEventImpl(InputEventImpl::InitAsProxy(),
340 instance
, data
))->GetReference();
343 bool ResourceCreationProxy::Send(IPC::Message
* msg
) {
344 return dispatcher()->Send(msg
);
347 bool ResourceCreationProxy::OnMessageReceived(const IPC::Message
& msg
) {
349 IPC_BEGIN_MESSAGE_MAP(ResourceCreationProxy
, msg
)
350 IPC_MESSAGE_HANDLER(PpapiHostMsg_ResourceCreation_Graphics2D
,
351 OnMsgCreateGraphics2D
)
352 IPC_MESSAGE_HANDLER(PpapiHostMsg_ResourceCreation_ImageData
,
353 OnMsgCreateImageData
)
354 IPC_MESSAGE_UNHANDLED(handled
= false)
355 IPC_END_MESSAGE_MAP()
359 void ResourceCreationProxy::OnMsgCreateGraphics2D(PP_Instance instance
,
361 PP_Bool is_always_opaque
,
362 HostResource
* result
) {
363 ppapi::thunk::EnterFunction
<ResourceCreationAPI
> enter(instance
, false);
364 if (enter
.succeeded()) {
365 result
->SetHostResource(instance
, enter
.functions()->CreateGraphics2D(
366 instance
, size
, is_always_opaque
));
370 void ResourceCreationProxy::OnMsgCreateImageData(
371 PP_Instance instance
,
374 PP_Bool init_to_zero
,
375 HostResource
* result
,
376 std::string
* image_data_desc
,
377 ImageHandle
* result_image_handle
) {
378 *result_image_handle
= ImageData::NullHandle
;
380 ppapi::thunk::EnterFunction
<ResourceCreationAPI
> enter(instance
, false);
384 PP_Resource resource
= enter
.functions()->CreateImageData(
385 instance
, static_cast<PP_ImageDataFormat
>(format
), size
, init_to_zero
);
388 result
->SetHostResource(instance
, resource
);
390 // Get the description, it's just serialized as a string.
391 ppapi::thunk::EnterResourceNoLock
<ppapi::thunk::PPB_ImageData_API
>
392 enter_resource(resource
, false);
393 PP_ImageDataDesc desc
;
394 if (enter_resource
.object()->Describe(&desc
) == PP_TRUE
) {
395 image_data_desc
->resize(sizeof(PP_ImageDataDesc
));
396 memcpy(&(*image_data_desc
)[0], &desc
, sizeof(PP_ImageDataDesc
));
399 // Get the shared memory handle.
400 const PPB_ImageDataTrusted
* trusted
=
401 reinterpret_cast<const PPB_ImageDataTrusted
*>(
402 dispatcher()->local_get_interface()(PPB_IMAGEDATA_TRUSTED_INTERFACE
));
403 uint32_t byte_count
= 0;
406 if (trusted
->GetSharedMemory(resource
, &handle
, &byte_count
) == PP_OK
) {
408 ImageHandle ih
= ImageData::HandleFromInt(handle
);
409 *result_image_handle
= dispatcher()->ShareHandleWithRemote(ih
, false);
411 *result_image_handle
= ImageData::HandleFromInt(handle
);