Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CONTENT_TEST_TEST_FILE_ERROR_INJECTOR_H_ | |
| 6 #define CONTENT_TEST_TEST_FILE_ERROR_INJECTOR_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "net/base/net_errors.h" | |
| 13 | |
| 14 class GURL; | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 class DownloadId; | |
| 19 | |
| 20 // Test helper for injecting errors into download file operations. | |
| 21 // All errors for a download must be injected before it starts. | |
|
cbentzel
2012/03/08 20:02:55
Good comment.
| |
| 22 // This class needs to be |RefCountedThreadSafe| because the implementation | |
| 23 // is referenced by other classes that live past the time when the user is | |
| 24 // nominally done with it. These classes are internal to content/. | |
| 25 // | |
| 26 // NOTE: No more than one download with the same URL can be in progress at | |
| 27 // the same time. You can have multiple simultaneous downloads as long as the | |
| 28 // URLs are different, as the URLs are used as keys to get information about | |
| 29 // the download. | |
| 30 // | |
| 31 // Example: | |
| 32 // | |
| 33 // FileErrorInfo a = { url1, ... }; | |
| 34 // FileErrorInfo b = { url2, ... }; | |
| 35 // | |
| 36 // scoped_refptr<TestFileErrorInjector> injector = | |
| 37 // TestFileErrorInjector::Create(); | |
| 38 // | |
| 39 // injector->AddError(a); | |
| 40 // injector->AddError(b); | |
| 41 // injector->InjectErrors(); | |
| 42 // | |
| 43 // download_manager->DownloadUrl(url1, ...); | |
| 44 // download_manager->DownloadUrl(url2, ...); | |
| 45 // ... wait for downloads to finish or get an injected error ... | |
| 46 // | |
| 47 class TestFileErrorInjector | |
| 48 : public base::RefCountedThreadSafe<TestFileErrorInjector> { | |
| 49 public: | |
| 50 enum FileOperationCode { | |
| 51 FILE_OPERATION_INITIALIZE, | |
| 52 FILE_OPERATION_WRITE, | |
| 53 FILE_OPERATION_RENAME | |
| 54 }; | |
| 55 | |
| 56 // Structure that encapsulates the information needed to inject a file error. | |
| 57 struct FileErrorInfo { | |
| 58 std::string url; // Full URL of the download. Identifies the download. | |
| 59 FileOperationCode code; // Operation to affect. | |
| 60 int operation_instance; // 0-based count of operation calls, for each code. | |
| 61 net::Error net_error; // Error to inject. | |
| 62 }; | |
| 63 | |
| 64 // Creates an instance. May only be called once. | |
| 65 // Lives until all callbacks (in the implementation) are complete and the | |
| 66 // creator goes out of scope. | |
| 67 static scoped_refptr<TestFileErrorInjector> Create(); | |
| 68 | |
| 69 // Adds an error. | |
| 70 // Must be called before |InjectErrors()| for a particular download file. | |
| 71 // It is an error to call |AddError()| more than once for the same file | |
| 72 // (URL), unless you call |ClearErrors()| in between them. | |
| 73 virtual bool AddError(const FileErrorInfo& error_info) = 0; | |
| 74 | |
| 75 // Clears all errors. | |
| 76 // Only affects files created after the next call to InjectErrors(). | |
| 77 virtual void ClearErrors() = 0; | |
| 78 | |
| 79 // Injects the errors such that new download files will be affected. | |
| 80 // The download system must already be initialized before calling this. | |
| 81 // Multiple calls are allowed, but only useful if the errors have changed. | |
| 82 // Replaces the injected error list. | |
| 83 virtual bool InjectErrors() = 0; | |
| 84 | |
| 85 // Tells how many files are currently open. | |
| 86 virtual size_t CurrentFileCount() const = 0; | |
| 87 | |
| 88 // Tells how many files have ever been open (since construction or the | |
| 89 // last call to |ClearFoundFiles()|). | |
| 90 virtual size_t TotalFileCount() const = 0; | |
| 91 | |
| 92 // Returns whether or not a file matching |url| has been created. | |
| 93 virtual bool HadFile(const GURL& url) const = 0; | |
| 94 | |
| 95 // Gets the download ID associated with the file matching |url|. | |
| 96 virtual const content::DownloadId GetId(const GURL& url) const = 0; | |
| 97 | |
| 98 // Resets the found file list. | |
| 99 virtual void ClearFoundFiles() = 0; | |
| 100 | |
| 101 static std::string DebugString(FileOperationCode code); | |
| 102 | |
| 103 protected: | |
| 104 TestFileErrorInjector() {} | |
| 105 | |
| 106 virtual ~TestFileErrorInjector(); | |
| 107 | |
| 108 private: | |
| 109 friend class base::RefCountedThreadSafe<TestFileErrorInjector>; | |
| 110 | |
| 111 DISALLOW_COPY_AND_ASSIGN(TestFileErrorInjector); | |
| 112 }; | |
| 113 | |
| 114 } // namespace content | |
| 115 | |
| 116 #endif // CONTENT_TEST_TEST_FILE_ERROR_INJECTOR_H_ | |
| OLD | NEW |