1 // Copyright 2014 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 "chromecast/browser/cast_content_browser_client.h"
7 #include "base/command_line.h"
8 #include "base/files/scoped_file.h"
9 #include "base/i18n/rtl.h"
10 #include "base/path_service.h"
11 #include "chromecast/browser/cast_browser_context.h"
12 #include "chromecast/browser/cast_browser_main_parts.h"
13 #include "chromecast/browser/cast_browser_process.h"
14 #include "chromecast/browser/cast_network_delegate.h"
15 #include "chromecast/browser/devtools/cast_dev_tools_delegate.h"
16 #include "chromecast/browser/geolocation/cast_access_token_store.h"
17 #include "chromecast/browser/url_request_context_factory.h"
18 #include "chromecast/common/cast_paths.h"
19 #include "chromecast/common/global_descriptors.h"
20 #include "components/crash/app/breakpad_linux.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/certificate_request_result_type.h"
23 #include "content/public/browser/render_process_host.h"
24 #include "content/public/common/content_descriptors.h"
25 #include "content/public/common/content_switches.h"
26 #include "content/public/common/url_constants.h"
27 #include "content/public/common/web_preferences.h"
28 #include "net/ssl/ssl_cert_request_info.h"
30 #if defined(OS_ANDROID)
31 #include "chromecast/browser/android/external_video_surface_container_impl.h"
32 #endif // defined(OS_ANDROID)
34 #if defined(OS_ANDROID)
35 #include "components/crash/browser/crash_dump_manager_android.h"
36 #endif // defined(OS_ANDROID)
38 namespace chromecast
{
41 CastContentBrowserClient::CastContentBrowserClient()
42 : url_request_context_factory_(new URLRequestContextFactory()) {
45 CastContentBrowserClient::~CastContentBrowserClient() {
46 content::BrowserThread::DeleteSoon(
47 content::BrowserThread::IO
,
49 url_request_context_factory_
.release());
52 content::BrowserMainParts
* CastContentBrowserClient::CreateBrowserMainParts(
53 const content::MainFunctionParams
& parameters
) {
54 return new CastBrowserMainParts(parameters
,
55 url_request_context_factory_
.get());
58 void CastContentBrowserClient::RenderProcessWillLaunch(
59 content::RenderProcessHost
* host
) {
62 net::URLRequestContextGetter
* CastContentBrowserClient::CreateRequestContext(
63 content::BrowserContext
* browser_context
,
64 content::ProtocolHandlerMap
* protocol_handlers
,
65 content::URLRequestInterceptorScopedVector request_interceptors
) {
66 return url_request_context_factory_
->CreateMainGetter(
69 request_interceptors
.Pass());
72 bool CastContentBrowserClient::IsHandledURL(const GURL
& url
) {
76 static const char* const kProtocolList
[] = {
78 url::kFileSystemScheme
,
79 content::kChromeUIScheme
,
80 content::kChromeDevToolsScheme
,
82 #if defined(OS_ANDROID)
84 #endif // defined(OS_ANDROID)
87 const std::string
& scheme
= url
.scheme();
88 for (size_t i
= 0; i
< arraysize(kProtocolList
); ++i
) {
89 if (scheme
== kProtocolList
[i
])
95 void CastContentBrowserClient::AppendExtraCommandLineSwitches(
96 base::CommandLine
* command_line
,
97 int child_process_id
) {
99 std::string process_type
=
100 command_line
->GetSwitchValueNative(switches::kProcessType
);
101 // Renderer process comamndline
102 if (process_type
== switches::kRendererProcess
) {
103 // Any browser command-line switches that should be propagated to
104 // the renderer go here.
108 content::AccessTokenStore
* CastContentBrowserClient::CreateAccessTokenStore() {
109 return new CastAccessTokenStore(
110 CastBrowserProcess::GetInstance()->browser_context());
113 void CastContentBrowserClient::OverrideWebkitPrefs(
114 content::RenderViewHost
* render_view_host
,
116 content::WebPreferences
* prefs
) {
117 prefs
->allow_scripts_to_close_windows
= true;
118 // TODO(lcwu): http://crbug.com/391089. This pref is set to true by default
119 // because some content providers such as YouTube use plain http requests
120 // to retrieve media data chunks while running in a https page. This pref
121 // should be disabled once all the content providers are no longer doing that.
122 prefs
->allow_running_insecure_content
= true;
125 std::string
CastContentBrowserClient::GetApplicationLocale() {
126 const std::string
locale(base::i18n::GetConfiguredLocale());
127 return locale
.empty() ? "en-US" : locale
;
130 void CastContentBrowserClient::AllowCertificateError(
131 int render_process_id
,
134 const net::SSLInfo
& ssl_info
,
135 const GURL
& request_url
,
136 content::ResourceType resource_type
,
138 bool strict_enforcement
,
139 bool expired_previous_decision
,
140 const base::Callback
<void(bool)>& callback
,
141 content::CertificateRequestResultType
* result
) {
142 // Allow developers to override certificate errors.
143 // Otherwise, any fatal certificate errors will cause an abort.
144 *result
= content::CERTIFICATE_REQUEST_RESULT_TYPE_CANCEL
;
148 void CastContentBrowserClient::SelectClientCertificate(
149 int render_process_id
,
151 net::SSLCertRequestInfo
* cert_request_info
,
152 const base::Callback
<void(net::X509Certificate
*)>& callback
) {
153 GURL
requesting_url("https://" + cert_request_info
->host_and_port
.ToString());
155 if (!requesting_url
.is_valid()) {
156 LOG(ERROR
) << "Invalid URL string: "
157 << requesting_url
.possibly_invalid_spec();
162 // In our case there are no relevant certs in the cert_request_info. The cert
163 // we need to return (if permitted) is the Cast device cert, which we can
164 // access directly through the ClientAuthSigner instance. However, we need to
165 // be on the IO thread to determine whether the app is whitelisted to return
166 // it, because CastNetworkDelegate is bound to the IO thread.
167 // Subsequently, the callback must then itself be performed back here
169 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
170 content::BrowserThread::PostTaskAndReplyWithResult(
171 content::BrowserThread::IO
,
174 &CastContentBrowserClient::SelectClientCertificateOnIOThread
,
175 base::Unretained(this),
180 net::X509Certificate
*
181 CastContentBrowserClient::SelectClientCertificateOnIOThread(
182 GURL requesting_url
) {
183 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
184 CastNetworkDelegate
* network_delegate
=
185 url_request_context_factory_
->app_network_delegate();
186 if (network_delegate
->IsWhitelisted(requesting_url
, false)) {
187 return CastNetworkDelegate::DeviceCert();
189 LOG(ERROR
) << "Invalid host for client certificate request: "
190 << requesting_url
.host();
195 bool CastContentBrowserClient::CanCreateWindow(
196 const GURL
& opener_url
,
197 const GURL
& opener_top_level_frame_url
,
198 const GURL
& source_origin
,
199 WindowContainerType container_type
,
200 const GURL
& target_url
,
201 const content::Referrer
& referrer
,
202 WindowOpenDisposition disposition
,
203 const blink::WebWindowFeatures
& features
,
205 bool opener_suppressed
,
206 content::ResourceContext
* context
,
207 int render_process_id
,
209 bool* no_javascript_access
) {
210 *no_javascript_access
= true;
214 content::DevToolsManagerDelegate
*
215 CastContentBrowserClient::GetDevToolsManagerDelegate() {
216 return new CastDevToolsManagerDelegate();
219 void CastContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
220 const base::CommandLine
& command_line
,
221 int child_process_id
,
222 content::FileDescriptorInfo
* mappings
) {
223 #if defined(OS_ANDROID)
224 int flags
= base::File::FLAG_OPEN
| base::File::FLAG_READ
;
225 base::FilePath pak_file
;
226 CHECK(PathService::Get(FILE_CAST_PAK
, &pak_file
));
227 base::File
pak_with_flags(pak_file
, flags
);
228 if (!pak_with_flags
.IsValid()) {
229 NOTREACHED() << "Failed to open file when creating renderer process: "
233 kAndroidPakDescriptor
,
234 base::ScopedFD(pak_with_flags
.TakePlatformFile()));
236 if (breakpad::IsCrashReporterEnabled()) {
237 base::File
minidump_file(
238 breakpad::CrashDumpManager::GetInstance()->CreateMinidumpFile(
240 if (!minidump_file
.IsValid()) {
241 LOG(ERROR
) << "Failed to create file for minidump, crash reporting will "
242 << "be disabled for this process.";
244 mappings
->Transfer(kAndroidMinidumpDescriptor
,
245 base::ScopedFD(minidump_file
.TakePlatformFile()));
248 #endif // defined(OS_ANDROID)
251 #if defined(OS_ANDROID) && defined(VIDEO_HOLE)
252 content::ExternalVideoSurfaceContainer
*
253 CastContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
254 content::WebContents
* web_contents
) {
255 return new ExternalVideoSurfaceContainerImpl(web_contents
);
257 #endif // defined(OS_ANDROID) && defined(VIDEO_HOLE)
261 } // namespace chromecast