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