Implement HasPermission() method in PermissionService.
[chromium-blink-merge.git] / content / renderer / pepper / content_renderer_pepper_host_factory.cc
blob24d48a50df9fa731f4f58b16945e9a8fd54c32ce
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/public/common/content_client.h"
10 #include "content/public/renderer/content_renderer_client.h"
11 #include "content/renderer/pepper/pepper_audio_input_host.h"
12 #include "content/renderer/pepper/pepper_compositor_host.h"
13 #include "content/renderer/pepper/pepper_file_chooser_host.h"
14 #include "content/renderer/pepper/pepper_file_ref_renderer_host.h"
15 #include "content/renderer/pepper/pepper_file_system_host.h"
16 #include "content/renderer/pepper/pepper_graphics_2d_host.h"
17 #include "content/renderer/pepper/pepper_media_stream_video_track_host.h"
18 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
19 #include "content/renderer/pepper/pepper_url_loader_host.h"
20 #include "content/renderer/pepper/pepper_video_capture_host.h"
21 #include "content/renderer/pepper/pepper_video_decoder_host.h"
22 #include "content/renderer/pepper/pepper_video_destination_host.h"
23 #include "content/renderer/pepper/pepper_video_source_host.h"
24 #include "content/renderer/pepper/pepper_websocket_host.h"
25 #include "content/renderer/pepper/ppb_image_data_impl.h"
26 #include "content/renderer/pepper/renderer_ppapi_host_impl.h"
27 #include "ppapi/host/resource_host.h"
28 #include "ppapi/proxy/ppapi_message_utils.h"
29 #include "ppapi/proxy/ppapi_messages.h"
30 #include "ppapi/proxy/serialized_structs.h"
31 #include "ppapi/shared_impl/ppb_image_data_shared.h"
32 #include "third_party/WebKit/public/platform/WebURL.h"
33 #include "third_party/WebKit/public/web/WebDocument.h"
34 #include "third_party/WebKit/public/web/WebElement.h"
35 #include "third_party/WebKit/public/web/WebPluginContainer.h"
37 #if defined(OS_WIN)
38 #include "base/command_line.h"
39 #include "base/win/windows_version.h"
40 #include "content/public/common/content_switches.h"
41 #endif
43 using ppapi::host::ResourceHost;
44 using ppapi::UnpackMessage;
46 namespace content {
48 namespace {
50 #if defined(ENABLE_WEBRTC)
51 bool CanUseMediaStreamAPI(const RendererPpapiHost* host, PP_Instance instance) {
52 blink::WebPluginContainer* container =
53 host->GetContainerForInstance(instance);
54 if (!container)
55 return false;
57 GURL document_url = container->element().document().url();
58 ContentRendererClient* content_renderer_client =
59 GetContentClient()->renderer();
60 return content_renderer_client->AllowPepperMediaStreamAPI(document_url);
62 #endif // defined(ENABLE_WEBRTC)
64 bool CanUseCompositorAPI(const RendererPpapiHost* host, PP_Instance instance) {
65 blink::WebPluginContainer* container =
66 host->GetContainerForInstance(instance);
67 if (!container)
68 return false;
70 GURL document_url = container->element().document().url();
71 ContentRendererClient* content_renderer_client =
72 GetContentClient()->renderer();
73 return content_renderer_client->IsPluginAllowedToUseCompositorAPI(
74 document_url);
77 bool CanUseVideoDecodeAPI(const RendererPpapiHost* host, PP_Instance instance) {
78 blink::WebPluginContainer* container =
79 host->GetContainerForInstance(instance);
80 if (!container)
81 return false;
83 GURL document_url = container->element().document().url();
84 ContentRendererClient* content_renderer_client =
85 GetContentClient()->renderer();
86 return content_renderer_client->IsPluginAllowedToUseVideoDecodeAPI(
87 document_url);
90 } // namespace
92 ContentRendererPepperHostFactory::ContentRendererPepperHostFactory(
93 RendererPpapiHostImpl* host)
94 : host_(host) {}
96 ContentRendererPepperHostFactory::~ContentRendererPepperHostFactory() {}
98 scoped_ptr<ResourceHost> ContentRendererPepperHostFactory::CreateResourceHost(
99 ppapi::host::PpapiHost* host,
100 PP_Resource resource,
101 PP_Instance instance,
102 const IPC::Message& message) {
103 DCHECK(host == host_->GetPpapiHost());
105 // Make sure the plugin is giving us a valid instance for this resource.
106 if (!host_->IsValidInstance(instance))
107 return scoped_ptr<ResourceHost>();
109 PepperPluginInstanceImpl* instance_impl =
110 host_->GetPluginInstanceImpl(instance);
111 if (!instance_impl->render_frame())
112 return scoped_ptr<ResourceHost>();
114 // Public interfaces.
115 switch (message.type()) {
116 case PpapiHostMsg_Compositor_Create::ID: {
117 if (!CanUseCompositorAPI(host_, instance))
118 return scoped_ptr<ResourceHost>();
119 return scoped_ptr<ResourceHost>(
120 new PepperCompositorHost(host_, instance, resource));
122 case PpapiHostMsg_FileRef_CreateForFileAPI::ID: {
123 PP_Resource file_system;
124 std::string internal_path;
125 if (!UnpackMessage<PpapiHostMsg_FileRef_CreateForFileAPI>(
126 message, &file_system, &internal_path)) {
127 NOTREACHED();
128 return scoped_ptr<ResourceHost>();
130 return scoped_ptr<ResourceHost>(new PepperFileRefRendererHost(
131 host_, instance, resource, file_system, internal_path));
133 case PpapiHostMsg_FileSystem_Create::ID: {
134 PP_FileSystemType file_system_type;
135 if (!UnpackMessage<PpapiHostMsg_FileSystem_Create>(message,
136 &file_system_type)) {
137 NOTREACHED();
138 return scoped_ptr<ResourceHost>();
140 return scoped_ptr<ResourceHost>(new PepperFileSystemHost(
141 host_, instance, resource, file_system_type));
143 case PpapiHostMsg_Graphics2D_Create::ID: {
144 PP_Size size;
145 PP_Bool is_always_opaque;
146 if (!UnpackMessage<PpapiHostMsg_Graphics2D_Create>(
147 message, &size, &is_always_opaque)) {
148 NOTREACHED();
149 return scoped_ptr<ResourceHost>();
151 ppapi::PPB_ImageData_Shared::ImageDataType image_type =
152 ppapi::PPB_ImageData_Shared::PLATFORM;
153 #if defined(OS_WIN)
154 // If Win32K lockdown mitigations are enabled for Windows 8 and beyond
155 // we use the SIMPLE image data type as the PLATFORM image data type
156 // calls GDI functions to create DIB sections etc which fail in Win32K
157 // lockdown mode.
158 // TODO(ananta)
159 // Look into whether this causes a loss of functionality. From cursory
160 // testing things seem to work well.
161 if (CommandLine::ForCurrentProcess()->HasSwitch(
162 switches::kEnableWin32kRendererLockDown) &&
163 base::win::GetVersion() >= base::win::VERSION_WIN8) {
164 image_type = ppapi::PPB_ImageData_Shared::SIMPLE;
166 #endif
167 scoped_refptr<PPB_ImageData_Impl> image_data(new PPB_ImageData_Impl(
168 instance, image_type));
169 return scoped_ptr<ResourceHost>(PepperGraphics2DHost::Create(
170 host_, instance, resource, size, is_always_opaque, image_data));
172 case PpapiHostMsg_URLLoader_Create::ID:
173 return scoped_ptr<ResourceHost>(
174 new PepperURLLoaderHost(host_, false, instance, resource));
175 case PpapiHostMsg_VideoDecoder_Create::ID: {
176 if (!CanUseVideoDecodeAPI(host_, instance))
177 return scoped_ptr<ResourceHost>();
178 return scoped_ptr<ResourceHost>(
179 new PepperVideoDecoderHost(host_, instance, resource));
181 case PpapiHostMsg_WebSocket_Create::ID:
182 return scoped_ptr<ResourceHost>(
183 new PepperWebSocketHost(host_, instance, resource));
184 #if defined(ENABLE_WEBRTC)
185 case PpapiHostMsg_MediaStreamVideoTrack_Create::ID:
186 return scoped_ptr<ResourceHost>(
187 new PepperMediaStreamVideoTrackHost(host_, instance, resource));
188 // These private MediaStream interfaces are exposed as if they were public
189 // so they can be used by NaCl plugins. However, they are available only
190 // for whitelisted apps.
191 case PpapiHostMsg_VideoDestination_Create::ID:
192 if (CanUseMediaStreamAPI(host_, instance))
193 return scoped_ptr<ResourceHost>(
194 new PepperVideoDestinationHost(host_, instance, resource));
195 case PpapiHostMsg_VideoSource_Create::ID:
196 if (CanUseMediaStreamAPI(host_, instance))
197 return scoped_ptr<ResourceHost>(
198 new PepperVideoSourceHost(host_, instance, resource));
199 #endif // defined(ENABLE_WEBRTC)
202 // Dev interfaces.
203 if (GetPermissions().HasPermission(ppapi::PERMISSION_DEV)) {
204 switch (message.type()) {
205 case PpapiHostMsg_AudioInput_Create::ID:
206 return scoped_ptr<ResourceHost>(
207 new PepperAudioInputHost(host_, instance, resource));
208 case PpapiHostMsg_FileChooser_Create::ID:
209 return scoped_ptr<ResourceHost>(
210 new PepperFileChooserHost(host_, instance, resource));
211 case PpapiHostMsg_VideoCapture_Create::ID: {
212 PepperVideoCaptureHost* host =
213 new PepperVideoCaptureHost(host_, instance, resource);
214 if (!host->Init()) {
215 delete host;
216 return scoped_ptr<ResourceHost>();
218 return scoped_ptr<ResourceHost>(host);
223 return scoped_ptr<ResourceHost>();
226 const ppapi::PpapiPermissions&
227 ContentRendererPepperHostFactory::GetPermissions() const {
228 return host_->GetPpapiHost()->permissions();
231 } // namespace content