Skip to content

Commit

Permalink
[NTP Next] Add updated NTP with background support
Browse files Browse the repository at this point in the history
  • Loading branch information
zenparsing committed Dec 2, 2024
1 parent 89750d5 commit fc65e01
Show file tree
Hide file tree
Showing 75 changed files with 3,316 additions and 56 deletions.
8 changes: 8 additions & 0 deletions browser/about_flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "brave/components/brave_ads/core/public/ad_units/notification_ad/notification_ad_feature.h"
#include "brave/components/brave_ads/core/public/ads_feature.h"
#include "brave/components/brave_component_updater/browser/features.h"
#include "brave/components/brave_new_tab/new_tab_features.h"
#include "brave/components/brave_news/common/features.h"
#include "brave/components/brave_rewards/common/buildflags/buildflags.h"
#include "brave/components/brave_rewards/common/features.h"
Expand Down Expand Up @@ -475,6 +476,13 @@
kOsDesktop, \
FEATURE_VALUE_TYPE(features::kBraveNtpSearchWidget), \
}, \
{ \
"brave-use-updated-ntp", \
"Use the updated New Tab Page", \
"Uses an updated version of the New Tab Page", \
kOsDesktop, \
FEATURE_VALUE_TYPE(brave_new_tab::features::kUseUpdatedNTP), \
}, \
{ \
"brave-adblock-cname-uncloaking", \
"Enable CNAME uncloaking", \
Expand Down
4 changes: 4 additions & 0 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ using extensions::ChromeContentBrowserClientExtensionsPart;
#if !BUILDFLAG(IS_ANDROID)
#include "brave/browser/new_tab/new_tab_shows_navigation_throttle.h"
#include "brave/browser/ui/geolocation/brave_geolocation_permission_tab_helper.h"
#include "brave/browser/ui/webui/brave_new_tab/new_tab_page_ui.h"
#include "brave/browser/ui/webui/brave_news_internals/brave_news_internals_ui.h"
#include "brave/browser/ui/webui/brave_rewards/rewards_page_top_ui.h"
#include "brave/browser/ui/webui/brave_rewards/rewards_panel_ui.h"
Expand Down Expand Up @@ -840,6 +841,9 @@ void BraveContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
content::RegisterWebUIControllerInterfaceBinder<
commands::mojom::CommandsService, BraveSettingsUI>(map);
}
content::RegisterWebUIControllerInterfaceBinder<
brave_new_tab::mojom::NewTabPageHandler, brave_new_tab::NewTabPageUI>(
map);
#endif

auto* prefs =
Expand Down
6 changes: 2 additions & 4 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "brave/browser/brave_shields/brave_shields_web_contents_observer.h"
#include "brave/browser/ethereum_remote_client/buildflags/buildflags.h"
#include "brave/browser/new_tab/new_tab_shows_options.h"
#include "brave/browser/themes/brave_dark_mode_utils.h"
#include "brave/browser/translate/brave_translate_prefs_migration.h"
#include "brave/browser/ui/bookmark/brave_bookmark_prefs.h"
Expand All @@ -21,6 +20,7 @@
#include "brave/components/brave_ads/browser/analytics/p2a/p2a.h"
#include "brave/components/brave_ads/core/public/prefs/obsolete_pref_util.h"
#include "brave/components/brave_ads/core/public/prefs/pref_registry.h"
#include "brave/components/brave_new_tab/new_tab_prefs.h"
#include "brave/components/brave_news/browser/brave_news_controller.h"
#include "brave/components/brave_news/browser/brave_news_p3a.h"
#include "brave/components/brave_news/browser/brave_news_pref_manager.h"
Expand Down Expand Up @@ -371,9 +371,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
brave_private_new_tab::prefs::RegisterProfilePrefs(registry);
#endif

registry->RegisterIntegerPref(
kNewTabPageShowsOptions,
static_cast<int>(NewTabPageShowsOptions::kDashboard));
brave_new_tab::prefs::RegisterProfilePrefs(registry);

