| 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/download/download_service.h" | 13 #include "chrome/browser/download/download_service.h" |
| 13 #include "chrome/browser/download/download_service_factory.h" | 14 #include "chrome/browser/download/download_service_factory.h" |
| 14 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
| 15 #include "chrome/grit/generated_resources.h" | 16 #include "chrome/grit/generated_resources.h" |
| 16 #include "content/public/browser/android/download_controller_android.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 using content::DownloadControllerAndroid; | |
| 26 | 25 |
| 27 // static | 26 // static |
| 28 bool DownloadManagerService::RegisterDownloadManagerService(JNIEnv* env) { | 27 bool DownloadManagerService::RegisterDownloadManagerService(JNIEnv* env) { |
| 29 return RegisterNativesImpl(env); | 28 return RegisterNativesImpl(env); |
| 30 } | 29 } |
| 31 | 30 |
| 32 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& jobj) { | 31 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& jobj) { |
| 33 Profile* profile = ProfileManager::GetActiveUserProfile(); | 32 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 34 DownloadManagerService* service = | 33 DownloadManagerService* service = |
| 35 new DownloadManagerService(env, jobj); | 34 new DownloadManagerService(env, jobj); |
| 36 DownloadService* download_service = | 35 DownloadService* download_service = |
| 37 DownloadServiceFactory::GetForBrowserContext(profile); | 36 DownloadServiceFactory::GetForBrowserContext(profile); |
| 38 DownloadHistory* history = download_service->GetDownloadHistory(); | 37 DownloadHistory* history = download_service->GetDownloadHistory(); |
| 39 if (history) | 38 if (history) |
| 40 history->AddObserver(service); | 39 history->AddObserver(service); |
| 41 return reinterpret_cast<intptr_t>(service); | 40 return reinterpret_cast<intptr_t>(service); |
| 42 } | 41 } |
| 43 | 42 |
| 44 DownloadManagerService::DownloadManagerService( | 43 DownloadManagerService::DownloadManagerService( |
| 45 JNIEnv* env, | 44 JNIEnv* env, |
| 46 jobject obj) | 45 jobject obj) |
| 47 : java_ref_(env, obj), | 46 : java_ref_(env, obj), |
| 48 is_history_query_complete_(false) { | 47 is_history_query_complete_(false) { |
| 49 DownloadControllerAndroid::Get()->SetDefaultDownloadFileName( | 48 DownloadControllerBase::Get()->SetDefaultDownloadFileName( |
| 50 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); | 49 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); |
| 51 } | 50 } |
| 52 | 51 |
| 53 DownloadManagerService::~DownloadManagerService() {} | 52 DownloadManagerService::~DownloadManagerService() {} |
| 54 | 53 |
| 55 void DownloadManagerService::ResumeDownload( | 54 void DownloadManagerService::ResumeDownload( |
| 56 JNIEnv* env, | 55 JNIEnv* env, |
| 57 jobject obj, | 56 jobject obj, |
| 58 const JavaParamRef<jstring>& jdownload_guid) { | 57 const JavaParamRef<jstring>& jdownload_guid) { |
| 59 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); | 58 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 75 } | 74 } |
| 76 | 75 |
| 77 void DownloadManagerService::CancelDownload( | 76 void DownloadManagerService::CancelDownload( |
| 78 JNIEnv* env, | 77 JNIEnv* env, |
| 79 jobject obj, | 78 jobject obj, |
| 80 const JavaParamRef<jstring>& jdownload_guid, | 79 const JavaParamRef<jstring>& jdownload_guid, |
| 81 bool is_off_the_record, | 80 bool is_off_the_record, |
| 82 bool is_notification_dismissed) { | 81 bool is_notification_dismissed) { |
| 83 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); | 82 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
| 84 | 83 |
| 85 DownloadControllerAndroid::RecordDownloadCancelReason( | 84 DownloadController::RecordDownloadCancelReason( |
| 86 is_notification_dismissed ? | 85 is_notification_dismissed ? |
| 87 DownloadControllerAndroid::CANCEL_REASON_NOTIFICATION_DISMISSED : | 86 DownloadController::CANCEL_REASON_NOTIFICATION_DISMISSED : |
| 88 DownloadControllerAndroid::CANCEL_REASON_ACTION_BUTTON); | 87 DownloadController::CANCEL_REASON_ACTION_BUTTON); |
| 89 // Incognito download can only be cancelled in the same browser session, no | 88 // Incognito download can only be cancelled in the same browser session, no |
| 90 // need to wait for download history. | 89 // need to wait for download history. |
| 91 if (is_off_the_record) { | 90 if (is_off_the_record) { |
| 92 CancelDownloadInternal(download_guid, true); | 91 CancelDownloadInternal(download_guid, true); |
| 93 return; | 92 return; |
| 94 } | 93 } |
| 95 | 94 |
| 96 if (is_history_query_complete_) | 95 if (is_history_query_complete_) |
| 97 CancelDownloadInternal(download_guid, false); | 96 CancelDownloadInternal(download_guid, false); |
| 98 else | 97 else |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 } | 131 } |
| 133 content::DownloadItem* item = manager->GetDownloadByGuid(download_guid); | 132 content::DownloadItem* item = manager->GetDownloadByGuid(download_guid); |
| 134 if (!item) { | 133 if (!item) { |
| 135 OnResumptionFailed(download_guid); | 134 OnResumptionFailed(download_guid); |
| 136 return; | 135 return; |
| 137 } | 136 } |
| 138 if (!item->CanResume()) { | 137 if (!item->CanResume()) { |
| 139 OnResumptionFailed(download_guid); | 138 OnResumptionFailed(download_guid); |
| 140 return; | 139 return; |
| 141 } | 140 } |
| 142 item->AddObserver(content::DownloadControllerAndroid::Get()); | 141 item->AddObserver(DownloadControllerBase::Get()); |
| 143 item->Resume(); | 142 item->Resume(); |
| 144 if (!resume_callback_for_testing_.is_null()) | 143 if (!resume_callback_for_testing_.is_null()) |
| 145 resume_callback_for_testing_.Run(true); | 144 resume_callback_for_testing_.Run(true); |
| 146 } | 145 } |
| 147 | 146 |
| 148 void DownloadManagerService::CancelDownloadInternal( | 147 void DownloadManagerService::CancelDownloadInternal( |
| 149 const std::string& download_guid, bool is_off_the_record) { | 148 const std::string& download_guid, bool is_off_the_record) { |
| 150 content::DownloadManager* manager = GetDownloadManager(is_off_the_record); | 149 content::DownloadManager* manager = GetDownloadManager(is_off_the_record); |
| 151 if (!manager) | 150 if (!manager) |
| 152 return; | 151 return; |
| 153 content::DownloadItem* item = manager->GetDownloadByGuid(download_guid); | 152 content::DownloadItem* item = manager->GetDownloadByGuid(download_guid); |
| 154 if (item) { | 153 if (item) { |
| 155 item->Cancel(true); | 154 item->Cancel(true); |
| 156 item->RemoveObserver(content::DownloadControllerAndroid::Get()); | 155 item->RemoveObserver(DownloadControllerBase::Get()); |
| 157 } | 156 } |
| 158 } | 157 } |
| 159 | 158 |
| 160 void DownloadManagerService::PauseDownloadInternal( | 159 void DownloadManagerService::PauseDownloadInternal( |
| 161 const std::string& download_guid) { | 160 const std::string& download_guid) { |
| 162 content::DownloadManager* manager = GetDownloadManager(false); | 161 content::DownloadManager* manager = GetDownloadManager(false); |
| 163 if (!manager) | 162 if (!manager) |
| 164 return; | 163 return; |
| 165 content::DownloadItem* item = manager->GetDownloadByGuid(download_guid); | 164 content::DownloadItem* item = manager->GetDownloadByGuid(download_guid); |
| 166 if (item) { | 165 if (item) { |
| 167 item->Pause(); | 166 item->Pause(); |
| 168 item->RemoveObserver(content::DownloadControllerAndroid::Get()); | 167 item->RemoveObserver(DownloadControllerBase::Get()); |
| 169 } | 168 } |
| 170 } | 169 } |
| 171 | 170 |
| 172 void DownloadManagerService::EnqueueDownloadAction( | 171 void DownloadManagerService::EnqueueDownloadAction( |
| 173 const std::string& download_guid, | 172 const std::string& download_guid, |
| 174 DownloadAction action) { | 173 DownloadAction action) { |
| 175 auto iter = pending_actions_.find(download_guid); | 174 auto iter = pending_actions_.find(download_guid); |
| 176 if (iter == pending_actions_.end()) { | 175 if (iter == pending_actions_.end()) { |
| 177 pending_actions_[download_guid] = action; | 176 pending_actions_[download_guid] = action; |
| 178 return; | 177 return; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 193 NOTREACHED(); | 192 NOTREACHED(); |
| 194 break; | 193 break; |
| 195 } | 194 } |
| 196 } | 195 } |
| 197 | 196 |
| 198 void DownloadManagerService::OnResumptionFailed( | 197 void DownloadManagerService::OnResumptionFailed( |
| 199 const std::string& download_guid) { | 198 const std::string& download_guid) { |
| 200 base::ThreadTaskRunnerHandle::Get()->PostTask( | 199 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 201 FROM_HERE, base::Bind(&DownloadManagerService::OnResumptionFailedInternal, | 200 FROM_HERE, base::Bind(&DownloadManagerService::OnResumptionFailedInternal, |
| 202 base::Unretained(this), download_guid)); | 201 base::Unretained(this), download_guid)); |
| 203 DownloadControllerAndroid::RecordDownloadCancelReason( | 202 DownloadController::RecordDownloadCancelReason( |
| 204 DownloadControllerAndroid::CANCEL_REASON_NOT_CANCELED); | 203 DownloadController::CANCEL_REASON_NOT_CANCELED); |
| 205 } | 204 } |
| 206 | 205 |
| 207 void DownloadManagerService::OnResumptionFailedInternal( | 206 void DownloadManagerService::OnResumptionFailedInternal( |
| 208 const std::string& download_guid) { | 207 const std::string& download_guid) { |
| 209 if (!java_ref_.is_null()) { | 208 if (!java_ref_.is_null()) { |
| 210 JNIEnv* env = base::android::AttachCurrentThread(); | 209 JNIEnv* env = base::android::AttachCurrentThread(); |
| 211 Java_DownloadManagerService_onResumptionFailed( | 210 Java_DownloadManagerService_onResumptionFailed( |
| 212 env, java_ref_.obj(), | 211 env, java_ref_.obj(), |
| 213 ConvertUTF8ToJavaString(env, download_guid).obj()); | 212 ConvertUTF8ToJavaString(env, download_guid).obj()); |
| 214 } | 213 } |
| 215 if (!resume_callback_for_testing_.is_null()) | 214 if (!resume_callback_for_testing_.is_null()) |
| 216 resume_callback_for_testing_.Run(false); | 215 resume_callback_for_testing_.Run(false); |
| 217 } | 216 } |
| 218 | 217 |
| 219 content::DownloadManager* DownloadManagerService::GetDownloadManager( | 218 content::DownloadManager* DownloadManagerService::GetDownloadManager( |
| 220 bool is_off_the_record) { | 219 bool is_off_the_record) { |
| 221 Profile* profile = ProfileManager::GetActiveUserProfile(); | 220 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 222 if (is_off_the_record) | 221 if (is_off_the_record) |
| 223 profile = profile->GetOffTheRecordProfile(); | 222 profile = profile->GetOffTheRecordProfile(); |
| 224 return content::BrowserContext::GetDownloadManager(profile); | 223 return content::BrowserContext::GetDownloadManager(profile); |
| 225 } | 224 } |
| OLD | NEW |