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 CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ | 5 #ifndef CONTENT_TEST_DOWNLOAD_TEST_OBSERVER_H_ |
6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ | 6 #define CONTENT_TEST_DOWNLOAD_TEST_OBSERVER_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "content/public/browser/download_item.h" | 13 #include "content/public/browser/download_item.h" |
14 #include "content/public/browser/download_manager.h" | 14 #include "content/public/browser/download_manager.h" |
15 #include "content/public/browser/download_url_parameters.h" | 15 #include "content/public/browser/download_url_parameters.h" |
16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
17 | 17 |
18 namespace internal { | 18 namespace content { |
19 class MockFileChooserDownloadManagerDelegate; | |
20 } | |
21 | |
22 class Profile; | |
23 | 19 |
24 // Detects changes to the downloads after construction. | 20 // Detects changes to the downloads after construction. |
25 // Finishes when one of the following happens: | 21 // Finishes when one of the following happens: |
26 // - A specified number of downloads change to a terminal state (defined | 22 // - A specified number of downloads change to a terminal state (defined |
27 // in derived classes). | 23 // in derived classes). |
28 // - Specific events, such as a select file dialog. | 24 // - Specific events, such as a select file dialog. |
29 // Callers may either probe for the finished state, or wait on it. | 25 // Callers may either probe for the finished state, or wait on it. |
30 // | 26 // |
31 // TODO(rdsmith): Detect manager going down, remove pointer to | 27 // TODO(rdsmith): Detect manager going down, remove pointer to |
32 // DownloadManager, transition to finished. (For right now we | 28 // DownloadManager, transition to finished. (For right now we |
33 // just use a scoped_refptr<> to keep it around, but that may cause | 29 // just use a scoped_refptr<> to keep it around, but that may cause |
34 // timeouts on waiting if a DownloadManager::Shutdown() occurs which | 30 // timeouts on waiting if a DownloadManager::Shutdown() occurs which |
35 // cancels our in-progress downloads.) | 31 // cancels our in-progress downloads.) |
36 class DownloadTestObserver : public content::DownloadManager::Observer, | 32 class DownloadTestObserver : public DownloadManager::Observer, |
37 public content::DownloadItem::Observer { | 33 public DownloadItem::Observer { |
38 public: | 34 public: |
39 // Action an observer should take if a dangerous download is encountered. | 35 // Action an observer should take if a dangerous download is encountered. |
40 enum DangerousDownloadAction { | 36 enum DangerousDownloadAction { |
41 ON_DANGEROUS_DOWNLOAD_ACCEPT, // Accept the download | 37 ON_DANGEROUS_DOWNLOAD_ACCEPT, // Accept the download |
42 ON_DANGEROUS_DOWNLOAD_DENY, // Deny the download | 38 ON_DANGEROUS_DOWNLOAD_DENY, // Deny the download |
43 ON_DANGEROUS_DOWNLOAD_FAIL // Fail if a dangerous download is seen | 39 ON_DANGEROUS_DOWNLOAD_FAIL // Fail if a dangerous download is seen |
44 }; | 40 }; |
45 | 41 |
46 // Create an object that will be considered finished when |wait_count| | 42 // Create an object that will be considered finished when |wait_count| |
47 // download items have entered a terminal state. | 43 // download items have entered a terminal state. |
48 DownloadTestObserver( | 44 DownloadTestObserver(DownloadManager* download_manager, |
49 content::DownloadManager* download_manager, | 45 size_t wait_count, |
50 size_t wait_count, | 46 DangerousDownloadAction dangerous_download_action); |
51 DangerousDownloadAction dangerous_download_action); | |
52 | 47 |
53 virtual ~DownloadTestObserver(); | 48 virtual ~DownloadTestObserver(); |
54 | 49 |
55 // Wait for the requested number of downloads to enter a terminal state. | 50 // Wait for the requested number of downloads to enter a terminal state. |
56 void WaitForFinished(); | 51 void WaitForFinished(); |
57 | 52 |
58 // Return true if everything's happened that we're configured for. | 53 // Return true if everything's happened that we're configured for. |
59 bool IsFinished() const; | 54 bool IsFinished() const; |
60 | 55 |
61 // content::DownloadItem::Observer | 56 // DownloadItem::Observer |
62 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; | 57 virtual void OnDownloadUpdated(DownloadItem* download) OVERRIDE; |
63 virtual void OnDownloadDestroyed(content::DownloadItem* download) OVERRIDE; | 58 virtual void OnDownloadDestroyed(DownloadItem* download) OVERRIDE; |
64 | 59 |
65 // content::DownloadManager::Observer | 60 // DownloadManager::Observer |
66 virtual void ModelChanged(content::DownloadManager* manager) OVERRIDE; | 61 virtual void ModelChanged(DownloadManager* manager) OVERRIDE; |
67 | 62 |
68 size_t NumDangerousDownloadsSeen() const; | 63 size_t NumDangerousDownloadsSeen() const; |
69 | 64 |
70 size_t NumDownloadsSeenInState( | 65 size_t NumDownloadsSeenInState(DownloadItem::DownloadState state) const; |
71 content::DownloadItem::DownloadState state) const; | |
72 | 66 |
73 protected: | 67 protected: |
74 // Only to be called by derived classes' constructors. | 68 // Only to be called by derived classes' constructors. |
75 virtual void Init(); | 69 virtual void Init(); |
76 | 70 |
77 // Called to see if a download item is in a final state. | 71 // Called to see if a download item is in a final state. |
78 virtual bool IsDownloadInFinalState(content::DownloadItem* download) = 0; | 72 virtual bool IsDownloadInFinalState(DownloadItem* download) = 0; |
79 | 73 |
80 private: | 74 private: |
81 typedef std::set<content::DownloadItem*> DownloadSet; | 75 typedef std::set<DownloadItem*> DownloadSet; |
82 | 76 |
83 // Maps states to the number of times they have been encountered | 77 // Maps states to the number of times they have been encountered |
84 typedef std::map<content::DownloadItem::DownloadState, size_t> StateMap; | 78 typedef std::map<DownloadItem::DownloadState, size_t> StateMap; |
85 | 79 |
86 // Called when we know that a download item is in a final state. | 80 // Called when we know that a download item is in a final state. |
87 // Note that this is not the same as it first transitioning in to the | 81 // Note that this is not the same as it first transitioning in to the |
88 // final state; multiple notifications may occur once the item is in | 82 // final state; multiple notifications may occur once the item is in |
89 // that state. So we keep our own track of transitions into final. | 83 // that state. So we keep our own track of transitions into final. |
90 void DownloadInFinalState(content::DownloadItem* download); | 84 void DownloadInFinalState(DownloadItem* download); |
91 | 85 |
92 void SignalIfFinished(); | 86 void SignalIfFinished(); |
93 | 87 |
94 // The observed download manager. | 88 // The observed download manager. |
95 scoped_refptr<content::DownloadManager> download_manager_; | 89 scoped_refptr<DownloadManager> download_manager_; |
96 | 90 |
97 // The set of DownloadItem's that have transitioned to their finished state | 91 // The set of DownloadItem's that have transitioned to their finished state |
98 // since construction of this object. When the size of this array | 92 // since construction of this object. When the size of this array |
99 // reaches wait_count_, we're done. | 93 // reaches wait_count_, we're done. |
100 DownloadSet finished_downloads_; | 94 DownloadSet finished_downloads_; |
101 | 95 |
102 // The set of DownloadItem's we are currently observing. Generally there | 96 // The set of DownloadItem's we are currently observing. Generally there |
103 // won't be any overlap with the above; once we see the final state | 97 // won't be any overlap with the above; once we see the final state |
104 // on a DownloadItem, we'll stop observing it. | 98 // on a DownloadItem, we'll stop observing it. |
105 DownloadSet downloads_observed_; | 99 DownloadSet downloads_observed_; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 }; | 131 }; |
138 | 132 |
139 class DownloadTestObserverTerminal : public DownloadTestObserver { | 133 class DownloadTestObserverTerminal : public DownloadTestObserver { |
140 public: | 134 public: |
141 // Create an object that will be considered finished when |wait_count| | 135 // Create an object that will be considered finished when |wait_count| |
142 // download items have entered a terminal state (any but IN_PROGRESS). | 136 // download items have entered a terminal state (any but IN_PROGRESS). |
143 // If |finish_on_select_file| is true, the object will also be | 137 // If |finish_on_select_file| is true, the object will also be |
144 // considered finished if the DownloadManager raises a | 138 // considered finished if the DownloadManager raises a |
145 // SelectFileDialogDisplayed() notification. | 139 // SelectFileDialogDisplayed() notification. |
146 DownloadTestObserverTerminal( | 140 DownloadTestObserverTerminal( |
147 content::DownloadManager* download_manager, | 141 DownloadManager* download_manager, |
148 size_t wait_count, | 142 size_t wait_count, |
149 DangerousDownloadAction dangerous_download_action); | 143 DangerousDownloadAction dangerous_download_action); |
150 | 144 |
151 virtual ~DownloadTestObserverTerminal(); | 145 virtual ~DownloadTestObserverTerminal(); |
152 | 146 |
153 private: | 147 private: |
154 virtual bool IsDownloadInFinalState(content::DownloadItem* download) OVERRIDE; | 148 virtual bool IsDownloadInFinalState(DownloadItem* download) OVERRIDE; |
155 | 149 |
156 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverTerminal); | 150 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverTerminal); |
157 }; | 151 }; |
158 | 152 |
159 // Detects changes to the downloads after construction. | 153 // Detects changes to the downloads after construction. |
160 // Finishes when a specified number of downloads change to the | 154 // Finishes when a specified number of downloads change to the |
161 // IN_PROGRESS state, or a Select File Dialog has appeared. | 155 // IN_PROGRESS state, or a Select File Dialog has appeared. |
162 // Dangerous downloads are accepted. | 156 // Dangerous downloads are accepted. |
163 // Callers may either probe for the finished state, or wait on it. | 157 // Callers may either probe for the finished state, or wait on it. |
164 class DownloadTestObserverInProgress : public DownloadTestObserver { | 158 class DownloadTestObserverInProgress : public DownloadTestObserver { |
165 public: | 159 public: |
166 // Create an object that will be considered finished when |wait_count| | 160 // Create an object that will be considered finished when |wait_count| |
167 // download items have entered state |IN_PROGRESS|. | 161 // download items have entered state |IN_PROGRESS|. |
168 // If |finish_on_select_file| is true, the object will also be | 162 // If |finish_on_select_file| is true, the object will also be |
169 // considered finished if the DownloadManager raises a | 163 // considered finished if the DownloadManager raises a |
170 // SelectFileDialogDisplayed() notification. | 164 // SelectFileDialogDisplayed() notification. |
171 DownloadTestObserverInProgress( | 165 DownloadTestObserverInProgress( |
172 content::DownloadManager* download_manager, | 166 DownloadManager* download_manager, size_t wait_count); |
173 size_t wait_count); | |
174 | 167 |
175 virtual ~DownloadTestObserverInProgress(); | 168 virtual ~DownloadTestObserverInProgress(); |
176 | 169 |
177 private: | 170 private: |
178 virtual bool IsDownloadInFinalState(content::DownloadItem* download) OVERRIDE; | 171 virtual bool IsDownloadInFinalState(DownloadItem* download) OVERRIDE; |
179 | 172 |
180 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverInProgress); | 173 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverInProgress); |
181 }; | 174 }; |
182 | 175 |
183 // The WaitForFlush() method on this class returns after: | 176 // The WaitForFlush() method on this class returns after: |
184 // * There are no IN_PROGRESS download items remaining on the | 177 // * There are no IN_PROGRESS download items remaining on the |
185 // DownloadManager. | 178 // DownloadManager. |
186 // * There have been two round trip messages through the file and | 179 // * There have been two round trip messages through the file and |
187 // IO threads. | 180 // IO threads. |
188 // This almost certainly means that a Download cancel has propagated through | 181 // This almost certainly means that a Download cancel has propagated through |
189 // the system. | 182 // the system. |
190 class DownloadTestFlushObserver | 183 class DownloadTestFlushObserver |
191 : public content::DownloadManager::Observer, | 184 : public DownloadManager::Observer, |
192 public content::DownloadItem::Observer, | 185 public DownloadItem::Observer, |
193 public base::RefCountedThreadSafe<DownloadTestFlushObserver> { | 186 public base::RefCountedThreadSafe<DownloadTestFlushObserver> { |
194 public: | 187 public: |
195 explicit DownloadTestFlushObserver( | 188 explicit DownloadTestFlushObserver(DownloadManager* download_manager); |
196 content::DownloadManager* download_manager); | |
197 | 189 |
198 void WaitForFlush(); | 190 void WaitForFlush(); |
199 | 191 |
200 // DownloadsManager observer methods. | 192 // DownloadsManager observer methods. |
201 virtual void ModelChanged(content::DownloadManager* manager) OVERRIDE; | 193 virtual void ModelChanged(DownloadManager* manager) OVERRIDE; |
202 | 194 |
203 // DownloadItem observer methods. | 195 // DownloadItem observer methods. |
204 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; | 196 virtual void OnDownloadUpdated(DownloadItem* download) OVERRIDE; |
205 virtual void OnDownloadDestroyed(content::DownloadItem* download) OVERRIDE; | 197 virtual void OnDownloadDestroyed(DownloadItem* download) OVERRIDE; |
206 | 198 |
207 protected: | 199 protected: |
208 friend class base::RefCountedThreadSafe<DownloadTestFlushObserver>; | 200 friend class base::RefCountedThreadSafe<DownloadTestFlushObserver>; |
209 | 201 |
210 virtual ~DownloadTestFlushObserver(); | 202 virtual ~DownloadTestFlushObserver(); |
211 | 203 |
212 private: | 204 private: |
213 typedef std::set<content::DownloadItem*> DownloadSet; | 205 typedef std::set<DownloadItem*> DownloadSet; |
214 | 206 |
215 // If we're waiting for that flush point, check the number | 207 // If we're waiting for that flush point, check the number |
216 // of downloads in the IN_PROGRESS state and take appropriate | 208 // of downloads in the IN_PROGRESS state and take appropriate |
217 // action. If requested, also observes all downloads while iterating. | 209 // action. If requested, also observes all downloads while iterating. |
218 void CheckDownloadsInProgress(bool observe_downloads); | 210 void CheckDownloadsInProgress(bool observe_downloads); |
219 | 211 |
220 void PingFileThread(int cycle); | 212 void PingFileThread(int cycle); |
221 | 213 |
222 void PingIOThread(int cycle); | 214 void PingIOThread(int cycle); |
223 | 215 |
224 content::DownloadManager* download_manager_; | 216 DownloadManager* download_manager_; |
225 DownloadSet downloads_observed_; | 217 DownloadSet downloads_observed_; |
226 bool waiting_for_zero_inprogress_; | 218 bool waiting_for_zero_inprogress_; |
227 | 219 |
228 DISALLOW_COPY_AND_ASSIGN(DownloadTestFlushObserver); | 220 DISALLOW_COPY_AND_ASSIGN(DownloadTestFlushObserver); |
229 }; | 221 }; |
230 | 222 |
231 // Waits for a callback indicating that the DownloadItem is about to be created, | 223 // Waits for a callback indicating that the DownloadItem is about to be created, |
232 // or that an error occurred and it won't be created. | 224 // or that an error occurred and it won't be created. |
233 class DownloadTestItemCreationObserver | 225 class DownloadTestItemCreationObserver |
234 : public base::RefCountedThreadSafe<DownloadTestItemCreationObserver> { | 226 : public base::RefCountedThreadSafe<DownloadTestItemCreationObserver> { |
235 public: | 227 public: |
236 DownloadTestItemCreationObserver(); | 228 DownloadTestItemCreationObserver(); |
237 | 229 |
238 void WaitForDownloadItemCreation(); | 230 void WaitForDownloadItemCreation(); |
239 | 231 |
240 content::DownloadId download_id() const { return download_id_; } | 232 DownloadId download_id() const { return download_id_; } |
241 net::Error error() const { return error_; } | 233 net::Error error() const { return error_; } |
242 bool started() const { return called_back_count_ > 0; } | 234 bool started() const { return called_back_count_ > 0; } |
243 bool succeeded() const { return started() && (error_ == net::OK); } | 235 bool succeeded() const { return started() && (error_ == net::OK); } |
244 | 236 |
245 const content::DownloadUrlParameters::OnStartedCallback callback(); | 237 const DownloadUrlParameters::OnStartedCallback callback(); |
246 | 238 |
247 private: | 239 private: |
248 friend class base::RefCountedThreadSafe<DownloadTestItemCreationObserver>; | 240 friend class base::RefCountedThreadSafe<DownloadTestItemCreationObserver>; |
249 | 241 |
250 ~DownloadTestItemCreationObserver(); | 242 ~DownloadTestItemCreationObserver(); |
251 | 243 |
252 void DownloadItemCreationCallback(content::DownloadId download_id, | 244 void DownloadItemCreationCallback(DownloadId download_id, |
253 net::Error error); | 245 net::Error error); |
254 | 246 |
255 // The download creation information we received. | 247 // The download creation information we received. |
256 content::DownloadId download_id_; | 248 DownloadId download_id_; |
257 | 249 |
258 net::Error error_; | 250 net::Error error_; |
259 | 251 |
260 // Count of callbacks. | 252 // Count of callbacks. |
261 size_t called_back_count_; | 253 size_t called_back_count_; |
262 | 254 |
263 // We are in the message loop. | 255 // We are in the message loop. |
264 bool waiting_; | 256 bool waiting_; |
265 | 257 |
266 DISALLOW_COPY_AND_ASSIGN(DownloadTestItemCreationObserver); | 258 DISALLOW_COPY_AND_ASSIGN(DownloadTestItemCreationObserver); |
267 }; | 259 }; |
268 | 260 |
269 // Observes and overrides file chooser activity for a profile. By default, once | 261 } // namespace content` |
270 // attached to a profile, this class overrides the default file chooser by | |
271 // replacing the ChromeDownloadManagerDelegate associated with |profile|. | |
272 // NOTE: Again, this overrides the ChromeDownloadManagerDelegate for |profile|. | |
273 class DownloadTestFileChooserObserver { | |
274 public: | |
275 // Attaches to |profile|. By default file chooser dialogs will be disabled | |
276 // once attached. Call EnableFileChooser() to re-enable. | |
277 explicit DownloadTestFileChooserObserver(Profile* profile); | |
278 ~DownloadTestFileChooserObserver(); | |
279 | 262 |
280 // Sets whether the file chooser dialog is enabled. If |enable| is false, any | 263 #endif // CONTENT_TEST_DOWNLOAD_TEST_OBSERVER_H_ |
281 // attempt to display a file chooser dialog will cause the download to be | |
282 // canceled. Otherwise, attempting to display a file chooser dialog will | |
283 // result in the download continuing with the suggested path. | |
284 void EnableFileChooser(bool enable); | |
285 | |
286 // Returns true if a file chooser dialog was displayed since the last time | |
287 // this method was called. | |
288 bool TestAndResetDidShowFileChooser(); | |
289 | |
290 private: | |
291 scoped_refptr<internal::MockFileChooserDownloadManagerDelegate> | |
292 test_delegate_; | |
293 }; | |
294 | |
295 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ | |
OLD | NEW |