OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ |
| 6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <set> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "content/browser/download/download_item.h" |
| 14 #include "content/browser/download/download_manager.h" |
| 15 |
| 16 // Construction of this class defines a system state, based on some number |
| 17 // of downloads being seen in a particular state + other events that |
| 18 // may occur in the download system. That state will be recorded if it |
| 19 // occurs at any point after construction. When that state occurs, the class |
| 20 // is considered finished. Callers may either probe for the finished state, or |
| 21 // wait on it. |
| 22 // |
| 23 // TODO(rdsmith): Detect manager going down, remove pointer to |
| 24 // DownloadManager, transition to finished. (For right now we |
| 25 // just use a scoped_refptr<> to keep it around, but that may cause |
| 26 // timeouts on waiting if a DownloadManager::Shutdown() occurs which |
| 27 // cancels our in-progress downloads.) |
| 28 class DownloadTestObserver : public DownloadManager::Observer, |
| 29 public DownloadItem::Observer { |
| 30 public: |
| 31 // Action an observer should take if a dangerous download is encountered. |
| 32 enum DangerousDownloadAction { |
| 33 ON_DANGEROUS_DOWNLOAD_ACCEPT, // Accept the download |
| 34 ON_DANGEROUS_DOWNLOAD_DENY, // Deny the download |
| 35 ON_DANGEROUS_DOWNLOAD_FAIL // Fail if a dangerous download is seen |
| 36 }; |
| 37 |
| 38 // Create an object that will be considered finished when |wait_count| |
| 39 // download items have entered state |download_finished_state|. |
| 40 // If |finish_on_select_file| is true, the object will also be |
| 41 // considered finished if the DownloadManager raises a |
| 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(DownloadManager* download_manager, |
| 47 size_t wait_count, |
| 48 DownloadItem::DownloadState download_finished_state, |
| 49 bool finish_on_select_file, |
| 50 DangerousDownloadAction dangerous_download_action); |
| 51 |
| 52 virtual ~DownloadTestObserver(); |
| 53 |
| 54 // State accessors. |
| 55 bool select_file_dialog_seen() const { return select_file_dialog_seen_; } |
| 56 |
| 57 // Wait for whatever state was specified in the constructor. |
| 58 void WaitForFinished(); |
| 59 |
| 60 // Return true if everything's happened that we're configured for. |
| 61 bool IsFinished() const; |
| 62 |
| 63 // DownloadItem::Observer |
| 64 virtual void OnDownloadUpdated(DownloadItem* download) OVERRIDE; |
| 65 virtual void OnDownloadOpened(DownloadItem* download) OVERRIDE {} |
| 66 |
| 67 // DownloadManager::Observer |
| 68 virtual void ModelChanged() OVERRIDE; |
| 69 |
| 70 virtual void SelectFileDialogDisplayed(int32 id) OVERRIDE; |
| 71 |
| 72 size_t NumDangerousDownloadsSeen() const; |
| 73 |
| 74 private: |
| 75 typedef std::set<DownloadItem*> DownloadSet; |
| 76 |
| 77 // Called when we know that a download item is in a final state. |
| 78 // Note that this is not the same as it first transitioning in to the |
| 79 // final state; multiple notifications may occur once the item is in |
| 80 // that state. So we keep our own track of transitions into final. |
| 81 void DownloadInFinalState(DownloadItem* download); |
| 82 |
| 83 void SignalIfFinished(); |
| 84 |
| 85 // The observed download manager. |
| 86 scoped_refptr<DownloadManager> download_manager_; |
| 87 |
| 88 // The set of DownloadItem's that have transitioned to their finished state |
| 89 // since construction of this object. When the size of this array |
| 90 // reaches wait_count_, we're done. |
| 91 DownloadSet finished_downloads_; |
| 92 |
| 93 // The set of DownloadItem's we are currently observing. Generally there |
| 94 // won't be any overlap with the above; once we see the final state |
| 95 // on a DownloadItem, we'll stop observing it. |
| 96 DownloadSet downloads_observed_; |
| 97 |
| 98 // The number of downloads to wait on completing. |
| 99 size_t wait_count_; |
| 100 |
| 101 // The number of downloads entered in final state in initial |
| 102 // ModelChanged(). We use |finished_downloads_| to track the incoming |
| 103 // transitions to final state we should ignore, and to track the |
| 104 // number of final state transitions that occurred between |
| 105 // construction and return from wait. But some downloads may be in our |
| 106 // final state (and thus be entered into |finished_downloads_|) when we |
| 107 // construct this class. We don't want to count those in our transition |
| 108 // to finished. |
| 109 int finished_downloads_at_construction_; |
| 110 |
| 111 // Whether an internal message loop has been started and must be quit upon |
| 112 // all downloads completing. |
| 113 bool waiting_; |
| 114 |
| 115 // The state on which to consider the DownloadItem finished. |
| 116 DownloadItem::DownloadState download_finished_state_; |
| 117 |
| 118 // True if we should transition the DownloadTestObserver to finished if |
| 119 // the select file dialog comes up. |
| 120 bool finish_on_select_file_; |
| 121 |
| 122 // True if we've seen the select file dialog. |
| 123 bool select_file_dialog_seen_; |
| 124 |
| 125 // Action to take if a dangerous download is encountered. |
| 126 DangerousDownloadAction dangerous_download_action_; |
| 127 |
| 128 // Holds the download ids which were dangerous. |
| 129 std::set<int32> dangerous_downloads_seen_; |
| 130 |
| 131 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserver); |
| 132 }; |
| 133 |
| 134 // The WaitForFlush() method on this class returns after: |
| 135 // * There are no IN_PROGRESS download items remaining on the |
| 136 // DownloadManager. |
| 137 // * There have been two round trip messages through the file and |
| 138 // IO threads. |
| 139 // This almost certainly means that a Download cancel has propagated through |
| 140 // the system. |
| 141 class DownloadTestFlushObserver |
| 142 : public DownloadManager::Observer, |
| 143 public DownloadItem::Observer, |
| 144 public base::RefCountedThreadSafe<DownloadTestFlushObserver> { |
| 145 public: |
| 146 explicit DownloadTestFlushObserver(DownloadManager* download_manager); |
| 147 |
| 148 void WaitForFlush(); |
| 149 |
| 150 // DownloadsManager observer methods. |
| 151 virtual void ModelChanged() OVERRIDE; |
| 152 |
| 153 // DownloadItem observer methods. |
| 154 virtual void OnDownloadUpdated(DownloadItem* download) OVERRIDE; |
| 155 virtual void OnDownloadOpened(DownloadItem* download) OVERRIDE {} |
| 156 |
| 157 protected: |
| 158 friend class base::RefCountedThreadSafe<DownloadTestFlushObserver>; |
| 159 |
| 160 virtual ~DownloadTestFlushObserver(); |
| 161 |
| 162 private: |
| 163 typedef std::set<DownloadItem*> DownloadSet; |
| 164 |
| 165 // If we're waiting for that flush point, check the number |
| 166 // of downloads in the IN_PROGRESS state and take appropriate |
| 167 // action. If requested, also observes all downloads while iterating. |
| 168 void CheckDownloadsInProgress(bool observe_downloads); |
| 169 |
| 170 void PingFileThread(int cycle); |
| 171 |
| 172 void PingIOThread(int cycle); |
| 173 |
| 174 DownloadManager* download_manager_; |
| 175 DownloadSet downloads_observed_; |
| 176 bool waiting_for_zero_inprogress_; |
| 177 |
| 178 DISALLOW_COPY_AND_ASSIGN(DownloadTestFlushObserver); |
| 179 }; |
| 180 |
| 181 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ |
OLD | NEW |