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 "chrome/browser/download/download_request_limiter.h" | 5 #include "chrome/browser/download/download_request_limiter.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "chrome/browser/content_settings/host_content_settings_map.h" | 9 #include "chrome/browser/content_settings/host_content_settings_map.h" |
10 #include "chrome/browser/download/download_request_infobar_delegate.h" | 10 #include "chrome/browser/download/download_request_infobar_delegate.h" |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 | 174 |
175 void SetHostContentSetting(WebContents* contents, ContentSetting setting) { | 175 void SetHostContentSetting(WebContents* contents, ContentSetting setting) { |
176 content_settings_->SetContentSetting( | 176 content_settings_->SetContentSetting( |
177 ContentSettingsPattern::FromURL(contents->GetURL()), | 177 ContentSettingsPattern::FromURL(contents->GetURL()), |
178 ContentSettingsPattern::Wildcard(), | 178 ContentSettingsPattern::Wildcard(), |
179 CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, | 179 CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, |
180 std::string(), | 180 std::string(), |
181 setting); | 181 setting); |
182 } | 182 } |
183 | 183 |
| 184 void BubbleManagerDocumentLoadCompleted() { |
| 185 PermissionBubbleManager::FromWebContents(web_contents())-> |
| 186 DocumentOnLoadCompletedInMainFrame(); |
| 187 } |
| 188 |
184 scoped_refptr<DownloadRequestLimiter> download_request_limiter_; | 189 scoped_refptr<DownloadRequestLimiter> download_request_limiter_; |
185 | 190 |
186 // The action that FakeCreate() should take. | 191 // The action that FakeCreate() should take. |
187 TestingAction testing_action_; | 192 TestingAction testing_action_; |
188 | 193 |
189 // Number of times ContinueDownload was invoked. | 194 // Number of times ContinueDownload was invoked. |
190 int continue_count_; | 195 int continue_count_; |
191 | 196 |
192 // Number of times CancelDownload was invoked. | 197 // Number of times CancelDownload was invoked. |
193 int cancel_count_; | 198 int cancel_count_; |
194 | 199 |
195 // Number of times ShouldAllowDownload was invoked. | 200 // Number of times ShouldAllowDownload was invoked. |
196 int ask_allow_count_; | 201 int ask_allow_count_; |
197 | 202 |
198 scoped_refptr<HostContentSettingsMap> content_settings_; | 203 scoped_refptr<HostContentSettingsMap> content_settings_; |
199 | 204 |
200 private: | 205 private: |
201 DownloadRequestInfoBarDelegate::FakeCreateCallback fake_create_callback_; | 206 DownloadRequestInfoBarDelegate::FakeCreateCallback fake_create_callback_; |
202 TestingProfile profile_; | 207 TestingProfile profile_; |
203 scoped_ptr<FakePermissionBubbleView> view_; | 208 scoped_ptr<FakePermissionBubbleView> view_; |
204 }; | 209 }; |
205 | 210 |
206 void FakePermissionBubbleView::Show( | 211 void FakePermissionBubbleView::Show( |
207 const std::vector<PermissionBubbleRequest*>& requests, | 212 const std::vector<PermissionBubbleRequest*>& requests, |
208 const std::vector<bool>& accept_state, | 213 const std::vector<bool>& accept_state, |
209 bool customization_mode) { | 214 bool customization_mode) { |
210 test_->AskAllow(); | 215 test_->AskAllow(); |
211 int action = test_->GetAction(); | 216 int action = test_->GetAction(); |
212 if (action == DownloadRequestLimiterTest::ACCEPT) { | 217 if (action == DownloadRequestLimiterTest::ACCEPT) { |
| 218 LOG(INFO) << "Test: accept"; |
213 delegate_->Accept(); | 219 delegate_->Accept(); |
214 } else if (action == DownloadRequestLimiterTest::CANCEL) { | 220 } else if (action == DownloadRequestLimiterTest::CANCEL) { |
| 221 LOG(INFO) << "Test: deny"; |
215 delegate_->Deny(); | 222 delegate_->Deny(); |
216 } else if (action == DownloadRequestLimiterTest::WAIT) { | 223 } else if (action == DownloadRequestLimiterTest::WAIT) { |
217 // do nothing. | 224 // do nothing. |
218 } else { | 225 } else { |
219 delegate_->Closing(); | 226 delegate_->Closing(); |
220 } | 227 } |
221 } | 228 } |
222 | 229 |
223 TEST_F(DownloadRequestLimiterTest, | 230 TEST_F(DownloadRequestLimiterTest, |
224 DownloadRequestLimiter_Allow) { | 231 DownloadRequestLimiter_Allow) { |
| 232 BubbleManagerDocumentLoadCompleted(); |
| 233 |
225 // All tabs should initially start at ALLOW_ONE_DOWNLOAD. | 234 // All tabs should initially start at ALLOW_ONE_DOWNLOAD. |
226 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 235 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
227 download_request_limiter_->GetDownloadStatus(web_contents())); | 236 download_request_limiter_->GetDownloadStatus(web_contents())); |
228 | 237 |
229 // Ask if the tab can do a download. This moves to PROMPT_BEFORE_DOWNLOAD. | 238 // Ask if the tab can do a download. This moves to PROMPT_BEFORE_DOWNLOAD. |
230 CanDownload(); | 239 CanDownload(); |
231 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 240 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
232 download_request_limiter_->GetDownloadStatus(web_contents())); | 241 download_request_limiter_->GetDownloadStatus(web_contents())); |
233 // We should have been told we can download. | 242 // We should have been told we can download. |
234 ExpectAndResetCounts(1, 0, 0, __LINE__); | 243 ExpectAndResetCounts(1, 0, 0, __LINE__); |
(...skipping 12 matching lines...) Expand all Loading... |
247 // The state is at allow_all, which means the delegate shouldn't be asked. | 256 // The state is at allow_all, which means the delegate shouldn't be asked. |
248 // We should have been told we can download. | 257 // We should have been told we can download. |
249 ExpectAndResetCounts(1, 0, 0, __LINE__); | 258 ExpectAndResetCounts(1, 0, 0, __LINE__); |
250 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | 259 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
251 download_request_limiter_->GetDownloadStatus(web_contents())); | 260 download_request_limiter_->GetDownloadStatus(web_contents())); |
252 } | 261 } |
253 | 262 |
254 TEST_F(DownloadRequestLimiterTest, | 263 TEST_F(DownloadRequestLimiterTest, |
255 DownloadRequestLimiter_ResetOnNavigation) { | 264 DownloadRequestLimiter_ResetOnNavigation) { |
256 NavigateAndCommit(GURL("http://foo.com/bar")); | 265 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 266 BubbleManagerDocumentLoadCompleted(); |
257 | 267 |
258 // Do two downloads, allowing the second so that we end up with allow all. | 268 // Do two downloads, allowing the second so that we end up with allow all. |
259 CanDownload(); | 269 CanDownload(); |
260 ExpectAndResetCounts(1, 0, 0, __LINE__); | 270 ExpectAndResetCounts(1, 0, 0, __LINE__); |
261 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 271 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
262 download_request_limiter_->GetDownloadStatus(web_contents())); | 272 download_request_limiter_->GetDownloadStatus(web_contents())); |
263 | 273 |
264 testing_action_ = ACCEPT; | 274 testing_action_ = ACCEPT; |
265 CanDownload(); | 275 CanDownload(); |
266 ExpectAndResetCounts(1, 0, 1, __LINE__); | 276 ExpectAndResetCounts(1, 0, 1, __LINE__); |
267 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | 277 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
268 download_request_limiter_->GetDownloadStatus(web_contents())); | 278 download_request_limiter_->GetDownloadStatus(web_contents())); |
269 | 279 |
270 // Navigate to a new URL with the same host, which shouldn't reset the allow | 280 // Navigate to a new URL with the same host, which shouldn't reset the allow |
271 // all state. | 281 // all state. |
272 NavigateAndCommit(GURL("http://foo.com/bar2")); | 282 NavigateAndCommit(GURL("http://foo.com/bar2")); |
| 283 BubbleManagerDocumentLoadCompleted(); |
273 CanDownload(); | 284 CanDownload(); |
274 ExpectAndResetCounts(1, 0, 0, __LINE__); | 285 ExpectAndResetCounts(1, 0, 0, __LINE__); |
275 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | 286 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
276 download_request_limiter_->GetDownloadStatus(web_contents())); | 287 download_request_limiter_->GetDownloadStatus(web_contents())); |
277 | 288 |
278 // Do a user gesture, because we're at allow all, this shouldn't change the | 289 // Do a user gesture, because we're at allow all, this shouldn't change the |
279 // state. | 290 // state. |
280 OnUserGesture(); | 291 OnUserGesture(); |
281 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | 292 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
282 download_request_limiter_->GetDownloadStatus(web_contents())); | 293 download_request_limiter_->GetDownloadStatus(web_contents())); |
283 | 294 |
284 // Navigate to a completely different host, which should reset the state. | 295 // Navigate to a completely different host, which should reset the state. |
285 NavigateAndCommit(GURL("http://fooey.com")); | 296 NavigateAndCommit(GURL("http://fooey.com")); |
| 297 BubbleManagerDocumentLoadCompleted(); |
286 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 298 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
287 download_request_limiter_->GetDownloadStatus(web_contents())); | 299 download_request_limiter_->GetDownloadStatus(web_contents())); |
288 | 300 |
289 // Do two downloads, allowing the second so that we end up with allow all. | 301 // Do two downloads, allowing the second so that we end up with allow all. |
290 CanDownload(); | 302 CanDownload(); |
291 ExpectAndResetCounts(1, 0, 0, __LINE__); | 303 ExpectAndResetCounts(1, 0, 0, __LINE__); |
292 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 304 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
293 download_request_limiter_->GetDownloadStatus(web_contents())); | 305 download_request_limiter_->GetDownloadStatus(web_contents())); |
294 | 306 |
295 testing_action_ = CANCEL; | 307 testing_action_ = CANCEL; |
296 CanDownload(); | 308 CanDownload(); |
297 ExpectAndResetCounts(0, 1, 1, __LINE__); | 309 ExpectAndResetCounts(0, 1, 1, __LINE__); |
298 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 310 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
299 download_request_limiter_->GetDownloadStatus(web_contents())); | 311 download_request_limiter_->GetDownloadStatus(web_contents())); |
300 | 312 |
301 // Navigate to a new URL with the same host, which shouldn't reset the allow | 313 // Navigate to a new URL with the same host, which shouldn't reset the allow |
302 // all state. | 314 // all state. |
303 NavigateAndCommit(GURL("http://fooey.com/bar2")); | 315 NavigateAndCommit(GURL("http://fooey.com/bar2")); |
| 316 BubbleManagerDocumentLoadCompleted(); |
304 CanDownload(); | 317 CanDownload(); |
305 ExpectAndResetCounts(0, 1, 0, __LINE__); | 318 ExpectAndResetCounts(0, 1, 0, __LINE__); |
306 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 319 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
307 download_request_limiter_->GetDownloadStatus(web_contents())); | 320 download_request_limiter_->GetDownloadStatus(web_contents())); |
308 } | 321 } |
309 | 322 |
310 TEST_F(DownloadRequestLimiterTest, | 323 TEST_F(DownloadRequestLimiterTest, |
311 DownloadRequestLimiter_ResetOnUserGesture) { | 324 DownloadRequestLimiter_ResetOnUserGesture) { |
312 NavigateAndCommit(GURL("http://foo.com/bar")); | 325 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 326 BubbleManagerDocumentLoadCompleted(); |
313 | 327 |
314 // Do one download, which should change to prompt before download. | 328 // Do one download, which should change to prompt before download. |
315 CanDownload(); | 329 CanDownload(); |
316 ExpectAndResetCounts(1, 0, 0, __LINE__); | 330 ExpectAndResetCounts(1, 0, 0, __LINE__); |
317 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 331 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
318 download_request_limiter_->GetDownloadStatus(web_contents())); | 332 download_request_limiter_->GetDownloadStatus(web_contents())); |
319 | 333 |
320 // Do a user gesture, which should reset back to allow one. | 334 // Do a user gesture, which should reset back to allow one. |
321 OnUserGesture(); | 335 OnUserGesture(); |
322 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 336 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
(...skipping 20 matching lines...) Expand all Loading... |
343 CanDownload(); | 357 CanDownload(); |
344 ExpectAndResetCounts(0, 1, 0, __LINE__); | 358 ExpectAndResetCounts(0, 1, 0, __LINE__); |
345 // And the state shouldn't have changed. | 359 // And the state shouldn't have changed. |
346 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 360 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
347 download_request_limiter_->GetDownloadStatus(web_contents())); | 361 download_request_limiter_->GetDownloadStatus(web_contents())); |
348 } | 362 } |
349 | 363 |
350 TEST_F(DownloadRequestLimiterTest, | 364 TEST_F(DownloadRequestLimiterTest, |
351 DownloadRequestLimiter_ResetOnReload) { | 365 DownloadRequestLimiter_ResetOnReload) { |
352 NavigateAndCommit(GURL("http://foo.com/bar")); | 366 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 367 BubbleManagerDocumentLoadCompleted(); |
353 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 368 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
354 download_request_limiter_->GetDownloadStatus(web_contents())); | 369 download_request_limiter_->GetDownloadStatus(web_contents())); |
355 | 370 |
356 // If the user refreshes the page without responding to the infobar, pretend | 371 // If the user refreshes the page without responding to the infobar, pretend |
357 // like the refresh is the initial load: they get 1 free download (probably | 372 // like the refresh is the initial load: they get 1 free download (probably |
358 // the same as the actual initial load), then an infobar. | 373 // the same as the actual initial load), then an infobar. |
359 testing_action_ = WAIT; | 374 testing_action_ = WAIT; |
360 | 375 |
361 CanDownload(); | 376 CanDownload(); |
362 ExpectAndResetCounts(1, 0, 0, __LINE__); | 377 ExpectAndResetCounts(1, 0, 0, __LINE__); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 445 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
431 CanDownloadFor(web_contents.get()); | 446 CanDownloadFor(web_contents.get()); |
432 ExpectAndResetCounts(1, 0, 0, __LINE__); | 447 ExpectAndResetCounts(1, 0, 0, __LINE__); |
433 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 448 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
434 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 449 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
435 } | 450 } |
436 | 451 |
437 TEST_F(DownloadRequestLimiterTest, | 452 TEST_F(DownloadRequestLimiterTest, |
438 DownloadRequestLimiter_SetHostContentSetting) { | 453 DownloadRequestLimiter_SetHostContentSetting) { |
439 NavigateAndCommit(GURL("http://foo.com/bar")); | 454 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 455 BubbleManagerDocumentLoadCompleted(); |
440 SetHostContentSetting(web_contents(), CONTENT_SETTING_ALLOW); | 456 SetHostContentSetting(web_contents(), CONTENT_SETTING_ALLOW); |
441 | 457 |
442 CanDownload(); | 458 CanDownload(); |
443 ExpectAndResetCounts(1, 0, 0, __LINE__); | 459 ExpectAndResetCounts(1, 0, 0, __LINE__); |
444 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 460 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
445 download_request_limiter_->GetDownloadStatus(web_contents())); | 461 download_request_limiter_->GetDownloadStatus(web_contents())); |
446 | 462 |
447 CanDownload(); | 463 CanDownload(); |
448 ExpectAndResetCounts(1, 0, 0, __LINE__); | 464 ExpectAndResetCounts(1, 0, 0, __LINE__); |
449 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 465 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
450 download_request_limiter_->GetDownloadStatus(web_contents())); | 466 download_request_limiter_->GetDownloadStatus(web_contents())); |
451 | 467 |
452 SetHostContentSetting(web_contents(), CONTENT_SETTING_BLOCK); | 468 SetHostContentSetting(web_contents(), CONTENT_SETTING_BLOCK); |
453 | 469 |
454 CanDownload(); | 470 CanDownload(); |
455 ExpectAndResetCounts(0, 1, 0, __LINE__); | 471 ExpectAndResetCounts(0, 1, 0, __LINE__); |
456 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 472 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
457 download_request_limiter_->GetDownloadStatus(web_contents())); | 473 download_request_limiter_->GetDownloadStatus(web_contents())); |
458 | 474 |
459 CanDownload(); | 475 CanDownload(); |
460 ExpectAndResetCounts(0, 1, 0, __LINE__); | 476 ExpectAndResetCounts(0, 1, 0, __LINE__); |
461 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 477 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
462 download_request_limiter_->GetDownloadStatus(web_contents())); | 478 download_request_limiter_->GetDownloadStatus(web_contents())); |
463 } | 479 } |
OLD | NEW |