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"
9 #include "base/base_switches.h"
10 #include "base/command_line.h"
11 #include "base/files/scoped_file.h"
12 #include "base/i18n/icu_util.h"
13 #include "base/i18n/rtl.h"
14 #include "base/path_service.h"
15 #include "chromecast/base/cast_paths.h"
16 #include "chromecast/browser/cast_browser_context.h"
17 #include "chromecast/browser/cast_browser_main_parts.h"
18 #include "chromecast/browser/cast_browser_process.h"
19 #include "chromecast/browser/cast_network_delegate.h"
20 #include "chromecast/browser/cast_quota_permission_context.h"
21 #include "chromecast/browser/cast_resource_dispatcher_host_delegate.h"
22 #include "chromecast/browser/geolocation/cast_access_token_store.h"
23 #include "chromecast/browser/media/cma_message_filter_host.h"
24 #include "chromecast/browser/url_request_context_factory.h"
25 #include "chromecast/common/chromecast_switches.h"
26 #include "chromecast/common/global_descriptors.h"
27 #include "components/crash/app/breakpad_linux.h"
28 #include "components/crash/browser/crash_handler_host_linux.h"
29 #include "components/network_hints/browser/network_hints_message_filter.h"
30 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/certificate_request_result_type.h"
32 #include "content/public/browser/client_certificate_delegate.h"
33 #include "content/public/browser/render_process_host.h"
34 #include "content/public/browser/resource_dispatcher_host.h"
35 #include "content/public/browser/web_contents.h"
36 #include "content/public/common/content_descriptors.h"
37 #include "content/public/common/content_switches.h"
38 #include "content/public/common/url_constants.h"
39 #include "content/public/common/web_preferences.h"
40 #include "gin/v8_initializer.h"
41 #include "net/ssl/ssl_cert_request_info.h"
42 #include "net/url_request/url_request_context_getter.h"
43 #include "ui/gl/gl_switches.h"
45 #if defined(OS_ANDROID)
46 #include "chromecast/browser/android/external_video_surface_container_impl.h"
47 #include "components/crash/browser/crash_dump_manager_android.h"
48 #endif // defined(OS_ANDROID)
50 namespace chromecast
{
53 CastContentBrowserClient::CastContentBrowserClient()
56 url_request_context_factory_(new URLRequestContextFactory()) {
59 CastContentBrowserClient::~CastContentBrowserClient() {
60 content::BrowserThread::DeleteSoon(
61 content::BrowserThread::IO
,
63 url_request_context_factory_
.release());
66 content::BrowserMainParts
* CastContentBrowserClient::CreateBrowserMainParts(
67 const content::MainFunctionParams
& parameters
) {
68 return new CastBrowserMainParts(parameters
,
69 url_request_context_factory_
.get());
72 void CastContentBrowserClient::RenderProcessWillLaunch(
73 content::RenderProcessHost
* host
) {
74 #if !defined(OS_ANDROID)
75 scoped_refptr
<media::CmaMessageFilterHost
> cma_message_filter(
76 new media::CmaMessageFilterHost(host
->GetID()));
77 host
->AddFilter(cma_message_filter
.get());
78 #endif // !defined(OS_ANDROID)
80 // Forcibly trigger I/O-thread URLRequestContext initialization before
81 // getting HostResolver.
82 content::BrowserThread::PostTaskAndReplyWithResult(
83 content::BrowserThread::IO
, FROM_HERE
,
84 base::Bind(&net::URLRequestContextGetter::GetURLRequestContext
,
86 url_request_context_factory_
->GetSystemGetter())),
87 base::Bind(&CastContentBrowserClient::AddNetworkHintsMessageFilter
,
88 base::Unretained(this), host
->GetID()));
90 auto extra_filters
= PlatformGetBrowserMessageFilters();
91 for (auto const& filter
: extra_filters
) {
92 host
->AddFilter(filter
.get());
96 void CastContentBrowserClient::AddNetworkHintsMessageFilter(
97 int render_process_id
, net::URLRequestContext
* context
) {
98 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
100 content::RenderProcessHost
* host
=
101 content::RenderProcessHost::FromID(render_process_id
);
105 scoped_refptr
<content::BrowserMessageFilter
> network_hints_message_filter(
106 new network_hints::NetworkHintsMessageFilter(
107 url_request_context_factory_
->host_resolver()));
108 host
->AddFilter(network_hints_message_filter
.get());
111 net::URLRequestContextGetter
* CastContentBrowserClient::CreateRequestContext(
112 content::BrowserContext
* browser_context
,
113 content::ProtocolHandlerMap
* protocol_handlers
,
114 content::URLRequestInterceptorScopedVector request_interceptors
) {
115 return url_request_context_factory_
->CreateMainGetter(
118 request_interceptors
.Pass());
121 bool CastContentBrowserClient::IsHandledURL(const GURL
& url
) {
125 static const char* const kProtocolList
[] = {
127 url::kFileSystemScheme
,
128 content::kChromeUIScheme
,
129 content::kChromeDevToolsScheme
,
133 const std::string
& scheme
= url
.scheme();
134 for (size_t i
= 0; i
< arraysize(kProtocolList
); ++i
) {
135 if (scheme
== kProtocolList
[i
])
139 if (scheme
== url::kFileScheme
) {
140 return base::CommandLine::ForCurrentProcess()->HasSwitch(
141 switches::kEnableLocalFileAccesses
);
147 void CastContentBrowserClient::AppendExtraCommandLineSwitches(
148 base::CommandLine
* command_line
,
149 int child_process_id
) {
151 std::string process_type
=
152 command_line
->GetSwitchValueNative(switches::kProcessType
);
153 base::CommandLine
* browser_command_line
=
154 base::CommandLine::ForCurrentProcess();
156 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
157 if (process_type
!= switches::kZygoteProcess
) {
158 command_line
->AppendSwitch(::switches::kV8NativesPassedByFD
);
159 command_line
->AppendSwitch(::switches::kV8SnapshotPassedByFD
);
161 #endif // V8_USE_EXTERNAL_STARTUP_DATA
163 // IsCrashReporterEnabled() is set when InitCrashReporter() is called, and
164 // controlled by GetBreakpadClient()->EnableBreakpadForProcess(), therefore
165 // it's ok to add switch to every process here.
166 if (breakpad::IsCrashReporterEnabled()) {
167 command_line
->AppendSwitch(switches::kEnableCrashReporter
);
170 // Renderer process command-line
171 if (process_type
== switches::kRendererProcess
) {
172 // Any browser command-line switches that should be propagated to
173 // the renderer go here.
175 if (browser_command_line
->HasSwitch(switches::kEnableCmaMediaPipeline
))
176 command_line
->AppendSwitch(switches::kEnableCmaMediaPipeline
);
179 #if defined(OS_LINUX)
180 // Necessary for accelerated 2d canvas. By default on Linux, Chromium assumes
181 // GLES2 contexts can be lost to a power-save mode, which breaks GPU canvas
183 if (process_type
== switches::kGpuProcess
) {
184 command_line
->AppendSwitch(switches::kGpuNoContextLost
);
188 PlatformAppendExtraCommandLineSwitches(command_line
);
191 content::AccessTokenStore
* CastContentBrowserClient::CreateAccessTokenStore() {
192 return new CastAccessTokenStore(
193 CastBrowserProcess::GetInstance()->browser_context());
196 void CastContentBrowserClient::OverrideWebkitPrefs(
197 content::RenderViewHost
* render_view_host
,
198 content::WebPreferences
* prefs
) {
199 prefs
->allow_scripts_to_close_windows
= true;
200 // TODO(lcwu): http://crbug.com/391089. This pref is set to true by default
201 // because some content providers such as YouTube use plain http requests
202 // to retrieve media data chunks while running in a https page. This pref
203 // should be disabled once all the content providers are no longer doing that.
204 prefs
->allow_running_insecure_content
= true;
207 void CastContentBrowserClient::ResourceDispatcherHostCreated() {
208 CastBrowserProcess::GetInstance()->SetResourceDispatcherHostDelegate(
209 make_scoped_ptr(new CastResourceDispatcherHostDelegate
));
210 content::ResourceDispatcherHost::Get()->SetDelegate(
211 CastBrowserProcess::GetInstance()->resource_dispatcher_host_delegate());
214 std::string
CastContentBrowserClient::GetApplicationLocale() {
215 const std::string
locale(base::i18n::GetConfiguredLocale());
216 return locale
.empty() ? "en-US" : locale
;
219 content::QuotaPermissionContext
*
220 CastContentBrowserClient::CreateQuotaPermissionContext() {
221 return new CastQuotaPermissionContext();
224 void CastContentBrowserClient::AllowCertificateError(
225 int render_process_id
,
228 const net::SSLInfo
& ssl_info
,
229 const GURL
& request_url
,
230 content::ResourceType resource_type
,
232 bool strict_enforcement
,
233 bool expired_previous_decision
,
234 const base::Callback
<void(bool)>& callback
,
235 content::CertificateRequestResultType
* result
) {
236 // Allow developers to override certificate errors.
237 // Otherwise, any fatal certificate errors will cause an abort.
238 *result
= content::CERTIFICATE_REQUEST_RESULT_TYPE_CANCEL
;
242 void CastContentBrowserClient::SelectClientCertificate(
243 content::WebContents
* web_contents
,
244 net::SSLCertRequestInfo
* cert_request_info
,
245 scoped_ptr
<content::ClientCertificateDelegate
> delegate
) {
246 GURL
requesting_url("https://" + cert_request_info
->host_and_port
.ToString());
248 if (!requesting_url
.is_valid()) {
249 LOG(ERROR
) << "Invalid URL string: "
250 << requesting_url
.possibly_invalid_spec();
251 delegate
->ContinueWithCertificate(nullptr);
255 // In our case there are no relevant certs in the cert_request_info. The cert
256 // we need to return (if permitted) is the Cast device cert, which we can
257 // access directly through the ClientAuthSigner instance. However, we need to
258 // be on the IO thread to determine whether the app is whitelisted to return
259 // it, because CastNetworkDelegate is bound to the IO thread.
260 // Subsequently, the callback must then itself be performed back here
263 // TODO(davidben): Stop using child ID to identify an app.
264 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
265 content::BrowserThread::PostTaskAndReplyWithResult(
266 content::BrowserThread::IO
, FROM_HERE
,
267 base::Bind(&CastContentBrowserClient::SelectClientCertificateOnIOThread
,
268 base::Unretained(this), requesting_url
,
269 web_contents
->GetRenderProcessHost()->GetID()),
270 base::Bind(&content::ClientCertificateDelegate::ContinueWithCertificate
,
271 base::Owned(delegate
.release())));
274 net::X509Certificate
*
275 CastContentBrowserClient::SelectClientCertificateOnIOThread(
277 int render_process_id
) {
278 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
279 CastNetworkDelegate
* network_delegate
=
280 url_request_context_factory_
->app_network_delegate();
281 if (network_delegate
->IsWhitelisted(requesting_url
,
282 render_process_id
, false)) {
283 return CastNetworkDelegate::DeviceCert();
285 LOG(ERROR
) << "Invalid host for client certificate request: "
286 << requesting_url
.host()
287 << " with render_process_id: "
288 << render_process_id
;
293 bool CastContentBrowserClient::CanCreateWindow(
294 const GURL
& opener_url
,
295 const GURL
& opener_top_level_frame_url
,
296 const GURL
& source_origin
,
297 WindowContainerType container_type
,
298 const GURL
& target_url
,
299 const content::Referrer
& referrer
,
300 WindowOpenDisposition disposition
,
301 const blink::WebWindowFeatures
& features
,
303 bool opener_suppressed
,
304 content::ResourceContext
* context
,
305 int render_process_id
,
307 bool* no_javascript_access
) {
308 *no_javascript_access
= true;
312 void CastContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
313 const base::CommandLine
& command_line
,
314 int child_process_id
,
315 content::FileDescriptorInfo
* mappings
) {
316 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
317 if (v8_natives_fd_
.get() == -1 || v8_snapshot_fd_
.get() == -1) {
318 int v8_natives_fd
= -1;
319 int v8_snapshot_fd
= -1;
320 if (gin::V8Initializer::OpenV8FilesForChildProcesses(&v8_natives_fd
,
322 v8_natives_fd_
.reset(v8_natives_fd
);
323 v8_snapshot_fd_
.reset(v8_snapshot_fd
);
326 DCHECK(v8_natives_fd_
.get() != -1 && v8_snapshot_fd_
.get() != -1);
327 mappings
->Share(kV8NativesDataDescriptor
, v8_natives_fd_
.get());
328 mappings
->Share(kV8SnapshotDataDescriptor
, v8_snapshot_fd_
.get());
329 #endif // V8_USE_EXTERNAL_STARTUP_DATA
330 #if defined(OS_ANDROID)
331 const int flags_open_read
= base::File::FLAG_OPEN
| base::File::FLAG_READ
;
332 base::FilePath pak_file_path
;
333 CHECK(PathService::Get(FILE_CAST_PAK
, &pak_file_path
));
334 base::File
pak_file(pak_file_path
, flags_open_read
);
335 if (!pak_file
.IsValid()) {
336 NOTREACHED() << "Failed to open file when creating renderer process: "
339 mappings
->Transfer(kAndroidPakDescriptor
,
340 base::ScopedFD(pak_file
.TakePlatformFile()));
342 if (breakpad::IsCrashReporterEnabled()) {
343 base::File
minidump_file(
344 breakpad::CrashDumpManager::GetInstance()->CreateMinidumpFile(
346 if (!minidump_file
.IsValid()) {
347 LOG(ERROR
) << "Failed to create file for minidump, crash reporting will "
348 << "be disabled for this process.";
350 mappings
->Transfer(kAndroidMinidumpDescriptor
,
351 base::ScopedFD(minidump_file
.TakePlatformFile()));
355 base::FilePath app_data_path
;
356 CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA
, &app_data_path
));
357 base::FilePath icudata_path
=
358 app_data_path
.AppendASCII(base::i18n::kIcuDataFileName
);
359 base::File
icudata_file(icudata_path
, flags_open_read
);
360 if (!icudata_file
.IsValid())
361 NOTREACHED() << "Failed to open ICU file when creating renderer process";
362 mappings
->Transfer(kAndroidICUDataDescriptor
,
363 base::ScopedFD(icudata_file
.TakePlatformFile()));
365 int crash_signal_fd
= GetCrashSignalFD(command_line
);
366 if (crash_signal_fd
>= 0) {
367 mappings
->Share(kCrashDumpSignal
, crash_signal_fd
);
369 #endif // defined(OS_ANDROID)
372 #if defined(OS_ANDROID) && defined(VIDEO_HOLE)
373 content::ExternalVideoSurfaceContainer
*
374 CastContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
375 content::WebContents
* web_contents
) {
376 return ExternalVideoSurfaceContainerImpl::Create(web_contents
);
378 #endif // defined(OS_ANDROID) && defined(VIDEO_HOLE)
380 #if !defined(OS_ANDROID)
381 int CastContentBrowserClient::GetCrashSignalFD(
382 const base::CommandLine
& command_line
) {
383 std::string process_type
=
384 command_line
.GetSwitchValueASCII(switches::kProcessType
);
386 if (process_type
== switches::kRendererProcess
||
387 process_type
== switches::kGpuProcess
) {
388 breakpad::CrashHandlerHostLinux
* crash_handler
=
389 crash_handlers_
[process_type
];
390 if (!crash_handler
) {
391 crash_handler
= CreateCrashHandlerHost(process_type
);
392 crash_handlers_
[process_type
] = crash_handler
;
394 return crash_handler
->GetDeathSignalSocket();
400 breakpad::CrashHandlerHostLinux
*
401 CastContentBrowserClient::CreateCrashHandlerHost(
402 const std::string
& process_type
) {
403 // Let cast shell dump to /tmp. Internal minidump generator code can move it
404 // to /data/minidumps later, since /data/minidumps is file lock-controlled.
405 base::FilePath dumps_path
;
406 PathService::Get(base::DIR_TEMP
, &dumps_path
);
408 // Alway set "upload" to false to use our own uploader.
409 breakpad::CrashHandlerHostLinux
* crash_handler
=
410 new breakpad::CrashHandlerHostLinux(
411 process_type
, dumps_path
, false /* upload */);
412 // StartUploaderThread() even though upload is diferred.
413 // Breakpad-related memory is freed in the uploader thread.
414 crash_handler
->StartUploaderThread();
415 return crash_handler
;
417 #endif // !defined(OS_ANDROID)
420 } // namespace chromecast