| 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 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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. |
| 52 // |
| 52 // Finishes when one of the following happens: | 53 // Finishes when one of the following happens: |
| 53 // - A specified number of downloads change to a terminal state (defined | 54 // - A specified number of downloads change to a terminal state (defined |
| 54 // in derived classes). | 55 // in derived classes). |
| 55 // - Specific events, such as a select file dialog. | 56 // - The download manager was shutdown. |
| 57 // |
| 56 // Callers may either probe for the finished state, or wait on it. | 58 // Callers may either probe for the finished state, or wait on it. |
| 57 // | |
| 58 // TODO(rdsmith): Detect manager going down, remove pointer to | |
| 59 // DownloadManager, transition to finished. (For right now we | |
| 60 // just use a scoped_refptr<> to keep it around, but that may cause | |
| 61 // timeouts on waiting if a DownloadManager::Shutdown() occurs which | |
| 62 // cancels our in-progress downloads.) | |
| 63 class DownloadTestObserver : public DownloadManager::Observer, | 59 class DownloadTestObserver : public DownloadManager::Observer, |
| 64 public DownloadItem::Observer { | 60 public DownloadItem::Observer { |
| 65 public: | 61 public: |
| 66 // Action an observer should take if a dangerous download is encountered. | 62 // Action an observer should take if a dangerous download is encountered. |
| 67 enum DangerousDownloadAction { | 63 enum DangerousDownloadAction { |
| 68 ON_DANGEROUS_DOWNLOAD_ACCEPT, // Accept the download | 64 ON_DANGEROUS_DOWNLOAD_ACCEPT, // Accept the download |
| 69 ON_DANGEROUS_DOWNLOAD_DENY, // Deny the download | 65 ON_DANGEROUS_DOWNLOAD_DENY, // Deny the download |
| 70 ON_DANGEROUS_DOWNLOAD_FAIL, // Fail if a dangerous download is seen | 66 ON_DANGEROUS_DOWNLOAD_FAIL, // Fail if a dangerous download is seen |
| 71 ON_DANGEROUS_DOWNLOAD_IGNORE // Make it the callers problem. | 67 ON_DANGEROUS_DOWNLOAD_IGNORE // Make it the callers problem. |
| 72 }; | 68 }; |
| 73 | 69 |
| 74 // Create an object that will be considered finished when |wait_count| | 70 // Create an object that will be considered finished when |wait_count| |
| 75 // download items have entered a terminal state. | 71 // download items have entered a terminal state. |
| 76 DownloadTestObserver(DownloadManager* download_manager, | 72 DownloadTestObserver(DownloadManager* download_manager, |
| 77 size_t wait_count, | 73 size_t wait_count, |
| 78 DangerousDownloadAction dangerous_download_action); | 74 DangerousDownloadAction dangerous_download_action); |
| 79 | 75 |
| 80 virtual ~DownloadTestObserver(); | 76 virtual ~DownloadTestObserver(); |
| 81 | 77 |
| 82 // Wait for the requested number of downloads to enter a terminal state. | 78 // Wait for one of the finish conditions. |
| 83 void WaitForFinished(); | 79 void WaitForFinished(); |
| 84 | 80 |
| 85 // Return true if everything's happened that we're configured for. | 81 // Return true if we reached one of the finish conditions. |
| 86 bool IsFinished() const; | 82 bool IsFinished() const; |
| 87 | 83 |
| 88 // DownloadItem::Observer | 84 // DownloadItem::Observer |
| 89 virtual void OnDownloadUpdated(DownloadItem* download) OVERRIDE; | 85 virtual void OnDownloadUpdated(DownloadItem* download) OVERRIDE; |
| 90 virtual void OnDownloadDestroyed(DownloadItem* download) OVERRIDE; | 86 virtual void OnDownloadDestroyed(DownloadItem* download) OVERRIDE; |
| 91 | 87 |
| 92 // DownloadManager::Observer | 88 // DownloadManager::Observer |
| 93 virtual void OnDownloadCreated( | 89 virtual void OnDownloadCreated( |
| 94 DownloadManager* manager, DownloadItem* item) OVERRIDE; | 90 DownloadManager* manager, DownloadItem* item) OVERRIDE; |
| 91 virtual void ManagerGoingDown(DownloadManager* manager) OVERRIDE; |
| 95 | 92 |
| 96 size_t NumDangerousDownloadsSeen() const; | 93 size_t NumDangerousDownloadsSeen() const; |
| 97 | 94 |
| 98 size_t NumDownloadsSeenInState(DownloadItem::DownloadState state) const; | 95 size_t NumDownloadsSeenInState(DownloadItem::DownloadState state) const; |
| 99 | 96 |
| 100 protected: | 97 protected: |
| 101 // Only to be called by derived classes' constructors. | 98 // Only to be called by derived classes' constructors. |
| 102 virtual void Init(); | 99 virtual void Init(); |
| 103 | 100 |
| 104 // 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. |
| 105 virtual bool IsDownloadInFinalState(DownloadItem* download) = 0; | 102 virtual bool IsDownloadInFinalState(DownloadItem* download) = 0; |
| 106 | 103 |
| 107 private: | 104 private: |
| 108 typedef std::set<DownloadItem*> DownloadSet; | 105 typedef std::set<DownloadItem*> DownloadSet; |
| 109 | 106 |
| 110 // Maps states to the number of times they have been encountered | 107 // Maps states to the number of times they have been encountered |
| 111 typedef std::map<DownloadItem::DownloadState, size_t> StateMap; | 108 typedef std::map<DownloadItem::DownloadState, size_t> StateMap; |
| 112 | 109 |
| 113 // Called when we know that a download item is in a final state. | 110 // Called when we know that a download item is in a final state. |
| 114 // Note that this is not the same as it first transitioning in to the | 111 // Note that this is not the same as it first transitioning in to the |
| 115 // final state; multiple notifications may occur once the item is in | 112 // final state; multiple notifications may occur once the item is in |
| 116 // that state. So we keep our own track of transitions into final. | 113 // that state. So we keep our own track of transitions into final. |
| 117 void DownloadInFinalState(DownloadItem* download); | 114 void DownloadInFinalState(DownloadItem* download); |
| 118 | 115 |
| 119 void SignalIfFinished(); | 116 void SignalIfFinished(); |
| 120 | 117 |
| 118 // Fake user click on "Accept". |
| 119 void AcceptDangerousDownload(int32 download_id); |
| 120 |
| 121 // Fake user click on "Deny". |
| 122 void DenyDangerousDownload(int32 download_id); |
| 123 |
| 121 // The observed download manager. | 124 // The observed download manager. |
| 122 scoped_refptr<DownloadManager> download_manager_; | 125 DownloadManager* download_manager_; |
| 123 | 126 |
| 124 // The set of DownloadItem's that have transitioned to their finished state | 127 // The set of DownloadItem's that have transitioned to their finished state |
| 125 // since construction of this object. When the size of this array | 128 // since construction of this object. When the size of this array |
| 126 // reaches wait_count_, we're done. | 129 // reaches wait_count_, we're done. |
| 127 DownloadSet finished_downloads_; | 130 DownloadSet finished_downloads_; |
| 128 | 131 |
| 129 // The set of DownloadItem's we are currently observing. Generally there | 132 // The set of DownloadItem's we are currently observing. Generally there |
| 130 // won't be any overlap with the above; once we see the final state | 133 // won't be any overlap with the above; once we see the final state |
| 131 // on a DownloadItem, we'll stop observing it. | 134 // on a DownloadItem, we'll stop observing it. |
| 132 DownloadSet downloads_observed_; | 135 DownloadSet downloads_observed_; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 152 // Whether an internal message loop has been started and must be quit upon | 155 // Whether an internal message loop has been started and must be quit upon |
| 153 // all downloads completing. | 156 // all downloads completing. |
| 154 bool waiting_; | 157 bool waiting_; |
| 155 | 158 |
| 156 // Action to take if a dangerous download is encountered. | 159 // Action to take if a dangerous download is encountered. |
| 157 DangerousDownloadAction dangerous_download_action_; | 160 DangerousDownloadAction dangerous_download_action_; |
| 158 | 161 |
| 159 // Holds the download ids which were dangerous. | 162 // Holds the download ids which were dangerous. |
| 160 std::set<int32> dangerous_downloads_seen_; | 163 std::set<int32> dangerous_downloads_seen_; |
| 161 | 164 |
| 165 base::WeakPtrFactory<DownloadTestObserver> weak_factory_; |
| 166 |
| 162 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserver); | 167 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserver); |
| 163 }; | 168 }; |
| 164 | 169 |
| 165 class DownloadTestObserverTerminal : public DownloadTestObserver { | 170 class DownloadTestObserverTerminal : public DownloadTestObserver { |
| 166 public: | 171 public: |
| 167 // Create an object that will be considered finished when |wait_count| | 172 // Create an object that will be considered finished when |wait_count| |
| 168 // download items have entered a terminal state (any but IN_PROGRESS). | 173 // download items have entered a terminal state (any but IN_PROGRESS). |
| 169 // If |finish_on_select_file| is true, the object will also be | |
| 170 // considered finished if the DownloadManager raises a | |
| 171 // SelectFileDialogDisplayed() notification. | |
| 172 DownloadTestObserverTerminal( | 174 DownloadTestObserverTerminal( |
| 173 DownloadManager* download_manager, | 175 DownloadManager* download_manager, |
| 174 size_t wait_count, | 176 size_t wait_count, |
| 175 DangerousDownloadAction dangerous_download_action); | 177 DangerousDownloadAction dangerous_download_action); |
| 176 | 178 |
| 177 virtual ~DownloadTestObserverTerminal(); | 179 virtual ~DownloadTestObserverTerminal(); |
| 178 | 180 |
| 179 private: | 181 private: |
| 180 virtual bool IsDownloadInFinalState(DownloadItem* download) OVERRIDE; | 182 virtual bool IsDownloadInFinalState(DownloadItem* download) OVERRIDE; |
| 181 | 183 |
| 182 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverTerminal); | 184 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverTerminal); |
| 183 }; | 185 }; |
| 184 | 186 |
| 185 // Detects changes to the downloads after construction. | 187 // Detects changes to the downloads after construction. |
| 186 // Finishes when a specified number of downloads change to the | 188 // Finishes when a specified number of downloads change to the |
| 187 // IN_PROGRESS state, or a Select File Dialog has appeared. | 189 // IN_PROGRESS state, or when the download manager is destroyed. |
| 188 // Dangerous downloads are accepted. | 190 // Dangerous downloads are accepted. |
| 189 // Callers may either probe for the finished state, or wait on it. | 191 // Callers may either probe for the finished state, or wait on it. |
| 190 class DownloadTestObserverInProgress : public DownloadTestObserver { | 192 class DownloadTestObserverInProgress : public DownloadTestObserver { |
| 191 public: | 193 public: |
| 192 // Create an object that will be considered finished when |wait_count| | 194 // Create an object that will be considered finished when |wait_count| |
| 193 // download items have entered state |IN_PROGRESS|. | 195 // download items have entered state |IN_PROGRESS|. |
| 194 // If |finish_on_select_file| is true, the object will also be | |
| 195 // considered finished if the DownloadManager raises a | |
| 196 // SelectFileDialogDisplayed() notification. | |
| 197 DownloadTestObserverInProgress( | 196 DownloadTestObserverInProgress( |
| 198 DownloadManager* download_manager, size_t wait_count); | 197 DownloadManager* download_manager, size_t wait_count); |
| 199 | 198 |
| 200 virtual ~DownloadTestObserverInProgress(); | 199 virtual ~DownloadTestObserverInProgress(); |
| 201 | 200 |
| 202 private: | 201 private: |
| 203 virtual bool IsDownloadInFinalState(DownloadItem* download) OVERRIDE; | 202 virtual bool IsDownloadInFinalState(DownloadItem* download) OVERRIDE; |
| 204 | 203 |
| 205 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverInProgress); | 204 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverInProgress); |
| 206 }; | 205 }; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 | 285 |
| 287 // We are in the message loop. | 286 // We are in the message loop. |
| 288 bool waiting_; | 287 bool waiting_; |
| 289 | 288 |
| 290 DISALLOW_COPY_AND_ASSIGN(DownloadTestItemCreationObserver); | 289 DISALLOW_COPY_AND_ASSIGN(DownloadTestItemCreationObserver); |
| 291 }; | 290 }; |
| 292 | 291 |
| 293 } // namespace content` | 292 } // namespace content` |
| 294 | 293 |
| 295 #endif // CONTENT_TEST_DOWNLOAD_TEST_OBSERVER_H_ | 294 #endif // CONTENT_TEST_DOWNLOAD_TEST_OBSERVER_H_ |
| OLD | NEW |