Chromium Code Reviews| 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 // in derived classes). |
| 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 DownloadTestObserver : 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. |
| 40 // If |finish_on_select_file| is true, the object will also be | 40 // If |finish_on_select_file| is true, the object will also be |
| 41 // considered finished if the DownloadManager raises a | 41 // considered finished if the DownloadManager raises a |
| 42 // SelectFileDialogDisplayed() notification. | 42 // SelectFileDialogDisplayed() notification. |
| 43 | |
| 44 // TODO(rdsmith): Consider rewriting the interface to take a list of events | |
| 45 // to treat as completion events. | |
| 46 DownloadTestObserver( | 43 DownloadTestObserver( |
| 47 content::DownloadManager* download_manager, | 44 content::DownloadManager* download_manager, |
| 48 size_t wait_count, | 45 size_t wait_count, |
| 49 content::DownloadItem::DownloadState download_finished_state, | |
| 50 bool finish_on_select_file, | 46 bool finish_on_select_file, |
| 51 DangerousDownloadAction dangerous_download_action); | 47 DangerousDownloadAction dangerous_download_action); |
| 52 | 48 |
| 53 virtual ~DownloadTestObserver(); | 49 virtual ~DownloadTestObserver(); |
| 54 | 50 |
| 55 // State accessors. | 51 // State accessors. |
| 56 bool select_file_dialog_seen() const { return select_file_dialog_seen_; } | 52 bool select_file_dialog_seen() const { return select_file_dialog_seen_; } |
| 57 | 53 |
| 58 // Wait for whatever state was specified in the constructor. | 54 // Wait for the requested number of downloads to enter a terminal state. |
| 59 void WaitForFinished(); | 55 void WaitForFinished(); |
| 60 | 56 |
| 61 // Return true if everything's happened that we're configured for. | 57 // Return true if everything's happened that we're configured for. |
| 62 bool IsFinished() const; | 58 bool IsFinished() const; |
| 63 | 59 |
| 64 // content::DownloadItem::Observer | 60 // content::DownloadItem::Observer |
| 65 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; | 61 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; |
| 66 virtual void OnDownloadOpened(content::DownloadItem* download) OVERRIDE {} | 62 virtual void OnDownloadOpened(content::DownloadItem* download) OVERRIDE {} |
| 67 | 63 |
| 68 // content::DownloadManager::Observer | 64 // content::DownloadManager::Observer |
| 69 virtual void ModelChanged(content::DownloadManager* manager) OVERRIDE; | 65 virtual void ModelChanged(content::DownloadManager* manager) OVERRIDE; |
| 70 | 66 |
| 71 virtual void SelectFileDialogDisplayed( | 67 virtual void SelectFileDialogDisplayed( |
| 72 content::DownloadManager* manager, int32 id) OVERRIDE; | 68 content::DownloadManager* manager, int32 id) OVERRIDE; |
| 73 | 69 |
| 74 size_t NumDangerousDownloadsSeen() const; | 70 size_t NumDangerousDownloadsSeen() const; |
| 75 | 71 |
| 72 size_t NumDownloadsSeenInState( | |
| 73 content::DownloadItem::DownloadState state) const; | |
| 74 | |
| 75 protected: | |
| 76 // Only to be called by derived classes' constructors. | |
| 77 virtual void Init(); | |
| 78 | |
| 79 // Called to see if a download item is in a final state. | |
| 80 virtual bool IsDownloadInFinalState(content::DownloadItem* download) = 0; | |
| 81 | |
| 76 private: | 82 private: |
| 77 typedef std::set<content::DownloadItem*> DownloadSet; | 83 typedef std::set<content::DownloadItem*> DownloadSet; |
| 78 | 84 |
| 85 // Maps states to the number of times they have been encountered | |
| 86 typedef std::map<content::DownloadItem::DownloadState, size_t> StateMap; | |
| 87 | |
| 79 // Called when we know that a download item is in a final state. | 88 // 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 | 89 // 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 | 90 // final state; multiple notifications may occur once the item is in |
| 82 // that state. So we keep our own track of transitions into final. | 91 // that state. So we keep our own track of transitions into final. |
| 83 void DownloadInFinalState(content::DownloadItem* download); | 92 void DownloadInFinalState(content::DownloadItem* download); |
| 84 | 93 |
| 85 void SignalIfFinished(); | 94 void SignalIfFinished(); |
| 86 | 95 |
| 87 // The observed download manager. | 96 // The observed download manager. |
| 88 scoped_refptr<content::DownloadManager> download_manager_; | 97 scoped_refptr<content::DownloadManager> download_manager_; |
| 89 | 98 |
| 90 // The set of DownloadItem's that have transitioned to their finished state | 99 // The set of DownloadItem's that have transitioned to their finished state |
| 91 // since construction of this object. When the size of this array | 100 // since construction of this object. When the size of this array |
| 92 // reaches wait_count_, we're done. | 101 // reaches wait_count_, we're done. |
| 93 DownloadSet finished_downloads_; | 102 DownloadSet finished_downloads_; |
| 94 | 103 |
| 95 // The set of DownloadItem's we are currently observing. Generally there | 104 // The set of DownloadItem's we are currently observing. Generally there |
| 96 // won't be any overlap with the above; once we see the final state | 105 // won't be any overlap with the above; once we see the final state |
| 97 // on a DownloadItem, we'll stop observing it. | 106 // on a DownloadItem, we'll stop observing it. |
| 98 DownloadSet downloads_observed_; | 107 DownloadSet downloads_observed_; |
| 99 | 108 |
| 109 // The map of states to the number of times they have been observed since | |
| 110 // we started looking. | |
| 111 // Recorded at the time downloads_observed_ is recorded, but cleared in the | |
| 112 // constructor to exclude pre-existing states. | |
| 113 StateMap states_observed_; | |
| 114 | |
| 100 // The number of downloads to wait on completing. | 115 // The number of downloads to wait on completing. |
| 101 size_t wait_count_; | 116 size_t wait_count_; |
| 102 | 117 |
| 103 // The number of downloads entered in final state in initial | 118 // The number of downloads entered in final state in initial |
| 104 // ModelChanged(). We use |finished_downloads_| to track the incoming | 119 // ModelChanged(). We use |finished_downloads_| to track the incoming |
| 105 // transitions to final state we should ignore, and to track the | 120 // transitions to final state we should ignore, and to track the |
| 106 // number of final state transitions that occurred between | 121 // number of final state transitions that occurred between |
| 107 // construction and return from wait. But some downloads may be in our | 122 // construction and return from wait. But some downloads may be in our |
| 108 // final state (and thus be entered into |finished_downloads_|) when we | 123 // 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 | 124 // construct this class. We don't want to count those in our transition |
| 110 // to finished. | 125 // to finished. |
| 111 int finished_downloads_at_construction_; | 126 int finished_downloads_at_construction_; |
| 112 | 127 |
| 113 // Whether an internal message loop has been started and must be quit upon | 128 // Whether an internal message loop has been started and must be quit upon |
| 114 // all downloads completing. | 129 // all downloads completing. |
| 115 bool waiting_; | 130 bool waiting_; |
| 116 | 131 |
| 117 // The state on which to consider the DownloadItem finished. | |
| 118 content::DownloadItem::DownloadState download_finished_state_; | |
| 119 | |
| 120 // True if we should transition the DownloadTestObserver to finished if | 132 // True if we should transition the DownloadTestObserver to finished if |
| 121 // the select file dialog comes up. | 133 // the select file dialog comes up. |
| 122 bool finish_on_select_file_; | 134 bool finish_on_select_file_; |
| 123 | 135 |
| 124 // True if we've seen the select file dialog. | 136 // True if we've seen the select file dialog. |
| 125 bool select_file_dialog_seen_; | 137 bool select_file_dialog_seen_; |
| 126 | 138 |
| 127 // Action to take if a dangerous download is encountered. | 139 // Action to take if a dangerous download is encountered. |
| 128 DangerousDownloadAction dangerous_download_action_; | 140 DangerousDownloadAction dangerous_download_action_; |
| 129 | 141 |
| 130 // Holds the download ids which were dangerous. | 142 // Holds the download ids which were dangerous. |
| 131 std::set<int32> dangerous_downloads_seen_; | 143 std::set<int32> dangerous_downloads_seen_; |
| 132 | 144 |
| 133 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserver); | 145 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserver); |
| 134 }; | 146 }; |
| 135 | 147 |
| 148 class DownloadTestObserverTerminal : public DownloadTestObserver { | |
| 149 public: | |
| 150 // Create an object that will be considered finished when |wait_count| | |
| 151 // download items have entered a terminal state (any but IN_PROGRESS). | |
| 152 // If |finish_on_select_file| is true, the object will also be | |
| 153 // considered finished if the DownloadManager raises a | |
| 154 // SelectFileDialogDisplayed() notification. | |
| 155 DownloadTestObserverTerminal( | |
| 156 content::DownloadManager* download_manager, | |
| 157 size_t wait_count, | |
| 158 bool finish_on_select_file, | |
| 159 DangerousDownloadAction dangerous_download_action); | |
| 160 | |
| 161 virtual ~DownloadTestObserverTerminal(); | |
| 162 | |
| 163 private: | |
| 164 virtual bool IsDownloadInFinalState(content::DownloadItem* download) OVERRIDE; | |
| 165 | |
| 166 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverTerminal); | |
| 167 }; | |
| 168 | |
| 169 // Detects changes to the downloads after construction. | |
| 170 // Finishes when a specified number of downloads change to the | |
| 171 // IN_PROGRESS state, or a Select File Dialog has appeared. | |
|
Randy Smith (Not in Mondays)
2012/03/12 01:52:15
This comment about the select file dialog is no lo
ahendrickson
2012/03/12 03:13:54
It is; I added a parameter to the constructor that
Randy Smith (Not in Mondays)
2012/03/12 13:35:55
Um ... that was my point. This says that the obse
| |
| 172 // Dangerous downloads are accepted. | |
| 173 // Callers may either probe for the finished state, or wait on it. | |
| 174 class DownloadTestObserverInProgress : public DownloadTestObserver { | |
| 175 public: | |
| 176 // Create an object that will be considered finished when |wait_count| | |
| 177 // download items have entered state |IN_PROGRESS|. | |
| 178 // If |finish_on_select_file| is true, the object will also be | |
| 179 // considered finished if the DownloadManager raises a | |
| 180 // SelectFileDialogDisplayed() notification. | |
| 181 DownloadTestObserverInProgress( | |
| 182 content::DownloadManager* download_manager, | |
| 183 size_t wait_count, | |
| 184 bool finish_on_select_file); | |
| 185 | |
| 186 virtual ~DownloadTestObserverInProgress(); | |
| 187 | |
| 188 private: | |
| 189 virtual bool IsDownloadInFinalState(content::DownloadItem* download) OVERRIDE; | |
| 190 | |
| 191 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverInProgress); | |
| 192 }; | |
| 193 | |
| 136 // The WaitForFlush() method on this class returns after: | 194 // The WaitForFlush() method on this class returns after: |
| 137 // * There are no IN_PROGRESS download items remaining on the | 195 // * There are no IN_PROGRESS download items remaining on the |
| 138 // DownloadManager. | 196 // DownloadManager. |
| 139 // * There have been two round trip messages through the file and | 197 // * There have been two round trip messages through the file and |
| 140 // IO threads. | 198 // IO threads. |
| 141 // This almost certainly means that a Download cancel has propagated through | 199 // This almost certainly means that a Download cancel has propagated through |
| 142 // the system. | 200 // the system. |
| 143 class DownloadTestFlushObserver | 201 class DownloadTestFlushObserver |
| 144 : public content::DownloadManager::Observer, | 202 : public content::DownloadManager::Observer, |
| 145 public content::DownloadItem::Observer, | 203 public content::DownloadItem::Observer, |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 175 void PingIOThread(int cycle); | 233 void PingIOThread(int cycle); |
| 176 | 234 |
| 177 content::DownloadManager* download_manager_; | 235 content::DownloadManager* download_manager_; |
| 178 DownloadSet downloads_observed_; | 236 DownloadSet downloads_observed_; |
| 179 bool waiting_for_zero_inprogress_; | 237 bool waiting_for_zero_inprogress_; |
| 180 | 238 |
| 181 DISALLOW_COPY_AND_ASSIGN(DownloadTestFlushObserver); | 239 DISALLOW_COPY_AND_ASSIGN(DownloadTestFlushObserver); |
| 182 }; | 240 }; |
| 183 | 241 |
| 184 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ | 242 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ |
| OLD | NEW |