Clean up check for dependency_info.
[chromium-blink-merge.git] / chromecast / browser / cast_content_browser_client.cc
blobb12a2307eeed662e7195fea43297546d2038cbee
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 <string>
9 #include "base/base_switches.h"
10 #include "base/command_line.h"
11 #include "base/files/scoped_file.h"
12 #include "base/i18n/rtl.h"
13 #include "base/path_service.h"
14 #include "base/strings/utf_string_conversions.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_media_pipeline_client.h"
25 #include "chromecast/browser/media/cma_message_filter_host.h"
26 #include "chromecast/browser/service/cast_service_simple.h"
27 #include "chromecast/browser/url_request_context_factory.h"
28 #include "chromecast/common/global_descriptors.h"
29 #include "chromecast/media/audio/cast_audio_manager_factory.h"
30 #include "chromecast/media/base/media_message_loop.h"
31 #include "chromecast/public/cast_media_shlib.h"
32 #include "chromecast/public/media/media_pipeline_backend.h"
33 #include "components/crash/content/app/breakpad_linux.h"
34 #include "components/crash/content/browser/crash_handler_host_linux.h"
35 #include "components/network_hints/browser/network_hints_message_filter.h"
36 #include "content/public/browser/browser_thread.h"
37 #include "content/public/browser/certificate_request_result_type.h"
38 #include "content/public/browser/client_certificate_delegate.h"
39 #include "content/public/browser/render_process_host.h"
40 #include "content/public/browser/resource_dispatcher_host.h"
41 #include "content/public/browser/web_contents.h"
42 #include "content/public/common/content_descriptors.h"
43 #include "content/public/common/content_switches.h"
44 #include "content/public/common/url_constants.h"
45 #include "content/public/common/web_preferences.h"
46 #include "media/audio/audio_manager_factory.h"
47 #include "net/ssl/ssl_cert_request_info.h"
48 #include "net/url_request/url_request_context_getter.h"
49 #include "ui/gl/gl_switches.h"
51 #if defined(OS_ANDROID)
52 #include "components/crash/content/browser/crash_dump_manager_android.h"
53 #include "components/external_video_surface/browser/android/external_video_surface_container_impl.h"
54 #else
55 #include "chromecast/browser/media/cast_browser_cdm_factory.h"
56 #endif // defined(OS_ANDROID)
58 namespace chromecast {
59 namespace shell {
61 CastContentBrowserClient::CastContentBrowserClient()
62 : url_request_context_factory_(new URLRequestContextFactory()) {
65 CastContentBrowserClient::~CastContentBrowserClient() {
66 content::BrowserThread::DeleteSoon(
67 content::BrowserThread::IO,
68 FROM_HERE,
69 url_request_context_factory_.release());
72 void CastContentBrowserClient::AppendExtraCommandLineSwitches(
73 base::CommandLine* command_line) {
76 scoped_ptr<CastService> CastContentBrowserClient::CreateCastService(
77 content::BrowserContext* browser_context,
78 PrefService* pref_service,
79 net::URLRequestContextGetter* request_context_getter) {
80 return make_scoped_ptr(new CastServiceSimple(browser_context, pref_service));
83 scoped_ptr<::media::AudioManagerFactory>
84 CastContentBrowserClient::CreateAudioManagerFactory() {
85 #if defined(OS_ANDROID)
86 // Return nullptr. The factory will not be set, and the default
87 // implementation of AudioManager will be used.
88 return scoped_ptr<::media::AudioManagerFactory>();
89 #else
90 return make_scoped_ptr(new media::CastAudioManagerFactory());
91 #endif
94 #if !defined(OS_ANDROID)
95 scoped_refptr<media::CmaMediaPipelineClient>
96 CastContentBrowserClient::CreateCmaMediaPipelineClient() {
97 return make_scoped_refptr(new media::CmaMediaPipelineClient());
100 scoped_ptr<::media::BrowserCdmFactory>
101 CastContentBrowserClient::CreateBrowserCdmFactory() {
102 return make_scoped_ptr(new media::CastBrowserCdmFactory());
104 #endif
106 void CastContentBrowserClient::ProcessExiting() {
107 // Finalize CastMediaShlib on media thread to ensure it's not accessed
108 // after Finalize.
109 media::MediaMessageLoop::GetTaskRunner()->PostTask(
110 FROM_HERE, base::Bind(&media::CastMediaShlib::Finalize));
113 void CastContentBrowserClient::SetMetricsClientId(
114 const std::string& client_id) {
117 void CastContentBrowserClient::RegisterMetricsProviders(
118 ::metrics::MetricsService* metrics_service) {
121 bool CastContentBrowserClient::EnableRemoteDebuggingImmediately() {
122 return true;
125 content::BrowserMainParts* CastContentBrowserClient::CreateBrowserMainParts(
126 const content::MainFunctionParams& parameters) {
127 content::BrowserMainParts* parts = new CastBrowserMainParts(
128 parameters, url_request_context_factory_.get(),
129 CreateAudioManagerFactory());
130 CastBrowserProcess::GetInstance()->SetCastContentBrowserClient(this);
131 return parts;
134 void CastContentBrowserClient::RenderProcessWillLaunch(
135 content::RenderProcessHost* host) {
136 #if !defined(OS_ANDROID)
137 scoped_refptr<media::CmaMessageFilterHost> cma_message_filter(
138 new media::CmaMessageFilterHost(host->GetID(),
139 CreateCmaMediaPipelineClient()));
140 host->AddFilter(cma_message_filter.get());
141 #endif // !defined(OS_ANDROID)
143 // Forcibly trigger I/O-thread URLRequestContext initialization before
144 // getting HostResolver.
145 content::BrowserThread::PostTaskAndReplyWithResult(
146 content::BrowserThread::IO, FROM_HERE,
147 base::Bind(&net::URLRequestContextGetter::GetURLRequestContext,
148 base::Unretained(
149 url_request_context_factory_->GetSystemGetter())),
150 base::Bind(&CastContentBrowserClient::AddNetworkHintsMessageFilter,
151 base::Unretained(this), host->GetID()));
154 void CastContentBrowserClient::AddNetworkHintsMessageFilter(
155 int render_process_id, net::URLRequestContext* context) {
156 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
158 content::RenderProcessHost* host =
159 content::RenderProcessHost::FromID(render_process_id);
160 if (!host)
161 return;
163 scoped_refptr<content::BrowserMessageFilter> network_hints_message_filter(
164 new network_hints::NetworkHintsMessageFilter(
165 url_request_context_factory_->host_resolver()));
166 host->AddFilter(network_hints_message_filter.get());
169 net::URLRequestContextGetter* CastContentBrowserClient::CreateRequestContext(
170 content::BrowserContext* browser_context,
171 content::ProtocolHandlerMap* protocol_handlers,
172 content::URLRequestInterceptorScopedVector request_interceptors) {
173 return url_request_context_factory_->CreateMainGetter(
174 browser_context,
175 protocol_handlers,
176 request_interceptors.Pass());
179 bool CastContentBrowserClient::IsHandledURL(const GURL& url) {
180 if (!url.is_valid())
181 return false;
183 static const char* const kProtocolList[] = {
184 url::kBlobScheme,
185 url::kFileSystemScheme,
186 content::kChromeUIScheme,
187 content::kChromeDevToolsScheme,
188 url::kDataScheme,
191 const std::string& scheme = url.scheme();
192 for (size_t i = 0; i < arraysize(kProtocolList); ++i) {
193 if (scheme == kProtocolList[i])
194 return true;
197 if (scheme == url::kFileScheme) {
198 return base::CommandLine::ForCurrentProcess()->HasSwitch(
199 switches::kEnableLocalFileAccesses);
202 return false;
205 void CastContentBrowserClient::AppendExtraCommandLineSwitches(
206 base::CommandLine* command_line,
207 int child_process_id) {
208 std::string process_type =
209 command_line->GetSwitchValueNative(switches::kProcessType);
210 base::CommandLine* browser_command_line =
211 base::CommandLine::ForCurrentProcess();
213 // IsCrashReporterEnabled() is set when InitCrashReporter() is called, and
214 // controlled by GetBreakpadClient()->EnableBreakpadForProcess(), therefore
215 // it's ok to add switch to every process here.
216 if (breakpad::IsCrashReporterEnabled()) {
217 command_line->AppendSwitch(switches::kEnableCrashReporter);
220 // Renderer process command-line
221 if (process_type == switches::kRendererProcess) {
222 // Any browser command-line switches that should be propagated to
223 // the renderer go here.
225 if (browser_command_line->HasSwitch(switches::kEnableCmaMediaPipeline))
226 command_line->AppendSwitch(switches::kEnableCmaMediaPipeline);
227 if (browser_command_line->HasSwitch(switches::kEnableLegacyHolePunching))
228 command_line->AppendSwitch(switches::kEnableLegacyHolePunching);
229 if (browser_command_line->HasSwitch(switches::kAllowHiddenMediaPlayback))
230 command_line->AppendSwitch(switches::kAllowHiddenMediaPlayback);
233 #if defined(OS_LINUX)
234 // Necessary for accelerated 2d canvas. By default on Linux, Chromium assumes
235 // GLES2 contexts can be lost to a power-save mode, which breaks GPU canvas
236 // apps.
237 if (process_type == switches::kGpuProcess) {
238 command_line->AppendSwitch(switches::kGpuNoContextLost);
240 #endif
242 AppendExtraCommandLineSwitches(command_line);
245 content::AccessTokenStore* CastContentBrowserClient::CreateAccessTokenStore() {
246 return new CastAccessTokenStore(
247 CastBrowserProcess::GetInstance()->browser_context());
250 void CastContentBrowserClient::OverrideWebkitPrefs(
251 content::RenderViewHost* render_view_host,
252 content::WebPreferences* prefs) {
253 prefs->allow_scripts_to_close_windows = true;
254 // TODO(lcwu): http://crbug.com/391089. This pref is set to true by default
255 // because some content providers such as YouTube use plain http requests
256 // to retrieve media data chunks while running in a https page. This pref
257 // should be disabled once all the content providers are no longer doing that.
258 prefs->allow_running_insecure_content = true;
261 void CastContentBrowserClient::ResourceDispatcherHostCreated() {
262 CastBrowserProcess::GetInstance()->SetResourceDispatcherHostDelegate(
263 make_scoped_ptr(new CastResourceDispatcherHostDelegate));
264 content::ResourceDispatcherHost::Get()->SetDelegate(
265 CastBrowserProcess::GetInstance()->resource_dispatcher_host_delegate());
268 std::string CastContentBrowserClient::GetApplicationLocale() {
269 const std::string locale(base::i18n::GetConfiguredLocale());
270 return locale.empty() ? "en-US" : locale;
273 content::QuotaPermissionContext*
274 CastContentBrowserClient::CreateQuotaPermissionContext() {
275 return new CastQuotaPermissionContext();
278 void CastContentBrowserClient::AllowCertificateError(
279 int render_process_id,
280 int render_view_id,
281 int cert_error,
282 const net::SSLInfo& ssl_info,
283 const GURL& request_url,
284 content::ResourceType resource_type,
285 bool overridable,
286 bool strict_enforcement,
287 bool expired_previous_decision,
288 const base::Callback<void(bool)>& callback,
289 content::CertificateRequestResultType* result) {
290 // Allow developers to override certificate errors.
291 // Otherwise, any fatal certificate errors will cause an abort.
292 *result = content::CERTIFICATE_REQUEST_RESULT_TYPE_CANCEL;
293 return;
296 void CastContentBrowserClient::SelectClientCertificate(
297 content::WebContents* web_contents,
298 net::SSLCertRequestInfo* cert_request_info,
299 scoped_ptr<content::ClientCertificateDelegate> delegate) {
300 GURL requesting_url("https://" + cert_request_info->host_and_port.ToString());
302 if (!requesting_url.is_valid()) {
303 LOG(ERROR) << "Invalid URL string: "
304 << requesting_url.possibly_invalid_spec();
305 delegate->ContinueWithCertificate(nullptr);
306 return;
309 // In our case there are no relevant certs in the cert_request_info. The cert
310 // we need to return (if permitted) is the Cast device cert, which we can
311 // access directly through the ClientAuthSigner instance. However, we need to
312 // be on the IO thread to determine whether the app is whitelisted to return
313 // it, because CastNetworkDelegate is bound to the IO thread.
314 // Subsequently, the callback must then itself be performed back here
315 // on the UI thread.
317 // TODO(davidben): Stop using child ID to identify an app.
318 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
319 content::BrowserThread::PostTaskAndReplyWithResult(
320 content::BrowserThread::IO, FROM_HERE,
321 base::Bind(&CastContentBrowserClient::SelectClientCertificateOnIOThread,
322 base::Unretained(this), requesting_url,
323 web_contents->GetRenderProcessHost()->GetID()),
324 base::Bind(&content::ClientCertificateDelegate::ContinueWithCertificate,
325 base::Owned(delegate.release())));
328 net::X509Certificate*
329 CastContentBrowserClient::SelectClientCertificateOnIOThread(
330 GURL requesting_url,
331 int render_process_id) {
332 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
333 CastNetworkDelegate* network_delegate =
334 url_request_context_factory_->app_network_delegate();
335 if (network_delegate->IsWhitelisted(requesting_url,
336 render_process_id, false)) {
337 return CastNetworkDelegate::DeviceCert();
338 } else {
339 LOG(ERROR) << "Invalid host for client certificate request: "
340 << requesting_url.host()
341 << " with render_process_id: "
342 << render_process_id;
343 return NULL;
347 bool CastContentBrowserClient::CanCreateWindow(
348 const GURL& opener_url,
349 const GURL& opener_top_level_frame_url,
350 const GURL& source_origin,
351 WindowContainerType container_type,
352 const GURL& target_url,
353 const content::Referrer& referrer,
354 WindowOpenDisposition disposition,
355 const blink::WebWindowFeatures& features,
356 bool user_gesture,
357 bool opener_suppressed,
358 content::ResourceContext* context,
359 int render_process_id,
360 int opener_render_view_id,
361 int opener_render_frame_id,
362 bool* no_javascript_access) {
363 *no_javascript_access = true;
364 return false;
367 void CastContentBrowserClient::RegisterUnsandboxedOutOfProcessMojoApplications(
368 std::map<GURL, base::string16>* apps) {
369 #if defined(ENABLE_MOJO_MEDIA_IN_UTILITY_PROCESS)
370 apps->insert(std::make_pair(GURL("mojo:media"),
371 base::UTF8ToUTF16("Media Renderer")));
372 #endif
375 #if defined(OS_ANDROID)
376 void CastContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
377 const base::CommandLine& command_line,
378 int child_process_id,
379 content::FileDescriptorInfo* mappings,
380 std::map<int, base::MemoryMappedFile::Region>* regions) {
381 mappings->Share(
382 kAndroidPakDescriptor,
383 base::GlobalDescriptors::GetInstance()->Get(kAndroidPakDescriptor));
384 regions->insert(std::make_pair(
385 kAndroidPakDescriptor, base::GlobalDescriptors::GetInstance()->GetRegion(
386 kAndroidPakDescriptor)));
388 if (breakpad::IsCrashReporterEnabled()) {
389 base::File minidump_file(
390 breakpad::CrashDumpManager::GetInstance()->CreateMinidumpFile(
391 child_process_id));
392 if (!minidump_file.IsValid()) {
393 LOG(ERROR) << "Failed to create file for minidump, crash reporting will "
394 << "be disabled for this process.";
395 } else {
396 mappings->Transfer(kAndroidMinidumpDescriptor,
397 base::ScopedFD(minidump_file.TakePlatformFile()));
401 #else
402 void CastContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
403 const base::CommandLine& command_line,
404 int child_process_id,
405 content::FileDescriptorInfo* mappings) {
406 int crash_signal_fd = GetCrashSignalFD(command_line);
407 if (crash_signal_fd >= 0) {
408 mappings->Share(kCrashDumpSignal, crash_signal_fd);
411 #endif // defined(OS_ANDROID)
413 #if defined(OS_ANDROID) && defined(VIDEO_HOLE)
414 content::ExternalVideoSurfaceContainer*
415 CastContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
416 content::WebContents* web_contents) {
417 return external_video_surface::ExternalVideoSurfaceContainerImpl::Create(
418 web_contents);
420 #endif // defined(OS_ANDROID) && defined(VIDEO_HOLE)
422 #if !defined(OS_ANDROID)
423 int CastContentBrowserClient::GetCrashSignalFD(
424 const base::CommandLine& command_line) {
425 std::string process_type =
426 command_line.GetSwitchValueASCII(switches::kProcessType);
428 if (process_type == switches::kRendererProcess ||
429 process_type == switches::kGpuProcess) {
430 breakpad::CrashHandlerHostLinux* crash_handler =
431 crash_handlers_[process_type];
432 if (!crash_handler) {
433 crash_handler = CreateCrashHandlerHost(process_type);
434 crash_handlers_[process_type] = crash_handler;
436 return crash_handler->GetDeathSignalSocket();
439 return -1;
442 breakpad::CrashHandlerHostLinux*
443 CastContentBrowserClient::CreateCrashHandlerHost(
444 const std::string& process_type) {
445 // Let cast shell dump to /tmp. Internal minidump generator code can move it
446 // to /data/minidumps later, since /data/minidumps is file lock-controlled.
447 base::FilePath dumps_path;
448 PathService::Get(base::DIR_TEMP, &dumps_path);
450 // Alway set "upload" to false to use our own uploader.
451 breakpad::CrashHandlerHostLinux* crash_handler =
452 new breakpad::CrashHandlerHostLinux(
453 process_type, dumps_path, false /* upload */);
454 // StartUploaderThread() even though upload is diferred.
455 // Breakpad-related memory is freed in the uploader thread.
456 crash_handler->StartUploaderThread();
457 return crash_handler;
459 #endif // !defined(OS_ANDROID)
461 } // namespace shell
462 } // namespace chromecast