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