| 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 <sstream> | 5 #include <sstream> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 112 |
| 113 namespace { | 113 namespace { |
| 114 | 114 |
| 115 class CreatedObserver : public content::DownloadManager::Observer { | 115 class CreatedObserver : public content::DownloadManager::Observer { |
| 116 public: | 116 public: |
| 117 explicit CreatedObserver(content::DownloadManager* manager) | 117 explicit CreatedObserver(content::DownloadManager* manager) |
| 118 : manager_(manager), | 118 : manager_(manager), |
| 119 waiting_(false) { | 119 waiting_(false) { |
| 120 manager->AddObserver(this); | 120 manager->AddObserver(this); |
| 121 } | 121 } |
| 122 virtual ~CreatedObserver() { | 122 ~CreatedObserver() override { |
| 123 if (manager_) | 123 if (manager_) |
| 124 manager_->RemoveObserver(this); | 124 manager_->RemoveObserver(this); |
| 125 } | 125 } |
| 126 | 126 |
| 127 void Wait() { | 127 void Wait() { |
| 128 std::vector<DownloadItem*> downloads; | 128 std::vector<DownloadItem*> downloads; |
| 129 manager_->GetAllDownloads(&downloads); | 129 manager_->GetAllDownloads(&downloads); |
| 130 if (!downloads.empty()) | 130 if (!downloads.empty()) |
| 131 return; | 131 return; |
| 132 waiting_ = true; | 132 waiting_ = true; |
| 133 content::RunMessageLoop(); | 133 content::RunMessageLoop(); |
| 134 waiting_ = false; | 134 waiting_ = false; |
| 135 } | 135 } |
| 136 | 136 |
| 137 private: | 137 private: |
| 138 virtual void OnDownloadCreated(content::DownloadManager* manager, | 138 void OnDownloadCreated(content::DownloadManager* manager, |
| 139 content::DownloadItem* item) override { | 139 content::DownloadItem* item) override { |
| 140 DCHECK_EQ(manager_, manager); | 140 DCHECK_EQ(manager_, manager); |
| 141 if (waiting_) | 141 if (waiting_) |
| 142 base::MessageLoopForUI::current()->Quit(); | 142 base::MessageLoopForUI::current()->Quit(); |
| 143 } | 143 } |
| 144 | 144 |
| 145 content::DownloadManager* manager_; | 145 content::DownloadManager* manager_; |
| 146 bool waiting_; | 146 bool waiting_; |
| 147 | 147 |
| 148 DISALLOW_COPY_AND_ASSIGN(CreatedObserver); | 148 DISALLOW_COPY_AND_ASSIGN(CreatedObserver); |
| 149 }; | 149 }; |
| 150 | 150 |
| 151 class PercentWaiter : public content::DownloadItem::Observer { | 151 class PercentWaiter : public content::DownloadItem::Observer { |
| 152 public: | 152 public: |
| 153 explicit PercentWaiter(DownloadItem* item) | 153 explicit PercentWaiter(DownloadItem* item) |
| 154 : item_(item), | 154 : item_(item), |
| 155 waiting_(false), | 155 waiting_(false), |
| 156 error_(false), | 156 error_(false), |
| 157 prev_percent_(0) { | 157 prev_percent_(0) { |
| 158 item_->AddObserver(this); | 158 item_->AddObserver(this); |
| 159 } | 159 } |
| 160 virtual ~PercentWaiter() { | 160 ~PercentWaiter() override { |
| 161 if (item_) | 161 if (item_) |
| 162 item_->RemoveObserver(this); | 162 item_->RemoveObserver(this); |
| 163 } | 163 } |
| 164 | 164 |
| 165 bool WaitForFinished() { | 165 bool WaitForFinished() { |
| 166 if (item_->GetState() == DownloadItem::COMPLETE) { | 166 if (item_->GetState() == DownloadItem::COMPLETE) { |
| 167 return item_->PercentComplete() == 100; | 167 return item_->PercentComplete() == 100; |
| 168 } | 168 } |
| 169 waiting_ = true; | 169 waiting_ = true; |
| 170 content::RunMessageLoop(); | 170 content::RunMessageLoop(); |
| 171 waiting_ = false; | 171 waiting_ = false; |
| 172 return !error_; | 172 return !error_; |
| 173 } | 173 } |
| 174 | 174 |
| 175 private: | 175 private: |
| 176 virtual void OnDownloadUpdated(content::DownloadItem* item) override { | 176 void OnDownloadUpdated(content::DownloadItem* item) override { |
| 177 DCHECK_EQ(item_, item); | 177 DCHECK_EQ(item_, item); |
| 178 if (!error_ && | 178 if (!error_ && |
| 179 ((prev_percent_ > item_->PercentComplete()) || | 179 ((prev_percent_ > item_->PercentComplete()) || |
| 180 (item_->GetState() == DownloadItem::COMPLETE && | 180 (item_->GetState() == DownloadItem::COMPLETE && |
| 181 (item_->PercentComplete() != 100)))) { | 181 (item_->PercentComplete() != 100)))) { |
| 182 error_ = true; | 182 error_ = true; |
| 183 if (waiting_) | 183 if (waiting_) |
| 184 base::MessageLoopForUI::current()->Quit(); | 184 base::MessageLoopForUI::current()->Quit(); |
| 185 } | 185 } |
| 186 if (item_->GetState() == DownloadItem::COMPLETE && waiting_) | 186 if (item_->GetState() == DownloadItem::COMPLETE && waiting_) |
| 187 base::MessageLoopForUI::current()->Quit(); | 187 base::MessageLoopForUI::current()->Quit(); |
| 188 } | 188 } |
| 189 | 189 |
| 190 virtual void OnDownloadDestroyed(content::DownloadItem* item) override { | 190 void OnDownloadDestroyed(content::DownloadItem* item) override { |
| 191 DCHECK_EQ(item_, item); | 191 DCHECK_EQ(item_, item); |
| 192 item_->RemoveObserver(this); | 192 item_->RemoveObserver(this); |
| 193 item_ = NULL; | 193 item_ = NULL; |
| 194 } | 194 } |
| 195 | 195 |
| 196 content::DownloadItem* item_; | 196 content::DownloadItem* item_; |
| 197 bool waiting_; | 197 bool waiting_; |
| 198 bool error_; | 198 bool error_; |
| 199 int prev_percent_; | 199 int prev_percent_; |
| 200 | 200 |
| 201 DISALLOW_COPY_AND_ASSIGN(PercentWaiter); | 201 DISALLOW_COPY_AND_ASSIGN(PercentWaiter); |
| 202 }; | 202 }; |
| 203 | 203 |
| 204 // DownloadTestObserver subclass that observes one download until it transitions | 204 // DownloadTestObserver subclass that observes one download until it transitions |
| 205 // from a non-resumable state to a resumable state a specified number of | 205 // from a non-resumable state to a resumable state a specified number of |
| 206 // times. Note that this observer can only observe a single download. | 206 // times. Note that this observer can only observe a single download. |
| 207 class DownloadTestObserverResumable : public content::DownloadTestObserver { | 207 class DownloadTestObserverResumable : public content::DownloadTestObserver { |
| 208 public: | 208 public: |
| 209 // Construct a new observer. |transition_count| is the number of times the | 209 // Construct a new observer. |transition_count| is the number of times the |
| 210 // download should transition from a non-resumable state to a resumable state. | 210 // download should transition from a non-resumable state to a resumable state. |
| 211 DownloadTestObserverResumable(DownloadManager* download_manager, | 211 DownloadTestObserverResumable(DownloadManager* download_manager, |
| 212 size_t transition_count) | 212 size_t transition_count) |
| 213 : DownloadTestObserver(download_manager, 1, | 213 : DownloadTestObserver(download_manager, 1, |
| 214 ON_DANGEROUS_DOWNLOAD_FAIL), | 214 ON_DANGEROUS_DOWNLOAD_FAIL), |
| 215 was_previously_resumable_(false), | 215 was_previously_resumable_(false), |
| 216 transitions_left_(transition_count) { | 216 transitions_left_(transition_count) { |
| 217 Init(); | 217 Init(); |
| 218 } | 218 } |
| 219 virtual ~DownloadTestObserverResumable() {} | 219 ~DownloadTestObserverResumable() override {} |
| 220 | 220 |
| 221 private: | 221 private: |
| 222 virtual bool IsDownloadInFinalState(DownloadItem* download) override { | 222 bool IsDownloadInFinalState(DownloadItem* download) override { |
| 223 bool is_resumable_now = download->CanResume(); | 223 bool is_resumable_now = download->CanResume(); |
| 224 if (!was_previously_resumable_ && is_resumable_now) | 224 if (!was_previously_resumable_ && is_resumable_now) |
| 225 --transitions_left_; | 225 --transitions_left_; |
| 226 was_previously_resumable_ = is_resumable_now; | 226 was_previously_resumable_ = is_resumable_now; |
| 227 return transitions_left_ == 0; | 227 return transitions_left_ == 0; |
| 228 } | 228 } |
| 229 | 229 |
| 230 bool was_previously_resumable_; | 230 bool was_previously_resumable_; |
| 231 size_t transitions_left_; | 231 size_t transitions_left_; |
| 232 | 232 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 // Mock that simulates a permissions dialog where the user denies | 281 // Mock that simulates a permissions dialog where the user denies |
| 282 // permission to install. TODO(skerner): This could be shared with | 282 // permission to install. TODO(skerner): This could be shared with |
| 283 // extensions tests. Find a common place for this class. | 283 // extensions tests. Find a common place for this class. |
| 284 class MockAbortExtensionInstallPrompt : public ExtensionInstallPrompt { | 284 class MockAbortExtensionInstallPrompt : public ExtensionInstallPrompt { |
| 285 public: | 285 public: |
| 286 MockAbortExtensionInstallPrompt() : | 286 MockAbortExtensionInstallPrompt() : |
| 287 ExtensionInstallPrompt(NULL) { | 287 ExtensionInstallPrompt(NULL) { |
| 288 } | 288 } |
| 289 | 289 |
| 290 // Simulate a user abort on an extension installation. | 290 // Simulate a user abort on an extension installation. |
| 291 virtual void ConfirmInstall( | 291 void ConfirmInstall(Delegate* delegate, |
| 292 Delegate* delegate, | 292 const Extension* extension, |
| 293 const Extension* extension, | 293 const ShowDialogCallback& show_dialog_callback) override { |
| 294 const ShowDialogCallback& show_dialog_callback) override { | |
| 295 delegate->InstallUIAbort(true); | 294 delegate->InstallUIAbort(true); |
| 296 base::MessageLoopForUI::current()->Quit(); | 295 base::MessageLoopForUI::current()->Quit(); |
| 297 } | 296 } |
| 298 | 297 |
| 299 virtual void OnInstallSuccess(const Extension* extension, | 298 void OnInstallSuccess(const Extension* extension, SkBitmap* icon) override {} |
| 300 SkBitmap* icon) override { | 299 void OnInstallFailure(const extensions::CrxInstallerError& error) override {} |
| 301 } | |
| 302 virtual void OnInstallFailure( | |
| 303 const extensions::CrxInstallerError& error) override { | |
| 304 } | |
| 305 }; | 300 }; |
| 306 | 301 |
| 307 // Mock that simulates a permissions dialog where the user allows | 302 // Mock that simulates a permissions dialog where the user allows |
| 308 // installation. | 303 // installation. |
| 309 class MockAutoConfirmExtensionInstallPrompt : public ExtensionInstallPrompt { | 304 class MockAutoConfirmExtensionInstallPrompt : public ExtensionInstallPrompt { |
| 310 public: | 305 public: |
| 311 explicit MockAutoConfirmExtensionInstallPrompt( | 306 explicit MockAutoConfirmExtensionInstallPrompt( |
| 312 content::WebContents* web_contents) | 307 content::WebContents* web_contents) |
| 313 : ExtensionInstallPrompt(web_contents) {} | 308 : ExtensionInstallPrompt(web_contents) {} |
| 314 | 309 |
| 315 // Proceed without confirmation prompt. | 310 // Proceed without confirmation prompt. |
| 316 virtual void ConfirmInstall( | 311 void ConfirmInstall(Delegate* delegate, |
| 317 Delegate* delegate, | 312 const Extension* extension, |
| 318 const Extension* extension, | 313 const ShowDialogCallback& show_dialog_callback) override { |
| 319 const ShowDialogCallback& show_dialog_callback) override { | |
| 320 delegate->InstallUIProceed(); | 314 delegate->InstallUIProceed(); |
| 321 } | 315 } |
| 322 | 316 |
| 323 virtual void OnInstallSuccess(const Extension* extension, | 317 void OnInstallSuccess(const Extension* extension, SkBitmap* icon) override {} |
| 324 SkBitmap* icon) override { | 318 void OnInstallFailure(const extensions::CrxInstallerError& error) override {} |
| 325 } | |
| 326 virtual void OnInstallFailure( | |
| 327 const extensions::CrxInstallerError& error) override { | |
| 328 } | |
| 329 }; | 319 }; |
| 330 | 320 |
| 331 static DownloadManager* DownloadManagerForBrowser(Browser* browser) { | 321 static DownloadManager* DownloadManagerForBrowser(Browser* browser) { |
| 332 return BrowserContext::GetDownloadManager(browser->profile()); | 322 return BrowserContext::GetDownloadManager(browser->profile()); |
| 333 } | 323 } |
| 334 | 324 |
| 335 bool WasAutoOpened(DownloadItem* item) { | 325 bool WasAutoOpened(DownloadItem* item) { |
| 336 return item->GetAutoOpened(); | 326 return item->GetAutoOpened(); |
| 337 } | 327 } |
| 338 | 328 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 typedef base::Callback<bool(const history::DownloadRow&)> FilterCallback; | 364 typedef base::Callback<bool(const history::DownloadRow&)> FilterCallback; |
| 375 | 365 |
| 376 explicit HistoryObserver(Profile* profile) | 366 explicit HistoryObserver(Profile* profile) |
| 377 : profile_(profile), | 367 : profile_(profile), |
| 378 waiting_(false), | 368 waiting_(false), |
| 379 seen_stored_(false) { | 369 seen_stored_(false) { |
| 380 DownloadServiceFactory::GetForBrowserContext(profile_)-> | 370 DownloadServiceFactory::GetForBrowserContext(profile_)-> |
| 381 GetDownloadHistory()->AddObserver(this); | 371 GetDownloadHistory()->AddObserver(this); |
| 382 } | 372 } |
| 383 | 373 |
| 384 virtual ~HistoryObserver() { | 374 ~HistoryObserver() override { |
| 385 DownloadService* service = DownloadServiceFactory::GetForBrowserContext( | 375 DownloadService* service = DownloadServiceFactory::GetForBrowserContext( |
| 386 profile_); | 376 profile_); |
| 387 if (service && service->GetDownloadHistory()) | 377 if (service && service->GetDownloadHistory()) |
| 388 service->GetDownloadHistory()->RemoveObserver(this); | 378 service->GetDownloadHistory()->RemoveObserver(this); |
| 389 } | 379 } |
| 390 | 380 |
| 391 void SetFilterCallback(const FilterCallback& callback) { | 381 void SetFilterCallback(const FilterCallback& callback) { |
| 392 callback_ = callback; | 382 callback_ = callback; |
| 393 } | 383 } |
| 394 | 384 |
| 395 virtual void OnDownloadStored( | 385 void OnDownloadStored(content::DownloadItem* item, |
| 396 content::DownloadItem* item, | 386 const history::DownloadRow& info) override { |
| 397 const history::DownloadRow& info) override { | |
| 398 if (!callback_.is_null() && (!callback_.Run(info))) | 387 if (!callback_.is_null() && (!callback_.Run(info))) |
| 399 return; | 388 return; |
| 400 | 389 |
| 401 seen_stored_ = true; | 390 seen_stored_ = true; |
| 402 if (waiting_) | 391 if (waiting_) |
| 403 base::MessageLoopForUI::current()->Quit(); | 392 base::MessageLoopForUI::current()->Quit(); |
| 404 } | 393 } |
| 405 | 394 |
| 406 virtual void OnDownloadHistoryDestroyed() override { | 395 void OnDownloadHistoryDestroyed() override { |
| 407 DownloadServiceFactory::GetForBrowserContext(profile_)-> | 396 DownloadServiceFactory::GetForBrowserContext(profile_)-> |
| 408 GetDownloadHistory()->RemoveObserver(this); | 397 GetDownloadHistory()->RemoveObserver(this); |
| 409 } | 398 } |
| 410 | 399 |
| 411 void WaitForStored() { | 400 void WaitForStored() { |
| 412 if (seen_stored_) | 401 if (seen_stored_) |
| 413 return; | 402 return; |
| 414 waiting_ = true; | 403 waiting_ = true; |
| 415 content::RunMessageLoop(); | 404 content::RunMessageLoop(); |
| 416 waiting_ = false; | 405 waiting_ = false; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 443 bool should_redirect_to_documents; // True if we save it in "My Documents". | 432 bool should_redirect_to_documents; // True if we save it in "My Documents". |
| 444 }; | 433 }; |
| 445 | 434 |
| 446 struct FileErrorInjectInfo { | 435 struct FileErrorInjectInfo { |
| 447 DownloadInfo download_info; | 436 DownloadInfo download_info; |
| 448 content::TestFileErrorInjector::FileErrorInfo error_info; | 437 content::TestFileErrorInjector::FileErrorInfo error_info; |
| 449 }; | 438 }; |
| 450 | 439 |
| 451 DownloadTest() {} | 440 DownloadTest() {} |
| 452 | 441 |
| 453 virtual void SetUpOnMainThread() override { | 442 void SetUpOnMainThread() override { |
| 454 BrowserThread::PostTask( | 443 BrowserThread::PostTask( |
| 455 BrowserThread::IO, FROM_HERE, | 444 BrowserThread::IO, FROM_HERE, |
| 456 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); | 445 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
| 457 ASSERT_TRUE(InitialSetup()); | 446 ASSERT_TRUE(InitialSetup()); |
| 458 } | 447 } |
| 459 | 448 |
| 460 virtual void TearDownOnMainThread() override { | 449 void TearDownOnMainThread() override { |
| 461 // Needs to be torn down on the main thread. file_activity_observer_ holds a | 450 // Needs to be torn down on the main thread. file_activity_observer_ holds a |
| 462 // reference to the ChromeDownloadManagerDelegate which should be destroyed | 451 // reference to the ChromeDownloadManagerDelegate which should be destroyed |
| 463 // on the UI thread. | 452 // on the UI thread. |
| 464 file_activity_observer_.reset(); | 453 file_activity_observer_.reset(); |
| 465 } | 454 } |
| 466 | 455 |
| 467 virtual void SetUpCommandLine(CommandLine* command_line) override { | 456 void SetUpCommandLine(CommandLine* command_line) override { |
| 468 command_line->AppendSwitch(switches::kDisablePluginsDiscovery); | 457 command_line->AppendSwitch(switches::kDisablePluginsDiscovery); |
| 469 } | 458 } |
| 470 | 459 |
| 471 // Returning false indicates a failure of the setup, and should be asserted | 460 // Returning false indicates a failure of the setup, and should be asserted |
| 472 // in the caller. | 461 // in the caller. |
| 473 virtual bool InitialSetup() { | 462 virtual bool InitialSetup() { |
| 474 bool have_test_dir = PathService::Get(chrome::DIR_TEST_DATA, &test_dir_); | 463 bool have_test_dir = PathService::Get(chrome::DIR_TEST_DATA, &test_dir_); |
| 475 EXPECT_TRUE(have_test_dir); | 464 EXPECT_TRUE(have_test_dir); |
| 476 if (!have_test_dir) | 465 if (!have_test_dir) |
| 477 return false; | 466 return false; |
| (...skipping 3050 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3528 *(downloads[0]))); | 3517 *(downloads[0]))); |
| 3529 | 3518 |
| 3530 // Begin feedback and check that the file is "stolen". | 3519 // Begin feedback and check that the file is "stolen". |
| 3531 download_protection_service->feedback_service()->BeginFeedbackForDownload( | 3520 download_protection_service->feedback_service()->BeginFeedbackForDownload( |
| 3532 downloads[0]); | 3521 downloads[0]); |
| 3533 std::vector<DownloadItem*> updated_downloads; | 3522 std::vector<DownloadItem*> updated_downloads; |
| 3534 GetDownloads(browser(), &updated_downloads); | 3523 GetDownloads(browser(), &updated_downloads); |
| 3535 ASSERT_TRUE(updated_downloads.empty()); | 3524 ASSERT_TRUE(updated_downloads.empty()); |
| 3536 } | 3525 } |
| 3537 #endif | 3526 #endif |
| OLD | NEW |