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/base/chromecast_switches.h"
17 #include "chromecast/browser/cast_browser_context.h"
18 #include "chromecast/browser/cast_browser_main_parts.h"
19 #include "chromecast/browser/cast_browser_process.h"
20 #include "chromecast/browser/cast_network_delegate.h"
21 #include "chromecast/browser/cast_quota_permission_context.h"
22 #include "chromecast/browser/cast_resource_dispatcher_host_delegate.h"
23 #include "chromecast/browser/geolocation/cast_access_token_store.h"
24 #include "chromecast/browser/media/cma_message_filter_host.h"
25 #include "chromecast/browser/url_request_context_factory.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 "media/audio/audio_manager_factory.h"
42 #include "net/ssl/ssl_cert_request_info.h"
43 #include "net/url_request/url_request_context_getter.h"
44 #include "ui/gl/gl_switches.h"
46 #if defined(OS_ANDROID)
47 #include "components/crash/browser/crash_dump_manager_android.h"
48 #include "components/external_video_surface/browser/android/external_video_surface_container_impl.h"
49 #endif // defined(OS_ANDROID)
51 namespace chromecast
{
54 CastContentBrowserClient::CastContentBrowserClient()
57 url_request_context_factory_(new URLRequestContextFactory()) {
60 CastContentBrowserClient::~CastContentBrowserClient() {
61 content::BrowserThread::DeleteSoon(
62 content::BrowserThread::IO
,
64 url_request_context_factory_
.release());
67 content::BrowserMainParts
* CastContentBrowserClient::CreateBrowserMainParts(
68 const content::MainFunctionParams
& parameters
) {
69 return new CastBrowserMainParts(parameters
,
70 url_request_context_factory_
.get(),
71 PlatformCreateAudioManagerFactory());
74 void CastContentBrowserClient::RenderProcessWillLaunch(
75 content::RenderProcessHost
* host
) {
76 #if !defined(OS_ANDROID)
77 scoped_refptr
<media::CmaMessageFilterHost
> cma_message_filter(
78 new media::CmaMessageFilterHost(host
->GetID()));
79 host
->AddFilter(cma_message_filter
.get());
80 #endif // !defined(OS_ANDROID)
82 // Forcibly trigger I/O-thread URLRequestContext initialization before
83 // getting HostResolver.
84 content::BrowserThread::PostTaskAndReplyWithResult(
85 content::BrowserThread::IO
, FROM_HERE
,
86 base::Bind(&net::URLRequestContextGetter::GetURLRequestContext
,
88 url_request_context_factory_
->GetSystemGetter())),
89 base::Bind(&CastContentBrowserClient::AddNetworkHintsMessageFilter
,
90 base::Unretained(this), host
->GetID()));
92 auto extra_filters
= PlatformGetBrowserMessageFilters();
93 for (auto const& filter
: extra_filters
) {
94 host
->AddFilter(filter
.get());
98 void CastContentBrowserClient::AddNetworkHintsMessageFilter(
99 int render_process_id
, net::URLRequestContext
* context
) {
100 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
102 content::RenderProcessHost
* host
=
103 content::RenderProcessHost::FromID(render_process_id
);
107 scoped_refptr
<content::BrowserMessageFilter
> network_hints_message_filter(
108 new network_hints::NetworkHintsMessageFilter(
109 url_request_context_factory_
->host_resolver()));
110 host
->AddFilter(network_hints_message_filter
.get());
113 net::URLRequestContextGetter
* CastContentBrowserClient::CreateRequestContext(
114 content::BrowserContext
* browser_context
,
115 content::ProtocolHandlerMap
* protocol_handlers
,
116 content::URLRequestInterceptorScopedVector request_interceptors
) {
117 return url_request_context_factory_
->CreateMainGetter(
120 request_interceptors
.Pass());
123 bool CastContentBrowserClient::IsHandledURL(const GURL
& url
) {
127 static const char* const kProtocolList
[] = {
129 url::kFileSystemScheme
,
130 content::kChromeUIScheme
,
131 content::kChromeDevToolsScheme
,
135 const std::string
& scheme
= url
.scheme();
136 for (size_t i
= 0; i
< arraysize(kProtocolList
); ++i
) {
137 if (scheme
== kProtocolList
[i
])
141 if (scheme
== url::kFileScheme
) {
142 return base::CommandLine::ForCurrentProcess()->HasSwitch(
143 switches::kEnableLocalFileAccesses
);
149 void CastContentBrowserClient::AppendExtraCommandLineSwitches(
150 base::CommandLine
* command_line
,
151 int child_process_id
) {
153 std::string process_type
=
154 command_line
->GetSwitchValueNative(switches::kProcessType
);
155 base::CommandLine
* browser_command_line
=
156 base::CommandLine::ForCurrentProcess();
158 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
159 if (process_type
!= switches::kZygoteProcess
) {
160 command_line
->AppendSwitch(::switches::kV8NativesPassedByFD
);
161 command_line
->AppendSwitch(::switches::kV8SnapshotPassedByFD
);
163 #endif // V8_USE_EXTERNAL_STARTUP_DATA
165 // IsCrashReporterEnabled() is set when InitCrashReporter() is called, and
166 // controlled by GetBreakpadClient()->EnableBreakpadForProcess(), therefore
167 // it's ok to add switch to every process here.
168 if (breakpad::IsCrashReporterEnabled()) {
169 command_line
->AppendSwitch(switches::kEnableCrashReporter
);
172 // Renderer process command-line
173 if (process_type
== switches::kRendererProcess
) {
174 // Any browser command-line switches that should be propagated to
175 // the renderer go here.
177 if (browser_command_line
->HasSwitch(switches::kEnableCmaMediaPipeline
))
178 command_line
->AppendSwitch(switches::kEnableCmaMediaPipeline
);
181 #if defined(OS_LINUX)
182 // Necessary for accelerated 2d canvas. By default on Linux, Chromium assumes
183 // GLES2 contexts can be lost to a power-save mode, which breaks GPU canvas
185 if (process_type
== switches::kGpuProcess
) {
186 command_line
->AppendSwitch(switches::kGpuNoContextLost
);
190 PlatformAppendExtraCommandLineSwitches(command_line
);
193 content::AccessTokenStore
* CastContentBrowserClient::CreateAccessTokenStore() {
194 return new CastAccessTokenStore(
195 CastBrowserProcess::GetInstance()->browser_context());
198 void CastContentBrowserClient::OverrideWebkitPrefs(
199 content::RenderViewHost
* render_view_host
,
200 content::WebPreferences
* prefs
) {
201 prefs
->allow_scripts_to_close_windows
= true;
202 // TODO(lcwu): http://crbug.com/391089. This pref is set to true by default
203 // because some content providers such as YouTube use plain http requests
204 // to retrieve media data chunks while running in a https page. This pref
205 // should be disabled once all the content providers are no longer doing that.
206 prefs
->allow_running_insecure_content
= true;
209 void CastContentBrowserClient::ResourceDispatcherHostCreated() {
210 CastBrowserProcess::GetInstance()->SetResourceDispatcherHostDelegate(
211 make_scoped_ptr(new CastResourceDispatcherHostDelegate
));
212 content::ResourceDispatcherHost::Get()->SetDelegate(
213 CastBrowserProcess::GetInstance()->resource_dispatcher_host_delegate());
216 std::string
CastContentBrowserClient::GetApplicationLocale() {
217 const std::string
locale(base::i18n::GetConfiguredLocale());
218 return locale
.empty() ? "en-US" : locale
;
221 content::QuotaPermissionContext
*
222 CastContentBrowserClient::CreateQuotaPermissionContext() {
223 return new CastQuotaPermissionContext();
226 void CastContentBrowserClient::AllowCertificateError(
227 int render_process_id
,
230 const net::SSLInfo
& ssl_info
,
231 const GURL
& request_url
,
232 content::ResourceType resource_type
,
234 bool strict_enforcement
,
235 bool expired_previous_decision
,
236 const base::Callback
<void(bool)>& callback
,
237 content::CertificateRequestResultType
* result
) {
238 // Allow developers to override certificate errors.
239 // Otherwise, any fatal certificate errors will cause an abort.
240 *result
= content::CERTIFICATE_REQUEST_RESULT_TYPE_CANCEL
;
244 void CastContentBrowserClient::SelectClientCertificate(
245 content::WebContents
* web_contents
,
246 net::SSLCertRequestInfo
* cert_request_info
,
247 scoped_ptr
<content::ClientCertificateDelegate
> delegate
) {
248 GURL
requesting_url("https://" + cert_request_info
->host_and_port
.ToString());
250 if (!requesting_url
.is_valid()) {
251 LOG(ERROR
) << "Invalid URL string: "
252 << requesting_url
.possibly_invalid_spec();
253 delegate
->ContinueWithCertificate(nullptr);
257 // In our case there are no relevant certs in the cert_request_info. The cert
258 // we need to return (if permitted) is the Cast device cert, which we can
259 // access directly through the ClientAuthSigner instance. However, we need to
260 // be on the IO thread to determine whether the app is whitelisted to return
261 // it, because CastNetworkDelegate is bound to the IO thread.
262 // Subsequently, the callback must then itself be performed back here
265 // TODO(davidben): Stop using child ID to identify an app.
266 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
267 content::BrowserThread::PostTaskAndReplyWithResult(
268 content::BrowserThread::IO
, FROM_HERE
,
269 base::Bind(&CastContentBrowserClient::SelectClientCertificateOnIOThread
,
270 base::Unretained(this), requesting_url
,
271 web_contents
->GetRenderProcessHost()->GetID()),
272 base::Bind(&content::ClientCertificateDelegate::ContinueWithCertificate
,
273 base::Owned(delegate
.release())));
276 net::X509Certificate
*
277 CastContentBrowserClient::SelectClientCertificateOnIOThread(
279 int render_process_id
) {
280 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
281 CastNetworkDelegate
* network_delegate
=
282 url_request_context_factory_
->app_network_delegate();
283 if (network_delegate
->IsWhitelisted(requesting_url
,
284 render_process_id
, false)) {
285 return CastNetworkDelegate::DeviceCert();
287 LOG(ERROR
) << "Invalid host for client certificate request: "
288 << requesting_url
.host()
289 << " with render_process_id: "
290 << render_process_id
;
295 bool CastContentBrowserClient::CanCreateWindow(
296 const GURL
& opener_url
,
297 const GURL
& opener_top_level_frame_url
,
298 const GURL
& source_origin
,
299 WindowContainerType container_type
,
300 const GURL
& target_url
,
301 const content::Referrer
& referrer
,
302 WindowOpenDisposition disposition
,
303 const blink::WebWindowFeatures
& features
,
305 bool opener_suppressed
,
306 content::ResourceContext
* context
,
307 int render_process_id
,
309 bool* no_javascript_access
) {
310 *no_javascript_access
= true;
314 void CastContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
315 const base::CommandLine
& command_line
,
316 int child_process_id
,
317 content::FileDescriptorInfo
* mappings
) {
318 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
319 if (v8_natives_fd_
.get() == -1 || v8_snapshot_fd_
.get() == -1) {
320 int v8_natives_fd
= -1;
321 int v8_snapshot_fd
= -1;
322 if (gin::V8Initializer::OpenV8FilesForChildProcesses(&v8_natives_fd
,
324 v8_natives_fd_
.reset(v8_natives_fd
);
325 v8_snapshot_fd_
.reset(v8_snapshot_fd
);
328 DCHECK(v8_natives_fd_
.get() != -1 && v8_snapshot_fd_
.get() != -1);
329 mappings
->Share(kV8NativesDataDescriptor
, v8_natives_fd_
.get());
330 mappings
->Share(kV8SnapshotDataDescriptor
, v8_snapshot_fd_
.get());
331 #endif // V8_USE_EXTERNAL_STARTUP_DATA
332 #if defined(OS_ANDROID)
333 const int flags_open_read
= base::File::FLAG_OPEN
| base::File::FLAG_READ
;
334 base::FilePath pak_file_path
;
335 CHECK(PathService::Get(FILE_CAST_PAK
, &pak_file_path
));
336 base::File
pak_file(pak_file_path
, flags_open_read
);
337 if (!pak_file
.IsValid()) {
338 NOTREACHED() << "Failed to open file when creating renderer process: "
341 mappings
->Transfer(kAndroidPakDescriptor
,
342 base::ScopedFD(pak_file
.TakePlatformFile()));
344 if (breakpad::IsCrashReporterEnabled()) {
345 base::File
minidump_file(
346 breakpad::CrashDumpManager::GetInstance()->CreateMinidumpFile(
348 if (!minidump_file
.IsValid()) {
349 LOG(ERROR
) << "Failed to create file for minidump, crash reporting will "
350 << "be disabled for this process.";
352 mappings
->Transfer(kAndroidMinidumpDescriptor
,
353 base::ScopedFD(minidump_file
.TakePlatformFile()));
357 base::FilePath app_data_path
;
358 CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA
, &app_data_path
));
359 base::FilePath icudata_path
=
360 app_data_path
.AppendASCII(base::i18n::kIcuDataFileName
);
361 base::File
icudata_file(icudata_path
, flags_open_read
);
362 if (!icudata_file
.IsValid())
363 NOTREACHED() << "Failed to open ICU file when creating renderer process";
364 mappings
->Transfer(kAndroidICUDataDescriptor
,
365 base::ScopedFD(icudata_file
.TakePlatformFile()));
367 int crash_signal_fd
= GetCrashSignalFD(command_line
);
368 if (crash_signal_fd
>= 0) {
369 mappings
->Share(kCrashDumpSignal
, crash_signal_fd
);
371 #endif // defined(OS_ANDROID)
374 #if defined(OS_ANDROID) && defined(VIDEO_HOLE)
375 content::ExternalVideoSurfaceContainer
*
376 CastContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
377 content::WebContents
* web_contents
) {
378 return external_video_surface::ExternalVideoSurfaceContainerImpl::Create(
381 #endif // defined(OS_ANDROID) && defined(VIDEO_HOLE)
383 #if !defined(OS_ANDROID)
384 int CastContentBrowserClient::GetCrashSignalFD(
385 const base::CommandLine
& command_line
) {
386 std::string process_type
=
387 command_line
.GetSwitchValueASCII(switches::kProcessType
);
389 if (process_type
== switches::kRendererProcess
||
390 process_type
== switches::kGpuProcess
) {
391 breakpad::CrashHandlerHostLinux
* crash_handler
=
392 crash_handlers_
[process_type
];
393 if (!crash_handler
) {
394 crash_handler
= CreateCrashHandlerHost(process_type
);
395 crash_handlers_
[process_type
] = crash_handler
;
397 return crash_handler
->GetDeathSignalSocket();
403 breakpad::CrashHandlerHostLinux
*
404 CastContentBrowserClient::CreateCrashHandlerHost(
405 const std::string
& process_type
) {
406 // Let cast shell dump to /tmp. Internal minidump generator code can move it
407 // to /data/minidumps later, since /data/minidumps is file lock-controlled.
408 base::FilePath dumps_path
;
409 PathService::Get(base::DIR_TEMP
, &dumps_path
);
411 // Alway set "upload" to false to use our own uploader.
412 breakpad::CrashHandlerHostLinux
* crash_handler
=
413 new breakpad::CrashHandlerHostLinux(
414 process_type
, dumps_path
, false /* upload */);
415 // StartUploaderThread() even though upload is diferred.
416 // Breakpad-related memory is freed in the uploader thread.
417 crash_handler
->StartUploaderThread();
418 return crash_handler
;
420 #endif // !defined(OS_ANDROID)
423 } // namespace chromecast