Skip to content

Commit

Permalink
Implement "Desktop Welcome Pages" feature
Browse files Browse the repository at this point in the history
  • Loading branch information
zenparsing committed Aug 7, 2024
1 parent 52426ef commit 56f5042
Show file tree
Hide file tree
Showing 42 changed files with 1,761 additions and 3 deletions.
15 changes: 15 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_education/common/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 @@ -433,6 +434,19 @@
FEATURE_VALUE_TYPE(kExtensionsManifestV2), \
}))

#if !BUILDFLAG(IS_ANDROID)
#define BRAVE_EDUCATION_FEATURE_ENTRIES \
EXPAND_FEATURE_ENTRIES({ \
"brave-show-getting-started-page", \
"Show getting started pages", \
"Show a getting started page after completing the Welcome UX.", \
kOsDesktop, \
FEATURE_VALUE_TYPE(brave_education::features::kShowGettingStartedPage), \
})
#else
#define BRAVE_EDUCATION_FEATURE_ENTRIES
#endif

// Keep the last item empty.
#define LAST_BRAVE_FEATURE_ENTRIES_ITEM

Expand Down Expand Up @@ -971,6 +985,7 @@
BRAVE_MIDDLE_CLICK_AUTOSCROLL_FEATURE_ENTRY \
BRAVE_EXTENSIONS_MANIFEST_V2 \
BRAVE_WORKAROUND_NEW_WINDOW_FLASH \
BRAVE_EDUCATION_FEATURE_ENTRIES \
LAST_BRAVE_FEATURE_ENTRIES_ITEM // Keep it as the last item.
namespace flags_ui {
namespace {
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 @@ -220,6 +220,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_education/brave_education_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 @@ -822,6 +823,9 @@ void BraveContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
content::RegisterWebUIControllerInterfaceBinder<
commands::mojom::CommandsService, BraveSettingsUI>(map);
}
content::RegisterWebUIControllerInterfaceBinder<
brave_education::mojom::EducationPageHandlerFactory,
brave_education::BraveEducationUI>(map);
#endif

#if BUILDFLAG(ENABLE_AI_CHAT)
Expand Down
11 changes: 11 additions & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ source_set("ui") {
"toolbar/brave_bookmark_sub_menu_model.cc",
"toolbar/brave_bookmark_sub_menu_model.h",
"toolbar/brave_recent_tabs_sub_menu_model.h",
"webui/brave_education/brave_education_ui.cc",
"webui/brave_education/brave_education_ui.h",
"webui/brave_education/education_page_handler.cc",
"webui/brave_education/education_page_handler.h",
"webui/brave_education/getting_started_helper.cc",
"webui/brave_education/getting_started_helper.h",
"webui/brave_rewards/rewards_page_top_ui.cc",
"webui/brave_rewards/rewards_page_top_ui.h",
"webui/brave_rewards/rewards_panel_handler.cc",
Expand All @@ -249,6 +255,8 @@ source_set("ui") {
"webui/brave_shields/shields_panel_handler.h",
"webui/brave_shields/shields_panel_ui.cc",
"webui/brave_shields/shields_panel_ui.h",
"webui/browser_command/brave_browser_command_handler.cc",
"webui/browser_command/brave_browser_command_handler.h",
"webui/navigation_bar_data_provider.cc",
"webui/navigation_bar_data_provider.h",
"webui/new_tab_page/brave_new_tab_message_handler.cc",
Expand Down Expand Up @@ -310,6 +318,9 @@ source_set("ui") {
"//base",
"//brave/browser/profiles:util",
"//brave/common",
"//brave/components/brave_education/common",
"//brave/components/brave_education/common:mojom",
"//brave/components/brave_education/resources:generated_resources",
"//brave/components/constants",
"//brave/components/misc_metrics",
"//brave/components/sidebar/browser",
Expand Down
111 changes: 111 additions & 0 deletions browser/ui/webui/brave_education/brave_education_ui.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// 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/.

#include "brave/browser/ui/webui/brave_education/brave_education_ui.h"

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "brave/browser/ui/webui/brave_education/education_page_handler.h"
#include "brave/browser/ui/webui/brave_webui_source.h"
#include "brave/browser/ui/webui/browser_command/brave_browser_command_handler.h"
#include "brave/components/brave_education/common/education_content_urls.h"
#include "brave/components/brave_education/resources/grit/brave_education_generated_map.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/webui_util.h"
#include "chrome/grit/branded_strings.h"
#include "components/grit/brave_components_resources.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
#include "services/network/public/mojom/content_security_policy.mojom.h"
#include "ui/base/webui/web_ui_util.h"

namespace brave_education {

namespace {

std::vector<browser_command::mojom::Command> GetSupportedCommands(
const GURL& webui_url) {
using browser_command::mojom::Command;

auto content_type = EducationContentTypeFromBrowserURL(webui_url.spec());
if (!content_type) {
return {};
}

switch (*content_type) {
case EducationContentType::kGettingStartedAdBlock:
return {Command::kOpenContentFilterSettings,
Command::kOpenShieldsSettings, Command::kOpenVPNOnboarding};
case EducationContentType::kGettingStartedWeb3:
return {Command::kOpenRewardsOnboarding, Command::kOpenWalletOnboarding,
Command::kOpenWeb3Settings};
case EducationContentType::kGettingStartedPrivacy:
return {Command::kOpenPrivacySettings, Command::kOpenVPNOnboarding};
}
}

} // namespace

BraveEducationUI::BraveEducationUI(content::WebUI* web_ui,
const std::string& host_name)
: ui::MojoWebUIController(web_ui) {
content::WebUIDataSource* source = content::WebUIDataSource::CreateAndAdd(
Profile::FromWebUI(web_ui), host_name);

webui::SetupWebUIDataSource(
source,
base::make_span(kBraveEducationGenerated, kBraveEducationGeneratedSize),
IDR_BRAVE_EDUCATION_HTML);

AddBackgroundColorToSource(source, web_ui->GetWebContents());

static constexpr webui::LocalizedString kStrings[] = {
{"headerText", IDS_WELCOME_HEADER}};
source->AddLocalizedStrings(kStrings);

// Allow embedding of iframe from brave.com.
source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::ChildSrc,
"child-src chrome://webui-test https://brave.com/;");
}

BraveEducationUI::~BraveEducationUI() = default;

void BraveEducationUI::BindInterface(
mojo::PendingReceiver<EducationPageHandlerFactory> pending_receiver) {
if (page_factory_receiver_.is_bound()) {
page_factory_receiver_.reset();
}
page_factory_receiver_.Bind(std::move(pending_receiver));
}

void BraveEducationUI::BindInterface(
mojo::PendingReceiver<CommandHandlerFactory> pending_receiver) {
if (command_handler_factory_receiver_.is_bound()) {
command_handler_factory_receiver_.reset();
}
command_handler_factory_receiver_.Bind(std::move(pending_receiver));
}

void BraveEducationUI::CreatePageHandler(
mojo::PendingReceiver<mojom::EducationPageHandler> handler) {
page_handler_ = std::make_unique<EducationPageHandler>(std::move(handler));
}

void BraveEducationUI::CreateBrowserCommandHandler(
mojo::PendingReceiver<browser_command::mojom::CommandHandler>
pending_handler) {
command_handler_ = std::make_unique<BraveBrowserCommandHandler>(
std::move(pending_handler), Profile::FromWebUI(web_ui()),
GetSupportedCommands(web_ui()->GetWebContents()->GetVisibleURL()));
}

WEB_UI_CONTROLLER_TYPE_IMPL(BraveEducationUI)

} // namespace brave_education
64 changes: 64 additions & 0 deletions browser/ui/webui/brave_education/brave_education_ui.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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/.

#ifndef BRAVE_BROWSER_UI_WEBUI_BRAVE_EDUCATION_BRAVE_EDUCATION_UI_H_
#define BRAVE_BROWSER_UI_WEBUI_BRAVE_EDUCATION_BRAVE_EDUCATION_UI_H_

#include <memory>
#include <string>

#include "base/memory/raw_ptr.h"
#include "brave/components/brave_education/common/education_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 "ui/webui/mojo_web_ui_controller.h"
#include "ui/webui/resources/js/browser_command/browser_command.mojom.h"

namespace content {
class WebUI;
}

namespace brave_education {

// The Web UI controller for the Brave product education page, which displays
// production education website content in an iframe.
class BraveEducationUI : public ui::MojoWebUIController,
public mojom::EducationPageHandlerFactory,
public browser_command::mojom::CommandHandlerFactory {
public:
BraveEducationUI(content::WebUI* web_ui, const std::string& host_name);
~BraveEducationUI() override;

BraveEducationUI(const BraveEducationUI&) = delete;
BraveEducationUI& operator=(const BraveEducationUI&) = delete;

void BindInterface(
mojo::PendingReceiver<EducationPageHandlerFactory> pending_receiver);

void BindInterface(
mojo::PendingReceiver<CommandHandlerFactory> pending_receiver);

// mojom::EducationPageHandlerFactory:
void CreatePageHandler(
mojo::PendingReceiver<mojom::EducationPageHandler> handler) override;

// browser_command::mojom::CommandHandlerFactory:
void CreateBrowserCommandHandler(
mojo::PendingReceiver<browser_command::mojom::CommandHandler> handler)
override;

private:
mojo::Receiver<EducationPageHandlerFactory> page_factory_receiver_{this};
mojo::Receiver<CommandHandlerFactory> command_handler_factory_receiver_{this};
std::unique_ptr<mojom::EducationPageHandler> page_handler_;
std::unique_ptr<browser_command::mojom::CommandHandler> command_handler_;

WEB_UI_CONTROLLER_TYPE_DECL();
};

} // namespace brave_education

#endif // BRAVE_BROWSER_UI_WEBUI_BRAVE_EDUCATION_BRAVE_EDUCATION_UI_H_
Loading

0 comments on commit 56f5042

Please sign in to comment.