From ae4f7beac76705508fb5361c3f35bc4fd4a4881f Mon Sep 17 00:00:00 2001 From: cepera Date: Fri, 8 Jul 2022 17:56:34 +0300 Subject: [PATCH 1/4] Closing last tab should open a new tab --- browser/ui/brave_browser.cc | 9 ++++ browser/ui/brave_browser.h | 1 + browser/ui/brave_browser_browsertest.cc | 47 +++++++++++++++++++ .../ui/webui/brave_welcome_ui_browsertest.cc | 1 + chromium_src/chrome/browser/ui/browser.h | 2 + .../chrome/browser/ui/unload_controller.cc | 14 ++++++ .../chrome/browser/ui/unload_controller.h | 18 +++++++ 7 files changed, 92 insertions(+) create mode 100644 chromium_src/chrome/browser/ui/unload_controller.cc create mode 100644 chromium_src/chrome/browser/ui/unload_controller.h diff --git a/browser/ui/brave_browser.cc b/browser/ui/brave_browser.cc index d2600caf395e..ad232b7c0c6e 100644 --- a/browser/ui/brave_browser.cc +++ b/browser/ui/brave_browser.cc @@ -10,6 +10,7 @@ #include "chrome/browser/lifetime/browser_close_manager.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/search/search.h" +#include "chrome/browser/ui/browser_tabstrip.h" #include "chrome/common/webui_url_constants.h" #include "components/prefs/pref_service.h" #include "content/public/common/url_constants.h" @@ -70,6 +71,14 @@ void BraveBrowser::ScheduleUIUpdate(content::WebContents* source, #endif } +void BraveBrowser::TabStripEmpty() { + if (unload_controller_.is_attempting_to_close_browser()) { + Browser::TabStripEmpty(); + return; + } + chrome::AddTabAt(this, GetNewTabURL(), -1, true); +} + bool BraveBrowser::ShouldDisplayFavicon( content::WebContents* web_contents) const { // Override to not show favicon for NTP in tab. diff --git a/browser/ui/brave_browser.h b/browser/ui/brave_browser.h index 357126cbd10e..1ec245bb7582 100644 --- a/browser/ui/brave_browser.h +++ b/browser/ui/brave_browser.h @@ -48,6 +48,7 @@ class BraveBrowser : public Browser { const base::RepeatingCallback& on_close_confirmed) override; void ResetTryToCloseWindow() override; + void TabStripEmpty() override; // Returns true when we should ask browser closing to users before handling // any warning/onbeforeunload handlers. bool ShouldAskForBrowserClosingBeforeHandlers(); diff --git a/browser/ui/brave_browser_browsertest.cc b/browser/ui/brave_browser_browsertest.cc index a1c29833edcf..2dbd9832e8b2 100644 --- a/browser/ui/brave_browser_browsertest.cc +++ b/browser/ui/brave_browser_browsertest.cc @@ -4,13 +4,31 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "brave/browser/ui/brave_browser.h" +#include "chrome/browser/ui/browser_finder.h" +#include "chrome/browser/ui/browser_tabstrip.h" +#include "chrome/browser/ui/startup/launch_mode_recorder.h" +#include "chrome/browser/ui/startup/startup_browser_creator.h" +#include "chrome/browser/ui/startup/startup_browser_creator_impl.h" #include "chrome/browser/ui/tabs/tab_strip_model.h" +#include "chrome/browser/ui/views/frame/browser_view.h" #include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/ui_test_utils.h" #include "content/public/test/browser_test.h" +#include "content/public/test/test_navigation_observer.h" +#include "content/public/test/test_utils.h" using BraveBrowserBrowserTest = InProcessBrowserTest; +namespace { +Browser* OpenNewBrowser(Profile* profile) { + base::CommandLine dummy(base::CommandLine::NO_PROGRAM); + StartupBrowserCreatorImpl creator(base::FilePath(), dummy, + chrome::startup::IsFirstRun::kYes); + creator.Launch(profile, chrome::startup::IsProcessStartup::kNo, nullptr); + return chrome::FindBrowserWithProfile(profile); +} +} // namespace + IN_PROC_BROWSER_TEST_F(BraveBrowserBrowserTest, NTPFaviconTest) { ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GURL("brave://newtab/"))); @@ -18,3 +36,32 @@ IN_PROC_BROWSER_TEST_F(BraveBrowserBrowserTest, NTPFaviconTest) { EXPECT_FALSE( browser()->ShouldDisplayFavicon(tab_model->GetActiveWebContents())); } + +IN_PROC_BROWSER_TEST_F(BraveBrowserBrowserTest, OpenNewTabWhenTabStripIsEmpty) { + ASSERT_TRUE(embedded_test_server()->Start()); + Browser* new_browser = OpenNewBrowser(browser()->profile()); + ASSERT_TRUE(new_browser); + TabStripModel* tab_strip = new_browser->tab_strip_model(); + auto page_url = embedded_test_server()->GetURL("/empty.html"); + ASSERT_TRUE(ui_test_utils::NavigateToURL(new_browser, page_url)); + + ASSERT_EQ(1, tab_strip->count()); + EXPECT_EQ(page_url, + tab_strip->GetWebContentsAt(0)->GetURL().possibly_invalid_spec()); + // Close the last tab. + tab_strip->GetActiveWebContents()->Close(); + base::RunLoop().RunUntilIdle(); + ASSERT_EQ(1, tab_strip->count()); + // Expecting a new tab is opened. + EXPECT_EQ(new_browser->GetNewTabURL(), + tab_strip->GetWebContentsAt(0)->GetURL().possibly_invalid_spec()); + // Add a couple of more tabs. + chrome::AddTabAt(new_browser, new_browser->GetNewTabURL(), -1, true); + chrome::AddTabAt(new_browser, new_browser->GetNewTabURL(), -1, true); + ASSERT_EQ(3, tab_strip->count()); + EXPECT_EQ(chrome::GetTotalBrowserCount(), 2u); + // Close the browser window. + new_browser->window()->Close(); + base::RunLoop().RunUntilIdle(); + EXPECT_EQ(chrome::GetTotalBrowserCount(), 1u); +} diff --git a/browser/ui/webui/brave_welcome_ui_browsertest.cc b/browser/ui/webui/brave_welcome_ui_browsertest.cc index 69abd875e8a6..09aa86a5b461 100644 --- a/browser/ui/webui/brave_welcome_ui_browsertest.cc +++ b/browser/ui/webui/brave_welcome_ui_browsertest.cc @@ -20,6 +20,7 @@ #include "content/public/test/browser_test.h" #include "content/public/test/test_navigation_observer.h" #include "content/public/test/test_utils.h" +#include "net/test/embedded_test_server/embedded_test_server.h" namespace { Browser* OpenNewBrowser(Profile* profile) { diff --git a/chromium_src/chrome/browser/ui/browser.h b/chromium_src/chrome/browser/ui/browser.h index 4cdb7c88d633..f8b8ad3c7fa8 100644 --- a/chromium_src/chrome/browser/ui/browser.h +++ b/chromium_src/chrome/browser/ui/browser.h @@ -12,6 +12,7 @@ #define ScheduleUIUpdate virtual ScheduleUIUpdate #define ShouldDisplayFavicon virtual ShouldDisplayFavicon #define TryToCloseWindow virtual TryToCloseWindow +#define TabStripEmpty virtual TabStripEmpty #define ResetTryToCloseWindow virtual ResetTryToCloseWindow #define FullscreenControllerInteractiveTest \ FullscreenControllerInteractiveTest; \ @@ -23,6 +24,7 @@ #undef FullscreenControllerInteractiveTest #undef ResetTryToCloseWindow #undef TryToCloseWindow +#undef TabStripEmpty #undef ShouldDisplayFavicon #undef ScheduleUIUpdate #undef FinishWarnBeforeClosing diff --git a/chromium_src/chrome/browser/ui/unload_controller.cc b/chromium_src/chrome/browser/ui/unload_controller.cc new file mode 100644 index 000000000000..512704b30b0b --- /dev/null +++ b/chromium_src/chrome/browser/ui/unload_controller.cc @@ -0,0 +1,14 @@ +/* Copyright (c) 2022 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "chrome/browser/ui/browser.h" + +#include "chrome/browser/ui/unload_controller.h" + +#define TabStripEmpty \ + TabStripEmpty() {} \ + void UnloadController::TabStripEmpty_ChromiumImpl +#include "src/chrome/browser/ui/unload_controller.cc" +#undef TabStripEmpty diff --git a/chromium_src/chrome/browser/ui/unload_controller.h b/chromium_src/chrome/browser/ui/unload_controller.h new file mode 100644 index 000000000000..1e7bd6e36bf7 --- /dev/null +++ b/chromium_src/chrome/browser/ui/unload_controller.h @@ -0,0 +1,18 @@ +/* Copyright (c) 2022 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_ +#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_ + +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/tabs/tab_strip_model_observer.h" + +#define TabStripEmpty \ + TabStripEmpty_ChromiumImpl(); \ + void TabStripEmpty +#include "src/chrome/browser/ui/unload_controller.h" +#undef TabStripEmpty + +#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_UNLOAD_CONTROLLER_H_ From b3f17130486fc862ae387c959db13d2a01123990 Mon Sep 17 00:00:00 2001 From: sergey Date: Wed, 13 Jul 2022 10:50:02 +0300 Subject: [PATCH 2/4] Added option to close last tab by default --- app/brave_settings_strings.grdp | 3 +++ browser/brave_profile_prefs.cc | 1 + .../api/settings_private/brave_prefs_util.cc | 2 ++ .../brave_help_tips_page.html | 4 +++ browser/ui/brave_browser.cc | 1 + browser/ui/brave_browser_browsertest.cc | 25 +++++++++++++++++++ ...ave_settings_localized_strings_provider.cc | 1 + chromium_src/chrome/browser/ui/browser.h | 1 + .../chrome/browser/ui/unload_controller.cc | 14 ++++++++--- .../chrome/browser/ui/unload_controller.h | 1 - components/constants/pref_names.cc | 1 + components/constants/pref_names.h | 2 +- 12 files changed, 50 insertions(+), 6 deletions(-) diff --git a/app/brave_settings_strings.grdp b/app/brave_settings_strings.grdp index fb1ba4eb8847..c289ea6112e4 100644 --- a/app/brave_settings_strings.grdp +++ b/app/brave_settings_strings.grdp @@ -988,6 +988,9 @@ Warn me before closing window with multiple tabs + + Close window when closing last tab +