Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / chromecast / browser / cast_content_browser_client.cc
blob2fc59c2d3242aa58949fc24c384d7abcd2be0c85
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_resource_dispatcher_host_delegate.h"
21 #include "chromecast/browser/devtools/cast_dev_tools_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/chromecast_switches.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 "net/ssl/ssl_cert_request_info.h"
41 #include "ui/gl/gl_switches.h"
43 #if defined(OS_ANDROID)
44 #include "chromecast/browser/android/external_video_surface_container_impl.h"
45 #endif // defined(OS_ANDROID)
47 #if defined(OS_ANDROID)
48 #include "components/crash/browser/crash_dump_manager_android.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 content::BrowserMainParts* CastContentBrowserClient::CreateBrowserMainParts(
66 const content::MainFunctionParams& parameters) {
67 return new CastBrowserMainParts(parameters,
68 url_request_context_factory_.get());
71 void CastContentBrowserClient::RenderProcessWillLaunch(
72 content::RenderProcessHost* host) {
73 scoped_refptr<content::BrowserMessageFilter> network_hints_message_filter(
74 new network_hints::NetworkHintsMessageFilter(
75 url_request_context_factory_->host_resolver()));
76 host->AddFilter(network_hints_message_filter.get());
77 #if !defined(OS_ANDROID)
78 scoped_refptr<media::CmaMessageFilterHost> cma_message_filter(
79 new media::CmaMessageFilterHost(host->GetID()));
80 host->AddFilter(cma_message_filter.get());
81 #endif // !defined(OS_ANDROID)
83 auto extra_filters = PlatformGetBrowserMessageFilters();
84 for (auto const& filter : extra_filters) {
85 host->AddFilter(filter.get());
89 net::URLRequestContextGetter* CastContentBrowserClient::CreateRequestContext(
90 content::BrowserContext* browser_context,
91 content::ProtocolHandlerMap* protocol_handlers,
92 content::URLRequestInterceptorScopedVector request_interceptors) {
93 return url_request_context_factory_->CreateMainGetter(
94 browser_context,
95 protocol_handlers,
96 request_interceptors.Pass());
99 bool CastContentBrowserClient::IsHandledURL(const GURL& url) {
100 if (!url.is_valid())
101 return false;
103 static const char* const kProtocolList[] = {
104 url::kBlobScheme,
105 url::kFileSystemScheme,
106 content::kChromeUIScheme,
107 content::kChromeDevToolsScheme,
108 url::kDataScheme,
109 #if defined(OS_ANDROID)
110 url::kFileScheme,
111 #endif // defined(OS_ANDROID)
114 const std::string& scheme = url.scheme();
115 for (size_t i = 0; i < arraysize(kProtocolList); ++i) {
116 if (scheme == kProtocolList[i])
117 return true;
119 return false;
122 void CastContentBrowserClient::AppendExtraCommandLineSwitches(
123 base::CommandLine* command_line,
124 int child_process_id) {
126 std::string process_type =
127 command_line->GetSwitchValueNative(switches::kProcessType);
128 base::CommandLine* browser_command_line =
129 base::CommandLine::ForCurrentProcess();
131 // IsCrashReporterEnabled() is set when InitCrashReporter() is called, and
132 // controlled by GetBreakpadClient()->EnableBreakpadForProcess(), therefore
133 // it's ok to add switch to every process here.
134 if (breakpad::IsCrashReporterEnabled()) {
135 command_line->AppendSwitch(switches::kEnableCrashReporter);
138 // Renderer process command-line
139 if (process_type == switches::kRendererProcess) {
140 // Any browser command-line switches that should be propagated to
141 // the renderer go here.
143 if (browser_command_line->HasSwitch(switches::kEnableCmaMediaPipeline))
144 command_line->AppendSwitch(switches::kEnableCmaMediaPipeline);
147 #if defined(OS_LINUX)
148 // Necessary for accelerated 2d canvas. By default on Linux, Chromium assumes
149 // GLES2 contexts can be lost to a power-save mode, which breaks GPU canvas
150 // apps.
151 if (process_type == switches::kGpuProcess) {
152 command_line->AppendSwitch(switches::kGpuNoContextLost);
154 #endif
156 PlatformAppendExtraCommandLineSwitches(command_line);
159 content::AccessTokenStore* CastContentBrowserClient::CreateAccessTokenStore() {
160 return new CastAccessTokenStore(
161 CastBrowserProcess::GetInstance()->browser_context());
164 void CastContentBrowserClient::OverrideWebkitPrefs(
165 content::RenderViewHost* render_view_host,
166 content::WebPreferences* prefs) {
167 prefs->allow_scripts_to_close_windows = true;
168 // TODO(lcwu): http://crbug.com/391089. This pref is set to true by default
169 // because some content providers such as YouTube use plain http requests
170 // to retrieve media data chunks while running in a https page. This pref
171 // should be disabled once all the content providers are no longer doing that.
172 prefs->allow_running_insecure_content = true;
175 void CastContentBrowserClient::ResourceDispatcherHostCreated() {
176 CastBrowserProcess::GetInstance()->SetResourceDispatcherHostDelegate(
177 make_scoped_ptr(new CastResourceDispatcherHostDelegate));
178 content::ResourceDispatcherHost::Get()->SetDelegate(
179 CastBrowserProcess::GetInstance()->resource_dispatcher_host_delegate());
182 std::string CastContentBrowserClient::GetApplicationLocale() {
183 const std::string locale(base::i18n::GetConfiguredLocale());
184 return locale.empty() ? "en-US" : locale;
187 void CastContentBrowserClient::AllowCertificateError(
188 int render_process_id,
189 int render_view_id,
190 int cert_error,
191 const net::SSLInfo& ssl_info,
192 const GURL& request_url,
193 content::ResourceType resource_type,
194 bool overridable,
195 bool strict_enforcement,
196 bool expired_previous_decision,
197 const base::Callback<void(bool)>& callback,
198 content::CertificateRequestResultType* result) {
199 // Allow developers to override certificate errors.
200 // Otherwise, any fatal certificate errors will cause an abort.
201 *result = content::CERTIFICATE_REQUEST_RESULT_TYPE_CANCEL;
202 return;
205 void CastContentBrowserClient::SelectClientCertificate(
206 content::WebContents* web_contents,
207 net::SSLCertRequestInfo* cert_request_info,
208 scoped_ptr<content::ClientCertificateDelegate> delegate) {
209 GURL requesting_url("https://" + cert_request_info->host_and_port.ToString());
211 if (!requesting_url.is_valid()) {
212 LOG(ERROR) << "Invalid URL string: "
213 << requesting_url.possibly_invalid_spec();
214 delegate->ContinueWithCertificate(nullptr);
215 return;
218 // In our case there are no relevant certs in the cert_request_info. The cert
219 // we need to return (if permitted) is the Cast device cert, which we can
220 // access directly through the ClientAuthSigner instance. However, we need to
221 // be on the IO thread to determine whether the app is whitelisted to return
222 // it, because CastNetworkDelegate is bound to the IO thread.
223 // Subsequently, the callback must then itself be performed back here
224 // on the UI thread.
226 // TODO(davidben): Stop using child ID to identify an app.
227 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
228 content::BrowserThread::PostTaskAndReplyWithResult(
229 content::BrowserThread::IO, FROM_HERE,
230 base::Bind(&CastContentBrowserClient::SelectClientCertificateOnIOThread,
231 base::Unretained(this), requesting_url,
232 web_contents->GetRenderProcessHost()->GetID()),
233 base::Bind(&content::ClientCertificateDelegate::ContinueWithCertificate,
234 base::Owned(delegate.release())));
237 net::X509Certificate*
238 CastContentBrowserClient::SelectClientCertificateOnIOThread(
239 GURL requesting_url,
240 int render_process_id) {
241 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
242 CastNetworkDelegate* network_delegate =
243 url_request_context_factory_->app_network_delegate();
244 if (network_delegate->IsWhitelisted(requesting_url,
245 render_process_id, false)) {
246 return CastNetworkDelegate::DeviceCert();
247 } else {
248 LOG(ERROR) << "Invalid host for client certificate request: "
249 << requesting_url.host()
250 << " with render_process_id: "
251 << render_process_id;
252 return NULL;
256 bool CastContentBrowserClient::CanCreateWindow(
257 const GURL& opener_url,
258 const GURL& opener_top_level_frame_url,
259 const GURL& source_origin,
260 WindowContainerType container_type,
261 const GURL& target_url,
262 const content::Referrer& referrer,
263 WindowOpenDisposition disposition,
264 const blink::WebWindowFeatures& features,
265 bool user_gesture,
266 bool opener_suppressed,
267 content::ResourceContext* context,
268 int render_process_id,
269 int opener_id,
270 bool* no_javascript_access) {
271 *no_javascript_access = true;
272 return false;
275 content::DevToolsManagerDelegate*
276 CastContentBrowserClient::GetDevToolsManagerDelegate() {
277 return new CastDevToolsManagerDelegate();
280 void CastContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
281 const base::CommandLine& command_line,
282 int child_process_id,
283 content::FileDescriptorInfo* mappings) {
284 #if defined(OS_ANDROID)
285 const int flags_open_read = base::File::FLAG_OPEN | base::File::FLAG_READ;
286 base::FilePath pak_file_path;
287 CHECK(PathService::Get(FILE_CAST_PAK, &pak_file_path));
288 base::File pak_file(pak_file_path, flags_open_read);
289 if (!pak_file.IsValid()) {
290 NOTREACHED() << "Failed to open file when creating renderer process: "
291 << "cast_shell.pak";
293 mappings->Transfer(kAndroidPakDescriptor,
294 base::ScopedFD(pak_file.TakePlatformFile()));
296 if (breakpad::IsCrashReporterEnabled()) {
297 base::File minidump_file(
298 breakpad::CrashDumpManager::GetInstance()->CreateMinidumpFile(
299 child_process_id));
300 if (!minidump_file.IsValid()) {
301 LOG(ERROR) << "Failed to create file for minidump, crash reporting will "
302 << "be disabled for this process.";
303 } else {
304 mappings->Transfer(kAndroidMinidumpDescriptor,
305 base::ScopedFD(minidump_file.TakePlatformFile()));
309 base::FilePath app_data_path;
310 CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &app_data_path));
311 base::FilePath icudata_path =
312 app_data_path.AppendASCII(base::i18n::kIcuDataFileName);
313 base::File icudata_file(icudata_path, flags_open_read);
314 if (!icudata_file.IsValid())
315 NOTREACHED() << "Failed to open ICU file when creating renderer process";
316 mappings->Transfer(kAndroidICUDataDescriptor,
317 base::ScopedFD(icudata_file.TakePlatformFile()));
318 #else
319 int crash_signal_fd = GetCrashSignalFD(command_line);
320 if (crash_signal_fd >= 0) {
321 mappings->Share(kCrashDumpSignal, crash_signal_fd);
323 #endif // defined(OS_ANDROID)
326 #if defined(OS_ANDROID) && defined(VIDEO_HOLE)
327 content::ExternalVideoSurfaceContainer*
328 CastContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
329 content::WebContents* web_contents) {
330 return new ExternalVideoSurfaceContainerImpl(web_contents);
332 #endif // defined(OS_ANDROID) && defined(VIDEO_HOLE)
334 #if !defined(OS_ANDROID)
335 int CastContentBrowserClient::GetCrashSignalFD(
336 const base::CommandLine& command_line) {
337 std::string process_type =
338 command_line.GetSwitchValueASCII(switches::kProcessType);
340 if (process_type == switches::kRendererProcess ||
341 process_type == switches::kGpuProcess) {
342 breakpad::CrashHandlerHostLinux* crash_handler =
343 crash_handlers_[process_type];
344 if (!crash_handler) {
345 crash_handler = CreateCrashHandlerHost(process_type);
346 crash_handlers_[process_type] = crash_handler;
348 return crash_handler->GetDeathSignalSocket();
351 return -1;
354 breakpad::CrashHandlerHostLinux*
355 CastContentBrowserClient::CreateCrashHandlerHost(
356 const std::string& process_type) {
357 // Let cast shell dump to /tmp. Internal minidump generator code can move it
358 // to /data/minidumps later, since /data/minidumps is file lock-controlled.
359 base::FilePath dumps_path;
360 PathService::Get(base::DIR_TEMP, &dumps_path);
362 // Alway set "upload" to false to use our own uploader.
363 breakpad::CrashHandlerHostLinux* crash_handler =
364 new breakpad::CrashHandlerHostLinux(
365 process_type, dumps_path, false /* upload */);
366 // StartUploaderThread() even though upload is diferred.
367 // Breakpad-related memory is freed in the uploader thread.
368 crash_handler->StartUploaderThread();
369 return crash_handler;
371 #endif // !defined(OS_ANDROID)
373 } // namespace shell
374 } // namespace chromecast