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