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

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

Issue 148133007: [Downloads] Always call DM::StartDownload() for explicit downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comment updates Created 4 years, 10 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 | Annotate | Revision Log
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/compiler_specific.h" 10 #include "base/compiler_specific.h"
(...skipping 10 matching lines...) Expand all
21 class ByteStreamReader; 21 class ByteStreamReader;
22 22
23 namespace { 23 namespace {
24 24
25 // A class that performs file operations and injects errors. 25 // A class that performs file operations and injects errors.
26 class DownloadFileWithErrors: public DownloadFileImpl { 26 class DownloadFileWithErrors: public DownloadFileImpl {
27 public: 27 public:
28 typedef base::Callback<void(const GURL& url)> ConstructionCallback; 28 typedef base::Callback<void(const GURL& url)> ConstructionCallback;
29 typedef base::Callback<void(const GURL& url)> DestructionCallback; 29 typedef base::Callback<void(const GURL& url)> DestructionCallback;
30 30
31 DownloadFileWithErrors( 31 DownloadFileWithErrors(const DownloadSaveInfo& save_info,
32 scoped_ptr<DownloadSaveInfo> save_info, 32 const base::FilePath& default_download_directory,
33 const base::FilePath& default_download_directory, 33 const GURL& url,
34 const GURL& url, 34 const GURL& referrer_url,
35 const GURL& referrer_url, 35 bool calculate_hash,
36 bool calculate_hash, 36 base::File file,
37 scoped_ptr<ByteStreamReader> stream, 37 scoped_ptr<ByteStreamReader> byte_stream,
38 const net::BoundNetLog& bound_net_log, 38 const net::BoundNetLog& bound_net_log,
39 base::WeakPtr<DownloadDestinationObserver> observer, 39 base::WeakPtr<DownloadDestinationObserver> observer,
40 const TestFileErrorInjector::FileErrorInfo& error_info, 40 const TestFileErrorInjector::FileErrorInfo& error_info,
41 const ConstructionCallback& ctor_callback, 41 const ConstructionCallback& ctor_callback,
42 const DestructionCallback& dtor_callback); 42 const DestructionCallback& dtor_callback);
43 43
44 ~DownloadFileWithErrors() override; 44 ~DownloadFileWithErrors() override;
45 45
46 void Initialize(const InitializeCallback& callback) override; 46 void Initialize(const InitializeCallback& callback) override;
47 47
48 // DownloadFile interface. 48 // DownloadFile interface.
49 DownloadInterruptReason AppendDataToFile(const char* data, 49 DownloadInterruptReason AppendDataToFile(const char* data,
50 size_t data_len) override; 50 size_t data_len) override;
51 void RenameAndUniquify(const base::FilePath& full_path, 51 void RenameAndUniquify(const base::FilePath& full_path,
52 const RenameCompletionCallback& callback) override; 52 const RenameCompletionCallback& callback) override;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 DownloadInterruptReason overwrite_error, 94 DownloadInterruptReason overwrite_error,
95 DownloadInterruptReason original_error, 95 DownloadInterruptReason original_error,
96 const base::FilePath& path_result) { 96 const base::FilePath& path_result) {
97 original_callback.Run( 97 original_callback.Run(
98 overwrite_error, 98 overwrite_error,
99 overwrite_error == DOWNLOAD_INTERRUPT_REASON_NONE ? 99 overwrite_error == DOWNLOAD_INTERRUPT_REASON_NONE ?
100 path_result : base::FilePath()); 100 path_result : base::FilePath());
101 } 101 }
102 102
103 DownloadFileWithErrors::DownloadFileWithErrors( 103 DownloadFileWithErrors::DownloadFileWithErrors(
104 scoped_ptr<DownloadSaveInfo> save_info, 104 const DownloadSaveInfo& save_info,
105 const base::FilePath& default_download_directory, 105 const base::FilePath& default_download_directory,
106 const GURL& url, 106 const GURL& url,
107 const GURL& referrer_url, 107 const GURL& referrer_url,
108 bool calculate_hash, 108 bool calculate_hash,
109 scoped_ptr<ByteStreamReader> stream, 109 base::File file,
110 scoped_ptr<ByteStreamReader> byte_stream,
110 const net::BoundNetLog& bound_net_log, 111 const net::BoundNetLog& bound_net_log,
111 base::WeakPtr<DownloadDestinationObserver> observer, 112 base::WeakPtr<DownloadDestinationObserver> observer,
112 const TestFileErrorInjector::FileErrorInfo& error_info, 113 const TestFileErrorInjector::FileErrorInfo& error_info,
113 const ConstructionCallback& ctor_callback, 114 const ConstructionCallback& ctor_callback,
114 const DestructionCallback& dtor_callback) 115 const DestructionCallback& dtor_callback)
115 : DownloadFileImpl(std::move(save_info), 116 : DownloadFileImpl(save_info,
116 default_download_directory, 117 default_download_directory,
117 url, 118 url,
118 referrer_url, 119 referrer_url,
119 calculate_hash, 120 calculate_hash,
120 std::move(stream), 121 std::move(file),
122 std::move(byte_stream),
121 bound_net_log, 123 bound_net_log,
122 observer), 124 observer),
123 source_url_(url), 125 source_url_(url),
124 error_info_(error_info), 126 error_info_(error_info),
125 destruction_callback_(dtor_callback) { 127 destruction_callback_(dtor_callback) {
126 // DownloadFiles are created on the UI thread and are destroyed on the FILE 128 // DownloadFiles are created on the UI thread and are destroyed on the FILE
127 // thread. Schedule the ConstructionCallback on the FILE thread so that if a 129 // thread. Schedule the ConstructionCallback on the FILE thread so that if a
128 // DownloadItem schedules a DownloadFile to be destroyed and creates another 130 // DownloadItem schedules a DownloadFile to be destroyed and creates another
129 // one (as happens during download resumption), then the DestructionCallback 131 // one (as happens during download resumption), then the DestructionCallback
130 // for the old DownloadFile is run before the ConstructionCallback for the 132 // for the old DownloadFile is run before the ConstructionCallback for the
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 // A factory for constructing DownloadFiles that inject errors. 257 // A factory for constructing DownloadFiles that inject errors.
256 class DownloadFileWithErrorsFactory : public DownloadFileFactory { 258 class DownloadFileWithErrorsFactory : public DownloadFileFactory {
257 public: 259 public:
258 DownloadFileWithErrorsFactory( 260 DownloadFileWithErrorsFactory(
259 const DownloadFileWithErrors::ConstructionCallback& ctor_callback, 261 const DownloadFileWithErrors::ConstructionCallback& ctor_callback,
260 const DownloadFileWithErrors::DestructionCallback& dtor_callback); 262 const DownloadFileWithErrors::DestructionCallback& dtor_callback);
261 ~DownloadFileWithErrorsFactory() override; 263 ~DownloadFileWithErrorsFactory() override;
262 264
263 // DownloadFileFactory interface. 265 // DownloadFileFactory interface.
264 DownloadFile* CreateFile( 266 DownloadFile* CreateFile(
265 scoped_ptr<DownloadSaveInfo> save_info, 267 const DownloadSaveInfo& save_info,
266 const base::FilePath& default_download_directory, 268 const base::FilePath& default_download_directory,
267 const GURL& url, 269 const GURL& url,
268 const GURL& referrer_url, 270 const GURL& referrer_url,
269 bool calculate_hash, 271 bool calculate_hash,
270 scoped_ptr<ByteStreamReader> stream, 272 base::File file,
273 scoped_ptr<ByteStreamReader> byte_stream,
271 const net::BoundNetLog& bound_net_log, 274 const net::BoundNetLog& bound_net_log,
272 base::WeakPtr<DownloadDestinationObserver> observer) override; 275 base::WeakPtr<DownloadDestinationObserver> observer) override;
273 276
274 bool AddError( 277 bool AddError(
275 const TestFileErrorInjector::FileErrorInfo& error_info); 278 const TestFileErrorInjector::FileErrorInfo& error_info);
276 279
277 void ClearErrors(); 280 void ClearErrors();
278 281
279 private: 282 private:
280 // Our injected error list, mapped by URL. One per file. 283 // Our injected error list, mapped by URL. One per file.
281 TestFileErrorInjector::ErrorMap injected_errors_; 284 TestFileErrorInjector::ErrorMap injected_errors_;
282 285
283 // Callback for creation and destruction. 286 // Callback for creation and destruction.
284 DownloadFileWithErrors::ConstructionCallback construction_callback_; 287 DownloadFileWithErrors::ConstructionCallback construction_callback_;
285 DownloadFileWithErrors::DestructionCallback destruction_callback_; 288 DownloadFileWithErrors::DestructionCallback destruction_callback_;
286 }; 289 };
287 290
288 DownloadFileWithErrorsFactory::DownloadFileWithErrorsFactory( 291 DownloadFileWithErrorsFactory::DownloadFileWithErrorsFactory(
289 const DownloadFileWithErrors::ConstructionCallback& ctor_callback, 292 const DownloadFileWithErrors::ConstructionCallback& ctor_callback,
290 const DownloadFileWithErrors::DestructionCallback& dtor_callback) 293 const DownloadFileWithErrors::DestructionCallback& dtor_callback)
291 : construction_callback_(ctor_callback), 294 : construction_callback_(ctor_callback),
292 destruction_callback_(dtor_callback) { 295 destruction_callback_(dtor_callback) {
293 } 296 }
294 297
295 DownloadFileWithErrorsFactory::~DownloadFileWithErrorsFactory() { 298 DownloadFileWithErrorsFactory::~DownloadFileWithErrorsFactory() {
296 } 299 }
297 300
298 DownloadFile* DownloadFileWithErrorsFactory::CreateFile( 301 DownloadFile* DownloadFileWithErrorsFactory::CreateFile(
299 scoped_ptr<DownloadSaveInfo> save_info, 302 const DownloadSaveInfo& save_info,
300 const base::FilePath& default_download_directory, 303 const base::FilePath& default_download_directory,
301 const GURL& url, 304 const GURL& url,
302 const GURL& referrer_url, 305 const GURL& referrer_url,
303 bool calculate_hash, 306 bool calculate_hash,
304 scoped_ptr<ByteStreamReader> stream, 307 base::File file,
308 scoped_ptr<ByteStreamReader> byte_stream,
305 const net::BoundNetLog& bound_net_log, 309 const net::BoundNetLog& bound_net_log,
306 base::WeakPtr<DownloadDestinationObserver> observer) { 310 base::WeakPtr<DownloadDestinationObserver> observer) {
307 if (injected_errors_.find(url.spec()) == injected_errors_.end()) { 311 if (injected_errors_.find(url.spec()) == injected_errors_.end()) {
308 // Have to create entry, because FileErrorInfo is not a POD type. 312 // Have to create entry, because FileErrorInfo is not a POD type.
309 TestFileErrorInjector::FileErrorInfo err_info = { 313 TestFileErrorInjector::FileErrorInfo err_info = {
310 url.spec(), 314 url.spec(),
311 TestFileErrorInjector::FILE_OPERATION_INITIALIZE, 315 TestFileErrorInjector::FILE_OPERATION_INITIALIZE,
312 -1, 316 -1,
313 DOWNLOAD_INTERRUPT_REASON_NONE 317 DOWNLOAD_INTERRUPT_REASON_NONE
314 }; 318 };
315 injected_errors_[url.spec()] = err_info; 319 injected_errors_[url.spec()] = err_info;
316 } 320 }
317 321
318 return new DownloadFileWithErrors( 322 return new DownloadFileWithErrors(
319 std::move(save_info), default_download_directory, url, referrer_url, 323 save_info, default_download_directory, url, referrer_url, calculate_hash,
320 calculate_hash, std::move(stream), bound_net_log, observer, 324 std::move(file), std::move(byte_stream), bound_net_log, observer,
321 injected_errors_[url.spec()], construction_callback_, 325 injected_errors_[url.spec()], construction_callback_,
322 destruction_callback_); 326 destruction_callback_);
323 } 327 }
324 328
325 bool DownloadFileWithErrorsFactory::AddError( 329 bool DownloadFileWithErrorsFactory::AddError(
326 const TestFileErrorInjector::FileErrorInfo& error_info) { 330 const TestFileErrorInjector::FileErrorInfo& error_info) {
327 // Creates an empty entry if necessary. Duplicate entries overwrite. 331 // Creates an empty entry if necessary. Duplicate entries overwrite.
328 injected_errors_[error_info.url] = error_info; 332 injected_errors_[error_info.url] = error_info;
329 333
330 return true; 334 return true;
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 case FILE_OPERATION_RENAME_ANNOTATE: 469 case FILE_OPERATION_RENAME_ANNOTATE:
466 return "RENAME_ANNOTATE"; 470 return "RENAME_ANNOTATE";
467 default: 471 default:
468 break; 472 break;
469 } 473 }
470 474
471 return "Unknown"; 475 return "Unknown";
472 } 476 }
473 477
474 } // namespace content 478 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698