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, | |
148 java_ref_.obj(), | |
149 ConvertUTF8ToJavaString(env, item->GetGuid()).obj(), | |
150 ConvertUTF8ToJavaString( | |
151 env, item->GetFileNameToReportUser().value()).obj(), | |
152 ConvertUTF8ToJavaString( | |
153 env, item->GetTargetFilePath().value()).obj(), | |
154 ConvertUTF8ToJavaString(env, item->GetTabUrl().spec()).obj(), | |
155 ConvertUTF8ToJavaString(env, item->GetMimeType()).obj(), | |
156 item->GetStartTime().ToJavaTime(), | |
157 item->GetTotalBytes()); | |
158 break; | |
159 } | |
160 } | |
161 | |
162 void DownloadManagerService::GetAllDownloadsInternal() { | |
163 content::DownloadManager* manager = GetDownloadManager(false); | |
164 if (java_ref_.is_null() || !manager) | |
165 return; | |
166 | |
167 content::DownloadManager::DownloadVector all_items; | 133 content::DownloadManager::DownloadVector all_items; |
168 manager->GetAllDownloads(&all_items); | 134 manager->GetAllDownloads(&all_items); |
169 | 135 |
170 // Create a Java array of all of the visible DownloadItems. | 136 // Create a Java array of all of the visible DownloadItems. |
171 JNIEnv* env = base::android::AttachCurrentThread(); | 137 JNIEnv* env = base::android::AttachCurrentThread(); |
172 ScopedJavaLocalRef<jobject> j_download_item_list = | 138 ScopedJavaLocalRef<jobject> j_download_item_list = |
173 Java_DownloadManagerService_createDownloadItemList(env, java_ref_.obj()); | 139 Java_DownloadManagerService_createDownloadItemList(env, java_ref_.obj()); |
174 | 140 |
175 for (size_t i = 0; i < all_items.size(); i++) { | 141 for (size_t i = 0; i < all_items.size(); i++) { |
176 content::DownloadItem* item = all_items[i]; | 142 content::DownloadItem* item = all_items[i]; |
177 if (!ShouldShowDownloadItem(item)) | 143 if (!ShouldShowDownloadItem(item)) |
178 continue; | 144 continue; |
179 | 145 |
180 Java_DownloadManagerService_addDownloadItemToList( | 146 Java_DownloadManagerService_addDownloadItemToList( |
181 env, | 147 env, |
182 java_ref_.obj(), | 148 java_ref_.obj(), |
183 j_download_item_list.obj(), | 149 j_download_item_list.obj(), |
184 ConvertUTF8ToJavaString(env, item->GetGuid()).obj(), | 150 ConvertUTF8ToJavaString(env, item->GetGuid()).obj(), |
185 ConvertUTF8ToJavaString( | 151 ConvertUTF8ToJavaString( |
186 env, item->GetFileNameToReportUser().value()).obj(), | 152 env, item->GetFileNameToReportUser().value()).obj(), |
187 ConvertUTF8ToJavaString( | 153 ConvertUTF8ToJavaString( |
188 env, item->GetTargetFilePath().value()).obj(), | 154 env, item->GetTargetFilePath().value()).obj(), |
189 ConvertUTF8ToJavaString(env, item->GetTabUrl().spec()).obj(), | 155 ConvertUTF8ToJavaString(env, item->GetTabUrl().spec()).obj(), |
190 ConvertUTF8ToJavaString(env, item->GetMimeType()).obj(), | 156 ConvertUTF8ToJavaString(env, item->GetMimeType()).obj(), |
191 item->GetStartTime().ToJavaTime(), | 157 item->GetStartTime().ToJavaTime(), |
192 item->GetTotalBytes()); | 158 item->GetTotalBytes()); |
193 } | 159 } |
194 | 160 |
195 Java_DownloadManagerService_onAllDownloadsRetrieved( | 161 Java_DownloadManagerService_onAllDownloadsRetrieved( |
196 env, java_ref_.obj(), j_download_item_list.obj()); | 162 env, java_ref_.obj(), j_download_item_list.obj(), is_off_the_record); |
197 } | 163 } |
198 | 164 |
199 | 165 |
200 void DownloadManagerService::CancelDownload( | 166 void DownloadManagerService::CancelDownload( |
201 JNIEnv* env, | 167 JNIEnv* env, |
202 jobject obj, | 168 jobject obj, |
203 const JavaParamRef<jstring>& jdownload_guid, | 169 const JavaParamRef<jstring>& jdownload_guid, |
204 bool is_off_the_record, | 170 bool is_off_the_record, |
205 bool is_notification_dismissed) { | 171 bool is_notification_dismissed) { |
206 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); | 172 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
(...skipping 17 matching lines...) Expand all Loading... | |
224 case RESUME: | 190 case RESUME: |
225 ResumeDownloadInternal(download_guid, false); | 191 ResumeDownloadInternal(download_guid, false); |
226 break; | 192 break; |
227 case PAUSE: | 193 case PAUSE: |
228 PauseDownloadInternal(download_guid, false); | 194 PauseDownloadInternal(download_guid, false); |
229 break; | 195 break; |
230 case CANCEL: | 196 case CANCEL: |
231 CancelDownloadInternal(download_guid, false); | 197 CancelDownloadInternal(download_guid, false); |
232 break; | 198 break; |
233 case INITIALIZE_UI: | 199 case INITIALIZE_UI: |
234 GetAllDownloadsInternal(); | 200 GetAllDownloadsInternal(false); |
201 break; | |
202 case INITIALIZE_OFF_THE_RECORD_UI: | |
203 GetAllDownloadsInternal(true); | |
235 break; | 204 break; |
236 default: | 205 default: |
237 NOTREACHED(); | 206 NOTREACHED(); |
238 break; | 207 break; |
239 } | 208 } |
240 } | 209 } |
241 pending_actions_.clear(); | 210 pending_actions_.clear(); |
242 | 211 |
243 // Monitor all DownloadItems for changes. | 212 // Monitor all DownloadItems for changes. |
244 content::DownloadManager* manager = GetDownloadManager(false); | 213 content::DownloadManager* manager = GetDownloadManager(false); |
245 if (manager) | 214 if (manager) |
246 original_notifier_.reset(new AllDownloadItemNotifier(manager, this)); | 215 original_notifier_.reset(new AllDownloadItemNotifier(manager, this)); |
216 | |
217 content::DownloadManager* off_the_record_manager = GetDownloadManager(true); | |
218 if (off_the_record_manager) { | |
219 off_the_record_notifier_.reset( | |
220 new AllDownloadItemNotifier(off_the_record_manager, this)); | |
221 } | |
222 } | |
223 | |
224 void DownloadManagerService::OnDownloadUpdated( | |
225 content::DownloadManager* manager, content::DownloadItem* item) { | |
226 // Ignore anything that isn't a completed download notification. | |
227 if (!item->IsDone() || java_ref_.is_null()) | |
228 return; | |
229 | |
230 bool is_off_the_record = false; | |
231 content::DownloadManager* off_the_record_manager = GetDownloadManager(true); | |
qinmin
2016/08/16 16:53:14
Can we do this by checking the DownloadItem's Brow
gone
2016/08/16 18:24:25
Didn't work before, but seems to now. Weird, but
| |
232 if (off_the_record_manager && manager == off_the_record_manager) | |
233 is_off_the_record = true; | |
234 | |
235 JNIEnv* env = base::android::AttachCurrentThread(); | |
236 Java_DownloadManagerService_onDownloadItemUpdated( | |
237 env, | |
238 java_ref_.obj(), | |
239 ConvertUTF8ToJavaString(env, item->GetGuid()).obj(), | |
240 ConvertUTF8ToJavaString( | |
241 env, item->GetFileNameToReportUser().value()).obj(), | |
242 ConvertUTF8ToJavaString( | |
243 env, item->GetTargetFilePath().value()).obj(), | |
244 ConvertUTF8ToJavaString(env, item->GetTabUrl().spec()).obj(), | |
245 ConvertUTF8ToJavaString(env, item->GetMimeType()).obj(), | |
246 item->GetStartTime().ToJavaTime(), | |
247 item->GetTotalBytes(), | |
248 is_off_the_record); | |
247 } | 249 } |
248 | 250 |
249 void DownloadManagerService::OnDownloadRemoved( | 251 void DownloadManagerService::OnDownloadRemoved( |
250 content::DownloadManager* manager, content::DownloadItem* item) { | 252 content::DownloadManager* manager, content::DownloadItem* item) { |
253 bool is_off_the_record = false; | |
254 content::DownloadManager* off_the_record_manager = GetDownloadManager(true); | |
qinmin
2016/08/16 16:53:14
ditto
gone
2016/08/16 18:24:25
Done.
| |
255 if (off_the_record_manager && manager == off_the_record_manager) | |
256 is_off_the_record = true; | |
257 | |
251 JNIEnv* env = base::android::AttachCurrentThread(); | 258 JNIEnv* env = base::android::AttachCurrentThread(); |
252 Java_DownloadManagerService_onDownloadItemRemoved( | 259 Java_DownloadManagerService_onDownloadItemRemoved( |
253 env, | 260 env, |
254 java_ref_.obj(), | 261 java_ref_.obj(), |
255 ConvertUTF8ToJavaString(env, item->GetGuid()).obj()); | 262 ConvertUTF8ToJavaString(env, item->GetGuid()).obj(), |
263 is_off_the_record); | |
256 } | 264 } |
257 | 265 |
258 void DownloadManagerService::ResumeDownloadInternal( | 266 void DownloadManagerService::ResumeDownloadInternal( |
259 const std::string& download_guid, bool is_off_the_record) { | 267 const std::string& download_guid, bool is_off_the_record) { |
260 content::DownloadManager* manager = GetDownloadManager(is_off_the_record); | 268 content::DownloadManager* manager = GetDownloadManager(is_off_the_record); |
261 if (!manager) { | 269 if (!manager) { |
262 OnResumptionFailed(download_guid); | 270 OnResumptionFailed(download_guid); |
263 return; | 271 return; |
264 } | 272 } |
265 content::DownloadItem* item = manager->GetDownloadByGuid(download_guid); | 273 content::DownloadItem* item = manager->GetDownloadByGuid(download_guid); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
328 if (iter->second == RESUME) | 336 if (iter->second == RESUME) |
329 iter->second = action; | 337 iter->second = action; |
330 break; | 338 break; |
331 case CANCEL: | 339 case CANCEL: |
332 iter->second = action; | 340 iter->second = action; |
333 break; | 341 break; |
334 case REMOVE: | 342 case REMOVE: |
335 iter->second = action; | 343 iter->second = action; |
336 break; | 344 break; |
337 case INITIALIZE_UI: | 345 case INITIALIZE_UI: |
346 case INITIALIZE_OFF_THE_RECORD_UI: | |
338 iter->second = action; | 347 iter->second = action; |
339 break; | 348 break; |
340 default: | 349 default: |
341 NOTREACHED(); | 350 NOTREACHED(); |
342 break; | 351 break; |
343 } | 352 } |
344 } | 353 } |
345 | 354 |
346 void DownloadManagerService::OnResumptionFailed( | 355 void DownloadManagerService::OnResumptionFailed( |
347 const std::string& download_guid) { | 356 const std::string& download_guid) { |
(...skipping 16 matching lines...) Expand all Loading... | |
364 resume_callback_for_testing_.Run(false); | 373 resume_callback_for_testing_.Run(false); |
365 } | 374 } |
366 | 375 |
367 content::DownloadManager* DownloadManagerService::GetDownloadManager( | 376 content::DownloadManager* DownloadManagerService::GetDownloadManager( |
368 bool is_off_the_record) { | 377 bool is_off_the_record) { |
369 Profile* profile = ProfileManager::GetActiveUserProfile(); | 378 Profile* profile = ProfileManager::GetActiveUserProfile(); |
370 if (is_off_the_record) | 379 if (is_off_the_record) |
371 profile = profile->GetOffTheRecordProfile(); | 380 profile = profile->GetOffTheRecordProfile(); |
372 return content::BrowserContext::GetDownloadManager(profile); | 381 return content::BrowserContext::GetDownloadManager(profile); |
373 } | 382 } |
OLD | NEW |