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/browser/renderer_host/pepper/content_browser_pepper_host_factory.h"
7 #include "content/browser/renderer_host/pepper/browser_ppapi_host_impl.h"
8 #include "content/browser/renderer_host/pepper/pepper_browser_font_singleton_host.h"
9 #include "content/browser/renderer_host/pepper/pepper_file_io_host.h"
10 #include "content/browser/renderer_host/pepper/pepper_file_ref_host.h"
11 #include "content/browser/renderer_host/pepper/pepper_file_system_browser_host.h"
12 #include "content/browser/renderer_host/pepper/pepper_flash_file_message_filter.h"
13 #include "content/browser/renderer_host/pepper/pepper_gamepad_host.h"
14 #include "content/browser/renderer_host/pepper/pepper_host_resolver_message_filter.h"
15 #include "content/browser/renderer_host/pepper/pepper_network_monitor_host.h"
16 #include "content/browser/renderer_host/pepper/pepper_network_proxy_host.h"
17 #include "content/browser/renderer_host/pepper/pepper_print_settings_manager.h"
18 #include "content/browser/renderer_host/pepper/pepper_printing_host.h"
19 #include "content/browser/renderer_host/pepper/pepper_tcp_server_socket_message_filter.h"
20 #include "content/browser/renderer_host/pepper/pepper_tcp_socket_message_filter.h"
21 #include "content/browser/renderer_host/pepper/pepper_truetype_font_host.h"
22 #include "content/browser/renderer_host/pepper/pepper_truetype_font_list_host.h"
23 #include "content/browser/renderer_host/pepper/pepper_udp_socket_message_filter.h"
24 #include "ppapi/host/message_filter_host.h"
25 #include "ppapi/host/ppapi_host.h"
26 #include "ppapi/host/resource_host.h"
27 #include "ppapi/proxy/ppapi_messages.h"
28 #include "ppapi/shared_impl/ppapi_permissions.h"
30 using ppapi::host::MessageFilterHost
;
31 using ppapi::host::ResourceHost
;
32 using ppapi::host::ResourceMessageFilter
;
33 using ppapi::proxy::SerializedTrueTypeFontDesc
;
34 using ppapi::UnpackMessage
;
40 const size_t kMaxSocketsAllowed
= 1024;
42 bool CanCreateSocket() {
43 return PepperTCPServerSocketMessageFilter::GetNumInstances() +
44 PepperTCPSocketMessageFilter::GetNumInstances() +
45 PepperUDPSocketMessageFilter::GetNumInstances() <
51 ContentBrowserPepperHostFactory::ContentBrowserPepperHostFactory(
52 BrowserPpapiHostImpl
* host
)
55 ContentBrowserPepperHostFactory::~ContentBrowserPepperHostFactory() {}
57 scoped_ptr
<ResourceHost
> ContentBrowserPepperHostFactory::CreateResourceHost(
58 ppapi::host::PpapiHost
* host
,
61 const IPC::Message
& message
) {
62 DCHECK(host
== host_
->GetPpapiHost());
64 // Make sure the plugin is giving us a valid instance for this resource.
65 if (!host_
->IsValidInstance(instance
))
66 return scoped_ptr
<ResourceHost
>();
69 switch (message
.type()) {
70 case PpapiHostMsg_FileIO_Create::ID
: {
71 return scoped_ptr
<ResourceHost
>(
72 new PepperFileIOHost(host_
, instance
, resource
));
74 case PpapiHostMsg_FileSystem_Create::ID
: {
75 PP_FileSystemType file_system_type
;
76 if (!ppapi::UnpackMessage
<PpapiHostMsg_FileSystem_Create
>(
77 message
, &file_system_type
)) {
79 return scoped_ptr
<ResourceHost
>();
81 return scoped_ptr
<ResourceHost
>(new PepperFileSystemBrowserHost(
82 host_
, instance
, resource
, file_system_type
));
84 case PpapiHostMsg_Gamepad_Create::ID
: {
85 return scoped_ptr
<ResourceHost
>(
86 new PepperGamepadHost(host_
, instance
, resource
));
88 case PpapiHostMsg_NetworkProxy_Create::ID
: {
89 return scoped_ptr
<ResourceHost
>(
90 new PepperNetworkProxyHost(host_
, instance
, resource
));
92 case PpapiHostMsg_HostResolver_Create::ID
: {
93 scoped_refptr
<ResourceMessageFilter
> host_resolver(
94 new PepperHostResolverMessageFilter(host_
, instance
, false));
95 return scoped_ptr
<ResourceHost
>(new MessageFilterHost(
96 host_
->GetPpapiHost(), instance
, resource
, host_resolver
));
98 case PpapiHostMsg_FileRef_CreateForFileAPI::ID
: {
99 PP_Resource file_system
;
100 std::string internal_path
;
101 if (!UnpackMessage
<PpapiHostMsg_FileRef_CreateForFileAPI
>(
102 message
, &file_system
, &internal_path
)) {
104 return scoped_ptr
<ResourceHost
>();
106 return scoped_ptr
<ResourceHost
>(new PepperFileRefHost(
107 host_
, instance
, resource
, file_system
, internal_path
));
109 case PpapiHostMsg_TCPSocket_Create::ID
: {
110 ppapi::TCPSocketVersion version
;
111 if (!UnpackMessage
<PpapiHostMsg_TCPSocket_Create
>(message
, &version
) ||
112 version
== ppapi::TCP_SOCKET_VERSION_PRIVATE
) {
113 return scoped_ptr
<ResourceHost
>();
116 return CreateNewTCPSocket(instance
, resource
, version
);
118 case PpapiHostMsg_UDPSocket_Create::ID
: {
119 if (CanCreateSocket()) {
120 scoped_refptr
<ResourceMessageFilter
> udp_socket(
121 new PepperUDPSocketMessageFilter(host_
, instance
, false));
122 return scoped_ptr
<ResourceHost
>(new MessageFilterHost(
123 host_
->GetPpapiHost(), instance
, resource
, udp_socket
));
125 return scoped_ptr
<ResourceHost
>();
131 if (GetPermissions().HasPermission(ppapi::PERMISSION_DEV
)) {
132 switch (message
.type()) {
133 case PpapiHostMsg_Printing_Create::ID
: {
134 scoped_ptr
<PepperPrintSettingsManager
> manager(
135 new PepperPrintSettingsManagerImpl());
136 return scoped_ptr
<ResourceHost
>(new PepperPrintingHost(
137 host_
->GetPpapiHost(), instance
, resource
, manager
.Pass()));
139 case PpapiHostMsg_TrueTypeFont_Create::ID
: {
140 SerializedTrueTypeFontDesc desc
;
141 if (!UnpackMessage
<PpapiHostMsg_TrueTypeFont_Create
>(message
, &desc
)) {
143 return scoped_ptr
<ResourceHost
>();
145 // Check that the family name is valid UTF-8 before passing it to the
147 if (!base::IsStringUTF8(desc
.family
))
148 return scoped_ptr
<ResourceHost
>();
150 return scoped_ptr
<ResourceHost
>(
151 new PepperTrueTypeFontHost(host_
, instance
, resource
, desc
));
153 case PpapiHostMsg_TrueTypeFontSingleton_Create::ID
: {
154 return scoped_ptr
<ResourceHost
>(
155 new PepperTrueTypeFontListHost(host_
, instance
, resource
));
160 // Private interfaces.
161 if (GetPermissions().HasPermission(ppapi::PERMISSION_PRIVATE
)) {
162 switch (message
.type()) {
163 case PpapiHostMsg_BrowserFontSingleton_Create::ID
:
164 return scoped_ptr
<ResourceHost
>(
165 new PepperBrowserFontSingletonHost(host_
, instance
, resource
));
169 // Permissions for the following interfaces will be checked at the
170 // time of the corresponding instance's methods calls (because
171 // permission check can be performed only on the UI
172 // thread). Currently these interfaces are available only for
173 // whitelisted apps which may not have access to the other private
175 if (message
.type() == PpapiHostMsg_HostResolver_CreatePrivate::ID
) {
176 scoped_refptr
<ResourceMessageFilter
> host_resolver(
177 new PepperHostResolverMessageFilter(host_
, instance
, true));
178 return scoped_ptr
<ResourceHost
>(new MessageFilterHost(
179 host_
->GetPpapiHost(), instance
, resource
, host_resolver
));
181 if (message
.type() == PpapiHostMsg_TCPServerSocket_CreatePrivate::ID
) {
182 if (CanCreateSocket()) {
183 scoped_refptr
<ResourceMessageFilter
> tcp_server_socket(
184 new PepperTCPServerSocketMessageFilter(this, host_
, instance
, true));
185 return scoped_ptr
<ResourceHost
>(new MessageFilterHost(
186 host_
->GetPpapiHost(), instance
, resource
, tcp_server_socket
));
188 return scoped_ptr
<ResourceHost
>();
191 if (message
.type() == PpapiHostMsg_TCPSocket_CreatePrivate::ID
) {
192 return CreateNewTCPSocket(instance
, resource
,
193 ppapi::TCP_SOCKET_VERSION_PRIVATE
);
195 if (message
.type() == PpapiHostMsg_UDPSocket_CreatePrivate::ID
) {
196 if (CanCreateSocket()) {
197 scoped_refptr
<ResourceMessageFilter
> udp_socket(
198 new PepperUDPSocketMessageFilter(host_
, instance
, true));
199 return scoped_ptr
<ResourceHost
>(new MessageFilterHost(
200 host_
->GetPpapiHost(), instance
, resource
, udp_socket
));
202 return scoped_ptr
<ResourceHost
>();
205 if (message
.type() == PpapiHostMsg_NetworkMonitor_Create::ID
) {
206 return scoped_ptr
<ResourceHost
>(
207 new PepperNetworkMonitorHost(host_
, instance
, resource
));
211 if (GetPermissions().HasPermission(ppapi::PERMISSION_FLASH
)) {
212 switch (message
.type()) {
213 case PpapiHostMsg_FlashFile_Create::ID
: {
214 scoped_refptr
<ResourceMessageFilter
> file_filter(
215 new PepperFlashFileMessageFilter(instance
, host_
));
216 return scoped_ptr
<ResourceHost
>(new MessageFilterHost(
217 host_
->GetPpapiHost(), instance
, resource
, file_filter
));
222 return scoped_ptr
<ResourceHost
>();
225 scoped_ptr
<ppapi::host::ResourceHost
>
226 ContentBrowserPepperHostFactory::CreateAcceptedTCPSocket(
227 PP_Instance instance
,
228 ppapi::TCPSocketVersion version
,
229 scoped_ptr
<net::TCPSocket
> socket
) {
230 if (!CanCreateSocket())
231 return scoped_ptr
<ResourceHost
>();
232 scoped_refptr
<ResourceMessageFilter
> tcp_socket(
233 new PepperTCPSocketMessageFilter(
234 host_
, instance
, version
, socket
.Pass()));
235 return scoped_ptr
<ResourceHost
>(
236 new MessageFilterHost(host_
->GetPpapiHost(), instance
, 0, tcp_socket
));
239 scoped_ptr
<ppapi::host::ResourceHost
>
240 ContentBrowserPepperHostFactory::CreateNewTCPSocket(
241 PP_Instance instance
,
242 PP_Resource resource
,
243 ppapi::TCPSocketVersion version
) {
244 if (!CanCreateSocket())
245 return scoped_ptr
<ResourceHost
>();
247 scoped_refptr
<ResourceMessageFilter
> tcp_socket(
248 new PepperTCPSocketMessageFilter(this, host_
, instance
, version
));
249 if (!tcp_socket
.get())
250 return scoped_ptr
<ResourceHost
>();
252 return scoped_ptr
<ResourceHost
>(new MessageFilterHost(
253 host_
->GetPpapiHost(), instance
, resource
, tcp_socket
));
256 const ppapi::PpapiPermissions
& ContentBrowserPepperHostFactory::GetPermissions()
258 return host_
->GetPpapiHost()->permissions();
261 } // namespace content