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 mark_downloads_dangerous_(false), | |
29 global_danger_type_(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS), | |
28 weak_ptr_factory_(this) { | 30 weak_ptr_factory_(this) { |
29 if (!profile->IsOffTheRecord()) | 31 if (!profile->IsOffTheRecord()) |
30 GetDownloadIdReceiverCallback().Run( | 32 GetDownloadIdReceiverCallback().Run( |
31 content::DownloadItem::kInvalidId + 1); | 33 content::DownloadItem::kInvalidId + 1); |
32 } | 34 } |
33 | 35 |
34 virtual ~MockDownloadManagerDelegate() {} | 36 virtual ~MockDownloadManagerDelegate() {} |
35 | 37 |
36 void EnableFileChooser(bool enable) { | 38 void EnableFileChooser(bool enable) { |
37 file_chooser_enabled_ = enable; | 39 file_chooser_enabled_ = enable; |
38 } | 40 } |
39 | 41 |
40 bool TestAndResetDidShowFileChooser() { | 42 bool TestAndResetDidShowFileChooser() { |
41 bool did_show = file_chooser_displayed_; | 43 bool did_show = file_chooser_displayed_; |
42 file_chooser_displayed_ = false; | 44 file_chooser_displayed_ = false; |
43 return did_show; | 45 return did_show; |
44 } | 46 } |
45 | 47 |
46 base::WeakPtr<MockDownloadManagerDelegate> GetWeakPtr() { | 48 base::WeakPtr<MockDownloadManagerDelegate> GetWeakPtr() { |
47 return weak_ptr_factory_.GetWeakPtr(); | 49 return weak_ptr_factory_.GetWeakPtr(); |
48 } | 50 } |
49 | 51 |
52 void SetDangerTypeForNewDownloads(bool mark_downloads_dangerous, | |
53 content::DownloadDangerType danger_type) { | |
54 mark_downloads_dangerous_ = mark_downloads_dangerous; | |
55 global_danger_type_ = danger_type; | |
56 } | |
57 | |
58 virtual bool DetermineDownloadTarget( | |
59 content::DownloadItem* item, | |
60 const content::DownloadTargetCallback& callback) OVERRIDE { | |
61 if (mark_downloads_dangerous_ && | |
62 item->GetDangerType() != content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) { | |
asanka
2014/06/05 19:20:12
Nit: Might be simpler to just route the callback t
| |
63 content::DownloadTargetCallback dangerous_callback = | |
64 base::Bind(&MockDownloadManagerDelegate::SetDangerous, | |
65 callback); | |
66 return ChromeDownloadManagerDelegate::DetermineDownloadTarget( | |
67 item, dangerous_callback); | |
68 } else { | |
69 return ChromeDownloadManagerDelegate::DetermineDownloadTarget( | |
70 item, callback); | |
71 } | |
72 } | |
73 | |
74 static void SetDangerous( | |
75 const content::DownloadTargetCallback& callback, | |
76 const base::FilePath& target_path, | |
77 content::DownloadItem::TargetDisposition disp, | |
78 content::DownloadDangerType danger_type, | |
79 const base::FilePath& intermediate_path) { | |
80 callback.Run(target_path, | |
81 disp, | |
82 content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT, | |
83 intermediate_path); | |
84 } | |
85 | |
50 protected: | 86 protected: |
51 | 87 |
52 virtual void PromptUserForDownloadPath(content::DownloadItem* item, | 88 virtual void PromptUserForDownloadPath(content::DownloadItem* item, |
53 const base::FilePath& suggested_path, | 89 const base::FilePath& suggested_path, |
54 const FileSelectedCallback& | 90 const FileSelectedCallback& |
55 callback) OVERRIDE { | 91 callback) OVERRIDE { |
56 file_chooser_displayed_ = true; | 92 file_chooser_displayed_ = true; |
57 base::MessageLoop::current()->PostTask( | 93 base::MessageLoop::current()->PostTask( |
58 FROM_HERE, base::Bind(callback, (file_chooser_enabled_ ? suggested_path | 94 FROM_HERE, base::Bind(callback, (file_chooser_enabled_ ? suggested_path |
59 : base::FilePath()))); | 95 : base::FilePath()))); |
60 } | 96 } |
61 | 97 |
62 virtual void OpenDownload(content::DownloadItem* item) OVERRIDE {} | 98 virtual void OpenDownload(content::DownloadItem* item) OVERRIDE {} |
63 | 99 |
64 private: | 100 private: |
65 bool file_chooser_enabled_; | 101 bool file_chooser_enabled_; |
66 bool file_chooser_displayed_; | 102 bool file_chooser_displayed_; |
103 bool mark_downloads_dangerous_; // Whether the danger type should be changed. | |
104 content::DownloadDangerType global_danger_type_; | |
asanka
2014/06/05 19:20:12
Nit: maybe call it "forced_danger_type_" ? Just a
| |
67 base::WeakPtrFactory<MockDownloadManagerDelegate> weak_ptr_factory_; | 105 base::WeakPtrFactory<MockDownloadManagerDelegate> weak_ptr_factory_; |
68 }; | 106 }; |
69 | 107 |
70 DownloadTestFileActivityObserver::DownloadTestFileActivityObserver( | 108 DownloadTestFileActivityObserver::DownloadTestFileActivityObserver( |
71 Profile* profile) { | 109 Profile* profile) { |
72 scoped_ptr<MockDownloadManagerDelegate> mock_delegate( | 110 scoped_ptr<MockDownloadManagerDelegate> mock_delegate( |
73 new MockDownloadManagerDelegate(profile)); | 111 new MockDownloadManagerDelegate(profile)); |
74 test_delegate_ = mock_delegate->GetWeakPtr(); | 112 test_delegate_ = mock_delegate->GetWeakPtr(); |
75 DownloadServiceFactory::GetForBrowserContext(profile)-> | 113 DownloadServiceFactory::GetForBrowserContext(profile)-> |
76 SetDownloadManagerDelegateForTesting( | 114 SetDownloadManagerDelegateForTesting( |
77 mock_delegate.PassAs<ChromeDownloadManagerDelegate>()); | 115 mock_delegate.PassAs<ChromeDownloadManagerDelegate>()); |
78 } | 116 } |
79 | 117 |
80 DownloadTestFileActivityObserver::~DownloadTestFileActivityObserver() { | 118 DownloadTestFileActivityObserver::~DownloadTestFileActivityObserver() { |
81 } | 119 } |
82 | 120 |
83 void DownloadTestFileActivityObserver::EnableFileChooser(bool enable) { | 121 void DownloadTestFileActivityObserver::EnableFileChooser(bool enable) { |
84 if (test_delegate_.get()) | 122 if (test_delegate_.get()) |
85 test_delegate_->EnableFileChooser(enable); | 123 test_delegate_->EnableFileChooser(enable); |
86 } | 124 } |
87 | 125 |
88 bool DownloadTestFileActivityObserver::TestAndResetDidShowFileChooser() { | 126 bool DownloadTestFileActivityObserver::TestAndResetDidShowFileChooser() { |
89 return test_delegate_.get() && | 127 return test_delegate_.get() && |
90 test_delegate_->TestAndResetDidShowFileChooser(); | 128 test_delegate_->TestAndResetDidShowFileChooser(); |
91 } | 129 } |
130 | |
131 void DownloadTestFileActivityObserver::SetDangerTypeForNewDownloads( | |
132 bool mark_dangerous, | |
133 content::DownloadDangerType danger_type) { | |
134 if (test_delegate_.get()) | |
135 test_delegate_->SetDangerTypeForNewDownloads(mark_dangerous, danger_type); | |
136 } | |
OLD | NEW |