Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closing last tab should open a new tab #14092

Merged
merged 4 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/brave_settings_strings.grdp
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,9 @@
<message name="IDS_SETTINGS_WINDOW_CLOSING_CONFIRM_OPTION_LABEL" desc="The text for settings option">
Warn me before closing window with multiple tabs
</message>
<message name="IDS_SETTINGS_CLOSING_LAST_TAB_OPTION_LABEL" desc="The text for settings option">
Close window when closing last tab
</message>

<!-- Avatars. Generated via zsh and node:
declare -i a=56
Expand Down
1 change: 1 addition & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->SetDefaultPrefValue(ntp_prefs::kNtpUseMostVisitedTiles,
base::Value(true));
registry->RegisterBooleanPref(kEnableWindowClosingConfirm, true);
registry->RegisterBooleanPref(kEnableClosingLastTab, true);
RegisterDefaultBraveBrowserPromptPrefs(registry);
#endif

Expand Down
2 changes: 2 additions & 0 deletions browser/extensions/api/settings_private/brave_prefs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetAllowlistedKeys() {
#endif
(*s_brave_allowlist)[kEnableWindowClosingConfirm] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_allowlist)[kEnableClosingLastTab] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
// Hangouts pref
(*s_brave_allowlist)[kHangoutsEnabled] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
Expand Down
17 changes: 17 additions & 0 deletions browser/resources/settings/brave_overrides/basic_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ function createSectionElement (sectionName, titleName, childName, childAttribute
`
}

/**
* Creates a settings-toggle-button element and returns it.
* @param {string} pref - preference path to handle by toggle
* @param {string} label - label for the element
* @returns {Element}
*/
function createToggleButtonElement (pref, label) {
return html`
<settings-toggle-button class="cr-row" pref="{{${pref}}}" label="${loadTimeData.getString(label)}">
</settings-toggle-button>
`
}

RegisterStyleOverride(
'settings-basic-page',
html`
Expand Down Expand Up @@ -240,6 +253,10 @@ RegisterPolymerTemplateModifications({
// Move help tips after downloads
const sectionDownloads = getSectionElement(advancedSubSectionsTemplate.content, 'downloads')
sectionDownloads.insertAdjacentElement('afterend', sectionHelpTips)
// Add an element to Chromium's system section
const buttonElement = createToggleButtonElement("prefs.brave.enable_closing_last_tab", "braveHelpTipsClosingLastTab")
const sectionSystem = getSectionElement(advancedSubSectionsTemplate.content, 'system')
sectionSystem.appendChild(buttonElement)
}
}
})
12 changes: 12 additions & 0 deletions browser/ui/brave_browser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -70,6 +71,17 @@ void BraveBrowser::ScheduleUIUpdate(content::WebContents* source,
#endif
}

void BraveBrowser::TabStripEmpty() {
if (unload_controller_.is_attempting_to_close_browser()) {
Browser::TabStripEmpty();
return;
}

base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(&chrome::AddTabAt, this, GetNewTabURL(), -1,
true, absl::nullopt));
}

bool BraveBrowser::ShouldDisplayFavicon(
content::WebContents* web_contents) const {
// Override to not show favicon for NTP in tab.
Expand Down
1 change: 1 addition & 0 deletions browser/ui/brave_browser.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class BraveBrowser : public Browser {
const base::RepeatingCallback<void(bool)>& 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();
Expand Down
78 changes: 78 additions & 0 deletions browser/ui/brave_browser_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,95 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/ui/brave_browser.h"
#include "brave/components/constants/pref_names.h"
#include "chrome/browser/profiles/profile.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 "components/prefs/pref_service.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/")));

auto* tab_model = browser()->tab_strip_model();
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);
new_browser->profile()->GetPrefs()->SetBoolean(kEnableClosingLastTab, false);
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());
// No reentrancy for Ctrl+W
tab_strip->CloseSelectedTabs();
base::RunLoop().RunUntilIdle();
// 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);
}

