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