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