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_TEST_DOWNLOAD_TEST_OBSERVER_H_ | 5 #ifndef CONTENT_TEST_DOWNLOAD_TEST_OBSERVER_H_ |
6 #define CONTENT_TEST_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/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "content/public/browser/download_interrupt_reasons.h" | 14 #include "content/public/browser/download_interrupt_reasons.h" |
15 #include "content/public/browser/download_item.h" | 15 #include "content/public/browser/download_item.h" |
16 #include "content/public/browser/download_manager.h" | 16 #include "content/public/browser/download_manager.h" |
17 #include "content/public/browser/download_url_parameters.h" | 17 #include "content/public/browser/download_url_parameters.h" |
18 | 18 |
19 namespace content { | 19 namespace content { |
20 | 20 |
21 // Detects an arbitrary change on a download item. | 21 // Detects an arbitrary change on a download item. |
22 // TODO: Rewrite other observers to use this (or be replaced by it). | 22 // TODO: Rewrite other observers to use this (or be replaced by it). |
23 class DownloadUpdatedObserver : public DownloadItem::Observer { | 23 class DownloadUpdatedObserver : public DownloadItem::Observer { |
24 public: | 24 public: |
25 typedef base::Callback<bool(DownloadItem*)> EventFilter; | 25 typedef base::Callback<bool(DownloadItem*)> EventFilter; |
26 | 26 |
27 // The filter passed may be called multiple times, even after it | 27 // The filter passed may be called multiple times, even after it |
28 // returns true. | 28 // returns true. |
29 DownloadUpdatedObserver(DownloadItem* item, EventFilter filter); | 29 DownloadUpdatedObserver(DownloadItem* item, EventFilter filter); |
30 virtual ~DownloadUpdatedObserver(); | 30 ~DownloadUpdatedObserver() override; |
31 | 31 |
32 // Returns when either the event has been seen (at least once since | 32 // Returns when either the event has been seen (at least once since |
33 // object construction) or the item is destroyed. Return value indicates | 33 // object construction) or the item is destroyed. Return value indicates |
34 // if the wait ended because the item was seen (true) or the object | 34 // if the wait ended because the item was seen (true) or the object |
35 // destroyed (false). | 35 // destroyed (false). |
36 bool WaitForEvent(); | 36 bool WaitForEvent(); |
37 | 37 |
38 private: | 38 private: |
39 // DownloadItem::Observer | 39 // DownloadItem::Observer |
40 virtual void OnDownloadUpdated(DownloadItem* item) override; | 40 void OnDownloadUpdated(DownloadItem* item) override; |
41 virtual void OnDownloadDestroyed(DownloadItem* item) override; | 41 void OnDownloadDestroyed(DownloadItem* item) override; |
42 | 42 |
43 DownloadItem* item_; | 43 DownloadItem* item_; |
44 EventFilter filter_; | 44 EventFilter filter_; |
45 bool waiting_; | 45 bool waiting_; |
46 bool event_seen_; | 46 bool event_seen_; |
47 | 47 |
48 DISALLOW_COPY_AND_ASSIGN(DownloadUpdatedObserver); | 48 DISALLOW_COPY_AND_ASSIGN(DownloadUpdatedObserver); |
49 }; | 49 }; |
50 | 50 |
51 // Detects changes to the downloads after construction. | 51 // Detects changes to the downloads after construction. |
(...skipping 15 matching lines...) Expand all Loading... |
67 ON_DANGEROUS_DOWNLOAD_IGNORE, // Make it the callers problem. | 67 ON_DANGEROUS_DOWNLOAD_IGNORE, // Make it the callers problem. |
68 ON_DANGEROUS_DOWNLOAD_QUIT // Will set final state without decision. | 68 ON_DANGEROUS_DOWNLOAD_QUIT // Will set final state without decision. |
69 }; | 69 }; |
70 | 70 |
71 // Create an object that will be considered finished when |wait_count| | 71 // Create an object that will be considered finished when |wait_count| |
72 // download items have entered a terminal state. | 72 // download items have entered a terminal state. |
73 DownloadTestObserver(DownloadManager* download_manager, | 73 DownloadTestObserver(DownloadManager* download_manager, |
74 size_t wait_count, | 74 size_t wait_count, |
75 DangerousDownloadAction dangerous_download_action); | 75 DangerousDownloadAction dangerous_download_action); |
76 | 76 |
77 virtual ~DownloadTestObserver(); | 77 ~DownloadTestObserver() override; |
78 | 78 |
79 // Wait for one of the finish conditions. | 79 // Wait for one of the finish conditions. |
80 void WaitForFinished(); | 80 void WaitForFinished(); |
81 | 81 |
82 // Return true if we reached one of the finish conditions. | 82 // Return true if we reached one of the finish conditions. |
83 bool IsFinished() const; | 83 bool IsFinished() const; |
84 | 84 |
85 // DownloadItem::Observer | 85 // DownloadItem::Observer |
86 virtual void OnDownloadUpdated(DownloadItem* download) override; | 86 void OnDownloadUpdated(DownloadItem* download) override; |
87 virtual void OnDownloadDestroyed(DownloadItem* download) override; | 87 void OnDownloadDestroyed(DownloadItem* download) override; |
88 | 88 |
89 // DownloadManager::Observer | 89 // DownloadManager::Observer |
90 virtual void OnDownloadCreated( | 90 void OnDownloadCreated(DownloadManager* manager, DownloadItem* item) override; |
91 DownloadManager* manager, DownloadItem* item) override; | 91 void ManagerGoingDown(DownloadManager* manager) override; |
92 virtual void ManagerGoingDown(DownloadManager* manager) override; | |
93 | 92 |
94 size_t NumDangerousDownloadsSeen() const; | 93 size_t NumDangerousDownloadsSeen() const; |
95 | 94 |
96 size_t NumDownloadsSeenInState(DownloadItem::DownloadState state) const; | 95 size_t NumDownloadsSeenInState(DownloadItem::DownloadState state) const; |
97 | 96 |
98 protected: | 97 protected: |
99 // Only to be called by derived classes' constructors. | 98 // Only to be called by derived classes' constructors. |
100 virtual void Init(); | 99 virtual void Init(); |
101 | 100 |
102 // Called to see if a download item is in a final state. | 101 // Called to see if a download item is in a final state. |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 class DownloadTestObserverTerminal : public DownloadTestObserver { | 170 class DownloadTestObserverTerminal : public DownloadTestObserver { |
172 public: | 171 public: |
173 // Create an object that will be considered finished when |wait_count| | 172 // Create an object that will be considered finished when |wait_count| |
174 // download items have entered a terminal state (DownloadItem::IsDone() is | 173 // download items have entered a terminal state (DownloadItem::IsDone() is |
175 // true). | 174 // true). |
176 DownloadTestObserverTerminal( | 175 DownloadTestObserverTerminal( |
177 DownloadManager* download_manager, | 176 DownloadManager* download_manager, |
178 size_t wait_count, | 177 size_t wait_count, |
179 DangerousDownloadAction dangerous_download_action); | 178 DangerousDownloadAction dangerous_download_action); |
180 | 179 |
181 virtual ~DownloadTestObserverTerminal(); | 180 ~DownloadTestObserverTerminal() override; |
182 | 181 |
183 private: | 182 private: |
184 virtual bool IsDownloadInFinalState(DownloadItem* download) override; | 183 bool IsDownloadInFinalState(DownloadItem* download) override; |
185 | 184 |
186 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverTerminal); | 185 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverTerminal); |
187 }; | 186 }; |
188 | 187 |
189 // Detects changes to the downloads after construction. | 188 // Detects changes to the downloads after construction. |
190 // Finishes when a specified number of downloads change to the | 189 // Finishes when a specified number of downloads change to the |
191 // IN_PROGRESS state, or when the download manager is destroyed. | 190 // IN_PROGRESS state, or when the download manager is destroyed. |
192 // Dangerous downloads are accepted. | 191 // Dangerous downloads are accepted. |
193 // Callers may either probe for the finished state, or wait on it. | 192 // Callers may either probe for the finished state, or wait on it. |
194 class DownloadTestObserverInProgress : public DownloadTestObserver { | 193 class DownloadTestObserverInProgress : public DownloadTestObserver { |
195 public: | 194 public: |
196 // Create an object that will be considered finished when |wait_count| | 195 // Create an object that will be considered finished when |wait_count| |
197 // download items have entered state |IN_PROGRESS|. | 196 // download items have entered state |IN_PROGRESS|. |
198 DownloadTestObserverInProgress( | 197 DownloadTestObserverInProgress( |
199 DownloadManager* download_manager, size_t wait_count); | 198 DownloadManager* download_manager, size_t wait_count); |
200 | 199 |
201 virtual ~DownloadTestObserverInProgress(); | 200 ~DownloadTestObserverInProgress() override; |
202 | 201 |
203 private: | 202 private: |
204 virtual bool IsDownloadInFinalState(DownloadItem* download) override; | 203 bool IsDownloadInFinalState(DownloadItem* download) override; |
205 | 204 |
206 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverInProgress); | 205 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverInProgress); |
207 }; | 206 }; |
208 | 207 |
209 class DownloadTestObserverInterrupted : public DownloadTestObserver { | 208 class DownloadTestObserverInterrupted : public DownloadTestObserver { |
210 public: | 209 public: |
211 // Create an object that will be considered finished when |wait_count| | 210 // Create an object that will be considered finished when |wait_count| |
212 // download items are interrupted. | 211 // download items are interrupted. |
213 DownloadTestObserverInterrupted( | 212 DownloadTestObserverInterrupted( |
214 DownloadManager* download_manager, | 213 DownloadManager* download_manager, |
215 size_t wait_count, | 214 size_t wait_count, |
216 DangerousDownloadAction dangerous_download_action); | 215 DangerousDownloadAction dangerous_download_action); |
217 | 216 |
218 virtual ~DownloadTestObserverInterrupted(); | 217 ~DownloadTestObserverInterrupted() override; |
219 | 218 |
220 private: | 219 private: |
221 virtual bool IsDownloadInFinalState(DownloadItem* download) override; | 220 bool IsDownloadInFinalState(DownloadItem* download) override; |
222 | 221 |
223 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverInterrupted); | 222 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverInterrupted); |
224 }; | 223 }; |
225 | 224 |
226 // The WaitForFlush() method on this class returns after: | 225 // The WaitForFlush() method on this class returns after: |
227 // * There are no IN_PROGRESS download items remaining on the | 226 // * There are no IN_PROGRESS download items remaining on the |
228 // DownloadManager. | 227 // DownloadManager. |
229 // * There have been two round trip messages through the file and | 228 // * There have been two round trip messages through the file and |
230 // IO threads. | 229 // IO threads. |
231 // This almost certainly means that a Download cancel has propagated through | 230 // This almost certainly means that a Download cancel has propagated through |
232 // the system. | 231 // the system. |
233 class DownloadTestFlushObserver | 232 class DownloadTestFlushObserver |
234 : public DownloadManager::Observer, | 233 : public DownloadManager::Observer, |
235 public DownloadItem::Observer, | 234 public DownloadItem::Observer, |
236 public base::RefCountedThreadSafe<DownloadTestFlushObserver> { | 235 public base::RefCountedThreadSafe<DownloadTestFlushObserver> { |
237 public: | 236 public: |
238 explicit DownloadTestFlushObserver(DownloadManager* download_manager); | 237 explicit DownloadTestFlushObserver(DownloadManager* download_manager); |
239 | 238 |
240 void WaitForFlush(); | 239 void WaitForFlush(); |
241 | 240 |
242 // DownloadsManager observer methods. | 241 // DownloadsManager observer methods. |
243 virtual void OnDownloadCreated( | 242 void OnDownloadCreated(DownloadManager* manager, DownloadItem* item) override; |
244 DownloadManager* manager, | |
245 DownloadItem* item) override; | |
246 | 243 |
247 // DownloadItem observer methods. | 244 // DownloadItem observer methods. |
248 virtual void OnDownloadUpdated(DownloadItem* download) override; | 245 void OnDownloadUpdated(DownloadItem* download) override; |
249 virtual void OnDownloadDestroyed(DownloadItem* download) override; | 246 void OnDownloadDestroyed(DownloadItem* download) override; |
250 | 247 |
251 protected: | 248 protected: |
252 friend class base::RefCountedThreadSafe<DownloadTestFlushObserver>; | 249 friend class base::RefCountedThreadSafe<DownloadTestFlushObserver>; |
253 | 250 |
254 virtual ~DownloadTestFlushObserver(); | 251 ~DownloadTestFlushObserver() override; |
255 | 252 |
256 private: | 253 private: |
257 typedef std::set<DownloadItem*> DownloadSet; | 254 typedef std::set<DownloadItem*> DownloadSet; |
258 | 255 |
259 // If we're waiting for that flush point, check the number | 256 // If we're waiting for that flush point, check the number |
260 // of downloads in the IN_PROGRESS state and take appropriate | 257 // of downloads in the IN_PROGRESS state and take appropriate |
261 // action. If requested, also observes all downloads while iterating. | 258 // action. If requested, also observes all downloads while iterating. |
262 void CheckDownloadsInProgress(bool observe_downloads); | 259 void CheckDownloadsInProgress(bool observe_downloads); |
263 | 260 |
264 void PingFileThread(int cycle); | 261 void PingFileThread(int cycle); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 | 304 |
308 // We are in the message loop. | 305 // We are in the message loop. |
309 bool waiting_; | 306 bool waiting_; |
310 | 307 |
311 DISALLOW_COPY_AND_ASSIGN(DownloadTestItemCreationObserver); | 308 DISALLOW_COPY_AND_ASSIGN(DownloadTestItemCreationObserver); |
312 }; | 309 }; |
313 | 310 |
314 } // namespace content` | 311 } // namespace content` |
315 | 312 |
316 #endif // CONTENT_TEST_DOWNLOAD_TEST_OBSERVER_H_ | 313 #endif // CONTENT_TEST_DOWNLOAD_TEST_OBSERVER_H_ |
OLD | NEW |