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) { |
| 170 SetNextId(content::DownloadItem::kInvalidId + 1); |
| 171 } |
| 172 |
| 173 virtual bool DetermineDownloadTarget( |
| 174 content::DownloadItem* item, |
| 175 const content::DownloadTargetCallback& callback) OVERRIDE { |
| 176 content::DownloadTargetCallback dangerous_callback = |
| 177 base::Bind(&TestDownloadManagerDelegate::SetDangerous, this, callback); |
| 178 return ChromeDownloadManagerDelegate::DetermineDownloadTarget( |
| 179 item, dangerous_callback); |
| 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 // Test shutdown with a DANGEROUS_URL download undecided. |
| 662 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
| 663 TestWithDangerousUrlDownload) { |
| 664 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 665 |
| 666 // Set up the fake delegate that forces the download to be malicious. |
| 667 scoped_refptr<TestDownloadManagerDelegate> test_delegate( |
| 668 new TestDownloadManagerDelegate(browser()->profile())); |
| 669 DownloadServiceFactory::GetForBrowserContext(browser()->profile())-> |
| 670 SetDownloadManagerDelegateForTesting(test_delegate.get()); |
| 671 |
| 672 // Run a dangerous download, but the user doesn't make a decision. |
| 673 // This .swf normally would be categorized as DANGEROUS_FILE, but |
| 674 // TestDownloadManagerDelegate turns it into DANGEROUS_URL. |
| 675 base::FilePath file(FILE_PATH_LITERAL("downloads/dangerous/dangerous.swf")); |
| 676 GURL download_url(content::URLRequestMockHTTPJob::GetMockUrl(file)); |
| 677 content::DownloadTestObserverInterrupted observer( |
| 678 content::BrowserContext::GetDownloadManager(browser()->profile()), |
| 679 1, |
| 680 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_QUIT); |
| 681 ui_test_utils::NavigateToURLWithDisposition( |
| 682 browser(), |
| 683 GURL(download_url), |
| 684 NEW_BACKGROUND_TAB, |
| 685 ui_test_utils::BROWSER_TEST_NONE); |
| 686 observer.WaitForFinished(); |
| 687 |
| 688 // Check that the download manager has the expected state. |
| 689 EXPECT_EQ(1, content::BrowserContext::GetDownloadManager( |
| 690 browser()->profile())->InProgressCount()); |
| 691 EXPECT_EQ(0, content::BrowserContext::GetDownloadManager( |
| 692 browser()->profile())->NonMaliciousInProgressCount()); |
| 693 |
| 694 // Close the browser with no user action. |
| 695 RepeatedNotificationObserver close_observer( |
| 696 chrome::NOTIFICATION_BROWSER_CLOSED, 1); |
| 697 TestBrowserCloseManager::AttemptClose( |
| 698 TestBrowserCloseManager::NO_USER_CHOICE); |
| 699 close_observer.Wait(); |
| 700 EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
| 701 EXPECT_TRUE(chrome::BrowserIterator().done()); |
| 702 } |
| 703 |
622 // Test shutdown with a download in progress. | 704 // Test shutdown with a download in progress. |
623 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestWithDownloads) { | 705 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, TestWithDownloads) { |
624 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 706 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
625 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); | 707 ASSERT_NO_FATAL_FAILURE(CreateStalledDownload(browser())); |
626 content::TestNavigationObserver navigation_observer( | 708 content::TestNavigationObserver navigation_observer( |
627 browser()->tab_strip_model()->GetActiveWebContents(), 1); | 709 browser()->tab_strip_model()->GetActiveWebContents(), 1); |
628 TestBrowserCloseManager::AttemptClose( | 710 TestBrowserCloseManager::AttemptClose( |
629 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE); | 711 TestBrowserCloseManager::USER_CHOICE_USER_CANCELS_CLOSE); |
630 EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); | 712 EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
631 navigation_observer.Wait(); | 713 navigation_observer.Wait(); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
711 TestBrowserCloseManager::AttemptClose( | 793 TestBrowserCloseManager::AttemptClose( |
712 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); | 794 TestBrowserCloseManager::USER_CHOICE_USER_ALLOWS_CLOSE); |
713 close_observer.Wait(); | 795 close_observer.Wait(); |
714 EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); | 796 EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
715 EXPECT_TRUE(chrome::BrowserIterator().done()); | 797 EXPECT_TRUE(chrome::BrowserIterator().done()); |
716 } | 798 } |
717 | 799 |
718 INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest, | 800 INSTANTIATE_TEST_CASE_P(BrowserCloseManagerBrowserTest, |
719 BrowserCloseManagerBrowserTest, | 801 BrowserCloseManagerBrowserTest, |
720 testing::Bool()); | 802 testing::Bool()); |
OLD | NEW |