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 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 // Finishes when one of the following happens: | 52 // Finishes when one of the following happens: |
| 53 // - A specified number of downloads change to a terminal state (defined | 53 // - A specified number of downloads change to a terminal state (defined |
| 54 // in derived classes). | 54 // in derived classes). |
| 55 // - Specific events, such as a select file dialog. | 55 // - Specific events, such as a select file dialog. |
| 56 // Callers may either probe for the finished state, or wait on it. | 56 // Callers may either probe for the finished state, or wait on it. |
| 57 // | 57 // |
| 58 // TODO(rdsmith): Detect manager going down, remove pointer to | 58 // TODO: Transition to finish when manager goes down. |
|
benjhayden
2013/05/17 14:50:03
Does this require more than `if (waiting_) base::M
cmarcelo
2013/05/20 15:22:06
It would work in current code base, but would brea
| |
| 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 }; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 85 // Return true if everything's happened that we're configured for. | 81 // Return true if everything's happened that we're configured for. |
| 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 | 174 // If |finish_on_select_file| is true, the object will also be |
| 170 // considered finished if the DownloadManager raises a | 175 // considered finished if the DownloadManager raises a |
| 171 // SelectFileDialogDisplayed() notification. | 176 // SelectFileDialogDisplayed() notification. |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 286 | 291 |
| 287 // We are in the message loop. | 292 // We are in the message loop. |
| 288 bool waiting_; | 293 bool waiting_; |
| 289 | 294 |
| 290 DISALLOW_COPY_AND_ASSIGN(DownloadTestItemCreationObserver); | 295 DISALLOW_COPY_AND_ASSIGN(DownloadTestItemCreationObserver); |
| 291 }; | 296 }; |
| 292 | 297 |
| 293 } // namespace content` | 298 } // namespace content` |
| 294 | 299 |
| 295 #endif // CONTENT_TEST_DOWNLOAD_TEST_OBSERVER_H_ | 300 #endif // CONTENT_TEST_DOWNLOAD_TEST_OBSERVER_H_ |
| OLD | NEW |