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 "chrome/browser/lifetime/browser_close_manager.h" | 5 #include "chrome/browser/lifetime/browser_close_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "chrome/browser/background/background_mode_manager.h" | 14 #include "chrome/browser/background/background_mode_manager.h" |
15 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
16 #include "chrome/browser/browser_shutdown.h" | 16 #include "chrome/browser/browser_shutdown.h" |
17 #include "chrome/browser/chrome_notification_types.h" | 17 #include "chrome/browser/chrome_notification_types.h" |
18 #include "chrome/browser/defaults.h" | 18 #include "chrome/browser/defaults.h" |
19 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 19 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
20 #include "chrome/browser/download/download_prefs.h" | 20 #include "chrome/browser/download/download_prefs.h" |
21 #include "chrome/browser/download/download_service.h" | 21 #include "chrome/browser/download/download_service.h" |
22 #include "chrome/browser/download/download_service_factory.h" | 22 #include "chrome/browser/download/download_service_factory.h" |
23 #include "chrome/browser/lifetime/application_lifetime.h" | 23 #include "chrome/browser/lifetime/application_lifetime.h" |
24 #include "chrome/browser/lifetime/keep_alive_types.h" | 24 #include "chrome/browser/lifetime/keep_alive_types.h" |
25 #include "chrome/browser/lifetime/scoped_keep_alive.h" | 25 #include "chrome/browser/lifetime/scoped_keep_alive.h" |
26 #include "chrome/browser/net/url_request_mock_util.h" | 26 #include "chrome/browser/net/url_request_mock_util.h" |
27 #include "chrome/browser/prefs/session_startup_pref.h" | 27 #include "chrome/browser/prefs/session_startup_pref.h" |
28 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
29 #include "chrome/browser/profiles/profile_manager.h" | 29 #include "chrome/browser/profiles/profile_manager.h" |
| 30 #include "chrome/browser/sessions/tab_restore_service_factory.h" |
30 #include "chrome/browser/ui/browser.h" | 31 #include "chrome/browser/ui/browser.h" |
31 #include "chrome/browser/ui/browser_commands.h" | 32 #include "chrome/browser/ui/browser_commands.h" |
32 #include "chrome/browser/ui/browser_list.h" | 33 #include "chrome/browser/ui/browser_list.h" |
33 #include "chrome/browser/ui/browser_window.h" | 34 #include "chrome/browser/ui/browser_window.h" |
34 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 35 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
35 #include "chrome/common/chrome_switches.h" | 36 #include "chrome/common/chrome_switches.h" |
36 #include "chrome/common/url_constants.h" | 37 #include "chrome/common/url_constants.h" |
37 #include "chrome/test/base/in_process_browser_test.h" | 38 #include "chrome/test/base/in_process_browser_test.h" |
38 #include "chrome/test/base/ui_test_utils.h" | 39 #include "chrome/test/base/ui_test_utils.h" |
39 #include "components/app_modal/javascript_app_modal_dialog.h" | 40 #include "components/app_modal/javascript_app_modal_dialog.h" |
40 #include "components/app_modal/native_app_modal_dialog.h" | 41 #include "components/app_modal/native_app_modal_dialog.h" |
| 42 #include "components/sessions/core/tab_restore_service.h" |
| 43 #include "components/sessions/core/tab_restore_service_observer.h" |
41 #include "content/public/browser/browser_context.h" | 44 #include "content/public/browser/browser_context.h" |
42 #include "content/public/browser/download_item.h" | 45 #include "content/public/browser/download_item.h" |
43 #include "content/public/browser/download_manager.h" | 46 #include "content/public/browser/download_manager.h" |
44 #include "content/public/browser/notification_service.h" | 47 #include "content/public/browser/notification_service.h" |
45 #include "content/public/browser/render_view_host.h" | 48 #include "content/public/browser/render_view_host.h" |
46 #include "content/public/browser/render_widget_host.h" | 49 #include "content/public/browser/render_widget_host.h" |
47 #include "content/public/browser/web_contents.h" | 50 #include "content/public/browser/web_contents.h" |
| 51 #include "content/public/test/browser_test_utils.h" |
48 #include "content/public/test/download_test_observer.h" | 52 #include "content/public/test/download_test_observer.h" |
49 #include "content/public/test/test_navigation_observer.h" | 53 #include "content/public/test/test_navigation_observer.h" |
50 #include "net/test/embedded_test_server/embedded_test_server.h" | 54 #include "net/test/embedded_test_server/embedded_test_server.h" |
51 #include "net/test/url_request/url_request_mock_http_job.h" | 55 #include "net/test/url_request/url_request_mock_http_job.h" |
52 #include "net/test/url_request/url_request_slow_download_job.h" | 56 #include "net/test/url_request/url_request_slow_download_job.h" |
53 | 57 |
54 #if defined(OS_CHROMEOS) | 58 #if defined(OS_CHROMEOS) |
55 #include "chromeos/chromeos_switches.h" | 59 #include "chromeos/chromeos_switches.h" |
56 #endif | 60 #endif |
57 | 61 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 | 110 |
107 private: | 111 private: |
108 int num_outstanding_; | 112 int num_outstanding_; |
109 content::NotificationRegistrar registrar_; | 113 content::NotificationRegistrar registrar_; |
110 bool running_; | 114 bool running_; |
111 base::RunLoop run_loop_; | 115 base::RunLoop run_loop_; |
112 | 116 |
113 DISALLOW_COPY_AND_ASSIGN(RepeatedNotificationObserver); | 117 DISALLOW_COPY_AND_ASSIGN(RepeatedNotificationObserver); |
114 }; | 118 }; |
115 | 119 |
| 120 class TabRestoreServiceChangesObserver |
| 121 : public sessions::TabRestoreServiceObserver { |
| 122 public: |
| 123 explicit TabRestoreServiceChangesObserver(Profile* profile) |
| 124 : service_(TabRestoreServiceFactory::GetForProfile(profile)) { |
| 125 if (service_) |
| 126 service_->AddObserver(this); |
| 127 } |
| 128 |
| 129 ~TabRestoreServiceChangesObserver() override { |
| 130 if (service_) |
| 131 service_->RemoveObserver(this); |
| 132 } |
| 133 |
| 134 size_t changes_count() const { return changes_count_; } |
| 135 |
| 136 private: |
| 137 // sessions::TabRestoreServiceObserver: |
| 138 void TabRestoreServiceChanged(sessions::TabRestoreService*) override { |
| 139 changes_count_++; |
| 140 } |
| 141 |
| 142 // sessions::TabRestoreServiceObserver: |
| 143 void TabRestoreServiceDestroyed(sessions::TabRestoreService*) override { |
| 144 service_ = nullptr; |
| 145 } |
| 146 |
| 147 sessions::TabRestoreService* service_ = nullptr; |
| 148 size_t changes_count_ = 0; |
| 149 |
| 150 DISALLOW_COPY_AND_ASSIGN(TabRestoreServiceChangesObserver); |
| 151 }; |
| 152 |
116 class TestBrowserCloseManager : public BrowserCloseManager { | 153 class TestBrowserCloseManager : public BrowserCloseManager { |
117 public: | 154 public: |
118 enum UserChoice { | 155 enum UserChoice { |
119 USER_CHOICE_USER_CANCELS_CLOSE, | 156 USER_CHOICE_USER_CANCELS_CLOSE, |
120 USER_CHOICE_USER_ALLOWS_CLOSE, | 157 USER_CHOICE_USER_ALLOWS_CLOSE, |
121 NO_USER_CHOICE | 158 NO_USER_CHOICE |
122 }; | 159 }; |
123 | 160 |
124 static void AttemptClose(UserChoice user_choice) { | 161 static void AttemptClose(UserChoice user_choice) { |
125 scoped_refptr<BrowserCloseManager> browser_close_manager = | 162 scoped_refptr<BrowserCloseManager> browser_close_manager = |
(...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
701 ASSERT_NO_FATAL_FAILURE(AcceptClose()); | 738 ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
702 ASSERT_NO_FATAL_FAILURE(AcceptClose()); | 739 ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
703 ASSERT_NO_FATAL_FAILURE(AcceptClose()); | 740 ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
704 | 741 |
705 close_observer.Wait(); | 742 close_observer.Wait(); |
706 EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); | 743 EXPECT_TRUE(browser_shutdown::IsTryingToQuit()); |
707 EXPECT_TRUE(BrowserList::GetInstance()->empty()); | 744 EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
708 } | 745 } |
709 | 746 |
710 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, | 747 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
| 748 AddBeforeUnloadDuringClosing) { |
| 749 // TODO(crbug.com/250305): Currently FastUnloadController is broken. |
| 750 // And it is difficult to fix this issue without fixing that one. |
| 751 if (GetParam()) |
| 752 return; |
| 753 |
| 754 ASSERT_TRUE(embedded_test_server()->Start()); |
| 755 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 756 browser(), embedded_test_server()->GetURL("/title1.html"))); |
| 757 |
| 758 // Open second window. |
| 759 ui_test_utils::NavigateToURLWithDisposition( |
| 760 browser(), embedded_test_server()->GetURL("/beforeunload.html"), |
| 761 WindowOpenDisposition::NEW_WINDOW, |
| 762 ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER); |
| 763 EXPECT_EQ(2u, BrowserList::GetInstance()->size()); |
| 764 auto* browser2 = BrowserList::GetInstance()->get(0) != browser() |
| 765 ? BrowserList::GetInstance()->get(0) |
| 766 : BrowserList::GetInstance()->get(1); |
| 767 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(0)); |
| 768 |
| 769 // Let's work with second window only. |
| 770 // This page has beforeunload handler already. |
| 771 EXPECT_TRUE(browser2->tab_strip_model() |
| 772 ->GetWebContentsAt(0) |
| 773 ->NeedToFireBeforeUnload()); |
| 774 // This page doesn't have beforeunload handler. Yet. |
| 775 ui_test_utils::NavigateToURLWithDisposition( |
| 776 browser2, embedded_test_server()->GetURL("/title2.html"), |
| 777 WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 778 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 779 content::WaitForLoadStop(browser2->tab_strip_model()->GetWebContentsAt(1)); |
| 780 EXPECT_FALSE(browser2->tab_strip_model() |
| 781 ->GetWebContentsAt(1) |
| 782 ->NeedToFireBeforeUnload()); |
| 783 EXPECT_EQ(2, browser2->tab_strip_model()->count()); |
| 784 |
| 785 DisableHangMonitor(browser2); |
| 786 |
| 787 // The test. |
| 788 |
| 789 TabRestoreServiceChangesObserver restore_observer(browser2->profile()); |
| 790 content::WindowedNotificationObserver observer( |
| 791 chrome::NOTIFICATION_BROWSER_CLOSED, |
| 792 content::NotificationService::AllSources()); |
| 793 chrome::CloseWindow(browser2); |
| 794 // Just to be sure CloseWindow doesn't have asynchronous tasks |
| 795 // that could have an impact. |
| 796 content::RunAllPendingInMessageLoop(); |
| 797 |
| 798 // Closing browser shouldn't happen because of beforeunload handler. |
| 799 EXPECT_EQ(2u, BrowserList::GetInstance()->size()); |
| 800 // Add beforeunload handler for the 2nd (title2.html) tab which haven't had it |
| 801 // yet. |
| 802 ASSERT_TRUE(content::ExecuteScript( |
| 803 browser2->tab_strip_model()->GetWebContentsAt(1)->GetRenderViewHost(), |
| 804 "window.addEventListener('beforeunload', " |
| 805 "function(event) { event.returnValue = 'Foo'; });")); |
| 806 EXPECT_TRUE(browser2->tab_strip_model() |
| 807 ->GetWebContentsAt(1) |
| 808 ->NeedToFireBeforeUnload()); |
| 809 // Accept closing the first tab. |
| 810 ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 811 // Just to be sure accepting a dialog doesn't have asynchronous tasks |
| 812 // that could have an impact. |
| 813 content::RunAllPendingInMessageLoop(); |
| 814 // It shouldn't close the whole window/browser. |
| 815 EXPECT_EQ(2u, BrowserList::GetInstance()->size()); |
| 816 EXPECT_EQ(2, browser2->tab_strip_model()->count()); |
| 817 // Accept closing the second tab. |
| 818 ASSERT_NO_FATAL_FAILURE(AcceptClose()); |
| 819 observer.Wait(); |
| 820 // Now the second window/browser should be closed. |
| 821 EXPECT_EQ(1u, BrowserList::GetInstance()->size()); |
| 822 EXPECT_EQ(browser(), BrowserList::GetInstance()->get(0)); |
| 823 EXPECT_EQ(1u, restore_observer.changes_count()); |
| 824 |
| 825 // Restore the closed browser. |
| 826 content::WindowedNotificationObserver open_window_observer( |
| 827 chrome::NOTIFICATION_BROWSER_OPENED, |
| 828 content::NotificationService::AllSources()); |
| 829 chrome::OpenWindowWithRestoredTabs(browser()->profile()); |
| 830 open_window_observer.Wait(); |
| 831 EXPECT_EQ(2u, BrowserList::GetInstance()->size()); |
| 832 browser2 = BrowserList::GetInstance()->get(0) != browser() |
| 833 ? BrowserList::GetInstance()->get(0) |
| 834 : BrowserList::GetInstance()->get(1); |
| 835 |
| 836 // Check the restored browser contents. |
| 837 EXPECT_EQ(2, browser2->tab_strip_model()->count()); |
| 838 EXPECT_EQ(embedded_test_server()->GetURL("/beforeunload.html"), |
| 839 browser2->tab_strip_model()->GetWebContentsAt(0)->GetURL()); |
| 840 EXPECT_EQ(embedded_test_server()->GetURL("/title2.html"), |
| 841 browser2->tab_strip_model()->GetWebContentsAt(1)->GetURL()); |
| 842 } |
| 843 |
| 844 IN_PROC_BROWSER_TEST_P(BrowserCloseManagerBrowserTest, |
711 TestCloseTabDuringShutdown) { | 845 TestCloseTabDuringShutdown) { |
712 ASSERT_TRUE(embedded_test_server()->Start()); | 846 ASSERT_TRUE(embedded_test_server()->Start()); |
713 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( | 847 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
714 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); | 848 browsers_[0], embedded_test_server()->GetURL("/beforeunload.html"))); |
715 DisableHangMonitor(browsers_[0]); | 849 DisableHangMonitor(browsers_[0]); |
716 | 850 |
717 RepeatedNotificationObserver cancel_observer( | 851 RepeatedNotificationObserver cancel_observer( |
718 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); | 852 chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED, 1); |
719 chrome::CloseAllBrowsersAndQuit(); | 853 chrome::CloseAllBrowsersAndQuit(); |
720 | 854 |
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1147 | 1281 |
1148 chrome::CloseAllBrowsers(); | 1282 chrome::CloseAllBrowsers(); |
1149 EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); | 1283 EXPECT_FALSE(browser_shutdown::IsTryingToQuit()); |
1150 EXPECT_TRUE(BrowserList::GetInstance()->empty()); | 1284 EXPECT_TRUE(BrowserList::GetInstance()->empty()); |
1151 EXPECT_TRUE(IsBackgroundModeSuspended()); | 1285 EXPECT_TRUE(IsBackgroundModeSuspended()); |
1152 } | 1286 } |
1153 | 1287 |
1154 INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest, | 1288 INSTANTIATE_TEST_CASE_P(BrowserCloseManagerWithBackgroundModeBrowserTest, |
1155 BrowserCloseManagerWithBackgroundModeBrowserTest, | 1289 BrowserCloseManagerWithBackgroundModeBrowserTest, |
1156 testing::Bool()); | 1290 testing::Bool()); |
OLD | NEW |