| 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 <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "content/browser/download/download_file_factory.h" | 13 #include "content/browser/download/download_file_factory.h" |
| 14 #include "content/browser/download/download_file_impl.h" | 14 #include "content/browser/download/download_file_impl.h" |
| 15 #include "content/browser/download/download_interrupt_reasons_impl.h" | 15 #include "content/browser/download/download_interrupt_reasons_impl.h" |
| 16 #include "content/browser/download/download_manager_impl.h" | 16 #include "content/browser/download/download_manager_impl.h" |
| 17 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 17 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 20 | 20 |
| 21 namespace content { | 21 namespace content { |
| 22 class ByteStreamReader; | 22 class ByteStreamReader; |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 // A class that performs file operations and injects errors. | 26 // A class that performs file operations and injects errors. |
| 27 class DownloadFileWithError: public DownloadFileImpl { | 27 class DownloadFileWithError: public DownloadFileImpl { |
| 28 public: | 28 public: |
| 29 DownloadFileWithError(scoped_ptr<DownloadSaveInfo> save_info, | 29 DownloadFileWithError(std::unique_ptr<DownloadSaveInfo> save_info, |
| 30 const base::FilePath& default_download_directory, | 30 const base::FilePath& default_download_directory, |
| 31 scoped_ptr<ByteStreamReader> byte_stream, | 31 std::unique_ptr<ByteStreamReader> byte_stream, |
| 32 const net::BoundNetLog& bound_net_log, | 32 const net::BoundNetLog& bound_net_log, |
| 33 base::WeakPtr<DownloadDestinationObserver> observer, | 33 base::WeakPtr<DownloadDestinationObserver> observer, |
| 34 const TestFileErrorInjector::FileErrorInfo& error_info, | 34 const TestFileErrorInjector::FileErrorInfo& error_info, |
| 35 const base::Closure& ctor_callback, | 35 const base::Closure& ctor_callback, |
| 36 const base::Closure& dtor_callback); | 36 const base::Closure& dtor_callback); |
| 37 | 37 |
| 38 ~DownloadFileWithError() override; | 38 ~DownloadFileWithError() override; |
| 39 | 39 |
| 40 void Initialize(const InitializeCallback& callback) override; | 40 void Initialize(const InitializeCallback& callback) override; |
| 41 | 41 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 DownloadInterruptReason overwrite_error, | 88 DownloadInterruptReason overwrite_error, |
| 89 DownloadInterruptReason original_error, | 89 DownloadInterruptReason original_error, |
| 90 const base::FilePath& path_result) { | 90 const base::FilePath& path_result) { |
| 91 original_callback.Run( | 91 original_callback.Run( |
| 92 overwrite_error, | 92 overwrite_error, |
| 93 overwrite_error == DOWNLOAD_INTERRUPT_REASON_NONE ? | 93 overwrite_error == DOWNLOAD_INTERRUPT_REASON_NONE ? |
| 94 path_result : base::FilePath()); | 94 path_result : base::FilePath()); |
| 95 } | 95 } |
| 96 | 96 |
| 97 DownloadFileWithError::DownloadFileWithError( | 97 DownloadFileWithError::DownloadFileWithError( |
| 98 scoped_ptr<DownloadSaveInfo> save_info, | 98 std::unique_ptr<DownloadSaveInfo> save_info, |
| 99 const base::FilePath& default_download_directory, | 99 const base::FilePath& default_download_directory, |
| 100 scoped_ptr<ByteStreamReader> byte_stream, | 100 std::unique_ptr<ByteStreamReader> byte_stream, |
| 101 const net::BoundNetLog& bound_net_log, | 101 const net::BoundNetLog& bound_net_log, |
| 102 base::WeakPtr<DownloadDestinationObserver> observer, | 102 base::WeakPtr<DownloadDestinationObserver> observer, |
| 103 const TestFileErrorInjector::FileErrorInfo& error_info, | 103 const TestFileErrorInjector::FileErrorInfo& error_info, |
| 104 const base::Closure& ctor_callback, | 104 const base::Closure& ctor_callback, |
| 105 const base::Closure& dtor_callback) | 105 const base::Closure& dtor_callback) |
| 106 : DownloadFileImpl(std::move(save_info), | 106 : DownloadFileImpl(std::move(save_info), |
| 107 default_download_directory, | 107 default_download_directory, |
| 108 std::move(byte_stream), | 108 std::move(byte_stream), |
| 109 bound_net_log, | 109 bound_net_log, |
| 110 observer), | 110 observer), |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 | 242 |
| 243 // A factory for constructing DownloadFiles that inject errors. | 243 // A factory for constructing DownloadFiles that inject errors. |
| 244 class DownloadFileWithErrorFactory : public DownloadFileFactory { | 244 class DownloadFileWithErrorFactory : public DownloadFileFactory { |
| 245 public: | 245 public: |
| 246 DownloadFileWithErrorFactory(const base::Closure& ctor_callback, | 246 DownloadFileWithErrorFactory(const base::Closure& ctor_callback, |
| 247 const base::Closure& dtor_callback); | 247 const base::Closure& dtor_callback); |
| 248 ~DownloadFileWithErrorFactory() override; | 248 ~DownloadFileWithErrorFactory() override; |
| 249 | 249 |
| 250 // DownloadFileFactory interface. | 250 // DownloadFileFactory interface. |
| 251 DownloadFile* CreateFile( | 251 DownloadFile* CreateFile( |
| 252 scoped_ptr<DownloadSaveInfo> save_info, | 252 std::unique_ptr<DownloadSaveInfo> save_info, |
| 253 const base::FilePath& default_download_directory, | 253 const base::FilePath& default_download_directory, |
| 254 scoped_ptr<ByteStreamReader> byte_stream, | 254 std::unique_ptr<ByteStreamReader> byte_stream, |
| 255 const net::BoundNetLog& bound_net_log, | 255 const net::BoundNetLog& bound_net_log, |
| 256 base::WeakPtr<DownloadDestinationObserver> observer) override; | 256 base::WeakPtr<DownloadDestinationObserver> observer) override; |
| 257 | 257 |
| 258 bool SetError(TestFileErrorInjector::FileErrorInfo error); | 258 bool SetError(TestFileErrorInjector::FileErrorInfo error); |
| 259 | 259 |
| 260 private: | 260 private: |
| 261 // Our injected error. | 261 // Our injected error. |
| 262 TestFileErrorInjector::FileErrorInfo injected_error_; | 262 TestFileErrorInjector::FileErrorInfo injected_error_; |
| 263 | 263 |
| 264 // Callback for creation and destruction. | 264 // Callback for creation and destruction. |
| 265 base::Closure construction_callback_; | 265 base::Closure construction_callback_; |
| 266 base::Closure destruction_callback_; | 266 base::Closure destruction_callback_; |
| 267 }; | 267 }; |
| 268 | 268 |
| 269 DownloadFileWithErrorFactory::DownloadFileWithErrorFactory( | 269 DownloadFileWithErrorFactory::DownloadFileWithErrorFactory( |
| 270 const base::Closure& ctor_callback, | 270 const base::Closure& ctor_callback, |
| 271 const base::Closure& dtor_callback) | 271 const base::Closure& dtor_callback) |
| 272 : construction_callback_(ctor_callback), | 272 : construction_callback_(ctor_callback), |
| 273 destruction_callback_(dtor_callback) { | 273 destruction_callback_(dtor_callback) { |
| 274 injected_error_.code = TestFileErrorInjector::FILE_OPERATION_INITIALIZE; | 274 injected_error_.code = TestFileErrorInjector::FILE_OPERATION_INITIALIZE; |
| 275 injected_error_.error = DOWNLOAD_INTERRUPT_REASON_NONE; | 275 injected_error_.error = DOWNLOAD_INTERRUPT_REASON_NONE; |
| 276 injected_error_.operation_instance = -1; | 276 injected_error_.operation_instance = -1; |
| 277 } | 277 } |
| 278 | 278 |
| 279 DownloadFileWithErrorFactory::~DownloadFileWithErrorFactory() {} | 279 DownloadFileWithErrorFactory::~DownloadFileWithErrorFactory() {} |
| 280 | 280 |
| 281 DownloadFile* DownloadFileWithErrorFactory::CreateFile( | 281 DownloadFile* DownloadFileWithErrorFactory::CreateFile( |
| 282 scoped_ptr<DownloadSaveInfo> save_info, | 282 std::unique_ptr<DownloadSaveInfo> save_info, |
| 283 const base::FilePath& default_download_directory, | 283 const base::FilePath& default_download_directory, |
| 284 scoped_ptr<ByteStreamReader> byte_stream, | 284 std::unique_ptr<ByteStreamReader> byte_stream, |
| 285 const net::BoundNetLog& bound_net_log, | 285 const net::BoundNetLog& bound_net_log, |
| 286 base::WeakPtr<DownloadDestinationObserver> observer) { | 286 base::WeakPtr<DownloadDestinationObserver> observer) { |
| 287 return new DownloadFileWithError(std::move(save_info), | 287 return new DownloadFileWithError(std::move(save_info), |
| 288 default_download_directory, | 288 default_download_directory, |
| 289 std::move(byte_stream), | 289 std::move(byte_stream), |
| 290 bound_net_log, | 290 bound_net_log, |
| 291 observer, | 291 observer, |
| 292 injected_error_, | 292 injected_error_, |
| 293 construction_callback_, | 293 construction_callback_, |
| 294 destruction_callback_); | 294 destruction_callback_); |
| 295 } | 295 } |
| 296 | 296 |
| 297 bool DownloadFileWithErrorFactory::SetError( | 297 bool DownloadFileWithErrorFactory::SetError( |
| 298 TestFileErrorInjector::FileErrorInfo error) { | 298 TestFileErrorInjector::FileErrorInfo error) { |
| 299 injected_error_ = std::move(error); | 299 injected_error_ = std::move(error); |
| 300 return true; | 300 return true; |
| 301 } | 301 } |
| 302 | 302 |
| 303 TestFileErrorInjector::TestFileErrorInjector(DownloadManager* download_manager) | 303 TestFileErrorInjector::TestFileErrorInjector(DownloadManager* download_manager) |
| 304 : // This code is only used for browser_tests, so a | 304 : // This code is only used for browser_tests, so a |
| 305 // DownloadManager is always a DownloadManagerImpl. | 305 // DownloadManager is always a DownloadManagerImpl. |
| 306 download_manager_(static_cast<DownloadManagerImpl*>(download_manager)) { | 306 download_manager_(static_cast<DownloadManagerImpl*>(download_manager)) { |
| 307 // Record the value of the pointer, for later validation. | 307 // Record the value of the pointer, for later validation. |
| 308 created_factory_ = new DownloadFileWithErrorFactory( | 308 created_factory_ = new DownloadFileWithErrorFactory( |
| 309 base::Bind(&TestFileErrorInjector::RecordDownloadFileConstruction, this), | 309 base::Bind(&TestFileErrorInjector::RecordDownloadFileConstruction, this), |
| 310 base::Bind(&TestFileErrorInjector::RecordDownloadFileDestruction, this)); | 310 base::Bind(&TestFileErrorInjector::RecordDownloadFileDestruction, this)); |
| 311 | 311 |
| 312 // We will transfer ownership of the factory to the download manager. | 312 // We will transfer ownership of the factory to the download manager. |
| 313 scoped_ptr<DownloadFileFactory> download_file_factory( | 313 std::unique_ptr<DownloadFileFactory> download_file_factory(created_factory_); |
| 314 created_factory_); | |
| 315 | 314 |
| 316 download_manager_->SetDownloadFileFactoryForTesting( | 315 download_manager_->SetDownloadFileFactoryForTesting( |
| 317 std::move(download_file_factory)); | 316 std::move(download_file_factory)); |
| 318 } | 317 } |
| 319 | 318 |
| 320 TestFileErrorInjector::~TestFileErrorInjector() { | 319 TestFileErrorInjector::~TestFileErrorInjector() { |
| 321 } | 320 } |
| 322 | 321 |
| 323 void TestFileErrorInjector::ClearError() { | 322 void TestFileErrorInjector::ClearError() { |
| 324 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 323 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 case FILE_OPERATION_RENAME_ANNOTATE: | 399 case FILE_OPERATION_RENAME_ANNOTATE: |
| 401 return "RENAME_ANNOTATE"; | 400 return "RENAME_ANNOTATE"; |
| 402 default: | 401 default: |
| 403 break; | 402 break; |
| 404 } | 403 } |
| 405 | 404 |
| 406 return "Unknown"; | 405 return "Unknown"; |
| 407 } | 406 } |
| 408 | 407 |
| 409 } // namespace content | 408 } // namespace content |
| OLD | NEW |