| 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 "content/public/browser/browser_context.h" | 17 #include "content/public/browser/browser_context.h" |
| 18 #include "content/public/browser/download_item.h" | 18 #include "content/public/browser/download_item.h" |
| 19 #include "jni/DownloadManagerService_jni.h" | 19 #include "jni/DownloadManagerService_jni.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
| 21 | 21 |
| 22 using base::android::JavaParamRef; | 22 using base::android::JavaParamRef; |
| 23 using base::android::ConvertJavaStringToUTF8; | 23 using base::android::ConvertJavaStringToUTF8; |
| 24 using base::android::ConvertUTF8ToJavaString; | 24 using base::android::ConvertUTF8ToJavaString; |
| 25 | 25 |
| 26 namespace { |
| 27 |
| 28 bool ShouldShowDownloadItem(content::DownloadItem* item) { |
| 29 return !item->IsTemporary() && |
| 30 !item->GetFileNameToReportUser().empty() && |
| 31 !item->GetTargetFilePath().empty(); |
| 32 } |
| 33 |
| 34 } // namespace |
| 35 |
| 26 // static | 36 // static |
| 27 bool DownloadManagerService::RegisterDownloadManagerService(JNIEnv* env) { | 37 bool DownloadManagerService::RegisterDownloadManagerService(JNIEnv* env) { |
| 28 return RegisterNativesImpl(env); | 38 return RegisterNativesImpl(env); |
| 29 } | 39 } |
| 30 | 40 |
| 31 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& jobj) { | 41 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& jobj) { |
| 32 Profile* profile = ProfileManager::GetActiveUserProfile(); | 42 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 33 DownloadManagerService* service = | 43 DownloadManagerService* service = |
| 34 new DownloadManagerService(env, jobj); | 44 new DownloadManagerService(env, jobj); |
| 35 DownloadService* download_service = | 45 DownloadService* download_service = |
| (...skipping 30 matching lines...) Expand all Loading... |
| 66 JNIEnv* env, | 76 JNIEnv* env, |
| 67 jobject obj, | 77 jobject obj, |
| 68 const JavaParamRef<jstring>& jdownload_guid) { | 78 const JavaParamRef<jstring>& jdownload_guid) { |
| 69 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); | 79 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
| 70 if (is_history_query_complete_) | 80 if (is_history_query_complete_) |
| 71 PauseDownloadInternal(download_guid); | 81 PauseDownloadInternal(download_guid); |
| 72 else | 82 else |
| 73 EnqueueDownloadAction(download_guid, PAUSE); | 83 EnqueueDownloadAction(download_guid, PAUSE); |
| 74 } | 84 } |
| 75 | 85 |
| 86 void DownloadManagerService::GetAllDownloads(JNIEnv* env, |
| 87 const JavaParamRef<jobject>& obj) { |
| 88 if (is_history_query_complete_) |
| 89 GetAllDownloadsInternal(); |
| 90 else |
| 91 EnqueueDownloadAction(std::string(), INITIALIZE_UI); |
| 92 } |
| 93 |
| 94 void DownloadManagerService::GetAllDownloadsInternal() { |
| 95 content::DownloadManager* manager = GetDownloadManager(false); |
| 96 if (java_ref_.is_null() || !manager) |
| 97 return; |
| 98 |
| 99 content::DownloadManager::DownloadVector all_items; |
| 100 manager->GetAllDownloads(&all_items); |
| 101 |
| 102 // Create a Java array of all of the visible DownloadItems. |
| 103 JNIEnv* env = base::android::AttachCurrentThread(); |
| 104 ScopedJavaLocalRef<jobject> j_download_item_list = |
| 105 Java_DownloadManagerService_createDownloadItemList(env, java_ref_.obj()); |
| 106 |
| 107 for (size_t i = 0; i < all_items.size(); i++) { |
| 108 content::DownloadItem* item = all_items[i]; |
| 109 if (!ShouldShowDownloadItem(item)) |
| 110 continue; |
| 111 |
| 112 Java_DownloadManagerService_addDownloadItemToList( |
| 113 env, |
| 114 java_ref_.obj(), |
| 115 j_download_item_list.obj(), |
| 116 ConvertUTF8ToJavaString(env, item->GetGuid()).obj(), |
| 117 ConvertUTF8ToJavaString( |
| 118 env, item->GetFileNameToReportUser().BaseName().value()).obj(), |
| 119 ConvertUTF8ToJavaString(env, item->GetTabUrl().spec()).obj(), |
| 120 ConvertUTF8ToJavaString(env, item->GetMimeType()).obj(), |
| 121 item->GetStartTime().ToJavaTime(), |
| 122 item->GetTotalBytes()); |
| 123 } |
| 124 |
| 125 Java_DownloadManagerService_onAllDownloadsRetrieved( |
| 126 env, java_ref_.obj(), j_download_item_list.obj()); |
| 127 } |
| 128 |
| 129 |
| 76 void DownloadManagerService::CancelDownload( | 130 void DownloadManagerService::CancelDownload( |
| 77 JNIEnv* env, | 131 JNIEnv* env, |
| 78 jobject obj, | 132 jobject obj, |
| 79 const JavaParamRef<jstring>& jdownload_guid, | 133 const JavaParamRef<jstring>& jdownload_guid, |
| 80 bool is_off_the_record, | 134 bool is_off_the_record, |
| 81 bool is_notification_dismissed) { | 135 bool is_notification_dismissed) { |
| 82 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); | 136 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
| 83 | 137 |
| 84 DownloadController::RecordDownloadCancelReason( | 138 DownloadController::RecordDownloadCancelReason( |
| 85 is_notification_dismissed ? | 139 is_notification_dismissed ? |
| (...skipping 21 matching lines...) Expand all Loading... |
| 107 switch (action) { | 161 switch (action) { |
| 108 case RESUME: | 162 case RESUME: |
| 109 ResumeDownloadInternal(download_guid); | 163 ResumeDownloadInternal(download_guid); |
| 110 break; | 164 break; |
| 111 case PAUSE: | 165 case PAUSE: |
| 112 PauseDownloadInternal(download_guid); | 166 PauseDownloadInternal(download_guid); |
| 113 break; | 167 break; |
| 114 case CANCEL: | 168 case CANCEL: |
| 115 CancelDownloadInternal(download_guid, false); | 169 CancelDownloadInternal(download_guid, false); |
| 116 break; | 170 break; |
| 171 case INITIALIZE_UI: |
| 172 GetAllDownloadsInternal(); |
| 173 break; |
| 117 default: | 174 default: |
| 118 NOTREACHED(); | 175 NOTREACHED(); |
| 119 break; | 176 break; |
| 120 } | 177 } |
| 121 } | 178 } |
| 122 pending_actions_.clear(); | 179 pending_actions_.clear(); |
| 123 } | 180 } |
| 124 | 181 |
| 125 void DownloadManagerService::ResumeDownloadInternal( | 182 void DownloadManagerService::ResumeDownloadInternal( |
| 126 const std::string& download_guid) { | 183 const std::string& download_guid) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 if (iter->second == PAUSE) | 238 if (iter->second == PAUSE) |
| 182 iter->second = action; | 239 iter->second = action; |
| 183 break; | 240 break; |
| 184 case PAUSE: | 241 case PAUSE: |
| 185 if (iter->second == RESUME) | 242 if (iter->second == RESUME) |
| 186 iter->second = action; | 243 iter->second = action; |
| 187 break; | 244 break; |
| 188 case CANCEL: | 245 case CANCEL: |
| 189 iter->second = action; | 246 iter->second = action; |
| 190 break; | 247 break; |
| 248 case INITIALIZE_UI: |
| 249 iter->second = action; |
| 250 break; |
| 191 default: | 251 default: |
| 192 NOTREACHED(); | 252 NOTREACHED(); |
| 193 break; | 253 break; |
| 194 } | 254 } |
| 195 } | 255 } |
| 196 | 256 |
| 197 void DownloadManagerService::OnResumptionFailed( | 257 void DownloadManagerService::OnResumptionFailed( |
| 198 const std::string& download_guid) { | 258 const std::string& download_guid) { |
| 199 base::ThreadTaskRunnerHandle::Get()->PostTask( | 259 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 200 FROM_HERE, base::Bind(&DownloadManagerService::OnResumptionFailedInternal, | 260 FROM_HERE, base::Bind(&DownloadManagerService::OnResumptionFailedInternal, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 215 resume_callback_for_testing_.Run(false); | 275 resume_callback_for_testing_.Run(false); |
| 216 } | 276 } |
| 217 | 277 |
| 218 content::DownloadManager* DownloadManagerService::GetDownloadManager( | 278 content::DownloadManager* DownloadManagerService::GetDownloadManager( |
| 219 bool is_off_the_record) { | 279 bool is_off_the_record) { |
| 220 Profile* profile = ProfileManager::GetActiveUserProfile(); | 280 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 221 if (is_off_the_record) | 281 if (is_off_the_record) |
| 222 profile = profile->GetOffTheRecordProfile(); | 282 profile = profile->GetOffTheRecordProfile(); |
| 223 return content::BrowserContext::GetDownloadManager(profile); | 283 return content::BrowserContext::GetDownloadManager(profile); |
| 224 } | 284 } |
| OLD | NEW |