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 if (download_manager_) |
| 81 download_manager_->RemoveObserver(this); |
109 } | 82 } |
110 | 83 |
111 void DownloadTestObserver::Init() { | 84 void DownloadTestObserver::Init() { |
112 download_manager_->AddObserver(this); | 85 download_manager_->AddObserver(this); |
113 std::vector<DownloadItem*> downloads; | 86 std::vector<DownloadItem*> downloads; |
114 download_manager_->GetAllDownloads(&downloads); | 87 download_manager_->GetAllDownloads(&downloads); |
115 for (std::vector<DownloadItem*>::iterator it = downloads.begin(); | 88 for (std::vector<DownloadItem*>::iterator it = downloads.begin(); |
116 it != downloads.end(); ++it) { | 89 it != downloads.end(); ++it) { |
117 OnDownloadCreated(download_manager_, *it); | 90 OnDownloadCreated(download_manager_, *it); |
118 } | 91 } |
119 finished_downloads_at_construction_ = finished_downloads_.size(); | 92 finished_downloads_at_construction_ = finished_downloads_.size(); |
120 states_observed_.clear(); | 93 states_observed_.clear(); |
121 } | 94 } |
122 | 95 |
| 96 void DownloadTestObserver::ManagerGoingDown(DownloadManager* manager) { |
| 97 CHECK_EQ(manager, download_manager_); |
| 98 download_manager_ = NULL; |
| 99 SignalIfFinished(); |
| 100 } |
| 101 |
123 void DownloadTestObserver::WaitForFinished() { | 102 void DownloadTestObserver::WaitForFinished() { |
124 if (!IsFinished()) { | 103 if (!IsFinished()) { |
125 waiting_ = true; | 104 waiting_ = true; |
126 RunMessageLoop(); | 105 RunMessageLoop(); |
127 waiting_ = false; | 106 waiting_ = false; |
128 } | 107 } |
129 } | 108 } |
130 | 109 |
131 bool DownloadTestObserver::IsFinished() const { | 110 bool DownloadTestObserver::IsFinished() const { |
132 return (finished_downloads_.size() - finished_downloads_at_construction_ >= | 111 return (finished_downloads_.size() - finished_downloads_at_construction_ >= |
133 wait_count_); | 112 wait_count_) || (download_manager_ == NULL); |
134 } | 113 } |
135 | 114 |
136 void DownloadTestObserver::OnDownloadCreated( | 115 void DownloadTestObserver::OnDownloadCreated( |
137 DownloadManager* manager, | 116 DownloadManager* manager, |
138 DownloadItem* item) { | 117 DownloadItem* item) { |
139 // NOTE: This method is called both by DownloadManager when a download is | 118 // NOTE: This method is called both by DownloadManager when a download is |
140 // created as well as in DownloadTestObserver::Init() for downloads that | 119 // created as well as in DownloadTestObserver::Init() for downloads that |
141 // existed before |this| was created. | 120 // existed before |this| was created. |
142 OnDownloadUpdated(item); | 121 OnDownloadUpdated(item); |
143 DownloadSet::const_iterator finished_it(finished_downloads_.find(item)); | 122 DownloadSet::const_iterator finished_it(finished_downloads_.find(item)); |
(...skipping 20 matching lines...) Expand all Loading... |
164 | 143 |
165 // Calling DangerousDownloadValidated() at this point will | 144 // Calling DangerousDownloadValidated() at this point will |
166 // cause the download to be completed twice. Do what the real UI | 145 // cause the download to be completed twice. Do what the real UI |
167 // code does: make the call as a delayed task. | 146 // code does: make the call as a delayed task. |
168 switch (dangerous_download_action_) { | 147 switch (dangerous_download_action_) { |
169 case ON_DANGEROUS_DOWNLOAD_ACCEPT: | 148 case ON_DANGEROUS_DOWNLOAD_ACCEPT: |
170 // Fake user click on "Accept". Delay the actual click, as the | 149 // Fake user click on "Accept". Delay the actual click, as the |
171 // real UI would. | 150 // real UI would. |
172 BrowserThread::PostTask( | 151 BrowserThread::PostTask( |
173 BrowserThread::UI, FROM_HERE, | 152 BrowserThread::UI, FROM_HERE, |
174 base::Bind(&AcceptDangerousDownload, download_manager_, | 153 base::Bind(&DownloadTestObserver::AcceptDangerousDownload, |
| 154 weak_factory_.GetWeakPtr(), |
175 download->GetId())); | 155 download->GetId())); |
176 break; | 156 break; |
177 | 157 |
178 case ON_DANGEROUS_DOWNLOAD_DENY: | 158 case ON_DANGEROUS_DOWNLOAD_DENY: |
179 // Fake a user click on "Deny". Delay the actual click, as the | 159 // Fake a user click on "Deny". Delay the actual click, as the |
180 // real UI would. | 160 // real UI would. |
181 BrowserThread::PostTask( | 161 BrowserThread::PostTask( |
182 BrowserThread::UI, FROM_HERE, | 162 BrowserThread::UI, FROM_HERE, |
183 base::Bind(&DenyDangerousDownload, download_manager_, | 163 base::Bind(&DownloadTestObserver::DenyDangerousDownload, |
| 164 weak_factory_.GetWeakPtr(), |
184 download->GetId())); | 165 download->GetId())); |
185 break; | 166 break; |
186 | 167 |
187 case ON_DANGEROUS_DOWNLOAD_FAIL: | 168 case ON_DANGEROUS_DOWNLOAD_FAIL: |
188 ADD_FAILURE() << "Unexpected dangerous download item."; | 169 ADD_FAILURE() << "Unexpected dangerous download item."; |
189 break; | 170 break; |
190 | 171 |
191 case ON_DANGEROUS_DOWNLOAD_IGNORE: | 172 case ON_DANGEROUS_DOWNLOAD_IGNORE: |
192 break; | 173 break; |
193 | 174 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 states_observed_[download->GetState()]++; // Initializes to 0 the first time. | 208 states_observed_[download->GetState()]++; // Initializes to 0 the first time. |
228 | 209 |
229 SignalIfFinished(); | 210 SignalIfFinished(); |
230 } | 211 } |
231 | 212 |
232 void DownloadTestObserver::SignalIfFinished() { | 213 void DownloadTestObserver::SignalIfFinished() { |
233 if (waiting_ && IsFinished()) | 214 if (waiting_ && IsFinished()) |
234 base::MessageLoopForUI::current()->Quit(); | 215 base::MessageLoopForUI::current()->Quit(); |
235 } | 216 } |
236 | 217 |
| 218 void DownloadTestObserver::AcceptDangerousDownload(int32 download_id) { |
| 219 // Download manager was shutdown before the UI thread could accept the |
| 220 // download. |
| 221 if (!download_manager_) |
| 222 return; |
| 223 DownloadItem* download = download_manager_->GetDownload(download_id); |
| 224 if (download && (download->GetState() == DownloadItem::IN_PROGRESS)) |
| 225 download->DangerousDownloadValidated(); |
| 226 } |
| 227 |
| 228 void DownloadTestObserver::DenyDangerousDownload(int32 download_id) { |
| 229 // Download manager was shutdown before the UI thread could deny the |
| 230 // download. |
| 231 if (!download_manager_) |
| 232 return; |
| 233 DownloadItem* download = download_manager_->GetDownload(download_id); |
| 234 if (download && (download->GetState() == DownloadItem::IN_PROGRESS)) { |
| 235 download->Cancel(true); |
| 236 download->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); |
| 237 } |
| 238 } |
| 239 |
237 DownloadTestObserverTerminal::DownloadTestObserverTerminal( | 240 DownloadTestObserverTerminal::DownloadTestObserverTerminal( |
238 DownloadManager* download_manager, | 241 DownloadManager* download_manager, |
239 size_t wait_count, | 242 size_t wait_count, |
240 DangerousDownloadAction dangerous_download_action) | 243 DangerousDownloadAction dangerous_download_action) |
241 : DownloadTestObserver(download_manager, | 244 : DownloadTestObserver(download_manager, |
242 wait_count, | 245 wait_count, |
243 dangerous_download_action) { | 246 dangerous_download_action) { |
244 // You can't rely on overriden virtual functions in a base class constructor; | 247 // 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 | 248 // 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 | 249 // 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(); | 421 base::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 |