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 // Determine how many items will be displayed. | |
103 int num_to_display = 0; | |
104 for (size_t i = 0; i < all_items.size(); i++) { | |
105 content::DownloadItem* item = all_items[i]; | |
106 num_to_display += (ShouldShowDownloadItem(item) ? 1 : 0); | |
107 } | |
108 | |
109 // Create a Java array of all of the visible DownloadItems. | |
110 JNIEnv* env = base::android::AttachCurrentThread(); | |
111 ScopedJavaLocalRef<jclass> j_download_item_class = base::android::GetClass( | |
qinmin
2016/07/14 19:48:18
This looks a little worse than the previous patch,
gone
2016/07/14 20:22:58
No worries; I reverted most of it, but kept the fu
| |
112 env, "org/chromium/chrome/browser/download/DownloadItem"); | |
113 ScopedJavaLocalRef<jobjectArray> j_item_array( | |
114 env, | |
115 env->NewObjectArray( | |
116 num_to_display, j_download_item_class.obj(), nullptr)); | |
117 | |
118 for (size_t i = 0, index = 0; i < all_items.size(); i++) { | |
119 content::DownloadItem* item = all_items[i]; | |
120 if (!ShouldShowDownloadItem(item)) | |
121 continue; | |
122 | |
123 ScopedJavaLocalRef<jobject> j_item = | |
124 Java_DownloadManagerService_createDownloadItem( | |
125 env, | |
126 java_ref_.obj(), | |
127 ConvertUTF8ToJavaString(env, item->GetGuid()).obj(), | |
128 ConvertUTF8ToJavaString( | |
129 env, | |
130 item->GetFileNameToReportUser().BaseName().value()).obj(), | |
131 ConvertUTF8ToJavaString(env, item->GetTabUrl().spec()).obj(), | |
132 ConvertUTF8ToJavaString(env, item->GetMimeType()).obj(), | |
133 item->GetStartTime().ToJavaTime(), | |
134 item->GetTotalBytes()); | |
135 env->SetObjectArrayElement(j_item_array.obj(), index++, j_item.obj()); | |
136 } | |
137 | |
138 Java_DownloadManagerService_onAllDownloadsRetrieved( | |
139 env, java_ref_.obj(), j_item_array.obj()); | |
140 } | |
141 | |
142 | |
76 void DownloadManagerService::CancelDownload( | 143 void DownloadManagerService::CancelDownload( |
77 JNIEnv* env, | 144 JNIEnv* env, |
78 jobject obj, | 145 jobject obj, |
79 const JavaParamRef<jstring>& jdownload_guid, | 146 const JavaParamRef<jstring>& jdownload_guid, |
80 bool is_off_the_record, | 147 bool is_off_the_record, |
81 bool is_notification_dismissed) { | 148 bool is_notification_dismissed) { |
82 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); | 149 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
83 | 150 |
84 DownloadController::RecordDownloadCancelReason( | 151 DownloadController::RecordDownloadCancelReason( |
85 is_notification_dismissed ? | 152 is_notification_dismissed ? |
(...skipping 21 matching lines...) Expand all Loading... | |
107 switch (action) { | 174 switch (action) { |
108 case RESUME: | 175 case RESUME: |
109 ResumeDownloadInternal(download_guid); | 176 ResumeDownloadInternal(download_guid); |
110 break; | 177 break; |
111 case PAUSE: | 178 case PAUSE: |
112 PauseDownloadInternal(download_guid); | 179 PauseDownloadInternal(download_guid); |
113 break; | 180 break; |
114 case CANCEL: | 181 case CANCEL: |
115 CancelDownloadInternal(download_guid, false); | 182 CancelDownloadInternal(download_guid, false); |
116 break; | 183 break; |
184 case INITIALIZE_UI: | |
185 GetAllDownloadsInternal(); | |
qinmin
2016/07/14 19:48:18
Do you need to check whether user has closed the d
gone
2016/07/14 20:22:58
Should be fine: the Adapter is owned by the Downlo
| |
186 break; | |
117 default: | 187 default: |
118 NOTREACHED(); | 188 NOTREACHED(); |
119 break; | 189 break; |
120 } | 190 } |
121 } | 191 } |
122 pending_actions_.clear(); | 192 pending_actions_.clear(); |
123 } | 193 } |
124 | 194 |
125 void DownloadManagerService::ResumeDownloadInternal( | 195 void DownloadManagerService::ResumeDownloadInternal( |
126 const std::string& download_guid) { | 196 const std::string& download_guid) { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 if (iter->second == PAUSE) | 251 if (iter->second == PAUSE) |
182 iter->second = action; | 252 iter->second = action; |
183 break; | 253 break; |
184 case PAUSE: | 254 case PAUSE: |
185 if (iter->second == RESUME) | 255 if (iter->second == RESUME) |
186 iter->second = action; | 256 iter->second = action; |
187 break; | 257 break; |
188 case CANCEL: | 258 case CANCEL: |
189 iter->second = action; | 259 iter->second = action; |
190 break; | 260 break; |
261 case INITIALIZE_UI: | |
262 iter->second = action; | |
263 break; | |
191 default: | 264 default: |
192 NOTREACHED(); | 265 NOTREACHED(); |
193 break; | 266 break; |
194 } | 267 } |
195 } | 268 } |
196 | 269 |
197 void DownloadManagerService::OnResumptionFailed( | 270 void DownloadManagerService::OnResumptionFailed( |
198 const std::string& download_guid) { | 271 const std::string& download_guid) { |
199 base::ThreadTaskRunnerHandle::Get()->PostTask( | 272 base::ThreadTaskRunnerHandle::Get()->PostTask( |
200 FROM_HERE, base::Bind(&DownloadManagerService::OnResumptionFailedInternal, | 273 FROM_HERE, base::Bind(&DownloadManagerService::OnResumptionFailedInternal, |
(...skipping 14 matching lines...) Expand all Loading... | |
215 resume_callback_for_testing_.Run(false); | 288 resume_callback_for_testing_.Run(false); |
216 } | 289 } |
217 | 290 |
218 content::DownloadManager* DownloadManagerService::GetDownloadManager( | 291 content::DownloadManager* DownloadManagerService::GetDownloadManager( |
219 bool is_off_the_record) { | 292 bool is_off_the_record) { |
220 Profile* profile = ProfileManager::GetActiveUserProfile(); | 293 Profile* profile = ProfileManager::GetActiveUserProfile(); |
221 if (is_off_the_record) | 294 if (is_off_the_record) |
222 profile = profile->GetOffTheRecordProfile(); | 295 profile = profile->GetOffTheRecordProfile(); |
223 return content::BrowserContext::GetDownloadManager(profile); | 296 return content::BrowserContext::GetDownloadManager(profile); |
224 } | 297 } |
OLD | NEW |