| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_manager_service.h" | 5 #include "chrome/browser/android/download/download_manager_service.h" |
| 6 | 6 |
| 7 #include "base/android/jni_string.h" | 7 #include "base/android/jni_string.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "chrome/browser/android/download/download_controller.h" | 12 #include "chrome/browser/android/download/download_controller.h" |
| 13 #include "chrome/browser/download/download_service.h" | 13 #include "chrome/browser/download/download_service.h" |
| 14 #include "chrome/browser/download/download_service_factory.h" | 14 #include "chrome/browser/download/download_service_factory.h" |
| 15 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
| 16 #include "chrome/grit/generated_resources.h" | 16 #include "chrome/grit/generated_resources.h" |
| 17 #include "components/mime_util/mime_util.h" | 17 #include "components/mime_util/mime_util.h" |
| 18 #include "content/public/browser/browser_context.h" | 18 #include "content/public/browser/browser_context.h" |
| 19 #include "content/public/browser/download_item.h" | 19 #include "content/public/browser/download_item.h" |
| 20 #include "jni/DownloadInfo_jni.h" |
| 21 #include "jni/DownloadItem_jni.h" |
| 20 #include "jni/DownloadManagerService_jni.h" | 22 #include "jni/DownloadManagerService_jni.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
| 22 | 24 |
| 23 using base::android::JavaParamRef; | 25 using base::android::JavaParamRef; |
| 24 using base::android::ConvertJavaStringToUTF8; | 26 using base::android::ConvertJavaStringToUTF8; |
| 25 using base::android::ConvertUTF8ToJavaString; | 27 using base::android::ConvertUTF8ToJavaString; |
| 26 using base::android::ScopedJavaLocalRef; | 28 using base::android::ScopedJavaLocalRef; |
| 27 | 29 |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 30 bool ShouldShowDownloadItem(content::DownloadItem* item) { | 32 bool ShouldShowDownloadItem(content::DownloadItem* item) { |
| 31 return !item->IsTemporary(); | 33 return !item->IsTemporary(); |
| 32 } | 34 } |
| 33 | 35 |
| 34 void UpdateNotifier(DownloadManagerService* service, | 36 void UpdateNotifier(DownloadManagerService* service, |
| 35 content::DownloadManager* manager, | 37 content::DownloadManager* manager, |
| 36 std::unique_ptr<AllDownloadItemNotifier>& notifier) { | 38 std::unique_ptr<AllDownloadItemNotifier>& notifier) { |
| 37 if (manager) { | 39 if (manager) { |
| 38 if (!notifier || notifier->GetManager() != manager) | 40 if (!notifier || notifier->GetManager() != manager) |
| 39 notifier.reset(new AllDownloadItemNotifier(manager, service)); | 41 notifier.reset(new AllDownloadItemNotifier(manager, service)); |
| 40 } else { | 42 } else { |
| 41 notifier.reset(nullptr); | 43 notifier.reset(nullptr); |
| 42 } | 44 } |
| 43 } | 45 } |
| 44 | 46 |
| 45 void RemoveDownloadsFromDownloadManager( | |
| 46 content::DownloadManager* manager, | |
| 47 const base::FilePath& path) { | |
| 48 if (!manager) | |
| 49 return; | |
| 50 content::DownloadManager::DownloadVector all_items; | |
| 51 manager->GetAllDownloads(&all_items); | |
| 52 | |
| 53 for (size_t i = 0; i < all_items.size(); i++) { | |
| 54 content::DownloadItem* item = all_items[i]; | |
| 55 if (item->GetState() == content::DownloadItem::COMPLETE && | |
| 56 item->GetTargetFilePath() == path) { | |
| 57 item->Remove(); | |
| 58 } | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 ScopedJavaLocalRef<jobject> CreateJavaDownloadItem( | 47 ScopedJavaLocalRef<jobject> CreateJavaDownloadItem( |
| 63 JNIEnv* env, content::DownloadItem* item) { | 48 JNIEnv* env, content::DownloadItem* item) { |
| 64 return Java_DownloadManagerService_createDownloadItem( | 49 return Java_DownloadItem_createDownloadItem( |
| 65 env, | 50 env, DownloadManagerService::CreateJavaDownloadInfo(env, item), |
| 66 ConvertUTF8ToJavaString(env, item->GetGuid()), | 51 item->GetStartTime().ToJavaTime(), item->GetFileExternallyRemoved()); |
| 67 ConvertUTF8ToJavaString(env, | |
| 68 item->GetFileNameToReportUser().value()), | |
| 69 ConvertUTF8ToJavaString(env, item->GetTargetFilePath().value()), | |
| 70 ConvertUTF8ToJavaString(env, item->GetTabUrl().spec()), | |
| 71 ConvertUTF8ToJavaString(env, item->GetMimeType()), | |
| 72 item->GetStartTime().ToJavaTime(), item->GetTotalBytes(), | |
| 73 item->GetFileExternallyRemoved(), | |
| 74 item->GetBrowserContext()->IsOffTheRecord(), | |
| 75 item->GetState(), | |
| 76 item->PercentComplete(), | |
| 77 item->IsPaused()); | |
| 78 } | 52 } |
| 79 | 53 |
| 80 } // namespace | 54 } // namespace |
| 81 | 55 |
| 82 // static | 56 // static |
| 83 bool DownloadManagerService::RegisterDownloadManagerService(JNIEnv* env) { | 57 bool DownloadManagerService::RegisterDownloadManagerService(JNIEnv* env) { |
| 84 return RegisterNativesImpl(env); | 58 return RegisterNativesImpl(env); |
| 85 } | 59 } |
| 86 | 60 |
| 87 // static | 61 // static |
| 88 void DownloadManagerService::OnDownloadCanceled( | 62 void DownloadManagerService::OnDownloadCanceled( |
| 89 content::DownloadItem* download, | 63 content::DownloadItem* download, |
| 90 DownloadController::DownloadCancelReason reason) { | 64 DownloadController::DownloadCancelReason reason) { |
| 91 bool has_no_external_storage = | 65 bool has_no_external_storage = |
| 92 (reason == DownloadController::CANCEL_REASON_NO_EXTERNAL_STORAGE); | 66 (reason == DownloadController::CANCEL_REASON_NO_EXTERNAL_STORAGE); |
| 93 JNIEnv* env = base::android::AttachCurrentThread(); | 67 JNIEnv* env = base::android::AttachCurrentThread(); |
| 94 ScopedJavaLocalRef<jstring> jname = | 68 ScopedJavaLocalRef<jstring> jname = |
| 95 ConvertUTF8ToJavaString(env, download->GetURL().ExtractFileName()); | 69 ConvertUTF8ToJavaString(env, download->GetURL().ExtractFileName()); |
| 96 Java_DownloadManagerService_onDownloadItemCanceled(env, jname, | 70 Java_DownloadManagerService_onDownloadItemCanceled(env, jname, |
| 97 has_no_external_storage); | 71 has_no_external_storage); |
| 98 DownloadController::RecordDownloadCancelReason(reason); | 72 DownloadController::RecordDownloadCancelReason(reason); |
| 99 } | 73 } |
| 100 | 74 |
| 101 // static | 75 // static |
| 102 DownloadManagerService* DownloadManagerService::GetInstance() { | 76 DownloadManagerService* DownloadManagerService::GetInstance() { |
| 103 return base::Singleton<DownloadManagerService>::get(); | 77 return base::Singleton<DownloadManagerService>::get(); |
| 104 } | 78 } |
| 105 | 79 |
| 80 // static |
| 81 ScopedJavaLocalRef<jobject> DownloadManagerService::CreateJavaDownloadInfo( |
| 82 JNIEnv* env, content::DownloadItem* item) { |
| 83 ui::PageTransition base_transition = |
| 84 ui::PageTransitionStripQualifier(item->GetTransitionType()); |
| 85 bool user_initiated = |
| 86 (item->GetTransitionType() & ui::PAGE_TRANSITION_FROM_ADDRESS_BAR) || |
| 87 base_transition == ui::PAGE_TRANSITION_TYPED || |
| 88 base_transition == ui::PAGE_TRANSITION_AUTO_BOOKMARK || |
| 89 base_transition == ui::PAGE_TRANSITION_GENERATED || |
| 90 base_transition == ui::PAGE_TRANSITION_RELOAD || |
| 91 base_transition == ui::PAGE_TRANSITION_KEYWORD; |
| 92 bool has_user_gesture = item->HasUserGesture() || user_initiated; |
| 93 |
| 94 base::TimeDelta time_delta; |
| 95 item->TimeRemaining(&time_delta); |
| 96 |
| 97 return Java_DownloadInfo_createDownloadInfo( |
| 98 env, |
| 99 ConvertUTF8ToJavaString(env, item->GetGuid()), |
| 100 ConvertUTF8ToJavaString(env, |
| 101 item->GetFileNameToReportUser().value()), |
| 102 ConvertUTF8ToJavaString(env, item->GetTargetFilePath().value()), |
| 103 ConvertUTF8ToJavaString(env, item->GetTabUrl().spec()), |
| 104 ConvertUTF8ToJavaString(env, item->GetMimeType()), |
| 105 item->GetReceivedBytes(), |
| 106 item->GetBrowserContext()->IsOffTheRecord(), |
| 107 item->GetState(), |
| 108 item->PercentComplete(), |
| 109 item->IsPaused(), |
| 110 has_user_gesture, |
| 111 item->CanResume(), |
| 112 ConvertUTF8ToJavaString(env, item->GetOriginalUrl().spec()), |
| 113 ConvertUTF8ToJavaString(env, item->GetReferrerUrl().spec()), |
| 114 time_delta.InMilliseconds()); |
| 115 } |
| 116 |
| 106 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& jobj) { | 117 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& jobj) { |
| 107 Profile* profile = ProfileManager::GetActiveUserProfile(); | 118 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 108 DownloadManagerService* service = DownloadManagerService::GetInstance(); | 119 DownloadManagerService* service = DownloadManagerService::GetInstance(); |
| 109 service->Init(env, jobj); | 120 service->Init(env, jobj); |
| 110 DownloadService* download_service = | 121 DownloadService* download_service = |
| 111 DownloadServiceFactory::GetForBrowserContext(profile); | 122 DownloadServiceFactory::GetForBrowserContext(profile); |
| 112 DownloadHistory* history = download_service->GetDownloadHistory(); | 123 DownloadHistory* history = download_service->GetDownloadHistory(); |
| 113 if (history) | 124 if (history) |
| 114 history->AddObserver(service); | 125 history->AddObserver(service); |
| 115 return reinterpret_cast<intptr_t>(service); | 126 return reinterpret_cast<intptr_t>(service); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 // requests to check for externally removed downloads. | 222 // requests to check for externally removed downloads. |
| 212 if (!is_history_query_complete_) | 223 if (!is_history_query_complete_) |
| 213 return; | 224 return; |
| 214 | 225 |
| 215 content::DownloadManager* manager = GetDownloadManager(is_off_the_record); | 226 content::DownloadManager* manager = GetDownloadManager(is_off_the_record); |
| 216 if (!manager) | 227 if (!manager) |
| 217 return; | 228 return; |
| 218 manager->CheckForHistoryFilesRemoval(); | 229 manager->CheckForHistoryFilesRemoval(); |
| 219 } | 230 } |
| 220 | 231 |
| 221 void DownloadManagerService::RemoveDownloadsForPath( | |
| 222 const base::FilePath& path) { | |
| 223 content::DownloadManager* manager = GetDownloadManager(false); | |
| 224 RemoveDownloadsFromDownloadManager(manager, path); | |
| 225 manager = GetDownloadManager(true); | |
| 226 RemoveDownloadsFromDownloadManager(manager, path); | |
| 227 } | |
| 228 | |
| 229 void DownloadManagerService::CancelDownload( | 232 void DownloadManagerService::CancelDownload( |
| 230 JNIEnv* env, | 233 JNIEnv* env, |
| 231 jobject obj, | 234 jobject obj, |
| 232 const JavaParamRef<jstring>& jdownload_guid, | 235 const JavaParamRef<jstring>& jdownload_guid, |
| 233 bool is_off_the_record, | 236 bool is_off_the_record, |
| 234 bool is_notification_dismissed) { | 237 bool is_notification_dismissed) { |
| 235 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); | 238 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
| 236 DownloadController::RecordDownloadCancelReason( | 239 DownloadController::RecordDownloadCancelReason( |
| 237 is_notification_dismissed ? | 240 is_notification_dismissed ? |
| 238 DownloadController::CANCEL_REASON_NOTIFICATION_DISMISSED : | 241 DownloadController::CANCEL_REASON_NOTIFICATION_DISMISSED : |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 } | 440 } |
| 438 | 441 |
| 439 // static | 442 // static |
| 440 jboolean IsSupportedMimeType( | 443 jboolean IsSupportedMimeType( |
| 441 JNIEnv* env, | 444 JNIEnv* env, |
| 442 const JavaParamRef<jclass>& clazz, | 445 const JavaParamRef<jclass>& clazz, |
| 443 const JavaParamRef<jstring>& jmime_type) { | 446 const JavaParamRef<jstring>& jmime_type) { |
| 444 std::string mime_type = ConvertJavaStringToUTF8(env, jmime_type); | 447 std::string mime_type = ConvertJavaStringToUTF8(env, jmime_type); |
| 445 return mime_util::IsSupportedMimeType(mime_type); | 448 return mime_util::IsSupportedMimeType(mime_type); |
| 446 } | 449 } |
| OLD | NEW |