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 Nov 21, 2024
1 parent a91a6cb commit e9bef8c
Show file tree
Hide file tree
Showing 63 changed files with 3,178 additions and 12 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/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 @@ -969,6 +970,13 @@
"corners, padding, and a drop shadow", \
kOsWin | kOsLinux | kOsMac, \
FEATURE_VALUE_TYPE(features::kBraveWebViewRoundedCorners), \
}, \
{ \
"brave-use-updated-ntp", \
"Use the updated New Tab Page", \
"Uses an updated version of the New Tab Page", \
kOsWin | kOsLinux | kOsMac, \
FEATURE_VALUE_TYPE(brave_new_tab::features::kUseUpdatedNTP), \
}) \
BRAVE_NATIVE_WALLET_FEATURE_ENTRIES \
BRAVE_NEWS_FEATURE_ENTRIES \
Expand Down
3 changes: 3 additions & 0 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,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_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 @@ -836,6 +837,8 @@ void BraveContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
content::RegisterWebUIControllerInterfaceBinder<
commands::mojom::CommandsService, BraveSettingsUI>(map);
}
content::RegisterWebUIControllerInterfaceBinder<
brave_new_tab::mojom::NewTabPageHandler, brave_new_tab::NewTabUI>(map);
#endif

auto* prefs =
Expand Down
2 changes: 2 additions & 0 deletions browser/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,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/common",
"//brave/components/brave_new_tab_ui:mojom",
"//brave/components/brave_news/common:mojom",
"//brave/components/brave_perf_predictor/browser",
Expand Down Expand Up @@ -288,6 +289,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 @@ -777,6 +777,7 @@ source_set("ui") {
"//brave/components/brave_ads/core",
"//brave/components/brave_federated",
"//brave/components/brave_federated/public/interfaces",
"//brave/components/brave_new_tab/common",
"//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 @@ -5,6 +5,8 @@

brave_ui_allow_circular_includes_from = []
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",
]
}
44 changes: 44 additions & 0 deletions browser/ui/webui/brave_new_tab/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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_ui.cc",
"new_tab_ui.h",
"update_observer.cc",
"update_observer.h",
]

deps = [
"//brave/browser:browser_process",
"//brave/browser/ntp_background",
"//brave/components/brave_new_tab/common",
"//brave/components/brave_new_tab/common: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/ui/webui:webui_util",
"//components/prefs",
"//components/strings:components_strings",
"//ui/base",
"//ui/shell_dialogs",
"//ui/webui",
]
}
86 changes: 86 additions & 0 deletions browser/ui/webui/brave_new_tab/custom_image_chooser.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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_new_tab/custom_image_chooser.h"

#include <utility>

#include "brave/components/l10n/common/localization_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/chrome_select_file_policy.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/web_contents.h"
#include "ui/shell_dialogs/selected_file_info.h"

namespace brave_new_tab {

CustomImageChooser::CustomImageChooser(content::WebContents* web_contents)
: web_contents_(web_contents),
profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())) {
}

CustomImageChooser::~CustomImageChooser() = default;

void CustomImageChooser::ShowDialog(ShowDialogCallback callback) {
if (callback_) {
std::move(callback_).Run({});
}

callback_ = std::move(callback);

if (dialog_) {
return;
}

dialog_ = ui::SelectFileDialog::Create(
this, std::make_unique<ChromeSelectFilePolicy>(web_contents_));

ui::SelectFileDialog::FileTypeInfo file_types;
file_types.allowed_paths = ui::SelectFileDialog::FileTypeInfo::NATIVE_PATH;
file_types.extensions.push_back(
{{FILE_PATH_LITERAL("jpg"), FILE_PATH_LITERAL("jpeg"),
FILE_PATH_LITERAL("png"), FILE_PATH_LITERAL("gif")}});
file_types.extension_description_overrides.push_back(
brave_l10n::GetLocalizedResourceUTF16String(IDS_UPLOAD_IMAGE_FORMAT));

dialog_->SelectFile(ui::SelectFileDialog::SELECT_OPEN_MULTI_FILE,
std::u16string(), profile_->last_selected_directory(),
&file_types, 0, base::FilePath::StringType(),
web_contents_->GetTopLevelNativeWindow(), nullptr);
}

void CustomImageChooser::FileSelected(const ui::SelectedFileInfo& file,
int index) {
dialog_ = nullptr;
profile_->set_last_selected_directory(file.path().DirName());
if (callback_) {
std::move(callback_).Run({file.path()});
}
}

void CustomImageChooser::MultiFilesSelected(
const std::vector<ui::SelectedFileInfo>& files) {
dialog_ = nullptr;
if (!files.empty()) {
profile_->set_last_selected_directory(files.back().path().DirName());
}
std::vector<base::FilePath> paths;
paths.reserve(files.size());
for (auto& file : files) {
paths.push_back(file.path());
}
if (callback_) {
std::move(callback_).Run(std::move(paths));
}
}

void CustomImageChooser::FileSelectionCanceled() {
dialog_ = nullptr;
if (callback_) {
std::move(callback_).Run({});
}
}

} // namespace brave_new_tab
53 changes: 53 additions & 0 deletions browser/ui/webui/brave_new_tab/custom_image_chooser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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_NEW_TAB_CUSTOM_IMAGE_CHOOSER_H_
#define BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_CUSTOM_IMAGE_CHOOSER_H_

#include <memory>
#include <vector>

#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "ui/shell_dialogs/select_file_dialog.h"

class Profile;

namespace content {
class WebContents;
}

namespace brave_new_tab {

class CustomImageChooser : public ui::SelectFileDialog::Listener {
public:
explicit CustomImageChooser(content::WebContents* web_contents);
~CustomImageChooser() override;

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

using ShowDialogCallback =
base::OnceCallback<void(std::vector<base::FilePath>)>;

void ShowDialog(ShowDialogCallback callback);

// ui::SelectFileDialog::Listener:
void FileSelected(const ui::SelectedFileInfo& file, int index) override;
void MultiFilesSelected(
const std::vector<ui::SelectedFileInfo>& files) override;
void FileSelectionCanceled() override;

private:
raw_ptr<content::WebContents> web_contents_ = nullptr;
raw_ptr<Profile> profile_ = nullptr;
scoped_refptr<ui::SelectFileDialog> dialog_;
ShowDialogCallback callback_;
};

} // namespace brave_new_tab

#endif // BRAVE_BROWSER_UI_WEBUI_BRAVE_NEW_TAB_CUSTOM_IMAGE_CHOOSER_H_
Loading

0 comments on commit e9bef8c

Please sign in to comment.