IN_PROC_BROWSER_TEST_F(BraveBrowserBrowserTest,
DoNotOpenNewTabWhenTabStripIsEmpty) {
ASSERT_TRUE(embedded_test_server()->Start());
Browser* new_browser = OpenNewBrowser(browser()->profile());
ASSERT_TRUE(new_browser);
new_browser->profile()->GetPrefs()->SetBoolean(kEnableClosingLastTab, true);
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());
EXPECT_EQ(chrome::GetTotalBrowserCount(), 2u);
// Close the last tab.
tab_strip->GetActiveWebContents()->Close();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(chrome::GetTotalBrowserCount(), 1u);
}
1 change: 1 addition & 0 deletions browser/ui/webui/brave_welcome_ui_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source,
IDS_SETTINGS_HELP_TIPS_SHOW_BRAVE_WAYBACK_MACHINE_PROMPT},
{"braveHelpTipsWarnBeforeClosingWindow",
IDS_SETTINGS_WINDOW_CLOSING_CONFIRM_OPTION_LABEL},
{"braveHelpTipsClosingLastTab", IDS_SETTINGS_CLOSING_LAST_TAB_OPTION_LABEL},
// New Tab Page
{"braveNewTab", IDS_SETTINGS_NEW_TAB},
{"braveNewTabBraveRewards", IDS_SETTINGS_NEW_TAB_BRAVE_REWARDS},
Expand Down
3 changes: 3 additions & 0 deletions chromium_src/chrome/browser/ui/browser.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_BROWSER_H_
#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_BROWSER_H_

#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#include "chrome/browser/ui/unload_controller.h"

#define FinishWarnBeforeClosing virtual FinishWarnBeforeClosing
#define ScheduleUIUpdate virtual ScheduleUIUpdate
#define ShouldDisplayFavicon virtual ShouldDisplayFavicon
#define TryToCloseWindow virtual TryToCloseWindow
#define TabStripEmpty virtual TabStripEmpty
#define ResetTryToCloseWindow virtual ResetTryToCloseWindow
#define FullscreenControllerInteractiveTest \
FullscreenControllerInteractiveTest; \
Expand All @@ -23,6 +25,7 @@
#undef FullscreenControllerInteractiveTest
#undef ResetTryToCloseWindow
#undef TryToCloseWindow
#undef TabStripEmpty
#undef ShouldDisplayFavicon
#undef ScheduleUIUpdate
#undef FinishWarnBeforeClosing
Expand Down
20 changes: 20 additions & 0 deletions chromium_src/chrome/browser/ui/unload_controller.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* 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/unload_controller.h"
#include "brave/components/constants/pref_names.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "components/prefs/pref_service.h"

#define TabStripEmpty \
TabStripEmpty() { \
if (browser_->profile()->GetPrefs()->GetBoolean(kEnableClosingLastTab)) { \
TabStripEmpty_ChromiumImpl(); \
} \
} \
void UnloadController::TabStripEmpty_ChromiumImpl
#include "src/chrome/browser/ui/unload_controller.cc"
#undef TabStripEmpty
17 changes: 17 additions & 0 deletions chromium_src/chrome/browser/ui/unload_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* 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/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_
1 change: 1 addition & 0 deletions components/constants/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ const char kSafetynetStatus[] = "safetynet.status";
#if !BUILDFLAG(IS_ANDROID)
const char kEnableWindowClosingConfirm[] =
"brave.enable_window_closing_confirm";
const char kEnableClosingLastTab[] = "brave.enable_closing_last_tab";
#endif

const char kDefaultBrowserLaunchingCount[] =
Expand Down
2 changes: 1 addition & 1 deletion components/constants/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ extern const char kSafetynetStatus[];

#if !BUILDFLAG(IS_ANDROID)
extern const char kEnableWindowClosingConfirm[];
extern const char kEnableClosingLastTab[];
#endif

extern const char kDefaultBrowserLaunchingCount[];
extern const char kTabsSearchShow[];
extern const char kDontAskForCrashReporting[];
Expand Down