Skip to content

Commit

Permalink
[NTP Next] Add top sites
Browse files Browse the repository at this point in the history
  • Loading branch information
zenparsing committed Jan 2, 2025
1 parent 503b329 commit 70863db
Show file tree
Hide file tree
Showing 48 changed files with 2,876 additions and 47 deletions.
2 changes: 2 additions & 0 deletions browser/ui/webui/brave_new_tab/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ source_set("brave_new_tab") {
"new_tab_page_handler.h",
"new_tab_page_ui.cc",
"new_tab_page_ui.h",
"top_sites_adapter.cc",
"top_sites_adapter.h",
"update_observer.cc",
"update_observer.h",
]
Expand Down
148 changes: 148 additions & 0 deletions browser/ui/webui/brave_new_tab/new_tab_page_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
#include "base/strings/utf_string_conversions.h"
#include "brave/browser/ui/webui/brave_new_tab/background_adapter.h"
#include "brave/browser/ui/webui/brave_new_tab/custom_image_chooser.h"
#include "brave/browser/ui/webui/brave_new_tab/top_sites_adapter.h"
#include "brave/components/brave_private_cdn/private_cdn_helper.h"
#include "brave/components/brave_private_cdn/private_cdn_request_helper.h"
#include "brave/components/brave_search_conversion/pref_names.h"
#include "brave/components/constants/pref_names.h"
#include "brave/components/ntp_background_images/common/pref_names.h"
#include "chrome/browser/themes/theme_syncable_service.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
Expand All @@ -32,6 +34,7 @@ NewTabPageHandler::NewTabPageHandler(
mojo::PendingReceiver<mojom::NewTabPageHandler> receiver,
std::unique_ptr<CustomImageChooser> custom_image_chooser,
std::unique_ptr<BackgroundAdapter> background_adapter,
std::unique_ptr<TopSitesAdapter> top_sites_adapter,
std::unique_ptr<brave_private_cdn::PrivateCDNRequestHelper> pcdn_helper,
tabs::TabInterface& tab,
PrefService& pref_service,
Expand All @@ -40,6 +43,7 @@ NewTabPageHandler::NewTabPageHandler(
update_observer_(pref_service),
custom_image_chooser_(std::move(custom_image_chooser)),
background_adapter_(std::move(background_adapter)),
top_sites_adapter_(std::move(top_sites_adapter)),
pcdn_helper_(std::move(pcdn_helper)),
tab_(tab),
pref_service_(pref_service),
Expand All @@ -50,6 +54,10 @@ NewTabPageHandler::NewTabPageHandler(

update_observer_.SetCallback(base::BindRepeating(&NewTabPageHandler::OnUpdate,
weak_factory_.GetWeakPtr()));
if (top_sites_adapter_) {
top_sites_adapter_->SetSitesUpdatedCallback(base::BindRepeating(
&NewTabPageHandler::OnTopSitesListUpdated, weak_factory_.GetWeakPtr()));
}
}

NewTabPageHandler::~NewTabPageHandler() = default;
Expand Down Expand Up @@ -278,6 +286,113 @@ void NewTabPageHandler::OpenURLFromSearch(const std::string& url,
std::move(callback).Run();
}

void NewTabPageHandler::GetShowTopSites(GetShowTopSitesCallback callback) {
if (top_sites_adapter_) {
std::move(callback).Run(top_sites_adapter_->GetTopSitesVisible());
} else {
std::move(callback).Run(false);
}
}

void NewTabPageHandler::SetShowTopSites(bool show_top_sites,
SetShowTopSitesCallback callback) {
if (top_sites_adapter_) {
top_sites_adapter_->SetTopSitesVisible(show_top_sites);
}
std::move(callback).Run();
}

void NewTabPageHandler::GetTopSitesListKind(
GetTopSitesListKindCallback callback) {
if (top_sites_adapter_) {
std::move(callback).Run(top_sites_adapter_->GetListKind());
} else {
std::move(callback).Run(mojom::TopSitesListKind::kMostVisited);
}
}

void NewTabPageHandler::SetTopSitesListKind(
mojom::TopSitesListKind list_kind,
SetTopSitesListKindCallback callback) {
if (top_sites_adapter_) {
top_sites_adapter_->SetListKind(list_kind);
}
std::move(callback).Run();
}

void NewTabPageHandler::GetTopSites(GetTopSitesCallback callback) {
if (!top_sites_adapter_) {
std::move(callback).Run({});
return;
}
top_sites_adapter_->GetSites(std::move(callback));
}

void NewTabPageHandler::AddCustomTopSite(const std::string& url,
const std::string& title,
AddCustomTopSiteCallback callback) {
if (top_sites_adapter_) {
top_sites_adapter_->AddCustomSite(url, title);
}
std::move(callback).Run();
}

void NewTabPageHandler::UpdateCustomTopSite(
const std::string& url,
const std::string& new_url,
const std::string& title,
UpdateCustomTopSiteCallback callback) {
if (top_sites_adapter_) {
top_sites_adapter_->UpdateCustomSite(url, new_url, title);
}
std::move(callback).Run();
}

void NewTabPageHandler::RemoveCustomTopSite(
const std::string& url,
RemoveCustomTopSiteCallback callback) {
if (top_sites_adapter_) {
top_sites_adapter_->RemoveCustomSite(url);
}
std::move(callback).Run();
}

void NewTabPageHandler::UndoCustomTopSiteAction(
UndoCustomTopSiteActionCallback callback) {
if (top_sites_adapter_) {
top_sites_adapter_->UndoCustomSiteAction();
}
std::move(callback).Run();
}

void NewTabPageHandler::ExcludeMostVisitedTopSite(
const std::string& url,
ExcludeMostVisitedTopSiteCallback callback) {
if (top_sites_adapter_) {
top_sites_adapter_->ExcludeMostVisitedSite(url);
}
std::move(callback).Run();
}

void NewTabPageHandler::IncludeMostVisitedTopSite(
const std::string& url,
IncludeMostVisitedTopSiteCallback callback) {
if (top_sites_adapter_) {
top_sites_adapter_->IncludeMostVisitedTopSite(url);
}
std::move(callback).Run();
}

void NewTabPageHandler::SetCustomTopSitePosition(
const std::string& url,
int32_t position,
SetCustomTopSitePositionCallback callback) {
if (top_sites_adapter_) {
top_sites_adapter_->SetCustomSitePosition(url, position);
}
std::move(callback).Run();
}

void NewTabPageHandler::OnCustomBackgroundsSelected(
ShowCustomBackgroundChooserCallback callback,
std::vector<base::FilePath> paths) {
Expand All @@ -292,6 +407,26 @@ void NewTabPageHandler::OnCustomBackgroundsSelected(
}
}

void NewTabPageHandler::GetShowClock(GetShowClockCallback callback) {
std::move(callback).Run(pref_service_->GetBoolean(kNewTabPageShowClock));
}

void NewTabPageHandler::SetShowClock(bool show_clock,
SetShowClockCallback callback) {
pref_service_->SetBoolean(kNewTabPageShowClock, show_clock);
std::move(callback).Run();
}

void NewTabPageHandler::GetClockFormat(GetClockFormatCallback callback) {
std::move(callback).Run(pref_service_->GetString(kNewTabPageClockFormat));
}

void NewTabPageHandler::SetClockFormat(const std::string& clock_format,
SetClockFormatCallback callback) {
pref_service_->SetString(kNewTabPageClockFormat, clock_format);
std::move(callback).Run();
}

void NewTabPageHandler::OnUpdate(UpdateObserver::Source update_source) {
if (!page_.is_bound()) {
return;
Expand All @@ -300,10 +435,23 @@ void NewTabPageHandler::OnUpdate(UpdateObserver::Source update_source) {
case UpdateObserver::Source::kBackgroundPrefs:
page_->OnBackgroundPrefsUpdated();
break;
case UpdateObserver::Source::kClockPrefs:
page_->OnClockPrefsUpdated();
break;
case UpdateObserver::Source::kSearchPrefs:
page_->OnSearchPrefsUpdated();
break;
case UpdateObserver::Source::kTopSitesPrefs:
page_->OnTopSitesPrefsUpdated();
break;
}
}

void NewTabPageHandler::OnTopSitesListUpdated() {
if (!page_.is_bound()) {
return;
}
page_->OnTopSitesListUpdated();
}

} // namespace brave_new_tab
38 changes: 38 additions & 0 deletions browser/ui/webui/brave_new_tab/new_tab_page_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ namespace brave_new_tab {

class BackgroundAdapter;
class CustomImageChooser;
class TopSitesAdapter;

class NewTabPageHandler : public mojom::NewTabPageHandler {
public:
NewTabPageHandler(
mojo::PendingReceiver<mojom::NewTabPageHandler> receiver,
std::unique_ptr<CustomImageChooser> custom_image_chooser,
std::unique_ptr<BackgroundAdapter> background_adapter,
std::unique_ptr<TopSitesAdapter> top_sites_adapter,
std::unique_ptr<brave_private_cdn::PrivateCDNRequestHelper> pcdn_helper,
tabs::TabInterface& tab,
PrefService& pref_service,
Expand Down Expand Up @@ -100,18 +102,54 @@ class NewTabPageHandler : public mojom::NewTabPageHandler {
void OpenURLFromSearch(const std::string& url,
mojom::EventDetailsPtr details,
OpenURLFromSearchCallback callback) override;
void GetShowTopSites(GetShowTopSitesCallback callback) override;
void SetShowTopSites(bool show_top_sites,
SetShowTopSitesCallback callback) override;
void GetTopSitesListKind(GetTopSitesListKindCallback callback) override;
void SetTopSitesListKind(mojom::TopSitesListKind list_kind,
SetTopSitesListKindCallback callback) override;
void GetTopSites(GetTopSitesCallback callback) override;
void AddCustomTopSite(const std::string& url,
const std::string& title,
AddCustomTopSiteCallback callback) override;
void UpdateCustomTopSite(const std::string& url,
const std::string& new_url,
const std::string& title,
UpdateCustomTopSiteCallback callback) override;
void SetCustomTopSitePosition(
const std::string& url,
int32_t position,
SetCustomTopSitePositionCallback callback) override;
void RemoveCustomTopSite(const std::string& url,
RemoveCustomTopSiteCallback callback) override;
void UndoCustomTopSiteAction(
UndoCustomTopSiteActionCallback callback) override;
void ExcludeMostVisitedTopSite(
const std::string& url,
ExcludeMostVisitedTopSiteCallback callback) override;
void IncludeMostVisitedTopSite(
const std::string& url,
IncludeMostVisitedTopSiteCallback callback) override;
void GetShowClock(GetShowClockCallback callback) override;
void SetShowClock(bool show_clock, SetShowClockCallback callback) override;
void GetClockFormat(GetClockFormatCallback callback) override;
void SetClockFormat(const std::string& clock_format,
SetClockFormatCallback callback) override;

private:
void OnCustomBackgroundsSelected(ShowCustomBackgroundChooserCallback callback,
std::vector<base::FilePath> paths);

void OnUpdate(UpdateObserver::Source update_source);

void OnTopSitesListUpdated();

mojo::Receiver<mojom::NewTabPageHandler> receiver_;
mojo::Remote<mojom::NewTabPage> page_;
UpdateObserver update_observer_;
std::unique_ptr<CustomImageChooser> custom_image_chooser_;
std::unique_ptr<BackgroundAdapter> background_adapter_;
std::unique_ptr<TopSitesAdapter> top_sites_adapter_;
std::unique_ptr<brave_private_cdn::PrivateCDNRequestHelper> pcdn_helper_;
raw_ref<tabs::TabInterface> tab_;
raw_ref<PrefService> pref_service_;
Expand Down
44 changes: 40 additions & 4 deletions browser/ui/webui/brave_new_tab/new_tab_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
#include "brave/browser/ui/webui/brave_new_tab/background_adapter.h"
#include "brave/browser/ui/webui/brave_new_tab/custom_image_chooser.h"
#include "brave/browser/ui/webui/brave_new_tab/new_tab_page_handler.h"
#include "brave/browser/ui/webui/brave_new_tab/top_sites_adapter.h"
#include "brave/browser/ui/webui/brave_webui_source.h"
#include "brave/components/brave_new_tab/new_tab_prefs.h"
#include "brave/components/brave_new_tab/resources/grit/brave_new_tab_generated_map.h"
#include "brave/components/brave_private_cdn/private_cdn_request_helper.h"
#include "brave/components/l10n/common/localization_util.h"
#include "brave/components/ntp_background_images/browser/ntp_custom_images_source.h"
#include "chrome/browser/ntp_tiles/chrome_most_visited_sites_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/tabs/public/tab_interface.h"
Expand All @@ -28,6 +30,7 @@
#include "components/favicon_base/favicon_url_parser.h"
#include "components/grit/brave_components_resources.h"
#include "components/grit/brave_components_strings.h"
#include "components/ntp_tiles/most_visited_sites.h"
#include "components/prefs/pref_service.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/web_ui.h"
Expand All @@ -40,16 +43,30 @@ namespace brave_new_tab {
namespace {

static constexpr webui::LocalizedString kStrings[] = {
{"addTopSiteLabel", IDS_NEW_TAB_ADD_TOP_SITE_LABEL},
{"addTopSiteTitle", IDS_NEW_TAB_ADD_TOP_SITE_TITLE},
{"backgroundSettingsTitle", IDS_NEW_TAB_BACKGROUND_SETTINGS_TITLE},
{"braveBackgroundLabel", IDS_NEW_TAB_BRAVE_BACKGROUND_LABEL},
{"cancelButtonLabel", IDS_NEW_TAB_CANCEL_BUTTON_LABEL},
{"clockFormatLabel", IDS_NEW_TAB_CLOCK_FORMAT_LABEL},
{"clockFormatOption12HourText", IDS_NEW_TAB_CLOCK_FORMAT_OPTION12HOUR_TEXT},
{"clockFormatOption24HourText", IDS_NEW_TAB_CLOCK_FORMAT_OPTION24HOUR_TEXT},
{"clockFormatOptionAutomaticText",
IDS_NEW_TAB_CLOCK_FORMAT_OPTION_AUTOMATIC_TEXT},
{"clockSettingsTitle", IDS_NEW_TAB_CLOCK_SETTINGS_TITLE},
{"customBackgroundLabel", IDS_NEW_TAB_CUSTOM_BACKGROUND_LABEL},
{"customBackgroundTitle", IDS_NEW_TAB_CUSTOM_BACKGROUND_LABEL},
{"customizeSearchEnginesLink", IDS_NEW_TAB_CUSTOMIZE_SEARCH_ENGINES_LINK},
{"editTopSiteLabel", IDS_NEW_TAB_EDIT_TOP_SITE_LABEL},
{"editTopSiteTitle", IDS_NEW_TAB_EDIT_TOP_SITE_TITLE},
{"enabledSearchEnginesLabel", IDS_NEW_TAB_ENABLED_SEARCH_ENGINES_LABEL},
{"gradientBackgroundLabel", IDS_NEW_TAB_GRADIENT_BACKGROUND_LABEL},
{"gradientBackgroundTitle", IDS_NEW_TAB_GRADIENT_BACKGROUND_LABEL},
{"hideTopSitesLabel", IDS_NEW_TAB_HIDE_TOP_SITES_LABEL},
{"photoCreditsText", IDS_NEW_TAB_PHOTO_CREDITS_TEXT},
{"randomizeBackgroundLabel", IDS_NEW_TAB_RANDOMIZE_BACKGROUND_LABEL},
{"removeTopSiteLabel", IDS_NEW_TAB_REMOVE_TOP_SITE_LABEL},
{"saveChangesButtonLabel", IDS_NEW_TAB_SAVE_CHANGES_BUTTON_LABEL},
{"searchAskLeoDescription", IDS_OMNIBOX_ASK_LEO_DESCRIPTION},
{"searchBoxPlaceholderText", IDS_NEW_TAB_SEARCH_BOX_PLACEHOLDER_TEXT},
{"searchBoxPlaceholderTextBrave",
Expand All @@ -66,12 +83,28 @@ static constexpr webui::LocalizedString kStrings[] = {
IDS_NEW_TAB_SEARCH_SUGGESTIONS_PROMPT_TITLE},
{"settingsTitle", IDS_NEW_TAB_SETTINGS_TITLE},
{"showBackgroundsLabel", IDS_NEW_TAB_SHOW_BACKGROUNDS_LABEL},
{"showClockLabel", IDS_NEW_TAB_SHOW_CLOCK_LABEL},
{"showSearchBoxLabel", IDS_NEW_TAB_SHOW_SEARCH_BOX_LABEL},
{"showSponsoredImagesLabel", IDS_NEW_TAB_SHOW_SPONSORED_IMAGES_LABEL},
{"showTopSitesLabel", IDS_NEW_TAB_SHOW_TOP_SITES_LABEL},
{"solidBackgroundLabel", IDS_NEW_TAB_SOLID_BACKGROUND_LABEL},
{"solidBackgroundTitle", IDS_NEW_TAB_SOLID_BACKGROUND_LABEL},
{"uploadBackgroundLabel", IDS_NEW_TAB_UPLOAD_BACKGROUND_LABEL},
};
{"topSiteRemovedText", IDS_NEW_TAB_TOP_SITE_REMOVED_TEXT},
{"topSiteRemovedTitle", IDS_NEW_TAB_TOP_SITE_REMOVED_TITLE},
{"topSitesCustomOptionText", IDS_NEW_TAB_TOP_SITES_CUSTOM_OPTION_TEXT},
{"topSitesCustomOptionTitle", IDS_NEW_TAB_TOP_SITES_CUSTOM_OPTION_TITLE},
{"topSitesMostVisitedOptionText",
IDS_NEW_TAB_TOP_SITES_MOST_VISITED_OPTION_TEXT},
{"topSitesMostVisitedOptionTitle",
IDS_NEW_TAB_TOP_SITES_MOST_VISITED_OPTION_TITLE},
{"topSitesSettingsTitle", IDS_NEW_TAB_TOP_SITES_SETTINGS_TITLE},
{"topSitesShowCustomLabel", IDS_NEW_TAB_TOP_SITES_SHOW_CUSTOM_LABEL},
{"topSitesShowMostVisitedLabel",
IDS_NEW_TAB_TOP_SITES_SHOW_MOST_VISITED_LABEL},
{"topSitesTitleLabel", IDS_NEW_TAB_TOP_SITES_TITLE_LABEL},
{"topSitesURLLabel", IDS_NEW_TAB_TOP_SITES_URL_LABEL},
{"undoButtonLabel", IDS_NEW_TAB_UNDO_BUTTON_LABEL},
{"uploadBackgroundLabel", IDS_NEW_TAB_UPLOAD_BACKGROUND_LABEL}};

constexpr auto kPcdnImageLoaderTrafficAnnotation =
net::DefineNetworkTrafficAnnotation("brave_new_tab_pcdn_loader",
Expand Down Expand Up @@ -151,7 +184,6 @@ void NewTabPageUI::BindInterface(
mojo::PendingReceiver<mojom::NewTabPageHandler> pending_receiver) {
auto* web_contents = web_ui()->GetWebContents();
auto* profile = Profile::FromWebUI(web_ui());

auto* prefs = profile->GetPrefs();

auto image_chooser =
Expand All @@ -161,13 +193,17 @@ void NewTabPageUI::BindInterface(
std::make_unique<CustomBackgroundFileManager>(profile), *prefs,
ntp_background_images::ViewCounterServiceFactory::GetForProfile(profile));

auto top_sites_adapter = std::make_unique<TopSitesAdapter>(
ChromeMostVisitedSitesFactory::NewForProfile(profile), *prefs);

auto pcdn_helper =
std::make_unique<brave_private_cdn::PrivateCDNRequestHelper>(
kPcdnImageLoaderTrafficAnnotation, profile->GetURLLoaderFactory());

page_handler_ = std::make_unique<NewTabPageHandler>(
std::move(pending_receiver), std::move(image_chooser),
std::move(background_adapter), std::move(pcdn_helper),
std::move(background_adapter), std::move(top_sites_adapter),
std::move(pcdn_helper),
*tabs::TabInterface::GetFromContents(web_contents), *prefs,
*TemplateURLServiceFactory::GetForProfile(profile));
}
Expand Down
Loading

0 comments on commit 70863db

Please sign in to comment.