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 13 matching lines...) Expand all Loading... |
24 using base::android::ConvertJavaStringToUTF8; | 24 using base::android::ConvertJavaStringToUTF8; |
25 using base::android::ConvertUTF8ToJavaString; | 25 using base::android::ConvertUTF8ToJavaString; |
26 using base::android::ScopedJavaLocalRef; | 26 using base::android::ScopedJavaLocalRef; |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 bool ShouldShowDownloadItem(content::DownloadItem* item) { | 30 bool ShouldShowDownloadItem(content::DownloadItem* item) { |
31 return !item->IsTemporary() && | 31 return !item->IsTemporary() && |
32 !item->GetFileNameToReportUser().empty() && | 32 !item->GetFileNameToReportUser().empty() && |
33 !item->GetTargetFilePath().empty() && | 33 !item->GetTargetFilePath().empty() && |
34 item->GetState() == content::DownloadItem::COMPLETE; | 34 item->GetState() != content::DownloadItem::CANCELLED; |
35 } | 35 } |
36 | 36 |
37 void updateNotifier(DownloadManagerService* service, | 37 void updateNotifier(DownloadManagerService* service, |
38 content::DownloadManager* manager, | 38 content::DownloadManager* manager, |
39 std::unique_ptr<AllDownloadItemNotifier>& notifier) { | 39 std::unique_ptr<AllDownloadItemNotifier>& notifier) { |
40 if (manager) { | 40 if (manager) { |
41 if (!notifier || notifier->GetManager() != manager) | 41 if (!notifier || notifier->GetManager() != manager) |
42 notifier.reset(new AllDownloadItemNotifier(manager, service)); | 42 notifier.reset(new AllDownloadItemNotifier(manager, service)); |
43 } else { | 43 } else { |
44 notifier.reset(nullptr); | 44 notifier.reset(nullptr); |
(...skipping 10 matching lines...) Expand all Loading... |
55 | 55 |
56 for (size_t i = 0; i < all_items.size(); i++) { | 56 for (size_t i = 0; i < all_items.size(); i++) { |
57 content::DownloadItem* item = all_items[i]; | 57 content::DownloadItem* item = all_items[i]; |
58 if (item->GetState() == content::DownloadItem::COMPLETE && | 58 if (item->GetState() == content::DownloadItem::COMPLETE && |
59 item->GetTargetFilePath() == path) { | 59 item->GetTargetFilePath() == path) { |
60 item->Remove(); | 60 item->Remove(); |
61 } | 61 } |
62 } | 62 } |
63 } | 63 } |
64 | 64 |
| 65 ScopedJavaLocalRef<jobject> CreateJavaDownloadItem( |
| 66 JNIEnv* env, content::DownloadItem* item) { |
| 67 return Java_DownloadManagerService_createDownloadItem( |
| 68 env, |
| 69 ConvertUTF8ToJavaString(env, item->GetGuid()), |
| 70 ConvertUTF8ToJavaString(env, |
| 71 item->GetFileNameToReportUser().value()), |
| 72 ConvertUTF8ToJavaString(env, item->GetTargetFilePath().value()), |
| 73 ConvertUTF8ToJavaString(env, item->GetTabUrl().spec()), |
| 74 ConvertUTF8ToJavaString(env, item->GetMimeType()), |
| 75 item->GetStartTime().ToJavaTime(), item->GetTotalBytes(), |
| 76 item->GetFileExternallyRemoved(), |
| 77 item->GetBrowserContext()->IsOffTheRecord(), |
| 78 item->GetState(), |
| 79 item->PercentComplete(), |
| 80 item->IsPaused()); |
| 81 } |
| 82 |
65 } // namespace | 83 } // namespace |
66 | 84 |
67 // static | 85 // static |
68 bool DownloadManagerService::RegisterDownloadManagerService(JNIEnv* env) { | 86 bool DownloadManagerService::RegisterDownloadManagerService(JNIEnv* env) { |
69 return RegisterNativesImpl(env); | 87 return RegisterNativesImpl(env); |
70 } | 88 } |
71 | 89 |
72 // static | 90 // static |
73 void DownloadManagerService::OnDownloadCanceled( | 91 void DownloadManagerService::OnDownloadCanceled( |
74 content::DownloadItem* download, | 92 content::DownloadItem* download, |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 // Create a Java array of all of the visible DownloadItems. | 189 // Create a Java array of all of the visible DownloadItems. |
172 JNIEnv* env = base::android::AttachCurrentThread(); | 190 JNIEnv* env = base::android::AttachCurrentThread(); |
173 ScopedJavaLocalRef<jobject> j_download_item_list = | 191 ScopedJavaLocalRef<jobject> j_download_item_list = |
174 Java_DownloadManagerService_createDownloadItemList(env, java_ref_); | 192 Java_DownloadManagerService_createDownloadItemList(env, java_ref_); |
175 | 193 |
176 for (size_t i = 0; i < all_items.size(); i++) { | 194 for (size_t i = 0; i < all_items.size(); i++) { |
177 content::DownloadItem* item = all_items[i]; | 195 content::DownloadItem* item = all_items[i]; |
178 if (!ShouldShowDownloadItem(item)) | 196 if (!ShouldShowDownloadItem(item)) |
179 continue; | 197 continue; |
180 | 198 |
| 199 ScopedJavaLocalRef<jobject> j_item = CreateJavaDownloadItem(env, item); |
181 Java_DownloadManagerService_addDownloadItemToList( | 200 Java_DownloadManagerService_addDownloadItemToList( |
182 env, java_ref_, j_download_item_list, | 201 env, java_ref_, j_download_item_list, j_item); |
183 ConvertUTF8ToJavaString(env, item->GetGuid()), | |
184 ConvertUTF8ToJavaString(env, item->GetFileNameToReportUser().value()), | |
185 ConvertUTF8ToJavaString(env, item->GetTargetFilePath().value()), | |
186 ConvertUTF8ToJavaString(env, item->GetTabUrl().spec()), | |
187 ConvertUTF8ToJavaString(env, item->GetMimeType()), | |
188 item->GetStartTime().ToJavaTime(), item->GetTotalBytes(), | |
189 item->GetFileExternallyRemoved()); | |
190 } | 202 } |
191 | 203 |
192 Java_DownloadManagerService_onAllDownloadsRetrieved( | 204 Java_DownloadManagerService_onAllDownloadsRetrieved( |
193 env, java_ref_, j_download_item_list, is_off_the_record); | 205 env, java_ref_, j_download_item_list, is_off_the_record); |
194 } | 206 } |
195 | 207 |
196 void DownloadManagerService::CheckForExternallyRemovedDownloads( | 208 void DownloadManagerService::CheckForExternallyRemovedDownloads( |
197 JNIEnv* env, | 209 JNIEnv* env, |
198 const JavaParamRef<jobject>& obj, | 210 const JavaParamRef<jobject>& obj, |
199 bool is_off_the_record) { | 211 bool is_off_the_record) { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 updateNotifier(this, GetDownloadManager(false), original_notifier_); | 279 updateNotifier(this, GetDownloadManager(false), original_notifier_); |
268 updateNotifier(this, GetDownloadManager(true), off_the_record_notifier_); | 280 updateNotifier(this, GetDownloadManager(true), off_the_record_notifier_); |
269 } | 281 } |
270 | 282 |
271 void DownloadManagerService::OnDownloadUpdated( | 283 void DownloadManagerService::OnDownloadUpdated( |
272 content::DownloadManager* manager, content::DownloadItem* item) { | 284 content::DownloadManager* manager, content::DownloadItem* item) { |
273 if (java_ref_.is_null()) | 285 if (java_ref_.is_null()) |
274 return; | 286 return; |
275 | 287 |
276 JNIEnv* env = base::android::AttachCurrentThread(); | 288 JNIEnv* env = base::android::AttachCurrentThread(); |
| 289 ScopedJavaLocalRef<jobject> j_item = CreateJavaDownloadItem(env, item); |
277 Java_DownloadManagerService_onDownloadItemUpdated( | 290 Java_DownloadManagerService_onDownloadItemUpdated( |
278 env, | 291 env, java_ref_.obj(), j_item); |
279 java_ref_.obj(), | |
280 item->GetState(), | |
281 ConvertUTF8ToJavaString(env, item->GetGuid()).obj(), | |
282 ConvertUTF8ToJavaString( | |
283 env, item->GetFileNameToReportUser().value()).obj(), | |
284 ConvertUTF8ToJavaString( | |
285 env, item->GetTargetFilePath().value()).obj(), | |
286 ConvertUTF8ToJavaString(env, item->GetTabUrl().spec()).obj(), | |
287 ConvertUTF8ToJavaString(env, item->GetMimeType()).obj(), | |
288 item->GetStartTime().ToJavaTime(), | |
289 item->GetTotalBytes(), | |
290 item->GetBrowserContext()->IsOffTheRecord(), | |
291 item->GetFileExternallyRemoved()); | |
292 } | 292 } |
293 | 293 |
294 void DownloadManagerService::OnDownloadRemoved( | 294 void DownloadManagerService::OnDownloadRemoved( |
295 content::DownloadManager* manager, content::DownloadItem* item) { | 295 content::DownloadManager* manager, content::DownloadItem* item) { |
296 if (java_ref_.is_null()) | 296 if (java_ref_.is_null()) |
297 return; | 297 return; |
298 | 298 |
299 JNIEnv* env = base::android::AttachCurrentThread(); | 299 JNIEnv* env = base::android::AttachCurrentThread(); |
300 Java_DownloadManagerService_onDownloadItemRemoved( | 300 Java_DownloadManagerService_onDownloadItemRemoved( |
301 env, | 301 env, |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 } | 428 } |
429 | 429 |
430 // static | 430 // static |
431 jboolean IsSupportedMimeType( | 431 jboolean IsSupportedMimeType( |
432 JNIEnv* env, | 432 JNIEnv* env, |
433 const JavaParamRef<jclass>& clazz, | 433 const JavaParamRef<jclass>& clazz, |
434 const JavaParamRef<jstring>& jmime_type) { | 434 const JavaParamRef<jstring>& jmime_type) { |
435 std::string mime_type = ConvertJavaStringToUTF8(env, jmime_type); | 435 std::string mime_type = ConvertJavaStringToUTF8(env, jmime_type); |
436 return mime_util::IsSupportedMimeType(mime_type); | 436 return mime_util::IsSupportedMimeType(mime_type); |
437 } | 437 } |
OLD | NEW |