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 "chrome/browser/download/download_test_file_activity_observer.h" | 5 #include "chrome/browser/download/download_test_file_activity_observer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 9 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 10 #include "chrome/browser/download/download_service.h" | 10 #include "chrome/browser/download/download_service.h" |
| 11 #include "chrome/browser/download/download_service_factory.h" | 11 #include "chrome/browser/download/download_service_factory.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 | 13 |
| 14 namespace content { | 14 namespace content { |
| 15 class DownloadItem; | 15 class DownloadItem; |
| 16 } | 16 } |
| 17 | 17 |
| 18 // Test ChromeDownloadManagerDelegate that controls whether how file chooser | 18 // Test ChromeDownloadManagerDelegate that controls whether how file chooser |
| 19 // dialogs are handled, and how files are opend. | 19 // dialogs are handled, and how files are opend. |
| 20 // By default, file chooser dialogs are disabled. | 20 // By default, file chooser dialogs are disabled. |
| 21 class DownloadTestFileActivityObserver::MockDownloadManagerDelegate | 21 class DownloadTestFileActivityObserver::MockDownloadManagerDelegate |
| 22 : public ChromeDownloadManagerDelegate { | 22 : public ChromeDownloadManagerDelegate { |
| 23 public: | 23 public: |
| 24 explicit MockDownloadManagerDelegate(Profile* profile) | 24 explicit MockDownloadManagerDelegate(Profile* profile) |
| 25 : ChromeDownloadManagerDelegate(profile), | 25 : ChromeDownloadManagerDelegate(profile), |
| 26 file_chooser_enabled_(false), | 26 file_chooser_enabled_(false), |
| 27 file_chooser_displayed_(false), | 27 file_chooser_displayed_(false), |
| 28 downloads_dangerous_(false), | |
| 28 weak_ptr_factory_(this) { | 29 weak_ptr_factory_(this) { |
| 29 if (!profile->IsOffTheRecord()) | 30 if (!profile->IsOffTheRecord()) |
| 30 GetDownloadIdReceiverCallback().Run( | 31 GetDownloadIdReceiverCallback().Run( |
| 31 content::DownloadItem::kInvalidId + 1); | 32 content::DownloadItem::kInvalidId + 1); |
| 32 } | 33 } |
| 33 | 34 |
| 34 virtual ~MockDownloadManagerDelegate() {} | 35 virtual ~MockDownloadManagerDelegate() {} |
| 35 | 36 |
| 36 void EnableFileChooser(bool enable) { | 37 void EnableFileChooser(bool enable) { |
| 37 file_chooser_enabled_ = enable; | 38 file_chooser_enabled_ = enable; |
| 38 } | 39 } |
| 39 | 40 |
| 40 bool TestAndResetDidShowFileChooser() { | 41 bool TestAndResetDidShowFileChooser() { |
| 41 bool did_show = file_chooser_displayed_; | 42 bool did_show = file_chooser_displayed_; |
| 42 file_chooser_displayed_ = false; | 43 file_chooser_displayed_ = false; |
| 43 return did_show; | 44 return did_show; |
| 44 } | 45 } |
| 45 | 46 |
| 46 base::WeakPtr<MockDownloadManagerDelegate> GetWeakPtr() { | 47 base::WeakPtr<MockDownloadManagerDelegate> GetWeakPtr() { |
| 47 return weak_ptr_factory_.GetWeakPtr(); | 48 return weak_ptr_factory_.GetWeakPtr(); |
| 48 } | 49 } |
| 49 | 50 |
| 51 void ChangeDangerTypeForFeedbackTest() { | |
|
asanka
2014/03/26 18:01:30
Suggestion: Use a more generic name since this is
felt
2014/06/03 00:35:22
My problem here is that I don't know how to get th
asanka
2014/06/05 19:20:12
static void SetDangerous(const DownloadTargetCallb
| |
| 52 downloads_dangerous_ = true; | |
|
asanka
2014/03/26 18:01:30
Use a more descriptive name? mark_downloads_as_dan
felt
2014/06/03 00:35:22
Done.
| |
| 53 } | |
| 54 | |
| 55 virtual bool DetermineDownloadTarget( | |
| 56 content::DownloadItem* item, | |
| 57 const content::DownloadTargetCallback& callback) OVERRIDE { | |
| 58 if (downloads_dangerous_ && | |
| 59 item->GetDangerType() == content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE) { | |
|
asanka
2014/03/26 18:01:30
If you are forcing a danger type, I'd propose just
felt
2014/06/03 00:35:22
I believe this should be fixed now.
| |
| 60 content::DownloadTargetCallback dangerous_callback = | |
| 61 base::Bind(&MockDownloadManagerDelegate::SetDangerous, | |
| 62 callback); | |
| 63 return ChromeDownloadManagerDelegate::DetermineDownloadTarget( | |
| 64 item, dangerous_callback); | |
| 65 } else { | |
| 66 return ChromeDownloadManagerDelegate::DetermineDownloadTarget( | |
| 67 item, callback); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 static void SetDangerous( | |
| 72 const content::DownloadTargetCallback& callback, | |
| 73 const base::FilePath& target_path, | |
| 74 content::DownloadItem::TargetDisposition disp, | |
| 75 content::DownloadDangerType danger_type, | |
| 76 const base::FilePath& intermediate_path) { | |
| 77 callback.Run(target_path, | |
| 78 disp, | |
| 79 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT, | |
| 80 intermediate_path); | |
| 81 } | |
| 82 | |
| 50 protected: | 83 protected: |
| 51 | 84 |
| 52 virtual void PromptUserForDownloadPath(content::DownloadItem* item, | 85 virtual void PromptUserForDownloadPath(content::DownloadItem* item, |
| 53 const base::FilePath& suggested_path, | 86 const base::FilePath& suggested_path, |
| 54 const FileSelectedCallback& | 87 const FileSelectedCallback& |
| 55 callback) OVERRIDE { | 88 callback) OVERRIDE { |
| 56 file_chooser_displayed_ = true; | 89 file_chooser_displayed_ = true; |
| 57 base::MessageLoop::current()->PostTask( | 90 base::MessageLoop::current()->PostTask( |
| 58 FROM_HERE, base::Bind(callback, (file_chooser_enabled_ ? suggested_path | 91 FROM_HERE, base::Bind(callback, (file_chooser_enabled_ ? suggested_path |
| 59 : base::FilePath()))); | 92 : base::FilePath()))); |
| 60 } | 93 } |
| 61 | 94 |
| 62 virtual void OpenDownload(content::DownloadItem* item) OVERRIDE {} | 95 virtual void OpenDownload(content::DownloadItem* item) OVERRIDE {} |
| 63 | 96 |
| 64 private: | 97 private: |
| 65 bool file_chooser_enabled_; | 98 bool file_chooser_enabled_; |
| 66 bool file_chooser_displayed_; | 99 bool file_chooser_displayed_; |
| 100 bool downloads_dangerous_; | |
| 67 base::WeakPtrFactory<MockDownloadManagerDelegate> weak_ptr_factory_; | 101 base::WeakPtrFactory<MockDownloadManagerDelegate> weak_ptr_factory_; |
| 68 }; | 102 }; |
| 69 | 103 |
| 70 DownloadTestFileActivityObserver::DownloadTestFileActivityObserver( | 104 DownloadTestFileActivityObserver::DownloadTestFileActivityObserver( |
| 71 Profile* profile) { | 105 Profile* profile) { |
| 72 scoped_ptr<MockDownloadManagerDelegate> mock_delegate( | 106 scoped_ptr<MockDownloadManagerDelegate> mock_delegate( |
| 73 new MockDownloadManagerDelegate(profile)); | 107 new MockDownloadManagerDelegate(profile)); |
| 74 test_delegate_ = mock_delegate->GetWeakPtr(); | 108 test_delegate_ = mock_delegate->GetWeakPtr(); |
| 75 DownloadServiceFactory::GetForBrowserContext(profile)-> | 109 DownloadServiceFactory::GetForBrowserContext(profile)-> |
| 76 SetDownloadManagerDelegateForTesting( | 110 SetDownloadManagerDelegateForTesting( |
| 77 mock_delegate.PassAs<ChromeDownloadManagerDelegate>()); | 111 mock_delegate.PassAs<ChromeDownloadManagerDelegate>()); |
| 78 } | 112 } |
| 79 | 113 |
| 80 DownloadTestFileActivityObserver::~DownloadTestFileActivityObserver() { | 114 DownloadTestFileActivityObserver::~DownloadTestFileActivityObserver() { |
| 81 } | 115 } |
| 82 | 116 |
| 83 void DownloadTestFileActivityObserver::EnableFileChooser(bool enable) { | 117 void DownloadTestFileActivityObserver::EnableFileChooser(bool enable) { |
| 84 if (test_delegate_.get()) | 118 if (test_delegate_.get()) |
| 85 test_delegate_->EnableFileChooser(enable); | 119 test_delegate_->EnableFileChooser(enable); |
| 86 } | 120 } |
| 87 | 121 |
| 88 bool DownloadTestFileActivityObserver::TestAndResetDidShowFileChooser() { | 122 bool DownloadTestFileActivityObserver::TestAndResetDidShowFileChooser() { |
| 89 return test_delegate_.get() && | 123 return test_delegate_.get() && |
| 90 test_delegate_->TestAndResetDidShowFileChooser(); | 124 test_delegate_->TestAndResetDidShowFileChooser(); |
| 91 } | 125 } |
| 126 | |
| 127 void DownloadTestFileActivityObserver::ChangeDangerTypeForFeedbackTest() { | |
| 128 if (test_delegate_.get()) | |
| 129 test_delegate_->ChangeDangerTypeForFeedbackTest(); | |
| 130 } | |
| OLD | NEW |