| 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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 DownloadTestObserver::~DownloadTestObserver() { | 102 DownloadTestObserver::~DownloadTestObserver() { |
| 103 for (DownloadSet::iterator it = downloads_observed_.begin(); | 103 for (DownloadSet::iterator it = downloads_observed_.begin(); |
| 104 it != downloads_observed_.end(); ++it) | 104 it != downloads_observed_.end(); ++it) |
| 105 (*it)->RemoveObserver(this); | 105 (*it)->RemoveObserver(this); |
| 106 | 106 |
| 107 download_manager_->RemoveObserver(this); | 107 download_manager_->RemoveObserver(this); |
| 108 } | 108 } |
| 109 | 109 |
| 110 void DownloadTestObserver::Init() { | 110 void DownloadTestObserver::Init() { |
| 111 download_manager_->AddObserver(this); | 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; | 112 std::vector<DownloadItem*> downloads; |
| 116 download_manager_->GetAllDownloads(&downloads); | 113 download_manager_->GetAllDownloads(&downloads); |
| 117 | |
| 118 for (std::vector<DownloadItem*>::iterator it = downloads.begin(); | 114 for (std::vector<DownloadItem*>::iterator it = downloads.begin(); |
| 119 it != downloads.end(); ++it) { | 115 it != downloads.end(); ++it) { |
| 120 OnDownloadUpdated(*it); // Safe to call multiple times; checks state. | 116 OnDownloadCreated(download_manager_, *it); |
| 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 } | 117 } |
| 132 finished_downloads_at_construction_ = finished_downloads_.size(); | 118 finished_downloads_at_construction_ = finished_downloads_.size(); |
| 133 states_observed_.clear(); | 119 states_observed_.clear(); |
| 134 } | 120 } |
| 135 | 121 |
| 136 void DownloadTestObserver::WaitForFinished() { | 122 void DownloadTestObserver::WaitForFinished() { |
| 137 if (!IsFinished()) { | 123 if (!IsFinished()) { |
| 138 waiting_ = true; | 124 waiting_ = true; |
| 139 RunMessageLoop(); | 125 RunMessageLoop(); |
| 140 waiting_ = false; | 126 waiting_ = false; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 NOTREACHED(); | 176 NOTREACHED(); |
| 191 } | 177 } |
| 192 } | 178 } |
| 193 | 179 |
| 194 if (IsDownloadInFinalState(download)) | 180 if (IsDownloadInFinalState(download)) |
| 195 DownloadInFinalState(download); | 181 DownloadInFinalState(download); |
| 196 } | 182 } |
| 197 | 183 |
| 198 void DownloadTestObserver::OnDownloadCreated( | 184 void DownloadTestObserver::OnDownloadCreated( |
| 199 DownloadManager* manager, DownloadItem* item) { | 185 DownloadManager* manager, DownloadItem* item) { |
| 186 DCHECK_EQ(manager, download_manager_); |
| 200 OnDownloadUpdated(item); | 187 OnDownloadUpdated(item); |
| 188 |
| 201 DownloadSet::const_iterator finished_it(finished_downloads_.find(item)); | 189 DownloadSet::const_iterator finished_it(finished_downloads_.find(item)); |
| 202 DownloadSet::iterator observed_it(downloads_observed_.find(item)); | 190 DownloadSet::iterator observed_it(downloads_observed_.find(item)); |
| 203 | 191 |
| 204 // If it isn't finished and we're aren't observing it, start. | 192 // If it isn't finished and we're aren't observing it, start. |
| 205 if (finished_it == finished_downloads_.end() && | 193 if (finished_it == finished_downloads_.end() && |
| 206 observed_it == downloads_observed_.end()) { | 194 observed_it == downloads_observed_.end()) { |
| 207 item->AddObserver(this); | 195 item->AddObserver(this); |
| 208 downloads_observed_.insert(item); | 196 downloads_observed_.insert(item); |
| 209 } | 197 } |
| 210 } | 198 } |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 MessageLoopForUI::current()->Quit(); | 411 MessageLoopForUI::current()->Quit(); |
| 424 } | 412 } |
| 425 | 413 |
| 426 const DownloadUrlParameters::OnStartedCallback | 414 const DownloadUrlParameters::OnStartedCallback |
| 427 DownloadTestItemCreationObserver::callback() { | 415 DownloadTestItemCreationObserver::callback() { |
| 428 return base::Bind( | 416 return base::Bind( |
| 429 &DownloadTestItemCreationObserver::DownloadItemCreationCallback, this); | 417 &DownloadTestItemCreationObserver::DownloadItemCreationCallback, this); |
| 430 } | 418 } |
| 431 | 419 |
| 432 } // namespace content | 420 } // namespace content |
| OLD | NEW |