| 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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 const JavaParamRef<jstring>& jdownload_guid, | 108 const JavaParamRef<jstring>& jdownload_guid, |
| 109 bool is_off_the_record) { | 109 bool is_off_the_record) { |
| 110 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); | 110 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
| 111 if (is_history_query_complete_ || is_off_the_record) | 111 if (is_history_query_complete_ || is_off_the_record) |
| 112 RemoveDownloadInternal(download_guid, is_off_the_record); | 112 RemoveDownloadInternal(download_guid, is_off_the_record); |
| 113 else | 113 else |
| 114 EnqueueDownloadAction(download_guid, REMOVE); | 114 EnqueueDownloadAction(download_guid, REMOVE); |
| 115 } | 115 } |
| 116 | 116 |
| 117 void DownloadManagerService::GetAllDownloads(JNIEnv* env, | 117 void DownloadManagerService::GetAllDownloads(JNIEnv* env, |
| 118 const JavaParamRef<jobject>& obj) { | 118 const JavaParamRef<jobject>& obj, |
| 119 bool is_off_the_record) { |
| 119 if (is_history_query_complete_) | 120 if (is_history_query_complete_) |
| 120 GetAllDownloadsInternal(); | 121 GetAllDownloadsInternal(is_off_the_record); |
| 122 else if (is_off_the_record) |
| 123 EnqueueDownloadAction(std::string(), INITIALIZE_OFF_THE_RECORD_UI); |
| 121 else | 124 else |
| 122 EnqueueDownloadAction(std::string(), INITIALIZE_UI); | 125 EnqueueDownloadAction(std::string(), INITIALIZE_UI); |
| 123 } | 126 } |
| 124 | 127 |
| 125 void DownloadManagerService::GetDownloadInfoFor( | 128 void DownloadManagerService::GetAllDownloadsInternal(bool is_off_the_record) { |
| 126 JNIEnv* env, | |
| 127 jobject obj, | |
| 128 const JavaParamRef<jstring>& jdownload_guid, | |
| 129 bool is_off_the_record) { | |
| 130 // The UI shouldn't be able to request any info about a specific item until it | |
| 131 // has been initialized with the list of existing downloads. | |
| 132 DCHECK(is_history_query_complete_ || is_off_the_record); | |
| 133 | |
| 134 content::DownloadManager* manager = GetDownloadManager(is_off_the_record); | 129 content::DownloadManager* manager = GetDownloadManager(is_off_the_record); |
| 135 if (java_ref_.is_null() || !manager) | 130 if (java_ref_.is_null() || !manager) |
| 136 return; | 131 return; |
| 137 | 132 |
| 138 // Search through the downloads for the entry with the given GUID. | |
| 139 content::DownloadManager::DownloadVector all_items; | |
| 140 manager->GetAllDownloads(&all_items); | |
| 141 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); | |
| 142 for (auto item : all_items) { | |
| 143 if (item->GetGuid() != download_guid) | |
| 144 continue; | |
| 145 | |
| 146 Java_DownloadManagerService_onDownloadItemUpdated( | |
| 147 env, java_ref_, ConvertUTF8ToJavaString(env, item->GetGuid()), | |
| 148 ConvertUTF8ToJavaString(env, item->GetFileNameToReportUser().value()), | |
| 149 ConvertUTF8ToJavaString(env, item->GetTargetFilePath().value()), | |
| 150 ConvertUTF8ToJavaString(env, item->GetTabUrl().spec()), | |
| 151 ConvertUTF8ToJavaString(env, item->GetMimeType()), | |
| 152 item->GetStartTime().ToJavaTime(), item->GetTotalBytes()); | |
| 153 break; | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 void DownloadManagerService::GetAllDownloadsInternal() { | |
| 158 content::DownloadManager* manager = GetDownloadManager(false); | |
| 159 if (java_ref_.is_null() || !manager) | |
| 160 return; | |
| 161 | |
| 162 content::DownloadManager::DownloadVector all_items; | 133 content::DownloadManager::DownloadVector all_items; |
| 163 manager->GetAllDownloads(&all_items); | 134 manager->GetAllDownloads(&all_items); |
| 164 | 135 |
| 165 // Create a Java array of all of the visible DownloadItems. | 136 // Create a Java array of all of the visible DownloadItems. |
| 166 JNIEnv* env = base::android::AttachCurrentThread(); | 137 JNIEnv* env = base::android::AttachCurrentThread(); |
| 167 ScopedJavaLocalRef<jobject> j_download_item_list = | 138 ScopedJavaLocalRef<jobject> j_download_item_list = |
| 168 Java_DownloadManagerService_createDownloadItemList(env, java_ref_); | 139 Java_DownloadManagerService_createDownloadItemList(env, java_ref_); |
| 169 | 140 |
| 170 for (size_t i = 0; i < all_items.size(); i++) { | 141 for (size_t i = 0; i < all_items.size(); i++) { |
| 171 content::DownloadItem* item = all_items[i]; | 142 content::DownloadItem* item = all_items[i]; |
| 172 if (!ShouldShowDownloadItem(item)) | 143 if (!ShouldShowDownloadItem(item)) |
| 173 continue; | 144 continue; |
| 174 | 145 |
| 175 Java_DownloadManagerService_addDownloadItemToList( | 146 Java_DownloadManagerService_addDownloadItemToList( |
| 176 env, java_ref_, j_download_item_list, | 147 env, java_ref_, j_download_item_list, |
| 177 ConvertUTF8ToJavaString(env, item->GetGuid()), | 148 ConvertUTF8ToJavaString(env, item->GetGuid()), |
| 178 ConvertUTF8ToJavaString(env, item->GetFileNameToReportUser().value()), | 149 ConvertUTF8ToJavaString(env, item->GetFileNameToReportUser().value()), |
| 179 ConvertUTF8ToJavaString(env, item->GetTargetFilePath().value()), | 150 ConvertUTF8ToJavaString(env, item->GetTargetFilePath().value()), |
| 180 ConvertUTF8ToJavaString(env, item->GetTabUrl().spec()), | 151 ConvertUTF8ToJavaString(env, item->GetTabUrl().spec()), |
| 181 ConvertUTF8ToJavaString(env, item->GetMimeType()), | 152 ConvertUTF8ToJavaString(env, item->GetMimeType()), |
| 182 item->GetStartTime().ToJavaTime(), item->GetTotalBytes()); | 153 item->GetStartTime().ToJavaTime(), item->GetTotalBytes()); |
| 183 } | 154 } |
| 184 | 155 |
| 185 Java_DownloadManagerService_onAllDownloadsRetrieved(env, java_ref_, | 156 Java_DownloadManagerService_onAllDownloadsRetrieved( |
| 186 j_download_item_list); | 157 env, java_ref_, j_download_item_list, is_off_the_record); |
| 187 } | 158 } |
| 188 | 159 |
| 189 | 160 |
| 190 void DownloadManagerService::CancelDownload( | 161 void DownloadManagerService::CancelDownload( |
| 191 JNIEnv* env, | 162 JNIEnv* env, |
| 192 jobject obj, | 163 jobject obj, |
| 193 const JavaParamRef<jstring>& jdownload_guid, | 164 const JavaParamRef<jstring>& jdownload_guid, |
| 194 bool is_off_the_record, | 165 bool is_off_the_record, |
| 195 bool is_notification_dismissed) { | 166 bool is_notification_dismissed) { |
| 196 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); | 167 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 214 case RESUME: | 185 case RESUME: |
| 215 ResumeDownloadInternal(download_guid, false); | 186 ResumeDownloadInternal(download_guid, false); |
| 216 break; | 187 break; |
| 217 case PAUSE: | 188 case PAUSE: |
| 218 PauseDownloadInternal(download_guid, false); | 189 PauseDownloadInternal(download_guid, false); |
| 219 break; | 190 break; |
| 220 case CANCEL: | 191 case CANCEL: |
| 221 CancelDownloadInternal(download_guid, false); | 192 CancelDownloadInternal(download_guid, false); |
| 222 break; | 193 break; |
| 223 case INITIALIZE_UI: | 194 case INITIALIZE_UI: |
| 224 GetAllDownloadsInternal(); | 195 GetAllDownloadsInternal(false); |
| 196 break; |
| 197 case INITIALIZE_OFF_THE_RECORD_UI: |
| 198 GetAllDownloadsInternal(true); |
| 225 break; | 199 break; |
| 226 default: | 200 default: |
| 227 NOTREACHED(); | 201 NOTREACHED(); |
| 228 break; | 202 break; |
| 229 } | 203 } |
| 230 } | 204 } |
| 231 pending_actions_.clear(); | 205 pending_actions_.clear(); |
| 232 | 206 |
| 233 // Monitor all DownloadItems for changes. | 207 // Monitor all DownloadItems for changes. |
| 234 content::DownloadManager* manager = GetDownloadManager(false); | 208 content::DownloadManager* manager = GetDownloadManager(false); |
| 235 if (manager) | 209 if (manager) |
| 236 original_notifier_.reset(new AllDownloadItemNotifier(manager, this)); | 210 original_notifier_.reset(new AllDownloadItemNotifier(manager, this)); |
| 211 |
| 212 content::DownloadManager* off_the_record_manager = GetDownloadManager(true); |
| 213 if (off_the_record_manager) { |
| 214 off_the_record_notifier_.reset( |
| 215 new AllDownloadItemNotifier(off_the_record_manager, this)); |
| 216 } |
| 217 } |
| 218 |
| 219 void DownloadManagerService::OnDownloadUpdated( |
| 220 content::DownloadManager* manager, content::DownloadItem* item) { |
| 221 // Ignore anything that isn't a completed download notification. |
| 222 if (!item->IsDone() || java_ref_.is_null()) |
| 223 return; |
| 224 |
| 225 JNIEnv* env = base::android::AttachCurrentThread(); |
| 226 Java_DownloadManagerService_onDownloadItemUpdated( |
| 227 env, |
| 228 java_ref_.obj(), |
| 229 ConvertUTF8ToJavaString(env, item->GetGuid()).obj(), |
| 230 ConvertUTF8ToJavaString( |
| 231 env, item->GetFileNameToReportUser().value()).obj(), |
| 232 ConvertUTF8ToJavaString( |
| 233 env, item->GetTargetFilePath().value()).obj(), |
| 234 ConvertUTF8ToJavaString(env, item->GetTabUrl().spec()).obj(), |
| 235 ConvertUTF8ToJavaString(env, item->GetMimeType()).obj(), |
| 236 item->GetStartTime().ToJavaTime(), |
| 237 item->GetTotalBytes(), |
| 238 item->GetBrowserContext()->IsOffTheRecord()); |
| 237 } | 239 } |
| 238 | 240 |
| 239 void DownloadManagerService::OnDownloadRemoved( | 241 void DownloadManagerService::OnDownloadRemoved( |
| 240 content::DownloadManager* manager, content::DownloadItem* item) { | 242 content::DownloadManager* manager, content::DownloadItem* item) { |
| 243 if (java_ref_.is_null()) |
| 244 return; |
| 245 |
| 241 JNIEnv* env = base::android::AttachCurrentThread(); | 246 JNIEnv* env = base::android::AttachCurrentThread(); |
| 242 Java_DownloadManagerService_onDownloadItemRemoved( | 247 Java_DownloadManagerService_onDownloadItemRemoved( |
| 243 env, java_ref_, ConvertUTF8ToJavaString(env, item->GetGuid())); | 248 env, |
| 249 java_ref_.obj(), |
| 250 ConvertUTF8ToJavaString(env, item->GetGuid()), |
| 251 item->GetBrowserContext()->IsOffTheRecord()); |
| 244 } | 252 } |
| 245 | 253 |
| 246 void DownloadManagerService::ResumeDownloadInternal( | 254 void DownloadManagerService::ResumeDownloadInternal( |
| 247 const std::string& download_guid, bool is_off_the_record) { | 255 const std::string& download_guid, bool is_off_the_record) { |
| 248 content::DownloadManager* manager = GetDownloadManager(is_off_the_record); | 256 content::DownloadManager* manager = GetDownloadManager(is_off_the_record); |
| 249 if (!manager) { | 257 if (!manager) { |
| 250 OnResumptionFailed(download_guid); | 258 OnResumptionFailed(download_guid); |
| 251 return; | 259 return; |
| 252 } | 260 } |
| 253 content::DownloadItem* item = manager->GetDownloadByGuid(download_guid); | 261 content::DownloadItem* item = manager->GetDownloadByGuid(download_guid); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 if (iter->second == RESUME) | 324 if (iter->second == RESUME) |
| 317 iter->second = action; | 325 iter->second = action; |
| 318 break; | 326 break; |
| 319 case CANCEL: | 327 case CANCEL: |
| 320 iter->second = action; | 328 iter->second = action; |
| 321 break; | 329 break; |
| 322 case REMOVE: | 330 case REMOVE: |
| 323 iter->second = action; | 331 iter->second = action; |
| 324 break; | 332 break; |
| 325 case INITIALIZE_UI: | 333 case INITIALIZE_UI: |
| 334 case INITIALIZE_OFF_THE_RECORD_UI: |
| 326 iter->second = action; | 335 iter->second = action; |
| 327 break; | 336 break; |
| 328 default: | 337 default: |
| 329 NOTREACHED(); | 338 NOTREACHED(); |
| 330 break; | 339 break; |
| 331 } | 340 } |
| 332 } | 341 } |
| 333 | 342 |
| 334 void DownloadManagerService::OnResumptionFailed( | 343 void DownloadManagerService::OnResumptionFailed( |
| 335 const std::string& download_guid) { | 344 const std::string& download_guid) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 351 resume_callback_for_testing_.Run(false); | 360 resume_callback_for_testing_.Run(false); |
| 352 } | 361 } |
| 353 | 362 |
| 354 content::DownloadManager* DownloadManagerService::GetDownloadManager( | 363 content::DownloadManager* DownloadManagerService::GetDownloadManager( |
| 355 bool is_off_the_record) { | 364 bool is_off_the_record) { |
| 356 Profile* profile = ProfileManager::GetActiveUserProfile(); | 365 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 357 if (is_off_the_record) | 366 if (is_off_the_record) |
| 358 profile = profile->GetOffTheRecordProfile(); | 367 profile = profile->GetOffTheRecordProfile(); |
| 359 return content::BrowserContext::GetDownloadManager(profile); | 368 return content::BrowserContext::GetDownloadManager(profile); |
| 360 } | 369 } |
| OLD | NEW |