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 CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ |
6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ | 6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <set> | 9 #include <set> |
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 | 15 |
16 // Construction of this class defines a system state, based on some number | 16 // Detects changes to the downloads after construction. |
17 // of downloads being seen in a particular state + other events that | 17 // Finishes when one of the following happens: |
18 // may occur in the download system. That state will be recorded if it | 18 // - A specified number of downloads change to a terminal state (defined |
19 // occurs at any point after construction. When that state occurs, the class | 19 // as anything but IN_PROGRESS). |
20 // is considered finished. Callers may either probe for the finished state, or | 20 // - Specific events, such as a select file dialog. |
21 // wait on it. | 21 // Callers may either probe for the finished state, or wait on it. |
22 // | 22 // |
23 // TODO(rdsmith): Detect manager going down, remove pointer to | 23 // TODO(rdsmith): Detect manager going down, remove pointer to |
24 // DownloadManager, transition to finished. (For right now we | 24 // DownloadManager, transition to finished. (For right now we |
25 // just use a scoped_refptr<> to keep it around, but that may cause | 25 // just use a scoped_refptr<> to keep it around, but that may cause |
26 // timeouts on waiting if a DownloadManager::Shutdown() occurs which | 26 // timeouts on waiting if a DownloadManager::Shutdown() occurs which |
27 // cancels our in-progress downloads.) | 27 // cancels our in-progress downloads.) |
28 class DownloadTestObserver : public content::DownloadManager::Observer, | 28 class DownloadTestObserverTerminal : public content::DownloadManager::Observer, |
29 public content::DownloadItem::Observer { | 29 public content::DownloadItem::Observer { |
30 public: | 30 public: |
31 // Action an observer should take if a dangerous download is encountered. | 31 // Action an observer should take if a dangerous download is encountered. |
32 enum DangerousDownloadAction { | 32 enum DangerousDownloadAction { |
33 ON_DANGEROUS_DOWNLOAD_ACCEPT, // Accept the download | 33 ON_DANGEROUS_DOWNLOAD_ACCEPT, // Accept the download |
34 ON_DANGEROUS_DOWNLOAD_DENY, // Deny the download | 34 ON_DANGEROUS_DOWNLOAD_DENY, // Deny the download |
35 ON_DANGEROUS_DOWNLOAD_FAIL // Fail if a dangerous download is seen | 35 ON_DANGEROUS_DOWNLOAD_FAIL // Fail if a dangerous download is seen |
36 }; | 36 }; |
37 | 37 |
38 // Create an object that will be considered finished when |wait_count| | 38 // Create an object that will be considered finished when |wait_count| |
39 // download items have entered state |download_finished_state|. | 39 // download items have entered a terminal state (either CANCELLED or |
40 // another state that you can't normally transition out of). | |
40 // If |finish_on_select_file| is true, the object will also be | 41 // If |finish_on_select_file| is true, the object will also be |
41 // considered finished if the DownloadManager raises a | 42 // considered finished if the DownloadManager raises a |
42 // SelectFileDialogDisplayed() notification. | 43 // SelectFileDialogDisplayed() notification. |
43 | 44 DownloadTestObserverTerminal( |
44 // TODO(rdsmith): Consider rewriting the interface to take a list of events | |
45 // to treat as completion events. | |
46 DownloadTestObserver( | |
47 content::DownloadManager* download_manager, | 45 content::DownloadManager* download_manager, |
48 size_t wait_count, | 46 size_t wait_count, |
49 content::DownloadItem::DownloadState download_finished_state, | |
50 bool finish_on_select_file, | 47 bool finish_on_select_file, |
51 DangerousDownloadAction dangerous_download_action); | 48 DangerousDownloadAction dangerous_download_action); |
52 | 49 |
53 virtual ~DownloadTestObserver(); | 50 virtual ~DownloadTestObserverTerminal(); |
54 | 51 |
55 // State accessors. | 52 // State accessors. |
56 bool select_file_dialog_seen() const { return select_file_dialog_seen_; } | 53 bool select_file_dialog_seen() const { return select_file_dialog_seen_; } |
57 | 54 |
58 // Wait for whatever state was specified in the constructor. | 55 // Wait for the requested number of downloads to enter a terminal state. |
59 void WaitForFinished(); | 56 void WaitForFinished(); |
60 | 57 |
61 // Return true if everything's happened that we're configured for. | 58 // Return true if everything's happened that we're configured for. |
62 bool IsFinished() const; | 59 bool IsFinished() const; |
63 | 60 |
64 // content::DownloadItem::Observer | 61 // content::DownloadItem::Observer |
65 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; | 62 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; |
66 virtual void OnDownloadOpened(content::DownloadItem* download) OVERRIDE {} | 63 virtual void OnDownloadOpened(content::DownloadItem* download) OVERRIDE {} |
67 | 64 |
68 // content::DownloadManager::Observer | 65 // content::DownloadManager::Observer |
69 virtual void ModelChanged(content::DownloadManager* manager) OVERRIDE; | 66 virtual void ModelChanged(content::DownloadManager* manager) OVERRIDE; |
70 | 67 |
71 virtual void SelectFileDialogDisplayed( | 68 virtual void SelectFileDialogDisplayed( |
72 content::DownloadManager* manager, int32 id) OVERRIDE; | 69 content::DownloadManager* manager, int32 id) OVERRIDE; |
73 | 70 |
74 size_t NumDangerousDownloadsSeen() const; | 71 size_t NumDangerousDownloadsSeen() const; |
75 | 72 |
73 protected: | |
74 // Called to see if a download item is in a final state. | |
75 virtual bool IsDownloadInFinalState(content::DownloadItem* download); | |
76 | |
76 private: | 77 private: |
77 typedef std::set<content::DownloadItem*> DownloadSet; | 78 typedef std::set<content::DownloadItem*> DownloadSet; |
78 | 79 |
79 // 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. |
80 // 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 |
81 // final state; multiple notifications may occur once the item is in | 82 // final state; multiple notifications may occur once the item is in |
82 // 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. |
83 void DownloadInFinalState(content::DownloadItem* download); | 84 void DownloadInFinalState(content::DownloadItem* download); |
84 | 85 |
85 void SignalIfFinished(); | 86 void SignalIfFinished(); |
(...skipping 21 matching lines...) Expand all Loading... | |
107 // construction and return from wait. But some downloads may be in our | 108 // construction and return from wait. But some downloads may be in our |
108 // final state (and thus be entered into |finished_downloads_|) when we | 109 // final state (and thus be entered into |finished_downloads_|) when we |
109 // construct this class. We don't want to count those in our transition | 110 // construct this class. We don't want to count those in our transition |
110 // to finished. | 111 // to finished. |
111 int finished_downloads_at_construction_; | 112 int finished_downloads_at_construction_; |
112 | 113 |
113 // Whether an internal message loop has been started and must be quit upon | 114 // Whether an internal message loop has been started and must be quit upon |
114 // all downloads completing. | 115 // all downloads completing. |
115 bool waiting_; | 116 bool waiting_; |
116 | 117 |
117 // The state on which to consider the DownloadItem finished. | 118 // True if we should transition the DownloadTestObserverTerminal to |
118 content::DownloadItem::DownloadState download_finished_state_; | 119 // finished if the select file dialog comes up. |
119 | |
120 // True if we should transition the DownloadTestObserver to finished if | |
121 // the select file dialog comes up. | |
122 bool finish_on_select_file_; | 120 bool finish_on_select_file_; |
123 | 121 |
124 // True if we've seen the select file dialog. | 122 // True if we've seen the select file dialog. |
125 bool select_file_dialog_seen_; | 123 bool select_file_dialog_seen_; |
126 | 124 |
127 // Action to take if a dangerous download is encountered. | 125 // Action to take if a dangerous download is encountered. |
128 DangerousDownloadAction dangerous_download_action_; | 126 DangerousDownloadAction dangerous_download_action_; |
129 | 127 |
130 // Holds the download ids which were dangerous. | 128 // Holds the download ids which were dangerous. |
131 std::set<int32> dangerous_downloads_seen_; | 129 std::set<int32> dangerous_downloads_seen_; |
132 | 130 |
133 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserver); | 131 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverTerminal); |
132 }; | |
133 | |
134 // Detects changes to the downloads after construction. | |
135 // Finishes when one of the following happens: | |
136 // - A specified number of downloads change to the IN_PROGRESS state. | |
137 // Callers may either probe for the finished state, or wait on it. | |
138 // | |
139 class DownloadTestObserverInProgress : public DownloadTestObserverTerminal { | |
Randy Smith (Not in Mondays)
2012/03/07 21:39:16
This is test code, so I won't insist on a change,
ahendrickson
2012/03/08 17:36:07
Done.
| |
140 public: | |
141 // Create an object that will be considered finished when |wait_count| | |
142 // download items have entered state |IN_PROGRESS|. | |
143 | |
144 DownloadTestObserverInProgress( | |
145 content::DownloadManager* download_manager, | |
146 size_t wait_count); | |
147 | |
148 virtual ~DownloadTestObserverInProgress(); | |
149 | |
150 private: | |
151 virtual bool IsDownloadInFinalState(content::DownloadItem* download) OVERRIDE; | |
152 | |
153 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverInProgress); | |
134 }; | 154 }; |
135 | 155 |
136 // The WaitForFlush() method on this class returns after: | 156 // The WaitForFlush() method on this class returns after: |
137 // * There are no IN_PROGRESS download items remaining on the | 157 // * There are no IN_PROGRESS download items remaining on the |
138 // DownloadManager. | 158 // DownloadManager. |
139 // * There have been two round trip messages through the file and | 159 // * There have been two round trip messages through the file and |
140 // IO threads. | 160 // IO threads. |
141 // This almost certainly means that a Download cancel has propagated through | 161 // This almost certainly means that a Download cancel has propagated through |
142 // the system. | 162 // the system. |
143 class DownloadTestFlushObserver | 163 class DownloadTestFlushObserver |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 void PingIOThread(int cycle); | 195 void PingIOThread(int cycle); |
176 | 196 |
177 content::DownloadManager* download_manager_; | 197 content::DownloadManager* download_manager_; |
178 DownloadSet downloads_observed_; | 198 DownloadSet downloads_observed_; |
179 bool waiting_for_zero_inprogress_; | 199 bool waiting_for_zero_inprogress_; |
180 | 200 |
181 DISALLOW_COPY_AND_ASSIGN(DownloadTestFlushObserver); | 201 DISALLOW_COPY_AND_ASSIGN(DownloadTestFlushObserver); |
182 }; | 202 }; |
183 | 203 |
184 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ | 204 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ |
OLD | NEW |