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 Jul 7, 2022
1 parent d9addf9 commit 964b84c
Show file tree
Hide file tree
Showing 108 changed files with 3,347 additions and 609 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 @@ -536,6 +541,11 @@ constexpr char kRestrictWebSocketsPoolDescription[] =
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,11 +10,13 @@
#include <utility>

#include "base/memory/raw_ptr.h"
#include "brave/browser/brave_rewards/rewards_panel_helper.h"
#include "brave/browser/brave_rewards/rewards_panel/rewards_panel_coordinator.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"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/storage_partition.h"
Expand All @@ -30,7 +32,20 @@ class CaptchaDelegate

bool ShowScheduledCaptcha(const std::string& payment_id,
const std::string& captcha_id) override {
return brave_rewards::ShowRewardsPanel(context_, true);
// Because this is triggered from the adaptive captcha tooltip, this call
// isn't associated with any particular `Browser` instance and we can use
// the last active browser for this profile.
auto* profile = Profile::FromBrowserContext(context_);
auto* browser = chrome::FindTabbedBrowser(profile, false);
if (!browser) {
return false;
}
auto* coordinator =
brave_rewards::RewardsPanelCoordinator::FromBrowser(browser);
if (!coordinator) {
return false;
}
return coordinator->ShowAdaptiveCaptcha();
}

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

#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/rewards_panel_coordinator.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)) {
DCHECK(profile_);
if (web_contents) {
browser_ = chrome::FindBrowserWithWebContents(web_contents);
DCHECK(browser_);
}
}

BraveAdsHost::~BraveAdsHost() {}
Expand Down Expand Up @@ -90,7 +83,7 @@ void BraveAdsHost::RequestAdsEnabled(RequestAdsEnabledCallback callback) {

rewards_service_observation_.Observe(rewards_service);

if (!ShowRewardsPopup(rewards_service)) {
if (!ShowRewardsPopup()) {
RunCallbacksAndReset(false);
}
}
Expand All @@ -109,34 +102,19 @@ 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) {
bool BraveAdsHost::ShowRewardsPopup() {
if (!browser_) {
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();
auto* coordinator =
brave_rewards::RewardsPanelCoordinator::FromBrowser(browser_);

rewards_service->StartProcess(base::DoNothing());
if (coordinator) {
return coordinator->ShowBraveTalkOptIn();
}

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

void BraveAdsHost::RunCallbacksAndReset(bool result) {
Expand Down
4 changes: 3 additions & 1 deletion browser/brave_ads/brave_ads_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "brave/components/brave_rewards/browser/rewards_service_observer.h"
#include "components/sessions/core/session_id.h"

class Browser;
class Profile;

namespace content {
Expand Down Expand Up @@ -45,10 +46,11 @@ class BraveAdsHost : public brave_ads::mojom::BraveAdsHost,
bool ads_enabled) override;

private:
bool ShowRewardsPopup(brave_rewards::RewardsService* rewards_service);
bool ShowRewardsPopup();
void RunCallbacksAndReset(bool result);

raw_ptr<Profile> profile_ = nullptr;
raw_ptr<Browser> browser_ = nullptr;
SessionID tab_id_;
std::vector<RequestAdsEnabledCallback> callbacks_;
base::ScopedObservation<brave_rewards::RewardsService,
Expand Down
11 changes: 5 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/rewards_panel_coordinator.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,13 +28,14 @@
#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*

namespace {

using brave_rewards::RewardsPanelCoordinator;

constexpr char kAllowedDomain[] = "talk.brave.com";
constexpr char kNotAllowedDomain[] = "brave.com";
constexpr char kBraveRequestAdsEnabledExists[] =
Expand Down Expand Up @@ -121,11 +123,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 RewardsPanelCoordinator::IsRewardsPanelURLForTesting(
(*popup_contents)->GetLastCommittedURL());
};

content::WindowedNotificationObserver popup_observer(
Expand Down
8 changes: 8 additions & 0 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,16 @@ 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/browser/ui/webui/private_new_tab_page/brave_private_new_tab_ui.h"
#include "brave/components/brave_new_tab_ui/brave_new_tab_page.mojom.h"
#include "brave/components/brave_private_new_tab_ui/common/brave_private_new_tab.mojom.h"
#include "brave/components/brave_rewards/common/brave_rewards_panel.mojom.h"
#include "brave/components/brave_rewards/common/features.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 @@ -563,6 +566,11 @@ void BraveContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
brave_private_new_tab::mojom::PageHandler, BravePrivateNewTabUI>(map);
chrome::internal::RegisterWebUIControllerInterfaceBinder<
brave_shields::mojom::PanelHandlerFactory, ShieldsPanelUI>(map);
if (base::FeatureList::IsEnabled(
brave_rewards::features::kWebUIPanelFeature)) {
chrome::internal::RegisterWebUIControllerInterfaceBinder<
brave_rewards::mojom::PanelHandlerFactory, RewardsPanelUI>(map);
}
if (base::FeatureList::IsEnabled(
brave_federated::features::kFederatedLearning)) {
chrome::internal::RegisterWebUIControllerInterfaceBinder<
Expand Down
49 changes: 49 additions & 0 deletions browser/brave_rewards/rewards_panel/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import("//extensions/buildflags/buildflags.gni")

source_set("rewards_panel") {
if (!is_android) {
sources = [
"rewards_panel_coordinator.cc",
"rewards_panel_coordinator.h",
]

deps = [
"//base",
"//brave/components/brave_rewards/browser",
"//brave/components/brave_rewards/common:features",
"//brave/components/brave_rewards/common:mojom",
"//brave/components/constants",
"//chrome/browser/profiles:profile",
"//chrome/browser/ui",
"//url",
]

if (enable_extensions) {
sources += [
"rewards_panel_extension_handler.cc",
"rewards_panel_extension_handler.h",
]

deps += [
"//brave/browser/extensions",
"//chrome/browser/extensions",
"//extensions/browser",
]
}
}
}

source_set("unit_tests") {
testonly = true

if (!is_android) {
sources = [ "rewards_panel_coordinator_unittest.cc" ]
deps = [
":rewards_panel",
"//base",
"//brave/components/brave_rewards/common:features",
"//chrome/browser/ui",
"//chrome/test:test_support",
]
}
}
Loading

0 comments on commit 964b84c

Please sign in to comment.