| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/public/test/test_file_error_injector.h" | 5 #include "content/public/test/test_file_error_injector.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 // A class that performs file operations and injects errors. | 25 // A class that performs file operations and injects errors. |
| 26 class DownloadFileWithErrors: public DownloadFileImpl { | 26 class DownloadFileWithErrors: public DownloadFileImpl { |
| 27 public: | 27 public: |
| 28 typedef base::Callback<void(const GURL& url)> ConstructionCallback; | 28 typedef base::Callback<void(const GURL& url)> ConstructionCallback; |
| 29 typedef base::Callback<void(const GURL& url)> DestructionCallback; | 29 typedef base::Callback<void(const GURL& url)> DestructionCallback; |
| 30 | 30 |
| 31 DownloadFileWithErrors( | 31 DownloadFileWithErrors( |
| 32 scoped_ptr<DownloadSaveInfo> save_info, | 32 const DownloadSaveInfo& save_info, |
| 33 const base::FilePath& default_download_directory, | 33 const base::FilePath& default_download_directory, |
| 34 const GURL& url, | 34 const GURL& url, |
| 35 const GURL& referrer_url, | 35 const GURL& referrer_url, |
| 36 bool calculate_hash, | 36 bool calculate_hash, |
| 37 scoped_ptr<ByteStreamReader> stream, | 37 scoped_ptr<net::FileStream> file_stream, |
| 38 scoped_ptr<ByteStreamReader> byte_stream, |
| 38 const net::BoundNetLog& bound_net_log, | 39 const net::BoundNetLog& bound_net_log, |
| 39 scoped_ptr<PowerSaveBlocker> power_save_blocker, | 40 scoped_ptr<PowerSaveBlocker> power_save_blocker, |
| 40 base::WeakPtr<DownloadDestinationObserver> observer, | 41 base::WeakPtr<DownloadDestinationObserver> observer, |
| 41 const TestFileErrorInjector::FileErrorInfo& error_info, | 42 const TestFileErrorInjector::FileErrorInfo& error_info, |
| 42 const ConstructionCallback& ctor_callback, | 43 const ConstructionCallback& ctor_callback, |
| 43 const DestructionCallback& dtor_callback); | 44 const DestructionCallback& dtor_callback); |
| 44 | 45 |
| 45 virtual ~DownloadFileWithErrors(); | 46 virtual ~DownloadFileWithErrors(); |
| 46 | 47 |
| 47 virtual void Initialize(const InitializeCallback& callback) OVERRIDE; | 48 virtual void Initialize(const InitializeCallback& callback) OVERRIDE; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 DownloadInterruptReason overwrite_error, | 98 DownloadInterruptReason overwrite_error, |
| 98 DownloadInterruptReason original_error, | 99 DownloadInterruptReason original_error, |
| 99 const base::FilePath& path_result) { | 100 const base::FilePath& path_result) { |
| 100 original_callback.Run( | 101 original_callback.Run( |
| 101 overwrite_error, | 102 overwrite_error, |
| 102 overwrite_error == DOWNLOAD_INTERRUPT_REASON_NONE ? | 103 overwrite_error == DOWNLOAD_INTERRUPT_REASON_NONE ? |
| 103 path_result : base::FilePath()); | 104 path_result : base::FilePath()); |
| 104 } | 105 } |
| 105 | 106 |
| 106 DownloadFileWithErrors::DownloadFileWithErrors( | 107 DownloadFileWithErrors::DownloadFileWithErrors( |
| 107 scoped_ptr<DownloadSaveInfo> save_info, | 108 const DownloadSaveInfo& save_info, |
| 108 const base::FilePath& default_download_directory, | 109 const base::FilePath& default_download_directory, |
| 109 const GURL& url, | 110 const GURL& url, |
| 110 const GURL& referrer_url, | 111 const GURL& referrer_url, |
| 111 bool calculate_hash, | 112 bool calculate_hash, |
| 112 scoped_ptr<ByteStreamReader> stream, | 113 scoped_ptr<net::FileStream> file_stream, |
| 114 scoped_ptr<ByteStreamReader> byte_stream, |
| 113 const net::BoundNetLog& bound_net_log, | 115 const net::BoundNetLog& bound_net_log, |
| 114 scoped_ptr<PowerSaveBlocker> power_save_blocker, | 116 scoped_ptr<PowerSaveBlocker> power_save_blocker, |
| 115 base::WeakPtr<DownloadDestinationObserver> observer, | 117 base::WeakPtr<DownloadDestinationObserver> observer, |
| 116 const TestFileErrorInjector::FileErrorInfo& error_info, | 118 const TestFileErrorInjector::FileErrorInfo& error_info, |
| 117 const ConstructionCallback& ctor_callback, | 119 const ConstructionCallback& ctor_callback, |
| 118 const DestructionCallback& dtor_callback) | 120 const DestructionCallback& dtor_callback) |
| 119 : DownloadFileImpl( | 121 : DownloadFileImpl(save_info, |
| 120 save_info.Pass(), default_download_directory, url, referrer_url, | 122 default_download_directory, |
| 121 calculate_hash, stream.Pass(), bound_net_log, | 123 url, |
| 122 power_save_blocker.Pass(), observer), | 124 referrer_url, |
| 123 source_url_(url), | 125 calculate_hash, |
| 124 error_info_(error_info), | 126 file_stream.Pass(), |
| 125 destruction_callback_(dtor_callback) { | 127 byte_stream.Pass(), |
| 128 bound_net_log, |
| 129 power_save_blocker.Pass(), |
| 130 observer), |
| 131 source_url_(url), |
| 132 error_info_(error_info), |
| 133 destruction_callback_(dtor_callback) { |
| 126 // DownloadFiles are created on the UI thread and are destroyed on the FILE | 134 // DownloadFiles are created on the UI thread and are destroyed on the FILE |
| 127 // thread. Schedule the ConstructionCallback on the FILE thread so that if a | 135 // thread. Schedule the ConstructionCallback on the FILE thread so that if a |
| 128 // DownloadItem schedules a DownloadFile to be destroyed and creates another | 136 // DownloadItem schedules a DownloadFile to be destroyed and creates another |
| 129 // one (as happens during download resumption), then the DestructionCallback | 137 // one (as happens during download resumption), then the DestructionCallback |
| 130 // for the old DownloadFile is run before the ConstructionCallback for the | 138 // for the old DownloadFile is run before the ConstructionCallback for the |
| 131 // next DownloadFile. | 139 // next DownloadFile. |
| 132 BrowserThread::PostTask( | 140 BrowserThread::PostTask( |
| 133 BrowserThread::FILE, | 141 BrowserThread::FILE, |
| 134 FROM_HERE, | 142 FROM_HERE, |
| 135 base::Bind(ctor_callback, source_url_)); | 143 base::Bind(ctor_callback, source_url_)); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 // A factory for constructing DownloadFiles that inject errors. | 263 // A factory for constructing DownloadFiles that inject errors. |
| 256 class DownloadFileWithErrorsFactory : public DownloadFileFactory { | 264 class DownloadFileWithErrorsFactory : public DownloadFileFactory { |
| 257 public: | 265 public: |
| 258 DownloadFileWithErrorsFactory( | 266 DownloadFileWithErrorsFactory( |
| 259 const DownloadFileWithErrors::ConstructionCallback& ctor_callback, | 267 const DownloadFileWithErrors::ConstructionCallback& ctor_callback, |
| 260 const DownloadFileWithErrors::DestructionCallback& dtor_callback); | 268 const DownloadFileWithErrors::DestructionCallback& dtor_callback); |
| 261 virtual ~DownloadFileWithErrorsFactory(); | 269 virtual ~DownloadFileWithErrorsFactory(); |
| 262 | 270 |
| 263 // DownloadFileFactory interface. | 271 // DownloadFileFactory interface. |
| 264 virtual DownloadFile* CreateFile( | 272 virtual DownloadFile* CreateFile( |
| 265 scoped_ptr<DownloadSaveInfo> save_info, | 273 const DownloadSaveInfo& save_info, |
| 266 const base::FilePath& default_download_directory, | 274 const base::FilePath& default_download_directory, |
| 267 const GURL& url, | 275 const GURL& url, |
| 268 const GURL& referrer_url, | 276 const GURL& referrer_url, |
| 269 bool calculate_hash, | 277 bool calculate_hash, |
| 270 scoped_ptr<ByteStreamReader> stream, | 278 scoped_ptr<net::FileStream> file_stream, |
| 279 scoped_ptr<ByteStreamReader> byte_stream, |
| 271 const net::BoundNetLog& bound_net_log, | 280 const net::BoundNetLog& bound_net_log, |
| 272 base::WeakPtr<DownloadDestinationObserver> observer) OVERRIDE; | 281 base::WeakPtr<DownloadDestinationObserver> observer) OVERRIDE; |
| 273 | 282 |
| 274 bool AddError( | 283 bool AddError( |
| 275 const TestFileErrorInjector::FileErrorInfo& error_info); | 284 const TestFileErrorInjector::FileErrorInfo& error_info); |
| 276 | 285 |
| 277 void ClearErrors(); | 286 void ClearErrors(); |
| 278 | 287 |
| 279 private: | 288 private: |
| 280 // Our injected error list, mapped by URL. One per file. | 289 // Our injected error list, mapped by URL. One per file. |
| 281 TestFileErrorInjector::ErrorMap injected_errors_; | 290 TestFileErrorInjector::ErrorMap injected_errors_; |
| 282 | 291 |
| 283 // Callback for creation and destruction. | 292 // Callback for creation and destruction. |
| 284 DownloadFileWithErrors::ConstructionCallback construction_callback_; | 293 DownloadFileWithErrors::ConstructionCallback construction_callback_; |
| 285 DownloadFileWithErrors::DestructionCallback destruction_callback_; | 294 DownloadFileWithErrors::DestructionCallback destruction_callback_; |
| 286 }; | 295 }; |
| 287 | 296 |
| 288 DownloadFileWithErrorsFactory::DownloadFileWithErrorsFactory( | 297 DownloadFileWithErrorsFactory::DownloadFileWithErrorsFactory( |
| 289 const DownloadFileWithErrors::ConstructionCallback& ctor_callback, | 298 const DownloadFileWithErrors::ConstructionCallback& ctor_callback, |
| 290 const DownloadFileWithErrors::DestructionCallback& dtor_callback) | 299 const DownloadFileWithErrors::DestructionCallback& dtor_callback) |
| 291 : construction_callback_(ctor_callback), | 300 : construction_callback_(ctor_callback), |
| 292 destruction_callback_(dtor_callback) { | 301 destruction_callback_(dtor_callback) { |
| 293 } | 302 } |
| 294 | 303 |
| 295 DownloadFileWithErrorsFactory::~DownloadFileWithErrorsFactory() { | 304 DownloadFileWithErrorsFactory::~DownloadFileWithErrorsFactory() { |
| 296 } | 305 } |
| 297 | 306 |
| 298 DownloadFile* DownloadFileWithErrorsFactory::CreateFile( | 307 DownloadFile* DownloadFileWithErrorsFactory::CreateFile( |
| 299 scoped_ptr<DownloadSaveInfo> save_info, | 308 const DownloadSaveInfo& save_info, |
| 300 const base::FilePath& default_download_directory, | 309 const base::FilePath& default_download_directory, |
| 301 const GURL& url, | 310 const GURL& url, |
| 302 const GURL& referrer_url, | 311 const GURL& referrer_url, |
| 303 bool calculate_hash, | 312 bool calculate_hash, |
| 304 scoped_ptr<ByteStreamReader> stream, | 313 scoped_ptr<net::FileStream> file_stream, |
| 314 scoped_ptr<ByteStreamReader> byte_stream, |
| 305 const net::BoundNetLog& bound_net_log, | 315 const net::BoundNetLog& bound_net_log, |
| 306 base::WeakPtr<DownloadDestinationObserver> observer) { | 316 base::WeakPtr<DownloadDestinationObserver> observer) { |
| 307 if (injected_errors_.find(url.spec()) == injected_errors_.end()) { | 317 if (injected_errors_.find(url.spec()) == injected_errors_.end()) { |
| 308 // Have to create entry, because FileErrorInfo is not a POD type. | 318 // Have to create entry, because FileErrorInfo is not a POD type. |
| 309 TestFileErrorInjector::FileErrorInfo err_info = { | 319 TestFileErrorInjector::FileErrorInfo err_info = { |
| 310 url.spec(), | 320 url.spec(), |
| 311 TestFileErrorInjector::FILE_OPERATION_INITIALIZE, | 321 TestFileErrorInjector::FILE_OPERATION_INITIALIZE, |
| 312 -1, | 322 -1, |
| 313 DOWNLOAD_INTERRUPT_REASON_NONE | 323 DOWNLOAD_INTERRUPT_REASON_NONE |
| 314 }; | 324 }; |
| 315 injected_errors_[url.spec()] = err_info; | 325 injected_errors_[url.spec()] = err_info; |
| 316 } | 326 } |
| 317 | 327 |
| 318 scoped_ptr<PowerSaveBlocker> psb( | 328 scoped_ptr<PowerSaveBlocker> psb( |
| 319 PowerSaveBlocker::Create( | 329 PowerSaveBlocker::Create( |
| 320 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, | 330 PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, |
| 321 "Download in progress")); | 331 "Download in progress")); |
| 322 | 332 |
| 323 return new DownloadFileWithErrors( | 333 return new DownloadFileWithErrors( |
| 324 save_info.Pass(), | 334 save_info, |
| 325 default_download_directory, | 335 default_download_directory, |
| 326 url, | 336 url, |
| 327 referrer_url, | 337 referrer_url, |
| 328 calculate_hash, | 338 calculate_hash, |
| 329 stream.Pass(), | 339 file_stream.Pass(), |
| 340 byte_stream.Pass(), |
| 330 bound_net_log, | 341 bound_net_log, |
| 331 psb.Pass(), | 342 psb.Pass(), |
| 332 observer, | 343 observer, |
| 333 injected_errors_[url.spec()], | 344 injected_errors_[url.spec()], |
| 334 construction_callback_, | 345 construction_callback_, |
| 335 destruction_callback_); | 346 destruction_callback_); |
| 336 } | 347 } |
| 337 | 348 |
| 338 bool DownloadFileWithErrorsFactory::AddError( | 349 bool DownloadFileWithErrorsFactory::AddError( |
| 339 const TestFileErrorInjector::FileErrorInfo& error_info) { | 350 const TestFileErrorInjector::FileErrorInfo& error_info) { |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 case FILE_OPERATION_RENAME_ANNOTATE: | 489 case FILE_OPERATION_RENAME_ANNOTATE: |
| 479 return "RENAME_ANNOTATE"; | 490 return "RENAME_ANNOTATE"; |
| 480 default: | 491 default: |
| 481 break; | 492 break; |
| 482 } | 493 } |
| 483 | 494 |
| 484 return "Unknown"; | 495 return "Unknown"; |
| 485 } | 496 } |
| 486 | 497 |
| 487 } // namespace content | 498 } // namespace content |
| OLD | NEW |