| 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" |
| 11 #include "content/browser/download/download_create_info.h" | |
| 12 #include "content/browser/download/download_file_impl.h" | 11 #include "content/browser/download/download_file_impl.h" |
| 13 #include "content/browser/download/download_file_manager.h" | 12 #include "content/browser/download/download_file_factory.h" |
| 14 #include "content/browser/download/download_interrupt_reasons_impl.h" | 13 #include "content/browser/download/download_interrupt_reasons_impl.h" |
| 14 #include "content/browser/download/download_manager_impl.h" |
| 15 #include "content/browser/power_save_blocker.h" | 15 #include "content/browser/power_save_blocker.h" |
| 16 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" | 16 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/download_id.h" | |
| 19 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 20 | 19 |
| 21 namespace content { | 20 namespace content { |
| 22 class ByteStreamReader; | 21 class ByteStreamReader; |
| 23 } | 22 } |
| 24 | 23 |
| 25 namespace { | 24 namespace { |
| 26 | 25 |
| 27 DownloadFileManager* GetDownloadFileManager() { | |
| 28 content::ResourceDispatcherHostImpl* rdh = | |
| 29 content::ResourceDispatcherHostImpl::Get(); | |
| 30 DCHECK(rdh != NULL); | |
| 31 return rdh->download_file_manager(); | |
| 32 } | |
| 33 | |
| 34 // A class that performs file operations and injects errors. | 26 // A class that performs file operations and injects errors. |
| 35 class DownloadFileWithErrors: public DownloadFileImpl { | 27 class DownloadFileWithErrors: public DownloadFileImpl { |
| 36 public: | 28 public: |
| 37 typedef base::Callback<void(const GURL& url, content::DownloadId id)> | 29 typedef base::Callback<void(const GURL& url)> ConstructionCallback; |
| 38 ConstructionCallback; | |
| 39 typedef base::Callback<void(const GURL& url)> DestructionCallback; | 30 typedef base::Callback<void(const GURL& url)> DestructionCallback; |
| 40 | 31 |
| 41 DownloadFileWithErrors( | 32 DownloadFileWithErrors( |
| 42 const DownloadCreateInfo* info, | 33 const content::DownloadSaveInfo& save_info, |
| 34 const FilePath& default_download_directory, |
| 35 const GURL& url, |
| 36 const GURL& referrer_url, |
| 37 int64 received_bytes, |
| 38 bool calculate_hash, |
| 43 scoped_ptr<content::ByteStreamReader> stream, | 39 scoped_ptr<content::ByteStreamReader> stream, |
| 44 DownloadRequestHandleInterface* request_handle, | |
| 45 content::DownloadManager* download_manager, | |
| 46 bool calculate_hash, | |
| 47 const net::BoundNetLog& bound_net_log, | 40 const net::BoundNetLog& bound_net_log, |
| 41 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, |
| 42 base::WeakPtr<content::DownloadDestinationObserver> observer, |
| 48 const content::TestFileErrorInjector::FileErrorInfo& error_info, | 43 const content::TestFileErrorInjector::FileErrorInfo& error_info, |
| 49 const ConstructionCallback& ctor_callback, | 44 const ConstructionCallback& ctor_callback, |
| 50 const DestructionCallback& dtor_callback); | 45 const DestructionCallback& dtor_callback); |
| 51 | 46 |
| 52 ~DownloadFileWithErrors(); | 47 ~DownloadFileWithErrors(); |
| 53 | 48 |
| 49 virtual void Initialize(const InitializeCallback& callback) OVERRIDE; |
| 50 |
| 54 // DownloadFile interface. | 51 // DownloadFile interface. |
| 55 virtual content::DownloadInterruptReason Initialize() OVERRIDE; | |
| 56 virtual content::DownloadInterruptReason AppendDataToFile( | 52 virtual content::DownloadInterruptReason AppendDataToFile( |
| 57 const char* data, size_t data_len) OVERRIDE; | 53 const char* data, size_t data_len) OVERRIDE; |
| 58 virtual void Rename(const FilePath& full_path, | 54 virtual void Rename(const FilePath& full_path, |
| 59 bool overwrite_existing_file, | 55 bool overwrite_existing_file, |
| 60 const RenameCompletionCallback& callback) OVERRIDE; | 56 const RenameCompletionCallback& callback) OVERRIDE; |
| 61 | 57 |
| 62 private: | 58 private: |
| 63 // Error generating helper. | 59 // Error generating helper. |
| 64 content::DownloadInterruptReason ShouldReturnError( | 60 content::DownloadInterruptReason ShouldReturnError( |
| 65 content::TestFileErrorInjector::FileOperationCode code, | 61 content::TestFileErrorInjector::FileOperationCode code, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 82 content::TestFileErrorInjector::FileErrorInfo error_info_; | 78 content::TestFileErrorInjector::FileErrorInfo error_info_; |
| 83 | 79 |
| 84 // Count per operation. 0-based. | 80 // Count per operation. 0-based. |
| 85 std::map<content::TestFileErrorInjector::FileOperationCode, int> | 81 std::map<content::TestFileErrorInjector::FileOperationCode, int> |
| 86 operation_counter_; | 82 operation_counter_; |
| 87 | 83 |
| 88 // Callback for destruction. | 84 // Callback for destruction. |
| 89 DestructionCallback destruction_callback_; | 85 DestructionCallback destruction_callback_; |
| 90 }; | 86 }; |
| 91 | 87 |
| 88 static void InitializeErrorCallback( |
| 89 const content::DownloadFile::InitializeCallback original_callback, |
| 90 content::DownloadInterruptReason overwrite_error, |
| 91 content::DownloadInterruptReason original_error) { |
| 92 original_callback.Run(overwrite_error); |
| 93 } |
| 94 |
| 92 static void RenameErrorCallback( | 95 static void RenameErrorCallback( |
| 93 const content::DownloadFile::RenameCompletionCallback original_callback, | 96 const content::DownloadFile::RenameCompletionCallback original_callback, |
| 94 content::DownloadInterruptReason overwrite_error, | 97 content::DownloadInterruptReason overwrite_error, |
| 95 content::DownloadInterruptReason original_error, | 98 content::DownloadInterruptReason original_error, |
| 96 const FilePath& path_result) { | 99 const FilePath& path_result) { |
| 97 original_callback.Run( | 100 original_callback.Run( |
| 98 overwrite_error, | 101 overwrite_error, |
| 99 overwrite_error == content::DOWNLOAD_INTERRUPT_REASON_NONE ? | 102 overwrite_error == content::DOWNLOAD_INTERRUPT_REASON_NONE ? |
| 100 path_result : FilePath()); | 103 path_result : FilePath()); |
| 101 } | 104 } |
| 102 | 105 |
| 103 | |
| 104 DownloadFileWithErrors::DownloadFileWithErrors( | 106 DownloadFileWithErrors::DownloadFileWithErrors( |
| 105 const DownloadCreateInfo* info, | 107 const content::DownloadSaveInfo& save_info, |
| 108 const FilePath& default_download_directory, |
| 109 const GURL& url, |
| 110 const GURL& referrer_url, |
| 111 int64 received_bytes, |
| 112 bool calculate_hash, |
| 106 scoped_ptr<content::ByteStreamReader> stream, | 113 scoped_ptr<content::ByteStreamReader> stream, |
| 107 DownloadRequestHandleInterface* request_handle, | |
| 108 content::DownloadManager* download_manager, | |
| 109 bool calculate_hash, | |
| 110 const net::BoundNetLog& bound_net_log, | 114 const net::BoundNetLog& bound_net_log, |
| 115 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, |
| 116 base::WeakPtr<content::DownloadDestinationObserver> observer, |
| 111 const content::TestFileErrorInjector::FileErrorInfo& error_info, | 117 const content::TestFileErrorInjector::FileErrorInfo& error_info, |
| 112 const ConstructionCallback& ctor_callback, | 118 const ConstructionCallback& ctor_callback, |
| 113 const DestructionCallback& dtor_callback) | 119 const DestructionCallback& dtor_callback) |
| 114 : DownloadFileImpl(info, | 120 : DownloadFileImpl( |
| 115 stream.Pass(), | 121 save_info, default_download_directory, url, referrer_url, |
| 116 request_handle, | 122 received_bytes, calculate_hash, stream.Pass(), bound_net_log, |
| 117 download_manager, | 123 power_save_blocker.Pass(), observer), |
| 118 calculate_hash, | 124 source_url_(url), |
| 119 scoped_ptr<content::PowerSaveBlocker>(NULL).Pass(), | |
| 120 bound_net_log), | |
| 121 source_url_(info->url()), | |
| 122 error_info_(error_info), | 125 error_info_(error_info), |
| 123 destruction_callback_(dtor_callback) { | 126 destruction_callback_(dtor_callback) { |
| 124 ctor_callback.Run(source_url_, info->download_id); | 127 ctor_callback.Run(source_url_); |
| 125 } | 128 } |
| 126 | 129 |
| 127 DownloadFileWithErrors::~DownloadFileWithErrors() { | 130 DownloadFileWithErrors::~DownloadFileWithErrors() { |
| 128 destruction_callback_.Run(source_url_); | 131 destruction_callback_.Run(source_url_); |
| 129 } | 132 } |
| 130 | 133 |
| 131 content::DownloadInterruptReason DownloadFileWithErrors::Initialize() { | 134 void DownloadFileWithErrors::Initialize( |
| 132 return ShouldReturnError( | 135 const InitializeCallback& callback) { |
| 133 content::TestFileErrorInjector::FILE_OPERATION_INITIALIZE, | 136 content::DownloadInterruptReason error_to_return = |
| 134 DownloadFileImpl::Initialize()); | 137 content::DOWNLOAD_INTERRUPT_REASON_NONE; |
| 138 InitializeCallback callback_to_use = callback; |
| 139 |
| 140 // Replace callback if the error needs to be overwritten. |
| 141 if (OverwriteError( |
| 142 content::TestFileErrorInjector::FILE_OPERATION_INITIALIZE, |
| 143 &error_to_return)) { |
| 144 callback_to_use = base::Bind(&InitializeErrorCallback, callback, |
| 145 error_to_return); |
| 146 } |
| 147 |
| 148 DownloadFileImpl::Initialize(callback_to_use); |
| 135 } | 149 } |
| 136 | 150 |
| 137 content::DownloadInterruptReason DownloadFileWithErrors::AppendDataToFile( | 151 content::DownloadInterruptReason DownloadFileWithErrors::AppendDataToFile( |
| 138 const char* data, size_t data_len) { | 152 const char* data, size_t data_len) { |
| 139 return ShouldReturnError( | 153 return ShouldReturnError( |
| 140 content::TestFileErrorInjector::FILE_OPERATION_WRITE, | 154 content::TestFileErrorInjector::FILE_OPERATION_WRITE, |
| 141 DownloadFileImpl::AppendDataToFile(data, data_len)); | 155 DownloadFileImpl::AppendDataToFile(data, data_len)); |
| 142 } | 156 } |
| 143 | 157 |
| 144 void DownloadFileWithErrors::Rename( | 158 void DownloadFileWithErrors::Rename( |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 content::DownloadInterruptReason output_error = original_error; | 195 content::DownloadInterruptReason output_error = original_error; |
| 182 OverwriteError(code, &output_error); | 196 OverwriteError(code, &output_error); |
| 183 return output_error; | 197 return output_error; |
| 184 } | 198 } |
| 185 | 199 |
| 186 } // namespace | 200 } // namespace |
| 187 | 201 |
| 188 namespace content { | 202 namespace content { |
| 189 | 203 |
| 190 // A factory for constructing DownloadFiles that inject errors. | 204 // A factory for constructing DownloadFiles that inject errors. |
| 191 class DownloadFileWithErrorsFactory : public content::DownloadFileFactory { | 205 class DownloadFileWithErrorsFactory : public DownloadFileFactory { |
| 192 public: | 206 public: |
| 193 | |
| 194 DownloadFileWithErrorsFactory( | 207 DownloadFileWithErrorsFactory( |
| 195 const DownloadFileWithErrors::ConstructionCallback& ctor_callback, | 208 const DownloadFileWithErrors::ConstructionCallback& ctor_callback, |
| 196 const DownloadFileWithErrors::DestructionCallback& dtor_callback); | 209 const DownloadFileWithErrors::DestructionCallback& dtor_callback); |
| 197 virtual ~DownloadFileWithErrorsFactory(); | 210 virtual ~DownloadFileWithErrorsFactory(); |
| 198 | 211 |
| 199 // DownloadFileFactory interface. | 212 // DownloadFileFactory interface. |
| 200 virtual DownloadFile* CreateFile( | 213 virtual DownloadFile* CreateFile( |
| 201 DownloadCreateInfo* info, | 214 const content::DownloadSaveInfo& save_info, |
| 202 scoped_ptr<content::ByteStreamReader> stream, | 215 const FilePath& default_download_directory, |
| 203 content::DownloadManager* download_manager, | 216 GURL url, |
| 204 bool calculate_hash, | 217 GURL referrer_url, |
| 205 const net::BoundNetLog& bound_net_log); | 218 int64 received_bytes, |
| 219 bool calculate_hash, |
| 220 scoped_ptr<content::ByteStreamReader> stream, |
| 221 const net::BoundNetLog& bound_net_log, |
| 222 base::WeakPtr<content::DownloadDestinationObserver> observer) OVERRIDE; |
| 206 | 223 |
| 207 bool AddError( | 224 bool AddError( |
| 208 const TestFileErrorInjector::FileErrorInfo& error_info); | 225 const TestFileErrorInjector::FileErrorInfo& error_info); |
| 209 | 226 |
| 210 void ClearErrors(); | 227 void ClearErrors(); |
| 211 | 228 |
| 212 private: | 229 private: |
| 213 // Our injected error list, mapped by URL. One per file. | 230 // Our injected error list, mapped by URL. One per file. |
| 214 TestFileErrorInjector::ErrorMap injected_errors_; | 231 TestFileErrorInjector::ErrorMap injected_errors_; |
| 215 | 232 |
| 216 // Callback for creation and destruction. | 233 // Callback for creation and destruction. |
| 217 DownloadFileWithErrors::ConstructionCallback construction_callback_; | 234 DownloadFileWithErrors::ConstructionCallback construction_callback_; |
| 218 DownloadFileWithErrors::DestructionCallback destruction_callback_; | 235 DownloadFileWithErrors::DestructionCallback destruction_callback_; |
| 219 }; | 236 }; |
| 220 | 237 |
| 221 DownloadFileWithErrorsFactory::DownloadFileWithErrorsFactory( | 238 DownloadFileWithErrorsFactory::DownloadFileWithErrorsFactory( |
| 222 const DownloadFileWithErrors::ConstructionCallback& ctor_callback, | 239 const DownloadFileWithErrors::ConstructionCallback& ctor_callback, |
| 223 const DownloadFileWithErrors::DestructionCallback& dtor_callback) | 240 const DownloadFileWithErrors::DestructionCallback& dtor_callback) |
| 224 : construction_callback_(ctor_callback), | 241 : construction_callback_(ctor_callback), |
| 225 destruction_callback_(dtor_callback) { | 242 destruction_callback_(dtor_callback) { |
| 226 } | 243 } |
| 227 | 244 |
| 228 DownloadFileWithErrorsFactory::~DownloadFileWithErrorsFactory() { | 245 DownloadFileWithErrorsFactory::~DownloadFileWithErrorsFactory() { |
| 229 } | 246 } |
| 230 | 247 |
| 231 content::DownloadFile* DownloadFileWithErrorsFactory::CreateFile( | 248 content::DownloadFile* DownloadFileWithErrorsFactory::CreateFile( |
| 232 DownloadCreateInfo* info, | 249 const content::DownloadSaveInfo& save_info, |
| 250 const FilePath& default_download_directory, |
| 251 GURL url, |
| 252 GURL referrer_url, |
| 253 int64 received_bytes, |
| 254 bool calculate_hash, |
| 233 scoped_ptr<content::ByteStreamReader> stream, | 255 scoped_ptr<content::ByteStreamReader> stream, |
| 234 content::DownloadManager* download_manager, | 256 const net::BoundNetLog& bound_net_log, |
| 235 bool calculate_hash, | 257 base::WeakPtr<content::DownloadDestinationObserver> observer) { |
| 236 const net::BoundNetLog& bound_net_log) { | 258 if (injected_errors_.find(url.spec()) == injected_errors_.end()) { |
| 237 std::string url = info->url().spec(); | |
| 238 | |
| 239 if (injected_errors_.find(url) == injected_errors_.end()) { | |
| 240 // Have to create entry, because FileErrorInfo is not a POD type. | 259 // Have to create entry, because FileErrorInfo is not a POD type. |
| 241 TestFileErrorInjector::FileErrorInfo err_info = { | 260 TestFileErrorInjector::FileErrorInfo err_info = { |
| 242 url, | 261 url.spec(), |
| 243 TestFileErrorInjector::FILE_OPERATION_INITIALIZE, | 262 TestFileErrorInjector::FILE_OPERATION_INITIALIZE, |
| 244 -1, | 263 -1, |
| 245 content::DOWNLOAD_INTERRUPT_REASON_NONE | 264 content::DOWNLOAD_INTERRUPT_REASON_NONE |
| 246 }; | 265 }; |
| 247 injected_errors_[url] = err_info; | 266 injected_errors_[url.spec()] = err_info; |
| 248 } | 267 } |
| 249 | 268 |
| 269 scoped_ptr<content::PowerSaveBlocker> psb( |
| 270 new content::PowerSaveBlocker( |
| 271 content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, |
| 272 "Download in progress")); |
| 250 return new DownloadFileWithErrors( | 273 return new DownloadFileWithErrors( |
| 251 info, | 274 save_info, |
| 275 default_download_directory, |
| 276 url, |
| 277 referrer_url, |
| 278 received_bytes, |
| 279 calculate_hash, |
| 252 stream.Pass(), | 280 stream.Pass(), |
| 253 new DownloadRequestHandle(info->request_handle), | |
| 254 download_manager, | |
| 255 calculate_hash, | |
| 256 bound_net_log, | 281 bound_net_log, |
| 257 injected_errors_[url], | 282 psb.Pass(), |
| 283 observer, |
| 284 injected_errors_[url.spec()], |
| 258 construction_callback_, | 285 construction_callback_, |
| 259 destruction_callback_); | 286 destruction_callback_); |
| 260 } | 287 } |
| 261 | 288 |
| 262 bool DownloadFileWithErrorsFactory::AddError( | 289 bool DownloadFileWithErrorsFactory::AddError( |
| 263 const TestFileErrorInjector::FileErrorInfo& error_info) { | 290 const TestFileErrorInjector::FileErrorInfo& error_info) { |
| 264 // Creates an empty entry if necessary. Duplicate entries overwrite. | 291 // Creates an empty entry if necessary. Duplicate entries overwrite. |
| 265 injected_errors_[error_info.url] = error_info; | 292 injected_errors_[error_info.url] = error_info; |
| 266 | 293 |
| 267 return true; | 294 return true; |
| 268 } | 295 } |
| 269 | 296 |
| 270 void DownloadFileWithErrorsFactory::ClearErrors() { | 297 void DownloadFileWithErrorsFactory::ClearErrors() { |
| 271 injected_errors_.clear(); | 298 injected_errors_.clear(); |
| 272 } | 299 } |
| 273 | 300 |
| 274 TestFileErrorInjector::TestFileErrorInjector() | 301 TestFileErrorInjector::TestFileErrorInjector( |
| 275 : created_factory_(NULL) { | 302 scoped_refptr<content::DownloadManager> download_manager) |
| 303 : created_factory_(NULL), |
| 304 // This code is only used for browser_tests, so a |
| 305 // DownloadManager is always a DownloadManagerImpl. |
| 306 download_manager_( |
| 307 static_cast<DownloadManagerImpl*>(download_manager.release())) { |
| 276 // Record the value of the pointer, for later validation. | 308 // Record the value of the pointer, for later validation. |
| 277 created_factory_ = | 309 created_factory_ = |
| 278 new DownloadFileWithErrorsFactory( | 310 new DownloadFileWithErrorsFactory( |
| 279 base::Bind(&TestFileErrorInjector:: | 311 base::Bind(&TestFileErrorInjector::RecordDownloadFileConstruction, |
| 280 RecordDownloadFileConstruction, | |
| 281 this), | 312 this), |
| 282 base::Bind(&TestFileErrorInjector:: | 313 base::Bind(&TestFileErrorInjector::RecordDownloadFileDestruction, |
| 283 RecordDownloadFileDestruction, | |
| 284 this)); | 314 this)); |
| 285 | 315 |
| 286 // We will transfer ownership of the factory to the download file manager. | 316 // We will transfer ownership of the factory to the download manager. |
| 287 scoped_ptr<DownloadFileWithErrorsFactory> download_file_factory( | 317 scoped_ptr<DownloadFileFactory> download_file_factory( |
| 288 created_factory_); | 318 created_factory_); |
| 289 | 319 |
| 290 content::BrowserThread::PostTask( | 320 download_manager_->SetDownloadFileFactoryForTesting( |
| 291 content::BrowserThread::FILE, | 321 download_file_factory.Pass()); |
| 292 FROM_HERE, | |
| 293 base::Bind(&TestFileErrorInjector::AddFactory, | |
| 294 this, | |
| 295 base::Passed(&download_file_factory))); | |
| 296 } | 322 } |
| 297 | 323 |
| 298 TestFileErrorInjector::~TestFileErrorInjector() { | 324 TestFileErrorInjector::~TestFileErrorInjector() { |
| 299 } | 325 } |
| 300 | 326 |
| 301 void TestFileErrorInjector::AddFactory( | |
| 302 scoped_ptr<DownloadFileWithErrorsFactory> factory) { | |
| 303 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
| 304 | |
| 305 DownloadFileManager* download_file_manager = GetDownloadFileManager(); | |
| 306 DCHECK(download_file_manager); | |
| 307 | |
| 308 // Convert to base class pointer, for GCC. | |
| 309 scoped_ptr<content::DownloadFileFactory> plain_factory( | |
| 310 factory.release()); | |
| 311 | |
| 312 download_file_manager->SetFileFactoryForTesting(plain_factory.Pass()); | |
| 313 } | |
| 314 | |
| 315 bool TestFileErrorInjector::AddError(const FileErrorInfo& error_info) { | 327 bool TestFileErrorInjector::AddError(const FileErrorInfo& error_info) { |
| 316 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 328 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 317 DCHECK_LE(0, error_info.operation_instance); | 329 DCHECK_LE(0, error_info.operation_instance); |
| 318 DCHECK(injected_errors_.find(error_info.url) == injected_errors_.end()); | 330 DCHECK(injected_errors_.find(error_info.url) == injected_errors_.end()); |
| 319 | 331 |
| 320 // Creates an empty entry if necessary. | 332 // Creates an empty entry if necessary. |
| 321 injected_errors_[error_info.url] = error_info; | 333 injected_errors_[error_info.url] = error_info; |
| 322 | 334 |
| 323 return true; | 335 return true; |
| 324 } | 336 } |
| 325 | 337 |
| 326 void TestFileErrorInjector::ClearErrors() { | 338 void TestFileErrorInjector::ClearErrors() { |
| 327 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 339 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 328 injected_errors_.clear(); | 340 injected_errors_.clear(); |
| 329 } | 341 } |
| 330 | 342 |
| 331 bool TestFileErrorInjector::InjectErrors() { | 343 bool TestFileErrorInjector::InjectErrors() { |
| 332 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 344 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 333 | 345 |
| 334 ClearFoundFiles(); | 346 ClearFoundFiles(); |
| 335 | 347 |
| 336 content::BrowserThread::PostTask( | 348 DCHECK_EQ(static_cast<content::DownloadFileFactory*>(created_factory_), |
| 337 content::BrowserThread::FILE, | 349 download_manager_->GetDownloadFileFactoryForTesting()); |
| 338 FROM_HERE, | 350 |
| 339 base::Bind(&TestFileErrorInjector::InjectErrorsOnFileThread, | 351 created_factory_->ClearErrors(); |
| 340 this, | 352 |
| 341 injected_errors_, | 353 for (ErrorMap::const_iterator it = injected_errors_.begin(); |
| 342 created_factory_)); | 354 it != injected_errors_.end(); ++it) |
| 355 created_factory_->AddError(it->second); |
| 343 | 356 |
| 344 return true; | 357 return true; |
| 345 } | 358 } |
| 346 | 359 |
| 347 void TestFileErrorInjector::InjectErrorsOnFileThread( | |
| 348 ErrorMap map, DownloadFileWithErrorsFactory* factory) { | |
| 349 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
| 350 | |
| 351 // Validate that our factory is in use. | |
| 352 DownloadFileManager* download_file_manager = GetDownloadFileManager(); | |
| 353 DCHECK(download_file_manager); | |
| 354 | |
| 355 content::DownloadFileFactory* file_factory = | |
| 356 download_file_manager->GetFileFactoryForTesting(); | |
| 357 | |
| 358 // Validate that we still have the same factory. | |
| 359 DCHECK_EQ(static_cast<content::DownloadFileFactory*>(factory), | |
| 360 file_factory); | |
| 361 | |
| 362 // We want to replace all existing injection errors. | |
| 363 factory->ClearErrors(); | |
| 364 | |
| 365 for (ErrorMap::const_iterator it = map.begin(); it != map.end(); ++it) | |
| 366 factory->AddError(it->second); | |
| 367 } | |
| 368 | |
| 369 size_t TestFileErrorInjector::CurrentFileCount() const { | 360 size_t TestFileErrorInjector::CurrentFileCount() const { |
| 370 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 361 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 371 return files_.size(); | 362 return files_.size(); |
| 372 } | 363 } |
| 373 | 364 |
| 374 size_t TestFileErrorInjector::TotalFileCount() const { | 365 size_t TestFileErrorInjector::TotalFileCount() const { |
| 375 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 366 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 376 return found_files_.size(); | 367 return found_files_.size(); |
| 377 } | 368 } |
| 378 | 369 |
| 379 | 370 |
| 380 bool TestFileErrorInjector::HadFile(const GURL& url) const { | 371 bool TestFileErrorInjector::HadFile(const GURL& url) const { |
| 381 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 372 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 382 | 373 |
| 383 return (found_files_.find(url) != found_files_.end()); | 374 return (found_files_.find(url) != found_files_.end()); |
| 384 } | 375 } |
| 385 | 376 |
| 386 const content::DownloadId TestFileErrorInjector::GetId( | |
| 387 const GURL& url) const { | |
| 388 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 389 | |
| 390 FileMap::const_iterator it = found_files_.find(url); | |
| 391 if (it == found_files_.end()) | |
| 392 return content::DownloadId::Invalid(); | |
| 393 | |
| 394 return it->second; | |
| 395 } | |
| 396 | |
| 397 void TestFileErrorInjector::ClearFoundFiles() { | 377 void TestFileErrorInjector::ClearFoundFiles() { |
| 398 found_files_.clear(); | 378 found_files_.clear(); |
| 399 } | 379 } |
| 400 | 380 |
| 401 void TestFileErrorInjector::DownloadFileCreated(GURL url, | 381 void TestFileErrorInjector::DownloadFileCreated(GURL url) { |
| 402 content::DownloadId id) { | |
| 403 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 382 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 404 DCHECK(files_.find(url) == files_.end()); | 383 DCHECK(files_.find(url) == files_.end()); |
| 405 | 384 |
| 406 files_[url] = id; | 385 files_.insert(url); |
| 407 found_files_[url] = id; | 386 found_files_.insert(url); |
| 408 } | 387 } |
| 409 | 388 |
| 410 void TestFileErrorInjector::DestroyingDownloadFile(GURL url) { | 389 void TestFileErrorInjector::DestroyingDownloadFile(GURL url) { |
| 411 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 390 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 412 DCHECK(files_.find(url) != files_.end()); | 391 DCHECK(files_.find(url) != files_.end()); |
| 413 | 392 |
| 414 files_.erase(url); | 393 files_.erase(url); |
| 415 } | 394 } |
| 416 | 395 |
| 417 void TestFileErrorInjector::RecordDownloadFileConstruction( | 396 void TestFileErrorInjector::RecordDownloadFileConstruction(const GURL& url) { |
| 418 const GURL& url, content::DownloadId id) { | |
| 419 content::BrowserThread::PostTask( | 397 content::BrowserThread::PostTask( |
| 420 content::BrowserThread::UI, | 398 content::BrowserThread::UI, |
| 421 FROM_HERE, | 399 FROM_HERE, |
| 422 base::Bind(&TestFileErrorInjector::DownloadFileCreated, | 400 base::Bind(&TestFileErrorInjector::DownloadFileCreated, |
| 423 this, | 401 this, |
| 424 url, | 402 url)); |
| 425 id)); | |
| 426 } | 403 } |
| 427 | 404 |
| 428 void TestFileErrorInjector::RecordDownloadFileDestruction(const GURL& url) { | 405 void TestFileErrorInjector::RecordDownloadFileDestruction(const GURL& url) { |
| 429 content::BrowserThread::PostTask( | 406 content::BrowserThread::PostTask( |
| 430 content::BrowserThread::UI, | 407 content::BrowserThread::UI, |
| 431 FROM_HERE, | 408 FROM_HERE, |
| 432 base::Bind(&TestFileErrorInjector::DestroyingDownloadFile, | 409 base::Bind(&TestFileErrorInjector::DestroyingDownloadFile, |
| 433 this, | 410 this, |
| 434 url)); | 411 url)); |
| 435 } | 412 } |
| 436 | 413 |
| 437 // static | 414 // static |
| 438 scoped_refptr<TestFileErrorInjector> TestFileErrorInjector::Create() { | 415 scoped_refptr<TestFileErrorInjector> TestFileErrorInjector::Create( |
| 416 scoped_refptr<content::DownloadManager> download_manager) { |
| 439 static bool visited = false; | 417 static bool visited = false; |
| 440 DCHECK(!visited); // Only allowed to be called once. | 418 DCHECK(!visited); // Only allowed to be called once. |
| 441 visited = true; | 419 visited = true; |
| 442 | 420 |
| 443 scoped_refptr<TestFileErrorInjector> single_injector( | 421 scoped_refptr<TestFileErrorInjector> single_injector( |
| 444 new TestFileErrorInjector); | 422 new TestFileErrorInjector(download_manager)); |
| 445 | 423 |
| 446 return single_injector; | 424 return single_injector; |
| 447 } | 425 } |
| 448 | 426 |
| 449 // static | 427 // static |
| 450 std::string TestFileErrorInjector::DebugString(FileOperationCode code) { | 428 std::string TestFileErrorInjector::DebugString(FileOperationCode code) { |
| 451 switch (code) { | 429 switch (code) { |
| 452 case FILE_OPERATION_INITIALIZE: | 430 case FILE_OPERATION_INITIALIZE: |
| 453 return "INITIALIZE"; | 431 return "INITIALIZE"; |
| 454 case FILE_OPERATION_WRITE: | 432 case FILE_OPERATION_WRITE: |
| 455 return "WRITE"; | 433 return "WRITE"; |
| 456 case FILE_OPERATION_RENAME: | 434 case FILE_OPERATION_RENAME: |
| 457 return "RENAME"; | 435 return "RENAME"; |
| 458 default: | 436 default: |
| 459 break; | 437 break; |
| 460 } | 438 } |
| 461 | 439 |
| 462 return "Unknown"; | 440 return "Unknown"; |
| 463 } | 441 } |
| 464 | 442 |
| 465 } // namespace content | 443 } // namespace content |
| OLD | NEW |