Chromium Code Reviews| 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() { return select_file_dialog_seen_; } | |
|
achuithb
2011/10/13 23:05:08
const
Randy Smith (Not in Mondays)
2011/10/14 00:37:41
Done.
| |
| 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(); | |
|
achuithb
2011/10/13 23:05:08
const
Randy Smith (Not in Mondays)
2011/10/14 00:37:41
Done.
| |
| 62 | |
| 63 // DownloadItem::Observer | |
| 64 virtual void OnDownloadUpdated(DownloadItem* download); | |
|
achuithb
2011/10/13 23:05:08
OVERRIDE here and for the other 4 virtual methods
Randy Smith (Not in Mondays)
2011/10/14 00:37:41
Done for the other three; NumDangerousDownloadsSee
| |
| 65 virtual void OnDownloadOpened(DownloadItem* download) {} | |
| 66 | |
| 67 // DownloadManager::Observer | |
| 68 virtual void ModelChanged(); | |
| 69 | |
| 70 virtual void SelectFileDialogDisplayed(int32 id); | |
| 71 | |
| 72 virtual size_t NumDangerousDownloadsSeen() const; | |
| 73 | |
| 74 private: | |
| 75 // Called when we know that a download item is in a final state. | |
| 76 // Note that this is not the same as it first transitioning in to the | |
| 77 // final state; multiple notifications may occur once the item is in | |
| 78 // that state. So we keep our own track of transitions into final. | |
| 79 void DownloadInFinalState(DownloadItem* download); | |
| 80 | |
| 81 void SignalIfFinished(); | |
| 82 | |
| 83 // The observed download manager. | |
| 84 scoped_refptr<DownloadManager> download_manager_; | |
| 85 | |
| 86 // The set of DownloadItem's that have transitioned to their finished state | |
| 87 // since construction of this object. When the size of this array | |
| 88 // reaches wait_count_, we're done. | |
| 89 std::set<DownloadItem*> finished_downloads_; | |
|
achuithb
2011/10/13 23:05:08
You use this particular set many times; a concise
Randy Smith (Not in Mondays)
2011/10/14 00:37:41
Done.
| |
| 90 | |
| 91 // The set of DownloadItem's we are currently observing. Generally there | |
| 92 // won't be any overlap with the above; once we see the final state | |
| 93 // on a DownloadItem, we'll stop observing it. | |
| 94 std::set<DownloadItem*> downloads_observed_; | |
| 95 | |
| 96 // The number of downloads to wait on completing. | |
| 97 size_t wait_count_; | |
| 98 | |
| 99 // The number of downloads entered in final state in initial | |
| 100 // ModelChanged(). We use |finished_downloads_| to track the incoming | |
| 101 // transitions to final state we should ignore, and to track the | |
| 102 // number of final state transitions that occurred between | |
| 103 // construction and return from wait. But some downloads may be in our | |
| 104 // final state (and thus be entered into |finished_downloads_|) when we | |
| 105 // construct this class. We don't want to count those in our transition | |
| 106 // to finished. | |
| 107 int finished_downloads_at_construction_; | |
| 108 | |
| 109 // Whether an internal message loop has been started and must be quit upon | |
| 110 // all downloads completing. | |
| 111 bool waiting_; | |
| 112 | |
| 113 // The state on which to consider the DownloadItem finished. | |
| 114 DownloadItem::DownloadState download_finished_state_; | |
| 115 | |
| 116 // True if we should transition the DownloadTestObserver to finished if | |
| 117 // the select file dialog comes up. | |
| 118 bool finish_on_select_file_; | |
| 119 | |
| 120 // True if we've seen the select file dialog. | |
| 121 bool select_file_dialog_seen_; | |
| 122 | |
| 123 // Action to take if a dangerous download is encountered. | |
| 124 DangerousDownloadAction dangerous_download_action_; | |
| 125 | |
| 126 // Holds the download ids which were dangerous. | |
| 127 std::set<int32> dangerous_downloads_seen_; | |
| 128 | |
| 129 DISALLOW_COPY_AND_ASSIGN(DownloadTestObserver); | |
| 130 }; | |
| 131 | |
| 132 // The WaitForFlush() method on this class returns after: | |
| 133 // * There are no IN_PROGRESS download items remaining on the | |
| 134 // DownloadManager. | |
| 135 // * There have been two round trip messages through the file and | |
| 136 // IO threads. | |
| 137 // This almost certainly means that a Download cancel has propagated through | |
| 138 // the system. | |
| 139 class DownloadTestFlushObserver | |
| 140 : public DownloadManager::Observer, | |
| 141 public DownloadItem::Observer, | |
| 142 public base::RefCountedThreadSafe<DownloadTestFlushObserver> { | |
| 143 public: | |
| 144 explicit DownloadTestFlushObserver(DownloadManager* download_manager); | |
| 145 | |
| 146 void WaitForFlush(); | |
| 147 | |
| 148 // DownloadsManager observer methods. | |
| 149 virtual void ModelChanged(); | |
|
achuithb
2011/10/13 23:05:08
OVERRIDE here and other 2 functions in this class
Randy Smith (Not in Mondays)
2011/10/14 00:37:41
Done.
| |
| 150 | |
| 151 // DownloadItem observer methods. | |
| 152 virtual void OnDownloadUpdated(DownloadItem* download); | |
| 153 virtual void OnDownloadOpened(DownloadItem* download) {} | |
| 154 | |
| 155 protected: | |
| 156 friend class base::RefCountedThreadSafe<DownloadTestFlushObserver>; | |
| 157 | |
| 158 virtual ~DownloadTestFlushObserver(); | |
| 159 | |
| 160 private: | |
| 161 // If we're waiting for that flush point, check the number | |
| 162 // of downloads in the IN_PROGRESS state and take appropriate | |
| 163 // action. If requested, also observes all downloads while iterating. | |
| 164 void CheckDownloadsInProgress(bool observe_downloads); | |
| 165 | |
| 166 void PingFileThread(int cycle); | |
| 167 | |
| 168 void PingIOThread(int cycle); | |
| 169 | |
| 170 DownloadManager* download_manager_; | |
| 171 std::set<DownloadItem*> downloads_observed_; | |
| 172 bool waiting_for_zero_inprogress_; | |
| 173 | |
| 174 DISALLOW_COPY_AND_ASSIGN(DownloadTestFlushObserver); | |
| 175 }; | |
| 176 | |
| 177 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TEST_OBSERVER_H_ | |
| OLD | NEW |