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