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 "content/renderer/pepper/content_renderer_pepper_host_factory.h" 6 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_truetype_font_host.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" 37 38using ppapi::host::ResourceHost; 39using ppapi::proxy::SerializedTrueTypeFontDesc; 40using ppapi::UnpackMessage; 41 42namespace content { 43 44#if defined(ENABLE_WEBRTC) 45namespace { 46 47bool CanUseMediaStreamAPI(const RendererPpapiHost* host, PP_Instance instance) { 48 blink::WebPluginContainer* container = 49 host->GetContainerForInstance(instance); 50 if (!container) 51 return false; 52 53 GURL document_url = container->element().document().url(); 54 ContentRendererClient* content_renderer_client = 55 GetContentClient()->renderer(); 56 return content_renderer_client->AllowPepperMediaStreamAPI(document_url); 57} 58 59} // namespace 60#endif // defined(ENABLE_WEBRTC) 61 62ContentRendererPepperHostFactory::ContentRendererPepperHostFactory( 63 RendererPpapiHostImpl* host) 64 : host_(host) {} 65 66ContentRendererPepperHostFactory::~ContentRendererPepperHostFactory() {} 67 68scoped_ptr<ResourceHost> ContentRendererPepperHostFactory::CreateResourceHost( 69 ppapi::host::PpapiHost* host, 70 const ppapi::proxy::ResourceMessageCallParams& params, 71 PP_Instance instance, 72 const IPC::Message& message) { 73 DCHECK(host == host_->GetPpapiHost()); 74 75 // Make sure the plugin is giving us a valid instance for this resource. 76 if (!host_->IsValidInstance(instance)) 77 return scoped_ptr<ResourceHost>(); 78 79 PepperPluginInstanceImpl* instance_impl = 80 host_->GetPluginInstanceImpl(instance); 81 if (!instance_impl->render_frame()) 82 return scoped_ptr<ResourceHost>(); 83 84 // Public interfaces. 85 switch (message.type()) { 86 case PpapiHostMsg_Compositor_Create::ID: { 87 return scoped_ptr<ResourceHost>( 88 new PepperCompositorHost(host_, instance, params.pp_resource())); 89 } 90 case PpapiHostMsg_FileRef_CreateForFileAPI::ID: { 91 PP_Resource file_system; 92 std::string internal_path; 93 if (!UnpackMessage<PpapiHostMsg_FileRef_CreateForFileAPI>( 94 message, &file_system, &internal_path)) { 95 NOTREACHED(); 96 return scoped_ptr<ResourceHost>(); 97 } 98 return scoped_ptr<ResourceHost>(new PepperFileRefRendererHost( 99 host_, instance, params.pp_resource(), file_system, internal_path)); 100 } 101 case PpapiHostMsg_FileSystem_Create::ID: { 102 PP_FileSystemType file_system_type; 103 if (!UnpackMessage<PpapiHostMsg_FileSystem_Create>(message, 104 &file_system_type)) { 105 NOTREACHED(); 106 return scoped_ptr<ResourceHost>(); 107 } 108 return scoped_ptr<ResourceHost>(new PepperFileSystemHost( 109 host_, instance, params.pp_resource(), file_system_type)); 110 } 111 case PpapiHostMsg_Graphics2D_Create::ID: { 112 PP_Size size; 113 PP_Bool is_always_opaque; 114 if (!UnpackMessage<PpapiHostMsg_Graphics2D_Create>( 115 message, &size, &is_always_opaque)) { 116 NOTREACHED(); 117 return scoped_ptr<ResourceHost>(); 118 } 119 scoped_refptr<PPB_ImageData_Impl> image_data(new PPB_ImageData_Impl( 120 instance, ppapi::PPB_ImageData_Shared::PLATFORM)); 121 return scoped_ptr<ResourceHost>( 122 PepperGraphics2DHost::Create(host_, 123 instance, 124 params.pp_resource(), 125 size, 126 is_always_opaque, 127 image_data)); 128 } 129 case PpapiHostMsg_URLLoader_Create::ID: 130 return scoped_ptr<ResourceHost>(new PepperURLLoaderHost( 131 host_, false, instance, params.pp_resource())); 132 case PpapiHostMsg_VideoDecoder_Create::ID: 133 return scoped_ptr<ResourceHost>( 134 new PepperVideoDecoderHost(host_, instance, params.pp_resource())); 135 case PpapiHostMsg_WebSocket_Create::ID: 136 return scoped_ptr<ResourceHost>( 137 new PepperWebSocketHost(host_, instance, params.pp_resource())); 138#if defined(ENABLE_WEBRTC) 139 case PpapiHostMsg_MediaStreamVideoTrack_Create::ID: 140 return scoped_ptr<ResourceHost>(new PepperMediaStreamVideoTrackHost( 141 host_, instance, params.pp_resource())); 142 // These private MediaStream interfaces are exposed as if they were public 143 // so they can be used by NaCl plugins. However, they are available only 144 // for whitelisted apps. 145 case PpapiHostMsg_VideoDestination_Create::ID: 146 if (CanUseMediaStreamAPI(host_, instance)) 147 return scoped_ptr<ResourceHost>(new PepperVideoDestinationHost( 148 host_, instance, params.pp_resource())); 149 case PpapiHostMsg_VideoSource_Create::ID: 150 if (CanUseMediaStreamAPI(host_, instance)) 151 return scoped_ptr<ResourceHost>( 152 new PepperVideoSourceHost(host_, instance, params.pp_resource())); 153#endif // defined(ENABLE_WEBRTC) 154 } 155 156 // Dev interfaces. 157 if (GetPermissions().HasPermission(ppapi::PERMISSION_DEV)) { 158 switch (message.type()) { 159 case PpapiHostMsg_AudioInput_Create::ID: 160 return scoped_ptr<ResourceHost>( 161 new PepperAudioInputHost(host_, instance, params.pp_resource())); 162 case PpapiHostMsg_FileChooser_Create::ID: 163 return scoped_ptr<ResourceHost>( 164 new PepperFileChooserHost(host_, instance, params.pp_resource())); 165 case PpapiHostMsg_TrueTypeFont_Create::ID: { 166 SerializedTrueTypeFontDesc desc; 167 if (!UnpackMessage<PpapiHostMsg_TrueTypeFont_Create>(message, &desc)) { 168 NOTREACHED(); 169 return scoped_ptr<ResourceHost>(); 170 } 171 // Check that the family name is valid UTF-8 before passing it to the 172 // host OS. 173 if (base::IsStringUTF8(desc.family)) { 174 return scoped_ptr<ResourceHost>(new PepperTrueTypeFontHost( 175 host_, instance, params.pp_resource(), desc)); 176 } 177 break; // Drop through and return null host. 178 } 179 case PpapiHostMsg_VideoCapture_Create::ID: { 180 PepperVideoCaptureHost* host = 181 new PepperVideoCaptureHost(host_, instance, params.pp_resource()); 182 if (!host->Init()) { 183 delete host; 184 return scoped_ptr<ResourceHost>(); 185 } 186 return scoped_ptr<ResourceHost>(host); 187 } 188 } 189 } 190 191 return scoped_ptr<ResourceHost>(); 192} 193 194const ppapi::PpapiPermissions& 195ContentRendererPepperHostFactory::GetPermissions() const { 196 return host_->GetPpapiHost()->permissions(); 197} 198 199} // namespace content 200