ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chromecast / browser / cast_content_browser_client.cc
blob2ccdcd3dcb581bea43d2b4ba437319793857fcb4
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/browser/cast_browser_context.h"
15 #include "chromecast/browser/cast_browser_main_parts.h"
16 #include "chromecast/browser/cast_browser_process.h"
17 #include "chromecast/browser/cast_network_delegate.h"
18 #include "chromecast/browser/cast_resource_dispatcher_host_delegate.h"
19 #include "chromecast/browser/devtools/cast_dev_tools_delegate.h"
20 #include "chromecast/browser/geolocation/cast_access_token_store.h"
21 #include "chromecast/browser/media/cma_message_filter_host.h"
22 #include "chromecast/browser/url_request_context_factory.h"
23 #include "chromecast/common/cast_paths.h"
24 #include "chromecast/common/chromecast_switches.h"
25 #include "chromecast/common/global_descriptors.h"
26 #include "components/crash/app/breakpad_linux.h"
27 #include "components/crash/browser/crash_handler_host_linux.h"
28 #include "components/network_hints/browser/network_hints_message_filter.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/certificate_request_result_type.h"
31 #include "content/public/browser/render_process_host.h"
32 #include "content/public/browser/resource_dispatcher_host.h"
33 #include "content/public/common/content_descriptors.h"
34 #include "content/public/common/content_switches.h"
35 #include "content/public/common/url_constants.h"
36 #include "content/public/common/web_preferences.h"
37 #include "net/ssl/ssl_cert_request_info.h"
39 #if defined(OS_ANDROID)
40 #include "chromecast/browser/android/external_video_surface_container_impl.h"
41 #endif // defined(OS_ANDROID)
43 #if defined(OS_ANDROID)
44 #include "components/crash/browser/crash_dump_manager_android.h"
45 #endif // defined(OS_ANDROID)
47 namespace chromecast {
48 namespace shell {
50 CastContentBrowserClient::CastContentBrowserClient()
51 : url_request_context_factory_(new URLRequestContextFactory()) {
54 CastContentBrowserClient::~CastContentBrowserClient() {
55 content::BrowserThread::DeleteSoon(
56 content::BrowserThread::IO,
57 FROM_HERE,
58 url_request_context_factory_.release());
61 content::BrowserMainParts* CastContentBrowserClient::CreateBrowserMainParts(
62 const content::MainFunctionParams& parameters) {
63 return new CastBrowserMainParts(parameters,
64 url_request_context_factory_.get());
67 void CastContentBrowserClient::RenderProcessWillLaunch(
68 content::RenderProcessHost* host) {
69 scoped_refptr<content::BrowserMessageFilter> network_hints_message_filter(
70 new network_hints::NetworkHintsMessageFilter(
71 url_request_context_factory_->host_resolver()));
72 host->AddFilter(network_hints_message_filter.get());
73 #if !defined(OS_ANDROID)
74 scoped_refptr<media::CmaMessageFilterHost> cma_message_filter(
75 new media::CmaMessageFilterHost(host->GetID()));
76 host->AddFilter(cma_message_filter.get());
77 #endif // !defined(OS_ANDROID)
79 auto extra_filters = PlatformGetBrowserMessageFilters();
80 for (auto const& filter : extra_filters) {
81 host->AddFilter(filter.get());
85 net::URLRequestContextGetter* CastContentBrowserClient::CreateRequestContext(
86 content::BrowserContext* browser_context,
87 content::ProtocolHandlerMap* protocol_handlers,
88 content::URLRequestInterceptorScopedVector request_interceptors) {
89 return url_request_context_factory_->CreateMainGetter(
90 browser_context,
91 protocol_handlers,
92 request_interceptors.Pass());
95 bool CastContentBrowserClient::IsHandledURL(const GURL& url) {
96 if (!url.is_valid())
97 return false;
99 static const char* const kProtocolList[] = {
100 url::kBlobScheme,
101 url::kFileSystemScheme,
102 content::kChromeUIScheme,
103 content::kChromeDevToolsScheme,
104 url::kDataScheme,
105 #if defined(OS_ANDROID)
106 url::kFileScheme,
107 #endif // defined(OS_ANDROID)
110 const std::string& scheme = url.scheme();
111 for (size_t i = 0; i < arraysize(kProtocolList); ++i) {
112 if (scheme == kProtocolList[i])
113 return true;
115 return false;
118 void CastContentBrowserClient::AppendExtraCommandLineSwitches(
119 base::CommandLine* command_line,
120 int child_process_id) {
122 std::string process_type =
123 command_line->GetSwitchValueNative(switches::kProcessType);
124 base::CommandLine* browser_command_line =
125 base::CommandLine::ForCurrentProcess();
127 // IsCrashReporterEnabled() is set when InitCrashReporter() is called, and
128 // controlled by GetBreakpadClient()->EnableBreakpadForProcess(), therefore
129 // it's ok to add switch to every process here.
130 if (breakpad::IsCrashReporterEnabled()) {
131 command_line->AppendSwitch(switches::kEnableCrashReporter);
134 // Renderer process command-line
135 if (process_type == switches::kRendererProcess) {
136 // Any browser command-line switches that should be propagated to
137 // the renderer go here.
138 #if defined(OS_ANDROID)
139 command_line->AppendSwitch(switches::kForceUseOverlayEmbeddedVideo);
140 #endif // defined(OS_ANDROID)
142 if (browser_command_line->HasSwitch(switches::kEnableCmaMediaPipeline))
143 command_line->AppendSwitch(switches::kEnableCmaMediaPipeline);
146 PlatformAppendExtraCommandLineSwitches(command_line);
149 content::AccessTokenStore* CastContentBrowserClient::CreateAccessTokenStore() {
150 return new CastAccessTokenStore(
151 CastBrowserProcess::GetInstance()->browser_context());
154 void CastContentBrowserClient::OverrideWebkitPrefs(
155 content::RenderViewHost* render_view_host,
156 content::WebPreferences* prefs) {
157 prefs->allow_scripts_to_close_windows = true;
158 // TODO(lcwu): http://crbug.com/391089. This pref is set to true by default
159 // because some content providers such as YouTube use plain http requests
160 // to retrieve media data chunks while running in a https page. This pref
161 // should be disabled once all the content providers are no longer doing that.
162 prefs->allow_running_insecure_content = true;
165 void CastContentBrowserClient::ResourceDispatcherHostCreated() {
166 CastBrowserProcess::GetInstance()->SetResourceDispatcherHostDelegate(
167 make_scoped_ptr(new CastResourceDispatcherHostDelegate));
168 content::ResourceDispatcherHost::Get()->SetDelegate(
169 CastBrowserProcess::GetInstance()->resource_dispatcher_host_delegate());
172 std::string CastContentBrowserClient::GetApplicationLocale() {
173 const std::string locale(base::i18n::GetConfiguredLocale());
174 return locale.empty() ? "en-US" : locale;
177 void CastContentBrowserClient::AllowCertificateError(
178 int render_process_id,
179 int render_view_id,
180 int cert_error,
181 const net::SSLInfo& ssl_info,
182 const GURL& request_url,
183 content::ResourceType resource_type,
184 bool overridable,
185 bool strict_enforcement,
186 bool expired_previous_decision,
187 const base::Callback<void(bool)>& callback,
188 content::CertificateRequestResultType* result) {
189 // Allow developers to override certificate errors.
190 // Otherwise, any fatal certificate errors will cause an abort.
191 *result = content::CERTIFICATE_REQUEST_RESULT_TYPE_CANCEL;
192 return;
195 void CastContentBrowserClient::SelectClientCertificate(
196 int render_process_id,
197 int render_view_id,
198 net::SSLCertRequestInfo* cert_request_info,
199 const base::Callback<void(net::X509Certificate*)>& callback) {
200 GURL requesting_url("https://" + cert_request_info->host_and_port.ToString());
202 if (!requesting_url.is_valid()) {
203 LOG(ERROR) << "Invalid URL string: "
204 << requesting_url.possibly_invalid_spec();
205 callback.Run(NULL);
206 return;
209 // In our case there are no relevant certs in the cert_request_info. The cert
210 // we need to return (if permitted) is the Cast device cert, which we can
211 // access directly through the ClientAuthSigner instance. However, we need to
212 // be on the IO thread to determine whether the app is whitelisted to return
213 // it, because CastNetworkDelegate is bound to the IO thread.
214 // Subsequently, the callback must then itself be performed back here
215 // on the UI thread.
216 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
217 content::BrowserThread::PostTaskAndReplyWithResult(
218 content::BrowserThread::IO,
219 FROM_HERE,
220 base::Bind(
221 &CastContentBrowserClient::SelectClientCertificateOnIOThread,
222 base::Unretained(this),
223 requesting_url,
224 render_process_id),
225 callback);
228 net::X509Certificate*
229 CastContentBrowserClient::SelectClientCertificateOnIOThread(
230 GURL requesting_url,
231 int render_process_id) {
232 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
233 CastNetworkDelegate* network_delegate =
234 url_request_context_factory_->app_network_delegate();
235 if (network_delegate->IsWhitelisted(requesting_url,
236 render_process_id, false)) {
237 return CastNetworkDelegate::DeviceCert();
238 } else {
239 LOG(ERROR) << "Invalid host for client certificate request: "
240 << requesting_url.host()
241 << " with render_process_id: "
242 << render_process_id;
243 return NULL;
247 bool CastContentBrowserClient::CanCreateWindow(
248 const GURL& opener_url,
249 const GURL& opener_top_level_frame_url,
250 const GURL& source_origin,
251 WindowContainerType container_type,
252 const GURL& target_url,
253 const content::Referrer& referrer,
254 WindowOpenDisposition disposition,
255 const blink::WebWindowFeatures& features,
256 bool user_gesture,
257 bool opener_suppressed,
258 content::ResourceContext* context,
259 int render_process_id,
260 int opener_id,
261 bool* no_javascript_access) {
262 *no_javascript_access = true;
263 return false;
266 content::DevToolsManagerDelegate*
267 CastContentBrowserClient::GetDevToolsManagerDelegate() {
268 return new CastDevToolsManagerDelegate();
271 void CastContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
272 const base::CommandLine& command_line,
273 int child_process_id,
274 content::FileDescriptorInfo* mappings) {
275 #if defined(OS_ANDROID)
276 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
277 base::FilePath pak_file;
278 CHECK(PathService::Get(FILE_CAST_PAK, &pak_file));
279 base::File pak_with_flags(pak_file, flags);
280 if (!pak_with_flags.IsValid()) {
281 NOTREACHED() << "Failed to open file when creating renderer process: "
282 << "cast_shell.pak";
284 mappings->Transfer(
285 kAndroidPakDescriptor,
286 base::ScopedFD(pak_with_flags.TakePlatformFile()));
288 if (breakpad::IsCrashReporterEnabled()) {
289 base::File minidump_file(
290 breakpad::CrashDumpManager::GetInstance()->CreateMinidumpFile(
291 child_process_id));
292 if (!minidump_file.IsValid()) {
293 LOG(ERROR) << "Failed to create file for minidump, crash reporting will "
294 << "be disabled for this process.";
295 } else {
296 mappings->Transfer(kAndroidMinidumpDescriptor,
297 base::ScopedFD(minidump_file.TakePlatformFile()));
300 #else
301 int crash_signal_fd = GetCrashSignalFD(command_line);
302 if (crash_signal_fd >= 0) {
303 mappings->Share(kCrashDumpSignal, crash_signal_fd);
305 #endif // defined(OS_ANDROID)
308 #if defined(OS_ANDROID) && defined(VIDEO_HOLE)
309 content::ExternalVideoSurfaceContainer*
310 CastContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer(
311 content::WebContents* web_contents) {
312 return new ExternalVideoSurfaceContainerImpl(web_contents);
314 #endif // defined(OS_ANDROID) && defined(VIDEO_HOLE)
316 #if !defined(OS_ANDROID)
317 int CastContentBrowserClient::GetCrashSignalFD(
318 const base::CommandLine& command_line) {
319 std::string process_type =
320 command_line.GetSwitchValueASCII(switches::kProcessType);
322 if (process_type == switches::kRendererProcess ||
323 process_type == switches::kGpuProcess) {
324 breakpad::CrashHandlerHostLinux* crash_handler =
325 crash_handlers_[process_type];
326 if (!crash_handler) {
327 crash_handler = CreateCrashHandlerHost(process_type);
328 crash_handlers_[process_type] = crash_handler;
330 return crash_handler->GetDeathSignalSocket();
333 return -1;
336 breakpad::CrashHandlerHostLinux*
337 CastContentBrowserClient::CreateCrashHandlerHost(
338 const std::string& process_type) {
339 // Let cast shell dump to /tmp. Internal minidump generator code can move it
340 // to /data/minidumps later, since /data/minidumps is file lock-controlled.
341 base::FilePath dumps_path;
342 PathService::Get(base::DIR_TEMP, &dumps_path);
344 // Alway set "upload" to false to use our own uploader.
345 breakpad::CrashHandlerHostLinux* crash_handler =
346 new breakpad::CrashHandlerHostLinux(
347 process_type, dumps_path, false /* upload */);
348 // StartUploaderThread() even though upload is diferred.
349 // Breakpad-related memory is freed in the uploader thread.
350 crash_handler->StartUploaderThread();
351 return crash_handler;
353 #endif // !defined(OS_ANDROID)
355 } // namespace shell
356 } // namespace chromecast