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 // as either CANCELLED, or one which you can't switch out of). |
|
Randy Smith (Not in Mondays)
2012/03/06 21:49:58
I'd just list them here; INTERRUPTED can be switch
ahendrickson
2012/03/07 15:38:58
I've changed it to read "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 DownloadTestObserver : public content::DownloadManager::Observer, |
|
Randy Smith (Not in Mondays)
2012/03/06 21:49:58
I'd make the names more parallel. Maybe DownloadT
ahendrickson
2012/03/07 15:38:58
Done.
| |
| 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 |
| 44 // TODO(rdsmith): Consider rewriting the interface to take a list of events | 45 // TODO(rdsmith): Consider rewriting the interface to take a list of events |
| 45 // to treat as completion events. | 46 // to treat as completion events. |
|
Randy Smith (Not in Mondays)
2012/03/06 21:49:58
This TODO is now obsolete.
ahendrickson
2012/03/07 15:38:58
Removed comment.
| |
| 46 DownloadTestObserver( | 47 DownloadTestObserver( |
| 47 content::DownloadManager* download_manager, | 48 content::DownloadManager* download_manager, |
| 48 size_t wait_count, | 49 size_t wait_count, |
| 49 content::DownloadItem::DownloadState download_finished_state, | |
| 50 bool finish_on_select_file, | 50 bool finish_on_select_file, |
| 51 DangerousDownloadAction dangerous_download_action); | 51 DangerousDownloadAction dangerous_download_action); |
| 52 | 52 |
| 53 virtual ~DownloadTestObserver(); | 53 virtual ~DownloadTestObserver(); |
| 54 | 54 |
| 55 // State accessors. | 55 // State accessors. |
| 56 bool select_file_dialog_seen() const { return select_file_dialog_seen_; } | 56 bool select_file_dialog_seen() const { return select_file_dialog_seen_; } |
| 57 | 57 |
| 58 // Wait for whatever state was specified in the constructor. | 58 // Wait for a terminal state, for the number of downloads requested. |
|
Randy Smith (Not in Mondays)
2012/03/06 21:49:58
nit, suggestion: Maybe better worded as "Wait for
ahendrickson
2012/03/07 15:38:58
Done.
| |
| 59 void WaitForFinished(); | 59 void WaitForFinished(); |
| 60 | 60 |
| 61 // Return true if everything's happened that we're configured for. | 61 // Return true if everything's happened that we're configured for. |
| 62 bool IsFinished() const; | 62 bool IsFinished() const; |
| 63 | 63 |
| 64 // content::DownloadItem::Observer | 64 // content::DownloadItem::Observer |
|
Randy Smith (Not in Mondays)
2012/03/06 21:49:58
Not your problem; my mistake when I wrote the clas
| |
| 65 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; | 65 virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE; |
| 66 virtual void OnDownloadOpened(content::DownloadItem* download) OVERRIDE {} | 66 virtual void OnDownloadOpened(content::DownloadItem* download) OVERRIDE {} |
| 67 | 67 |
| 68 // content::DownloadManager::Observer | 68 // content::DownloadManager::Observer |
| 69 virtual void ModelChanged(content::DownloadManager* manager) OVERRIDE; | 69 virtual void ModelChanged(content::DownloadManager* manager) OVERRIDE; |
| 70 | 70 |
| 71 virtual void SelectFileDialogDisplayed( | 71 virtual void SelectFileDialogDisplayed( |
| 72 content::DownloadManager* manager, int32 id) OVERRIDE; | 72 content::DownloadManager* manager, int32 id) OVERRIDE; |
| 73 | 73 |
| 74 size_t NumDangerousDownloadsSeen() const; | 74 size_t NumDangerousDownloadsSeen() const; |
| 75 | 75 |
| 76 private: | 76 protected: |
|
Randy Smith (Not in Mondays)
2012/03/06 21:49:58
So this gives me a bit of an interface minimality
ahendrickson
2012/03/07 15:38:58
Done.
| |
| 77 typedef std::set<content::DownloadItem*> DownloadSet; | 77 typedef std::set<content::DownloadItem*> DownloadSet; |
| 78 | 78 |
| 79 // Called when we know that a download item is in a final state. | 79 // 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 | 80 // 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 | 81 // final state; multiple notifications may occur once the item is in |
| 82 // that state. So we keep our own track of transitions into final. | 82 // that state. So we keep our own track of transitions into final. |
| 83 void DownloadInFinalState(content::DownloadItem* download); | 83 void DownloadInFinalState(content::DownloadItem* download); |
| 84 | 84 |
| 85 // Called to see if a download item is in a final state. | |
| 86 virtual bool IsDownloadInFinalState(content::DownloadItem* download); | |
| 87 | |
| 85 void SignalIfFinished(); | 88 void SignalIfFinished(); |
| 86 | 89 |
| 87 // The observed download manager. | 90 // The observed download manager. |
| 88 scoped_refptr<content::DownloadManager> download_manager_; | 91 scoped_refptr<content::DownloadManager> download_manager_; |
| 89 | 92 |
| 90 // The set of DownloadItem's that have transitioned to their finished state | 93 // The set of DownloadItem's that have transitioned to their finished state |
| 91 // since construction of this object. When the size of this array | 94 // since construction of this object. When the size of this array |
| 92 // reaches wait_count_, we're done. | 95 // reaches wait_count_, we're done. |
| 93 DownloadSet finished_downloads_; | 96 DownloadSet finished_downloads_; |
| 94 | 97 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 107 // construction and return from wait. But some downloads may be in our | 110 // construction and return from wait. But some downloads may be in our |
| 108 // final state (and thus be entered into |finished_downloads_|) when we | 111 // 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 | 112 // construct this class. We don't want to count those in our transition |
| 110 // to finished. | 113 // to finished. |
| 111 int finished_downloads_at_construction_; | 114 int finished_downloads_at_construction_; |
| 112 | 115 |
| 113 // Whether an internal message loop has been started and must be quit upon | 116 // Whether an internal message loop has been started and must be quit upon |
| 114 // all downloads completing. | 117 // all downloads completing. |
| 115 bool waiting_; | 118 bool waiting_; |
| 116 | 119 |
| 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 | 120 // True if we should transition the DownloadTestObserver to finished if |
| 121 // the select file dialog comes up. | 121 // the select file dialog comes up. |
| 122 bool finish_on_select_file_; | 122 bool finish_on_select_file_; |
| 123 | 123 |
| 124 // True if we've seen the select file dialog. | 124 // True if we've seen the select file dialog. |
| 125 bool select_file_dialog_seen_; | 125 bool select_file_dialog_seen_; |
| 126 | 126 |
| 127 // Action to take if a dangerous download is encountered. | 127 // Action to take if a dangerous download is encountered. |
| 128 DangerousDownloadAction dangerous_download_action_; | 128 DangerousDownloadAction dangerous_download_action_; |
| 129 | 129 |
| 130 // Holds the download ids which were dangerous. | 130 // Holds the download ids which were dangerous. |
| 131 std::set<int32> dangerous_downloads_seen_; | 131 std::set<int32> dangerous_downloads_seen_; |
| 132 | 132 |
| 133 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserver); | 133 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserver); |
| 134 }; | 134 }; |
| 135 | 135 |
| 136 // Detects changes to the downloads after construction. | |
| 137 // Finishes when one of the following happens: | |
| 138 // - A specified number of downloads change to the IN_PROGRESS state. | |
| 139 // Callers may either probe for the finished state, or wait on it. | |
| 140 // | |
| 141 class DownloadTestObserverInProgress : public DownloadTestObserver { | |
| 142 public: | |
| 143 // Create an object that will be considered finished when |wait_count| | |
| 144 // download items have entered state |IN_PROGRESS|. | |
| 145 | |
| 146 DownloadTestObserverInProgress( | |
| 147 content::DownloadManager* download_manager, | |
| 148 size_t wait_count); | |
| 149 | |
| 150 virtual ~DownloadTestObserverInProgress(); | |
| 151 | |
| 152 protected: | |
|
Randy Smith (Not in Mondays)
2012/03/06 21:49:58
FYI: This can be private and everything will work
ahendrickson
2012/03/07 15:38:58
Done.
| |
| 153 virtual bool IsDownloadInFinalState(content::DownloadItem* download); | |
| 154 | |
| 155 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserverInProgress); | |
| 156 }; | |
| 157 | |
| 136 // The WaitForFlush() method on this class returns after: | 158 // The WaitForFlush() method on this class returns after: |
| 137 // * There are no IN_PROGRESS download items remaining on the | 159 // * There are no IN_PROGRESS download items remaining on the |
| 138 // DownloadManager. | 160 // DownloadManager. |
| 139 // * There have been two round trip messages through the file and | 161 // * There have been two round trip messages through the file and |
| 140 // IO threads. | 162 // IO threads. |
| 141 // This almost certainly means that a Download cancel has propagated through | 163 // This almost certainly means that a Download cancel has propagated through |
| 142 // the system. | 164 // the system. |
| 143 class DownloadTestFlushObserver | 165 class DownloadTestFlushObserver |
| 144 : public content::DownloadManager::Observer, | 166 : public content::DownloadManager::Observer, |
| 145 public content::DownloadItem::Observer, | 167 public content::DownloadItem::Observer, |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 175 void PingIOThread(int cycle); | 197 void PingIOThread(int cycle); |
| 176 | 198 |
| 177 content::DownloadManager* download_manager_; | 199 content::DownloadManager* download_manager_; |
| 178 DownloadSet downloads_observed_; | 200 DownloadSet downloads_observed_; |
| 179 bool waiting_for_zero_inprogress_; | 201 bool waiting_for_zero_inprogress_; |
| 180 | 202 |
| 181 DISALLOW_COPY_AND_ASSIGN(DownloadTestFlushObserver); | 203 DISALLOW_COPY_AND_ASSIGN(DownloadTestFlushObserver); |
| 182 }; | 204 }; |
| 183 | 205 |
| 184 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ | 206 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ |
| OLD | NEW |