| 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/profiles/profile_manager.h" | 10 #include "chrome/browser/profiles/profile_manager.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 content::DownloadControllerAndroid::Get()->SetDefaultDownloadFileName( | 50 content::DownloadControllerAndroid::Get()->SetDefaultDownloadFileName( |
| 51 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); | 51 l10n_util::GetStringUTF8(IDS_DEFAULT_DOWNLOAD_FILENAME)); |
| 52 manager_->AddObserver(this); | 52 manager_->AddObserver(this); |
| 53 } | 53 } |
| 54 | 54 |
| 55 DownloadManagerService::~DownloadManagerService() { | 55 DownloadManagerService::~DownloadManagerService() { |
| 56 if (manager_) | 56 if (manager_) |
| 57 manager_->RemoveObserver(this); | 57 manager_->RemoveObserver(this); |
| 58 } | 58 } |
| 59 | 59 |
| 60 void DownloadManagerService::ResumeDownload(JNIEnv* env, | 60 void DownloadManagerService::ResumeDownload( |
| 61 jobject obj, | 61 JNIEnv* env, |
| 62 uint32_t download_id, | 62 jobject obj, |
| 63 jstring fileName) { | 63 uint32_t download_id, |
| 64 ResumeDownloadInternal(download_id, ConvertJavaStringToUTF8(env, fileName), | 64 const JavaParamRef<jstring>& jdownload_guid, |
| 65 true); | 65 jstring fileName) { |
| 66 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
| 67 ResumeDownloadInternal(download_id, download_guid, |
| 68 ConvertJavaStringToUTF8(env, fileName), true); |
| 66 } | 69 } |
| 67 | 70 |
| 68 void DownloadManagerService::CancelDownload(JNIEnv* env, | 71 void DownloadManagerService::CancelDownload( |
| 69 jobject obj, | 72 JNIEnv* env, |
| 70 uint32_t download_id) { | 73 jobject obj, |
| 71 CancelDownloadInternal(download_id, true); | 74 const JavaParamRef<jstring>& jdownload_guid) { |
| 75 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
| 76 CancelDownloadInternal(download_guid, true); |
| 72 } | 77 } |
| 73 | 78 |
| 74 void DownloadManagerService::PauseDownload(JNIEnv* env, | 79 void DownloadManagerService::PauseDownload( |
| 75 jobject obj, | 80 JNIEnv* env, |
| 76 uint32_t download_id) { | 81 jobject obj, |
| 77 content::DownloadItem* item = manager_->GetDownload(download_id); | 82 const JavaParamRef<jstring>& jdownload_guid) { |
| 83 std::string download_guid = ConvertJavaStringToUTF8(env, jdownload_guid); |
| 84 content::DownloadItem* item = manager_->GetDownloadByGuid(download_guid); |
| 78 if (item) | 85 if (item) |
| 79 item->Pause(); | 86 item->Pause(); |
| 87 item->RemoveObserver(content::DownloadControllerAndroid::Get()); |
| 80 } | 88 } |
| 81 | 89 |
| 82 void DownloadManagerService::ManagerGoingDown( | 90 void DownloadManagerService::ManagerGoingDown( |
| 83 content::DownloadManager* manager) { | 91 content::DownloadManager* manager) { |
| 84 manager_ = nullptr; | 92 manager_ = nullptr; |
| 85 } | 93 } |
| 86 | 94 |
| 87 void DownloadManagerService::ResumeDownloadItem(content::DownloadItem* item, | 95 void DownloadManagerService::ResumeDownloadItem(content::DownloadItem* item, |
| 88 const std::string& fileName) { | 96 const std::string& fileName) { |
| 89 if (!item->CanResume()) { | 97 if (!item->CanResume()) { |
| 90 OnResumptionFailed(item->GetId(), fileName); | 98 OnResumptionFailed(item->GetId(), fileName); |
| 91 return; | 99 return; |
| 92 } | 100 } |
| 93 item->AddObserver(content::DownloadControllerAndroid::Get()); | 101 item->AddObserver(content::DownloadControllerAndroid::Get()); |
| 94 item->Resume(); | 102 item->Resume(); |
| 95 if (!resume_callback_for_testing_.is_null()) | 103 if (!resume_callback_for_testing_.is_null()) |
| 96 resume_callback_for_testing_.Run(true); | 104 resume_callback_for_testing_.Run(true); |
| 97 } | 105 } |
| 98 | 106 |
| 99 void DownloadManagerService::ResumeDownloadInternal(uint32_t download_id, | 107 void DownloadManagerService::ResumeDownloadInternal( |
| 100 const std::string& fileName, | 108 uint32_t download_id, |
| 101 bool retry) { | 109 const std::string& download_guid, |
| 110 const std::string& fileName, |
| 111 bool retry) { |
| 102 if (!manager_) { | 112 if (!manager_) { |
| 103 OnResumptionFailed(download_id, fileName); | 113 OnResumptionFailed(download_id, fileName); |
| 104 return; | 114 return; |
| 105 } | 115 } |
| 106 content::DownloadItem* item = manager_->GetDownload(download_id); | 116 content::DownloadItem* item = manager_->GetDownloadByGuid(download_guid); |
| 107 if (item) { | 117 if (item) { |
| 108 ResumeDownloadItem(item, fileName); | 118 ResumeDownloadItem(item, fileName); |
| 109 return; | 119 return; |
| 110 } | 120 } |
| 111 if (!retry) { | 121 if (!retry) { |
| 112 OnResumptionFailed(download_id, fileName); | 122 OnResumptionFailed(download_id, fileName); |
| 113 return; | 123 return; |
| 114 } | 124 } |
| 115 // Post a delayed task to wait for the download history to load the download | 125 // Post a delayed task to wait for the download history to load the download |
| 116 // item. If the download item is not loaded when the delayed task runs, show | 126 // item. If the download item is not loaded when the delayed task runs, show |
| 117 // an download failed notification. Alternatively, we can have the | 127 // an download failed notification. Alternatively, we can have the |
| 118 // DownloadManager inform us when a download item is created. However, there | 128 // DownloadManager inform us when a download item is created. However, there |
| 119 // is no guarantee when the download item will be created, since the newly | 129 // is no guarantee when the download item will be created, since the newly |
| 120 // created item might not be loaded from download history. So user might wait | 130 // created item might not be loaded from download history. So user might wait |
| 121 // indefinitely to see the failed notification. See http://crbug.com/577893. | 131 // indefinitely to see the failed notification. See http://crbug.com/577893. |
| 122 base::MessageLoop::current()->PostDelayedTask( | 132 base::MessageLoop::current()->PostDelayedTask( |
| 123 FROM_HERE, | 133 FROM_HERE, base::Bind(&DownloadManagerService::ResumeDownloadInternal, |
| 124 base::Bind(&DownloadManagerService::ResumeDownloadInternal, | 134 base::Unretained(this), download_id, download_guid, |
| 125 base::Unretained(this), download_id, fileName, false), | 135 fileName, false), |
| 126 base::TimeDelta::FromMilliseconds(kRetryIntervalInMilliseconds)); | 136 base::TimeDelta::FromMilliseconds(kRetryIntervalInMilliseconds)); |
| 127 } | 137 } |
| 128 | 138 |
| 129 void DownloadManagerService::CancelDownloadInternal(uint32_t download_id, | 139 void DownloadManagerService::CancelDownloadInternal( |
| 130 bool retry) { | 140 const std::string& download_guid, |
| 141 bool retry) { |
| 131 if (!manager_) | 142 if (!manager_) |
| 132 return; | 143 return; |
| 133 content::DownloadItem* item = manager_->GetDownload(download_id); | 144 content::DownloadItem* item = manager_->GetDownloadByGuid(download_guid); |
| 134 if (item) { | 145 if (item) { |
| 135 item->Cancel(true); | 146 item->Cancel(true); |
| 147 item->RemoveObserver(content::DownloadControllerAndroid::Get()); |
| 136 return; | 148 return; |
| 137 } | 149 } |
| 138 if (retry) { | 150 if (retry) { |
| 139 base::MessageLoop::current()->PostDelayedTask( | 151 base::MessageLoop::current()->PostDelayedTask( |
| 140 FROM_HERE, base::Bind(&DownloadManagerService::CancelDownloadInternal, | 152 FROM_HERE, base::Bind(&DownloadManagerService::CancelDownloadInternal, |
| 141 base::Unretained(this), download_id, false), | 153 base::Unretained(this), download_guid, false), |
| 142 base::TimeDelta::FromMilliseconds(kRetryIntervalInMilliseconds)); | 154 base::TimeDelta::FromMilliseconds(kRetryIntervalInMilliseconds)); |
| 143 } | 155 } |
| 144 } | 156 } |
| 145 | 157 |
| 146 void DownloadManagerService::OnResumptionFailed(uint32_t download_id, | 158 void DownloadManagerService::OnResumptionFailed(uint32_t download_id, |
| 147 const std::string& fileName) { | 159 const std::string& fileName) { |
| 148 if (!java_ref_.is_null()) { | 160 if (!java_ref_.is_null()) { |
| 149 JNIEnv* env = base::android::AttachCurrentThread(); | 161 JNIEnv* env = base::android::AttachCurrentThread(); |
| 150 Java_DownloadManagerService_onResumptionFailed( | 162 Java_DownloadManagerService_onResumptionFailed( |
| 151 env, java_ref_.obj(), download_id, | 163 env, java_ref_.obj(), download_id, |
| 152 ConvertUTF8ToJavaString(env, fileName).obj()); | 164 ConvertUTF8ToJavaString(env, fileName).obj()); |
| 153 } | 165 } |
| 154 if (!resume_callback_for_testing_.is_null()) | 166 if (!resume_callback_for_testing_.is_null()) |
| 155 resume_callback_for_testing_.Run(false); | 167 resume_callback_for_testing_.Run(false); |
| 156 } | 168 } |
| OLD | NEW |