OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/browser_shutdown.h" | 9 #include "chrome/browser/browser_shutdown.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
11 #include "chrome/browser/download/chrome_download_manager_delegate.h" | |
12 #include "chrome/browser/download/download_service.h" | |
13 #include "chrome/browser/download/download_service_factory.h" | |
11 #include "chrome/browser/lifetime/application_lifetime.h" | 14 #include "chrome/browser/lifetime/application_lifetime.h" |
12 #include "chrome/browser/lifetime/browser_close_manager.h" | 15 #include "chrome/browser/lifetime/browser_close_manager.h" |
13 #include "chrome/browser/net/url_request_mock_util.h" | 16 #include "chrome/browser/net/url_request_mock_util.h" |
14 #include "chrome/browser/prefs/session_startup_pref.h" | 17 #include "chrome/browser/prefs/session_startup_pref.h" |
15 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/profiles/profile_manager.h" | 19 #include "chrome/browser/profiles/profile_manager.h" |
17 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" | 20 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" |
18 #include "chrome/browser/ui/app_modal_dialogs/native_app_modal_dialog.h" | 21 #include "chrome/browser/ui/app_modal_dialogs/native_app_modal_dialog.h" |
19 #include "chrome/browser/ui/browser.h" | 22 #include "chrome/browser/ui/browser.h" |
20 #include "chrome/browser/ui/browser_iterator.h" | 23 #include "chrome/browser/ui/browser_iterator.h" |
21 #include "chrome/browser/ui/browser_window.h" | 24 #include "chrome/browser/ui/browser_window.h" |
22 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 25 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
23 #include "chrome/common/chrome_switches.h" | 26 #include "chrome/common/chrome_switches.h" |
24 #include "chrome/common/url_constants.h" | 27 #include "chrome/common/url_constants.h" |
25 #include "chrome/test/base/in_process_browser_test.h" | 28 #include "chrome/test/base/in_process_browser_test.h" |
26 #include "chrome/test/base/ui_test_utils.h" | 29 #include "chrome/test/base/ui_test_utils.h" |
27 #include "content/public/browser/browser_context.h" | 30 #include "content/public/browser/browser_context.h" |
28 #include "content/public/browser/download_item.h" | 31 #include "content/public/browser/download_item.h" |
29 #include "content/public/browser/download_manager.h" | 32 #include "content/public/browser/download_manager.h" |
30 #include "content/public/browser/notification_service.h" | 33 #include "content/public/browser/notification_service.h" |
31 #include "content/public/browser/web_contents.h" | 34 #include "content/public/browser/web_contents.h" |
32 #include "content/public/test/download_test_observer.h" | 35 #include "content/public/test/download_test_observer.h" |
33 #include "content/public/test/test_navigation_observer.h" | 36 #include "content/public/test/test_navigation_observer.h" |
37 #include "content/test/net/url_request_mock_http_job.h" | |
34 #include "content/test/net/url_request_slow_download_job.h" | 38 #include "content/test/net/url_request_slow_download_job.h" |
35 #include "net/test/embedded_test_server/embedded_test_server.h" | 39 #include "net/test/embedded_test_server/embedded_test_server.h" |
36 | 40 |
37 namespace { | 41 namespace { |
38 | 42 |
39 class AppModalDialogObserver { | 43 class AppModalDialogObserver { |
40 public: | 44 public: |
41 AppModalDialogObserver() {} | 45 AppModalDialogObserver() {} |
42 | 46 |
43 void Start() { | 47 void Start() { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 base::RunLoop run_loop_; | 118 base::RunLoop run_loop_; |
115 | 119 |
116 DISALLOW_COPY_AND_ASSIGN(RepeatedNotificationObserver); | 120 DISALLOW_COPY_AND_ASSIGN(RepeatedNotificationObserver); |
117 }; | 121 }; |
118 | 122 |
119 class TestBrowserCloseManager : public BrowserCloseManager { | 123 class TestBrowserCloseManager : public BrowserCloseManager { |
120 public: | 124 public: |
121 enum UserChoice { | 125 enum UserChoice { |
122 USER_CHOICE_USER_CANCELS_CLOSE, | 126 USER_CHOICE_USER_CANCELS_CLOSE, |
123 USER_CHOICE_USER_ALLOWS_CLOSE, | 127 USER_CHOICE_USER_ALLOWS_CLOSE, |
128 NO_USER_CHOICE | |
124 }; | 129 }; |
125 | 130 |
126 static void AttemptClose(UserChoice user_choice) { | 131 static void AttemptClose(UserChoice user_choice) { |
127 scoped_refptr<BrowserCloseManager> browser_close_manager = | 132 scoped_refptr<BrowserCloseManager> browser_close_manager = |
128 new TestBrowserCloseManager(user_choice); | 133 new TestBrowserCloseManager(user_choice); |
129 browser_close_manager->StartClosingBrowsers(); | 134 browser_close_manager->StartClosingBrowsers(); |
130 } | 135 } |
131 | 136 |
132 protected: | 137 protected: |
133 virtual ~TestBrowserCloseManager() {} | 138 virtual ~TestBrowserCloseManager() {} |
134 | 139 |
135 virtual void ConfirmCloseWithPendingDownloads( | 140 virtual void ConfirmCloseWithPendingDownloads( |
136 int download_count, | 141 int download_count, |
137 const base::Callback<void(bool)>& callback) OVERRIDE { | 142 const base::Callback<void(bool)>& callback) OVERRIDE { |
143 EXPECT_NE(NO_USER_CHOICE, user_choice_); | |
138 switch (user_choice_) { | 144 switch (user_choice_) { |
145 case NO_USER_CHOICE: | |
139 case USER_CHOICE_USER_CANCELS_CLOSE: { | 146 case USER_CHOICE_USER_CANCELS_CLOSE: { |
140 callback.Run(false); | 147 callback.Run(false); |
141 break; | 148 break; |
142 } | 149 } |
143 case USER_CHOICE_USER_ALLOWS_CLOSE: { | 150 case USER_CHOICE_USER_ALLOWS_CLOSE: { |
144 callback.Run(true); | 151 callback.Run(true); |
145 break; | 152 break; |
146 } | 153 } |
147 } | 154 } |
148 } | 155 } |
149 | 156 |
150 private: | 157 private: |
151 explicit TestBrowserCloseManager(UserChoice user_choice) | 158 explicit TestBrowserCloseManager(UserChoice user_choice) |
152 : user_choice_(user_choice) {} | 159 : user_choice_(user_choice) {} |
153 | 160 |
154 UserChoice user_choice_; | 161 UserChoice user_choice_; |
155 | 162 |
156 DISALLOW_COPY_AND_ASSIGN(TestBrowserCloseManager); | 163 DISALLOW_COPY_AND_ASSIGN(TestBrowserCloseManager); |
157 }; | 164 }; |
158 | 165 |
166 class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate { | |
167 public: | |
168 explicit TestDownloadManagerDelegate(Profile* profile) | |
169 : ChromeDownloadManagerDelegate(profile) { | |
asanka
2013/10/19 00:42:31
On ChromeOS, the DriveIntegrationService kicks in
felt
2013/10/19 02:51:50
Thank you for looking into this. That has fixed it
| |
170 } | |
171 | |
172 virtual bool DetermineDownloadTarget( | |
173 content::DownloadItem* item, | |
174 const content::DownloadTargetCallback& callback) OVERRIDE { | |
175 content::DownloadTargetCallback dangerous_callback = | |
176 base::Bind(&TestDownloadManagerDelegate::SetDangerous, this, callback); | |
177 ChromeDownloadManagerDelegate::DetermineDownloadTarget( | |
178 item, dangerous_callback); | |
asanka
2013/10/19 00:42:31
Nit: For extra resiliency:
return CDMD::Determine
felt
2013/10/19 02:51:50
Done.
| |
179 return true; | |
180 } | |
181 | |
182 void SetDangerous( | |
183 const content::DownloadTargetCallback& callback, | |
184 const base::FilePath& target_path, | |
185 content::DownloadItem::TargetDisposition disp, | |
186 content::DownloadDangerType danger_type, | |
187 const base::FilePath& intermediate_path) { | |
188 callback.Run(target_path, | |
189 disp, | |
190 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, | |
191 intermediate_path); | |
192 } | |
193 | |
194 private: | |
195 virtual ~TestDownloadManagerDelegate() {} | |
196 }; | |
197 | |
159 } // namespace | 198 } // namespace |
160 | 199 |
161 class BrowserCloseManagerBrowserTest | 200 class BrowserCloseManagerBrowserTest |
162 : public InProcessBrowserTest, | 201 : public InProcessBrowserTest, |
163 public testing::WithParamInterface<bool> { | 202 public testing::WithParamInterface<bool> { |
164 protected: | 203 protected: |
165 virtual void SetUpOnMainThread() OVERRIDE { | 204 virtual void SetUpOnMainThread() OVERRIDE { |
166 InProcessBrowserTest::SetUpOnMainThread(); | 205 InProcessBrowserTest::SetUpOnMainThread(); |
167 SessionStartupPref::SetStartupPref( | 206 SessionStartupPref::SetStartupPref( |
168 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST)); | 207 browser()->profile(), SessionStartupPref(SessionStartupPref::LAST)); |
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
612 chrome::CloseAllBrowsers(); | 651 chrome::CloseAllBrowsers(); |
613 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow()); | 652 ASSERT_FALSE(browsers_[0]->ShouldCloseWindow()); |
614 ASSERT_NO_FATAL_FAILURE(dialogs_.AcceptClose()); | 653 ASSERT_NO_FATAL_FAILURE(dialogs_.AcceptClose()); |
615 ASSERT_NO_FATAL_FAILURE(dialogs_.AcceptClose()); | 654 ASSERT_NO_FATAL_FAILURE(dialogs_.AcceptClose()); |
616 | 655 |
617 close_observer.Wait(); | 656 close_observer.Wait(); |
618 EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); | 657 EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
619 EXPECT_TRUE(chrome::BrowserIterator().done()); | 658 EXPECT_TRUE(chrome::BrowserIterator().done()); |
620 } | 659 } |
621 | 660 |
661 #if defined(CHROMEOS) | |
662 // TODO(felt): figure out why this isn't working on CrOS. | |
663 #define MAYBE_TestWithDangerousUrlDownload DISABLED_TestWithDangerousUrlDownload | |
664 #else | |
665 #define MAYBE_TestWithDangerousUrlDownload TestWithDangerousUrlDownload | |
666 #endif | |
667 // Test shutdown with a DANGEROUS_URL download undecided. | |
668 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, | |
669 TestWithDangerousUrlDownload) { | |
670 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
671 | |
672 // Set up the fake delegate that forces the download to be malicious. | |
673 scoped_refptr<TestDownloadManagerDelegate> test_delegate( | |
674 new TestDownloadManagerDelegate(browser()->profile())); | |
675 DownloadServiceFactory::GetForBrowserContext(browser()->profile())-> | |
676 SetDownloadManagerDelegateForTesting(test_delegate.get()); | |
677 | |
678 // Run a dangerous download, but the user doesn't make a decision. | |
679 // This .swf normally would be categorized as DANGEROUS_HOST, but | |
asanka
2013/10/19 00:42:31
Nit: DANGEROUS_FILE
felt
2013/10/19 02:51:50
Done.
| |
680 // TestDownloadManagerDelegate turns it into DANGEROUS_URL. | |
681 base::FilePath file(FILE_PATH_LITERAL("downloads/dangerous/dangerous.swf")); | |
682 GURL download_url(content::URLRequestMockHTTPJob::GetMockUrl(file)); | |
683 content::DownloadTestObserverInterrupted observer( | |
684 content::BrowserContext::GetDownloadManager(browser()->profile()), | |
685 1, | |
686 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT); | |
687 ui_test_utils::NavigateToURLWithDisposition( | |
688 browser(), | |
689 GURL(download_url), | |
690 NEW_BACKGROUND_TAB, | |
691 ui_test_utils::BROWSER_TEST_NONE); | |
692 observer.WaitForFinished(); | |
693 | |
694 // Check that the download manager has the expected state. | |
695 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager( | |
696 browser()->profile())->InProgressCount()); | |
697 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager( | |
698 browser()->profile())->NonMaliciousInProgressCount()); | |
699 | |
700 // Close the browser with no user action. | |
701 RepeatedNotificationObserver close_observer( | |
702 chrome::NOTIFICATION_BROWSER_CLOSED, 1); | |
703 TestBrowserCloseManager::AttemptClose( | |
704 TestBrowserCloseManager::NO_USER_CHOICE); | |
705 close_observer.Wait(); | |
706 EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); | |
707 EXPECT_TRUE(chrome::BrowserIterator().done()); | |
708 } | |
709 | |
622 // Test shutdown with a download in progress. | 710 // Test shutdown with a download in progress. |
623 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestWithDownloads) { | 711 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestWithDownloads) { |
624 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 712 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
625 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); | 713 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); |
626 content::TestNavigationObserver navigation_observer( | 714 content::TestNavigationObserver navigation_observer( |
627 browser()->tab_strip_model()->GetActiveWebContents(), 1); | 715 browser()->tab_strip_model()->GetActiveWebContents(), 1); |
628 TestBrowserCloseManager::AttemptClose( | 716 TestBrowserCloseManager::AttemptClose( |
629 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE); | 717 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE); |
630 EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); | 718 EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
631 navigation_observer.Wait(); | 719 navigation_observer.Wait(); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
711 TestBrowserCloseManager::AttemptClose( | 799 TestBrowserCloseManager::AttemptClose( |
712 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); | 800 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); |
713 close_observer.Wait(); | 801 close_observer.Wait(); |
714 EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); | 802 EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
715 EXPECT_TRUE(chrome::BrowserIterator().done()); | 803 EXPECT_TRUE(chrome::BrowserIterator().done()); |
716 } | 804 } |
717 | 805 |
718 INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest, | 806 INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest, |
719 BrowserCloseManagerBrowserTest, | 807 BrowserCloseManagerBrowserTest, |
720 testing::Bool()); | 808 testing::Bool()); |
OLD | NEW |