| 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" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 content::DownloadManager* manager, | 37 content::DownloadManager* manager, |
| 38 std::unique_ptr<AllDownloadItemNotifier>& notifier) { | 38 std::unique_ptr<AllDownloadItemNotifier>& notifier) { |
| 39 if (manager) { | 39 if (manager) { |
| 40 if (!notifier || notifier->GetManager() != manager) | 40 if (!notifier || notifier->GetManager() != manager) |
| 41 notifier.reset(new AllDownloadItemNotifier(manager, service)); | 41 notifier.reset(new AllDownloadItemNotifier(manager, service)); |
| 42 } else { | 42 } else { |
| 43 notifier.reset(nullptr); | 43 notifier.reset(nullptr); |
| 44 } | 44 } |
| 45 } | 45 } |
| 46 | 46 |
| 47 void RemoveDownloadsFromDownloadManager( |
| 48 content::DownloadManager* manager, |
| 49 const base::FilePath& path) { |
| 50 if (!manager) |
| 51 return; |
| 52 content::DownloadManager::DownloadVector all_items; |
| 53 manager->GetAllDownloads(&all_items); |
| 54 |
| 55 for (size_t i = 0; i < all_items.size(); i++) { |
| 56 content::DownloadItem* item = all_items[i]; |
| 57 if (item->GetState() == content::DownloadItem::COMPLETE && |
| 58 item->GetTargetFilePath() == path) { |
| 59 item->Remove(); |
| 60 } |
| 61 } |
| 62 } |
| 63 |
| 47 } // namespace | 64 } // namespace |
| 48 | 65 |
| 49 // static | 66 // static |
| 50 bool DownloadManagerService::RegisterDownloadManagerService(JNIEnv* env) { | 67 bool DownloadManagerService::RegisterDownloadManagerService(JNIEnv* env) { |
| 51 return RegisterNativesImpl(env); | 68 return RegisterNativesImpl(env); |
| 52 } | 69 } |
| 53 | 70 |
| 54 // static | 71 // static |
| 55 void DownloadManagerService::OnDownloadCanceled( | 72 void DownloadManagerService::OnDownloadCanceled( |
| 56 content::DownloadItem* download, | 73 content::DownloadItem* download, |
| 57 DownloadController::DownloadCancelReason reason) { | 74 DownloadController::DownloadCancelReason reason) { |
| 58 bool has_no_external_storage = | 75 bool has_no_external_storage = |
| 59 (reason == DownloadController::CANCEL_REASON_NO_EXTERNAL_STORAGE); | 76 (reason == DownloadController::CANCEL_REASON_NO_EXTERNAL_STORAGE); |
| 60 JNIEnv* env = base::android::AttachCurrentThread(); | 77 JNIEnv* env = base::android::AttachCurrentThread(); |
| 61 ScopedJavaLocalRef<jstring> jname = | 78 ScopedJavaLocalRef<jstring> jname = |
| 62 ConvertUTF8ToJavaString(env, download->GetURL().ExtractFileName()); | 79 ConvertUTF8ToJavaString(env, download->GetURL().ExtractFileName()); |
| 63 Java_DownloadManagerService_onDownloadItemCanceled(env, jname, | 80 Java_DownloadManagerService_onDownloadItemCanceled(env, jname, |
| 64 has_no_external_storage); | 81 has_no_external_storage); |
| 65 DownloadController::RecordDownloadCancelReason(reason); | 82 DownloadController::RecordDownloadCancelReason(reason); |
| 66 } | 83 } |
| 67 | 84 |
| 85 // static |
| 86 DownloadManagerService* DownloadManagerService::GetInstance() { |
| 87 return base::Singleton<DownloadManagerService>::get(); |
| 88 } |
| 68 | 89 |
| 69 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& jobj) { | 90 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& jobj) { |
| 70 Profile* profile = ProfileManager::GetActiveUserProfile(); | 91 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 71 DownloadManagerService* service = | 92 DownloadManagerService* service = DownloadManagerService::GetInstance(); |
| 72 new DownloadManagerService(env, jobj); | 93 service->Init(env, jobj); |
| 73 DownloadService* download_service = | 94 DownloadService* download_service = |
| 74 DownloadServiceFactory::GetForBrowserContext(profile); | 95 DownloadServiceFactory::GetForBrowserContext(profile); |
| 75 DownloadHistory* history = download_service->GetDownloadHistory(); | 96 DownloadHistory* history = download_service->GetDownloadHistory(); |
| 76 if (history) | 97 if (history) |
| 77 history->AddObserver(service); | 98 history->AddObserver(service); |
| 78 return reinterpret_cast<intptr_t>(service); | 99 return reinterpret_cast<intptr_t>(service); |
| 79 } | 100 } |
| 80 | 101 |
| 81 DownloadManagerService::DownloadManagerService( | 102 DownloadManagerService::DownloadManagerService() |
| 103 : is_history_query_complete_(false), |
| 104 pending_get_downloads_actions_(NONE) { |
| 105 } |
| 106 |
| 107 DownloadManagerService::~DownloadManagerService() {} |
| 108 |
| 109 void DownloadManagerService::Init( |
| 82 JNIEnv* env, | 110 JNIEnv* env, |
| 83 jobject obj) | 111 jobject obj) { |
| 84 : java_ref_(env, obj), | 112 java_ref_.Reset(env, obj); |
| 85 is_history_query_complete_(false), | |
| 86 pending_get_downloads_actions_(NONE) { | |
| 87 DownloadControllerBase::Get()->SetDefaultDownloadFileName( | 113 DownloadControllerBase::Get()->SetDefaultDownloadFileName( |
| 88 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); | 114 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); |
| 89 } | 115 } |
| 90 | 116 |
| 91 DownloadManagerService::~DownloadManagerService() {} | |
| 92 | |
| 93 void DownloadManagerService::ResumeDownload( | 117 void DownloadManagerService::ResumeDownload( |
| 94 JNIEnv* env, | 118 JNIEnv* env, |
| 95 jobject obj, | 119 jobject obj, |
| 96 const JavaParamRef<jstring>& jdownload_guid, | 120 const JavaParamRef<jstring>& jdownload_guid, |
| 97 bool is_off_the_record) { | 121 bool is_off_the_record) { |
| 98 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); | 122 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
| 99 if (is_history_query_complete_ || is_off_the_record) | 123 if (is_history_query_complete_ || is_off_the_record) |
| 100 ResumeDownloadInternal(download_guid, is_off_the_record); | 124 ResumeDownloadInternal(download_guid, is_off_the_record); |
| 101 else | 125 else |
| 102 EnqueueDownloadAction(download_guid, RESUME); | 126 EnqueueDownloadAction(download_guid, RESUME); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 // requests to check for externally removed downloads. | 203 // requests to check for externally removed downloads. |
| 180 if (!is_history_query_complete_) | 204 if (!is_history_query_complete_) |
| 181 return; | 205 return; |
| 182 | 206 |
| 183 content::DownloadManager* manager = GetDownloadManager(is_off_the_record); | 207 content::DownloadManager* manager = GetDownloadManager(is_off_the_record); |
| 184 if (!manager) | 208 if (!manager) |
| 185 return; | 209 return; |
| 186 manager->CheckForHistoryFilesRemoval(); | 210 manager->CheckForHistoryFilesRemoval(); |
| 187 } | 211 } |
| 188 | 212 |
| 213 void DownloadManagerService::RemoveDownloadsForPath( |
| 214 const base::FilePath& path) { |
| 215 content::DownloadManager* manager = GetDownloadManager(false); |
| 216 RemoveDownloadsFromDownloadManager(manager, path); |
| 217 manager = GetDownloadManager(true); |
| 218 RemoveDownloadsFromDownloadManager(manager, path); |
| 219 } |
| 220 |
| 189 void DownloadManagerService::CancelDownload( | 221 void DownloadManagerService::CancelDownload( |
| 190 JNIEnv* env, | 222 JNIEnv* env, |
| 191 jobject obj, | 223 jobject obj, |
| 192 const JavaParamRef<jstring>& jdownload_guid, | 224 const JavaParamRef<jstring>& jdownload_guid, |
| 193 bool is_off_the_record, | 225 bool is_off_the_record, |
| 194 bool is_notification_dismissed) { | 226 bool is_notification_dismissed) { |
| 195 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); | 227 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
| 196 DownloadController::RecordDownloadCancelReason( | 228 DownloadController::RecordDownloadCancelReason( |
| 197 is_notification_dismissed ? | 229 is_notification_dismissed ? |
| 198 DownloadController::CANCEL_REASON_NOTIFICATION_DISMISSED : | 230 DownloadController::CANCEL_REASON_NOTIFICATION_DISMISSED : |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 content::DownloadManager* manager = | 418 content::DownloadManager* manager = |
| 387 content::BrowserContext::GetDownloadManager(profile); | 419 content::BrowserContext::GetDownloadManager(profile); |
| 388 | 420 |
| 389 // Update notifiers to monitor any newly created DownloadManagers. | 421 // Update notifiers to monitor any newly created DownloadManagers. |
| 390 updateNotifier( | 422 updateNotifier( |
| 391 this, manager, | 423 this, manager, |
| 392 is_off_the_record ? off_the_record_notifier_ : original_notifier_); | 424 is_off_the_record ? off_the_record_notifier_ : original_notifier_); |
| 393 | 425 |
| 394 return manager; | 426 return manager; |
| 395 } | 427 } |
| OLD | NEW |