Update frame around SAML IdP pages to new GAIA style
[chromium-blink-merge.git] / chromecast / browser / cast_content_browser_client.cc
blob3234a597e1b5f7780aa1ac1b484feabeadba41ab
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/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/devtools/cast_dev_tools_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/chromecast_switches.h"
27 #include "chromecast/common/global_descriptors.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 "gin/v8_initializer.h"
42 #include "net/ssl/ssl_cert_request_info.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 #endif // defined(OS_ANDROID)
49 #if defined(OS_ANDROID)
50 #include "components/crash/browser/crash_dump_manager_android.h"
51 #endif // defined(OS_ANDROID)
53 namespace chromecast {
54 namespace shell {
56 CastContentBrowserClient::CastContentBrowserClient()
57 : v8_natives_fd_(-1),
58 v8_snapshot_fd_(-1),
59 url_request_context_factory_(new URLRequestContextFactory()) {
62 CastContentBrowserClient::~CastContentBrowserClient() {
63 content::BrowserThread::DeleteSoon(
64 content::BrowserThread::IO,
65 FROM_HERE,
66 url_request_context_factory_.release());
69 content::BrowserMainParts* CastContentBrowserClient::CreateBrowserMainParts(
70 const content::MainFunctionParams& parameters) {
71 return new CastBrowserMainParts(parameters,
72 url_request_context_factory_.get());
75 void CastContentBrowserClient::RenderProcessWillLaunch(
76 content::RenderProcessHost* host) {
77 scoped_refptr<content::BrowserMessageFilter> network_hints_message_filter(
78 new network_hints::NetworkHintsMessageFilter(
79 url_request_context_factory_->host_resolver()));
80 host->AddFilter(network_hints_message_filter.get());
81 #if !defined(OS_ANDROID)
82 scoped_refptr<media::CmaMessageFilterHost> cma_message_filter(
83 new media::CmaMessageFilterHost(host->GetID()));
84 host->AddFilter(cma_message_filter.get());
85 #endif // !defined(OS_ANDROID)
87 auto extra_filters = PlatformGetBrowserMessageFilters();
88 for (auto const& filter : extra_filters) {
89 host->AddFilter(filter.get());
93 net::URLRequestContextGetter* CastContentBrowserClient::CreateRequestContext(
94 content::BrowserContext* browser_context,
95 content::ProtocolHandlerMap* protocol_handlers,
96 content::URLRequestInterceptorScopedVector request_interceptors) {
97 return url_request_context_factory_->CreateMainGetter(
98 browser_context,
99 protocol_handlers,
100 request_interceptors.Pass());
103 bool CastContentBrowserClient::IsHandledURL(const GURL& url) {
104 if (!url.is_valid())
105 return false;
107 static const char* const kProtocolList[] = {
108 url::kBlobScheme,
109 url::kFileSystemScheme,
110 content::kChromeUIScheme,
111 content::kChromeDevToolsScheme,
112 url::kDataScheme,
115 const std::string& scheme = url.scheme();
116 for (size_t i = 0; i < arraysize(kProtocolList); ++i) {
117 if (scheme == kProtocolList[i])
118 return true;
121 if (scheme == url::kFileScheme) {
122 return base::CommandLine::ForCurrentProcess()->HasSwitch(
123 switches::kEnableLocalFileAccesses);
126 return false;
129 void CastContentBrowserClient::AppendExtraCommandLineSwitches(
130 base::CommandLine* command_line,
131 int child_process_id) {
133 std::string process_type =
134 command_line->GetSwitchValueNative(switches::kProcessType);
135 base::CommandLine* browser_command_line =
136 base::CommandLine::ForCurrentProcess();
138 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
139 if (process_type != switches::kZygoteProcess) {
140 command_line->AppendSwitch(::switches::kV8NativesPassedByFD);
141 command_line->AppendSwitch(::switches::kV8SnapshotPassedByFD);
143 #endif // V8_USE_EXTERNAL_STARTUP_DATA
145 // IsCrashReporterEnabled() is set when InitCrashReporter() is called, and
146 // controlled by GetBreakpadClient()->EnableBreakpadForProcess(), therefore
147 // it's ok to add switch to every process here.
148 if (breakpad::IsCrashReporterEnabled()) {
149 command_line->AppendSwitch(switches::kEnableCrashReporter);
152 // Renderer process command-line
153 if (process_type == switches::kRendererProcess) {
154 // Any browser command-line switches that should be propagated to
155 // the renderer go here.
157 if (browser_command_line->HasSwitch(switches::kEnableCmaMediaPipeline))
158 command_line->AppendSwitch(switches::kEnableCmaMediaPipeline);
161 #if defined(OS_LINUX)
162 // Necessary for accelerated 2d canvas. By default on Linux, Chromium assumes
163 // GLES2 contexts can be lost to a power-save mode, which breaks GPU canvas
164 // apps.
165 if (process_type == switches::kGpuProcess) {
166 command_line->AppendSwitch(switches::kGpuNoContextLost);
168 #endif
170 PlatformAppendExtraCommandLineSwitches(command_line);
173 content::AccessTokenStore* CastContentBrowserClient::CreateAccessTokenStore() {
174 return new CastAccessTokenStore(
175 CastBrowserProcess::GetInstance()->browser_context());
178 void CastContentBrowserClient::OverrideWebkitPrefs(
179 content::RenderViewHost* render_view_host,
180 content::WebPreferences* prefs) {
181 prefs->allow_scripts_to_close_windows = true;
182 // TODO(lcwu): http://crbug.com/391089. This pref is set to true by default
183 // because some content providers such as YouTube use plain http requests
184 // to retrieve media data chunks while running in a https page. This pref
185 // should be disabled once all the content providers are no longer doing that.
186 prefs->allow_running_insecure_content = true;
189 void CastContentBrowserClient::ResourceDispatcherHostCreated() {
190 CastBrowserProcess::GetInstance()->SetResourceDispatcherHostDelegate(
191 make_scoped_ptr(new CastResourceDispatcherHostDelegate));
192 content::ResourceDispatcherHost::Get()->SetDelegate(
193 CastBrowserProcess::GetInstance()->resource_dispatcher_host_delegate());
196 std::string CastContentBrowserClient::GetApplicationLocale() {
197 const std::string locale(base::i18n::GetConfiguredLocale());
198 return locale.empty() ? "en-US" : locale;
201 content::QuotaPermissionContext*
202 CastContentBrowserClient::CreateQuotaPermissionContext() {
203 return new CastQuotaPermissionContext();
206 void CastContentBrowserClient::AllowCertificateError(
207 int render_process_id,
208 int render_view_id,
209 int cert_error,
210 const net::SSLInfo& ssl_info,
211 const GURL& request_url,
212 content::ResourceType resource_type,
213 bool overridable,
214 bool strict_enforcement,
215 bool expired_previous_decision,
216 const base::Callback<void(bool)>& callback,
217 content::CertificateRequestResultType* result) {
218 // Allow developers to override certificate errors.
219 // Otherwise, any fatal certificate errors will cause an abort.
220 *result = content::CERTIFICATE_REQUEST_RESULT_TYPE_CANCEL;
221 return;
224 void CastContentBrowserClient::SelectClientCertificate(
225 content::WebContents* web_contents,
226 net::SSLCertRequestInfo* cert_request_info,
227 scoped_ptr<content::ClientCertificateDelegate> delegate) {
228 GURL requesting_url("https://" + cert_request_info->host_and_port.ToString());
230 if (!requesting_url.is_valid()) {
231 LOG(ERROR) << "Invalid URL string: "
232 << requesting_url.possibly_invalid_spec();
233 delegate->ContinueWithCertificate(nullptr);
234 return;
237 // In our case there are no relevant certs in the cert_request_info. The cert
238 // we need to return (if permitted) is the Cast device cert, which we can
239 // access directly through the ClientAuthSigner instance. However, we need to
240 // be on the IO thread to determine whether the app is whitelisted to return
241 // it, because CastNetworkDelegate is bound to the IO thread.
242 // Subsequently, the callback must then itself be performed back here
243 // on the UI thread.
245 // TODO(davidben): Stop using child ID to identify an app.
246 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
247 content::BrowserThread::PostTaskAndReplyWithResult(
248 content::BrowserThread::IO, FROM_HERE,
249 base::Bind(&CastContentBrowserClient::SelectClientCertificateOnIOThread,
250 base::Unretained(this), requesting_url,
251 web_contents->GetRenderProcessHost()->GetID()),
252 base::Bind(&content::ClientCertificateDelegate::ContinueWithCertificate,
253 base::Owned(delegate.release())));
256 net::X509Certificate*
257 CastContentBrowserClient::SelectClientCertificateOnIOThread(
258 GURL requesting_url,
259 int render_process_id) {
260 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
261 CastNetworkDelegate* network_delegate =
262 url_request_context_factory_->app_network_delegate();
263 if (network_delegate->IsWhitelisted(requesting_url,
264 render_process_id, false)) {
265 return CastNetworkDelegate::DeviceCert();
266 } else {
267 LOG(ERROR) << "Invalid host for client certificate request: "
268 << requesting_url.host()
269 << " with render_process_id: "
270 << render_process_id;
271 return NULL;
275 bool CastContentBrowserClient::CanCreateWindow(
276 const GURL& opener_url,
277 const GURL& opener_top_level_frame_url,
278 const GURL& source_origin,
279 WindowContainerType container_type,
280 const GURL& target_url,
281 const content::Referrer& referrer,
282 WindowOpenDisposition disposition,
283 const blink::WebWindowFeatures& features,
284 bool user_gesture,
285 bool opener_suppressed,
286 content::ResourceContext* context,
287 int render_process_id,
288 int opener_id,
289 bool* no_javascript_access) {
290 *no_javascript_access = true;
291 return false;
294 content::DevToolsManagerDelegate*
295 CastContentBrowserClient::GetDevToolsManagerDelegate() {
296 return new CastDevToolsManagerDelegate();
299 void CastContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
300 const base::CommandLine& command_line,
301 int child_process_id,
302 content::FileDescriptorInfo* mappings) {
303 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
304 if (v8_natives_fd_.get() == -1 || v8_snapshot_fd_.get() == -1) {
305 int v8_natives_fd = -1;
306 int v8_snapshot_fd = -1;
307 if (gin::V8Initializer::OpenV8FilesForChildProcesses(&v8_natives_fd,
308 &v8_snapshot_fd)) {
309 v8_natives_fd_.reset(v8_natives_fd);
310 v8_snapshot_fd_.reset(v8_snapshot_fd);
313 DCHECK(v8_natives_fd_.get() != -1 && v8_snapshot_fd_.get() != -1);
314 mappings->Share(kV8NativesDataDescriptor, v8_natives_fd_.get());
315 mappings->Share(kV8SnapshotDataDescriptor, v8_snapshot_fd_.get());
316 #endif // V8_USE_EXTERNAL_STARTUP_DATA
317 #if defined(OS_ANDROID)
318 const int flags_open_read = base::File::FLAG_OPEN | base::File::FLAG_READ;
319 base::FilePath pak_file_path;
320 CHECK(PathService::Get(FILE_CAST_PAK, &pak_file_path));
321 base::File pak_file(pak_file_path, flags_open_read);
322 if (!pak_file.IsValid()) {
323 NOTREACHED() << "Failed to open file when creating renderer process: "
324 << "cast_shell.pak";
326 mappings->Transfer(kAndroidPakDescriptor,
327 base::ScopedFD(pak_file.TakePlatformFile()));
329 if (breakpad::IsCrashReporterEnabled()) {
330 base::File minidump_file(
331 breakpad::CrashDumpManager::GetInstance()->CreateMinidumpFile(
332 child_process_id));
333 if (!minidump_file.IsValid()) {
334 LOG(ERROR) << "Failed to create file for minidump, crash reporting will "
335 << "be disabled for this process.";
336 } else {
337 mappings->Transfer(kAndroidMinidumpDescriptor,
338 base::ScopedFD(minidump_file.TakePlatformFile()));
342 base::FilePath app_data_path;
343 CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &app_data_path));
344 base::FilePath icudata_path =
345 app_data_path.AppendASCII(base::i18n::kIcuDataFileName);
346 base::File icudata_file(icudata_path, flags_open_read);
347 if (!icudata_file.IsValid())
348 NOTREACHED() << "Failed to open ICU file when creating renderer process";
349 mappings->Transfer(kAndroidICUDataDescriptor,
350 base::ScopedFD(icudata_file.TakePlatformFile()));
351 #else
352 int crash_signal_fd = GetCrashSignalFD(command_line);
353 if (crash_signal_fd >= 0) {
354 mappings->Share(kCrashDumpSignal, crash_signal_fd);
356 #endif // defined(OS_ANDROID)
359 #if defined(OS_ANDROID) && defined(VIDEO_HOLE)
360 content::ExternalVideoSurfaceContainer*
361 CastContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
362 content::WebContents* web_contents) {
363 return ExternalVideoSurfaceContainerImpl::Create(web_contents);
365 #endif // defined(OS_ANDROID) && defined(VIDEO_HOLE)
367 #if !defined(OS_ANDROID)
368 int CastContentBrowserClient::GetCrashSignalFD(
369 const base::CommandLine& command_line) {
370 std::string process_type =
371 command_line.GetSwitchValueASCII(switches::kProcessType);
373 if (process_type == switches::kRendererProcess ||
374 process_type == switches::kGpuProcess) {
375 breakpad::CrashHandlerHostLinux* crash_handler =
376 crash_handlers_[process_type];
377 if (!crash_handler) {
378 crash_handler = CreateCrashHandlerHost(process_type);
379 crash_handlers_[process_type] = crash_handler;
381 return crash_handler->GetDeathSignalSocket();
384 return -1;
387 breakpad::CrashHandlerHostLinux*
388 CastContentBrowserClient::CreateCrashHandlerHost(
389 const std::string& process_type) {
390 // Let cast shell dump to /tmp. Internal minidump generator code can move it
391 // to /data/minidumps later, since /data/minidumps is file lock-controlled.
392 base::FilePath dumps_path;
393 PathService::Get(base::DIR_TEMP, &dumps_path);
395 // Alway set "upload" to false to use our own uploader.
396 breakpad::CrashHandlerHostLinux* crash_handler =
397 new breakpad::CrashHandlerHostLinux(
398 process_type, dumps_path, false /* upload */);
399 // StartUploaderThread() even though upload is diferred.
400 // Breakpad-related memory is freed in the uploader thread.
401 crash_handler->StartUploaderThread();
402 return crash_handler;
404 #endif // !defined(OS_ANDROID)
406 } // namespace shell
407 } // namespace chromecast