Skip to content

Commit

Permalink
Remove dependency on the Rewards extension
Browse files Browse the repository at this point in the history
  • Loading branch information
zenparsing committed Jun 9, 2022
1 parent e2062a8 commit 5268471
Show file tree
Hide file tree
Showing 98 changed files with 3,086 additions and 685 deletions.
1 change: 1 addition & 0 deletions app/vector_icons/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ aggregate_vector_icons("brave_vector_icons") {
icon_directory = "."

sources = [
"bat.icon",
"brave_ads_close_button.icon",
"brave_ads_dark_mode_info_button.icon",
"brave_ads_light_mode_info_button.icon",
Expand Down
39 changes: 39 additions & 0 deletions app/vector_icons/bat.icon
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
CANVAS_DIMENSIONS, 16,
PATH_COLOR_ARGB, 0xFF, 0xFF, 0x47, 0x24,
MOVE_TO, 0.03f, 14.56f,
R_LINE_TO, 4.68f, -2.75f,
LINE_TO, 7.94f, 6.15f,
V_LINE_TO, 0.69f,
R_CUBIC_TO, -0.09f, 0, -0.17f, 0.06f, -0.23f, 0.17f,
LINE_TO, 3.88f, 7.57f,
LINE_TO, 0.06f, 14.27f,
R_CUBIC_TO, -0.06f, 0.11f, -0.07f, 0.22f, -0.03f, 0.29f,
CLOSE,
NEW_PATH,
PATH_COLOR_ARGB, 0xFF, 0x9E, 0x1F, 0x63,
MOVE_TO, 7.94f, 0.69f,
V_LINE_TO, 6.15f,
R_LINE_TO, 3.23f, 5.66f,
R_LINE_TO, 4.68f, 2.75f,
R_CUBIC_TO, 0.04f, -0.07f, 0.04f, -0.18f, -0.03f, -0.29f,
R_LINE_TO, -3.83f, -6.7f,
LINE_TO, 8.18f, 0.86f,
R_CUBIC_TO, -0.06f, -0.11f, -0.15f, -0.17f, -0.23f, -0.17f,
CLOSE,
NEW_PATH,
PATH_COLOR_ARGB, 0xFF, 0x66, 0x2D, 0x91,
MOVE_TO, 15.85f, 14.56f,
R_LINE_TO, -4.68f, -2.75f,
H_LINE_TO, 4.71f,
LINE_TO, 0.03f, 14.56f,
R_CUBIC_TO, 0.04f, 0.08f, 0.13f, 0.12f, 0.26f, 0.12f,
H_LINE_TO, 15.59f,
R_CUBIC_TO, 0.13f, 0, 0.22f, -0.05f, 0.26f, -0.12f,
CLOSE,
NEW_PATH,
PATH_COLOR_ARGB, 0xFF, 0xFF, 0xFF, 0xFF,
MOVE_TO, 4.71f, 11.81f,
R_H_LINE_TO, 6.46f,
LINE_TO, 7.94f, 6.15f,
LINE_TO, 4.71f, 11.81f,
CLOSE
10 changes: 10 additions & 0 deletions browser/about_flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ constexpr char kBraveRewardsGeminiDescription[] =
"Enables support for Gemini as an external wallet provider for Brave";
#endif

constexpr char kBraveRewardsWebUIPanelName[] = "Use WebUI Rewards Panel";
constexpr char kBraveRewardsWebUIPanelDescription[] =
"When enabled, the Brave Rewards panel will be displayed using WebUI "
"instead of the built-in Rewards extension.";

constexpr char kBraveRewardsVerboseLoggingName[] =
"Enable Brave Rewards verbose logging";
constexpr char kBraveRewardsVerboseLoggingDescription[] =
Expand Down Expand Up @@ -548,6 +553,11 @@ const flags_ui::FeatureEntry::Choice kBraveSkusEnvChoices[] = {
flag_descriptions::kBraveRewardsVerboseLoggingDescription, \
kOsDesktop | kOsAndroid, \
FEATURE_VALUE_TYPE(brave_rewards::features::kVerboseLoggingFeature)}, \
{"brave-rewards-webui-panel", \
flag_descriptions::kBraveRewardsWebUIPanelName, \
flag_descriptions::kBraveRewardsWebUIPanelDescription, \
kOsDesktop, \
FEATURE_VALUE_TYPE(brave_rewards::features::kWebUIPanelFeature)}, \
{"brave-ads-custom-push-notifications-ads", \
flag_descriptions::kBraveAdsCustomNotificationsName, \
flag_descriptions::kBraveAdsCustomNotificationsDescription, \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#include <utility>

#include "base/memory/raw_ptr.h"
#include "brave/browser/brave_rewards/rewards_panel_helper.h"
#include "brave/browser/brave_rewards/rewards_panel_service.h"
#include "brave/browser/brave_rewards/rewards_panel_service_factory.h"
#include "brave/browser/brave_rewards/rewards_service_factory.h"
#include "brave/browser/profiles/profile_util.h"
#include "brave/components/brave_adaptive_captcha/brave_adaptive_captcha_service.h"
Expand All @@ -30,7 +31,10 @@ class CaptchaDelegate

bool ShowScheduledCaptcha(const std::string& payment_id,
const std::string& captcha_id) override {
return brave_rewards::ShowRewardsPanel(context_, true);
auto* service = brave_rewards::RewardsPanelServiceFactory::GetForProfile(
Profile::FromBrowserContext(context_));

return service && service->ShowAdaptiveCaptcha();
}

private:
Expand Down
55 changes: 7 additions & 48 deletions browser/brave_ads/brave_ads_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,23 @@

#include "brave/browser/brave_ads/brave_ads_host.h"

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

#include "brave/browser/brave_ads/ads_service_factory.h"
#include "brave/browser/brave_ads/search_result_ad/search_result_ad_service_factory.h"
#include "brave/browser/brave_rewards/rewards_panel_service.h"
#include "brave/browser/brave_rewards/rewards_panel_service_factory.h"
#include "brave/browser/brave_rewards/rewards_service_factory.h"
#include "brave/browser/extensions/api/brave_action_api.h"
#include "brave/browser/extensions/brave_component_loader.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/content/browser/search_result_ad/search_result_ad_service.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "components/sessions/content/session_tab_helper.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/constants.h"

namespace brave_ads {

namespace {

constexpr char kAdsEnableRelativeUrl[] = "request_ads_enabled_panel.html";

} // namespace

BraveAdsHost::BraveAdsHost(Profile* profile, content::WebContents* web_contents)
: profile_(profile),
tab_id_(sessions::SessionTabHelper::IdForTab(web_contents)) {
Expand Down Expand Up @@ -71,16 +59,17 @@ void BraveAdsHost::RequestAdsEnabled(RequestAdsEnabledCallback callback) {
const AdsService* ads_service = AdsServiceFactory::GetForProfile(profile_);
brave_rewards::RewardsService* rewards_service =
brave_rewards::RewardsServiceFactory::GetForProfile(profile_);
if (!rewards_service || !ads_service || !ads_service->IsSupportedLocale()) {
auto* panel_service =
brave_rewards::RewardsPanelServiceFactory::GetForProfile(profile_);
if (!rewards_service || !panel_service || !ads_service ||
!ads_service->IsSupportedLocale()) {
std::move(callback).Run(false);
return;
}

if (ads_service->IsEnabled()) {
std::move(callback).Run(true);
return;
}

if (!callbacks_.empty()) {
callbacks_.push_back(std::move(callback));
return;
Expand All @@ -90,7 +79,7 @@ void BraveAdsHost::RequestAdsEnabled(RequestAdsEnabledCallback callback) {

rewards_service_observation_.Observe(rewards_service);

if (!ShowRewardsPopup(rewards_service)) {
if (!panel_service->ShowBraveTalkOptIn()) {
RunCallbacksAndReset(false);
}
}
Expand All @@ -109,36 +98,6 @@ void BraveAdsHost::OnAdsEnabled(brave_rewards::RewardsService* rewards_service,
RunCallbacksAndReset(ads_enabled);
}

bool BraveAdsHost::ShowRewardsPopup(
brave_rewards::RewardsService* rewards_service) {
DCHECK(rewards_service);

Browser* browser = chrome::FindBrowserWithProfile(profile_);
DCHECK(browser);

auto* extension_service =
extensions::ExtensionSystem::Get(profile_)->extension_service();
if (!extension_service) {
return false;
}

extensions::BraveComponentLoader* component_loader =
static_cast<extensions::BraveComponentLoader*>(
extension_service->component_loader());
DCHECK(component_loader);

if (!component_loader->Exists(brave_rewards_extension_id)) {
component_loader->AddRewardsExtension();

rewards_service->StartProcess(base::DoNothing());
}

std::string error;
return extensions::BraveActionAPI::ShowActionUI(
browser, brave_rewards_extension_id,
std::make_unique<std::string>(kAdsEnableRelativeUrl), &error);
}

void BraveAdsHost::RunCallbacksAndReset(bool result) {
DCHECK(!callbacks_.empty());

Expand Down
9 changes: 3 additions & 6 deletions browser/brave_ads/request_ads_enabled_api_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "base/test/scoped_feature_list.h"
#include "bat/ads/pref_names.h"
#include "brave/browser/brave_ads/ads_service_factory.h"
#include "brave/browser/brave_rewards/rewards_panel_service.h"
#include "brave/browser/brave_rewards/rewards_service_factory.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/common/features.h"
Expand All @@ -27,7 +28,6 @@
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_mock_cert_verifier.h"
#include "extensions/common/constants.h"
#include "net/dns/mock_host_resolver.h"

// npm run test -- brave_browser_tests --filter=RequestAdsEnabledApiTest*
Expand Down Expand Up @@ -121,11 +121,8 @@ class RequestAdsEnabledApiTestBase : public InProcessBrowserTest {

// Check that this notification is for the Rewards panel and not, say,
// the extension background page.
std::string url = (*popup_contents)->GetLastCommittedURL().spec();
std::string rewards_panel_url = std::string("chrome-extension://") +
brave_rewards_extension_id +
"/request_ads_enabled_panel.html";
return url == rewards_panel_url;
return brave_rewards::RewardsPanelService::IsRewardsPanelURLForTesting(
(*popup_contents)->GetLastCommittedURL());
};

content::WindowedNotificationObserver popup_observer(
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 @@ -192,11 +192,13 @@ using extensions::ChromeContentBrowserClientExtensionsPart;
#include "brave/browser/new_tab/new_tab_shows_navigation_throttle.h"
#include "brave/browser/ui/webui/brave_federated/federated_internals.mojom.h"
#include "brave/browser/ui/webui/brave_federated/federated_internals_ui.h"
#include "brave/browser/ui/webui/brave_rewards/rewards_panel_ui.h"
#include "brave/browser/ui/webui/brave_shields/shields_panel_ui.h"
#include "brave/browser/ui/webui/brave_wallet/wallet_page_ui.h"
#include "brave/browser/ui/webui/brave_wallet/wallet_panel_ui.h"
#include "brave/browser/ui/webui/new_tab_page/brave_new_tab_ui.h"
#include "brave/components/brave_new_tab_ui/brave_new_tab_page.mojom.h"
#include "brave/components/brave_rewards/common/brave_rewards_panel.mojom.h"
#include "brave/components/brave_shields/common/brave_shields_panel.mojom.h"
#include "brave/components/brave_today/common/brave_news.mojom.h"
#include "brave/components/brave_today/common/features.h"
Expand Down Expand Up @@ -540,6 +542,8 @@ void BraveContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
chrome::internal::RegisterWebUIControllerInterfaceBinder<
brave_shields::mojom::PanelHandlerFactory, ShieldsPanelUI>(map);
}
chrome::internal::RegisterWebUIControllerInterfaceBinder<
brave_rewards::mojom::PanelHandlerFactory, RewardsPanelUI>(map);
if (base::FeatureList::IsEnabled(
brave_federated::features::kFederatedLearning)) {
chrome::internal::RegisterWebUIControllerInterfaceBinder<
Expand Down
85 changes: 85 additions & 0 deletions browser/brave_rewards/rewards_extension_panel_handler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* Copyright (c) 2022 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 http://mozilla.org/MPL/2.0/. */

#include "brave/browser/brave_rewards/rewards_extension_panel_handler.h"

#include <memory>
#include <string>

#include "base/strings/strcat.h"
#include "brave/browser/brave_rewards/rewards_service_factory.h"
#include "brave/browser/extensions/brave_component_loader.h"
#include "brave/browser/ui/views/brave_actions/brave_actions_container.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "extensions/browser/extension_system.h"

namespace brave_rewards {

namespace {

constexpr char kRewardsPanelUrl[] = "/brave_rewards_panel.html";
constexpr char kAdsEnableRelativeUrl[] = "/request_ads_enabled_panel.html";

std::string GetExtensionPath(const mojom::RewardsPanelArgs& args) {
switch (args.view) {
case mojom::RewardsPanelView::kDefault:
return kRewardsPanelUrl;
case mojom::RewardsPanelView::kRewardsTour:
return base::StrCat({kRewardsPanelUrl, "#tour"});
case mojom::RewardsPanelView::kGrantCaptcha:
return base::StrCat({kRewardsPanelUrl, "#grant_", args.data});
case mojom::RewardsPanelView::kAdaptiveCaptcha:
return base::StrCat({kRewardsPanelUrl, "#load_adaptive_captcha"});
case mojom::RewardsPanelView::kBraveTalkOptIn:
return kAdsEnableRelativeUrl;
}
}

} // namespace

RewardsExtensionPanelHandler::~RewardsExtensionPanelHandler() = default;

bool RewardsExtensionPanelHandler::IsRewardsExtensionPanelURL(const GURL& url) {
return url.SchemeIs("chrome-extension") &&
url.host() == brave_rewards_extension_id &&
(url.path() == kRewardsPanelUrl ||
url.path() == kAdsEnableRelativeUrl);
}

void RewardsExtensionPanelHandler::OnRewardsPanelRequested(
Browser* browser,
const mojom::RewardsPanelArgs& args) {
DCHECK(browser);
auto* profile = browser->profile();

// Start the rewards ledger process if it is not already started
auto* rewards_service = RewardsServiceFactory::GetForProfile(profile);
if (!rewards_service) {
return;
}

rewards_service->StartProcess(base::DoNothing());

// Load the rewards extension if it is not already loaded.
auto* extension_service =
extensions::ExtensionSystem::Get(profile)->extension_service();
if (!extension_service) {
return;
}

static_cast<extensions::BraveComponentLoader*>(
extension_service->component_loader())
->AddRewardsExtension();

std::string error;
extensions::BraveActionAPI::ShowActionUI(
browser, brave_rewards_extension_id,
std::make_unique<std::string>(GetExtensionPath(args)), &error);
}

} // namespace brave_rewards
28 changes: 28 additions & 0 deletions browser/brave_rewards/rewards_extension_panel_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright (c) 2022 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 http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_BRAVE_REWARDS_REWARDS_EXTENSION_PANEL_HANDLER_H_
#define BRAVE_BROWSER_BRAVE_REWARDS_REWARDS_EXTENSION_PANEL_HANDLER_H_

#include "brave/browser/brave_rewards/rewards_panel_service.h"
#include "url/gurl.h"

namespace brave_rewards {

// A `RewardsPanelService` observer that loads the Rewards extension if required
// and dispatches panel requests to the extension.
class RewardsExtensionPanelHandler : public RewardsPanelService::Observer {
public:
~RewardsExtensionPanelHandler() override;

static bool IsRewardsExtensionPanelURL(const GURL& url);

void OnRewardsPanelRequested(Browser* browser,
const mojom::RewardsPanelArgs& args) override;
};

} // namespace brave_rewards

#endif // BRAVE_BROWSER_BRAVE_REWARDS_REWARDS_EXTENSION_PANEL_HANDLER_H_
Loading

0 comments on commit 5268471

Please sign in to comment.