Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(301)

Side by Side Diff: chrome/browser/android/download/download_manager_service.cc

Issue 2344483003: Ask download manager to remove a download if it is overwritten by another (Closed)
Patch Set: delete completed download with same path Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 &&
Theresa 2016/09/21 17:40:43 Is it possible to interleave downloading items wit
qinmin 2016/09/21 21:20:29 If a download starts (or resumes) with the same ta
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 }
105
106 DownloadManagerService::~DownloadManagerService() {}
107
108 void DownloadManagerService::Init(
82 JNIEnv* env, 109 JNIEnv* env,
83 jobject obj) 110 jobject obj) {
84 : java_ref_(env, obj), 111 java_ref_.Reset(env, obj);
85 is_history_query_complete_(false) {
86 DownloadControllerBase::Get()->SetDefaultDownloadFileName( 112 DownloadControllerBase::Get()->SetDefaultDownloadFileName(
87 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); 113 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME));
88 } 114 }
89 115
90 DownloadManagerService::~DownloadManagerService() {}
91
92 void DownloadManagerService::ResumeDownload( 116 void DownloadManagerService::ResumeDownload(
93 JNIEnv* env, 117 JNIEnv* env,
94 jobject obj, 118 jobject obj,
95 const JavaParamRef<jstring>& jdownload_guid, 119 const JavaParamRef<jstring>& jdownload_guid,
96 bool is_off_the_record) { 120 bool is_off_the_record) {
97 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); 121 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid);
98 if (is_history_query_complete_ || is_off_the_record) 122 if (is_history_query_complete_ || is_off_the_record)
99 ResumeDownloadInternal(download_guid, is_off_the_record); 123 ResumeDownloadInternal(download_guid, is_off_the_record);
100 else 124 else
101 EnqueueDownloadAction(download_guid, RESUME); 125 EnqueueDownloadAction(download_guid, RESUME);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 // requests to check for externally removed downloads. 202 // requests to check for externally removed downloads.
179 if (!is_history_query_complete_) 203 if (!is_history_query_complete_)
180 return; 204 return;
181 205
182 content::DownloadManager* manager = GetDownloadManager(is_off_the_record); 206 content::DownloadManager* manager = GetDownloadManager(is_off_the_record);
183 if (!manager) 207 if (!manager)
184 return; 208 return;
185 manager->CheckForHistoryFilesRemoval(); 209 manager->CheckForHistoryFilesRemoval();
186 } 210 }
187 211
212 void DownloadManagerService::RemoveExternallyRemovedDownloads(
213 const base::FilePath& path) {
214 content::DownloadManager* manager = GetDownloadManager(false);
215 RemoveDownloadsFromDownloadManager(manager, path);
216 manager = GetDownloadManager(true);
217 RemoveDownloadsFromDownloadManager(manager, path);
218 }
219
188 void DownloadManagerService::CancelDownload( 220 void DownloadManagerService::CancelDownload(
189 JNIEnv* env, 221 JNIEnv* env,
190 jobject obj, 222 jobject obj,
191 const JavaParamRef<jstring>& jdownload_guid, 223 const JavaParamRef<jstring>& jdownload_guid,
192 bool is_off_the_record, 224 bool is_off_the_record,
193 bool is_notification_dismissed) { 225 bool is_notification_dismissed) {
194 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); 226 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid);
195 DownloadController::RecordDownloadCancelReason( 227 DownloadController::RecordDownloadCancelReason(
196 is_notification_dismissed ? 228 is_notification_dismissed ?
197 DownloadController::CANCEL_REASON_NOTIFICATION_DISMISSED : 229 DownloadController::CANCEL_REASON_NOTIFICATION_DISMISSED :
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 content::DownloadManager* manager = 421 content::DownloadManager* manager =
390 content::BrowserContext::GetDownloadManager(profile); 422 content::BrowserContext::GetDownloadManager(profile);
391 423
392 // Update notifiers to monitor any newly created DownloadManagers. 424 // Update notifiers to monitor any newly created DownloadManagers.
393 updateNotifier( 425 updateNotifier(
394 this, manager, 426 this, manager,
395 is_off_the_record ? off_the_record_notifier_ : original_notifier_); 427 is_off_the_record ? off_the_record_notifier_ : original_notifier_);
396 428
397 return manager; 429 return manager;
398 } 430 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698