#if BUILDFLAG(ENABLE_CUSTOM_BACKGROUND)
NTPBackgroundPrefs::RegisterPref(registry);
Expand Down
1 change: 1 addition & 0 deletions browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ source_set("extensions") {
"//brave/components/brave_ads/browser",
"//brave/components/brave_ads/core/public:headers",
"//brave/components/brave_extension:static_resources",
"//brave/components/brave_new_tab",
"//brave/components/brave_news/common",
"//brave/components/brave_rewards/browser",
"//brave/components/brave_rewards/common",
Expand Down
3 changes: 2 additions & 1 deletion browser/extensions/api/settings_private/brave_prefs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "brave/browser/ui/tabs/brave_tab_prefs.h"
#include "brave/components/ai_chat/core/common/pref_names.h"
#include "brave/components/brave_new_tab/new_tab_prefs.h"
#include "brave/components/brave_news/common/pref_names.h"
#include "brave/components/brave_rewards/common/pref_names.h"
#include "brave/components/brave_shields/core/common/pref_names.h"
Expand Down Expand Up @@ -182,7 +183,7 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetAllowlistedKeys() {
settings_api::PrefType::kBoolean;
(*s_brave_allowlist)[kNewTabPageShowBraveVPN] =
settings_api::PrefType::kBoolean;
(*s_brave_allowlist)[kNewTabPageShowsOptions] =
(*s_brave_allowlist)[brave_new_tab::prefs::kNewTabShowsOption] =
settings_api::PrefType::kNumber;
#if BUILDFLAG(ENABLE_EXTENSIONS)
// Web discovery prefs
Expand Down
1 change: 1 addition & 0 deletions browser/new_tab/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ source_set("unittest") {
deps = [
"//brave/browser",
"//brave/common",
"//brave/components/brave_new_tab",
"//brave/components/constants",
"//chrome/common",
"//chrome/test:test_support",
Expand Down
38 changes: 17 additions & 21 deletions browser/new_tab/new_tab_shows_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <utility>

#include "brave/components/brave_new_tab/new_tab_prefs.h"
#include "brave/components/constants/pref_names.h"
#include "brave/components/constants/webui_url_constants.h"
#include "brave/grit/brave_generated_resources.h"
Expand All @@ -29,44 +30,40 @@ GURL GetNewTabPageURL(Profile* profile) {

auto* prefs = profile->GetPrefs();

NewTabPageShowsOptions option = static_cast<NewTabPageShowsOptions>(
prefs->GetInteger(kNewTabPageShowsOptions));
if (option == NewTabPageShowsOptions::kHomepage) {
if (prefs->GetBoolean(prefs::kHomePageIsNewTabPage)) {
switch (brave_new_tab::prefs::GetNewTabShowsOption(prefs)) {
case brave_new_tab::prefs::NewTabShowsOption::kHomepage:
return prefs->GetBoolean(prefs::kHomePageIsNewTabPage)
? GURL()
: GURL(prefs->GetString(prefs::kHomePage));
case brave_new_tab::prefs::NewTabShowsOption::kBlankpage:
case brave_new_tab::prefs::NewTabShowsOption::kDashboard:
return GURL();
}
return GURL(prefs->GetString(prefs::kHomePage));
} else if (option == NewTabPageShowsOptions::kBlankpage) {
// NewTab route will handle for blank page.
return GURL();
} else {
DCHECK_EQ(NewTabPageShowsOptions::kDashboard, option);
return GURL();
}
}

base::Value::List GetNewTabShowsOptionsList(Profile* profile) {
using brave_new_tab::prefs::NewTabShowsOption;

base::Value::List list;

base::Value::Dict dashboard_option;
dashboard_option.Set("value",
static_cast<int>(NewTabPageShowsOptions::kDashboard));
static_cast<int>(NewTabShowsOption::kDashboard));
dashboard_option.Set("name",
l10n_util::GetStringUTF8(
IDS_SETTINGS_NEW_TAB_NEW_TAB_PAGE_SHOWS_DASHBOARD));
list.Append(std::move(dashboard_option));

base::Value::Dict homepage_option;
homepage_option.Set("value",
static_cast<int>(NewTabPageShowsOptions::kHomepage));
homepage_option.Set("value", static_cast<int>(NewTabShowsOption::kHomepage));
homepage_option.Set("name",
l10n_util::GetStringUTF8(
IDS_SETTINGS_NEW_TAB_NEW_TAB_PAGE_SHOWS_HOMEPAGE));
list.Append(std::move(homepage_option));

base::Value::Dict blankpage_option;
blankpage_option.Set("value",
static_cast<int>(NewTabPageShowsOptions::kBlankpage));
static_cast<int>(NewTabShowsOption::kBlankpage));
blankpage_option.Set("name",
l10n_util::GetStringUTF8(
IDS_SETTINGS_NEW_TAB_NEW_TAB_PAGE_SHOWS_BLANKPAGE));
Expand All @@ -81,9 +78,8 @@ bool ShouldUseNewTabURLForNewTab(Profile* profile) {
}

bool ShouldNewTabShowDashboard(Profile* profile) {
auto* prefs = profile->GetPrefs();
if (static_cast<NewTabPageShowsOptions>(prefs->GetInteger(
kNewTabPageShowsOptions)) == NewTabPageShowsOptions::kBlankpage) {
auto option = brave_new_tab::prefs::GetNewTabShowsOption(profile->GetPrefs());
if (option == brave_new_tab::prefs::NewTabShowsOption::kBlankpage) {
return false;
}

Expand All @@ -95,8 +91,8 @@ bool ShouldNewTabShowBlankpage(Profile* profile) {
return false;
}

return profile->GetPrefs()->GetInteger(kNewTabPageShowsOptions) ==
static_cast<int>(brave::NewTabPageShowsOptions::kBlankpage);
auto option = brave_new_tab::prefs::GetNewTabShowsOption(profile->GetPrefs());
return option == brave_new_tab::prefs::NewTabShowsOption::kBlankpage;
}

} // namespace brave
6 changes: 0 additions & 6 deletions browser/new_tab/new_tab_shows_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ class Profile;

namespace brave {

enum class NewTabPageShowsOptions {
kDashboard,
kHomepage,
kBlankpage
};

GURL GetNewTabPageURL(Profile* profile);
base::Value::List GetNewTabShowsOptionsList(Profile* profile);
bool ShouldUseNewTabURLForNewTab(Profile* profile);
Expand Down
19 changes: 10 additions & 9 deletions browser/new_tab/new_tab_shows_options_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/new_tab/new_tab_shows_options.h"

#include "brave/components/brave_new_tab/new_tab_prefs.h"
#include "brave/components/constants/pref_names.h"
#include "brave/components/constants/webui_url_constants.h"
#include "chrome/common/pref_names.h"
Expand All @@ -15,6 +17,8 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "url/url_constants.h"

using brave_new_tab::prefs::NewTabShowsOption;

class BraveNewTabTest : public testing::Test {
public:
BraveNewTabTest() : manager_(TestingBrowserProcess::GetGlobal()) {}
Expand Down Expand Up @@ -42,19 +46,17 @@ TEST_F(BraveNewTabTest, BasicTest) {
auto* prefs = profile->GetPrefs();

// Check NTP url is empty for DASHBOARD.
prefs->SetInteger(
kNewTabPageShowsOptions,
static_cast<int>(brave::NewTabPageShowsOptions::kDashboard));
prefs->SetInteger(brave_new_tab::prefs::kNewTabShowsOption,
static_cast<int>(NewTabShowsOption::kDashboard));
EXPECT_EQ(GURL(), brave::GetNewTabPageURL(profile));
EXPECT_EQ(GURL(), brave::GetNewTabPageURL(otr_profile));
EXPECT_TRUE(brave::ShouldUseNewTabURLForNewTab(profile));
EXPECT_TRUE(brave::ShouldNewTabShowDashboard(profile));

// Check NTP url is empty when option is HOMEPAGE and kHomePageIsNewTabPage
// is true.
prefs->SetInteger(
kNewTabPageShowsOptions,
static_cast<int>(brave::NewTabPageShowsOptions::kHomepage));
prefs->SetInteger(brave_new_tab::prefs::kNewTabShowsOption,
static_cast<int>(NewTabShowsOption::kHomepage));
prefs->SetString(prefs::kHomePage, "https://www.brave.com/");
prefs->SetBoolean(prefs::kHomePageIsNewTabPage, true);
EXPECT_EQ(GURL(), brave::GetNewTabPageURL(profile));
Expand All @@ -77,9 +79,8 @@ TEST_F(BraveNewTabTest, BasicTest) {

// Check NTP url is used when option is BLANKPAGE.
// Blank page will go NTP route and BraveNewTabUI will handle it.
prefs->SetInteger(
kNewTabPageShowsOptions,
static_cast<int>(brave::NewTabPageShowsOptions::kBlankpage));
prefs->SetInteger(brave_new_tab::prefs::kNewTabShowsOption,
static_cast<int>(NewTabShowsOption::kBlankpage));
EXPECT_EQ(GURL(), brave::GetNewTabPageURL(profile));
EXPECT_EQ(GURL(), brave::GetNewTabPageURL(otr_profile));
EXPECT_TRUE(brave::ShouldUseNewTabURLForNewTab(profile));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {sendWithPromise} from 'chrome://resources/js/cr.js';

export type NewTabOption = {
name: string
value: number // corresponds to NewTabPageShowsOptions enum
value: number // corresponds to NewTabShowsOption enum
}

export interface BraveNewTabBrowserProxy {
Expand Down
2 changes: 2 additions & 0 deletions browser/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ brave_chrome_browser_deps = [
"//brave/components/brave_ads/core",
"//brave/components/brave_component_updater/browser",
"//brave/components/brave_federated",
"//brave/components/brave_new_tab",
"//brave/components/brave_new_tab_ui:mojom",
"//brave/components/brave_news/common:mojom",
"//brave/components/brave_perf_predictor/browser",
Expand Down Expand Up @@ -289,6 +290,7 @@ if (!is_android) {

brave_chrome_browser_deps += [
"//brave/browser/ui/ai_chat",
"//brave/browser/ui/webui/brave_new_tab",
"//brave/browser/ui/webui/brave_news_internals",
"//components/feed:feature_list",
"//components/feed/core/v2:feed_core_v2",
Expand Down
1 change: 1 addition & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ source_set("ui") {
"//brave/components/brave_ads/core",
"//brave/components/brave_federated",
"//brave/components/brave_federated/public/interfaces",
"//brave/components/brave_new_tab",
"//brave/components/brave_news/browser",
"//brave/components/brave_news/common",
"//brave/components/brave_perf_predictor/common",
Expand Down
6 changes: 4 additions & 2 deletions browser/ui/config.gni
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
brave_ui_allow_circular_includes_from =
[ "//brave/browser/ui/webui/ads_internals" ]
if (!is_android) {
brave_ui_allow_circular_includes_from +=
[ "//brave/browser/ui/webui/brave_news_internals" ]
brave_ui_allow_circular_includes_from += [
"//brave/browser/ui/webui/brave_new_tab",
"//brave/browser/ui/webui/brave_news_internals",
]
}
45 changes: 45 additions & 0 deletions browser/ui/webui/brave_new_tab/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/.

import("//brave/components/ntp_background_images/buildflags/buildflags.gni")

assert(!is_android)
assert(enable_custom_background)

source_set("brave_new_tab") {
sources = [
"custom_image_chooser.cc",
"custom_image_chooser.h",
"new_tab_page_handler.cc",
"new_tab_page_handler.h",
"new_tab_page_ui.cc",
"new_tab_page_ui.h",
"update_observer.cc",
"update_observer.h",
]

deps = [
"//brave/browser:browser_process",
"//brave/browser/ntp_background",
"//brave/components/brave_new_tab",
"//brave/components/brave_new_tab:mojom",
"//brave/components/brave_new_tab/resources:generated_resources",
"//brave/components/l10n/common",
"//brave/components/ntp_background_images/browser",
"//brave/components/ntp_background_images/common",
"//brave/components/resources:static_resources",
"//brave/components/resources:strings",
"//chrome/app:generated_resources",
"//chrome/browser:browser_public_dependencies",
"//chrome/browser/profiles:profile",
"//chrome/browser/themes",
"//chrome/browser/ui/webui:webui_util",
"//components/prefs",
"//components/strings:components_strings",
"//ui/base",
"//ui/shell_dialogs",
"//ui/webui",
]
}
Loading

0 comments on commit fc65e01

Please sign in to comment.