| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/android/download/download_controller.h" | 5 #include "chrome/browser/android/download/download_controller.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/android/context_utils.h" | 10 #include "base/android/context_utils.h" |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 ScopedJavaLocalRef<jobject> jwindow_android = window_android->GetJavaObject(); | 227 ScopedJavaLocalRef<jobject> jwindow_android = window_android->GetJavaObject(); |
| 228 | 228 |
| 229 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 229 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 230 DCHECK(!jwindow_android.is_null()); | 230 DCHECK(!jwindow_android.is_null()); |
| 231 | 231 |
| 232 JNIEnv* env = base::android::AttachCurrentThread(); | 232 JNIEnv* env = base::android::AttachCurrentThread(); |
| 233 return Java_DownloadController_hasFileAccess( | 233 return Java_DownloadController_hasFileAccess( |
| 234 env, GetJavaObject()->Controller(env), jwindow_android); | 234 env, GetJavaObject()->Controller(env), jwindow_android); |
| 235 } | 235 } |
| 236 | 236 |
| 237 void DownloadController::CreateGETDownload( | |
| 238 const content::ResourceRequestInfo::WebContentsGetter& wc_getter, | |
| 239 bool must_download, | |
| 240 const DownloadInfo& info) { | |
| 241 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 242 | |
| 243 // We are yielding the UI thread and render_view_host may go away by | |
| 244 // the time we come back. Pass along render_process_id and render_view_id | |
| 245 // to retrieve it later (if it still exists). | |
| 246 BrowserThread::PostTask( | |
| 247 BrowserThread::UI, FROM_HERE, | |
| 248 base::Bind(&DownloadController::StartAndroidDownload, | |
| 249 base::Unretained(this), | |
| 250 wc_getter, must_download, info)); | |
| 251 } | |
| 252 | |
| 253 void DownloadController::StartAndroidDownload( | |
| 254 const content::ResourceRequestInfo::WebContentsGetter& wc_getter, | |
| 255 bool must_download, const DownloadInfo& info) { | |
| 256 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 257 | |
| 258 WebContents* web_contents = wc_getter.Run(); | |
| 259 if (!web_contents) { | |
| 260 // The view went away. Can't proceed. | |
| 261 LOG(ERROR) << "Download failed on URL:" << info.url.spec(); | |
| 262 return; | |
| 263 } | |
| 264 | |
| 265 AcquireFileAccessPermission( | |
| 266 web_contents, | |
| 267 base::Bind(&DownloadController::StartAndroidDownloadInternal, | |
| 268 base::Unretained(this), wc_getter, must_download, info)); | |
| 269 } | |
| 270 | |
| 271 void DownloadController::StartAndroidDownloadInternal( | |
| 272 const content::ResourceRequestInfo::WebContentsGetter& wc_getter, | |
| 273 bool must_download, const DownloadInfo& info, bool allowed) { | |
| 274 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 275 if (!allowed) | |
| 276 return; | |
| 277 | |
| 278 WebContents* web_contents = wc_getter.Run(); | |
| 279 // The view went away. Can't proceed. | |
| 280 if (!web_contents) | |
| 281 return; | |
| 282 | |
| 283 base::string16 filename = net::GetSuggestedFilename( | |
| 284 info.url, info.content_disposition, | |
| 285 std::string(), // referrer_charset | |
| 286 std::string(), // suggested_name | |
| 287 info.original_mime_type, | |
| 288 default_file_name_); | |
| 289 ChromeDownloadDelegate::FromWebContents(web_contents)->RequestHTTPGetDownload( | |
| 290 info.url.spec(), info.user_agent, | |
| 291 info.content_disposition, info.original_mime_type, | |
| 292 info.cookie, info.referer, filename, | |
| 293 info.total_bytes, info.has_user_gesture, | |
| 294 must_download); | |
| 295 } | |
| 296 | |
| 297 void DownloadController::OnDownloadStarted( | 237 void DownloadController::OnDownloadStarted( |
| 298 DownloadItem* download_item) { | 238 DownloadItem* download_item) { |
| 299 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 239 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 300 WebContents* web_contents = download_item->GetWebContents(); | 240 WebContents* web_contents = download_item->GetWebContents(); |
| 301 if (!web_contents) | 241 if (!web_contents) |
| 302 return; | 242 return; |
| 303 | 243 |
| 304 // Register for updates to the DownloadItem. | 244 // Register for updates to the DownloadItem. |
| 305 download_item->AddObserver(this); | 245 download_item->AddObserver(this); |
| 306 | 246 |
| 307 ChromeDownloadDelegate::FromWebContents(web_contents)->OnDownloadStarted( | 247 ChromeDownloadDelegate::FromWebContents(web_contents)->OnDownloadStarted( |
| 308 download_item->GetTargetFilePath().BaseName().value(), | 248 download_item->GetTargetFilePath().BaseName().value()); |
| 309 download_item->GetMimeType()); | |
| 310 } | 249 } |
| 311 | 250 |
| 312 void DownloadController::OnDownloadUpdated(DownloadItem* item) { | 251 void DownloadController::OnDownloadUpdated(DownloadItem* item) { |
| 313 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 252 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 314 if (item->IsDangerous() && (item->GetState() != DownloadItem::CANCELLED)) | 253 if (item->IsDangerous() && (item->GetState() != DownloadItem::CANCELLED)) |
| 315 OnDangerousDownload(item); | 254 OnDangerousDownload(item); |
| 316 | 255 |
| 317 JNIEnv* env = base::android::AttachCurrentThread(); | 256 JNIEnv* env = base::android::AttachCurrentThread(); |
| 318 ScopedJavaLocalRef<jstring> jguid = | 257 ScopedJavaLocalRef<jstring> jguid = |
| 319 ConvertUTF8ToJavaString(env, item->GetGuid()); | 258 ConvertUTF8ToJavaString(env, item->GetGuid()); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 if (!item) | 367 if (!item) |
| 429 return; | 368 return; |
| 430 if (accept) { | 369 if (accept) { |
| 431 item->ValidateDangerousDownload(); | 370 item->ValidateDangerousDownload(); |
| 432 } else { | 371 } else { |
| 433 DownloadController::RecordDownloadCancelReason( | 372 DownloadController::RecordDownloadCancelReason( |
| 434 DownloadController::CANCEL_REASON_DANGEROUS_DOWNLOAD_INFOBAR_DISMISSED); | 373 DownloadController::CANCEL_REASON_DANGEROUS_DOWNLOAD_INFOBAR_DISMISSED); |
| 435 item->Remove(); | 374 item->Remove(); |
| 436 } | 375 } |
| 437 } | 376 } |
| OLD | NEW |