Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: content/public/test/test_file_error_injector.cc

Issue 1751603002: [Downloads] Rework how hashes are calculated for download files. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase on top of https://codereview.chromium.org/1781983002 since that's going in first. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/public/test/mock_download_manager.cc ('k') | crypto/secure_hash.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/callback.h"
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "content/browser/download/download_file_factory.h" 13 #include "content/browser/download/download_file_factory.h"
14 #include "content/browser/download/download_file_impl.h" 14 #include "content/browser/download/download_file_impl.h"
15 #include "content/browser/download/download_interrupt_reasons_impl.h" 15 #include "content/browser/download/download_interrupt_reasons_impl.h"
16 #include "content/browser/download/download_manager_impl.h" 16 #include "content/browser/download/download_manager_impl.h"
17 #include "content/browser/loader/resource_dispatcher_host_impl.h" 17 #include "content/browser/loader/resource_dispatcher_host_impl.h"
18 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
19 #include "url/gurl.h" 19 #include "url/gurl.h"
20 20
21 namespace content { 21 namespace content {
22 class ByteStreamReader; 22 class ByteStreamReader;
23 23
24 namespace { 24 namespace {
25 25
26 // A class that performs file operations and injects errors. 26 // A class that performs file operations and injects errors.
27 class DownloadFileWithError: public DownloadFileImpl { 27 class DownloadFileWithError: public DownloadFileImpl {
28 public: 28 public:
29 DownloadFileWithError(const DownloadSaveInfo& save_info, 29 DownloadFileWithError(scoped_ptr<DownloadSaveInfo> save_info,
30 const base::FilePath& default_download_directory, 30 const base::FilePath& default_download_directory,
31 const GURL& url, 31 scoped_ptr<ByteStreamReader> byte_stream,
32 const GURL& referrer_url, 32 const net::BoundNetLog& bound_net_log,
33 bool calculate_hash, 33 base::WeakPtr<DownloadDestinationObserver> observer,
34 base::File file, 34 const TestFileErrorInjector::FileErrorInfo& error_info,
35 scoped_ptr<ByteStreamReader> byte_stream, 35 const base::Closure& ctor_callback,
36 const net::BoundNetLog& bound_net_log, 36 const base::Closure& dtor_callback);
37 base::WeakPtr<DownloadDestinationObserver> observer,
38 const TestFileErrorInjector::FileErrorInfo& error_info,
39 const base::Closure& ctor_callback,
40 const base::Closure& dtor_callback);
41 37
42 ~DownloadFileWithError() override; 38 ~DownloadFileWithError() override;
43 39
44 void Initialize(const InitializeCallback& callback) override; 40 void Initialize(const InitializeCallback& callback) override;
45 41
46 // DownloadFile interface. 42 // DownloadFile interface.
47 DownloadInterruptReason AppendDataToFile(const char* data, 43 DownloadInterruptReason AppendDataToFile(const char* data,
48 size_t data_len) override; 44 size_t data_len) override;
49 void RenameAndUniquify(const base::FilePath& full_path, 45 void RenameAndUniquify(const base::FilePath& full_path,
50 const RenameCompletionCallback& callback) override; 46 const RenameCompletionCallback& callback) override;
51 void RenameAndAnnotate(const base::FilePath& full_path, 47 void RenameAndAnnotate(const base::FilePath& full_path,
48 const std::string& client_guid,
49 const GURL& source_url,
50 const GURL& referrer_url,
52 const RenameCompletionCallback& callback) override; 51 const RenameCompletionCallback& callback) override;
53 52
54 private: 53 private:
55 // Error generating helper. 54 // Error generating helper.
56 DownloadInterruptReason ShouldReturnError( 55 DownloadInterruptReason ShouldReturnError(
57 TestFileErrorInjector::FileOperationCode code, 56 TestFileErrorInjector::FileOperationCode code,
58 DownloadInterruptReason original_error); 57 DownloadInterruptReason original_error);
59 58
60 // Determine whether to overwrite an operation with the given code 59 // Determine whether to overwrite an operation with the given code
61 // with a substitute error; if returns true, |*original_error| is 60 // with a substitute error; if returns true, |*original_error| is
(...skipping 27 matching lines...) Expand all
89 DownloadInterruptReason overwrite_error, 88 DownloadInterruptReason overwrite_error,
90 DownloadInterruptReason original_error, 89 DownloadInterruptReason original_error,
91 const base::FilePath& path_result) { 90 const base::FilePath& path_result) {
92 original_callback.Run( 91 original_callback.Run(
93 overwrite_error, 92 overwrite_error,
94 overwrite_error == DOWNLOAD_INTERRUPT_REASON_NONE ? 93 overwrite_error == DOWNLOAD_INTERRUPT_REASON_NONE ?
95 path_result : base::FilePath()); 94 path_result : base::FilePath());
96 } 95 }
97 96
98 DownloadFileWithError::DownloadFileWithError( 97 DownloadFileWithError::DownloadFileWithError(
99 const DownloadSaveInfo& save_info, 98 scoped_ptr<DownloadSaveInfo> save_info,
100 const base::FilePath& default_download_directory, 99 const base::FilePath& default_download_directory,
101 const GURL& url,
102 const GURL& referrer_url,
103 bool calculate_hash,
104 base::File file,
105 scoped_ptr<ByteStreamReader> byte_stream, 100 scoped_ptr<ByteStreamReader> byte_stream,
106 const net::BoundNetLog& bound_net_log, 101 const net::BoundNetLog& bound_net_log,
107 base::WeakPtr<DownloadDestinationObserver> observer, 102 base::WeakPtr<DownloadDestinationObserver> observer,
108 const TestFileErrorInjector::FileErrorInfo& error_info, 103 const TestFileErrorInjector::FileErrorInfo& error_info,
109 const base::Closure& ctor_callback, 104 const base::Closure& ctor_callback,
110 const base::Closure& dtor_callback) 105 const base::Closure& dtor_callback)
111 : DownloadFileImpl(save_info, 106 : DownloadFileImpl(std::move(save_info),
112 default_download_directory, 107 default_download_directory,
113 url,
114 referrer_url,
115 calculate_hash,
116 std::move(file),
117 std::move(byte_stream), 108 std::move(byte_stream),
118 bound_net_log, 109 bound_net_log,
119 observer), 110 observer),
120 error_info_(error_info), 111 error_info_(error_info),
121 destruction_callback_(dtor_callback) { 112 destruction_callback_(dtor_callback) {
122 // DownloadFiles are created on the UI thread and are destroyed on the FILE 113 // DownloadFiles are created on the UI thread and are destroyed on the FILE
123 // thread. Schedule the ConstructionCallback on the FILE thread so that if a 114 // thread. Schedule the ConstructionCallback on the FILE thread so that if a
124 // DownloadItem schedules a DownloadFile to be destroyed and creates another 115 // DownloadItem schedules a DownloadFile to be destroyed and creates another
125 // one (as happens during download resumption), then the DestructionCallback 116 // one (as happens during download resumption), then the DestructionCallback
126 // for the old DownloadFile is run before the ConstructionCallback for the 117 // for the old DownloadFile is run before the ConstructionCallback for the
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 // Otherwise, just wrap the return. 179 // Otherwise, just wrap the return.
189 callback_to_use = base::Bind(&RenameErrorCallback, callback, 180 callback_to_use = base::Bind(&RenameErrorCallback, callback,
190 error_to_return); 181 error_to_return);
191 } 182 }
192 183
193 DownloadFileImpl::RenameAndUniquify(full_path, callback_to_use); 184 DownloadFileImpl::RenameAndUniquify(full_path, callback_to_use);
194 } 185 }
195 186
196 void DownloadFileWithError::RenameAndAnnotate( 187 void DownloadFileWithError::RenameAndAnnotate(
197 const base::FilePath& full_path, 188 const base::FilePath& full_path,
189 const std::string& client_guid,
190 const GURL& source_url,
191 const GURL& referrer_url,
198 const RenameCompletionCallback& callback) { 192 const RenameCompletionCallback& callback) {
199 DownloadInterruptReason error_to_return = DOWNLOAD_INTERRUPT_REASON_NONE; 193 DownloadInterruptReason error_to_return = DOWNLOAD_INTERRUPT_REASON_NONE;
200 RenameCompletionCallback callback_to_use = callback; 194 RenameCompletionCallback callback_to_use = callback;
201 195
202 // Replace callback if the error needs to be overwritten. 196 // Replace callback if the error needs to be overwritten.
203 if (OverwriteError( 197 if (OverwriteError(
204 TestFileErrorInjector::FILE_OPERATION_RENAME_ANNOTATE, 198 TestFileErrorInjector::FILE_OPERATION_RENAME_ANNOTATE,
205 &error_to_return)) { 199 &error_to_return)) {
206 if (DOWNLOAD_INTERRUPT_REASON_NONE != error_to_return) { 200 if (DOWNLOAD_INTERRUPT_REASON_NONE != error_to_return) {
207 // Don't execute a, probably successful, RenameAndAnnotate; just 201 // Don't execute a, probably successful, RenameAndAnnotate; just
208 // return the error. 202 // return the error.
209 BrowserThread::PostTask( 203 BrowserThread::PostTask(
210 BrowserThread::UI, FROM_HERE, base::Bind( 204 BrowserThread::UI, FROM_HERE, base::Bind(
211 callback, error_to_return, base::FilePath())); 205 callback, error_to_return, base::FilePath()));
212 return; 206 return;
213 } 207 }
214 208
215 // Otherwise, just wrap the return. 209 // Otherwise, just wrap the return.
216 callback_to_use = base::Bind(&RenameErrorCallback, callback, 210 callback_to_use = base::Bind(&RenameErrorCallback, callback,
217 error_to_return); 211 error_to_return);
218 } 212 }
219 213
220 DownloadFileImpl::RenameAndAnnotate(full_path, callback_to_use); 214 DownloadFileImpl::RenameAndAnnotate(
215 full_path, client_guid, source_url, referrer_url, callback_to_use);
221 } 216 }
222 217
223 bool DownloadFileWithError::OverwriteError( 218 bool DownloadFileWithError::OverwriteError(
224 TestFileErrorInjector::FileOperationCode code, 219 TestFileErrorInjector::FileOperationCode code,
225 DownloadInterruptReason* output_error) { 220 DownloadInterruptReason* output_error) {
226 int counter = operation_counter_[code]++; 221 int counter = operation_counter_[code]++;
227 222
228 if (code != error_info_.code) 223 if (code != error_info_.code)
229 return false; 224 return false;
230 225
(...skipping 16 matching lines...) Expand all
247 242
248 // A factory for constructing DownloadFiles that inject errors. 243 // A factory for constructing DownloadFiles that inject errors.
249 class DownloadFileWithErrorFactory : public DownloadFileFactory { 244 class DownloadFileWithErrorFactory : public DownloadFileFactory {
250 public: 245 public:
251 DownloadFileWithErrorFactory(const base::Closure& ctor_callback, 246 DownloadFileWithErrorFactory(const base::Closure& ctor_callback,
252 const base::Closure& dtor_callback); 247 const base::Closure& dtor_callback);
253 ~DownloadFileWithErrorFactory() override; 248 ~DownloadFileWithErrorFactory() override;
254 249
255 // DownloadFileFactory interface. 250 // DownloadFileFactory interface.
256 DownloadFile* CreateFile( 251 DownloadFile* CreateFile(
257 const DownloadSaveInfo& save_info, 252 scoped_ptr<DownloadSaveInfo> save_info,
258 const base::FilePath& default_download_directory, 253 const base::FilePath& default_download_directory,
259 const GURL& url,
260 const GURL& referrer_url,
261 bool calculate_hash,
262 base::File file,
263 scoped_ptr<ByteStreamReader> byte_stream, 254 scoped_ptr<ByteStreamReader> byte_stream,
264 const net::BoundNetLog& bound_net_log, 255 const net::BoundNetLog& bound_net_log,
265 base::WeakPtr<DownloadDestinationObserver> observer) override; 256 base::WeakPtr<DownloadDestinationObserver> observer) override;
266 257
267 void ClearError();
268 bool SetError(TestFileErrorInjector::FileErrorInfo error); 258 bool SetError(TestFileErrorInjector::FileErrorInfo error);
269 259
270 private: 260 private:
271 // Our injected error. 261 // Our injected error.
272 TestFileErrorInjector::FileErrorInfo injected_error_; 262 TestFileErrorInjector::FileErrorInfo injected_error_;
273 263
274 // Callback for creation and destruction. 264 // Callback for creation and destruction.
275 base::Closure construction_callback_; 265 base::Closure construction_callback_;
276 base::Closure destruction_callback_; 266 base::Closure destruction_callback_;
277 }; 267 };
278 268
279 DownloadFileWithErrorFactory::DownloadFileWithErrorFactory( 269 DownloadFileWithErrorFactory::DownloadFileWithErrorFactory(
280 const base::Closure& ctor_callback, 270 const base::Closure& ctor_callback,
281 const base::Closure& dtor_callback) 271 const base::Closure& dtor_callback)
282 : construction_callback_(ctor_callback), 272 : construction_callback_(ctor_callback),
283 destruction_callback_(dtor_callback) { 273 destruction_callback_(dtor_callback) {
284 injected_error_.code = TestFileErrorInjector::FILE_OPERATION_INITIALIZE; 274 injected_error_.code = TestFileErrorInjector::FILE_OPERATION_INITIALIZE;
285 injected_error_.error = DOWNLOAD_INTERRUPT_REASON_NONE; 275 injected_error_.error = DOWNLOAD_INTERRUPT_REASON_NONE;
286 injected_error_.operation_instance = -1; 276 injected_error_.operation_instance = -1;
287 } 277 }
288 278
289 DownloadFileWithErrorFactory::~DownloadFileWithErrorFactory() {} 279 DownloadFileWithErrorFactory::~DownloadFileWithErrorFactory() {}
290 280
291 DownloadFile* DownloadFileWithErrorFactory::CreateFile( 281 DownloadFile* DownloadFileWithErrorFactory::CreateFile(
292 const DownloadSaveInfo& save_info, 282 scoped_ptr<DownloadSaveInfo> save_info,
293 const base::FilePath& default_download_directory, 283 const base::FilePath& default_download_directory,
294 const GURL& url,
295 const GURL& referrer_url,
296 bool calculate_hash,
297 base::File file,
298 scoped_ptr<ByteStreamReader> byte_stream, 284 scoped_ptr<ByteStreamReader> byte_stream,
299 const net::BoundNetLog& bound_net_log, 285 const net::BoundNetLog& bound_net_log,
300 base::WeakPtr<DownloadDestinationObserver> observer) { 286 base::WeakPtr<DownloadDestinationObserver> observer) {
301 return new DownloadFileWithError( 287 return new DownloadFileWithError(std::move(save_info),
302 save_info, default_download_directory, url, referrer_url, calculate_hash, 288 default_download_directory,
303 std::move(file), std::move(byte_stream), bound_net_log, observer, 289 std::move(byte_stream),
304 injected_error_, construction_callback_, destruction_callback_); 290 bound_net_log,
291 observer,
292 injected_error_,
293 construction_callback_,
294 destruction_callback_);
305 } 295 }
306 296
307 bool DownloadFileWithErrorFactory::SetError( 297 bool DownloadFileWithErrorFactory::SetError(
308 TestFileErrorInjector::FileErrorInfo error) { 298 TestFileErrorInjector::FileErrorInfo error) {
309 injected_error_ = std::move(error); 299 injected_error_ = std::move(error);
310 return true; 300 return true;
311 } 301 }
312 302
313 TestFileErrorInjector::TestFileErrorInjector(DownloadManager* download_manager) 303 TestFileErrorInjector::TestFileErrorInjector(DownloadManager* download_manager)
314 : // This code is only used for browser_tests, so a 304 : // This code is only used for browser_tests, so a
315 // DownloadManager is always a DownloadManagerImpl. 305 // DownloadManager is always a DownloadManagerImpl.
316 download_manager_(static_cast<DownloadManagerImpl*>(download_manager)) { 306 download_manager_(static_cast<DownloadManagerImpl*>(download_manager)) {
317 // Record the value of the pointer, for later validation. 307 // Record the value of the pointer, for later validation.
318 created_factory_ = 308 created_factory_ = new DownloadFileWithErrorFactory(
319 new DownloadFileWithErrorFactory( 309 base::Bind(&TestFileErrorInjector::RecordDownloadFileConstruction, this),
320 base::Bind(&TestFileErrorInjector::RecordDownloadFileConstruction, 310 base::Bind(&TestFileErrorInjector::RecordDownloadFileDestruction, this));
321 this),
322 base::Bind(&TestFileErrorInjector::RecordDownloadFileDestruction,
323 this));
324 311
325 // We will transfer ownership of the factory to the download manager. 312 // We will transfer ownership of the factory to the download manager.
326 scoped_ptr<DownloadFileFactory> download_file_factory( 313 scoped_ptr<DownloadFileFactory> download_file_factory(
327 created_factory_); 314 created_factory_);
328 315
329 download_manager_->SetDownloadFileFactoryForTesting( 316 download_manager_->SetDownloadFileFactoryForTesting(
330 std::move(download_file_factory)); 317 std::move(download_file_factory));
331 } 318 }
332 319
333 TestFileErrorInjector::~TestFileErrorInjector() { 320 TestFileErrorInjector::~TestFileErrorInjector() {
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 case FILE_OPERATION_RENAME_ANNOTATE: 400 case FILE_OPERATION_RENAME_ANNOTATE:
414 return "RENAME_ANNOTATE"; 401 return "RENAME_ANNOTATE";
415 default: 402 default:
416 break; 403 break;
417 } 404 }
418 405
419 return "Unknown"; 406 return "Unknown";
420 } 407 }
421 408
422 } // namespace content 409 } // namespace content
OLDNEW
« no previous file with comments | « content/public/test/mock_download_manager.cc ('k') | crypto/secure_hash.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698