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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 | 99 |
100 DownloadTestObserver::~DownloadTestObserver() { | 100 DownloadTestObserver::~DownloadTestObserver() { |
101 for (DownloadSet::iterator it = downloads_observed_.begin(); | 101 for (DownloadSet::iterator it = downloads_observed_.begin(); |
102 it != downloads_observed_.end(); ++it) | 102 it != downloads_observed_.end(); ++it) |
103 (*it)->RemoveObserver(this); | 103 (*it)->RemoveObserver(this); |
104 | 104 |
105 download_manager_->RemoveObserver(this); | 105 download_manager_->RemoveObserver(this); |
106 } | 106 } |
107 | 107 |
108 void DownloadTestObserver::Init() { | 108 void DownloadTestObserver::Init() { |
109 download_manager_->AddObserver(this); // Will call initial ModelChanged(). | 109 download_manager_->AddObserver(this); |
110 std::vector<DownloadItem*> downloads; | |
111 download_manager_->GetAllDownloads(&downloads); | |
112 for (std::vector<DownloadItem*>::iterator it = downloads.begin(); | |
113 it != downloads.end(); ++it) { | |
114 OnDownloadCreated(download_manager_, *it); | |
115 } | |
110 finished_downloads_at_construction_ = finished_downloads_.size(); | 116 finished_downloads_at_construction_ = finished_downloads_.size(); |
111 states_observed_.clear(); | 117 states_observed_.clear(); |
112 } | 118 } |
113 | 119 |
114 void DownloadTestObserver::WaitForFinished() { | 120 void DownloadTestObserver::WaitForFinished() { |
115 if (!IsFinished()) { | 121 if (!IsFinished()) { |
116 waiting_ = true; | 122 waiting_ = true; |
117 RunMessageLoop(); | 123 RunMessageLoop(); |
118 waiting_ = false; | 124 waiting_ = false; |
119 } | 125 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 | 172 |
167 default: | 173 default: |
168 NOTREACHED(); | 174 NOTREACHED(); |
169 } | 175 } |
170 } | 176 } |
171 | 177 |
172 if (IsDownloadInFinalState(download)) | 178 if (IsDownloadInFinalState(download)) |
173 DownloadInFinalState(download); | 179 DownloadInFinalState(download); |
174 } | 180 } |
175 | 181 |
176 void DownloadTestObserver::ModelChanged(DownloadManager* manager) { | 182 void DownloadTestObserver::OnDownloadCreated( |
183 DownloadManager* manager, DownloadItem* item) { | |
177 DCHECK_EQ(manager, download_manager_); | 184 DCHECK_EQ(manager, download_manager_); |
185 OnDownloadUpdated(item); | |
178 | 186 |
179 // Regenerate DownloadItem observers. If there are any download items | 187 DownloadSet::const_iterator finished_it(finished_downloads_.find(item)); |
180 // in our final state, note them in |finished_downloads_| | 188 DownloadSet::iterator observed_it(downloads_observed_.find(item)); |
181 // (done by |OnDownloadUpdated()|). | |
182 std::vector<DownloadItem*> downloads; | |
183 download_manager_->GetAllDownloads(&downloads); | |
184 | 189 |
185 for (std::vector<DownloadItem*>::iterator it = downloads.begin(); | 190 // If it isn't finished and we're aren't observing it, start. |
186 it != downloads.end(); ++it) { | 191 if (finished_it == finished_downloads_.end() && |
187 OnDownloadUpdated(*it); // Safe to call multiple times; checks state. | 192 observed_it == downloads_observed_.end()) { |
193 item->AddObserver(this); | |
194 downloads_observed_.insert(item); | |
195 } | |
188 | 196 |
189 DownloadSet::const_iterator finished_it(finished_downloads_.find(*it)); | 197 // If it is finished and we are observing it, stop. |
190 DownloadSet::iterator observed_it(downloads_observed_.find(*it)); | 198 if (finished_it != finished_downloads_.end() && |
191 | 199 observed_it != downloads_observed_.end()) { |
192 // If it isn't finished and we're aren't observing it, start. | 200 item->RemoveObserver(this); |
193 if (finished_it == finished_downloads_.end() && | 201 downloads_observed_.erase(observed_it); |
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 } | 202 } |
208 } | 203 } |
209 | 204 |
210 size_t DownloadTestObserver::NumDangerousDownloadsSeen() const { | 205 size_t DownloadTestObserver::NumDangerousDownloadsSeen() const { |
211 return dangerous_downloads_seen_.size(); | 206 return dangerous_downloads_seen_.size(); |
212 } | 207 } |
213 | 208 |
214 size_t DownloadTestObserver::NumDownloadsSeenInState( | 209 size_t DownloadTestObserver::NumDownloadsSeenInState( |
215 DownloadItem::DownloadState state) const { | 210 DownloadItem::DownloadState state) const { |
216 StateMap::const_iterator it = states_observed_.find(state); | 211 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()|. | 271 // instead. In this case, it's because of |IsDownloadInFinalState()|. |
277 Init(); | 272 Init(); |
278 } | 273 } |
279 | 274 |
280 DownloadTestObserverInProgress::~DownloadTestObserverInProgress() { | 275 DownloadTestObserverInProgress::~DownloadTestObserverInProgress() { |
281 } | 276 } |
282 | 277 |
283 | 278 |
284 bool DownloadTestObserverInProgress::IsDownloadInFinalState( | 279 bool DownloadTestObserverInProgress::IsDownloadInFinalState( |
285 DownloadItem* download) { | 280 DownloadItem* download) { |
286 return (download->GetState() == DownloadItem::IN_PROGRESS); | 281 return (download->GetState() == DownloadItem::IN_PROGRESS) && |
282 !download->GetTargetFilePath().empty(); | |
Randy Smith (Not in Mondays)
2012/09/24 18:03:25
It's in test code, so I don't have strong feelings
benjhayden
2012/09/24 20:12:11
Could this CL (DownloadHistory-is-a-Observer) wait
Randy Smith (Not in Mondays)
2012/11/02 23:31:24
I suppose, if you're volunteering. But how would
benjhayden
2012/11/06 20:01:14
Sorry, I don't know what I was thinking.
Yes, this
| |
287 } | 283 } |
288 | 284 |
289 DownloadTestFlushObserver::DownloadTestFlushObserver( | 285 DownloadTestFlushObserver::DownloadTestFlushObserver( |
290 DownloadManager* download_manager) | 286 DownloadManager* download_manager) |
291 : download_manager_(download_manager), | 287 : download_manager_(download_manager), |
292 waiting_for_zero_inprogress_(true) {} | 288 waiting_for_zero_inprogress_(true) {} |
293 | 289 |
294 void DownloadTestFlushObserver::WaitForFlush() { | 290 void DownloadTestFlushObserver::WaitForFlush() { |
295 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 291 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
296 download_manager_->AddObserver(this); | 292 download_manager_->AddObserver(this); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
418 MessageLoopForUI::current()->Quit(); | 414 MessageLoopForUI::current()->Quit(); |
419 } | 415 } |
420 | 416 |
421 const DownloadUrlParameters::OnStartedCallback | 417 const DownloadUrlParameters::OnStartedCallback |
422 DownloadTestItemCreationObserver::callback() { | 418 DownloadTestItemCreationObserver::callback() { |
423 return base::Bind( | 419 return base::Bind( |
424 &DownloadTestItemCreationObserver::DownloadItemCreationCallback, this); | 420 &DownloadTestItemCreationObserver::DownloadItemCreationCallback, this); |
425 } | 421 } |
426 | 422 |
427 } // namespace content | 423 } // namespace content |
OLD | NEW |