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