Skip to content

Commit

Permalink
NTP: Introduce new "all widgets off" pref and migrate previous style …
Browse files Browse the repository at this point in the history
…of setting all individual widget prefs
  • Loading branch information
petemill committed Jun 10, 2021
1 parent 4a43acd commit 56b2011
Show file tree
Hide file tree
Showing 15 changed files with 194 additions and 129 deletions.
3 changes: 3 additions & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(kNewTabPageShowBinance, true);
registry->RegisterBooleanPref(kNewTabPageShowTogether, false);
registry->RegisterBooleanPref(kNewTabPageShowGemini, true);
registry->RegisterBooleanPref(kNewTabPageHideAllWidgets, false);
registry->RegisterBooleanPref(kNewTabPageMigratedHideAllWidgetsPref, false);

registry->RegisterIntegerPref(
kNewTabPageShowsOptions,
static_cast<int>(NewTabPageShowsOptions::kDashboard));
Expand Down
3 changes: 3 additions & 0 deletions browser/search/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ source_set("search") {
]

deps += [
"//base",
"//brave/browser/profiles:profiles",
"//brave/common:pref_names",
"//brave/components/crypto_dot_com/browser/buildflags",
"//brave/components/crypto_dot_com/common",
"//chrome/common",
"//components/pref_registry",
"//components/prefs",
Expand Down
74 changes: 69 additions & 5 deletions browser/search/ntp_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,96 @@

#include "brave/browser/search/ntp_utils.h"

#include "base/logging.h"
#include "brave/browser/profiles/profile_util.h"
#include "brave/common/pref_names.h"
#include "brave/components/crypto_dot_com/browser/buildflags/buildflags.h"
#include "chrome/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"

#if BUILDFLAG(CRYPTO_DOT_COM_ENABLED)
#include "brave/components/crypto_dot_com/common/pref_names.h"
#endif

namespace {

void ClearNewTabPageProfilePrefs(Profile* profile) {
PrefService* prefs = profile->GetPrefs();
prefs->ClearPref(kNewTabPageShowTopSites);
}

const char* const widget_pref_names[] = {
kNewTabPageShowRewards,
kNewTabPageShowTogether,
kNewTabPageShowBinance,
#if BUILDFLAG(CRYPTO_DOT_COM_ENABLED)
kCryptoDotComNewTabPageShowCryptoDotCom,
#endif
kNewTabPageShowGemini
};

} // namespace

namespace new_tab_page {

void MigrateNewTabPagePrefs(Profile* profile) {
// Migrate over to the Chromium setting for shortcuts visible
// Only sets the value if user has changed it
const PrefService::Preference* pref =
profile->GetPrefs()->FindPreference(kNewTabPageShowTopSites);
if (pref->HasUserSetting()) {
profile->GetPrefs()->SetBoolean(prefs::kNtpShortcutsVisible,
profile->GetPrefs()->GetBoolean(kNewTabPageShowTopSites));
auto* prefs = profile->GetPrefs();
const PrefService::Preference* top_sites_pref =
prefs->FindPreference(kNewTabPageShowTopSites);
if (top_sites_pref->HasUserSetting()) {
prefs->SetBoolean(prefs::kNtpShortcutsVisible,
prefs->GetBoolean(kNewTabPageShowTopSites));
}

// The toggle to turn off all widgets used to simply turn off
// all existing widgets. We later introduced a pref so that future
// new widgets do not show for that user. Perform a one-off migration
// for known widgets at the time to set this new pref.
// Clear and remove the migration pref at some point in the future.
// Note(petemill): It was created 2021-06-09.
const PrefService::Preference* migrate_hide_widgets_pref =
prefs->FindPreference(kNewTabPageMigratedHideAllWidgetsPref);
if (!migrate_hide_widgets_pref->HasUserSetting()) {
VLOG(1) << "Migrating hide widget pref...";
// If all the widgets are off, assume user wants no future widgets.
bool all_were_off = true;
for (auto* const pref_name : widget_pref_names) {
auto* pref = prefs->FindPreference(pref_name);
// Do not assume default is for pref to be on, so make
// sure user has overriden pref value and it is false,
// since that's what the previous version of the
// "turn off all widgets" toggle did.
bool user_turned_off = pref->HasUserSetting() &&
(pref->GetValue()->GetIfBool().value_or(true) == false);
VLOG(1) << "Setting: " << pref_name <<
", was off? " << user_turned_off;
if (user_turned_off == false) {
all_were_off = false;
break;
}
}
if (all_were_off) {
prefs->SetBoolean(kNewTabPageHideAllWidgets, true);
// Turn the widgets back on so that when user "un-hides all", they
// will show. This replicates the previous functionality, for the first
// time the user re-enables. After that, the new functionality will
// take effect, whereby each widget's show/hide setting is remembered
// individually.
prefs->SetBoolean(kNewTabPageShowRewards, true);
prefs->SetBoolean(kNewTabPageShowTogether, true);
prefs->SetBoolean(kNewTabPageShowBinance, true);
#if BUILDFLAG(CRYPTO_DOT_COM_ENABLED)
prefs->SetBoolean(kCryptoDotComNewTabPageShowCryptoDotCom, true);
#endif
prefs->SetBoolean(kNewTabPageShowGemini, true);
}
// One-off migration, so set that we have run this migration.
prefs->SetBoolean(kNewTabPageMigratedHideAllWidgetsPref, true);
VLOG(1) << "Done migrating hide widget pref.";
}

// Clear deprecated prefs.
Expand Down
79 changes: 79 additions & 0 deletions browser/search/ntp_utils_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) 2021 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 "brave/browser/search/ntp_utils.h"

#include <memory>

#include "brave/common/pref_names.h"
#include "brave/components/crypto_dot_com/browser/buildflags/buildflags.h"
#include "chrome/test/base/testing_profile.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(CRYPTO_DOT_COM_ENABLED)
#include "brave/components/crypto_dot_com/common/pref_names.h"
#endif

class NTPUtilsTest : public ::testing::Test {
public:
NTPUtilsTest() = default;

void SetUp() override {
profile_ = std::make_unique<TestingProfile>();
}

Profile* profile() { return profile_.get(); }

protected:
// BrowserTaskEnvironment is needed before TestingProfile
content::BrowserTaskEnvironment task_environment_;

std::unique_ptr<TestingProfile> profile_;
};

TEST_F(NTPUtilsTest, MigratesHideWidgetTrue) {
// Manually turn all off
auto* prefs = profile()->GetPrefs();
prefs->SetBoolean(kNewTabPageShowRewards, false);
prefs->SetBoolean(kNewTabPageShowTogether, false);
prefs->SetBoolean(kNewTabPageShowBinance, false);
#if BUILDFLAG(CRYPTO_DOT_COM_ENABLED)
prefs->SetBoolean(kCryptoDotComNewTabPageShowCryptoDotCom, false);
#endif
prefs->SetBoolean(kNewTabPageShowGemini, false);
// Migrate
new_tab_page::MigrateNewTabPagePrefs(profile());
// Expect migrated to off
EXPECT_TRUE(prefs->GetBoolean(kNewTabPageHideAllWidgets));
}

TEST_F(NTPUtilsTest, MigratesHideWidgetFalse) {
// Manually turn some off
auto* prefs = profile()->GetPrefs();
prefs->SetBoolean(kNewTabPageShowRewards, false);
prefs->SetBoolean(kNewTabPageShowTogether, true);
prefs->SetBoolean(kNewTabPageShowBinance, false);
#if BUILDFLAG(CRYPTO_DOT_COM_ENABLED)
prefs->SetBoolean(kCryptoDotComNewTabPageShowCryptoDotCom, false);
#endif
prefs->SetBoolean(kNewTabPageShowGemini, false);
// Migrate
new_tab_page::MigrateNewTabPagePrefs(profile());
// Expect not migrated
EXPECT_FALSE(prefs->GetBoolean(kNewTabPageHideAllWidgets));
}

TEST_F(NTPUtilsTest, MigratesHideWidgetFalseDefault) {
// Don't manually change any settings
// Migrate
new_tab_page::MigrateNewTabPagePrefs(profile());
// Expect not migrated
auto* prefs = profile()->GetPrefs();
EXPECT_FALSE(prefs->GetBoolean(kNewTabPageHideAllWidgets));
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ base::DictionaryValue GetPreferencesDictionary(PrefService* prefs) {
prefs->GetBoolean(kBrandedWallpaperNotificationDismissed));
pref_data.SetBoolean("isBraveTodayOptedIn",
prefs->GetBoolean(kBraveTodayOptedIn));
pref_data.SetBoolean(
"hideAllWidgets",
prefs->GetBoolean(kNewTabPageHideAllWidgets));
pref_data.SetBoolean(
"showBinance",
prefs->GetBoolean(kNewTabPageShowBinance));
Expand Down Expand Up @@ -381,6 +384,10 @@ void BraveNewTabMessageHandler::OnJavascriptAllowed() {
kNewTabPageShowGemini,
base::BindRepeating(&BraveNewTabMessageHandler::OnPreferencesChanged,
base::Unretained(this)));
pref_change_registrar_.Add(
kNewTabPageHideAllWidgets,
base::BindRepeating(&BraveNewTabMessageHandler::OnPreferencesChanged,
base::Unretained(this)));
#if BUILDFLAG(CRYPTO_DOT_COM_ENABLED)
pref_change_registrar_.Add(
kCryptoDotComNewTabPageShowCryptoDotCom,
Expand Down Expand Up @@ -501,6 +508,8 @@ void BraveNewTabMessageHandler::HandleSaveNewTabPagePref(
settingsKey = kNewTabPageShowRewards;
} else if (settingsKeyInput == "isBrandedWallpaperNotificationDismissed") {
settingsKey = kBrandedWallpaperNotificationDismissed;
} else if (settingsKeyInput == "hideAllWidgets") {
settingsKey = kNewTabPageHideAllWidgets;
} else if (settingsKeyInput == "showBinance") {
settingsKey = kNewTabPageShowBinance;
} else if (settingsKeyInput == "showTogether") {
Expand Down
3 changes: 3 additions & 0 deletions common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ const char kNewTabPageShowRewards[] = "brave.new_tab_page.show_rewards";
const char kNewTabPageShowBinance[] = "brave.new_tab_page.show_binance";
const char kNewTabPageShowGemini[] = "brave.new_tab_page.show_gemini";
const char kNewTabPageShowTogether[] = "brave.new_tab_page.show_together";
const char kNewTabPageHideAllWidgets[] = "brave.new_tab_page.hide_all_widgets";
const char kNewTabPageMigratedHideAllWidgetsPref[] =
"brave.new_tab_page.migrated_hide_all_widgets_pref";
const char kNewTabPageShowsOptions[] = "brave.new_tab_page.shows_options";
const char kBraveTodaySources[] = "brave.today.sources";
const char kBraveTodayIntroDismissed[] = "brave.today.intro_dismissed";
Expand Down
2 changes: 2 additions & 0 deletions common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ extern const char kNewTabPageShowRewards[];
extern const char kNewTabPageShowBinance[];
extern const char kNewTabPageShowGemini[];
extern const char kNewTabPageShowTogether[];
extern const char kNewTabPageHideAllWidgets[];
extern const char kNewTabPageMigratedHideAllWidgetsPref[];
extern const char kNewTabPageShowsOptions[];
extern const char kBraveTodaySources[];
extern const char kBraveTodayIntroDismissed[];
Expand Down
8 changes: 2 additions & 6 deletions components/brave_new_tab_ui/api/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,8 @@ export function saveIsBraveTodayOptedIn (value: boolean): void {
sendSavePref('isBraveTodayOptedIn', value)
}

export function saveSetAllStackWidgets (value: boolean): void {
sendSavePref('showRewards', value)
sendSavePref('showTogether', value)
sendSavePref('showBinance', value)
sendSavePref('showGemini', value)
sendSavePref('showCryptoDotCom', value)
export function saveSetAllStackWidgets (visible: boolean): void {
sendSavePref('hideAllWidgets', !visible)
}

export function addChangeListener (listener: PreferencesUpdatedHandler): void {
Expand Down
44 changes: 7 additions & 37 deletions components/brave_new_tab_ui/containers/newTab/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,9 @@ class NewTabPage extends React.Component<Props, State> {
}

getCryptoContent () {
if (this.props.newTabData.hideAllWidgets) {
return null
}
const {
widgetStackOrder,
togetherSupported,
Expand Down Expand Up @@ -770,9 +773,10 @@ class NewTabPage extends React.Component<Props, State> {
cryptoDotComSupported,
showFTX,
ftxSupported,
binanceSupported
binanceSupported,
hideAllWidgets
} = this.props.newTabData
return [
return hideAllWidgets || [
showRewards,
togetherSupported && showTogether,
binanceSupported && showBinance,
Expand All @@ -782,40 +786,6 @@ class NewTabPage extends React.Component<Props, State> {
].every((widget: boolean) => !widget)
}

toggleAllCards = (show: boolean) => {
if (!show) {
this.props.actions.saveWidgetStackOrder()
this.props.saveSetAllStackWidgets(false)
return
}

const saveShowProps = {
'binance': this.props.saveShowBinance,
'cryptoDotCom': this.props.saveShowCryptoDotCom,
'gemini': this.props.saveShowGemini,
'rewards': this.props.saveShowRewards,
'together': this.props.saveShowTogether,
'ftx': this.props.saveShowFTX
}

const setAllTrue = (list: NewTab.StackWidget[]) => {
list.forEach((widget: NewTab.StackWidget) => {
if (widget in saveShowProps) {
saveShowProps[widget](true)
}
})
}

const { savedWidgetStackOrder, widgetStackOrder } = this.props.newTabData
// When turning back on, all widgets should be set to shown
// in the case that all widgets were hidden previously.
setAllTrue(
!savedWidgetStackOrder.length ?
widgetStackOrder :
savedWidgetStackOrder
)
}

renderCryptoContent () {
const { newTabData } = this.props
const { widgetStackOrder } = newTabData
Expand Down Expand Up @@ -1258,7 +1228,7 @@ class NewTabPage extends React.Component<Props, State> {
showFTX={newTabData.showFTX}
todayPublishers={this.props.todayData.publishers}
cardsHidden={this.allWidgetsHidden()}
toggleCards={this.toggleAllCards}
toggleCards={this.props.saveSetAllStackWidgets}
/>
{
showEditTopSite ?
Expand Down
9 changes: 0 additions & 9 deletions components/brave_new_tab_ui/reducers/stack_widget_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,6 @@ const stackWidgetReducer: Reducer<NewTab.State | undefined> = (state: NewTab.Sta
case types.SET_FOREGROUND_STACK_WIDGET:
state = setForegroundStackWidget(payload.widget as NewTab.StackWidget, state)
break
case types.SAVE_WIDGET_STACK_ORDER:
const savedWidgets = state.widgetStackOrder.filter((widget: NewTab.StackWidget) => {
return state[widgets[widget]]
})
state = {
...state,
savedWidgetStackOrder: savedWidgets
}
break

default:
break
Expand Down
5 changes: 2 additions & 3 deletions components/brave_new_tab_ui/storage/new_tab_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const defaultState: NewTab.State = {
showBitcoinDotCom: false,
showCryptoDotCom: false,
showFTX: false,
hideAllWidgets: false,
brandedWallpaperOptIn: false,
isBrandedWallpaperNotificationDismissed: true,
isBraveTodayOptedIn: false,
Expand Down Expand Up @@ -75,7 +76,6 @@ export const defaultState: NewTab.State = {
currentStackWidget: '',
removedStackWidgets: [],
// Order is ascending, with last entry being in the foreground
savedWidgetStackOrder: [],
widgetStackOrder: ['ftx', 'cryptoDotCom', 'binance', 'gemini', 'rewards'],
binanceState: {
userTLD: 'com',
Expand Down Expand Up @@ -291,8 +291,7 @@ export const debouncedSave = debounce<NewTab.State>((data: NewTab.State) => {
cryptoDotComState: data.cryptoDotComState,
ftxState: data.ftxState,
removedStackWidgets: data.removedStackWidgets,
widgetStackOrder: data.widgetStackOrder,
savedWidgetStackOrder: data.savedWidgetStackOrder
widgetStackOrder: data.widgetStackOrder
}
window.localStorage.setItem(keyName, JSON.stringify(dataToSave))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const getNewTabData = (state: NewTab.State = defaultState): NewTab.State
ftxSupported: boolean('FTX supported?', true),
showFTX: boolean('Show FTX?', true),
showBinance: boolean('Show Binance?', true),
hideAllWidgets: boolean('Hide all widgets?', false),
isBraveTodayOptedIn: boolean('Brave Today opted-in?', false),
textDirection: select('Text direction', { ltr: 'ltr', rtl: 'rtl' } , 'ltr'),
stats: {
Expand Down
Loading

0 comments on commit 56b2011

Please sign in to comment.