Chromium Code Reviews| 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" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/threading/sequenced_worker_pool.h" | 13 #include "base/threading/sequenced_worker_pool.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/download_url_parameters.h" | 15 #include "content/public/browser/download_url_parameters.h" |
| 16 #include "content/public/test/test_utils.h" | 16 #include "content/public/test/test_utils.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 | 20 |
| 21 namespace { | |
| 22 | |
| 23 // These functions take scoped_refptr's to DownloadManager because they | |
| 24 // are posted to message queues, and hence may execute arbitrarily after | |
| 25 // their actual posting. Once posted, there is no connection between | |
| 26 // these routines and the DownloadTestObserver class from which | |
| 27 // they came, so the DownloadTestObserver's reference to the | |
| 28 // DownloadManager cannot be counted on to keep the DownloadManager around. | |
| 29 | |
| 30 // Fake user click on "Accept". | |
| 31 void AcceptDangerousDownload(scoped_refptr<DownloadManager> download_manager, | |
| 32 int32 download_id) { | |
| 33 DownloadItem* download = download_manager->GetDownload(download_id); | |
| 34 if (download && (download->GetState() == DownloadItem::IN_PROGRESS)) | |
| 35 download->DangerousDownloadValidated(); | |
| 36 } | |
| 37 | |
| 38 // Fake user click on "Deny". | |
| 39 void DenyDangerousDownload(scoped_refptr<DownloadManager> download_manager, | |
| 40 int32 download_id) { | |
| 41 DownloadItem* download = download_manager->GetDownload(download_id); | |
| 42 if (download && (download->GetState() == DownloadItem::IN_PROGRESS)) { | |
| 43 download->Cancel(true); | |
| 44 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 DownloadUpdatedObserver::DownloadUpdatedObserver( | 21 DownloadUpdatedObserver::DownloadUpdatedObserver( |
| 51 DownloadItem* item, DownloadUpdatedObserver::EventFilter filter) | 22 DownloadItem* item, DownloadUpdatedObserver::EventFilter filter) |
| 52 : item_(item), | 23 : item_(item), |
| 53 filter_(filter), | 24 filter_(filter), |
| 54 waiting_(false), | 25 waiting_(false), |
| 55 event_seen_(false) { | 26 event_seen_(false) { |
| 56 item->AddObserver(this); | 27 item->AddObserver(this); |
| 57 } | 28 } |
| 58 | 29 |
| 59 DownloadUpdatedObserver::~DownloadUpdatedObserver() { | 30 DownloadUpdatedObserver::~DownloadUpdatedObserver() { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 90 } | 61 } |
| 91 | 62 |
| 92 DownloadTestObserver::DownloadTestObserver( | 63 DownloadTestObserver::DownloadTestObserver( |
| 93 DownloadManager* download_manager, | 64 DownloadManager* download_manager, |
| 94 size_t wait_count, | 65 size_t wait_count, |
| 95 DangerousDownloadAction dangerous_download_action) | 66 DangerousDownloadAction dangerous_download_action) |
| 96 : download_manager_(download_manager), | 67 : download_manager_(download_manager), |
| 97 wait_count_(wait_count), | 68 wait_count_(wait_count), |
| 98 finished_downloads_at_construction_(0), | 69 finished_downloads_at_construction_(0), |
| 99 waiting_(false), | 70 waiting_(false), |
| 100 dangerous_download_action_(dangerous_download_action) { | 71 dangerous_download_action_(dangerous_download_action), |
| 72 weak_factory_(this) { | |
| 101 } | 73 } |
| 102 | 74 |
| 103 DownloadTestObserver::~DownloadTestObserver() { | 75 DownloadTestObserver::~DownloadTestObserver() { |
| 104 for (DownloadSet::iterator it = downloads_observed_.begin(); | 76 for (DownloadSet::iterator it = downloads_observed_.begin(); |
| 105 it != downloads_observed_.end(); ++it) | 77 it != downloads_observed_.end(); ++it) |
| 106 (*it)->RemoveObserver(this); | 78 (*it)->RemoveObserver(this); |
| 107 | 79 |
| 108 download_manager_->RemoveObserver(this); | 80 download_manager_->RemoveObserver(this); |
|
benjhayden
2013/05/17 14:50:03
if (download_manager_)
cmarcelo
2013/05/20 15:22:06
You're right, thanks.
| |
| 109 } | 81 } |
| 110 | 82 |
| 111 void DownloadTestObserver::Init() { | 83 void DownloadTestObserver::Init() { |
| 112 download_manager_->AddObserver(this); | 84 download_manager_->AddObserver(this); |
| 113 std::vector<DownloadItem*> downloads; | 85 std::vector<DownloadItem*> downloads; |
| 114 download_manager_->GetAllDownloads(&downloads); | 86 download_manager_->GetAllDownloads(&downloads); |
| 115 for (std::vector<DownloadItem*>::iterator it = downloads.begin(); | 87 for (std::vector<DownloadItem*>::iterator it = downloads.begin(); |
| 116 it != downloads.end(); ++it) { | 88 it != downloads.end(); ++it) { |
| 117 OnDownloadCreated(download_manager_, *it); | 89 OnDownloadCreated(download_manager_, *it); |
| 118 } | 90 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 141 // existed before |this| was created. | 113 // existed before |this| was created. |
| 142 OnDownloadUpdated(item); | 114 OnDownloadUpdated(item); |
| 143 DownloadSet::const_iterator finished_it(finished_downloads_.find(item)); | 115 DownloadSet::const_iterator finished_it(finished_downloads_.find(item)); |
| 144 // If it isn't finished, start observing it. | 116 // If it isn't finished, start observing it. |
| 145 if (finished_it == finished_downloads_.end()) { | 117 if (finished_it == finished_downloads_.end()) { |
| 146 item->AddObserver(this); | 118 item->AddObserver(this); |
| 147 downloads_observed_.insert(item); | 119 downloads_observed_.insert(item); |
| 148 } | 120 } |
| 149 } | 121 } |
| 150 | 122 |
| 123 void DownloadTestObserver::ManagerGoingDown(DownloadManager* manager) { | |
| 124 ASSERT_TRUE(manager == download_manager_); | |
| 125 download_manager_ = NULL; | |
| 126 } | |
| 127 | |
| 151 void DownloadTestObserver::OnDownloadDestroyed(DownloadItem* download) { | 128 void DownloadTestObserver::OnDownloadDestroyed(DownloadItem* download) { |
| 152 // Stop observing. Do not do anything with it, as it is about to be gone. | 129 // Stop observing. Do not do anything with it, as it is about to be gone. |
| 153 DownloadSet::iterator it = downloads_observed_.find(download); | 130 DownloadSet::iterator it = downloads_observed_.find(download); |
| 154 ASSERT_TRUE(it != downloads_observed_.end()); | 131 ASSERT_TRUE(it != downloads_observed_.end()); |
| 155 downloads_observed_.erase(it); | 132 downloads_observed_.erase(it); |
| 156 download->RemoveObserver(this); | 133 download->RemoveObserver(this); |
| 157 } | 134 } |
| 158 | 135 |
| 159 void DownloadTestObserver::OnDownloadUpdated(DownloadItem* download) { | 136 void DownloadTestObserver::OnDownloadUpdated(DownloadItem* download) { |
| 160 // Real UI code gets the user's response after returning from the observer. | 137 // Real UI code gets the user's response after returning from the observer. |
| 161 if (download->IsDangerous() && | 138 if (download->IsDangerous() && |
| 162 !ContainsKey(dangerous_downloads_seen_, download->GetId())) { | 139 !ContainsKey(dangerous_downloads_seen_, download->GetId())) { |
| 163 dangerous_downloads_seen_.insert(download->GetId()); | 140 dangerous_downloads_seen_.insert(download->GetId()); |
| 164 | 141 |
| 165 // Calling DangerousDownloadValidated() at this point will | 142 // Calling DangerousDownloadValidated() at this point will |
| 166 // cause the download to be completed twice. Do what the real UI | 143 // cause the download to be completed twice. Do what the real UI |
| 167 // code does: make the call as a delayed task. | 144 // code does: make the call as a delayed task. |
| 168 switch (dangerous_download_action_) { | 145 switch (dangerous_download_action_) { |
| 169 case ON_DANGEROUS_DOWNLOAD_ACCEPT: | 146 case ON_DANGEROUS_DOWNLOAD_ACCEPT: |
| 170 // Fake user click on "Accept". Delay the actual click, as the | 147 // Fake user click on "Accept". Delay the actual click, as the |
| 171 // real UI would. | 148 // real UI would. |
| 172 BrowserThread::PostTask( | 149 BrowserThread::PostTask( |
| 173 BrowserThread::UI, FROM_HERE, | 150 BrowserThread::UI, FROM_HERE, |
| 174 base::Bind(&AcceptDangerousDownload, download_manager_, | 151 base::Bind(&DownloadTestObserver::AcceptDangerousDownload, |
| 175 download->GetId())); | 152 weak_factory_.GetWeakPtr(), download->GetId())); |
| 176 break; | 153 break; |
| 177 | 154 |
| 178 case ON_DANGEROUS_DOWNLOAD_DENY: | 155 case ON_DANGEROUS_DOWNLOAD_DENY: |
| 179 // Fake a user click on "Deny". Delay the actual click, as the | 156 // Fake a user click on "Deny". Delay the actual click, as the |
| 180 // real UI would. | 157 // real UI would. |
| 181 BrowserThread::PostTask( | 158 BrowserThread::PostTask( |
| 182 BrowserThread::UI, FROM_HERE, | 159 BrowserThread::UI, FROM_HERE, |
| 183 base::Bind(&DenyDangerousDownload, download_manager_, | 160 base::Bind(&DownloadTestObserver::DenyDangerousDownload, |
| 184 download->GetId())); | 161 weak_factory_.GetWeakPtr(), download->GetId())); |
| 185 break; | 162 break; |
| 186 | 163 |
| 187 case ON_DANGEROUS_DOWNLOAD_FAIL: | 164 case ON_DANGEROUS_DOWNLOAD_FAIL: |
| 188 ADD_FAILURE() << "Unexpected dangerous download item."; | 165 ADD_FAILURE() << "Unexpected dangerous download item."; |
| 189 break; | 166 break; |
| 190 | 167 |
| 191 case ON_DANGEROUS_DOWNLOAD_IGNORE: | 168 case ON_DANGEROUS_DOWNLOAD_IGNORE: |
| 192 break; | 169 break; |
| 193 | 170 |
| 194 default: | 171 default: |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 states_observed_[download->GetState()]++; // Initializes to 0 the first time. | 204 states_observed_[download->GetState()]++; // Initializes to 0 the first time. |
| 228 | 205 |
| 229 SignalIfFinished(); | 206 SignalIfFinished(); |
| 230 } | 207 } |
| 231 | 208 |
| 232 void DownloadTestObserver::SignalIfFinished() { | 209 void DownloadTestObserver::SignalIfFinished() { |
| 233 if (waiting_ && IsFinished()) | 210 if (waiting_ && IsFinished()) |
| 234 base::MessageLoopForUI::current()->Quit(); | 211 base::MessageLoopForUI::current()->Quit(); |
| 235 } | 212 } |
| 236 | 213 |
| 214 void DownloadTestObserver::AcceptDangerousDownload(int32_t download_id) { | |
| 215 // Download manager was shutdown before the UI thread could accept | |
| 216 // the download. | |
| 217 if (!download_manager_) | |
| 218 return; | |
| 219 DownloadItem* download = download_manager_->GetDownload(download_id); | |
| 220 if (download && (download->GetState() == DownloadItem::IN_PROGRESS)) | |
| 221 download->DangerousDownloadValidated(); | |
| 222 } | |
| 223 | |
| 224 void DownloadTestObserver::DenyDangerousDownload(int32_t download_id) { | |
| 225 // Download manager was shutdown before the UI thread could deny the | |
| 226 // download. | |
| 227 if (!download_manager_) | |
| 228 return; | |
| 229 DownloadItem* download = download_manager_->GetDownload(download_id); | |
| 230 if (download && (download->GetState() == DownloadItem::IN_PROGRESS)) { | |
| 231 download->Cancel(true); | |
| 232 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); | |
| 233 } | |
| 234 } | |
| 235 | |
| 237 DownloadTestObserverTerminal::DownloadTestObserverTerminal( | 236 DownloadTestObserverTerminal::DownloadTestObserverTerminal( |
| 238 DownloadManager* download_manager, | 237 DownloadManager* download_manager, |
| 239 size_t wait_count, | 238 size_t wait_count, |
| 240 DangerousDownloadAction dangerous_download_action) | 239 DangerousDownloadAction dangerous_download_action) |
| 241 : DownloadTestObserver(download_manager, | 240 : DownloadTestObserver(download_manager, |
| 242 wait_count, | 241 wait_count, |
| 243 dangerous_download_action) { | 242 dangerous_download_action) { |
| 244 // You can't rely on overriden virtual functions in a base class constructor; | 243 // You can't rely on overriden virtual functions in a base class constructor; |
| 245 // the virtual function table hasn't been set up yet. So, we have to do any | 244 // the virtual function table hasn't been set up yet. So, we have to do any |
| 246 // work that depends on those functions in the derived class constructor | 245 // work that depends on those functions in the derived class constructor |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 418 base::MessageLoopForUI::current()->Quit(); | 417 base::MessageLoopForUI::current()->Quit(); |
| 419 } | 418 } |
| 420 | 419 |
| 421 const DownloadUrlParameters::OnStartedCallback | 420 const DownloadUrlParameters::OnStartedCallback |
| 422 DownloadTestItemCreationObserver::callback() { | 421 DownloadTestItemCreationObserver::callback() { |
| 423 return base::Bind( | 422 return base::Bind( |
| 424 &DownloadTestItemCreationObserver::DownloadItemCreationCallback, this); | 423 &DownloadTestItemCreationObserver::DownloadItemCreationCallback, this); |
| 425 } | 424 } |
| 426 | 425 |
| 427 } // namespace content | 426 } // namespace content |
| OLD | NEW |