[NTP Next] Create WebUI scaffolding (#27780)

This commit is contained in:
Kevin Smith
2025-03-06 00:54:07 -05:00
committed by GitHub
parent c8b3ad9eff
commit b76149adcf
26 changed files with 365 additions and 1 deletions
+5 -1
View File
@@ -22,7 +22,11 @@ const slashStoriesIndexer: Indexer = {
const config: StorybookConfig = {
stories: process.env.STORYBOOK_STORYPATH
? [`../${process.env.STORYBOOK_STORYPATH}`]
: ['../components/**/stories/*.tsx', '../components/**/*.stories.tsx'],
: [
'../components/**/stories/*.tsx',
'../components/**/*.stories.tsx',
'../browser/resources/**/stories/*.tsx'
],
typescript: {
check: false,
reactDocgen: false,
+7
View File
@@ -535,6 +535,13 @@ const flags_ui::FeatureEntry::FeatureVariation kZCashFeatureVariations[] = {
kOsDesktop, \
FEATURE_VALUE_TYPE(features::kBraveNtpSearchWidget), \
}, \
{ \
"brave-ntp-refresh-enabled", \
"New Tab Page refresh", \
"Enables the refreshed version of the New Tab Page", \
kOsDesktop, \
FEATURE_VALUE_TYPE(features::kBraveNewTabPageRefreshEnabled), \
}, \
{ \
"brave-adblock-cname-uncloaking", \
"Enable CNAME uncloaking", \
+4
View File
@@ -9,6 +9,10 @@
namespace features {
BASE_FEATURE(kBraveNewTabPageRefreshEnabled,
"BraveNewTabPageRefreshEnabled",
base::FEATURE_DISABLED_BY_DEFAULT);
// Cleanup Session Cookies on browser restart if Session Restore is enabled.
BASE_FEATURE(kBraveCleanupSessionCookiesOnSessionRestore,
"BraveCleanupSessionCookiesOnSessionRestore",
+1
View File
@@ -13,6 +13,7 @@
namespace features {
BASE_DECLARE_FEATURE(kBraveNewTabPageRefreshEnabled);
BASE_DECLARE_FEATURE(kBraveCleanupSessionCookiesOnSessionRestore);
BASE_DECLARE_FEATURE(kBraveCopyCleanLinkByDefault);
BASE_DECLARE_FEATURE(kBraveCopyCleanLinkFromJs);
+5
View File
@@ -234,6 +234,8 @@ 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_page_refresh/brave_new_tab_page.mojom.h"
#include "brave/browser/ui/webui/brave_new_tab_page_refresh/brave_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"
@@ -661,6 +663,9 @@ void BraveContentBrowserClient::RegisterWebUIInterfaceBrokers(
.Add<brave_rewards::mojom::RewardsPageHandler>();
#if !BUILDFLAG(IS_ANDROID)
registry.ForWebUI<BraveNewTabPageUI>()
.Add<brave_new_tab_page_refresh::mojom::NewTabPageHandler>();
auto ntp_registration =
registry.ForWebUI<BraveNewTabUI>()
.Add<brave_new_tab_page::mojom::PageHandlerFactory>()
@@ -0,0 +1,25 @@
# Copyright (c) 2025 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/common/typescript.gni")
assert(!is_android)
transpile_web_ui("resources") {
entry_points = [ [
"brave_new_tab_page",
rebase_path("brave_new_tab_page.tsx"),
] ]
resource_name = "brave_new_tab_page_refresh"
output_module = true
deps = [ "//brave/browser/ui/webui/brave_new_tab_page_refresh:mojom_js" ]
}
pack_web_resources("generated_resources") {
resource_name = "brave_new_tab_page_refresh"
output_dir =
"$root_gen_dir/brave/browser/resources/brave_new_tab_page_refresh"
deps = [ ":resources" ]
}
@@ -0,0 +1,22 @@
<!doctype html>
<html dir="$i18n{textdirection}" lang="$i18n{language}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>New Tab</title>
<link rel="stylesheet" href="chrome://resources/brave/css/reset.css" blocking="render" >
<link rel="stylesheet" href="chrome://resources/brave/fonts/poppins.css" blocking="render" >
<link rel="stylesheet" href="chrome://resources/brave/fonts/inter.css" blocking="render" >
<link rel="stylesheet" href="chrome://resources/brave/leo/css/variables.css" blocking="render" >
<script type="module" src="/strings.m.js"></script>
<script type="module" src="/brave_new_tab_page.bundle.js"></script>
<style>
body {
background: $i18n{backgroundColor};
}
</style>
</head>
<body>
<div id="root"></div>
</body>
</html>
@@ -0,0 +1,16 @@
/* Copyright (c) 2025 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 * as React from 'react'
import { createRoot } from 'react-dom/client'
import { setIconBasePath } from '@brave/leo/react/icon'
import { App } from './components/app'
setIconBasePath('chrome://resources/brave-icons')
createRoot(document.getElementById('root')!).render(
<App />
)
@@ -0,0 +1,12 @@
/* Copyright (c) 2025 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 * as React from 'react'
export function App() {
return (
<div></div>
)
}
@@ -0,0 +1,20 @@
/* Copyright (c) 2025 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 * as React from 'react'
import { App } from '../components/app'
export default {
title: 'New Tab/Refresh'
}
export function NTPRefresh() {
return (
<div style={{ position: 'absolute', inset: 0 }}>
<App />
</div>
)
}
@@ -0,0 +1,8 @@
{
"extends": "../../../tsconfig",
"include": [
"**/*.ts",
"**/*.tsx",
"**/*.d.ts"
]
}
+10
View File
@@ -292,6 +292,8 @@ if (!is_android) {
brave_chrome_browser_deps += [
"//brave/browser/ui/ai_chat",
"//brave/browser/ui/tabs:impl",
"//brave/browser/ui/webui/brave_new_tab_page_refresh",
"//brave/browser/ui/webui/brave_new_tab_page_refresh:mojom",
"//brave/browser/ui/webui/brave_news_internals",
"//brave/browser/user_education:features",
"//components/feed:feature_list",
@@ -634,3 +636,11 @@ if (is_android) {
"//brave/browser/android:tab_features",
]
}
# TODO(https://github.com/brave/brave-browser/issues/43310):
# brave_new_tab_page_ui.cc includes some headers (e.g. from
# //brave/browser/new_tab) that are currently in the //chrome/browser target.
if (!is_android) {
brave_chrome_browser_allow_circular_includes_from +=
[ "//brave/browser/ui/webui/brave_new_tab_page_refresh" ]
}
+1
View File
@@ -10,6 +10,7 @@ brave_ui_allow_circular_includes_from =
if (!is_android) {
brave_ui_allow_circular_includes_from += [
"//brave/browser/ui/webui/brave_education",
"//brave/browser/ui/webui/brave_new_tab_page_refresh",
"//brave/browser/ui/webui/brave_news_internals",
]
}
@@ -0,0 +1,37 @@
# Copyright (c) 2025 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("//mojo/public/tools/bindings/mojom.gni")
assert(!is_android)
mojom("mojom") {
sources = [ "brave_new_tab_page.mojom" ]
}
source_set("brave_new_tab_page_refresh") {
sources = [
"brave_new_tab_page_refresh_handler.cc",
"brave_new_tab_page_refresh_handler.h",
"brave_new_tab_page_ui.cc",
"brave_new_tab_page_ui.h",
]
deps = [
":mojom",
"//brave/browser/resources/brave_new_tab_page_refresh:generated_resources",
"//brave/components/l10n/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/strings:components_strings",
"//ui/base",
"//ui/webui",
]
}
@@ -0,0 +1,18 @@
// Copyright (c) 2025 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/.
module brave_new_tab_page_refresh.mojom;
// WebUI-side handler for notifications from the browser.
interface NewTabPage {};
// Browser-side handler for requests from the WebUI page.
interface NewTabPageHandler {
// Sets the NewTabPage remote interface that will receive notifications from
// the browser.
SetNewTabPage(pending_remote<NewTabPage> page);
};
@@ -0,0 +1,21 @@
// Copyright (c) 2025 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/.
#include "brave/browser/ui/webui/brave_new_tab_page_refresh/brave_new_tab_page_refresh_handler.h"
#include <utility>
BraveNewTabPageRefreshHandler::BraveNewTabPageRefreshHandler(
mojo::PendingReceiver<brave_new_tab_page_refresh::mojom::NewTabPageHandler>
receiver)
: receiver_(this, std::move(receiver)) {}
BraveNewTabPageRefreshHandler::~BraveNewTabPageRefreshHandler() = default;
void BraveNewTabPageRefreshHandler::SetNewTabPage(
mojo::PendingRemote<brave_new_tab_page_refresh::mojom::NewTabPage> page) {
page_.reset();
page_.Bind(std::move(page));
}
@@ -0,0 +1,35 @@
// Copyright (c) 2025 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/.
#ifndef BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_PAGE_REFRESH_BRAVE_NEW_TAB_PAGE_REFRESH_HANDLER_H_
#define BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_PAGE_REFRESH_BRAVE_NEW_TAB_PAGE_REFRESH_HANDLER_H_
#include "brave/browser/ui/webui/brave_new_tab_page_refresh/brave_new_tab_page.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
class BraveNewTabPageRefreshHandler
: public brave_new_tab_page_refresh::mojom::NewTabPageHandler {
public:
explicit BraveNewTabPageRefreshHandler(
mojo::PendingReceiver<
brave_new_tab_page_refresh::mojom::NewTabPageHandler> receiver);
~BraveNewTabPageRefreshHandler() override;
// brave_new_tab_page_refresh::mojom::NewTabPageHandler:
void SetNewTabPage(
mojo::PendingRemote<brave_new_tab_page_refresh::mojom::NewTabPage> page)
override;
private:
mojo::Receiver<brave_new_tab_page_refresh::mojom::NewTabPageHandler>
receiver_;
mojo::Remote<brave_new_tab_page_refresh::mojom::NewTabPage> page_;
};
#endif // BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_PAGE_REFRESH_BRAVE_NEW_TAB_PAGE_REFRESH_HANDLER_H_
@@ -0,0 +1,62 @@
// Copyright (c) 2025 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/.
#include "brave/browser/ui/webui/brave_new_tab_page_refresh/brave_new_tab_page_ui.h"
#include <utility>
#include "brave/browser/new_tab/new_tab_shows_options.h"
#include "brave/browser/resources/brave_new_tab_page_refresh/grit/brave_new_tab_page_refresh_generated_map.h"
#include "brave/browser/ui/webui/brave_new_tab_page_refresh/brave_new_tab_page_refresh_handler.h"
#include "brave/browser/ui/webui/brave_webui_source.h"
#include "brave/components/l10n/common/localization_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/favicon_source.h"
#include "chrome/common/webui_url_constants.h"
#include "components/favicon_base/favicon_url_parser.h"
#include "components/grit/brave_components_resources.h"
#include "components/grit/brave_components_strings.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
#include "ui/webui/webui_util.h"
BraveNewTabPageUI::BraveNewTabPageUI(content::WebUI* web_ui)
: ui::MojoWebUIController(web_ui) {
auto* profile = Profile::FromWebUI(web_ui);
auto* source = content::WebUIDataSource::CreateAndAdd(
profile, chrome::kChromeUINewTabHost);
if (brave::ShouldNewTabShowBlankpage(profile)) {
source->SetDefaultResource(IDR_BRAVE_BLANK_NEW_TAB_HTML);
} else {
webui::SetupWebUIDataSource(source, kBraveNewTabPageRefreshGenerated,
IDR_BRAVE_NEW_TAB_PAGE_HTML);
}
source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::ImgSrc, "img-src chrome://favicon2;");
AddBackgroundColorToSource(source, web_ui->GetWebContents());
content::URLDataSource::Add(
profile, std::make_unique<FaviconSource>(
profile, chrome::FaviconUrlFormat::kFavicon2));
web_ui->OverrideTitle(
brave_l10n::GetLocalizedResourceUTF16String(IDS_NEW_TAB_TITLE));
}
BraveNewTabPageUI::~BraveNewTabPageUI() = default;
void BraveNewTabPageUI::BindInterface(
mojo::PendingReceiver<brave_new_tab_page_refresh::mojom::NewTabPageHandler>
receiver) {
page_handler_ =
std::make_unique<BraveNewTabPageRefreshHandler>(std::move(receiver));
}
WEB_UI_CONTROLLER_TYPE_IMPL(BraveNewTabPageUI)
@@ -0,0 +1,32 @@
// Copyright (c) 2025 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/.
#ifndef BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_PAGE_REFRESH_BRAVE_NEW_TAB_PAGE_UI_H_
#define BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_PAGE_REFRESH_BRAVE_NEW_TAB_PAGE_UI_H_
#include <memory>
#include "brave/browser/ui/webui/brave_new_tab_page_refresh/brave_new_tab_page.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "ui/webui/mojo_web_ui_controller.h"
// The Web UI controller for the Brave new tab page.
class BraveNewTabPageUI : public ui::MojoWebUIController {
public:
explicit BraveNewTabPageUI(content::WebUI* web_ui);
~BraveNewTabPageUI() override;
void BindInterface(
mojo::PendingReceiver<
brave_new_tab_page_refresh::mojom::NewTabPageHandler> receiver);
private:
std::unique_ptr<brave_new_tab_page_refresh::mojom::NewTabPageHandler>
page_handler_;
WEB_UI_CONTROLLER_TYPE_DECL();
};
#endif // BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_PAGE_REFRESH_BRAVE_NEW_TAB_PAGE_UI_H_
@@ -12,6 +12,7 @@
#include "base/memory/ptr_util.h"
#include "base/no_destructor.h"
#include "brave/browser/brave_ads/ads_service_factory.h"
#include "brave/browser/brave_browser_features.h"
#include "brave/browser/brave_browser_process.h"
#include "brave/browser/brave_news/brave_news_controller_factory.h"
#include "brave/browser/brave_rewards/rewards_util.h"
@@ -43,6 +44,7 @@
#if !BUILDFLAG(IS_ANDROID)
#include "brave/browser/brave_wallet/brave_wallet_context_utils.h"
#include "brave/browser/ui/webui/brave_new_tab_page_refresh/brave_new_tab_page_ui.h"
#include "brave/browser/ui/webui/brave_news_internals/brave_news_internals_ui.h"
#include "brave/browser/ui/webui/brave_wallet/wallet_page_ui.h"
#include "brave/browser/ui/webui/new_tab_page/brave_new_tab_ui.h"
@@ -153,6 +155,10 @@ WebUIController* NewWebUI(WebUI* web_ui, const GURL& url) {
// WebUIConfig. Currently, we can't add both BravePrivateNewTabUI and
// BraveNewTabUI configs in RegisterChromeWebUIConfigs because they use the
// same origin (content::kChromeUIScheme + chrome::kChromeUINewTabHost).
if (base::FeatureList::IsEnabled(
features::kBraveNewTabPageRefreshEnabled)) {
return new BraveNewTabPageUI(web_ui);
}
return new BraveNewTabUI(
web_ui, url.host(),
brave_ads::AdsServiceFactory::GetForProfile(profile),
+2
View File
@@ -88,6 +88,7 @@ repack("resources") {
if (!is_android && !is_ios) {
deps += [
"//brave/browser/resources/brave_new_tab_page_refresh:generated_resources",
"//brave/components/brave_new_tab_ui:generated_resources",
"//brave/components/brave_news/browser/resources:generated_resources",
"//brave/components/brave_news/browser/resources:generated_resources",
@@ -100,6 +101,7 @@ repack("resources") {
]
sources += [
"$root_gen_dir/brave/browser/resources/brave_new_tab_page_refresh/brave_new_tab_page_refresh_generated.pak",
"$root_gen_dir/brave/components/brave_new_tab/resources/brave_new_tab_generated.pak",
"$root_gen_dir/brave/components/brave_news/browser/resources/brave_news_internals_generated.pak",
"$root_gen_dir/brave/components/brave_private_new_tab/resources/page/brave_private_new_tab_generated.pak",
@@ -88,6 +88,7 @@
<part file="../skus/browser/resources/skus_internals_resources.grdp" />
<if expr="not is_android">
<part file="brave_blank_page_resources.grdp" />
<part file="brave_new_tab_page_resources.grdp" />
</if>
<part file="brave_translate_resources.grdp" />
<part file="speedreader_resources.grdp" />
@@ -805,6 +805,7 @@
<part file="ai_chat_prompts.grdp" />
<part file="ai_chat_ui_strings.grdp" />
<if expr="not is_android">
<part file="brave_new_tab_page_strings.grdp" />
<part file="brave_shields_strings.grdp" />
<part file="brave_welcome_strings.grdp" />
</if>
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<grit-part>
<if expr="not is_android">
<include name="IDR_BRAVE_NEW_TAB_PAGE_HTML" file="../../browser/resources/brave_new_tab_page_refresh/brave_new_tab_page.html" type="BINDATA" />
</if>
</grit-part>
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<grit-part>
<!-- Add strings for Brave new tab page here. -->
</grit-part>
+4
View File
@@ -245,6 +245,10 @@
"META": {"sizes": {"includes": [10]}},
"includes": [34780],
},
"<(SHARED_INTERMEDIATE_DIR)/brave/web-ui-brave_new_tab_page_refresh/brave_new_tab_page_refresh.grd": {
"META": {"sizes": {"includes": [20]}},
"includes": [34790],
},
# WARNING: The upstream ChromeOS/Ash strings currently run through 36930. We
# must be careful not to exceed that maximum when adding new strings here.
}