Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / pepper / content_renderer_pepper_host_factory.cc
blob0187d7d0f464f1f13d022ae94562d36ae7299c5f
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_camera_device_host.h"
14 #include "content/renderer/pepper/pepper_compositor_host.h"
15 #include "content/renderer/pepper/pepper_file_chooser_host.h"
16 #include "content/renderer/pepper/pepper_file_ref_renderer_host.h"
17 #include "content/renderer/pepper/pepper_file_system_host.h"
18 #include "content/renderer/pepper/pepper_graphics_2d_host.h"
19 #include "content/renderer/pepper/pepper_media_stream_video_track_host.h"
20 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
21 #include "content/renderer/pepper/pepper_url_loader_host.h"
22 #include "content/renderer/pepper/pepper_video_capture_host.h"
23 #include "content/renderer/pepper/pepper_video_decoder_host.h"
24 #include "content/renderer/pepper/pepper_video_destination_host.h"
25 #include "content/renderer/pepper/pepper_video_encoder_host.h"
26 #include "content/renderer/pepper/pepper_video_source_host.h"
27 #include "content/renderer/pepper/pepper_websocket_host.h"
28 #include "content/renderer/pepper/ppb_image_data_impl.h"
29 #include "content/renderer/pepper/renderer_ppapi_host_impl.h"
30 #include "ppapi/host/resource_host.h"
31 #include "ppapi/proxy/ppapi_message_utils.h"
32 #include "ppapi/proxy/ppapi_messages.h"
33 #include "ppapi/proxy/serialized_structs.h"
34 #include "ppapi/shared_impl/ppb_image_data_shared.h"
35 #include "third_party/WebKit/public/platform/WebURL.h"
36 #include "third_party/WebKit/public/web/WebDocument.h"
37 #include "third_party/WebKit/public/web/WebElement.h"
38 #include "third_party/WebKit/public/web/WebPluginContainer.h"
40 #if defined(OS_WIN)
41 #include "base/win/windows_version.h"
42 #endif
44 using ppapi::host::ResourceHost;
45 using ppapi::UnpackMessage;
47 namespace content {
49 namespace {
51 #if defined(ENABLE_WEBRTC)
52 bool CanUseMediaStreamAPI(const RendererPpapiHost* host, PP_Instance instance) {
53 blink::WebPluginContainer* container =
54 host->GetContainerForInstance(instance);
55 if (!container)
56 return false;
58 GURL document_url = container->element().document().url();
59 ContentRendererClient* content_renderer_client =
60 GetContentClient()->renderer();
61 return content_renderer_client->AllowPepperMediaStreamAPI(document_url);
63 #endif // defined(ENABLE_WEBRTC)
65 static bool CanUseCameraDeviceAPI(const RendererPpapiHost* host,
66 PP_Instance instance) {
67 blink::WebPluginContainer* container =
68 host->GetContainerForInstance(instance);
69 if (!container)
70 return false;
72 GURL document_url = container->element().document().url();
73 ContentRendererClient* content_renderer_client =
74 GetContentClient()->renderer();
75 return content_renderer_client->IsPluginAllowedToUseCameraDeviceAPI(
76 document_url);
79 bool CanUseCompositorAPI(const RendererPpapiHost* host, PP_Instance instance) {
80 blink::WebPluginContainer* container =
81 host->GetContainerForInstance(instance);
82 if (!container)
83 return false;
85 GURL document_url = container->element().document().url();
86 ContentRendererClient* content_renderer_client =
87 GetContentClient()->renderer();
88 return content_renderer_client->IsPluginAllowedToUseCompositorAPI(
89 document_url);
92 } // namespace
94 ContentRendererPepperHostFactory::ContentRendererPepperHostFactory(
95 RendererPpapiHostImpl* host)
96 : host_(host) {}
98 ContentRendererPepperHostFactory::~ContentRendererPepperHostFactory() {}
100 scoped_ptr<ResourceHost> ContentRendererPepperHostFactory::CreateResourceHost(
101 ppapi::host::PpapiHost* host,
102 PP_Resource resource,
103 PP_Instance instance,
104 const IPC::Message& message) {
105 DCHECK(host == host_->GetPpapiHost());
107 // Make sure the plugin is giving us a valid instance for this resource.
108 if (!host_->IsValidInstance(instance))
109 return scoped_ptr<ResourceHost>();
111 PepperPluginInstanceImpl* instance_impl =
112 host_->GetPluginInstanceImpl(instance);
113 if (!instance_impl->render_frame())
114 return scoped_ptr<ResourceHost>();
116 // Public interfaces.
117 switch (message.type()) {
118 case PpapiHostMsg_Compositor_Create::ID: {
119 if (!CanUseCompositorAPI(host_, instance))
120 return scoped_ptr<ResourceHost>();
121 return scoped_ptr<ResourceHost>(
122 new PepperCompositorHost(host_, instance, resource));
124 case PpapiHostMsg_FileRef_CreateForFileAPI::ID: {
125 PP_Resource file_system;
126 std::string internal_path;
127 if (!UnpackMessage<PpapiHostMsg_FileRef_CreateForFileAPI>(
128 message, &file_system, &internal_path)) {
129 NOTREACHED();
130 return scoped_ptr<ResourceHost>();
132 return scoped_ptr<ResourceHost>(new PepperFileRefRendererHost(
133 host_, instance, resource, file_system, internal_path));
135 case PpapiHostMsg_FileSystem_Create::ID: {
136 PP_FileSystemType file_system_type;
137 if (!UnpackMessage<PpapiHostMsg_FileSystem_Create>(message,
138 &file_system_type)) {
139 NOTREACHED();
140 return scoped_ptr<ResourceHost>();
142 return scoped_ptr<ResourceHost>(new PepperFileSystemHost(
143 host_, instance, resource, file_system_type));
145 case PpapiHostMsg_Graphics2D_Create::ID: {
146 PP_Size size;
147 PP_Bool is_always_opaque;
148 if (!UnpackMessage<PpapiHostMsg_Graphics2D_Create>(
149 message, &size, &is_always_opaque)) {
150 NOTREACHED();
151 return scoped_ptr<ResourceHost>();
153 ppapi::PPB_ImageData_Shared::ImageDataType image_type =
154 ppapi::PPB_ImageData_Shared::PLATFORM;
155 #if defined(OS_WIN)
156 // If Win32K lockdown mitigations are enabled for Windows 8 and beyond
157 // we use the SIMPLE image data type as the PLATFORM image data type
158 // calls GDI functions to create DIB sections etc which fail in Win32K
159 // lockdown mode.
160 // TODO(ananta)
161 // Look into whether this causes a loss of functionality. From cursory
162 // testing things seem to work well.
163 if (IsWin32kRendererLockdownEnabled())
164 image_type = ppapi::PPB_ImageData_Shared::SIMPLE;
165 #endif
166 scoped_refptr<PPB_ImageData_Impl> image_data(new PPB_ImageData_Impl(
167 instance, image_type));
168 return scoped_ptr<ResourceHost>(PepperGraphics2DHost::Create(
169 host_, instance, resource, size, is_always_opaque, image_data));
171 case PpapiHostMsg_URLLoader_Create::ID:
172 return scoped_ptr<ResourceHost>(
173 new PepperURLLoaderHost(host_, false, instance, resource));
174 case PpapiHostMsg_VideoDecoder_Create::ID:
175 return scoped_ptr<ResourceHost>(
176 new PepperVideoDecoderHost(host_, instance, resource));
177 case PpapiHostMsg_VideoEncoder_Create::ID:
178 return scoped_ptr<ResourceHost>(
179 new PepperVideoEncoderHost(host_, instance, resource));
180 case PpapiHostMsg_WebSocket_Create::ID:
181 return scoped_ptr<ResourceHost>(
182 new PepperWebSocketHost(host_, instance, resource));
183 #if defined(ENABLE_WEBRTC)
184 case PpapiHostMsg_MediaStreamVideoTrack_Create::ID:
185 return scoped_ptr<ResourceHost>(
186 new PepperMediaStreamVideoTrackHost(host_, instance, resource));
187 // These private MediaStream interfaces are exposed as if they were public
188 // so they can be used by NaCl plugins. However, they are available only
189 // for whitelisted apps.
190 case PpapiHostMsg_VideoDestination_Create::ID:
191 if (CanUseMediaStreamAPI(host_, instance))
192 return scoped_ptr<ResourceHost>(
193 new PepperVideoDestinationHost(host_, instance, resource));
194 case PpapiHostMsg_VideoSource_Create::ID:
195 if (CanUseMediaStreamAPI(host_, instance))
196 return scoped_ptr<ResourceHost>(
197 new PepperVideoSourceHost(host_, instance, resource));
198 #endif // defined(ENABLE_WEBRTC)
201 // Dev interfaces.
202 if (GetPermissions().HasPermission(ppapi::PERMISSION_DEV)) {
203 switch (message.type()) {
204 case PpapiHostMsg_AudioInput_Create::ID:
205 return scoped_ptr<ResourceHost>(
206 new PepperAudioInputHost(host_, instance, resource));
207 case PpapiHostMsg_FileChooser_Create::ID:
208 return scoped_ptr<ResourceHost>(
209 new PepperFileChooserHost(host_, instance, resource));
210 case PpapiHostMsg_VideoCapture_Create::ID: {
211 PepperVideoCaptureHost* host =
212 new PepperVideoCaptureHost(host_, instance, resource);
213 if (!host->Init()) {
214 delete host;
215 return scoped_ptr<ResourceHost>();
217 return scoped_ptr<ResourceHost>(host);
222 // Permissions of the following interfaces are available for whitelisted apps
223 // which may not have access to the other private interfaces.
224 if (message.type() == PpapiHostMsg_CameraDevice_Create::ID) {
225 if (!GetPermissions().HasPermission(ppapi::PERMISSION_PRIVATE) &&
226 !CanUseCameraDeviceAPI(host_, instance))
227 return nullptr;
228 scoped_ptr<PepperCameraDeviceHost> host(
229 new PepperCameraDeviceHost(host_, instance, resource));
230 return host->Init() ? host.Pass() : nullptr;
233 return scoped_ptr<ResourceHost>();
236 const ppapi::PpapiPermissions&
237 ContentRendererPepperHostFactory::GetPermissions() const {
238 return host_->GetPpapiHost()->permissions();
241 } // namespace content