| 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 #include "content/public/test/download_test_observer.h" | 5 #include "content/public/test/download_test_observer.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // These functions take scoped_refptr's to DownloadManager because they | 22 // These functions take scoped_refptr's to DownloadManager because they |
| 23 // are posted to message queues, and hence may execute arbitrarily after | 23 // are posted to message queues, and hence may execute arbitrarily after |
| 24 // their actual posting. Once posted, there is no connection between | 24 // their actual posting. Once posted, there is no connection between |
| 25 // these routines and the DownloadTestObserver class from which | 25 // these routines and the DownloadTestObserver class from which |
| 26 // they came, so the DownloadTestObserver's reference to the | 26 // they came, so the DownloadTestObserver's reference to the |
| 27 // DownloadManager cannot be counted on to keep the DownloadManager around. | 27 // DownloadManager cannot be counted on to keep the DownloadManager around. |
| 28 | 28 |
| 29 // Fake user click on "Accept". | 29 // Fake user click on "Accept". |
| 30 void AcceptDangerousDownload(scoped_refptr<DownloadManager> download_manager, | 30 void AcceptDangerousDownload(scoped_refptr<DownloadManager> download_manager, |
| 31 int32 download_id) { | 31 int32 download_id) { |
| 32 DownloadItem* download = download_manager->GetDownloadItem(download_id); | 32 DownloadItem* download = download_manager->GetDownload(download_id); |
| 33 download->DangerousDownloadValidated(); | 33 if (download && (download->GetState() == DownloadItem::IN_PROGRESS)) |
| 34 download->DangerousDownloadValidated(); |
| 34 } | 35 } |
| 35 | 36 |
| 36 // Fake user click on "Deny". | 37 // Fake user click on "Deny". |
| 37 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager, | 38 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager, |
| 38 int32 download_id) { | 39 int32 download_id) { |
| 39 DownloadItem* download = download_manager->GetDownloadItem(download_id); | 40 DownloadItem* download = download_manager->GetDownload(download_id); |
| 40 ASSERT_TRUE(download->IsPartialDownload()); | 41 if (download && (download->GetState() == DownloadItem::IN_PROGRESS)) { |
| 41 download->Cancel(true); | 42 download->Cancel(true); |
| 42 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); | 43 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); |
| 44 } |
| 43 } | 45 } |
| 44 | 46 |
| 45 } // namespace | 47 } // namespace |
| 46 | 48 |
| 47 DownloadUpdatedObserver::DownloadUpdatedObserver( | 49 DownloadUpdatedObserver::DownloadUpdatedObserver( |
| 48 DownloadItem* item, DownloadUpdatedObserver::EventFilter filter) | 50 DownloadItem* item, DownloadUpdatedObserver::EventFilter filter) |
| 49 : item_(item), | 51 : item_(item), |
| 50 filter_(filter), | 52 filter_(filter), |
| 51 waiting_(false), | 53 waiting_(false), |
| 52 event_seen_(false) { | 54 event_seen_(false) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 | 101 |
| 100 DownloadTestObserver::~DownloadTestObserver() { | 102 DownloadTestObserver::~DownloadTestObserver() { |
| 101 for (DownloadSet::iterator it = downloads_observed_.begin(); | 103 for (DownloadSet::iterator it = downloads_observed_.begin(); |
| 102 it != downloads_observed_.end(); ++it) | 104 it != downloads_observed_.end(); ++it) |
| 103 (*it)->RemoveObserver(this); | 105 (*it)->RemoveObserver(this); |
| 104 | 106 |
| 105 download_manager_->RemoveObserver(this); | 107 download_manager_->RemoveObserver(this); |
| 106 } | 108 } |
| 107 | 109 |
| 108 void DownloadTestObserver::Init() { | 110 void DownloadTestObserver::Init() { |
| 109 download_manager_->AddObserver(this); // Will call initial ModelChanged(). | 111 download_manager_->AddObserver(this); |
| 112 // Regenerate DownloadItem observers. If there are any download items |
| 113 // in our final state, note them in |finished_downloads_| |
| 114 // (done by |OnDownloadUpdated()|). |
| 115 std::vector<DownloadItem*> downloads; |
| 116 download_manager_->GetAllDownloads(&downloads); |
| 117 |
| 118 for (std::vector<DownloadItem*>::iterator it = downloads.begin(); |
| 119 it != downloads.end(); ++it) { |
| 120 OnDownloadUpdated(*it); // Safe to call multiple times; checks state. |
| 121 |
| 122 DownloadSet::const_iterator finished_it(finished_downloads_.find(*it)); |
| 123 DownloadSet::iterator observed_it(downloads_observed_.find(*it)); |
| 124 |
| 125 // If it isn't finished and we're aren't observing it, start. |
| 126 if (finished_it == finished_downloads_.end() && |
| 127 observed_it == downloads_observed_.end()) { |
| 128 (*it)->AddObserver(this); |
| 129 downloads_observed_.insert(*it); |
| 130 } |
| 131 } |
| 110 finished_downloads_at_construction_ = finished_downloads_.size(); | 132 finished_downloads_at_construction_ = finished_downloads_.size(); |
| 111 states_observed_.clear(); | 133 states_observed_.clear(); |
| 112 } | 134 } |
| 113 | 135 |
| 114 void DownloadTestObserver::WaitForFinished() { | 136 void DownloadTestObserver::WaitForFinished() { |
| 115 if (!IsFinished()) { | 137 if (!IsFinished()) { |
| 116 waiting_ = true; | 138 waiting_ = true; |
| 117 RunMessageLoop(); | 139 RunMessageLoop(); |
| 118 waiting_ = false; | 140 waiting_ = false; |
| 119 } | 141 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 | 188 |
| 167 default: | 189 default: |
| 168 NOTREACHED(); | 190 NOTREACHED(); |
| 169 } | 191 } |
| 170 } | 192 } |
| 171 | 193 |
| 172 if (IsDownloadInFinalState(download)) | 194 if (IsDownloadInFinalState(download)) |
| 173 DownloadInFinalState(download); | 195 DownloadInFinalState(download); |
| 174 } | 196 } |
| 175 | 197 |
| 176 void DownloadTestObserver::ModelChanged(DownloadManager* manager) { | 198 void DownloadTestObserver::OnDownloadCreated( |
| 177 DCHECK_EQ(manager, download_manager_); | 199 DownloadManager* manager, DownloadItem* item) { |
| 200 OnDownloadUpdated(item); |
| 201 DownloadSet::const_iterator finished_it(finished_downloads_.find(item)); |
| 202 DownloadSet::iterator observed_it(downloads_observed_.find(item)); |
| 178 | 203 |
| 179 // Regenerate DownloadItem observers. If there are any download items | 204 // If it isn't finished and we're aren't observing it, start. |
| 180 // in our final state, note them in |finished_downloads_| | 205 if (finished_it == finished_downloads_.end() && |
| 181 // (done by |OnDownloadUpdated()|). | 206 observed_it == downloads_observed_.end()) { |
| 182 std::vector<DownloadItem*> downloads; | 207 item->AddObserver(this); |
| 183 download_manager_->GetAllDownloads(&downloads); | 208 downloads_observed_.insert(item); |
| 184 | |
| 185 for (std::vector<DownloadItem*>::iterator it = downloads.begin(); | |
| 186 it != downloads.end(); ++it) { | |
| 187 OnDownloadUpdated(*it); // Safe to call multiple times; checks state. | |
| 188 | |
| 189 DownloadSet::const_iterator finished_it(finished_downloads_.find(*it)); | |
| 190 DownloadSet::iterator observed_it(downloads_observed_.find(*it)); | |
| 191 | |
| 192 // If it isn't finished and we're aren't observing it, start. | |
| 193 if (finished_it == finished_downloads_.end() && | |
| 194 observed_it == downloads_observed_.end()) { | |
| 195 (*it)->AddObserver(this); | |
| 196 downloads_observed_.insert(*it); | |
| 197 continue; | |
| 198 } | |
| 199 | |
| 200 // If it is finished and we are observing it, stop. | |
| 201 if (finished_it != finished_downloads_.end() && | |
| 202 observed_it != downloads_observed_.end()) { | |
| 203 (*it)->RemoveObserver(this); | |
| 204 downloads_observed_.erase(observed_it); | |
| 205 continue; | |
| 206 } | |
| 207 } | 209 } |
| 208 } | 210 } |
| 209 | 211 |
| 210 size_t DownloadTestObserver::NumDangerousDownloadsSeen() const { | 212 size_t DownloadTestObserver::NumDangerousDownloadsSeen() const { |
| 211 return dangerous_downloads_seen_.size(); | 213 return dangerous_downloads_seen_.size(); |
| 212 } | 214 } |
| 213 | 215 |
| 214 size_t DownloadTestObserver::NumDownloadsSeenInState( | 216 size_t DownloadTestObserver::NumDownloadsSeenInState( |
| 215 DownloadItem::DownloadState state) const { | 217 DownloadItem::DownloadState state) const { |
| 216 StateMap::const_iterator it = states_observed_.find(state); | 218 StateMap::const_iterator it = states_observed_.find(state); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 // instead. In this case, it's because of |IsDownloadInFinalState()|. | 278 // instead. In this case, it's because of |IsDownloadInFinalState()|. |
| 277 Init(); | 279 Init(); |
| 278 } | 280 } |
| 279 | 281 |
| 280 DownloadTestObserverInProgress::~DownloadTestObserverInProgress() { | 282 DownloadTestObserverInProgress::~DownloadTestObserverInProgress() { |
| 281 } | 283 } |
| 282 | 284 |
| 283 | 285 |
| 284 bool DownloadTestObserverInProgress::IsDownloadInFinalState( | 286 bool DownloadTestObserverInProgress::IsDownloadInFinalState( |
| 285 DownloadItem* download) { | 287 DownloadItem* download) { |
| 286 return (download->GetState() == DownloadItem::IN_PROGRESS); | 288 return (download->GetState() == DownloadItem::IN_PROGRESS) && |
| 289 !download->GetTargetFilePath().empty(); |
| 287 } | 290 } |
| 288 | 291 |
| 289 DownloadTestFlushObserver::DownloadTestFlushObserver( | 292 DownloadTestFlushObserver::DownloadTestFlushObserver( |
| 290 DownloadManager* download_manager) | 293 DownloadManager* download_manager) |
| 291 : download_manager_(download_manager), | 294 : download_manager_(download_manager), |
| 292 waiting_for_zero_inprogress_(true) {} | 295 waiting_for_zero_inprogress_(true) {} |
| 293 | 296 |
| 294 void DownloadTestFlushObserver::WaitForFlush() { | 297 void DownloadTestFlushObserver::WaitForFlush() { |
| 295 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 298 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 296 download_manager_->AddObserver(this); | 299 download_manager_->AddObserver(this); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 MessageLoopForUI::current()->Quit(); | 421 MessageLoopForUI::current()->Quit(); |
| 419 } | 422 } |
| 420 | 423 |
| 421 const DownloadUrlParameters::OnStartedCallback | 424 const DownloadUrlParameters::OnStartedCallback |
| 422 DownloadTestItemCreationObserver::callback() { | 425 DownloadTestItemCreationObserver::callback() { |
| 423 return base::Bind( | 426 return base::Bind( |
| 424 &DownloadTestItemCreationObserver::DownloadItemCreationCallback, this); | 427 &DownloadTestItemCreationObserver::DownloadItemCreationCallback, this); |
| 425 } | 428 } |
| 426 | 429 |
| 427 } // namespace content | 430 } // namespace content |
| OLD | NEW |