| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chromecast/shell/browser/cast_content_browser_client.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/files/scoped_file.h" | |
| 9 #include "base/i18n/rtl.h" | |
| 10 #include "base/path_service.h" | |
| 11 #include "chromecast/common/cast_paths.h" | |
| 12 #include "chromecast/common/global_descriptors.h" | |
| 13 #include "chromecast/shell/browser/cast_browser_context.h" | |
| 14 #include "chromecast/shell/browser/cast_browser_main_parts.h" | |
| 15 #include "chromecast/shell/browser/cast_browser_process.h" | |
| 16 #include "chromecast/shell/browser/cast_network_delegate.h" | |
| 17 #include "chromecast/shell/browser/devtools/cast_dev_tools_delegate.h" | |
| 18 #include "chromecast/shell/browser/geolocation/cast_access_token_store.h" | |
| 19 #include "chromecast/shell/browser/url_request_context_factory.h" | |
| 20 #include "components/crash/app/breakpad_linux.h" | |
| 21 #include "content/public/browser/browser_thread.h" | |
| 22 #include "content/public/browser/certificate_request_result_type.h" | |
| 23 #include "content/public/browser/render_process_host.h" | |
| 24 #include "content/public/common/content_descriptors.h" | |
| 25 #include "content/public/common/content_switches.h" | |
| 26 #include "content/public/common/url_constants.h" | |
| 27 #include "content/public/common/web_preferences.h" | |
| 28 #include "net/ssl/ssl_cert_request_info.h" | |
| 29 | |
| 30 #if defined(OS_ANDROID) | |
| 31 #include "chromecast/shell/browser/android/external_video_surface_container_impl
.h" | |
| 32 #endif // defined(OS_ANDROID) | |
| 33 | |
| 34 #if defined(OS_ANDROID) | |
| 35 #include "components/crash/browser/crash_dump_manager_android.h" | |
| 36 #endif // defined(OS_ANDROID) | |
| 37 | |
| 38 namespace chromecast { | |
| 39 namespace shell { | |
| 40 | |
| 41 CastContentBrowserClient::CastContentBrowserClient() | |
| 42 : url_request_context_factory_(new URLRequestContextFactory()) { | |
| 43 } | |
| 44 | |
| 45 CastContentBrowserClient::~CastContentBrowserClient() { | |
| 46 content::BrowserThread::DeleteSoon( | |
| 47 content::BrowserThread::IO, | |
| 48 FROM_HERE, | |
| 49 url_request_context_factory_.release()); | |
| 50 } | |
| 51 | |
| 52 content::BrowserMainParts* CastContentBrowserClient::CreateBrowserMainParts( | |
| 53 const content::MainFunctionParams& parameters) { | |
| 54 return new CastBrowserMainParts(parameters, | |
| 55 url_request_context_factory_.get()); | |
| 56 } | |
| 57 | |
| 58 void CastContentBrowserClient::RenderProcessWillLaunch( | |
| 59 content::RenderProcessHost* host) { | |
| 60 } | |
| 61 | |
| 62 net::URLRequestContextGetter* CastContentBrowserClient::CreateRequestContext( | |
| 63 content::BrowserContext* browser_context, | |
| 64 content::ProtocolHandlerMap* protocol_handlers, | |
| 65 content::URLRequestInterceptorScopedVector request_interceptors) { | |
| 66 return url_request_context_factory_->CreateMainGetter( | |
| 67 browser_context, | |
| 68 protocol_handlers, | |
| 69 request_interceptors.Pass()); | |
| 70 } | |
| 71 | |
| 72 bool CastContentBrowserClient::IsHandledURL(const GURL& url) { | |
| 73 if (!url.is_valid()) | |
| 74 return false; | |
| 75 | |
| 76 static const char* const kProtocolList[] = { | |
| 77 url::kBlobScheme, | |
| 78 url::kFileSystemScheme, | |
| 79 content::kChromeUIScheme, | |
| 80 content::kChromeDevToolsScheme, | |
| 81 url::kDataScheme, | |
| 82 #if defined(OS_ANDROID) | |
| 83 url::kFileScheme, | |
| 84 #endif // defined(OS_ANDROID) | |
| 85 }; | |
| 86 | |
| 87 const std::string& scheme = url.scheme(); | |
| 88 for (size_t i = 0; i < arraysize(kProtocolList); ++i) { | |
| 89 if (scheme == kProtocolList[i]) | |
| 90 return true; | |
| 91 } | |
| 92 return false; | |
| 93 } | |
| 94 | |
| 95 void CastContentBrowserClient::AppendExtraCommandLineSwitches( | |
| 96 base::CommandLine* command_line, | |
| 97 int child_process_id) { | |
| 98 | |
| 99 std::string process_type = | |
| 100 command_line->GetSwitchValueNative(switches::kProcessType); | |
| 101 // Renderer process comamndline | |
| 102 if (process_type == switches::kRendererProcess) { | |
| 103 // Any browser command-line switches that should be propagated to | |
| 104 // the renderer go here. | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 content::AccessTokenStore* CastContentBrowserClient::CreateAccessTokenStore() { | |
| 109 return new CastAccessTokenStore( | |
| 110 CastBrowserProcess::GetInstance()->browser_context()); | |
| 111 } | |
| 112 | |
| 113 void CastContentBrowserClient::OverrideWebkitPrefs( | |
| 114 content::RenderViewHost* render_view_host, | |
| 115 const GURL& url, | |
| 116 content::WebPreferences* prefs) { | |
| 117 prefs->allow_scripts_to_close_windows = true; | |
| 118 // TODO(lcwu): http://crbug.com/391089. This pref is set to true by default | |
| 119 // because some content providers such as YouTube use plain http requests | |
| 120 // to retrieve media data chunks while running in a https page. This pref | |
| 121 // should be disabled once all the content providers are no longer doing that. | |
| 122 prefs->allow_running_insecure_content = true; | |
| 123 } | |
| 124 | |
| 125 std::string CastContentBrowserClient::GetApplicationLocale() { | |
| 126 const std::string locale(base::i18n::GetConfiguredLocale()); | |
| 127 return locale.empty() ? "en-US" : locale; | |
| 128 } | |
| 129 | |
| 130 void CastContentBrowserClient::AllowCertificateError( | |
| 131 int render_process_id, | |
| 132 int render_view_id, | |
| 133 int cert_error, | |
| 134 const net::SSLInfo& ssl_info, | |
| 135 const GURL& request_url, | |
| 136 content::ResourceType resource_type, | |
| 137 bool overridable, | |
| 138 bool strict_enforcement, | |
| 139 bool expired_previous_decision, | |
| 140 const base::Callback<void(bool)>& callback, | |
| 141 content::CertificateRequestResultType* result) { | |
| 142 // Allow developers to override certificate errors. | |
| 143 // Otherwise, any fatal certificate errors will cause an abort. | |
| 144 *result = content::CERTIFICATE_REQUEST_RESULT_TYPE_CANCEL; | |
| 145 return; | |
| 146 } | |
| 147 | |
| 148 void CastContentBrowserClient::SelectClientCertificate( | |
| 149 int render_process_id, | |
| 150 int render_view_id, | |
| 151 const net::HttpNetworkSession* network_session, | |
| 152 net::SSLCertRequestInfo* cert_request_info, | |
| 153 const base::Callback<void(net::X509Certificate*)>& callback) { | |
| 154 GURL requesting_url("https://" + cert_request_info->host_and_port.ToString()); | |
| 155 | |
| 156 if (!requesting_url.is_valid()) { | |
| 157 LOG(ERROR) << "Invalid URL string: " | |
| 158 << requesting_url.possibly_invalid_spec(); | |
| 159 callback.Run(NULL); | |
| 160 return; | |
| 161 } | |
| 162 | |
| 163 // In our case there are no relevant certs in the cert_request_info. The cert | |
| 164 // we need to return (if permitted) is the Cast device cert, which we can | |
| 165 // access directly through the ClientAuthSigner instance. However, we need to | |
| 166 // be on the IO thread to determine whether the app is whitelisted to return | |
| 167 // it, because CastNetworkDelegate is bound to the IO thread. | |
| 168 // Subsequently, the callback must then itself be performed back here | |
| 169 // on the UI thread. | |
| 170 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 171 content::BrowserThread::PostTaskAndReplyWithResult( | |
| 172 content::BrowserThread::IO, | |
| 173 FROM_HERE, | |
| 174 base::Bind( | |
| 175 &CastContentBrowserClient::SelectClientCertificateOnIOThread, | |
| 176 base::Unretained(this), | |
| 177 requesting_url), | |
| 178 callback); | |
| 179 } | |
| 180 | |
| 181 net::X509Certificate* | |
| 182 CastContentBrowserClient::SelectClientCertificateOnIOThread( | |
| 183 GURL requesting_url) { | |
| 184 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 185 CastNetworkDelegate* network_delegate = | |
| 186 url_request_context_factory_->app_network_delegate(); | |
| 187 if (network_delegate->IsWhitelisted(requesting_url, false)) { | |
| 188 return CastNetworkDelegate::DeviceCert(); | |
| 189 } else { | |
| 190 LOG(ERROR) << "Invalid host for client certificate request: " | |
| 191 << requesting_url.host(); | |
| 192 return NULL; | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 bool CastContentBrowserClient::CanCreateWindow( | |
| 197 const GURL& opener_url, | |
| 198 const GURL& opener_top_level_frame_url, | |
| 199 const GURL& source_origin, | |
| 200 WindowContainerType container_type, | |
| 201 const GURL& target_url, | |
| 202 const content::Referrer& referrer, | |
| 203 WindowOpenDisposition disposition, | |
| 204 const blink::WebWindowFeatures& features, | |
| 205 bool user_gesture, | |
| 206 bool opener_suppressed, | |
| 207 content::ResourceContext* context, | |
| 208 int render_process_id, | |
| 209 int opener_id, | |
| 210 bool* no_javascript_access) { | |
| 211 *no_javascript_access = true; | |
| 212 return false; | |
| 213 } | |
| 214 | |
| 215 content::DevToolsManagerDelegate* | |
| 216 CastContentBrowserClient::GetDevToolsManagerDelegate() { | |
| 217 return new CastDevToolsManagerDelegate(); | |
| 218 } | |
| 219 | |
| 220 void CastContentBrowserClient::GetAdditionalMappedFilesForChildProcess( | |
| 221 const base::CommandLine& command_line, | |
| 222 int child_process_id, | |
| 223 content::FileDescriptorInfo* mappings) { | |
| 224 #if defined(OS_ANDROID) | |
| 225 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; | |
| 226 base::FilePath pak_file; | |
| 227 CHECK(PathService::Get(FILE_CAST_PAK, &pak_file)); | |
| 228 base::File pak_with_flags(pak_file, flags); | |
| 229 if (!pak_with_flags.IsValid()) { | |
| 230 NOTREACHED() << "Failed to open file when creating renderer process: " | |
| 231 << "cast_shell.pak"; | |
| 232 } | |
| 233 mappings->Transfer( | |
| 234 kAndroidPakDescriptor, | |
| 235 base::ScopedFD(pak_with_flags.TakePlatformFile())); | |
| 236 | |
| 237 if (breakpad::IsCrashReporterEnabled()) { | |
| 238 base::File minidump_file( | |
| 239 breakpad::CrashDumpManager::GetInstance()->CreateMinidumpFile( | |
| 240 child_process_id)); | |
| 241 if (!minidump_file.IsValid()) { | |
| 242 LOG(ERROR) << "Failed to create file for minidump, crash reporting will " | |
| 243 << "be disabled for this process."; | |
| 244 } else { | |
| 245 mappings->Transfer(kAndroidMinidumpDescriptor, | |
| 246 base::ScopedFD(minidump_file.TakePlatformFile())); | |
| 247 } | |
| 248 } | |
| 249 #endif // defined(OS_ANDROID) | |
| 250 } | |
| 251 | |
| 252 #if defined(OS_ANDROID) && defined(VIDEO_HOLE) | |
| 253 content::ExternalVideoSurfaceContainer* | |
| 254 CastContentBrowserClient::OverrideCreateExternalVideoSurfaceContainer( | |
| 255 content::WebContents* web_contents) { | |
| 256 return new ExternalVideoSurfaceContainerImpl(web_contents); | |
| 257 } | |
| 258 #endif // defined(OS_ANDROID) && defined(VIDEO_HOLE) | |
| 259 | |
| 260 | |
| 261 } // namespace shell | |
| 262 } // namespace chromecast | |
| OLD | NEW |