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