OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/public/test/test_file_error_injector.h" | 5 #include "content/public/test/test_file_error_injector.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
| 10 #include "base/callback.h" |
10 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "content/browser/download/download_file_factory.h" | 13 #include "content/browser/download/download_file_factory.h" |
13 #include "content/browser/download/download_file_impl.h" | 14 #include "content/browser/download/download_file_impl.h" |
14 #include "content/browser/download/download_interrupt_reasons_impl.h" | 15 #include "content/browser/download/download_interrupt_reasons_impl.h" |
15 #include "content/browser/download/download_manager_impl.h" | 16 #include "content/browser/download/download_manager_impl.h" |
16 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 17 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
19 | 20 |
20 namespace content { | 21 namespace content { |
21 class ByteStreamReader; | 22 class ByteStreamReader; |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 // A class that performs file operations and injects errors. | 26 // A class that performs file operations and injects errors. |
26 class DownloadFileWithErrors: public DownloadFileImpl { | 27 class DownloadFileWithError: public DownloadFileImpl { |
27 public: | 28 public: |
28 typedef base::Callback<void(const GURL& url)> ConstructionCallback; | 29 DownloadFileWithError(const DownloadSaveInfo& save_info, |
29 typedef base::Callback<void(const GURL& url)> DestructionCallback; | |
30 | |
31 DownloadFileWithErrors(const DownloadSaveInfo& save_info, | |
32 const base::FilePath& default_download_directory, | 30 const base::FilePath& default_download_directory, |
33 const GURL& url, | 31 const GURL& url, |
34 const GURL& referrer_url, | 32 const GURL& referrer_url, |
35 bool calculate_hash, | 33 bool calculate_hash, |
36 base::File file, | 34 base::File file, |
37 scoped_ptr<ByteStreamReader> byte_stream, | 35 scoped_ptr<ByteStreamReader> byte_stream, |
38 const net::BoundNetLog& bound_net_log, | 36 const net::BoundNetLog& bound_net_log, |
39 base::WeakPtr<DownloadDestinationObserver> observer, | 37 base::WeakPtr<DownloadDestinationObserver> observer, |
40 const TestFileErrorInjector::FileErrorInfo& error_info, | 38 const TestFileErrorInjector::FileErrorInfo& error_info, |
41 const ConstructionCallback& ctor_callback, | 39 const base::Closure& ctor_callback, |
42 const DestructionCallback& dtor_callback); | 40 const base::Closure& dtor_callback); |
43 | 41 |
44 ~DownloadFileWithErrors() override; | 42 ~DownloadFileWithError() override; |
45 | 43 |
46 void Initialize(const InitializeCallback& callback) override; | 44 void Initialize(const InitializeCallback& callback) override; |
47 | 45 |
48 // DownloadFile interface. | 46 // DownloadFile interface. |
49 DownloadInterruptReason AppendDataToFile(const char* data, | 47 DownloadInterruptReason AppendDataToFile(const char* data, |
50 size_t data_len) override; | 48 size_t data_len) override; |
51 void RenameAndUniquify(const base::FilePath& full_path, | 49 void RenameAndUniquify(const base::FilePath& full_path, |
52 const RenameCompletionCallback& callback) override; | 50 const RenameCompletionCallback& callback) override; |
53 void RenameAndAnnotate(const base::FilePath& full_path, | 51 void RenameAndAnnotate(const base::FilePath& full_path, |
54 const RenameCompletionCallback& callback) override; | 52 const RenameCompletionCallback& callback) override; |
55 | 53 |
56 private: | 54 private: |
57 // Error generating helper. | 55 // Error generating helper. |
58 DownloadInterruptReason ShouldReturnError( | 56 DownloadInterruptReason ShouldReturnError( |
59 TestFileErrorInjector::FileOperationCode code, | 57 TestFileErrorInjector::FileOperationCode code, |
60 DownloadInterruptReason original_error); | 58 DownloadInterruptReason original_error); |
61 | 59 |
62 // Determine whether to overwrite an operation with the given code | 60 // Determine whether to overwrite an operation with the given code |
63 // with a substitute error; if returns true, |*original_error| is | 61 // with a substitute error; if returns true, |*original_error| is |
64 // written with the error to use for overwriting. | 62 // written with the error to use for overwriting. |
65 // NOTE: This routine changes state; specifically, it increases the | 63 // NOTE: This routine changes state; specifically, it increases the |
66 // operations counts for the specified code. It should only be called | 64 // operations counts for the specified code. It should only be called |
67 // once per operation. | 65 // once per operation. |
68 bool OverwriteError( | 66 bool OverwriteError( |
69 TestFileErrorInjector::FileOperationCode code, | 67 TestFileErrorInjector::FileOperationCode code, |
70 DownloadInterruptReason* output_error); | 68 DownloadInterruptReason* output_error); |
71 | 69 |
72 // Source URL for the file being downloaded. | |
73 GURL source_url_; | |
74 | |
75 // Our injected error. Only one per file. | 70 // Our injected error. Only one per file. |
76 TestFileErrorInjector::FileErrorInfo error_info_; | 71 TestFileErrorInjector::FileErrorInfo error_info_; |
77 | 72 |
78 // Count per operation. 0-based. | 73 // Count per operation. 0-based. |
79 std::map<TestFileErrorInjector::FileOperationCode, int> operation_counter_; | 74 std::map<TestFileErrorInjector::FileOperationCode, int> operation_counter_; |
80 | 75 |
81 // Callback for destruction. | 76 // Callback for destruction. |
82 DestructionCallback destruction_callback_; | 77 base::Closure destruction_callback_; |
83 }; | 78 }; |
84 | 79 |
85 static void InitializeErrorCallback( | 80 static void InitializeErrorCallback( |
86 const DownloadFile::InitializeCallback original_callback, | 81 const DownloadFile::InitializeCallback original_callback, |
87 DownloadInterruptReason overwrite_error, | 82 DownloadInterruptReason overwrite_error, |
88 DownloadInterruptReason original_error) { | 83 DownloadInterruptReason original_error) { |
89 original_callback.Run(overwrite_error); | 84 original_callback.Run(overwrite_error); |
90 } | 85 } |
91 | 86 |
92 static void RenameErrorCallback( | 87 static void RenameErrorCallback( |
93 const DownloadFile::RenameCompletionCallback original_callback, | 88 const DownloadFile::RenameCompletionCallback original_callback, |
94 DownloadInterruptReason overwrite_error, | 89 DownloadInterruptReason overwrite_error, |
95 DownloadInterruptReason original_error, | 90 DownloadInterruptReason original_error, |
96 const base::FilePath& path_result) { | 91 const base::FilePath& path_result) { |
97 original_callback.Run( | 92 original_callback.Run( |
98 overwrite_error, | 93 overwrite_error, |
99 overwrite_error == DOWNLOAD_INTERRUPT_REASON_NONE ? | 94 overwrite_error == DOWNLOAD_INTERRUPT_REASON_NONE ? |
100 path_result : base::FilePath()); | 95 path_result : base::FilePath()); |
101 } | 96 } |
102 | 97 |
103 DownloadFileWithErrors::DownloadFileWithErrors( | 98 DownloadFileWithError::DownloadFileWithError( |
104 const DownloadSaveInfo& save_info, | 99 const DownloadSaveInfo& save_info, |
105 const base::FilePath& default_download_directory, | 100 const base::FilePath& default_download_directory, |
106 const GURL& url, | 101 const GURL& url, |
107 const GURL& referrer_url, | 102 const GURL& referrer_url, |
108 bool calculate_hash, | 103 bool calculate_hash, |
109 base::File file, | 104 base::File file, |
110 scoped_ptr<ByteStreamReader> byte_stream, | 105 scoped_ptr<ByteStreamReader> byte_stream, |
111 const net::BoundNetLog& bound_net_log, | 106 const net::BoundNetLog& bound_net_log, |
112 base::WeakPtr<DownloadDestinationObserver> observer, | 107 base::WeakPtr<DownloadDestinationObserver> observer, |
113 const TestFileErrorInjector::FileErrorInfo& error_info, | 108 const TestFileErrorInjector::FileErrorInfo& error_info, |
114 const ConstructionCallback& ctor_callback, | 109 const base::Closure& ctor_callback, |
115 const DestructionCallback& dtor_callback) | 110 const base::Closure& dtor_callback) |
116 : DownloadFileImpl(save_info, | 111 : DownloadFileImpl(save_info, |
117 default_download_directory, | 112 default_download_directory, |
118 url, | 113 url, |
119 referrer_url, | 114 referrer_url, |
120 calculate_hash, | 115 calculate_hash, |
121 std::move(file), | 116 std::move(file), |
122 std::move(byte_stream), | 117 std::move(byte_stream), |
123 bound_net_log, | 118 bound_net_log, |
124 observer), | 119 observer), |
125 source_url_(url), | |
126 error_info_(error_info), | 120 error_info_(error_info), |
127 destruction_callback_(dtor_callback) { | 121 destruction_callback_(dtor_callback) { |
128 // DownloadFiles are created on the UI thread and are destroyed on the FILE | 122 // DownloadFiles are created on the UI thread and are destroyed on the FILE |
129 // thread. Schedule the ConstructionCallback on the FILE thread so that if a | 123 // thread. Schedule the ConstructionCallback on the FILE thread so that if a |
130 // DownloadItem schedules a DownloadFile to be destroyed and creates another | 124 // DownloadItem schedules a DownloadFile to be destroyed and creates another |
131 // one (as happens during download resumption), then the DestructionCallback | 125 // one (as happens during download resumption), then the DestructionCallback |
132 // for the old DownloadFile is run before the ConstructionCallback for the | 126 // for the old DownloadFile is run before the ConstructionCallback for the |
133 // next DownloadFile. | 127 // next DownloadFile. |
134 BrowserThread::PostTask( | 128 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, ctor_callback); |
135 BrowserThread::FILE, | |
136 FROM_HERE, | |
137 base::Bind(ctor_callback, source_url_)); | |
138 } | 129 } |
139 | 130 |
140 DownloadFileWithErrors::~DownloadFileWithErrors() { | 131 DownloadFileWithError::~DownloadFileWithError() { |
141 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 132 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
142 destruction_callback_.Run(source_url_); | 133 destruction_callback_.Run(); |
143 } | 134 } |
144 | 135 |
145 void DownloadFileWithErrors::Initialize( | 136 void DownloadFileWithError::Initialize( |
146 const InitializeCallback& callback) { | 137 const InitializeCallback& callback) { |
147 DownloadInterruptReason error_to_return = DOWNLOAD_INTERRUPT_REASON_NONE; | 138 DownloadInterruptReason error_to_return = DOWNLOAD_INTERRUPT_REASON_NONE; |
148 InitializeCallback callback_to_use = callback; | 139 InitializeCallback callback_to_use = callback; |
149 | 140 |
150 // Replace callback if the error needs to be overwritten. | 141 // Replace callback if the error needs to be overwritten. |
151 if (OverwriteError( | 142 if (OverwriteError( |
152 TestFileErrorInjector::FILE_OPERATION_INITIALIZE, | 143 TestFileErrorInjector::FILE_OPERATION_INITIALIZE, |
153 &error_to_return)) { | 144 &error_to_return)) { |
154 if (DOWNLOAD_INTERRUPT_REASON_NONE != error_to_return) { | 145 if (DOWNLOAD_INTERRUPT_REASON_NONE != error_to_return) { |
155 // Don't execute a, probably successful, Initialize; just | 146 // Don't execute a, probably successful, Initialize; just |
156 // return the error. | 147 // return the error. |
157 BrowserThread::PostTask( | 148 BrowserThread::PostTask( |
158 BrowserThread::UI, FROM_HERE, base::Bind( | 149 BrowserThread::UI, FROM_HERE, base::Bind( |
159 callback, error_to_return)); | 150 callback, error_to_return)); |
160 return; | 151 return; |
161 } | 152 } |
162 | 153 |
163 // Otherwise, just wrap the return. | 154 // Otherwise, just wrap the return. |
164 callback_to_use = base::Bind(&InitializeErrorCallback, callback, | 155 callback_to_use = base::Bind(&InitializeErrorCallback, callback, |
165 error_to_return); | 156 error_to_return); |
166 } | 157 } |
167 | 158 |
168 DownloadFileImpl::Initialize(callback_to_use); | 159 DownloadFileImpl::Initialize(callback_to_use); |
169 } | 160 } |
170 | 161 |
171 DownloadInterruptReason DownloadFileWithErrors::AppendDataToFile( | 162 DownloadInterruptReason DownloadFileWithError::AppendDataToFile( |
172 const char* data, size_t data_len) { | 163 const char* data, size_t data_len) { |
173 return ShouldReturnError( | 164 return ShouldReturnError( |
174 TestFileErrorInjector::FILE_OPERATION_WRITE, | 165 TestFileErrorInjector::FILE_OPERATION_WRITE, |
175 DownloadFileImpl::AppendDataToFile(data, data_len)); | 166 DownloadFileImpl::AppendDataToFile(data, data_len)); |
176 } | 167 } |
177 | 168 |
178 void DownloadFileWithErrors::RenameAndUniquify( | 169 void DownloadFileWithError::RenameAndUniquify( |
179 const base::FilePath& full_path, | 170 const base::FilePath& full_path, |
180 const RenameCompletionCallback& callback) { | 171 const RenameCompletionCallback& callback) { |
181 DownloadInterruptReason error_to_return = DOWNLOAD_INTERRUPT_REASON_NONE; | 172 DownloadInterruptReason error_to_return = DOWNLOAD_INTERRUPT_REASON_NONE; |
182 RenameCompletionCallback callback_to_use = callback; | 173 RenameCompletionCallback callback_to_use = callback; |
183 | 174 |
184 // Replace callback if the error needs to be overwritten. | 175 // Replace callback if the error needs to be overwritten. |
185 if (OverwriteError( | 176 if (OverwriteError( |
186 TestFileErrorInjector::FILE_OPERATION_RENAME_UNIQUIFY, | 177 TestFileErrorInjector::FILE_OPERATION_RENAME_UNIQUIFY, |
187 &error_to_return)) { | 178 &error_to_return)) { |
188 if (DOWNLOAD_INTERRUPT_REASON_NONE != error_to_return) { | 179 if (DOWNLOAD_INTERRUPT_REASON_NONE != error_to_return) { |
189 // Don't execute a, probably successful, RenameAndUniquify; just | 180 // Don't execute a, probably successful, RenameAndUniquify; just |
190 // return the error. | 181 // return the error. |
191 BrowserThread::PostTask( | 182 BrowserThread::PostTask( |
192 BrowserThread::UI, FROM_HERE, base::Bind( | 183 BrowserThread::UI, FROM_HERE, base::Bind( |
193 callback, error_to_return, base::FilePath())); | 184 callback, error_to_return, base::FilePath())); |
194 return; | 185 return; |
195 } | 186 } |
196 | 187 |
197 // Otherwise, just wrap the return. | 188 // Otherwise, just wrap the return. |
198 callback_to_use = base::Bind(&RenameErrorCallback, callback, | 189 callback_to_use = base::Bind(&RenameErrorCallback, callback, |
199 error_to_return); | 190 error_to_return); |
200 } | 191 } |
201 | 192 |
202 DownloadFileImpl::RenameAndUniquify(full_path, callback_to_use); | 193 DownloadFileImpl::RenameAndUniquify(full_path, callback_to_use); |
203 } | 194 } |
204 | 195 |
205 void DownloadFileWithErrors::RenameAndAnnotate( | 196 void DownloadFileWithError::RenameAndAnnotate( |
206 const base::FilePath& full_path, | 197 const base::FilePath& full_path, |
207 const RenameCompletionCallback& callback) { | 198 const RenameCompletionCallback& callback) { |
208 DownloadInterruptReason error_to_return = DOWNLOAD_INTERRUPT_REASON_NONE; | 199 DownloadInterruptReason error_to_return = DOWNLOAD_INTERRUPT_REASON_NONE; |
209 RenameCompletionCallback callback_to_use = callback; | 200 RenameCompletionCallback callback_to_use = callback; |
210 | 201 |
211 // Replace callback if the error needs to be overwritten. | 202 // Replace callback if the error needs to be overwritten. |
212 if (OverwriteError( | 203 if (OverwriteError( |
213 TestFileErrorInjector::FILE_OPERATION_RENAME_ANNOTATE, | 204 TestFileErrorInjector::FILE_OPERATION_RENAME_ANNOTATE, |
214 &error_to_return)) { | 205 &error_to_return)) { |
215 if (DOWNLOAD_INTERRUPT_REASON_NONE != error_to_return) { | 206 if (DOWNLOAD_INTERRUPT_REASON_NONE != error_to_return) { |
216 // Don't execute a, probably successful, RenameAndAnnotate; just | 207 // Don't execute a, probably successful, RenameAndAnnotate; just |
217 // return the error. | 208 // return the error. |
218 BrowserThread::PostTask( | 209 BrowserThread::PostTask( |
219 BrowserThread::UI, FROM_HERE, base::Bind( | 210 BrowserThread::UI, FROM_HERE, base::Bind( |
220 callback, error_to_return, base::FilePath())); | 211 callback, error_to_return, base::FilePath())); |
221 return; | 212 return; |
222 } | 213 } |
223 | 214 |
224 // Otherwise, just wrap the return. | 215 // Otherwise, just wrap the return. |
225 callback_to_use = base::Bind(&RenameErrorCallback, callback, | 216 callback_to_use = base::Bind(&RenameErrorCallback, callback, |
226 error_to_return); | 217 error_to_return); |
227 } | 218 } |
228 | 219 |
229 DownloadFileImpl::RenameAndAnnotate(full_path, callback_to_use); | 220 DownloadFileImpl::RenameAndAnnotate(full_path, callback_to_use); |
230 } | 221 } |
231 | 222 |
232 bool DownloadFileWithErrors::OverwriteError( | 223 bool DownloadFileWithError::OverwriteError( |
233 TestFileErrorInjector::FileOperationCode code, | 224 TestFileErrorInjector::FileOperationCode code, |
234 DownloadInterruptReason* output_error) { | 225 DownloadInterruptReason* output_error) { |
235 int counter = operation_counter_[code]++; | 226 int counter = operation_counter_[code]++; |
236 | 227 |
237 if (code != error_info_.code) | 228 if (code != error_info_.code) |
238 return false; | 229 return false; |
239 | 230 |
240 if (counter != error_info_.operation_instance) | 231 if (counter != error_info_.operation_instance) |
241 return false; | 232 return false; |
242 | 233 |
243 *output_error = error_info_.error; | 234 *output_error = error_info_.error; |
244 return true; | 235 return true; |
245 } | 236 } |
246 | 237 |
247 DownloadInterruptReason DownloadFileWithErrors::ShouldReturnError( | 238 DownloadInterruptReason DownloadFileWithError::ShouldReturnError( |
248 TestFileErrorInjector::FileOperationCode code, | 239 TestFileErrorInjector::FileOperationCode code, |
249 DownloadInterruptReason original_error) { | 240 DownloadInterruptReason original_error) { |
250 DownloadInterruptReason output_error = original_error; | 241 DownloadInterruptReason output_error = original_error; |
251 OverwriteError(code, &output_error); | 242 OverwriteError(code, &output_error); |
252 return output_error; | 243 return output_error; |
253 } | 244 } |
254 | 245 |
255 } // namespace | 246 } // namespace |
256 | 247 |
257 // A factory for constructing DownloadFiles that inject errors. | 248 // A factory for constructing DownloadFiles that inject errors. |
258 class DownloadFileWithErrorsFactory : public DownloadFileFactory { | 249 class DownloadFileWithErrorFactory : public DownloadFileFactory { |
259 public: | 250 public: |
260 DownloadFileWithErrorsFactory( | 251 DownloadFileWithErrorFactory(const base::Closure& ctor_callback, |
261 const DownloadFileWithErrors::ConstructionCallback& ctor_callback, | 252 const base::Closure& dtor_callback); |
262 const DownloadFileWithErrors::DestructionCallback& dtor_callback); | 253 ~DownloadFileWithErrorFactory() override; |
263 ~DownloadFileWithErrorsFactory() override; | |
264 | 254 |
265 // DownloadFileFactory interface. | 255 // DownloadFileFactory interface. |
266 DownloadFile* CreateFile( | 256 DownloadFile* CreateFile( |
267 const DownloadSaveInfo& save_info, | 257 const DownloadSaveInfo& save_info, |
268 const base::FilePath& default_download_directory, | 258 const base::FilePath& default_download_directory, |
269 const GURL& url, | 259 const GURL& url, |
270 const GURL& referrer_url, | 260 const GURL& referrer_url, |
271 bool calculate_hash, | 261 bool calculate_hash, |
272 base::File file, | 262 base::File file, |
273 scoped_ptr<ByteStreamReader> byte_stream, | 263 scoped_ptr<ByteStreamReader> byte_stream, |
274 const net::BoundNetLog& bound_net_log, | 264 const net::BoundNetLog& bound_net_log, |
275 base::WeakPtr<DownloadDestinationObserver> observer) override; | 265 base::WeakPtr<DownloadDestinationObserver> observer) override; |
276 | 266 |
277 bool AddError( | 267 void ClearError(); |
278 const TestFileErrorInjector::FileErrorInfo& error_info); | 268 bool SetError(TestFileErrorInjector::FileErrorInfo error); |
279 | |
280 void ClearErrors(); | |
281 | 269 |
282 private: | 270 private: |
283 // Our injected error list, mapped by URL. One per file. | 271 // Our injected error. |
284 TestFileErrorInjector::ErrorMap injected_errors_; | 272 TestFileErrorInjector::FileErrorInfo injected_error_; |
285 | 273 |
286 // Callback for creation and destruction. | 274 // Callback for creation and destruction. |
287 DownloadFileWithErrors::ConstructionCallback construction_callback_; | 275 base::Closure construction_callback_; |
288 DownloadFileWithErrors::DestructionCallback destruction_callback_; | 276 base::Closure destruction_callback_; |
289 }; | 277 }; |
290 | 278 |
291 DownloadFileWithErrorsFactory::DownloadFileWithErrorsFactory( | 279 DownloadFileWithErrorFactory::DownloadFileWithErrorFactory( |
292 const DownloadFileWithErrors::ConstructionCallback& ctor_callback, | 280 const base::Closure& ctor_callback, |
293 const DownloadFileWithErrors::DestructionCallback& dtor_callback) | 281 const base::Closure& dtor_callback) |
294 : construction_callback_(ctor_callback), | 282 : construction_callback_(ctor_callback), |
295 destruction_callback_(dtor_callback) { | 283 destruction_callback_(dtor_callback) { |
| 284 injected_error_.code = TestFileErrorInjector::FILE_OPERATION_INITIALIZE; |
| 285 injected_error_.error = DOWNLOAD_INTERRUPT_REASON_NONE; |
| 286 injected_error_.operation_instance = -1; |
296 } | 287 } |
297 | 288 |
298 DownloadFileWithErrorsFactory::~DownloadFileWithErrorsFactory() { | 289 DownloadFileWithErrorFactory::~DownloadFileWithErrorFactory() {} |
299 } | |
300 | 290 |
301 DownloadFile* DownloadFileWithErrorsFactory::CreateFile( | 291 DownloadFile* DownloadFileWithErrorFactory::CreateFile( |
302 const DownloadSaveInfo& save_info, | 292 const DownloadSaveInfo& save_info, |
303 const base::FilePath& default_download_directory, | 293 const base::FilePath& default_download_directory, |
304 const GURL& url, | 294 const GURL& url, |
305 const GURL& referrer_url, | 295 const GURL& referrer_url, |
306 bool calculate_hash, | 296 bool calculate_hash, |
307 base::File file, | 297 base::File file, |
308 scoped_ptr<ByteStreamReader> byte_stream, | 298 scoped_ptr<ByteStreamReader> byte_stream, |
309 const net::BoundNetLog& bound_net_log, | 299 const net::BoundNetLog& bound_net_log, |
310 base::WeakPtr<DownloadDestinationObserver> observer) { | 300 base::WeakPtr<DownloadDestinationObserver> observer) { |
311 if (injected_errors_.find(url.spec()) == injected_errors_.end()) { | 301 return new DownloadFileWithError( |
312 // Have to create entry, because FileErrorInfo is not a POD type. | |
313 TestFileErrorInjector::FileErrorInfo err_info = { | |
314 url.spec(), | |
315 TestFileErrorInjector::FILE_OPERATION_INITIALIZE, | |
316 -1, | |
317 DOWNLOAD_INTERRUPT_REASON_NONE | |
318 }; | |
319 injected_errors_[url.spec()] = err_info; | |
320 } | |
321 | |
322 return new DownloadFileWithErrors( | |
323 save_info, default_download_directory, url, referrer_url, calculate_hash, | 302 save_info, default_download_directory, url, referrer_url, calculate_hash, |
324 std::move(file), std::move(byte_stream), bound_net_log, observer, | 303 std::move(file), std::move(byte_stream), bound_net_log, observer, |
325 injected_errors_[url.spec()], construction_callback_, | 304 injected_error_, construction_callback_, destruction_callback_); |
326 destruction_callback_); | |
327 } | 305 } |
328 | 306 |
329 bool DownloadFileWithErrorsFactory::AddError( | 307 bool DownloadFileWithErrorFactory::SetError( |
330 const TestFileErrorInjector::FileErrorInfo& error_info) { | 308 TestFileErrorInjector::FileErrorInfo error) { |
331 // Creates an empty entry if necessary. Duplicate entries overwrite. | 309 injected_error_ = std::move(error); |
332 injected_errors_[error_info.url] = error_info; | |
333 | |
334 return true; | 310 return true; |
335 } | 311 } |
336 | 312 |
337 void DownloadFileWithErrorsFactory::ClearErrors() { | 313 TestFileErrorInjector::TestFileErrorInjector(DownloadManager* download_manager) |
338 injected_errors_.clear(); | 314 : // This code is only used for browser_tests, so a |
339 } | |
340 | |
341 TestFileErrorInjector::TestFileErrorInjector( | |
342 DownloadManager* download_manager) | |
343 : created_factory_(NULL), | |
344 // This code is only used for browser_tests, so a | |
345 // DownloadManager is always a DownloadManagerImpl. | 315 // DownloadManager is always a DownloadManagerImpl. |
346 download_manager_(static_cast<DownloadManagerImpl*>(download_manager)) { | 316 download_manager_(static_cast<DownloadManagerImpl*>(download_manager)) { |
347 // Record the value of the pointer, for later validation. | 317 // Record the value of the pointer, for later validation. |
348 created_factory_ = | 318 created_factory_ = |
349 new DownloadFileWithErrorsFactory( | 319 new DownloadFileWithErrorFactory( |
350 base::Bind(&TestFileErrorInjector::RecordDownloadFileConstruction, | 320 base::Bind(&TestFileErrorInjector::RecordDownloadFileConstruction, |
351 this), | 321 this), |
352 base::Bind(&TestFileErrorInjector::RecordDownloadFileDestruction, | 322 base::Bind(&TestFileErrorInjector::RecordDownloadFileDestruction, |
353 this)); | 323 this)); |
354 | 324 |
355 // We will transfer ownership of the factory to the download manager. | 325 // We will transfer ownership of the factory to the download manager. |
356 scoped_ptr<DownloadFileFactory> download_file_factory( | 326 scoped_ptr<DownloadFileFactory> download_file_factory( |
357 created_factory_); | 327 created_factory_); |
358 | 328 |
359 download_manager_->SetDownloadFileFactoryForTesting( | 329 download_manager_->SetDownloadFileFactoryForTesting( |
360 std::move(download_file_factory)); | 330 std::move(download_file_factory)); |
361 } | 331 } |
362 | 332 |
363 TestFileErrorInjector::~TestFileErrorInjector() { | 333 TestFileErrorInjector::~TestFileErrorInjector() { |
364 } | 334 } |
365 | 335 |
366 bool TestFileErrorInjector::AddError(const FileErrorInfo& error_info) { | 336 void TestFileErrorInjector::ClearError() { |
367 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 337 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
368 DCHECK_LE(0, error_info.operation_instance); | 338 // An error with an index of -1, which will never be reached. |
369 DCHECK(injected_errors_.find(error_info.url) == injected_errors_.end()); | 339 static const TestFileErrorInjector::FileErrorInfo kNoOpErrorInfo = { |
370 | 340 TestFileErrorInjector::FILE_OPERATION_INITIALIZE, -1, |
371 // Creates an empty entry if necessary. | 341 DOWNLOAD_INTERRUPT_REASON_NONE}; |
372 injected_errors_[error_info.url] = error_info; | 342 InjectError(kNoOpErrorInfo); |
373 | |
374 return true; | |
375 } | 343 } |
376 | 344 |
377 void TestFileErrorInjector::ClearErrors() { | 345 bool TestFileErrorInjector::InjectError(const FileErrorInfo& error_info) { |
378 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 346 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
379 injected_errors_.clear(); | 347 ClearTotalFileCount(); |
380 } | |
381 | |
382 bool TestFileErrorInjector::InjectErrors() { | |
383 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
384 | |
385 ClearFoundFiles(); | |
386 | |
387 DCHECK_EQ(static_cast<DownloadFileFactory*>(created_factory_), | 348 DCHECK_EQ(static_cast<DownloadFileFactory*>(created_factory_), |
388 download_manager_->GetDownloadFileFactoryForTesting()); | 349 download_manager_->GetDownloadFileFactoryForTesting()); |
389 | 350 created_factory_->SetError(error_info); |
390 created_factory_->ClearErrors(); | |
391 | |
392 for (ErrorMap::const_iterator it = injected_errors_.begin(); | |
393 it != injected_errors_.end(); ++it) | |
394 created_factory_->AddError(it->second); | |
395 | |
396 return true; | 351 return true; |
397 } | 352 } |
398 | 353 |
399 size_t TestFileErrorInjector::CurrentFileCount() const { | 354 size_t TestFileErrorInjector::CurrentFileCount() const { |
400 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 355 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
401 return files_.size(); | 356 return active_file_count_; |
402 } | 357 } |
403 | 358 |
404 size_t TestFileErrorInjector::TotalFileCount() const { | 359 size_t TestFileErrorInjector::TotalFileCount() const { |
405 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 360 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
406 return found_files_.size(); | 361 return total_file_count_; |
407 } | 362 } |
408 | 363 |
409 | 364 void TestFileErrorInjector::ClearTotalFileCount() { |
410 bool TestFileErrorInjector::HadFile(const GURL& url) const { | 365 total_file_count_ = 0; |
411 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
412 | |
413 return (found_files_.find(url) != found_files_.end()); | |
414 } | 366 } |
415 | 367 |
416 void TestFileErrorInjector::ClearFoundFiles() { | 368 void TestFileErrorInjector::DownloadFileCreated() { |
417 found_files_.clear(); | 369 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 370 ++active_file_count_; |
| 371 ++total_file_count_; |
418 } | 372 } |
419 | 373 |
420 void TestFileErrorInjector::DownloadFileCreated(GURL url) { | 374 void TestFileErrorInjector::DestroyingDownloadFile() { |
421 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 375 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
422 DCHECK(files_.find(url) == files_.end()); | 376 --active_file_count_; |
423 | |
424 files_.insert(url); | |
425 found_files_.insert(url); | |
426 } | 377 } |
427 | 378 |
428 void TestFileErrorInjector::DestroyingDownloadFile(GURL url) { | 379 void TestFileErrorInjector::RecordDownloadFileConstruction() { |
429 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 380 BrowserThread::PostTask( |
430 DCHECK(files_.find(url) != files_.end()); | 381 BrowserThread::UI, FROM_HERE, |
431 | 382 base::Bind(&TestFileErrorInjector::DownloadFileCreated, this)); |
432 files_.erase(url); | |
433 } | 383 } |
434 | 384 |
435 void TestFileErrorInjector::RecordDownloadFileConstruction(const GURL& url) { | 385 void TestFileErrorInjector::RecordDownloadFileDestruction() { |
436 BrowserThread::PostTask( | 386 BrowserThread::PostTask( |
437 BrowserThread::UI, | 387 BrowserThread::UI, FROM_HERE, |
438 FROM_HERE, | 388 base::Bind(&TestFileErrorInjector::DestroyingDownloadFile, this)); |
439 base::Bind(&TestFileErrorInjector::DownloadFileCreated, this, url)); | |
440 } | |
441 | |
442 void TestFileErrorInjector::RecordDownloadFileDestruction(const GURL& url) { | |
443 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
444 base::Bind(&TestFileErrorInjector::DestroyingDownloadFile, this, url)); | |
445 } | 389 } |
446 | 390 |
447 // static | 391 // static |
448 scoped_refptr<TestFileErrorInjector> TestFileErrorInjector::Create( | 392 scoped_refptr<TestFileErrorInjector> TestFileErrorInjector::Create( |
449 DownloadManager* download_manager) { | 393 DownloadManager* download_manager) { |
450 static bool visited = false; | 394 static bool visited = false; |
451 DCHECK(!visited); // Only allowed to be called once. | 395 DCHECK(!visited); // Only allowed to be called once. |
452 visited = true; | 396 visited = true; |
453 | 397 |
454 scoped_refptr<TestFileErrorInjector> single_injector( | 398 scoped_refptr<TestFileErrorInjector> single_injector( |
(...skipping 14 matching lines...) Expand all Loading... |
469 case FILE_OPERATION_RENAME_ANNOTATE: | 413 case FILE_OPERATION_RENAME_ANNOTATE: |
470 return "RENAME_ANNOTATE"; | 414 return "RENAME_ANNOTATE"; |
471 default: | 415 default: |
472 break; | 416 break; |
473 } | 417 } |
474 | 418 |
475 return "Unknown"; | 419 return "Unknown"; |
476 } | 420 } |
477 | 421 |
478 } // namespace content | 422 } // namespace content |
OLD | NEW |