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