Skip to content

Commit

Permalink
[NTP Next] Add search bar
Browse files Browse the repository at this point in the history
  • Loading branch information
zenparsing committed Dec 17, 2024
1 parent 62b662e commit 6a6452b
Show file tree
Hide file tree
Showing 37 changed files with 2,013 additions and 21 deletions.
8 changes: 5 additions & 3 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,10 @@ void BraveContentBrowserClient::RegisterWebUIInterfaceBrokers(
.Add<brave_new_tab_page::mojom::PageHandlerFactory>()
.Add<brave_news::mojom::BraveNewsController>();

auto ntp_next_registration =
registry.ForWebUI<brave_new_tab::NewTabPageUI>()
.Add<brave_new_tab::mojom::NewTabPageHandler>();

#if BUILDFLAG(ENABLE_BRAVE_VPN)
if (brave_vpn::IsBraveVPNFeatureEnabled()) {
ntp_registration.Add<brave_vpn::mojom::ServiceHandler>();
Expand All @@ -660,6 +664,7 @@ void BraveContentBrowserClient::RegisterWebUIInterfaceBrokers(

if (base::FeatureList::IsEnabled(features::kBraveNtpSearchWidget)) {
ntp_registration.Add<searchbox::mojom::PageHandler>();
ntp_next_registration.Add<searchbox::mojom::PageHandler>();
}

if (base::FeatureList::IsEnabled(
Expand Down Expand Up @@ -846,9 +851,6 @@ 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
5 changes: 5 additions & 0 deletions browser/ui/webui/brave_new_tab/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ source_set("brave_new_tab") {
"//brave/components/brave_new_tab",
"//brave/components/brave_new_tab:mojom",
"//brave/components/brave_new_tab/resources:generated_resources",
"//brave/components/brave_private_cdn",
"//brave/components/brave_search_conversion",
"//brave/components/l10n/common",
"//brave/components/ntp_background_images/browser",
"//brave/components/ntp_background_images/common",
Expand All @@ -36,8 +38,11 @@ source_set("brave_new_tab") {
"//chrome/app:generated_resources",
"//chrome/browser:browser_public_dependencies",
"//chrome/browser/profiles:profile",
"//chrome/browser/search_engines",
"//chrome/browser/themes",
"//chrome/browser/ui/browser_window",
"//chrome/browser/ui/webui:webui_util",
"//chrome/browser/ui/webui/searchbox",
"//components/prefs",
"//components/strings:components_strings",
"//ui/base",
Expand Down
165 changes: 163 additions & 2 deletions browser/ui/webui/brave_new_tab/new_tab_page_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,46 @@

#include <utility>

#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "brave/browser/ui/webui/brave_new_tab/background_provider.h"
#include "brave/browser/ui/webui/brave_new_tab/custom_image_chooser.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/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"
#include "chrome/browser/ui/tabs/public/tab_interface.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/search_engines/search_engine_type.h"
#include "components/search_engines/template_url_service.h"
#include "services/network/public/cpp/header_util.h"
#include "ui/base/window_open_disposition_utils.h"
#include "url/gurl.h"

namespace brave_new_tab {

NewTabPageHandler::NewTabPageHandler(
mojo::PendingReceiver<mojom::NewTabPageHandler> receiver,
std::unique_ptr<CustomImageChooser> custom_image_chooser,
std::unique_ptr<BackgroundProvider> background_provider,
PrefService& pref_service)
std::unique_ptr<brave_private_cdn::PrivateCDNRequestHelper> pcdn_helper,
tabs::TabInterface& tab,
PrefService& pref_service,
TemplateURLService& template_url_service)
: receiver_(this, std::move(receiver)),
update_observer_(pref_service),
custom_image_chooser_(std::move(custom_image_chooser)),
background_provider_(std::move(background_provider)),
pref_service_(pref_service) {
pcdn_helper_(std::move(pcdn_helper)),
tab_(tab),
pref_service_(pref_service),
template_url_service_(template_url_service) {
CHECK(custom_image_chooser_);
CHECK(background_provider_);
CHECK(pcdn_helper_);

update_observer_.SetCallback(base::BindRepeating(&NewTabPageHandler::OnUpdate,
weak_factory_.GetWeakPtr()));
Expand All @@ -40,6 +60,38 @@ void NewTabPageHandler::SetNewTabPage(
page_.Bind(std::move(page));
}

void NewTabPageHandler::LoadResourceFromPcdn(
const std::string& url,
LoadResourceFromPcdnCallback callback) {
GURL resource_url(url);
if (!resource_url.is_valid()) {
std::move(callback).Run(std::nullopt);
return;
}

auto on_resource_downloaded = [](decltype(callback) callback, bool is_padded,
int response_code, const std::string& body) {
if (!network::IsSuccessfulStatus(response_code)) {
std::move(callback).Run(std::nullopt);
return;
}
std::string_view body_view(body);
if (is_padded) {
if (!brave::PrivateCdnHelper::GetInstance()->RemovePadding(&body_view)) {
std::move(callback).Run(std::nullopt);
return;
}
}
std::move(callback).Run(
std::vector<uint8_t>(body_view.begin(), body_view.end()));
};

pcdn_helper_->DownloadToString(
resource_url,
base::BindOnce(on_resource_downloaded, std::move(callback),
base::EndsWith(resource_url.path(), ".pad")));
}

void NewTabPageHandler::GetBackgroundsEnabled(
GetBackgroundsEnabledCallback callback) {
bool backgrounds_enabled = pref_service_->GetBoolean(
Expand Down Expand Up @@ -120,6 +172,112 @@ void NewTabPageHandler::RemoveCustomBackground(
std::move(callback));
}

void NewTabPageHandler::GetShowSearchBox(GetShowSearchBoxCallback callback) {
std::move(callback).Run(pref_service_->GetBoolean(
brave_search_conversion::prefs::kShowNTPSearchBox));
}

void NewTabPageHandler::SetShowSearchBox(bool show_search_box,
SetShowSearchBoxCallback callback) {
pref_service_->SetBoolean(brave_search_conversion::prefs::kShowNTPSearchBox,
show_search_box);
std::move(callback).Run();
}

void NewTabPageHandler::GetSearchSuggestionsEnabled(
GetSearchSuggestionsEnabledCallback callback) {
std::move(callback).Run(
pref_service_->GetBoolean(prefs::kSearchSuggestEnabled));
}

void NewTabPageHandler::SetSearchSuggestionsEnabled(
bool enabled,
SetSearchSuggestionsEnabledCallback callback) {
pref_service_->SetBoolean(prefs::kSearchSuggestEnabled, enabled);
std::move(callback).Run();
}

void NewTabPageHandler::GetSearchSuggestionsPromptDismissed(
GetSearchSuggestionsPromptDismissedCallback callback) {
std::move(callback).Run(
pref_service_->GetBoolean(brave_search_conversion::prefs::kDismissed));
}

void NewTabPageHandler::SetSearchSuggestionsPromptDismissed(
bool dismissed,
SetSearchSuggestionsPromptDismissedCallback callback) {
pref_service_->SetBoolean(brave_search_conversion::prefs::kDismissed,
dismissed);
std::move(callback).Run();
}

void NewTabPageHandler::GetLastUsedSearchEngine(
GetLastUsedSearchEngineCallback callback) {
std::move(callback).Run(pref_service_->GetString(
brave_search_conversion::prefs::kLastUsedNTPSearchEngine));
}

void NewTabPageHandler::SetLastUsedSearchEngine(
const std::string& engine_host,
SetLastUsedSearchEngineCallback callback) {
pref_service_->SetString(
brave_search_conversion::prefs::kLastUsedNTPSearchEngine, engine_host);
std::move(callback).Run();
}

void NewTabPageHandler::GetAvailableSearchEngines(
GetAvailableSearchEnginesCallback callback) {
std::vector<mojom::SearchEngineInfoPtr> search_engines;
for (auto template_url : template_url_service_->GetTemplateURLs()) {
if (template_url->GetBuiltinEngineType() !=
BuiltinEngineType::KEYWORD_MODE_PREPOPULATED_ENGINE) {
continue;
}
auto search_engine = mojom::SearchEngineInfo::New();
search_engine->prepopulate_id = template_url->prepopulate_id();
search_engine->host = GURL(template_url->url()).host();
if (search_engine->host.empty()) {
search_engine->host = "google.com";
}
search_engine->name = base::UTF16ToUTF8(template_url->short_name());
search_engine->keyword = base::UTF16ToUTF8(template_url->keyword());
search_engine->favicon_url = template_url->favicon_url().spec();
search_engines.push_back(std::move(search_engine));
}
std::move(callback).Run(std::move(search_engines));
}

void NewTabPageHandler::OpenSearch(const std::string& query,
const std::string& engine,
mojom::EventDetailsPtr details,
OpenSearchCallback callback) {
auto* template_url = template_url_service_->GetTemplateURLForHost(engine);
if (!template_url) {
std::move(callback).Run();
return;
}

GURL search_url = template_url->GenerateSearchURL(
template_url_service_->search_terms_data(), base::UTF8ToUTF16(query));

tab_->GetBrowserWindowInterface()->OpenGURL(
search_url,
ui::DispositionFromClick(false, details->alt_key, details->ctrl_key,
details->meta_key, details->shift_key));

std::move(callback).Run();
}

void NewTabPageHandler::OpenURLFromSearch(const std::string& url,
mojom::EventDetailsPtr details,
OpenURLFromSearchCallback callback) {
tab_->GetBrowserWindowInterface()->OpenGURL(
GURL(url),
ui::DispositionFromClick(false, details->alt_key, details->ctrl_key,
details->meta_key, details->shift_key));
std::move(callback).Run();
}

void NewTabPageHandler::OnCustomBackgroundsSelected(
ShowCustomBackgroundChooserCallback callback,
std::vector<base::FilePath> paths) {
Expand All @@ -142,6 +300,9 @@ void NewTabPageHandler::OnUpdate(UpdateObserver::Source update_source) {
case UpdateObserver::Source::kBackgroundPrefs:
page_->OnBackgroundPrefsUpdated();
break;
case UpdateObserver::Source::kSearchPrefs:
page_->OnSearchPrefsUpdated();
break;
}
}

Expand Down
53 changes: 49 additions & 4 deletions browser/ui/webui/brave_new_tab/new_tab_page_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
#include "mojo/public/cpp/bindings/remote.h"

class PrefService;
class TemplateURLService;

namespace brave_private_cdn {
class PrivateCDNRequestHelper;
}

namespace tabs {
class TabInterface;
}

namespace brave_new_tab {

Expand All @@ -28,15 +37,21 @@ class CustomImageChooser;

class NewTabPageHandler : public mojom::NewTabPageHandler {
public:
NewTabPageHandler(mojo::PendingReceiver<mojom::NewTabPageHandler> receiver,
std::unique_ptr<CustomImageChooser> custom_image_chooser,
std::unique_ptr<BackgroundProvider> background_provider,
PrefService& pref_service);
NewTabPageHandler(
mojo::PendingReceiver<mojom::NewTabPageHandler> receiver,
std::unique_ptr<CustomImageChooser> custom_image_chooser,
std::unique_ptr<BackgroundProvider> background_provider,
std::unique_ptr<brave_private_cdn::PrivateCDNRequestHelper> pcdn_helper,
tabs::TabInterface& tab,
PrefService& pref_service,
TemplateURLService& template_url_service);

~NewTabPageHandler() override;

// mojom::NewTabPageHandler:
void SetNewTabPage(mojo::PendingRemote<mojom::NewTabPage> page) override;
void LoadResourceFromPcdn(const std::string& url,
LoadResourceFromPcdnCallback callback) override;
void GetBackgroundsEnabled(GetBackgroundsEnabledCallback callback) override;
void SetBackgroundsEnabled(bool enabled,
SetBackgroundsEnabledCallback callback) override;
Expand All @@ -58,6 +73,33 @@ class NewTabPageHandler : public mojom::NewTabPageHandler {
ShowCustomBackgroundChooserCallback callback) override;
void RemoveCustomBackground(const std::string& background_url,
RemoveCustomBackgroundCallback callback) override;
void GetShowSearchBox(GetShowSearchBoxCallback callback) override;
void SetShowSearchBox(bool show_search_box,
SetShowSearchBoxCallback callback) override;
void GetSearchSuggestionsEnabled(
GetSearchSuggestionsEnabledCallback callback) override;
void SetSearchSuggestionsEnabled(
bool enabled,
SetSearchSuggestionsEnabledCallback callback) override;
void GetSearchSuggestionsPromptDismissed(
GetSearchSuggestionsPromptDismissedCallback callback) override;
void SetSearchSuggestionsPromptDismissed(
bool dismissed,
SetSearchSuggestionsPromptDismissedCallback callback) override;
void GetLastUsedSearchEngine(
GetLastUsedSearchEngineCallback callback) override;
void SetLastUsedSearchEngine(
const std::string& engine_host,
SetLastUsedSearchEngineCallback callback) override;
void GetAvailableSearchEngines(
GetAvailableSearchEnginesCallback callback) override;
void OpenSearch(const std::string& query,
const std::string& engine,
mojom::EventDetailsPtr details,
OpenSearchCallback callback) override;
void OpenURLFromSearch(const std::string& url,
mojom::EventDetailsPtr details,
OpenURLFromSearchCallback callback) override;

private:
void OnCustomBackgroundsSelected(ShowCustomBackgroundChooserCallback callback,
Expand All @@ -70,7 +112,10 @@ class NewTabPageHandler : public mojom::NewTabPageHandler {
UpdateObserver update_observer_;
std::unique_ptr<CustomImageChooser> custom_image_chooser_;
std::unique_ptr<BackgroundProvider> background_provider_;
std::unique_ptr<brave_private_cdn::PrivateCDNRequestHelper> pcdn_helper_;
raw_ref<tabs::TabInterface> tab_;
raw_ref<PrefService> pref_service_;
raw_ref<TemplateURLService> template_url_service_;
base::WeakPtrFactory<NewTabPageHandler> weak_factory_{this};
};

Expand Down
Loading

0 comments on commit 6a6452b

Please sign in to comment.