| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <set> | 5 #include <set> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/i18n/number_formatting.h" | 10 #include "base/i18n/number_formatting.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 | 58 |
| 59 namespace { | 59 namespace { |
| 60 | 60 |
| 61 class MockDownloadFileFactory | 61 class MockDownloadFileFactory |
| 62 : public DownloadFileManager::DownloadFileFactory { | 62 : public DownloadFileManager::DownloadFileFactory { |
| 63 public: | 63 public: |
| 64 MockDownloadFileFactory() {} | 64 MockDownloadFileFactory() {} |
| 65 | 65 |
| 66 virtual DownloadFile* CreateFile(DownloadCreateInfo* info, | 66 virtual DownloadFile* CreateFile(DownloadCreateInfo* info, |
| 67 const DownloadRequestHandle& request_handle, | 67 const DownloadRequestHandle& request_handle, |
| 68 DownloadManager* download_manager) OVERRIDE; | 68 DownloadManager* download_manager, |
| 69 bool calculate_hash) OVERRIDE; |
| 69 }; | 70 }; |
| 70 | 71 |
| 71 DownloadFile* MockDownloadFileFactory::CreateFile( | 72 DownloadFile* MockDownloadFileFactory::CreateFile( |
| 72 DownloadCreateInfo* info, | 73 DownloadCreateInfo* info, |
| 73 const DownloadRequestHandle& request_handle, | 74 const DownloadRequestHandle& request_handle, |
| 74 DownloadManager* download_manager) { | 75 DownloadManager* download_manager, |
| 76 bool calculate_hash) { |
| 75 NOTREACHED(); | 77 NOTREACHED(); |
| 76 return NULL; | 78 return NULL; |
| 77 } | 79 } |
| 78 | 80 |
| 79 } // namespace | 81 } // namespace |
| 80 | 82 |
| 81 using content::BrowserThread; | 83 using content::BrowserThread; |
| 82 | 84 |
| 83 namespace { | 85 namespace { |
| 84 | 86 |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 | 217 |
| 216 BrowserThread::PostTask( | 218 BrowserThread::PostTask( |
| 217 BrowserThread::FILE, FROM_HERE, | 219 BrowserThread::FILE, FROM_HERE, |
| 218 base::Bind(&DownloadFileManager::UpdateDownload, file_manager_.get(), | 220 base::Bind(&DownloadFileManager::UpdateDownload, file_manager_.get(), |
| 219 DownloadId(kValidIdDomain, id), download_buffer_)); | 221 DownloadId(kValidIdDomain, id), download_buffer_)); |
| 220 | 222 |
| 221 message_loop_.RunAllPending(); | 223 message_loop_.RunAllPending(); |
| 222 } | 224 } |
| 223 | 225 |
| 224 void OnDownloadInterrupted(int32 download_id, int64 size, | 226 void OnDownloadInterrupted(int32 download_id, int64 size, |
| 227 const std::string& hash_state, |
| 225 InterruptReason reason) { | 228 InterruptReason reason) { |
| 226 download_manager_->OnDownloadInterrupted(download_id, size, reason); | 229 download_manager_->OnDownloadInterrupted(download_id, size, |
| 230 hash_state, reason); |
| 227 } | 231 } |
| 228 | 232 |
| 229 // Get the download item with ID |id|. | 233 // Get the download item with ID |id|. |
| 230 DownloadItem* GetActiveDownloadItem(int32 id) { | 234 DownloadItem* GetActiveDownloadItem(int32 id) { |
| 231 return download_manager_->GetActiveDownload(id); | 235 return download_manager_->GetActiveDownload(id); |
| 232 } | 236 } |
| 233 | 237 |
| 234 protected: | 238 protected: |
| 235 DownloadStatusUpdater download_status_updater_; | 239 DownloadStatusUpdater download_status_updater_; |
| 236 scoped_ptr<TestingProfile> profile_; | 240 scoped_ptr<TestingProfile> profile_; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 255 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); | 259 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); |
| 256 }; | 260 }; |
| 257 | 261 |
| 258 const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad"; | 262 const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad"; |
| 259 const size_t DownloadManagerTest::kTestDataLen = | 263 const size_t DownloadManagerTest::kTestDataLen = |
| 260 strlen(DownloadManagerTest::kTestData); | 264 strlen(DownloadManagerTest::kTestData); |
| 261 | 265 |
| 262 // A DownloadFile that we can inject errors into. | 266 // A DownloadFile that we can inject errors into. |
| 263 class DownloadFileWithErrors : public DownloadFileImpl { | 267 class DownloadFileWithErrors : public DownloadFileImpl { |
| 264 public: | 268 public: |
| 265 DownloadFileWithErrors(DownloadCreateInfo* info, DownloadManager* manager); | 269 DownloadFileWithErrors(DownloadCreateInfo* info, |
| 270 DownloadManager* manager, |
| 271 bool calculate_hash); |
| 266 virtual ~DownloadFileWithErrors() {} | 272 virtual ~DownloadFileWithErrors() {} |
| 267 | 273 |
| 268 // BaseFile delegated functions. | 274 // BaseFile delegated functions. |
| 269 virtual net::Error Initialize(bool calculate_hash); | 275 virtual net::Error Initialize(); |
| 270 virtual net::Error AppendDataToFile(const char* data, size_t data_len); | 276 virtual net::Error AppendDataToFile(const char* data, size_t data_len); |
| 271 virtual net::Error Rename(const FilePath& full_path); | 277 virtual net::Error Rename(const FilePath& full_path); |
| 272 | 278 |
| 273 void set_forced_error(net::Error error) { forced_error_ = error; } | 279 void set_forced_error(net::Error error) { forced_error_ = error; } |
| 274 void clear_forced_error() { forced_error_ = net::OK; } | 280 void clear_forced_error() { forced_error_ = net::OK; } |
| 275 net::Error forced_error() const { return forced_error_; } | 281 net::Error forced_error() const { return forced_error_; } |
| 276 | 282 |
| 277 private: | 283 private: |
| 278 net::Error ReturnError(net::Error function_error) { | 284 net::Error ReturnError(net::Error function_error) { |
| 279 if (forced_error_ != net::OK) { | 285 if (forced_error_ != net::OK) { |
| 280 net::Error ret = forced_error_; | 286 net::Error ret = forced_error_; |
| 281 clear_forced_error(); | 287 clear_forced_error(); |
| 282 return ret; | 288 return ret; |
| 283 } | 289 } |
| 284 | 290 |
| 285 return function_error; | 291 return function_error; |
| 286 } | 292 } |
| 287 | 293 |
| 288 net::Error forced_error_; | 294 net::Error forced_error_; |
| 289 }; | 295 }; |
| 290 | 296 |
| 291 DownloadFileWithErrors::DownloadFileWithErrors(DownloadCreateInfo* info, | 297 DownloadFileWithErrors::DownloadFileWithErrors(DownloadCreateInfo* info, |
| 292 DownloadManager* manager) | 298 DownloadManager* manager, |
| 293 : DownloadFileImpl(info, new DownloadRequestHandle(), manager), | 299 bool calculate_hash) |
| 300 : DownloadFileImpl(info, |
| 301 new DownloadRequestHandle(), |
| 302 manager, |
| 303 calculate_hash), |
| 294 forced_error_(net::OK) { | 304 forced_error_(net::OK) { |
| 295 } | 305 } |
| 296 | 306 |
| 297 net::Error DownloadFileWithErrors::Initialize(bool calculate_hash) { | 307 net::Error DownloadFileWithErrors::Initialize() { |
| 298 return ReturnError(DownloadFileImpl::Initialize(calculate_hash)); | 308 return ReturnError(DownloadFileImpl::Initialize()); |
| 299 } | 309 } |
| 300 | 310 |
| 301 net::Error DownloadFileWithErrors::AppendDataToFile(const char* data, | 311 net::Error DownloadFileWithErrors::AppendDataToFile(const char* data, |
| 302 size_t data_len) { | 312 size_t data_len) { |
| 303 return ReturnError(DownloadFileImpl::AppendDataToFile(data, data_len)); | 313 return ReturnError(DownloadFileImpl::AppendDataToFile(data, data_len)); |
| 304 } | 314 } |
| 305 | 315 |
| 306 net::Error DownloadFileWithErrors::Rename(const FilePath& full_path) { | 316 net::Error DownloadFileWithErrors::Rename(const FilePath& full_path) { |
| 307 return ReturnError(DownloadFileImpl::Rename(full_path)); | 317 return ReturnError(DownloadFileImpl::Rename(full_path)); |
| 308 } | 318 } |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 // don't call the function that deletes it, so we do so ourselves. | 486 // don't call the function that deletes it, so we do so ourselves. |
| 477 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 487 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 478 info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i)); | 488 info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i)); |
| 479 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; | 489 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; |
| 480 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); | 490 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); |
| 481 info->mime_type = kStartDownloadCases[i].mime_type; | 491 info->mime_type = kStartDownloadCases[i].mime_type; |
| 482 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 492 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
| 483 | 493 |
| 484 DownloadFile* download_file( | 494 DownloadFile* download_file( |
| 485 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), | 495 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), |
| 486 download_manager_)); | 496 download_manager_, false)); |
| 487 AddDownloadToFileManager(info->download_id.local(), download_file); | 497 AddDownloadToFileManager(info->download_id.local(), download_file); |
| 488 download_file->Initialize(false); | 498 download_file->Initialize(); |
| 489 download_manager_->StartDownload(info->download_id.local()); | 499 download_manager_->StartDownload(info->download_id.local()); |
| 490 message_loop_.RunAllPending(); | 500 message_loop_.RunAllPending(); |
| 491 | 501 |
| 492 // SelectFileObserver will have recorded any attempt to open the | 502 // SelectFileObserver will have recorded any attempt to open the |
| 493 // select file dialog. | 503 // select file dialog. |
| 494 // Note that DownloadManager::FileSelectionCanceled() is never called. | 504 // Note that DownloadManager::FileSelectionCanceled() is never called. |
| 495 EXPECT_EQ(kStartDownloadCases[i].expected_save_as, | 505 EXPECT_EQ(kStartDownloadCases[i].expected_save_as, |
| 496 observer.ShowedFileDialogForId(i)); | 506 observer.ShowedFileDialogForId(i)); |
| 497 } | 507 } |
| 498 } | 508 } |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 914 | 924 |
| 915 download_file->AppendDataToFile(kTestData, kTestDataLen); | 925 download_file->AppendDataToFile(kTestData, kTestDataLen); |
| 916 | 926 |
| 917 ContinueDownloadWithPath(download, new_path); | 927 ContinueDownloadWithPath(download, new_path); |
| 918 message_loop_.RunAllPending(); | 928 message_loop_.RunAllPending(); |
| 919 EXPECT_EQ(1, | 929 EXPECT_EQ(1, |
| 920 recorder.Count(MockDownloadFile::StatisticsRecorder::STAT_RENAME)); | 930 recorder.Count(MockDownloadFile::StatisticsRecorder::STAT_RENAME)); |
| 921 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 931 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 922 | 932 |
| 923 int64 error_size = 3; | 933 int64 error_size = 3; |
| 924 OnDownloadInterrupted(0, error_size, | 934 OnDownloadInterrupted(0, error_size, "", |
| 925 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 935 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
| 926 message_loop_.RunAllPending(); | 936 message_loop_.RunAllPending(); |
| 927 | 937 |
| 928 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); | 938 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); |
| 929 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); | 939 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
| 930 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); | 940 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); |
| 931 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); | 941 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); |
| 932 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); | 942 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); |
| 933 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); | 943 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
| 934 EXPECT_TRUE(observer->was_updated()); | 944 EXPECT_TRUE(observer->was_updated()); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 980 static const int32 local_id = 0; | 990 static const int32 local_id = 0; |
| 981 info->download_id = DownloadId(kValidIdDomain, local_id); | 991 info->download_id = DownloadId(kValidIdDomain, local_id); |
| 982 info->prompt_user_for_save_location = false; | 992 info->prompt_user_for_save_location = false; |
| 983 info->url_chain.push_back(GURL()); | 993 info->url_chain.push_back(GURL()); |
| 984 info->total_bytes = static_cast<int64>(kTestDataLen * 3); | 994 info->total_bytes = static_cast<int64>(kTestDataLen * 3); |
| 985 info->save_info.file_path = path; | 995 info->save_info.file_path = path; |
| 986 info->save_info.file_stream.reset(stream); | 996 info->save_info.file_stream.reset(stream); |
| 987 | 997 |
| 988 // Create a download file that we can insert errors into. | 998 // Create a download file that we can insert errors into. |
| 989 DownloadFileWithErrors* download_file(new DownloadFileWithErrors( | 999 DownloadFileWithErrors* download_file(new DownloadFileWithErrors( |
| 990 info.get(), download_manager_)); | 1000 info.get(), download_manager_, false)); |
| 1001 download_file->Initialize(); |
| 991 AddDownloadToFileManager(local_id, download_file); | 1002 AddDownloadToFileManager(local_id, download_file); |
| 992 | 1003 |
| 993 // |download_file| is owned by DownloadFileManager. | 1004 // |download_file| is owned by DownloadFileManager. |
| 994 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 1005 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
| 995 | 1006 |
| 996 DownloadItem* download = GetActiveDownloadItem(0); | 1007 DownloadItem* download = GetActiveDownloadItem(0); |
| 997 ASSERT_TRUE(download != NULL); | 1008 ASSERT_TRUE(download != NULL); |
| 998 // This will keep track of what should be displayed on the shelf. | 1009 // This will keep track of what should be displayed on the shelf. |
| 999 scoped_ptr<DownloadItemModel> download_item_model( | 1010 scoped_ptr<DownloadItemModel> download_item_model( |
| 1000 new DownloadItemModel(download)); | 1011 new DownloadItemModel(download)); |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1154 | 1165 |
| 1155 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 1166 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
| 1156 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 1167 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
| 1157 | 1168 |
| 1158 // Create and initialize the download file. We're bypassing the first part | 1169 // Create and initialize the download file. We're bypassing the first part |
| 1159 // of the download process and skipping to the part after the final file | 1170 // of the download process and skipping to the part after the final file |
| 1160 // name has been chosen, so we need to initialize the download file | 1171 // name has been chosen, so we need to initialize the download file |
| 1161 // properly. | 1172 // properly. |
| 1162 DownloadFile* download_file( | 1173 DownloadFile* download_file( |
| 1163 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), | 1174 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), |
| 1164 download_manager_)); | 1175 download_manager_, false)); |
| 1165 download_file->Rename(cr_path); | 1176 download_file->Rename(cr_path); |
| 1166 // This creates the .crdownload version of the file. | 1177 // This creates the .crdownload version of the file. |
| 1167 download_file->Initialize(false); | 1178 download_file->Initialize(); |
| 1168 // |download_file| is owned by DownloadFileManager. | 1179 // |download_file| is owned by DownloadFileManager. |
| 1169 AddDownloadToFileManager(info->download_id.local(), download_file); | 1180 AddDownloadToFileManager(info->download_id.local(), download_file); |
| 1170 | 1181 |
| 1171 ContinueDownloadWithPath(download, new_path); | 1182 ContinueDownloadWithPath(download, new_path); |
| 1172 message_loop_.RunAllPending(); | 1183 message_loop_.RunAllPending(); |
| 1173 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 1184 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 1174 | 1185 |
| 1175 download_file->AppendDataToFile(kTestData, kTestDataLen); | 1186 download_file->AppendDataToFile(kTestData, kTestDataLen); |
| 1176 | 1187 |
| 1177 // Finish the download. | 1188 // Finish the download. |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1231 | 1242 |
| 1232 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 1243 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
| 1233 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 1244 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
| 1234 | 1245 |
| 1235 // Create and initialize the download file. We're bypassing the first part | 1246 // Create and initialize the download file. We're bypassing the first part |
| 1236 // of the download process and skipping to the part after the final file | 1247 // of the download process and skipping to the part after the final file |
| 1237 // name has been chosen, so we need to initialize the download file | 1248 // name has been chosen, so we need to initialize the download file |
| 1238 // properly. | 1249 // properly. |
| 1239 DownloadFile* download_file( | 1250 DownloadFile* download_file( |
| 1240 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), | 1251 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), |
| 1241 download_manager_)); | 1252 download_manager_, false)); |
| 1242 download_file->Rename(cr_path); | 1253 download_file->Rename(cr_path); |
| 1243 // This creates the .crdownload version of the file. | 1254 // This creates the .crdownload version of the file. |
| 1244 download_file->Initialize(false); | 1255 download_file->Initialize(); |
| 1245 // |download_file| is owned by DownloadFileManager. | 1256 // |download_file| is owned by DownloadFileManager. |
| 1246 AddDownloadToFileManager(info->download_id.local(), download_file); | 1257 AddDownloadToFileManager(info->download_id.local(), download_file); |
| 1247 | 1258 |
| 1248 ContinueDownloadWithPath(download, new_path); | 1259 ContinueDownloadWithPath(download, new_path); |
| 1249 message_loop_.RunAllPending(); | 1260 message_loop_.RunAllPending(); |
| 1250 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 1261 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 1251 | 1262 |
| 1252 download_file->AppendDataToFile(kTestData, kTestDataLen); | 1263 download_file->AppendDataToFile(kTestData, kTestDataLen); |
| 1253 | 1264 |
| 1254 // Finish the download. | 1265 // Finish the download. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1284 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); | 1295 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
| 1285 EXPECT_TRUE(observer->was_updated()); | 1296 EXPECT_TRUE(observer->was_updated()); |
| 1286 EXPECT_FALSE(observer->was_opened()); | 1297 EXPECT_FALSE(observer->was_opened()); |
| 1287 EXPECT_TRUE(download->GetFileExternallyRemoved()); | 1298 EXPECT_TRUE(download->GetFileExternallyRemoved()); |
| 1288 EXPECT_EQ(DownloadItem::COMPLETE, download->GetState()); | 1299 EXPECT_EQ(DownloadItem::COMPLETE, download->GetState()); |
| 1289 EXPECT_EQ(download_item_model->GetStatusText(), | 1300 EXPECT_EQ(download_item_model->GetStatusText(), |
| 1290 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); | 1301 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); |
| 1291 | 1302 |
| 1292 EXPECT_FALSE(file_util::PathExists(new_path)); | 1303 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 1293 } | 1304 } |
| OLD | NEW |