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 "content/renderer/pepper/content_renderer_pepper_host_factory.h"
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "content/common/content_switches_internal.h"
10 #include "content/public/common/content_client.h"
11 #include "content/public/renderer/content_renderer_client.h"
12 #include "content/renderer/pepper/pepper_audio_input_host.h"
13 #include "content/renderer/pepper/pepper_compositor_host.h"
14 #include "content/renderer/pepper/pepper_file_chooser_host.h"
15 #include "content/renderer/pepper/pepper_file_ref_renderer_host.h"
16 #include "content/renderer/pepper/pepper_file_system_host.h"
17 #include "content/renderer/pepper/pepper_graphics_2d_host.h"
18 #include "content/renderer/pepper/pepper_media_stream_video_track_host.h"
19 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
20 #include "content/renderer/pepper/pepper_url_loader_host.h"
21 #include "content/renderer/pepper/pepper_video_capture_host.h"
22 #include "content/renderer/pepper/pepper_video_decoder_host.h"
23 #include "content/renderer/pepper/pepper_video_destination_host.h"
24 #include "content/renderer/pepper/pepper_video_source_host.h"
25 #include "content/renderer/pepper/pepper_websocket_host.h"
26 #include "content/renderer/pepper/ppb_image_data_impl.h"
27 #include "content/renderer/pepper/renderer_ppapi_host_impl.h"
28 #include "ppapi/host/resource_host.h"
29 #include "ppapi/proxy/ppapi_message_utils.h"
30 #include "ppapi/proxy/ppapi_messages.h"
31 #include "ppapi/proxy/serialized_structs.h"
32 #include "ppapi/shared_impl/ppb_image_data_shared.h"
33 #include "third_party/WebKit/public/platform/WebURL.h"
34 #include "third_party/WebKit/public/web/WebDocument.h"
35 #include "third_party/WebKit/public/web/WebElement.h"
36 #include "third_party/WebKit/public/web/WebPluginContainer.h"
39 #include "base/win/windows_version.h"
42 using ppapi::host::ResourceHost
;
43 using ppapi::UnpackMessage
;
49 #if defined(ENABLE_WEBRTC)
50 bool CanUseMediaStreamAPI(const RendererPpapiHost
* host
, PP_Instance instance
) {
51 blink::WebPluginContainer
* container
=
52 host
->GetContainerForInstance(instance
);
56 GURL document_url
= container
->element().document().url();
57 ContentRendererClient
* content_renderer_client
=
58 GetContentClient()->renderer();
59 return content_renderer_client
->AllowPepperMediaStreamAPI(document_url
);
61 #endif // defined(ENABLE_WEBRTC)
63 bool CanUseCompositorAPI(const RendererPpapiHost
* host
, PP_Instance instance
) {
64 blink::WebPluginContainer
* container
=
65 host
->GetContainerForInstance(instance
);
69 GURL document_url
= container
->element().document().url();
70 ContentRendererClient
* content_renderer_client
=
71 GetContentClient()->renderer();
72 return content_renderer_client
->IsPluginAllowedToUseCompositorAPI(
78 ContentRendererPepperHostFactory::ContentRendererPepperHostFactory(
79 RendererPpapiHostImpl
* host
)
82 ContentRendererPepperHostFactory::~ContentRendererPepperHostFactory() {}
84 scoped_ptr
<ResourceHost
> ContentRendererPepperHostFactory::CreateResourceHost(
85 ppapi::host::PpapiHost
* host
,
88 const IPC::Message
& message
) {
89 DCHECK(host
== host_
->GetPpapiHost());
91 // Make sure the plugin is giving us a valid instance for this resource.
92 if (!host_
->IsValidInstance(instance
))
93 return scoped_ptr
<ResourceHost
>();
95 PepperPluginInstanceImpl
* instance_impl
=
96 host_
->GetPluginInstanceImpl(instance
);
97 if (!instance_impl
->render_frame())
98 return scoped_ptr
<ResourceHost
>();
100 // Public interfaces.
101 switch (message
.type()) {
102 case PpapiHostMsg_Compositor_Create::ID
: {
103 if (!CanUseCompositorAPI(host_
, instance
))
104 return scoped_ptr
<ResourceHost
>();
105 return scoped_ptr
<ResourceHost
>(
106 new PepperCompositorHost(host_
, instance
, resource
));
108 case PpapiHostMsg_FileRef_CreateForFileAPI::ID
: {
109 PP_Resource file_system
;
110 std::string internal_path
;
111 if (!UnpackMessage
<PpapiHostMsg_FileRef_CreateForFileAPI
>(
112 message
, &file_system
, &internal_path
)) {
114 return scoped_ptr
<ResourceHost
>();
116 return scoped_ptr
<ResourceHost
>(new PepperFileRefRendererHost(
117 host_
, instance
, resource
, file_system
, internal_path
));
119 case PpapiHostMsg_FileSystem_Create::ID
: {
120 PP_FileSystemType file_system_type
;
121 if (!UnpackMessage
<PpapiHostMsg_FileSystem_Create
>(message
,
122 &file_system_type
)) {
124 return scoped_ptr
<ResourceHost
>();
126 return scoped_ptr
<ResourceHost
>(new PepperFileSystemHost(
127 host_
, instance
, resource
, file_system_type
));
129 case PpapiHostMsg_Graphics2D_Create::ID
: {
131 PP_Bool is_always_opaque
;
132 if (!UnpackMessage
<PpapiHostMsg_Graphics2D_Create
>(
133 message
, &size
, &is_always_opaque
)) {
135 return scoped_ptr
<ResourceHost
>();
137 ppapi::PPB_ImageData_Shared::ImageDataType image_type
=
138 ppapi::PPB_ImageData_Shared::PLATFORM
;
140 // If Win32K lockdown mitigations are enabled for Windows 8 and beyond
141 // we use the SIMPLE image data type as the PLATFORM image data type
142 // calls GDI functions to create DIB sections etc which fail in Win32K
145 // Look into whether this causes a loss of functionality. From cursory
146 // testing things seem to work well.
147 if (IsWin32kRendererLockdownEnabled())
148 image_type
= ppapi::PPB_ImageData_Shared::SIMPLE
;
150 scoped_refptr
<PPB_ImageData_Impl
> image_data(new PPB_ImageData_Impl(
151 instance
, image_type
));
152 return scoped_ptr
<ResourceHost
>(PepperGraphics2DHost::Create(
153 host_
, instance
, resource
, size
, is_always_opaque
, image_data
));
155 case PpapiHostMsg_URLLoader_Create::ID
:
156 return scoped_ptr
<ResourceHost
>(
157 new PepperURLLoaderHost(host_
, false, instance
, resource
));
158 case PpapiHostMsg_VideoDecoder_Create::ID
:
159 return scoped_ptr
<ResourceHost
>(
160 new PepperVideoDecoderHost(host_
, instance
, resource
));
161 case PpapiHostMsg_WebSocket_Create::ID
:
162 return scoped_ptr
<ResourceHost
>(
163 new PepperWebSocketHost(host_
, instance
, resource
));
164 #if defined(ENABLE_WEBRTC)
165 case PpapiHostMsg_MediaStreamVideoTrack_Create::ID
:
166 return scoped_ptr
<ResourceHost
>(
167 new PepperMediaStreamVideoTrackHost(host_
, instance
, resource
));
168 // These private MediaStream interfaces are exposed as if they were public
169 // so they can be used by NaCl plugins. However, they are available only
170 // for whitelisted apps.
171 case PpapiHostMsg_VideoDestination_Create::ID
:
172 if (CanUseMediaStreamAPI(host_
, instance
))
173 return scoped_ptr
<ResourceHost
>(
174 new PepperVideoDestinationHost(host_
, instance
, resource
));
175 case PpapiHostMsg_VideoSource_Create::ID
:
176 if (CanUseMediaStreamAPI(host_
, instance
))
177 return scoped_ptr
<ResourceHost
>(
178 new PepperVideoSourceHost(host_
, instance
, resource
));
179 #endif // defined(ENABLE_WEBRTC)
183 if (GetPermissions().HasPermission(ppapi::PERMISSION_DEV
)) {
184 switch (message
.type()) {
185 case PpapiHostMsg_AudioInput_Create::ID
:
186 return scoped_ptr
<ResourceHost
>(
187 new PepperAudioInputHost(host_
, instance
, resource
));
188 case PpapiHostMsg_FileChooser_Create::ID
:
189 return scoped_ptr
<ResourceHost
>(
190 new PepperFileChooserHost(host_
, instance
, resource
));
191 case PpapiHostMsg_VideoCapture_Create::ID
: {
192 PepperVideoCaptureHost
* host
=
193 new PepperVideoCaptureHost(host_
, instance
, resource
);
196 return scoped_ptr
<ResourceHost
>();
198 return scoped_ptr
<ResourceHost
>(host
);
203 return scoped_ptr
<ResourceHost
>();
206 const ppapi::PpapiPermissions
&
207 ContentRendererPepperHostFactory::GetPermissions() const {
208 return host_
->GetPpapiHost()->permissions();
211 } // namespace content