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 8, 2024
1 parent ba95b0a commit 58bd175
Show file tree
Hide file tree
Showing 53 changed files with 2,684 additions and 10 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 @@ -962,6 +963,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 @@ -824,6 +825,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
11 changes: 11 additions & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ 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_new_tab/custom_image_chooser.cc",
"webui/brave_new_tab/custom_image_chooser.h",
"webui/brave_new_tab/new_tab_page_handler.cc",
"webui/brave_new_tab/new_tab_page_handler.h",
"webui/brave_new_tab/new_tab_ui.cc",
"webui/brave_new_tab/new_tab_ui.h",
"webui/brave_new_tab/update_observer.cc",
"webui/brave_new_tab/update_observer.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 Down Expand Up @@ -327,6 +335,9 @@ source_set("ui") {
"//base",
"//brave/browser/profiles:util",
"//brave/common",
"//brave/components/brave_new_tab/common",
"//brave/components/brave_new_tab/common:mojom",
"//brave/components/brave_new_tab/resources:generated_resources",
"//brave/components/constants",
"//brave/components/misc_metrics",
"//brave/components/sidebar/browser",
Expand Down
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 58bd175

Please sign in to comment.