| 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" |
| 11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
| 12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "chrome/browser/android/download/chrome_download_delegate.h" | 19 #include "chrome/browser/android/download/chrome_download_delegate.h" |
| 20 #include "chrome/browser/android/download/dangerous_download_infobar_delegate.h" |
| 21 #include "chrome/browser/infobars/infobar_service.h" |
| 20 #include "chrome/browser/ui/android/view_android_helper.h" | 22 #include "chrome/browser/ui/android/view_android_helper.h" |
| 21 #include "content/public/browser/browser_context.h" | 23 #include "content/public/browser/browser_context.h" |
| 22 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/browser/download_manager.h" | 25 #include "content/public/browser/download_manager.h" |
| 24 #include "content/public/browser/download_url_parameters.h" | 26 #include "content/public/browser/download_url_parameters.h" |
| 25 #include "content/public/browser/render_process_host.h" | 27 #include "content/public/browser/render_process_host.h" |
| 26 #include "content/public/browser/render_view_host.h" | 28 #include "content/public/browser/render_view_host.h" |
| 27 #include "content/public/common/referrer.h" | 29 #include "content/public/common/referrer.h" |
| 28 #include "jni/DownloadController_jni.h" | 30 #include "jni/DownloadController_jni.h" |
| 29 #include "net/base/filename_util.h" | 31 #include "net/base/filename_util.h" |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 ChromeDownloadDelegate::FromWebContents(web_contents); | 245 ChromeDownloadDelegate::FromWebContents(web_contents); |
| 244 if (delegate) { | 246 if (delegate) { |
| 245 delegate->OnDownloadStarted( | 247 delegate->OnDownloadStarted( |
| 246 download_item->GetTargetFilePath().BaseName().value()); | 248 download_item->GetTargetFilePath().BaseName().value()); |
| 247 } | 249 } |
| 248 OnDownloadUpdated(download_item); | 250 OnDownloadUpdated(download_item); |
| 249 } | 251 } |
| 250 | 252 |
| 251 void DownloadController::OnDownloadUpdated(DownloadItem* item) { | 253 void DownloadController::OnDownloadUpdated(DownloadItem* item) { |
| 252 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 254 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 253 if (item->IsDangerous() && (item->GetState() != DownloadItem::CANCELLED)) | 255 if (item->IsDangerous() && (item->GetState() != DownloadItem::CANCELLED)) { |
| 256 // Dont't show notification for a dangerous download, as user can resume |
| 257 // the download after browser crash through notification. |
| 254 OnDangerousDownload(item); | 258 OnDangerousDownload(item); |
| 259 return; |
| 260 } |
| 255 | 261 |
| 256 JNIEnv* env = base::android::AttachCurrentThread(); | 262 JNIEnv* env = base::android::AttachCurrentThread(); |
| 257 ScopedJavaLocalRef<jstring> jguid = | 263 ScopedJavaLocalRef<jstring> jguid = |
| 258 ConvertUTF8ToJavaString(env, item->GetGuid()); | 264 ConvertUTF8ToJavaString(env, item->GetGuid()); |
| 259 ScopedJavaLocalRef<jstring> jurl = | 265 ScopedJavaLocalRef<jstring> jurl = |
| 260 ConvertUTF8ToJavaString(env, item->GetURL().spec()); | 266 ConvertUTF8ToJavaString(env, item->GetURL().spec()); |
| 261 ScopedJavaLocalRef<jstring> jmime_type = | 267 ScopedJavaLocalRef<jstring> jmime_type = |
| 262 ConvertUTF8ToJavaString(env, item->GetMimeType()); | 268 ConvertUTF8ToJavaString(env, item->GetMimeType()); |
| 263 ScopedJavaLocalRef<jstring> jpath = | 269 ScopedJavaLocalRef<jstring> jpath = |
| 264 ConvertUTF8ToJavaString(env, item->GetTargetFilePath().value()); | 270 ConvertUTF8ToJavaString(env, item->GetTargetFilePath().value()); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 item->GetBrowserContext()->IsOffTheRecord()); | 326 item->GetBrowserContext()->IsOffTheRecord()); |
| 321 item->RemoveObserver(this); | 327 item->RemoveObserver(this); |
| 322 break; | 328 break; |
| 323 case DownloadItem::MAX_DOWNLOAD_STATE: | 329 case DownloadItem::MAX_DOWNLOAD_STATE: |
| 324 NOTREACHED(); | 330 NOTREACHED(); |
| 325 } | 331 } |
| 326 } | 332 } |
| 327 | 333 |
| 328 void DownloadController::OnDangerousDownload(DownloadItem* item) { | 334 void DownloadController::OnDangerousDownload(DownloadItem* item) { |
| 329 WebContents* web_contents = item->GetWebContents(); | 335 WebContents* web_contents = item->GetWebContents(); |
| 330 if (!web_contents) | 336 if (!web_contents) { |
| 337 item->Remove(); |
| 331 return; | 338 return; |
| 332 ChromeDownloadDelegate::FromWebContents(web_contents)->OnDangerousDownload( | 339 } |
| 333 item->GetTargetFilePath().BaseName().value(), item->GetGuid()); | 340 |
| 341 DangerousDownloadInfoBarDelegate::Create( |
| 342 InfoBarService::FromWebContents(web_contents), item); |
| 334 } | 343 } |
| 335 | 344 |
| 336 DownloadController::JavaObject* | 345 DownloadController::JavaObject* |
| 337 DownloadController::GetJavaObject() { | 346 DownloadController::GetJavaObject() { |
| 338 if (!java_object_) { | 347 if (!java_object_) { |
| 339 // Initialize Java DownloadController by calling | 348 // Initialize Java DownloadController by calling |
| 340 // DownloadController.getInstance(), which will call Init() | 349 // DownloadController.getInstance(), which will call Init() |
| 341 // if Java DownloadController is not instantiated already. | 350 // if Java DownloadController is not instantiated already. |
| 342 JNIEnv* env = base::android::AttachCurrentThread(); | 351 JNIEnv* env = base::android::AttachCurrentThread(); |
| 343 Java_DownloadController_getInstance(env); | 352 Java_DownloadController_getInstance(env); |
| 344 } | 353 } |
| 345 | 354 |
| 346 DCHECK(java_object_); | 355 DCHECK(java_object_); |
| 347 return java_object_; | 356 return java_object_; |
| 348 } | 357 } |
| 349 | 358 |
| 350 void DownloadController::StartContextMenuDownload( | 359 void DownloadController::StartContextMenuDownload( |
| 351 const ContextMenuParams& params, WebContents* web_contents, bool is_link, | 360 const ContextMenuParams& params, WebContents* web_contents, bool is_link, |
| 352 const std::string& extra_headers) { | 361 const std::string& extra_headers) { |
| 353 int process_id = web_contents->GetRenderProcessHost()->GetID(); | 362 int process_id = web_contents->GetRenderProcessHost()->GetID(); |
| 354 int routing_id = web_contents->GetRoutingID(); | 363 int routing_id = web_contents->GetRoutingID(); |
| 355 AcquireFileAccessPermission( | 364 AcquireFileAccessPermission( |
| 356 web_contents, base::Bind(&CreateContextMenuDownload, process_id, | 365 web_contents, base::Bind(&CreateContextMenuDownload, process_id, |
| 357 routing_id, params, is_link, extra_headers)); | 366 routing_id, params, is_link, extra_headers)); |
| 358 } | 367 } |
| 359 | 368 |
| 360 void DownloadController::DangerousDownloadValidated( | |
| 361 WebContents* web_contents, | |
| 362 const std::string& download_guid, | |
| 363 bool accept) { | |
| 364 if (!web_contents) | |
| 365 return; | |
| 366 DownloadManager* dlm = | |
| 367 BrowserContext::GetDownloadManager(web_contents->GetBrowserContext()); | |
| 368 DownloadItem* item = dlm->GetDownloadByGuid(download_guid); | |
| 369 if (!item) | |
| 370 return; | |
| 371 if (accept) { | |
| 372 item->ValidateDangerousDownload(); | |
| 373 } else { | |
| 374 DownloadController::RecordDownloadCancelReason( | |
| 375 DownloadController::CANCEL_REASON_DANGEROUS_DOWNLOAD_INFOBAR_DISMISSED); | |
| 376 item->Remove(); | |
| 377 } | |
| 378 } | |
| OLD | NEW |