| 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" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 // A class that performs file operations and injects errors. | 30 // A class that performs file operations and injects errors. |
| 31 class DownloadFileWithError: public DownloadFileImpl { | 31 class DownloadFileWithError: public DownloadFileImpl { |
| 32 public: | 32 public: |
| 33 DownloadFileWithError( | 33 DownloadFileWithError( |
| 34 std::unique_ptr<DownloadSaveInfo> save_info, | 34 std::unique_ptr<DownloadSaveInfo> save_info, |
| 35 const base::FilePath& default_download_directory, | 35 const base::FilePath& default_download_directory, |
| 36 std::unique_ptr<ByteStreamReader> byte_stream, | 36 std::unique_ptr<ByteStreamReader> byte_stream, |
| 37 const std::vector<DownloadItem::ReceivedSlice>& received_slices, | |
| 38 const net::NetLogWithSource& net_log, | 37 const net::NetLogWithSource& net_log, |
| 39 base::WeakPtr<DownloadDestinationObserver> observer, | 38 base::WeakPtr<DownloadDestinationObserver> observer, |
| 40 const TestFileErrorInjector::FileErrorInfo& error_info, | 39 const TestFileErrorInjector::FileErrorInfo& error_info, |
| 41 const base::Closure& ctor_callback, | 40 const base::Closure& ctor_callback, |
| 42 const base::Closure& dtor_callback); | 41 const base::Closure& dtor_callback); |
| 43 | 42 |
| 44 ~DownloadFileWithError() override; | 43 ~DownloadFileWithError() override; |
| 45 | 44 |
| 46 void Initialize(const InitializeCallback& callback) override; | 45 void Initialize(const InitializeCallback& callback, |
| 46 const DownloadItem::ReceivedSlices& received_slices) override; |
| 47 | 47 |
| 48 // DownloadFile interface. | 48 // DownloadFile interface. |
| 49 DownloadInterruptReason WriteDataToFile(int64_t offset, | 49 DownloadInterruptReason WriteDataToFile(int64_t offset, |
| 50 const char* data, | 50 const char* data, |
| 51 size_t data_len) override; | 51 size_t data_len) override; |
| 52 | 52 |
| 53 void RenameAndUniquify(const base::FilePath& full_path, | 53 void RenameAndUniquify(const base::FilePath& full_path, |
| 54 const RenameCompletionCallback& callback) override; | 54 const RenameCompletionCallback& callback) override; |
| 55 void RenameAndAnnotate(const base::FilePath& full_path, | 55 void RenameAndAnnotate(const base::FilePath& full_path, |
| 56 const std::string& client_guid, | 56 const std::string& client_guid, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 original_callback.Run( | 99 original_callback.Run( |
| 100 overwrite_error, | 100 overwrite_error, |
| 101 overwrite_error == DOWNLOAD_INTERRUPT_REASON_NONE ? | 101 overwrite_error == DOWNLOAD_INTERRUPT_REASON_NONE ? |
| 102 path_result : base::FilePath()); | 102 path_result : base::FilePath()); |
| 103 } | 103 } |
| 104 | 104 |
| 105 DownloadFileWithError::DownloadFileWithError( | 105 DownloadFileWithError::DownloadFileWithError( |
| 106 std::unique_ptr<DownloadSaveInfo> save_info, | 106 std::unique_ptr<DownloadSaveInfo> save_info, |
| 107 const base::FilePath& default_download_directory, | 107 const base::FilePath& default_download_directory, |
| 108 std::unique_ptr<ByteStreamReader> byte_stream, | 108 std::unique_ptr<ByteStreamReader> byte_stream, |
| 109 const std::vector<DownloadItem::ReceivedSlice>& received_slices, | |
| 110 const net::NetLogWithSource& net_log, | 109 const net::NetLogWithSource& net_log, |
| 111 base::WeakPtr<DownloadDestinationObserver> observer, | 110 base::WeakPtr<DownloadDestinationObserver> observer, |
| 112 const TestFileErrorInjector::FileErrorInfo& error_info, | 111 const TestFileErrorInjector::FileErrorInfo& error_info, |
| 113 const base::Closure& ctor_callback, | 112 const base::Closure& ctor_callback, |
| 114 const base::Closure& dtor_callback) | 113 const base::Closure& dtor_callback) |
| 115 : DownloadFileImpl(std::move(save_info), | 114 : DownloadFileImpl(std::move(save_info), |
| 116 default_download_directory, | 115 default_download_directory, |
| 117 std::move(byte_stream), | 116 std::move(byte_stream), |
| 118 received_slices, | |
| 119 net_log, | 117 net_log, |
| 120 observer), | 118 observer), |
| 121 error_info_(error_info), | 119 error_info_(error_info), |
| 122 destruction_callback_(dtor_callback) { | 120 destruction_callback_(dtor_callback) { |
| 123 // DownloadFiles are created on the UI thread and are destroyed on the FILE | 121 // DownloadFiles are created on the UI thread and are destroyed on the FILE |
| 124 // thread. Schedule the ConstructionCallback on the FILE thread so that if a | 122 // thread. Schedule the ConstructionCallback on the FILE thread so that if a |
| 125 // DownloadItem schedules a DownloadFile to be destroyed and creates another | 123 // DownloadItem schedules a DownloadFile to be destroyed and creates another |
| 126 // one (as happens during download resumption), then the DestructionCallback | 124 // one (as happens during download resumption), then the DestructionCallback |
| 127 // for the old DownloadFile is run before the ConstructionCallback for the | 125 // for the old DownloadFile is run before the ConstructionCallback for the |
| 128 // next DownloadFile. | 126 // next DownloadFile. |
| 129 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, ctor_callback); | 127 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, ctor_callback); |
| 130 } | 128 } |
| 131 | 129 |
| 132 DownloadFileWithError::~DownloadFileWithError() { | 130 DownloadFileWithError::~DownloadFileWithError() { |
| 133 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 131 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 134 destruction_callback_.Run(); | 132 destruction_callback_.Run(); |
| 135 } | 133 } |
| 136 | 134 |
| 137 void DownloadFileWithError::Initialize( | 135 void DownloadFileWithError::Initialize( |
| 138 const InitializeCallback& callback) { | 136 const InitializeCallback& callback, |
| 137 const DownloadItem::ReceivedSlices& received_slices) { |
| 139 DownloadInterruptReason error_to_return = DOWNLOAD_INTERRUPT_REASON_NONE; | 138 DownloadInterruptReason error_to_return = DOWNLOAD_INTERRUPT_REASON_NONE; |
| 140 InitializeCallback callback_to_use = callback; | 139 InitializeCallback callback_to_use = callback; |
| 141 | 140 |
| 142 // Replace callback if the error needs to be overwritten. | 141 // Replace callback if the error needs to be overwritten. |
| 143 if (OverwriteError( | 142 if (OverwriteError( |
| 144 TestFileErrorInjector::FILE_OPERATION_INITIALIZE, | 143 TestFileErrorInjector::FILE_OPERATION_INITIALIZE, |
| 145 &error_to_return)) { | 144 &error_to_return)) { |
| 146 if (DOWNLOAD_INTERRUPT_REASON_NONE != error_to_return) { | 145 if (DOWNLOAD_INTERRUPT_REASON_NONE != error_to_return) { |
| 147 // Don't execute a, probably successful, Initialize; just | 146 // Don't execute a, probably successful, Initialize; just |
| 148 // return the error. | 147 // return the error. |
| 149 BrowserThread::PostTask( | 148 BrowserThread::PostTask( |
| 150 BrowserThread::UI, FROM_HERE, base::Bind( | 149 BrowserThread::UI, FROM_HERE, base::Bind( |
| 151 callback, error_to_return)); | 150 callback, error_to_return)); |
| 152 return; | 151 return; |
| 153 } | 152 } |
| 154 | 153 |
| 155 // Otherwise, just wrap the return. | 154 // Otherwise, just wrap the return. |
| 156 callback_to_use = base::Bind(&InitializeErrorCallback, callback, | 155 callback_to_use = base::Bind(&InitializeErrorCallback, callback, |
| 157 error_to_return); | 156 error_to_return); |
| 158 } | 157 } |
| 159 | 158 |
| 160 DownloadFileImpl::Initialize(callback_to_use); | 159 DownloadFileImpl::Initialize(callback_to_use, received_slices); |
| 161 } | 160 } |
| 162 | 161 |
| 163 DownloadInterruptReason DownloadFileWithError::WriteDataToFile( | 162 DownloadInterruptReason DownloadFileWithError::WriteDataToFile( |
| 164 int64_t offset, | 163 int64_t offset, |
| 165 const char* data, | 164 const char* data, |
| 166 size_t data_len) { | 165 size_t data_len) { |
| 167 return ShouldReturnError( | 166 return ShouldReturnError( |
| 168 TestFileErrorInjector::FILE_OPERATION_WRITE, | 167 TestFileErrorInjector::FILE_OPERATION_WRITE, |
| 169 DownloadFileImpl::WriteDataToFile(offset, data, data_len)); | 168 DownloadFileImpl::WriteDataToFile(offset, data, data_len)); |
| 170 } | 169 } |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 public: | 256 public: |
| 258 DownloadFileWithErrorFactory(const base::Closure& ctor_callback, | 257 DownloadFileWithErrorFactory(const base::Closure& ctor_callback, |
| 259 const base::Closure& dtor_callback); | 258 const base::Closure& dtor_callback); |
| 260 ~DownloadFileWithErrorFactory() override; | 259 ~DownloadFileWithErrorFactory() override; |
| 261 | 260 |
| 262 // DownloadFileFactory interface. | 261 // DownloadFileFactory interface. |
| 263 DownloadFile* CreateFile( | 262 DownloadFile* CreateFile( |
| 264 std::unique_ptr<DownloadSaveInfo> save_info, | 263 std::unique_ptr<DownloadSaveInfo> save_info, |
| 265 const base::FilePath& default_download_directory, | 264 const base::FilePath& default_download_directory, |
| 266 std::unique_ptr<ByteStreamReader> byte_stream, | 265 std::unique_ptr<ByteStreamReader> byte_stream, |
| 267 const std::vector<DownloadItem::ReceivedSlice>& received_slices, | |
| 268 const net::NetLogWithSource& net_log, | 266 const net::NetLogWithSource& net_log, |
| 269 base::WeakPtr<DownloadDestinationObserver> observer) override; | 267 base::WeakPtr<DownloadDestinationObserver> observer) override; |
| 270 | 268 |
| 271 bool SetError(TestFileErrorInjector::FileErrorInfo error); | 269 bool SetError(TestFileErrorInjector::FileErrorInfo error); |
| 272 | 270 |
| 273 private: | 271 private: |
| 274 // Our injected error. | 272 // Our injected error. |
| 275 TestFileErrorInjector::FileErrorInfo injected_error_; | 273 TestFileErrorInjector::FileErrorInfo injected_error_; |
| 276 | 274 |
| 277 // Callback for creation and destruction. | 275 // Callback for creation and destruction. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 288 injected_error_.error = DOWNLOAD_INTERRUPT_REASON_NONE; | 286 injected_error_.error = DOWNLOAD_INTERRUPT_REASON_NONE; |
| 289 injected_error_.operation_instance = -1; | 287 injected_error_.operation_instance = -1; |
| 290 } | 288 } |
| 291 | 289 |
| 292 DownloadFileWithErrorFactory::~DownloadFileWithErrorFactory() {} | 290 DownloadFileWithErrorFactory::~DownloadFileWithErrorFactory() {} |
| 293 | 291 |
| 294 DownloadFile* DownloadFileWithErrorFactory::CreateFile( | 292 DownloadFile* DownloadFileWithErrorFactory::CreateFile( |
| 295 std::unique_ptr<DownloadSaveInfo> save_info, | 293 std::unique_ptr<DownloadSaveInfo> save_info, |
| 296 const base::FilePath& default_download_directory, | 294 const base::FilePath& default_download_directory, |
| 297 std::unique_ptr<ByteStreamReader> byte_stream, | 295 std::unique_ptr<ByteStreamReader> byte_stream, |
| 298 const std::vector<DownloadItem::ReceivedSlice>& received_slices, | |
| 299 const net::NetLogWithSource& net_log, | 296 const net::NetLogWithSource& net_log, |
| 300 base::WeakPtr<DownloadDestinationObserver> observer) { | 297 base::WeakPtr<DownloadDestinationObserver> observer) { |
| 301 return new DownloadFileWithError(std::move(save_info), | 298 return new DownloadFileWithError(std::move(save_info), |
| 302 default_download_directory, | 299 default_download_directory, |
| 303 std::move(byte_stream), | 300 std::move(byte_stream), |
| 304 received_slices, | |
| 305 net_log, | 301 net_log, |
| 306 observer, | 302 observer, |
| 307 injected_error_, | 303 injected_error_, |
| 308 construction_callback_, | 304 construction_callback_, |
| 309 destruction_callback_); | 305 destruction_callback_); |
| 310 } | 306 } |
| 311 | 307 |
| 312 bool DownloadFileWithErrorFactory::SetError( | 308 bool DownloadFileWithErrorFactory::SetError( |
| 313 TestFileErrorInjector::FileErrorInfo error) { | 309 TestFileErrorInjector::FileErrorInfo error) { |
| 314 injected_error_ = std::move(error); | 310 injected_error_ = std::move(error); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 case FILE_OPERATION_RENAME_ANNOTATE: | 410 case FILE_OPERATION_RENAME_ANNOTATE: |
| 415 return "RENAME_ANNOTATE"; | 411 return "RENAME_ANNOTATE"; |
| 416 default: | 412 default: |
| 417 break; | 413 break; |
| 418 } | 414 } |
| 419 | 415 |
| 420 return "Unknown"; | 416 return "Unknown"; |
| 421 } | 417 } |
| 422 | 418 |
| 423 } // namespace content | 419 } // namespace content |
| OLD | NEW |