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

NTP / FTX fix group #9074

Merged
merged 5 commits into from
Jun 11, 2021
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
2 changes: 2 additions & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(kNewTabPageShowBinance, true);
registry->RegisterBooleanPref(kNewTabPageShowTogether, false);
registry->RegisterBooleanPref(kNewTabPageShowGemini, true);
registry->RegisterBooleanPref(kNewTabPageHideAllWidgets, false);

registry->RegisterIntegerPref(
kNewTabPageShowsOptions,
static_cast<int>(NewTabPageShowsOptions::kDashboard));
Expand Down
30 changes: 30 additions & 0 deletions browser/search/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import("//brave/components/crypto_dot_com/browser/buildflags/buildflags.gni")

source_set("search") {
sources = []

Expand All @@ -10,11 +12,39 @@ 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",
]
}
}

source_set("unit_tests") {
testonly = true

if (!is_android) {
sources = [
"ntp_utils_unittest.cc",
]
deps = [
"//brave/browser/search",
"//brave/common:pref_names",
"//brave/components/crypto_dot_com/browser/buildflags:buildflags",
"//chrome/test:test_support",
"//components/prefs",
"//components/sync_preferences",
"//content/test:test_support",
"//testing/gtest",
]
if (crypto_dot_com_enabled) {
deps += [
"//brave/components/crypto_dot_com/common",
]
}
}
}
72 changes: 67 additions & 5 deletions browser/search/ntp_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,94 @@

#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 kWidgetPrefNames[] = {
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.
const PrefService::Preference* hide_widgets_pref =
prefs->FindPreference(kNewTabPageHideAllWidgets);
if (!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 : kWidgetPrefNames) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we can use std::all_of here

const bool all_were_off = std::all_of(std::begin(kWidgetPrefNames), std::end(kWidgetPrefNames), [prefs](const char* const pref_names) {
  ...
});

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great tip, thanks!

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) {
// 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);
}
// Record whether this has been migrated by setting an explicit
// value for the HideAllWidgets pref.
prefs->SetBoolean(kNewTabPageHideAllWidgets, all_were_off);
VLOG(1) << "Done migrating hide widget pref: " << all_were_off;
}

// 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));
}
1 change: 1 addition & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "ftxConversionAmountAvailable", IDS_FTX_CONVERSION_AMOUNT_AVAILABLE },
{ "ftxSummaryBlurLabel", IDS_FTX_SUMMARY_BLUR_LABEL },
{ "ftxSummaryRevealLabel", IDS_FTX_SUMMARY_REVEAL_LABEL },
{ "ftxSummaryNoBalance", IDS_FTX_SUMMARY_NO_BALANCE},
#endif
}
}, {
Expand Down
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
1 change: 1 addition & 0 deletions common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ 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 kNewTabPageShowsOptions[] = "brave.new_tab_page.shows_options";
const char kBraveTodaySources[] = "brave.today.sources";
const char kBraveTodayIntroDismissed[] = "brave.today.intro_dismissed";
Expand Down
1 change: 1 addition & 0 deletions common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ extern const char kNewTabPageShowRewards[];
extern const char kNewTabPageShowBinance[];
extern const char kNewTabPageShowGemini[];
extern const char kNewTabPageShowTogether[];
extern const char kNewTabPageHideAllWidgets[];
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
11 changes: 0 additions & 11 deletions components/brave_new_tab_ui/components/default/page/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,6 @@ export const FooterContent = styled('div')`
}
`

export const VisitableBackground = styled('a')`
z-index: -1;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: block;
cursor: pointer;
`

function getPageBackground (p: HasImageProps) {
// Page background is duplicated since a backdrop-filter's
// ancestor which has blur must also have background.
Expand Down
Loading