| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 const ConstructionCallback& ctor_callback, | 42 const ConstructionCallback& ctor_callback, |
| 43 const DestructionCallback& dtor_callback); | 43 const DestructionCallback& dtor_callback); |
| 44 | 44 |
| 45 ~DownloadFileWithErrors(); | 45 ~DownloadFileWithErrors(); |
| 46 | 46 |
| 47 virtual void Initialize(const InitializeCallback& callback) OVERRIDE; | 47 virtual void Initialize(const InitializeCallback& callback) OVERRIDE; |
| 48 | 48 |
| 49 // DownloadFile interface. | 49 // DownloadFile interface. |
| 50 virtual DownloadInterruptReason AppendDataToFile( | 50 virtual DownloadInterruptReason AppendDataToFile( |
| 51 const char* data, size_t data_len) OVERRIDE; | 51 const char* data, size_t data_len) OVERRIDE; |
| 52 virtual void Rename(const FilePath& full_path, | 52 virtual void RenameAndUniquify( |
| 53 bool overwrite_existing_file, | 53 const FilePath& full_path, |
| 54 const RenameCompletionCallback& callback) OVERRIDE; | 54 const RenameCompletionCallback& callback) OVERRIDE; |
| 55 virtual void RenameAndAnnotate( |
| 56 const FilePath& full_path, |
| 57 const RenameCompletionCallback& callback) OVERRIDE; |
| 55 | 58 |
| 56 private: | 59 private: |
| 57 // Error generating helper. | 60 // Error generating helper. |
| 58 DownloadInterruptReason ShouldReturnError( | 61 DownloadInterruptReason ShouldReturnError( |
| 59 TestFileErrorInjector::FileOperationCode code, | 62 TestFileErrorInjector::FileOperationCode code, |
| 60 DownloadInterruptReason original_error); | 63 DownloadInterruptReason original_error); |
| 61 | 64 |
| 62 // Determine whether to overwrite an operation with the given code | 65 // Determine whether to overwrite an operation with the given code |
| 63 // with a substitute error; if returns true, |*original_error| is | 66 // with a substitute error; if returns true, |*original_error| is |
| 64 // written with the error to use for overwriting. | 67 // written with the error to use for overwriting. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 DownloadFileImpl::Initialize(callback_to_use); | 146 DownloadFileImpl::Initialize(callback_to_use); |
| 144 } | 147 } |
| 145 | 148 |
| 146 DownloadInterruptReason DownloadFileWithErrors::AppendDataToFile( | 149 DownloadInterruptReason DownloadFileWithErrors::AppendDataToFile( |
| 147 const char* data, size_t data_len) { | 150 const char* data, size_t data_len) { |
| 148 return ShouldReturnError( | 151 return ShouldReturnError( |
| 149 TestFileErrorInjector::FILE_OPERATION_WRITE, | 152 TestFileErrorInjector::FILE_OPERATION_WRITE, |
| 150 DownloadFileImpl::AppendDataToFile(data, data_len)); | 153 DownloadFileImpl::AppendDataToFile(data, data_len)); |
| 151 } | 154 } |
| 152 | 155 |
| 153 void DownloadFileWithErrors::Rename( | 156 void DownloadFileWithErrors::RenameAndUniquify( |
| 154 const FilePath& full_path, | 157 const FilePath& full_path, |
| 155 bool overwrite_existing_file, | |
| 156 const RenameCompletionCallback& callback) { | 158 const RenameCompletionCallback& callback) { |
| 157 DownloadInterruptReason error_to_return = DOWNLOAD_INTERRUPT_REASON_NONE; | 159 DownloadInterruptReason error_to_return = DOWNLOAD_INTERRUPT_REASON_NONE; |
| 158 RenameCompletionCallback callback_to_use = callback; | 160 RenameCompletionCallback callback_to_use = callback; |
| 159 | 161 |
| 160 // Replace callback if the error needs to be overwritten. | 162 // Replace callback if the error needs to be overwritten. |
| 161 if (OverwriteError( | 163 if (OverwriteError( |
| 162 TestFileErrorInjector::FILE_OPERATION_RENAME, | 164 TestFileErrorInjector::FILE_OPERATION_RENAME_UNIQUIFY, |
| 163 &error_to_return)) { | 165 &error_to_return)) { |
| 164 callback_to_use = base::Bind(&RenameErrorCallback, callback, | 166 callback_to_use = base::Bind(&RenameErrorCallback, callback, |
| 165 error_to_return); | 167 error_to_return); |
| 166 } | 168 } |
| 167 | 169 |
| 168 DownloadFileImpl::Rename(full_path, overwrite_existing_file, callback_to_use); | 170 DownloadFileImpl::RenameAndUniquify(full_path, callback_to_use); |
| 171 } |
| 172 |
| 173 void DownloadFileWithErrors::RenameAndAnnotate( |
| 174 const FilePath& full_path, |
| 175 const RenameCompletionCallback& callback) { |
| 176 DownloadInterruptReason error_to_return = DOWNLOAD_INTERRUPT_REASON_NONE; |
| 177 RenameCompletionCallback callback_to_use = callback; |
| 178 |
| 179 // Replace callback if the error needs to be overwritten. |
| 180 if (OverwriteError( |
| 181 TestFileErrorInjector::FILE_OPERATION_RENAME_ANNOTATE, |
| 182 &error_to_return)) { |
| 183 callback_to_use = base::Bind(&RenameErrorCallback, callback, |
| 184 error_to_return); |
| 185 } |
| 186 |
| 187 DownloadFileImpl::RenameAndAnnotate(full_path, callback_to_use); |
| 169 } | 188 } |
| 170 | 189 |
| 171 bool DownloadFileWithErrors::OverwriteError( | 190 bool DownloadFileWithErrors::OverwriteError( |
| 172 TestFileErrorInjector::FileOperationCode code, | 191 TestFileErrorInjector::FileOperationCode code, |
| 173 DownloadInterruptReason* output_error) { | 192 DownloadInterruptReason* output_error) { |
| 174 int counter = operation_counter_[code]++; | 193 int counter = operation_counter_[code]++; |
| 175 | 194 |
| 176 if (code != error_info_.code) | 195 if (code != error_info_.code) |
| 177 return false; | 196 return false; |
| 178 | 197 |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 return single_injector; | 427 return single_injector; |
| 409 } | 428 } |
| 410 | 429 |
| 411 // static | 430 // static |
| 412 std::string TestFileErrorInjector::DebugString(FileOperationCode code) { | 431 std::string TestFileErrorInjector::DebugString(FileOperationCode code) { |
| 413 switch (code) { | 432 switch (code) { |
| 414 case FILE_OPERATION_INITIALIZE: | 433 case FILE_OPERATION_INITIALIZE: |
| 415 return "INITIALIZE"; | 434 return "INITIALIZE"; |
| 416 case FILE_OPERATION_WRITE: | 435 case FILE_OPERATION_WRITE: |
| 417 return "WRITE"; | 436 return "WRITE"; |
| 418 case FILE_OPERATION_RENAME: | 437 case FILE_OPERATION_RENAME_UNIQUIFY: |
| 419 return "RENAME"; | 438 return "RENAME_UNIQUIFY"; |
| 439 case FILE_OPERATION_RENAME_ANNOTATE: |
| 440 return "RENAME_ANNOTATE"; |
| 420 default: | 441 default: |
| 421 break; | 442 break; |
| 422 } | 443 } |
| 423 | 444 |
| 424 return "Unknown"; | 445 return "Unknown"; |
| 425 } | 446 } |
| 426 | 447 |
| 427 } // namespace content | 448 } // namespace content |
| OLD | NEW |