| 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 #ifndef CONTENT_PUBLIC_TEST_TEST_FILE_ERROR_INJECTOR_H_ | 5 #ifndef CONTENT_PUBLIC_TEST_TEST_FILE_ERROR_INJECTOR_H_ |
| 6 #define CONTENT_PUBLIC_TEST_TEST_FILE_ERROR_INJECTOR_H_ | 6 #define CONTENT_PUBLIC_TEST_TEST_FILE_ERROR_INJECTOR_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <map> | |
| 11 #include <set> | |
| 12 #include <string> | 10 #include <string> |
| 13 | 11 |
| 14 #include "base/macros.h" | 12 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 18 #include "content/public/browser/download_interrupt_reasons.h" | 16 #include "content/public/browser/download_interrupt_reasons.h" |
| 19 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 20 | 18 |
| 21 namespace content { | 19 namespace content { |
| 22 | 20 |
| 23 class DownloadId; | 21 class DownloadId; |
| 24 class DownloadFileWithErrorsFactory; | 22 class DownloadFileWithErrorFactory; |
| 25 class DownloadManager; | 23 class DownloadManager; |
| 26 class DownloadManagerImpl; | 24 class DownloadManagerImpl; |
| 27 | 25 |
| 28 // Test helper for injecting errors into download file operations. | 26 // Test helper for injecting errors into download file operations. All errors |
| 29 // All errors for a download must be injected before it starts. | 27 // for a download must be injected before it starts. This class needs to be |
| 30 // This class needs to be |RefCountedThreadSafe| because the implementation | 28 // |RefCountedThreadSafe| because the implementation is referenced by other |
| 31 // is referenced by other classes that live past the time when the user is | 29 // classes that live past the time when the user is nominally done with it. |
| 32 // nominally done with it. These classes are internal to content/. | |
| 33 // | 30 // |
| 34 // NOTE: No more than one download with the same URL can be in progress at | 31 // Once created, an error injected via InjectError() will cause any |
| 35 // the same time. You can have multiple simultaneous downloads as long as the | 32 // DownloadFiles created to fail with that error. Call ClearError() to stop |
| 36 // URLs are different, as the URLs are used as keys to get information about | 33 // injecting errors. |
| 37 // the download. | |
| 38 // | 34 // |
| 39 // Example: | 35 // Example: |
| 40 // | 36 // |
| 41 // FileErrorInfo a = { url1, ... }; | 37 // FileErrorInfo a = { ... }; |
| 42 // FileErrorInfo b = { url2, ... }; | |
| 43 // | 38 // |
| 44 // scoped_refptr<TestFileErrorInjector> injector = | 39 // scoped_refptr<TestFileErrorInjector> injector = |
| 45 // TestFileErrorInjector::Create(download_manager); | 40 // TestFileErrorInjector::Create(download_manager); |
| 46 // | 41 // |
| 47 // injector->AddError(a); | 42 // injector->InjectError(a); |
| 48 // injector->AddError(b); | |
| 49 // injector->InjectErrors(); | |
| 50 // | 43 // |
| 51 // download_manager->DownloadUrl(url1, ...); | 44 // download_manager->DownloadUrl(url1, ...); // Will be interrupted due to |a|. |
| 52 // download_manager->DownloadUrl(url2, ...); | 45 // download_manager->DownloadUrl(url2, ...); // Will be interrupted due to |a|. |
| 53 // ... wait for downloads to finish or get an injected error ... | 46 // |
| 47 // injector->ClearError(); |
| 48 // |
| 49 // download_manager->DownloadUrl(url3, ...); // Won't be interrupted due to |a|. |
| 54 class TestFileErrorInjector | 50 class TestFileErrorInjector |
| 55 : public base::RefCountedThreadSafe<TestFileErrorInjector> { | 51 : public base::RefCountedThreadSafe<TestFileErrorInjector> { |
| 56 public: | 52 public: |
| 57 enum FileOperationCode { | 53 enum FileOperationCode { |
| 58 FILE_OPERATION_INITIALIZE, | 54 FILE_OPERATION_INITIALIZE, |
| 59 FILE_OPERATION_WRITE, | 55 FILE_OPERATION_WRITE, |
| 60 FILE_OPERATION_RENAME_UNIQUIFY, | 56 FILE_OPERATION_RENAME_UNIQUIFY, |
| 61 FILE_OPERATION_RENAME_ANNOTATE, | 57 FILE_OPERATION_RENAME_ANNOTATE, |
| 62 }; | 58 }; |
| 63 | 59 |
| 64 // Structure that encapsulates the information needed to inject a file error. | 60 // Structure that encapsulates the information needed to inject a file error. |
| 65 struct FileErrorInfo { | 61 struct FileErrorInfo { |
| 66 std::string url; // Full URL of the download. Identifies the download. | |
| 67 FileOperationCode code; // Operation to affect. | 62 FileOperationCode code; // Operation to affect. |
| 68 int operation_instance; // 0-based count of operation calls, for each code. | 63 int operation_instance; // 0-based count of operation calls, for each code. |
| 69 DownloadInterruptReason error; // Error to inject. | 64 DownloadInterruptReason error; // Error to inject. |
| 70 }; | 65 }; |
| 71 | 66 |
| 72 typedef std::map<std::string, FileErrorInfo> ErrorMap; | |
| 73 | |
| 74 // Creates an instance. May only be called once. | 67 // Creates an instance. May only be called once. |
| 75 // Lives until all callbacks (in the implementation) are complete and the | 68 // Lives until all callbacks (in the implementation) are complete and the |
| 76 // creator goes out of scope. | 69 // creator goes out of scope. |
| 77 // TODO(rdsmith): Allow multiple calls for different download managers. | 70 // TODO(rdsmith): Allow multiple calls for different download managers. |
| 78 static scoped_refptr<TestFileErrorInjector> Create( | 71 static scoped_refptr<TestFileErrorInjector> Create( |
| 79 DownloadManager* download_manager); | 72 DownloadManager* download_manager); |
| 80 | 73 |
| 81 // Adds an error. | |
| 82 // Must be called before |InjectErrors()| for a particular download file. | |
| 83 // It is an error to call |AddError()| more than once for the same file | |
| 84 // (URL), unless you call |ClearErrors()| in between them. | |
| 85 bool AddError(const FileErrorInfo& error_info); | |
| 86 | |
| 87 // Clears all errors. | |
| 88 // Only affects files created after the next call to InjectErrors(). | |
| 89 void ClearErrors(); | |
| 90 | |
| 91 // Injects the errors such that new download files will be affected. | 74 // Injects the errors such that new download files will be affected. |
| 92 // The download system must already be initialized before calling this. | 75 // The download system must already be initialized before calling this. |
| 93 // Multiple calls are allowed, but only useful if the errors have changed. | 76 // Multiple calls are allowed, but only useful if the errors have changed. |
| 94 // Replaces the injected error list. | 77 // Replaces the injected error list. |
| 95 bool InjectErrors(); | 78 bool InjectError(const FileErrorInfo& error_to_inject); |
| 79 |
| 80 // Clears all errors. |
| 81 // Only affects files created after the next call to InjectErrors(). |
| 82 void ClearError(); |
| 96 | 83 |
| 97 // Tells how many files are currently open. | 84 // Tells how many files are currently open. |
| 98 size_t CurrentFileCount() const; | 85 size_t CurrentFileCount() const; |
| 99 | 86 |
| 100 // Tells how many files have ever been open (since construction or the | 87 // Tells how many files have ever been open (since construction or the |
| 101 // last call to |ClearFoundFiles()|). | 88 // last call to |ClearTotalFileCount()|). |
| 102 size_t TotalFileCount() const; | 89 size_t TotalFileCount() const; |
| 103 | 90 |
| 104 // Returns whether or not a file matching |url| has been created. | 91 // Resets the total file count. Doesn't affect what's returned by |
| 105 bool HadFile(const GURL& url) const; | 92 // CurrentFileCount(). |
| 106 | 93 void ClearTotalFileCount(); |
| 107 // Resets the found file list. | |
| 108 void ClearFoundFiles(); | |
| 109 | 94 |
| 110 static std::string DebugString(FileOperationCode code); | 95 static std::string DebugString(FileOperationCode code); |
| 111 | 96 |
| 112 private: | 97 private: |
| 113 friend class base::RefCountedThreadSafe<TestFileErrorInjector>; | 98 friend class base::RefCountedThreadSafe<TestFileErrorInjector>; |
| 114 | 99 |
| 115 typedef std::set<GURL> FileSet; | |
| 116 | |
| 117 explicit TestFileErrorInjector(DownloadManager* download_manager); | 100 explicit TestFileErrorInjector(DownloadManager* download_manager); |
| 118 | 101 |
| 119 virtual ~TestFileErrorInjector(); | 102 virtual ~TestFileErrorInjector(); |
| 120 | 103 |
| 121 // Callbacks from the download file, to record lifetimes. | 104 // Callbacks from the download file, to record lifetimes. |
| 122 // These may be called on any thread. | 105 // These may be called on any thread. |
| 123 void RecordDownloadFileConstruction(const GURL& url); | 106 void RecordDownloadFileConstruction(); |
| 124 void RecordDownloadFileDestruction(const GURL& url); | 107 void RecordDownloadFileDestruction(); |
| 125 | 108 |
| 126 // These run on the UI thread. | 109 // These run on the UI thread. |
| 127 void DownloadFileCreated(GURL url); | 110 void DownloadFileCreated(); |
| 128 void DestroyingDownloadFile(GURL url); | 111 void DestroyingDownloadFile(); |
| 129 | 112 |
| 130 // All the data is used on the UI thread. | 113 // All the data is used on the UI thread. |
| 131 // Our injected error list, mapped by URL. One per file. | |
| 132 ErrorMap injected_errors_; | |
| 133 | |
| 134 // Keep track of active DownloadFiles. | 114 // Keep track of active DownloadFiles. |
| 135 FileSet files_; | 115 size_t active_file_count_ = 0; |
| 136 | 116 |
| 137 // Keep track of found DownloadFiles. | 117 // Keep track of found DownloadFiles. |
| 138 FileSet found_files_; | 118 size_t total_file_count_ = 0; |
| 139 | 119 |
| 140 // The factory we created. May outlive this class. | 120 // The factory we created. May outlive this class. |
| 141 DownloadFileWithErrorsFactory* created_factory_; | 121 DownloadFileWithErrorFactory* created_factory_ = nullptr; |
| 142 | 122 |
| 143 // The download manager we set the factory on. | 123 // The download manager we set the factory on. |
| 144 DownloadManagerImpl* download_manager_; | 124 DownloadManagerImpl* download_manager_ = nullptr; |
| 145 | 125 |
| 146 DISALLOW_COPY_AND_ASSIGN(TestFileErrorInjector); | 126 DISALLOW_COPY_AND_ASSIGN(TestFileErrorInjector); |
| 147 }; | 127 }; |
| 148 | 128 |
| 149 } // namespace content | 129 } // namespace content |
| 150 | 130 |
| 151 #endif // CONTENT_PUBLIC_TEST_TEST_FILE_ERROR_INJECTOR_H_ | 131 #endif // CONTENT_PUBLIC_TEST_TEST_FILE_ERROR_INJECTOR_H_ |
| OLD | NEW |