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->ValidateDangerousDownload(); |
| 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->Remove(); |
| 44 } |
| 45 |
| 46 } // namespace |
| 47 |
21 DownloadUpdatedObserver::DownloadUpdatedObserver( | 48 DownloadUpdatedObserver::DownloadUpdatedObserver( |
22 DownloadItem* item, DownloadUpdatedObserver::EventFilter filter) | 49 DownloadItem* item, DownloadUpdatedObserver::EventFilter filter) |
23 : item_(item), | 50 : item_(item), |
24 filter_(filter), | 51 filter_(filter), |
25 waiting_(false), | 52 waiting_(false), |
26 event_seen_(false) { | 53 event_seen_(false) { |
27 item->AddObserver(this); | 54 item->AddObserver(this); |
28 } | 55 } |
29 | 56 |
30 DownloadUpdatedObserver::~DownloadUpdatedObserver() { | 57 DownloadUpdatedObserver::~DownloadUpdatedObserver() { |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 downloads_observed_.erase(it); | 161 downloads_observed_.erase(it); |
135 download->RemoveObserver(this); | 162 download->RemoveObserver(this); |
136 } | 163 } |
137 | 164 |
138 void DownloadTestObserver::OnDownloadUpdated(DownloadItem* download) { | 165 void DownloadTestObserver::OnDownloadUpdated(DownloadItem* download) { |
139 // Real UI code gets the user's response after returning from the observer. | 166 // Real UI code gets the user's response after returning from the observer. |
140 if (download->IsDangerous() && | 167 if (download->IsDangerous() && |
141 !ContainsKey(dangerous_downloads_seen_, download->GetId())) { | 168 !ContainsKey(dangerous_downloads_seen_, download->GetId())) { |
142 dangerous_downloads_seen_.insert(download->GetId()); | 169 dangerous_downloads_seen_.insert(download->GetId()); |
143 | 170 |
144 // Calling DangerousDownloadValidated() at this point will | 171 // Calling ValidateDangerousDownload() at this point will |
145 // cause the download to be completed twice. Do what the real UI | 172 // cause the download to be completed twice. Do what the real UI |
146 // code does: make the call as a delayed task. | 173 // code does: make the call as a delayed task. |
147 switch (dangerous_download_action_) { | 174 switch (dangerous_download_action_) { |
148 case ON_DANGEROUS_DOWNLOAD_ACCEPT: | 175 case ON_DANGEROUS_DOWNLOAD_ACCEPT: |
149 // Fake user click on "Accept". Delay the actual click, as the | 176 // Fake user click on "Accept". Delay the actual click, as the |
150 // real UI would. | 177 // real UI would. |
151 BrowserThread::PostTask( | 178 BrowserThread::PostTask( |
152 BrowserThread::UI, FROM_HERE, | 179 BrowserThread::UI, FROM_HERE, |
153 base::Bind(&DownloadTestObserver::AcceptDangerousDownload, | 180 base::Bind(&DownloadTestObserver::AcceptDangerousDownload, |
154 weak_factory_.GetWeakPtr(), | 181 weak_factory_.GetWeakPtr(), |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 base::MessageLoopForUI::current()->Quit(); | 242 base::MessageLoopForUI::current()->Quit(); |
216 } | 243 } |
217 | 244 |
218 void DownloadTestObserver::AcceptDangerousDownload(int32 download_id) { | 245 void DownloadTestObserver::AcceptDangerousDownload(int32 download_id) { |
219 // Download manager was shutdown before the UI thread could accept the | 246 // Download manager was shutdown before the UI thread could accept the |
220 // download. | 247 // download. |
221 if (!download_manager_) | 248 if (!download_manager_) |
222 return; | 249 return; |
223 DownloadItem* download = download_manager_->GetDownload(download_id); | 250 DownloadItem* download = download_manager_->GetDownload(download_id); |
224 if (download && (download->GetState() == DownloadItem::IN_PROGRESS)) | 251 if (download && (download->GetState() == DownloadItem::IN_PROGRESS)) |
225 download->DangerousDownloadValidated(); | 252 download->ValidateDangerousDownload(); |
226 } | 253 } |
227 | 254 |
228 void DownloadTestObserver::DenyDangerousDownload(int32 download_id) { | 255 void DownloadTestObserver::DenyDangerousDownload(int32 download_id) { |
229 // Download manager was shutdown before the UI thread could deny the | 256 // Download manager was shutdown before the UI thread could deny the |
230 // download. | 257 // download. |
231 if (!download_manager_) | 258 if (!download_manager_) |
232 return; | 259 return; |
233 DownloadItem* download = download_manager_->GetDownload(download_id); | 260 DownloadItem* download = download_manager_->GetDownload(download_id); |
234 if (download && (download->GetState() == DownloadItem::IN_PROGRESS)) { | 261 if (download && (download->GetState() == DownloadItem::IN_PROGRESS)) { |
235 download->Cancel(true); | 262 download->Remove(); |
236 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); | |
237 } | 263 } |
238 } | 264 } |
239 | 265 |
240 DownloadTestObserverTerminal::DownloadTestObserverTerminal( | 266 DownloadTestObserverTerminal::DownloadTestObserverTerminal( |
241 DownloadManager* download_manager, | 267 DownloadManager* download_manager, |
242 size_t wait_count, | 268 size_t wait_count, |
243 DangerousDownloadAction dangerous_download_action) | 269 DangerousDownloadAction dangerous_download_action) |
244 : DownloadTestObserver(download_manager, | 270 : DownloadTestObserver(download_manager, |
245 wait_count, | 271 wait_count, |
246 dangerous_download_action) { | 272 dangerous_download_action) { |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 base::MessageLoopForUI::current()->Quit(); | 447 base::MessageLoopForUI::current()->Quit(); |
422 } | 448 } |
423 | 449 |
424 const DownloadUrlParameters::OnStartedCallback | 450 const DownloadUrlParameters::OnStartedCallback |
425 DownloadTestItemCreationObserver::callback() { | 451 DownloadTestItemCreationObserver::callback() { |
426 return base::Bind( | 452 return base::Bind( |
427 &DownloadTestItemCreationObserver::DownloadItemCreationCallback, this); | 453 &DownloadTestItemCreationObserver::DownloadItemCreationCallback, this); |
428 } | 454 } |
429 | 455 |
430 } // namespace content | 456 } // namespace content |
OLD | NEW |