Make synthesizeScrollGesture repetable and emit interaction markers
[chromium-blink-merge.git] / chromecast / browser / cast_content_browser_client.cc
blobe445c1e1ab0a2e46c5ccc4bf583368b6b324234c
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 "chromecast/base/cast_paths.h"
15 #include "chromecast/base/chromecast_switches.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/global_descriptors.h"
26 #include "chromecast/public/cast_media_shlib.h"
27 #include "chromecast/public/media/media_pipeline_backend.h"
28 #include "components/crash/app/breakpad_linux.h"
29 #include "components/crash/browser/crash_handler_host_linux.h"
30 #include "components/network_hints/browser/network_hints_message_filter.h"
31 #include "content/public/browser/browser_thread.h"
32 #include "content/public/browser/certificate_request_result_type.h"
33 #include "content/public/browser/client_certificate_delegate.h"
34 #include "content/public/browser/render_process_host.h"
35 #include "content/public/browser/resource_dispatcher_host.h"
36 #include "content/public/browser/web_contents.h"
37 #include "content/public/common/content_descriptors.h"
38 #include "content/public/common/content_switches.h"
39 #include "content/public/common/url_constants.h"
40 #include "content/public/common/web_preferences.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 {
52 namespace shell {
54 CastContentBrowserClient::CastContentBrowserClient()
55 : url_request_context_factory_(new URLRequestContextFactory()) {
58 CastContentBrowserClient::~CastContentBrowserClient() {
59 content::BrowserThread::DeleteSoon(
60 content::BrowserThread::IO,
61 FROM_HERE,
62 url_request_context_factory_.release());
65 void CastContentBrowserClient::AppendExtraCommandLineSwitches(
66 base::CommandLine* command_line) {
69 std::vector<scoped_refptr<content::BrowserMessageFilter>>
70 CastContentBrowserClient::GetBrowserMessageFilters() {
71 return std::vector<scoped_refptr<content::BrowserMessageFilter>>();
74 scoped_ptr<::media::AudioManagerFactory>
75 CastContentBrowserClient::CreateAudioManagerFactory() {
76 // Return nullptr. The factory will not be set, and the statically linked
77 // implementation of AudioManager will be used.
78 return scoped_ptr<::media::AudioManagerFactory>();
81 #if !defined(OS_ANDROID)
82 scoped_ptr<media::MediaPipelineBackend>
83 CastContentBrowserClient::CreateMediaPipelineBackend(
84 const media::MediaPipelineDeviceParams& params) {
85 return make_scoped_ptr(
86 media::CastMediaShlib::CreateMediaPipelineBackend(params));
88 #endif
90 content::BrowserMainParts* CastContentBrowserClient::CreateBrowserMainParts(
91 const content::MainFunctionParams& parameters) {
92 return new CastBrowserMainParts(parameters,
93 url_request_context_factory_.get(),
94 CreateAudioManagerFactory());
97 void CastContentBrowserClient::RenderProcessWillLaunch(
98 content::RenderProcessHost* host) {
99 #if !defined(OS_ANDROID)
100 scoped_refptr<media::CmaMessageFilterHost> cma_message_filter(
101 new media::CmaMessageFilterHost(
102 host->GetID(),
103 base::Bind(&CastContentBrowserClient::CreateMediaPipelineBackend,
104 base::Unretained(this))));
105 host->AddFilter(cma_message_filter.get());
106 #endif // !defined(OS_ANDROID)
108 // Forcibly trigger I/O-thread URLRequestContext initialization before
109 // getting HostResolver.
110 content::BrowserThread::PostTaskAndReplyWithResult(
111 content::BrowserThread::IO, FROM_HERE,
112 base::Bind(&net::URLRequestContextGetter::GetURLRequestContext,
113 base::Unretained(
114 url_request_context_factory_->GetSystemGetter())),
115 base::Bind(&CastContentBrowserClient::AddNetworkHintsMessageFilter,
116 base::Unretained(this), host->GetID()));
118 auto extra_filters = GetBrowserMessageFilters();
119 for (auto const& filter : extra_filters) {
120 host->AddFilter(filter.get());
124 void CastContentBrowserClient::AddNetworkHintsMessageFilter(
125 int render_process_id, net::URLRequestContext* context) {
126 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
128 content::RenderProcessHost* host =
129 content::RenderProcessHost::FromID(render_process_id);
130 if (!host)
131 return;
133 scoped_refptr<content::BrowserMessageFilter> network_hints_message_filter(
134 new network_hints::NetworkHintsMessageFilter(
135 url_request_context_factory_->host_resolver()));
136 host->AddFilter(network_hints_message_filter.get());
139 net::URLRequestContextGetter* CastContentBrowserClient::CreateRequestContext(
140 content::BrowserContext* browser_context,
141 content::ProtocolHandlerMap* protocol_handlers,
142 content::URLRequestInterceptorScopedVector request_interceptors) {
143 return url_request_context_factory_->CreateMainGetter(
144 browser_context,
145 protocol_handlers,
146 request_interceptors.Pass());
149 bool CastContentBrowserClient::IsHandledURL(const GURL& url) {
150 if (!url.is_valid())
151 return false;
153 static const char* const kProtocolList[] = {
154 url::kBlobScheme,
155 url::kFileSystemScheme,
156 content::kChromeUIScheme,
157 content::kChromeDevToolsScheme,
158 url::kDataScheme,
161 const std::string& scheme = url.scheme();
162 for (size_t i = 0; i < arraysize(kProtocolList); ++i) {
163 if (scheme == kProtocolList[i])
164 return true;
167 if (scheme == url::kFileScheme) {
168 return base::CommandLine::ForCurrentProcess()->HasSwitch(
169 switches::kEnableLocalFileAccesses);
172 return false;
175 void CastContentBrowserClient::AppendExtraCommandLineSwitches(
176 base::CommandLine* command_line,
177 int child_process_id) {
178 std::string process_type =
179 command_line->GetSwitchValueNative(switches::kProcessType);
180 base::CommandLine* browser_command_line =
181 base::CommandLine::ForCurrentProcess();
183 // IsCrashReporterEnabled() is set when InitCrashReporter() is called, and
184 // controlled by GetBreakpadClient()->EnableBreakpadForProcess(), therefore
185 // it's ok to add switch to every process here.
186 if (breakpad::IsCrashReporterEnabled()) {
187 command_line->AppendSwitch(switches::kEnableCrashReporter);
190 // Renderer process command-line
191 if (process_type == switches::kRendererProcess) {
192 // Any browser command-line switches that should be propagated to
193 // the renderer go here.
195 if (browser_command_line->HasSwitch(switches::kEnableCmaMediaPipeline))
196 command_line->AppendSwitch(switches::kEnableCmaMediaPipeline);
197 if (browser_command_line->HasSwitch(switches::kEnableLegacyHolePunching))
198 command_line->AppendSwitch(switches::kEnableLegacyHolePunching);
201 #if defined(OS_LINUX)
202 // Necessary for accelerated 2d canvas. By default on Linux, Chromium assumes
203 // GLES2 contexts can be lost to a power-save mode, which breaks GPU canvas
204 // apps.
205 if (process_type == switches::kGpuProcess) {
206 command_line->AppendSwitch(switches::kGpuNoContextLost);
208 #endif
210 AppendExtraCommandLineSwitches(command_line);
213 content::AccessTokenStore* CastContentBrowserClient::CreateAccessTokenStore() {
214 return new CastAccessTokenStore(
215 CastBrowserProcess::GetInstance()->browser_context());
218 void CastContentBrowserClient::OverrideWebkitPrefs(
219 content::RenderViewHost* render_view_host,
220 content::WebPreferences* prefs) {
221 prefs->allow_scripts_to_close_windows = true;
222 // TODO(lcwu): http://crbug.com/391089. This pref is set to true by default
223 // because some content providers such as YouTube use plain http requests
224 // to retrieve media data chunks while running in a https page. This pref
225 // should be disabled once all the content providers are no longer doing that.
226 prefs->allow_running_insecure_content = true;
229 void CastContentBrowserClient::ResourceDispatcherHostCreated() {
230 CastBrowserProcess::GetInstance()->SetResourceDispatcherHostDelegate(
231 make_scoped_ptr(new CastResourceDispatcherHostDelegate));
232 content::ResourceDispatcherHost::Get()->SetDelegate(
233 CastBrowserProcess::GetInstance()->resource_dispatcher_host_delegate());
236 std::string CastContentBrowserClient::GetApplicationLocale() {
237 const std::string locale(base::i18n::GetConfiguredLocale());
238 return locale.empty() ? "en-US" : locale;
241 content::QuotaPermissionContext*
242 CastContentBrowserClient::CreateQuotaPermissionContext() {
243 return new CastQuotaPermissionContext();
246 void CastContentBrowserClient::AllowCertificateError(
247 int render_process_id,
248 int render_view_id,
249 int cert_error,
250 const net::SSLInfo& ssl_info,
251 const GURL& request_url,
252 content::ResourceType resource_type,
253 bool overridable,
254 bool strict_enforcement,
255 bool expired_previous_decision,
256 const base::Callback<void(bool)>& callback,
257 content::CertificateRequestResultType* result) {
258 // Allow developers to override certificate errors.
259 // Otherwise, any fatal certificate errors will cause an abort.
260 *result = content::CERTIFICATE_REQUEST_RESULT_TYPE_CANCEL;
261 return;
264 void CastContentBrowserClient::SelectClientCertificate(
265 content::WebContents* web_contents,
266 net::SSLCertRequestInfo* cert_request_info,
267 scoped_ptr<content::ClientCertificateDelegate> delegate) {
268 GURL requesting_url("https://" + cert_request_info->host_and_port.ToString());
270 if (!requesting_url.is_valid()) {
271 LOG(ERROR) << "Invalid URL string: "
272 << requesting_url.possibly_invalid_spec();
273 delegate->ContinueWithCertificate(nullptr);
274 return;
277 // In our case there are no relevant certs in the cert_request_info. The cert
278 // we need to return (if permitted) is the Cast device cert, which we can
279 // access directly through the ClientAuthSigner instance. However, we need to
280 // be on the IO thread to determine whether the app is whitelisted to return
281 // it, because CastNetworkDelegate is bound to the IO thread.
282 // Subsequently, the callback must then itself be performed back here
283 // on the UI thread.
285 // TODO(davidben): Stop using child ID to identify an app.
286 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
287 content::BrowserThread::PostTaskAndReplyWithResult(
288 content::BrowserThread::IO, FROM_HERE,
289 base::Bind(&CastContentBrowserClient::SelectClientCertificateOnIOThread,
290 base::Unretained(this), requesting_url,
291 web_contents->GetRenderProcessHost()->GetID()),
292 base::Bind(&content::ClientCertificateDelegate::ContinueWithCertificate,
293 base::Owned(delegate.release())));
296 net::X509Certificate*
297 CastContentBrowserClient::SelectClientCertificateOnIOThread(
298 GURL requesting_url,
299 int render_process_id) {
300 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
301 CastNetworkDelegate* network_delegate =
302 url_request_context_factory_->app_network_delegate();
303 if (network_delegate->IsWhitelisted(requesting_url,
304 render_process_id, false)) {
305 return CastNetworkDelegate::DeviceCert();
306 } else {
307 LOG(ERROR) << "Invalid host for client certificate request: "
308 << requesting_url.host()
309 << " with render_process_id: "
310 << render_process_id;
311 return NULL;
315 bool CastContentBrowserClient::CanCreateWindow(
316 const GURL& opener_url,
317 const GURL& opener_top_level_frame_url,
318 const GURL& source_origin,
319 WindowContainerType container_type,
320 const GURL& target_url,
321 const content::Referrer& referrer,
322 WindowOpenDisposition disposition,
323 const blink::WebWindowFeatures& features,
324 bool user_gesture,
325 bool opener_suppressed,
326 content::ResourceContext* context,
327 int render_process_id,
328 int opener_render_view_id,
329 int opener_render_frame_id,
330 bool* no_javascript_access) {
331 *no_javascript_access = true;
332 return false;
335 void CastContentBrowserClient::RegisterUnsandboxedOutOfProcessMojoApplications(
336 std::vector<GURL>* urls) {
337 #if defined(ENABLE_MOJO_MEDIA_IN_UTILITY_PROCESS)
338 urls->push_back("mojo:media");
339 #endif
342 #if defined(OS_ANDROID)
343 void CastContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
344 const base::CommandLine& command_line,
345 int child_process_id,
346 content::FileDescriptorInfo* mappings,
347 std::map<int, base::MemoryMappedFile::Region>* regions) {
348 mappings->Share(
349 kAndroidPakDescriptor,
350 base::GlobalDescriptors::GetInstance()->Get(kAndroidPakDescriptor));
351 regions->insert(std::make_pair(
352 kAndroidPakDescriptor, base::GlobalDescriptors::GetInstance()->GetRegion(
353 kAndroidPakDescriptor)));
355 if (breakpad::IsCrashReporterEnabled()) {
356 base::File minidump_file(
357 breakpad::CrashDumpManager::GetInstance()->CreateMinidumpFile(
358 child_process_id));
359 if (!minidump_file.IsValid()) {
360 LOG(ERROR) << "Failed to create file for minidump, crash reporting will "
361 << "be disabled for this process.";
362 } else {
363 mappings->Transfer(kAndroidMinidumpDescriptor,
364 base::ScopedFD(minidump_file.TakePlatformFile()));
368 #else
369 void CastContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
370 const base::CommandLine& command_line,
371 int child_process_id,
372 content::FileDescriptorInfo* mappings) {
373 int crash_signal_fd = GetCrashSignalFD(command_line);
374 if (crash_signal_fd >= 0) {
375 mappings->Share(kCrashDumpSignal, crash_signal_fd);
378 #endif // defined(OS_ANDROID)
380 #if defined(OS_ANDROID) && defined(VIDEO_HOLE)
381 content::ExternalVideoSurfaceContainer*
382 CastContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
383 content::WebContents* web_contents) {
384 return external_video_surface::ExternalVideoSurfaceContainerImpl::Create(
385 web_contents);
387 #endif // defined(OS_ANDROID) && defined(VIDEO_HOLE)
389 #if !defined(OS_ANDROID)
390 int CastContentBrowserClient::GetCrashSignalFD(
391 const base::CommandLine& command_line) {
392 std::string process_type =
393 command_line.GetSwitchValueASCII(switches::kProcessType);
395 if (process_type == switches::kRendererProcess ||
396 process_type == switches::kGpuProcess) {
397 breakpad::CrashHandlerHostLinux* crash_handler =
398 crash_handlers_[process_type];
399 if (!crash_handler) {
400 crash_handler = CreateCrashHandlerHost(process_type);
401 crash_handlers_[process_type] = crash_handler;
403 return crash_handler->GetDeathSignalSocket();
406 return -1;
409 breakpad::CrashHandlerHostLinux*
410 CastContentBrowserClient::CreateCrashHandlerHost(
411 const std::string& process_type) {
412 // Let cast shell dump to /tmp. Internal minidump generator code can move it
413 // to /data/minidumps later, since /data/minidumps is file lock-controlled.
414 base::FilePath dumps_path;
415 PathService::Get(base::DIR_TEMP, &dumps_path);
417 // Alway set "upload" to false to use our own uploader.
418 breakpad::CrashHandlerHostLinux* crash_handler =
419 new breakpad::CrashHandlerHostLinux(
420 process_type, dumps_path, false /* upload */);
421 // StartUploaderThread() even though upload is diferred.
422 // Breakpad-related memory is freed in the uploader thread.
423 crash_handler->StartUploaderThread();
424 return crash_handler;
426 #endif // !defined(OS_ANDROID)
428 } // namespace shell
429 } // namespace chromecast