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
);
102 // Renderer process command-line
103 if (process_type
== switches::kRendererProcess
) {
104 // Any browser command-line switches that should be propagated to
105 // the renderer go here.
106 #if defined(OS_ANDROID)
107 command_line
->AppendSwitch(switches::kForceUseOverlayEmbeddedVideo
);
108 #endif // defined(OS_ANDROID)
112 content::AccessTokenStore
* CastContentBrowserClient::CreateAccessTokenStore() {
113 return new CastAccessTokenStore(
114 CastBrowserProcess::GetInstance()->browser_context());
117 void CastContentBrowserClient::OverrideWebkitPrefs(
118 content::RenderViewHost
* render_view_host
,
120 content::WebPreferences
* prefs
) {
121 prefs
->allow_scripts_to_close_windows
= true;
122 // TODO(lcwu): http://crbug.com/391089. This pref is set to true by default
123 // because some content providers such as YouTube use plain http requests
124 // to retrieve media data chunks while running in a https page. This pref
125 // should be disabled once all the content providers are no longer doing that.
126 prefs
->allow_running_insecure_content
= true;
129 std::string
CastContentBrowserClient::GetApplicationLocale() {
130 const std::string
locale(base::i18n::GetConfiguredLocale());
131 return locale
.empty() ? "en-US" : locale
;
134 void CastContentBrowserClient::AllowCertificateError(
135 int render_process_id
,
138 const net::SSLInfo
& ssl_info
,
139 const GURL
& request_url
,
140 content::ResourceType resource_type
,
142 bool strict_enforcement
,
143 bool expired_previous_decision
,
144 const base::Callback
<void(bool)>& callback
,
145 content::CertificateRequestResultType
* result
) {
146 // Allow developers to override certificate errors.
147 // Otherwise, any fatal certificate errors will cause an abort.
148 *result
= content::CERTIFICATE_REQUEST_RESULT_TYPE_CANCEL
;
152 void CastContentBrowserClient::SelectClientCertificate(
153 int render_process_id
,
155 net::SSLCertRequestInfo
* cert_request_info
,
156 const base::Callback
<void(net::X509Certificate
*)>& callback
) {
157 GURL
requesting_url("https://" + cert_request_info
->host_and_port
.ToString());
159 if (!requesting_url
.is_valid()) {
160 LOG(ERROR
) << "Invalid URL string: "
161 << requesting_url
.possibly_invalid_spec();
166 // In our case there are no relevant certs in the cert_request_info. The cert
167 // we need to return (if permitted) is the Cast device cert, which we can
168 // access directly through the ClientAuthSigner instance. However, we need to
169 // be on the IO thread to determine whether the app is whitelisted to return
170 // it, because CastNetworkDelegate is bound to the IO thread.
171 // Subsequently, the callback must then itself be performed back here
173 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
174 content::BrowserThread::PostTaskAndReplyWithResult(
175 content::BrowserThread::IO
,
178 &CastContentBrowserClient::SelectClientCertificateOnIOThread
,
179 base::Unretained(this),
185 net::X509Certificate
*
186 CastContentBrowserClient::SelectClientCertificateOnIOThread(
188 int render_process_id
) {
189 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
190 CastNetworkDelegate
* network_delegate
=
191 url_request_context_factory_
->app_network_delegate();
192 if (network_delegate
->IsWhitelisted(requesting_url
,
193 render_process_id
, false)) {
194 return CastNetworkDelegate::DeviceCert();
196 LOG(ERROR
) << "Invalid host for client certificate request: "
197 << requesting_url
.host()
198 << " with render_process_id: "
199 << render_process_id
;
204 bool CastContentBrowserClient::CanCreateWindow(
205 const GURL
& opener_url
,
206 const GURL
& opener_top_level_frame_url
,
207 const GURL
& source_origin
,
208 WindowContainerType container_type
,
209 const GURL
& target_url
,
210 const content::Referrer
& referrer
,
211 WindowOpenDisposition disposition
,
212 const blink::WebWindowFeatures
& features
,
214 bool opener_suppressed
,
215 content::ResourceContext
* context
,
216 int render_process_id
,
218 bool* no_javascript_access
) {
219 *no_javascript_access
= true;
223 content::DevToolsManagerDelegate
*
224 CastContentBrowserClient::GetDevToolsManagerDelegate() {
225 return new CastDevToolsManagerDelegate();
228 void CastContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
229 const base::CommandLine
& command_line
,
230 int child_process_id
,
231 content::FileDescriptorInfo
* mappings
) {
232 #if defined(OS_ANDROID)
233 int flags
= base::File::FLAG_OPEN
| base::File::FLAG_READ
;
234 base::FilePath pak_file
;
235 CHECK(PathService::Get(FILE_CAST_PAK
, &pak_file
));
236 base::File
pak_with_flags(pak_file
, flags
);
237 if (!pak_with_flags
.IsValid()) {
238 NOTREACHED() << "Failed to open file when creating renderer process: "
242 kAndroidPakDescriptor
,
243 base::ScopedFD(pak_with_flags
.TakePlatformFile()));
245 if (breakpad::IsCrashReporterEnabled()) {
246 base::File
minidump_file(
247 breakpad::CrashDumpManager::GetInstance()->CreateMinidumpFile(
249 if (!minidump_file
.IsValid()) {
250 LOG(ERROR
) << "Failed to create file for minidump, crash reporting will "
251 << "be disabled for this process.";
253 mappings
->Transfer(kAndroidMinidumpDescriptor
,
254 base::ScopedFD(minidump_file
.TakePlatformFile()));
257 #endif // defined(OS_ANDROID)
260 #if defined(OS_ANDROID) && defined(VIDEO_HOLE)
261 content::ExternalVideoSurfaceContainer
*
262 CastContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
263 content::WebContents
* web_contents
) {
264 return new ExternalVideoSurfaceContainerImpl(web_contents
);
266 #endif // defined(OS_ANDROID) && defined(VIDEO_HOLE)
270 } // namespace chromecast