| 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/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "chrome/browser/download/download_service.h" | 10 #include "chrome/browser/download/download_service.h" |
| 11 #include "chrome/browser/download/download_service_factory.h" | 11 #include "chrome/browser/download/download_service_factory.h" |
| 12 #include "chrome/browser/profiles/profile_manager.h" | 12 #include "chrome/browser/profiles/profile_manager.h" |
| 13 #include "chrome/grit/generated_resources.h" | 13 #include "chrome/grit/generated_resources.h" |
| 14 #include "content/public/browser/android/download_controller_android.h" | 14 #include "content/public/browser/android/download_controller_android.h" |
| 15 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
| 16 #include "content/public/browser/download_item.h" | 16 #include "content/public/browser/download_item.h" |
| 17 #include "jni/DownloadManagerService_jni.h" | 17 #include "jni/DownloadManagerService_jni.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 19 | 19 |
| 20 using base::android::JavaParamRef; | 20 using base::android::JavaParamRef; |
| 21 using base::android::ConvertJavaStringToUTF8; | 21 using base::android::ConvertJavaStringToUTF8; |
| 22 using base::android::ConvertUTF8ToJavaString; | 22 using base::android::ConvertUTF8ToJavaString; |
| 23 using content::DownloadControllerAndroid; |
| 23 | 24 |
| 24 // static | 25 // static |
| 25 bool DownloadManagerService::RegisterDownloadManagerService(JNIEnv* env) { | 26 bool DownloadManagerService::RegisterDownloadManagerService(JNIEnv* env) { |
| 26 return RegisterNativesImpl(env); | 27 return RegisterNativesImpl(env); |
| 27 } | 28 } |
| 28 | 29 |
| 29 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& jobj) { | 30 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& jobj) { |
| 30 Profile* profile = ProfileManager::GetActiveUserProfile(); | 31 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 31 DownloadManagerService* service = | 32 DownloadManagerService* service = |
| 32 new DownloadManagerService(env, jobj); | 33 new DownloadManagerService(env, jobj); |
| 33 DownloadService* download_service = | 34 DownloadService* download_service = |
| 34 DownloadServiceFactory::GetForBrowserContext(profile); | 35 DownloadServiceFactory::GetForBrowserContext(profile); |
| 35 DownloadHistory* history = download_service->GetDownloadHistory(); | 36 DownloadHistory* history = download_service->GetDownloadHistory(); |
| 36 if (history) | 37 if (history) |
| 37 history->AddObserver(service); | 38 history->AddObserver(service); |
| 38 return reinterpret_cast<intptr_t>(service); | 39 return reinterpret_cast<intptr_t>(service); |
| 39 } | 40 } |
| 40 | 41 |
| 41 DownloadManagerService::DownloadManagerService( | 42 DownloadManagerService::DownloadManagerService( |
| 42 JNIEnv* env, | 43 JNIEnv* env, |
| 43 jobject obj) | 44 jobject obj) |
| 44 : java_ref_(env, obj), | 45 : java_ref_(env, obj), |
| 45 is_history_query_complete_(false) { | 46 is_history_query_complete_(false) { |
| 46 content::DownloadControllerAndroid::Get()->SetDefaultDownloadFileName( | 47 DownloadControllerAndroid::Get()->SetDefaultDownloadFileName( |
| 47 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); | 48 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); |
| 48 } | 49 } |
| 49 | 50 |
| 50 DownloadManagerService::~DownloadManagerService() {} | 51 DownloadManagerService::~DownloadManagerService() {} |
| 51 | 52 |
| 52 void DownloadManagerService::ResumeDownload( | 53 void DownloadManagerService::ResumeDownload( |
| 53 JNIEnv* env, | 54 JNIEnv* env, |
| 54 jobject obj, | 55 jobject obj, |
| 55 const JavaParamRef<jstring>& jdownload_guid) { | 56 const JavaParamRef<jstring>& jdownload_guid) { |
| 56 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); | 57 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 68 if (is_history_query_complete_) | 69 if (is_history_query_complete_) |
| 69 PauseDownloadInternal(download_guid); | 70 PauseDownloadInternal(download_guid); |
| 70 else | 71 else |
| 71 EnqueueDownloadAction(download_guid, PAUSE); | 72 EnqueueDownloadAction(download_guid, PAUSE); |
| 72 } | 73 } |
| 73 | 74 |
| 74 void DownloadManagerService::CancelDownload( | 75 void DownloadManagerService::CancelDownload( |
| 75 JNIEnv* env, | 76 JNIEnv* env, |
| 76 jobject obj, | 77 jobject obj, |
| 77 const JavaParamRef<jstring>& jdownload_guid, | 78 const JavaParamRef<jstring>& jdownload_guid, |
| 78 bool is_off_the_record) { | 79 bool is_off_the_record, |
| 80 bool is_notification_dismissed) { |
| 79 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); | 81 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
| 82 |
| 83 DownloadControllerAndroid::RecordDownloadCancelReason( |
| 84 is_notification_dismissed ? |
| 85 DownloadControllerAndroid::CANCEL_REASON_NOTIFICATION_DISMISSED : |
| 86 DownloadControllerAndroid::CANCEL_REASON_ACTION_BUTTON); |
| 80 // Incognito download can only be cancelled in the same browser session, no | 87 // Incognito download can only be cancelled in the same browser session, no |
| 81 // need to wait for download history. | 88 // need to wait for download history. |
| 82 if (is_off_the_record) { | 89 if (is_off_the_record) { |
| 83 CancelDownloadInternal(download_guid, true); | 90 CancelDownloadInternal(download_guid, true); |
| 84 return; | 91 return; |
| 85 } | 92 } |
| 86 | 93 |
| 87 if (is_history_query_complete_) | 94 if (is_history_query_complete_) |
| 88 CancelDownloadInternal(download_guid, false); | 95 CancelDownloadInternal(download_guid, false); |
| 89 else | 96 else |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 NOTREACHED(); | 191 NOTREACHED(); |
| 185 break; | 192 break; |
| 186 } | 193 } |
| 187 } | 194 } |
| 188 | 195 |
| 189 void DownloadManagerService::OnResumptionFailed( | 196 void DownloadManagerService::OnResumptionFailed( |
| 190 const std::string& download_guid) { | 197 const std::string& download_guid) { |
| 191 base::MessageLoop::current()->PostTask( | 198 base::MessageLoop::current()->PostTask( |
| 192 FROM_HERE, base::Bind(&DownloadManagerService::OnResumptionFailedInternal, | 199 FROM_HERE, base::Bind(&DownloadManagerService::OnResumptionFailedInternal, |
| 193 base::Unretained(this), download_guid)); | 200 base::Unretained(this), download_guid)); |
| 201 DownloadControllerAndroid::RecordDownloadCancelReason( |
| 202 DownloadControllerAndroid::CANCEL_REASON_NOT_CANCELED); |
| 194 } | 203 } |
| 195 | 204 |
| 196 void DownloadManagerService::OnResumptionFailedInternal( | 205 void DownloadManagerService::OnResumptionFailedInternal( |
| 197 const std::string& download_guid) { | 206 const std::string& download_guid) { |
| 198 if (!java_ref_.is_null()) { | 207 if (!java_ref_.is_null()) { |
| 199 JNIEnv* env = base::android::AttachCurrentThread(); | 208 JNIEnv* env = base::android::AttachCurrentThread(); |
| 200 Java_DownloadManagerService_onResumptionFailed( | 209 Java_DownloadManagerService_onResumptionFailed( |
| 201 env, java_ref_.obj(), | 210 env, java_ref_.obj(), |
| 202 ConvertUTF8ToJavaString(env, download_guid).obj()); | 211 ConvertUTF8ToJavaString(env, download_guid).obj()); |
| 203 } | 212 } |
| 204 if (!resume_callback_for_testing_.is_null()) | 213 if (!resume_callback_for_testing_.is_null()) |
| 205 resume_callback_for_testing_.Run(false); | 214 resume_callback_for_testing_.Run(false); |
| 206 } | 215 } |
| 207 | 216 |
| 208 content::DownloadManager* DownloadManagerService::GetDownloadManager( | 217 content::DownloadManager* DownloadManagerService::GetDownloadManager( |
| 209 bool is_off_the_record) { | 218 bool is_off_the_record) { |
| 210 Profile* profile = ProfileManager::GetActiveUserProfile(); | 219 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 211 if (is_off_the_record) | 220 if (is_off_the_record) |
| 212 profile = profile->GetOffTheRecordProfile(); | 221 profile = profile->GetOffTheRecordProfile(); |
| 213 return content::BrowserContext::GetDownloadManager(profile); | 222 return content::BrowserContext::GetDownloadManager(profile); |
| 214 } | 223 } |
| OLD | NEW |