| 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 | 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/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 #include "content/public/browser/download_url_parameters.h" | 15 #include "content/public/browser/download_url_parameters.h" |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 | 17 |
| 18 namespace internal { |
| 19 class MockFileChooserDownloadManagerDelegate; |
| 20 } |
| 21 |
| 22 class Profile; |
| 23 |
| 18 // Detects changes to the downloads after construction. | 24 // Detects changes to the downloads after construction. |
| 19 // Finishes when one of the following happens: | 25 // Finishes when one of the following happens: |
| 20 // - A specified number of downloads change to a terminal state (defined | 26 // - A specified number of downloads change to a terminal state (defined |
| 21 // in derived classes). | 27 // in derived classes). |
| 22 // - Specific events, such as a select file dialog. | 28 // - Specific events, such as a select file dialog. |
| 23 // Callers may either probe for the finished state, or wait on it. | 29 // Callers may either probe for the finished state, or wait on it. |
| 24 // | 30 // |
| 25 // TODO(rdsmith): Detect manager going down, remove pointer to | 31 // TODO(rdsmith): Detect manager going down, remove pointer to |
| 26 // DownloadManager, transition to finished. (For right now we | 32 // DownloadManager, transition to finished. (For right now we |
| 27 // just use a scoped_refptr<> to keep it around, but that may cause | 33 // just use a scoped_refptr<> to keep it around, but that may cause |
| 28 // timeouts on waiting if a DownloadManager::Shutdown() occurs which | 34 // timeouts on waiting if a DownloadManager::Shutdown() occurs which |
| 29 // cancels our in-progress downloads.) | 35 // cancels our in-progress downloads.) |
| 30 class DownloadTestObserver : public content::DownloadManager::Observer, | 36 class DownloadTestObserver : public content::DownloadManager::Observer, |
| 31 public content::DownloadItem::Observer { | 37 public content::DownloadItem::Observer { |
| 32 public: | 38 public: |
| 33 // Action an observer should take if a dangerous download is encountered. | 39 // Action an observer should take if a dangerous download is encountered. |
| 34 enum DangerousDownloadAction { | 40 enum DangerousDownloadAction { |
| 35 ON_DANGEROUS_DOWNLOAD_ACCEPT, // Accept the download | 41 ON_DANGEROUS_DOWNLOAD_ACCEPT, // Accept the download |
| 36 ON_DANGEROUS_DOWNLOAD_DENY, // Deny the download | 42 ON_DANGEROUS_DOWNLOAD_DENY, // Deny the download |
| 37 ON_DANGEROUS_DOWNLOAD_FAIL // Fail if a dangerous download is seen | 43 ON_DANGEROUS_DOWNLOAD_FAIL // Fail if a dangerous download is seen |
| 38 }; | 44 }; |
| 39 | 45 |
| 40 // Create an object that will be considered finished when |wait_count| | 46 // Create an object that will be considered finished when |wait_count| |
| 41 // download items have entered a terminal state. | 47 // download items have entered a terminal state. |
| 42 // If |finish_on_select_file| is true, the object will also be | |
| 43 // considered finished if the DownloadManager raises a | |
| 44 // SelectFileDialogDisplayed() notification. | |
| 45 DownloadTestObserver( | 48 DownloadTestObserver( |
| 46 content::DownloadManager* download_manager, | 49 content::DownloadManager* download_manager, |
| 47 size_t wait_count, | 50 size_t wait_count, |
| 48 bool finish_on_select_file, | |
| 49 DangerousDownloadAction dangerous_download_action); | 51 DangerousDownloadAction dangerous_download_action); |
| 50 | 52 |
| 51 virtual ~DownloadTestObserver(); | 53 virtual ~DownloadTestObserver(); |
| 52 | 54 |
| 53 // State accessors. | |
| 54 bool select_file_dialog_seen() const { return select_file_dialog_seen_; } | |
| 55 | |
| 56 // Wait for the requested number of downloads to enter a terminal state. | 55 // Wait for the requested number of downloads to enter a terminal state. |
| 57 void WaitForFinished(); | 56 void WaitForFinished(); |
| 58 | 57 |
| 59 // Return true if everything's happened that we're configured for. | 58 // Return true if everything's happened that we're configured for. |
| 60 bool IsFinished() const; | 59 bool IsFinished() const; |
| 61 | 60 |
| 62 // content::DownloadItem::Observer | 61 // content::DownloadItem::Observer |
| 63 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; | 62 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; |
| 64 virtual void OnDownloadOpened(content::DownloadItem* download) OVERRIDE {} | 63 virtual void OnDownloadOpened(content::DownloadItem* download) OVERRIDE {} |
| 65 | 64 |
| 66 // content::DownloadManager::Observer | 65 // content::DownloadManager::Observer |
| 67 virtual void ModelChanged(content::DownloadManager* manager) OVERRIDE; | 66 virtual void ModelChanged(content::DownloadManager* manager) OVERRIDE; |
| 68 | 67 |
| 69 virtual void SelectFileDialogDisplayed( | |
| 70 content::DownloadManager* manager, int32 id) OVERRIDE; | |
| 71 | |
| 72 size_t NumDangerousDownloadsSeen() const; | 68 size_t NumDangerousDownloadsSeen() const; |
| 73 | 69 |
| 74 size_t NumDownloadsSeenInState( | 70 size_t NumDownloadsSeenInState( |
| 75 content::DownloadItem::DownloadState state) const; | 71 content::DownloadItem::DownloadState state) const; |
| 76 | 72 |
| 77 protected: | 73 protected: |
| 78 // Only to be called by derived classes' constructors. | 74 // Only to be called by derived classes' constructors. |
| 79 virtual void Init(); | 75 virtual void Init(); |
| 80 | 76 |
| 81 // Called to see if a download item is in a final state. | 77 // Called to see if a download item is in a final state. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 // construction and return from wait. But some downloads may be in our | 120 // construction and return from wait. But some downloads may be in our |
| 125 // final state (and thus be entered into |finished_downloads_|) when we | 121 // final state (and thus be entered into |finished_downloads_|) when we |
| 126 // construct this class. We don't want to count those in our transition | 122 // construct this class. We don't want to count those in our transition |
| 127 // to finished. | 123 // to finished. |
| 128 int finished_downloads_at_construction_; | 124 int finished_downloads_at_construction_; |
| 129 | 125 |
| 130 // Whether an internal message loop has been started and must be quit upon | 126 // Whether an internal message loop has been started and must be quit upon |
| 131 // all downloads completing. | 127 // all downloads completing. |
| 132 bool waiting_; | 128 bool waiting_; |
| 133 | 129 |
| 134 // True if we should transition the DownloadTestObserver to finished if | |
| 135 // the select file dialog comes up. | |
| 136 bool finish_on_select_file_; | |
| 137 | |
| 138 // True if we've seen the select file dialog. | |
| 139 bool select_file_dialog_seen_; | |
| 140 | |
| 141 // Action to take if a dangerous download is encountered. | 130 // Action to take if a dangerous download is encountered. |
| 142 DangerousDownloadAction dangerous_download_action_; | 131 DangerousDownloadAction dangerous_download_action_; |
| 143 | 132 |
| 144 // Holds the download ids which were dangerous. | 133 // Holds the download ids which were dangerous. |
| 145 std::set<int32> dangerous_downloads_seen_; | 134 std::set<int32> dangerous_downloads_seen_; |
| 146 | 135 |
| 147 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserver); | 136 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserver); |
| 148 }; | 137 }; |
| 149 | 138 |
| 150 class DownloadTestObserverTerminal : public DownloadTestObserver { | 139 class DownloadTestObserverTerminal : public DownloadTestObserver { |
| 151 public: | 140 public: |
| 152 // Create an object that will be considered finished when |wait_count| | 141 // Create an object that will be considered finished when |wait_count| |
| 153 // download items have entered a terminal state (any but IN_PROGRESS). | 142 // download items have entered a terminal state (any but IN_PROGRESS). |
| 154 // If |finish_on_select_file| is true, the object will also be | 143 // If |finish_on_select_file| is true, the object will also be |
| 155 // considered finished if the DownloadManager raises a | 144 // considered finished if the DownloadManager raises a |
| 156 // SelectFileDialogDisplayed() notification. | 145 // SelectFileDialogDisplayed() notification. |
| 157 DownloadTestObserverTerminal( | 146 DownloadTestObserverTerminal( |
| 158 content::DownloadManager* download_manager, | 147 content::DownloadManager* download_manager, |
| 159 size_t wait_count, | 148 size_t wait_count, |
| 160 bool finish_on_select_file, | |
| 161 DangerousDownloadAction dangerous_download_action); | 149 DangerousDownloadAction dangerous_download_action); |
| 162 | 150 |
| 163 virtual ~DownloadTestObserverTerminal(); | 151 virtual ~DownloadTestObserverTerminal(); |
| 164 | 152 |
| 165 private: | 153 private: |
| 166 virtual bool IsDownloadInFinalState(content::DownloadItem* download) OVERRIDE; | 154 virtual bool IsDownloadInFinalState(content::DownloadItem* download) OVERRIDE; |
| 167 | 155 |
| 168 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverTerminal); | 156 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverTerminal); |
| 169 }; | 157 }; |
| 170 | 158 |
| 171 // Detects changes to the downloads after construction. | 159 // Detects changes to the downloads after construction. |
| 172 // Finishes when a specified number of downloads change to the | 160 // Finishes when a specified number of downloads change to the |
| 173 // IN_PROGRESS state, or a Select File Dialog has appeared. | 161 // IN_PROGRESS state, or a Select File Dialog has appeared. |
| 174 // Dangerous downloads are accepted. | 162 // Dangerous downloads are accepted. |
| 175 // Callers may either probe for the finished state, or wait on it. | 163 // Callers may either probe for the finished state, or wait on it. |
| 176 class DownloadTestObserverInProgress : public DownloadTestObserver { | 164 class DownloadTestObserverInProgress : public DownloadTestObserver { |
| 177 public: | 165 public: |
| 178 // Create an object that will be considered finished when |wait_count| | 166 // Create an object that will be considered finished when |wait_count| |
| 179 // download items have entered state |IN_PROGRESS|. | 167 // download items have entered state |IN_PROGRESS|. |
| 180 // If |finish_on_select_file| is true, the object will also be | 168 // If |finish_on_select_file| is true, the object will also be |
| 181 // considered finished if the DownloadManager raises a | 169 // considered finished if the DownloadManager raises a |
| 182 // SelectFileDialogDisplayed() notification. | 170 // SelectFileDialogDisplayed() notification. |
| 183 DownloadTestObserverInProgress( | 171 DownloadTestObserverInProgress( |
| 184 content::DownloadManager* download_manager, | 172 content::DownloadManager* download_manager, |
| 185 size_t wait_count, | 173 size_t wait_count); |
| 186 bool finish_on_select_file); | |
| 187 | 174 |
| 188 virtual ~DownloadTestObserverInProgress(); | 175 virtual ~DownloadTestObserverInProgress(); |
| 189 | 176 |
| 190 private: | 177 private: |
| 191 virtual bool IsDownloadInFinalState(content::DownloadItem* download) OVERRIDE; | 178 virtual bool IsDownloadInFinalState(content::DownloadItem* download) OVERRIDE; |
| 192 | 179 |
| 193 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverInProgress); | 180 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverInProgress); |
| 194 }; | 181 }; |
| 195 | 182 |
| 196 // The WaitForFlush() method on this class returns after: | 183 // The WaitForFlush() method on this class returns after: |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 | 259 |
| 273 // Count of callbacks. | 260 // Count of callbacks. |
| 274 size_t called_back_count_; | 261 size_t called_back_count_; |
| 275 | 262 |
| 276 // We are in the message loop. | 263 // We are in the message loop. |
| 277 bool waiting_; | 264 bool waiting_; |
| 278 | 265 |
| 279 DISALLOW_COPY_AND_ASSIGN(DownloadTestItemCreationObserver); | 266 DISALLOW_COPY_AND_ASSIGN(DownloadTestItemCreationObserver); |
| 280 }; | 267 }; |
| 281 | 268 |
| 269 // Observes and overrides file chooser activity for a profile. By default, once |
| 270 // attached to a profile, this class overrides the default file chooser by |
| 271 // replacing the ChromeDownloadManagerDelegate associated with |profile|. |
| 272 // NOTE: Again, this overrides the ChromeDownloadManagerDelegate for |profile|. |
| 273 class DownloadTestFileChooserObserver { |
| 274 public: |
| 275 // Attaches to |profile|. By default file chooser dialogs will be disabled |
| 276 // once attached. Call EnableFileChooser() to re-enable. |
| 277 explicit DownloadTestFileChooserObserver(Profile* profile); |
| 278 ~DownloadTestFileChooserObserver(); |
| 279 |
| 280 // Sets whether the file chooser dialog is enabled. If |enable| is false, any |
| 281 // attempt to display a file chooser dialog will cause the download to be |
| 282 // canceled. Otherwise, attempting to display a file chooser dialog will |
| 283 // result in the download continuing with the suggested path. |
| 284 void EnableFileChooser(bool enable); |
| 285 |
| 286 // Returns true if a file chooser dialog was displayed since the last time |
| 287 // this method was called. |
| 288 bool TestAndResetDidShowFileChooser(); |
| 289 |
| 290 private: |
| 291 scoped_refptr<internal::MockFileChooserDownloadManagerDelegate> |
| 292 test_delegate_; |
| 293 }; |
| 294 |
| 282 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ | 295 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ |
| OLD | NEW |