Chromium Code Reviews| 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 using DownloadVector = content::DownloadManager::DownloadVector; | |
| 27 | |
| 26 // static | 28 // static |
| 27 bool DownloadManagerService::RegisterDownloadManagerService(JNIEnv* env) { | 29 bool DownloadManagerService::RegisterDownloadManagerService(JNIEnv* env) { |
| 28 return RegisterNativesImpl(env); | 30 return RegisterNativesImpl(env); |
| 29 } | 31 } |
| 30 | 32 |
| 31 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& jobj) { | 33 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& jobj) { |
| 32 Profile* profile = ProfileManager::GetActiveUserProfile(); | 34 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 33 DownloadManagerService* service = | 35 DownloadManagerService* service = |
| 34 new DownloadManagerService(env, jobj); | 36 new DownloadManagerService(env, jobj); |
| 35 DownloadService* download_service = | 37 DownloadService* download_service = |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 66 JNIEnv* env, | 68 JNIEnv* env, |
| 67 jobject obj, | 69 jobject obj, |
| 68 const JavaParamRef<jstring>& jdownload_guid) { | 70 const JavaParamRef<jstring>& jdownload_guid) { |
| 69 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); | 71 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
| 70 if (is_history_query_complete_) | 72 if (is_history_query_complete_) |
| 71 PauseDownloadInternal(download_guid); | 73 PauseDownloadInternal(download_guid); |
| 72 else | 74 else |
| 73 EnqueueDownloadAction(download_guid, PAUSE); | 75 EnqueueDownloadAction(download_guid, PAUSE); |
| 74 } | 76 } |
| 75 | 77 |
| 78 void DownloadManagerService::GetAllDownloads(JNIEnv* env, | |
| 79 const JavaParamRef<jobject>& obj) { | |
| 80 if (is_history_query_complete_) | |
| 81 GetAllDownloadsInternal(); | |
| 82 else | |
| 83 EnqueueDownloadAction(std::string(), INITIALIZE_UI); | |
| 84 } | |
| 85 | |
| 86 void DownloadManagerService::GetAllDownloadsInternal() { | |
| 87 content::DownloadManager* manager = GetDownloadManager(false); | |
| 88 if (java_ref_.is_null() || !manager) | |
| 89 return; | |
| 90 | |
| 91 DownloadVector all_items; | |
| 92 manager->GetAllDownloads(&all_items); | |
| 93 | |
| 94 // Create a list in Java-land and then add information about all the | |
| 95 // downloads to it. | |
| 96 JNIEnv* env = base::android::AttachCurrentThread(); | |
|
qinmin
2016/07/14 17:08:05
Since we always call onAllDownloadsRetrieved, can
gone
2016/07/14 17:11:08
This is actually what the DownloadsBridge does, so
qinmin
2016/07/14 18:06:54
You can call a jni call to pack the fields into a
Ian Wen
2016/07/14 18:16:26
Aha! I found a trick that BookmarkBridge did to si
gone
2016/07/14 19:11:56
Changed it around to create the jobjectarray like
| |
| 97 ScopedJavaLocalRef<jobject> jdownload_item_list = | |
| 98 Java_DownloadManagerService_createDownloadItemList( | |
| 99 env, java_ref_.obj()); | |
| 100 | |
| 101 for (size_t i = 0; i < all_items.size(); i++) { | |
| 102 content::DownloadItem* item = all_items[i]; | |
| 103 if (item->IsTemporary() || | |
| 104 item->GetFileNameToReportUser().empty() || | |
| 105 item->GetTargetFilePath().empty()) { | |
| 106 continue; | |
| 107 } | |
| 108 | |
| 109 Java_DownloadManagerService_addItemToDownloadItemList( | |
| 110 env, | |
| 111 java_ref_.obj(), | |
| 112 jdownload_item_list.obj(), | |
| 113 ConvertUTF8ToJavaString(env, item->GetGuid()).obj(), | |
| 114 ConvertUTF8ToJavaString( | |
| 115 env, | |
| 116 item->GetFileNameToReportUser().BaseName().value()).obj(), | |
| 117 ConvertUTF8ToJavaString(env, item->GetTabUrl().spec()).obj(), | |
| 118 ConvertUTF8ToJavaString(env, item->GetMimeType()).obj(), | |
| 119 item->GetStartTime().ToJavaTime(), | |
| 120 item->GetTotalBytes()); | |
| 121 } | |
| 122 | |
| 123 Java_DownloadManagerService_onAllDownloadsRetrieved( | |
| 124 env, java_ref_.obj(), jdownload_item_list.obj()); | |
| 125 } | |
| 126 | |
| 127 | |
| 76 void DownloadManagerService::CancelDownload( | 128 void DownloadManagerService::CancelDownload( |
| 77 JNIEnv* env, | 129 JNIEnv* env, |
| 78 jobject obj, | 130 jobject obj, |
| 79 const JavaParamRef<jstring>& jdownload_guid, | 131 const JavaParamRef<jstring>& jdownload_guid, |
| 80 bool is_off_the_record, | 132 bool is_off_the_record, |
| 81 bool is_notification_dismissed) { | 133 bool is_notification_dismissed) { |
| 82 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); | 134 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
| 83 | 135 |
| 84 DownloadController::RecordDownloadCancelReason( | 136 DownloadController::RecordDownloadCancelReason( |
| 85 is_notification_dismissed ? | 137 is_notification_dismissed ? |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 107 switch (action) { | 159 switch (action) { |
| 108 case RESUME: | 160 case RESUME: |
| 109 ResumeDownloadInternal(download_guid); | 161 ResumeDownloadInternal(download_guid); |
| 110 break; | 162 break; |
| 111 case PAUSE: | 163 case PAUSE: |
| 112 PauseDownloadInternal(download_guid); | 164 PauseDownloadInternal(download_guid); |
| 113 break; | 165 break; |
| 114 case CANCEL: | 166 case CANCEL: |
| 115 CancelDownloadInternal(download_guid, false); | 167 CancelDownloadInternal(download_guid, false); |
| 116 break; | 168 break; |
| 169 case INITIALIZE_UI: | |
| 170 GetAllDownloadsInternal(); | |
| 171 break; | |
| 117 default: | 172 default: |
| 118 NOTREACHED(); | 173 NOTREACHED(); |
| 119 break; | 174 break; |
| 120 } | 175 } |
| 121 } | 176 } |
| 122 pending_actions_.clear(); | 177 pending_actions_.clear(); |
| 123 } | 178 } |
| 124 | 179 |
| 125 void DownloadManagerService::ResumeDownloadInternal( | 180 void DownloadManagerService::ResumeDownloadInternal( |
| 126 const std::string& download_guid) { | 181 const std::string& download_guid) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 181 if (iter->second == PAUSE) | 236 if (iter->second == PAUSE) |
| 182 iter->second = action; | 237 iter->second = action; |
| 183 break; | 238 break; |
| 184 case PAUSE: | 239 case PAUSE: |
| 185 if (iter->second == RESUME) | 240 if (iter->second == RESUME) |
| 186 iter->second = action; | 241 iter->second = action; |
| 187 break; | 242 break; |
| 188 case CANCEL: | 243 case CANCEL: |
| 189 iter->second = action; | 244 iter->second = action; |
| 190 break; | 245 break; |
| 246 case INITIALIZE_UI: | |
| 247 iter->second = action; | |
| 248 break; | |
| 191 default: | 249 default: |
| 192 NOTREACHED(); | 250 NOTREACHED(); |
| 193 break; | 251 break; |
| 194 } | 252 } |
| 195 } | 253 } |
| 196 | 254 |
| 197 void DownloadManagerService::OnResumptionFailed( | 255 void DownloadManagerService::OnResumptionFailed( |
| 198 const std::string& download_guid) { | 256 const std::string& download_guid) { |
| 199 base::ThreadTaskRunnerHandle::Get()->PostTask( | 257 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 200 FROM_HERE, base::Bind(&DownloadManagerService::OnResumptionFailedInternal, | 258 FROM_HERE, base::Bind(&DownloadManagerService::OnResumptionFailedInternal, |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 215 resume_callback_for_testing_.Run(false); | 273 resume_callback_for_testing_.Run(false); |
| 216 } | 274 } |
| 217 | 275 |
| 218 content::DownloadManager* DownloadManagerService::GetDownloadManager( | 276 content::DownloadManager* DownloadManagerService::GetDownloadManager( |
| 219 bool is_off_the_record) { | 277 bool is_off_the_record) { |
| 220 Profile* profile = ProfileManager::GetActiveUserProfile(); | 278 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 221 if (is_off_the_record) | 279 if (is_off_the_record) |
| 222 profile = profile->GetOffTheRecordProfile(); | 280 profile = profile->GetOffTheRecordProfile(); |
| 223 return content::BrowserContext::GetDownloadManager(profile); | 281 return content::BrowserContext::GetDownloadManager(profile); |
| 224 } | 282 } |
| OLD | NEW |