Skip to content

Commit

Permalink
Merge pull request #3624 from brave/rewards-widget
Browse files Browse the repository at this point in the history
Adding Rewards Widget for New Tab Page
  • Loading branch information
ryanml authored Oct 31, 2019
2 parents ce26c1b + 7c27e45 commit 763fc7e
Show file tree
Hide file tree
Showing 47 changed files with 1,545 additions and 32 deletions.
1 change: 1 addition & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(kNewTabPageShowClock, true);
registry->RegisterBooleanPref(kNewTabPageShowTopSites, true);
registry->RegisterBooleanPref(kNewTabPageShowStats, true);
registry->RegisterBooleanPref(kNewTabPageShowRewards, true);

// Brave Wallet
registry->RegisterStringPref(kBraveWalletAES256GCMSivNonce, "");
Expand Down
116 changes: 116 additions & 0 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -968,5 +968,121 @@ BraveRewardsOnlyAnonWalletFunction::Run() {
return RespondNow(OneArgument(std::make_unique<base::Value>(only)));
}

BraveRewardsGetAdsEnabledFunction::
~BraveRewardsGetAdsEnabledFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsGetAdsEnabledFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
AdsService* ads_service_ =
AdsServiceFactory::GetForProfile(profile);

if (!ads_service_) {
return RespondNow(Error("Ads service is not initialized"));
}

const bool enabled = ads_service_->IsEnabled();
return RespondNow(
OneArgument(std::make_unique<base::Value>(enabled)));
}

BraveRewardsGetAdsEstimatedEarningsFunction::
~BraveRewardsGetAdsEstimatedEarningsFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsGetAdsEstimatedEarningsFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
RewardsService* rewards_service =
RewardsServiceFactory::GetForProfile(profile);

if (!rewards_service) {
return RespondNow(Error("Rewards service is not initialized"));
}

rewards_service->GetTransactionHistory(base::Bind(
&BraveRewardsGetAdsEstimatedEarningsFunction::OnAdsEstimatedEarnings,
this));
return RespondLater();
}

void BraveRewardsGetAdsEstimatedEarningsFunction::OnAdsEstimatedEarnings(
const double estimated_pending_rewards,
const uint64_t next_payment_date_in_seconds,
const uint64_t ad_notifications_received_this_month) {
Respond(OneArgument(
std::make_unique<base::Value>(estimated_pending_rewards)));
}

BraveRewardsGetBalanceReportsFunction::
~BraveRewardsGetBalanceReportsFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsGetBalanceReportsFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
auto* rewards_service_ = RewardsServiceFactory::GetForProfile(profile);

if (!rewards_service_) {
return RespondNow(Error("Rewards service is not not initialized"));
}

rewards_service_->GetAllBalanceReports(base::Bind(
&BraveRewardsGetBalanceReportsFunction::OnGetBalanceReports,
this));

return RespondLater();
}

void BraveRewardsGetBalanceReportsFunction::OnGetBalanceReports(
const std::map<std::string,
::brave_rewards::BalanceReport>& reports) {
std::unique_ptr<base::DictionaryValue> data(
new base::DictionaryValue());

if (!reports.empty()) {
for (auto const& report : reports) {
const ::brave_rewards::BalanceReport old_report = report.second;
auto new_report = std::make_unique<base::DictionaryValue>();
new_report->SetString("grant", old_report.grants);
new_report->SetString("deposit", old_report.deposits);
new_report->SetString("ads", old_report.earning_from_ads);
new_report->SetString("contribute", old_report.auto_contribute);
new_report->SetString("donation", old_report.recurring_donation);
new_report->SetString("tips", old_report.one_time_donation);
new_report->SetString("total", old_report.total);
data->SetDictionary(report.first, std::move(new_report));
}
}

Respond(OneArgument(std::move(data)));
}

BraveRewardsGetWalletExistsFunction::
~BraveRewardsGetWalletExistsFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsGetWalletExistsFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
RewardsService* rewards_service =
RewardsServiceFactory::GetForProfile(profile);

if (!rewards_service) {
return RespondNow(Error("Rewards service is not initialized"));
}

rewards_service->IsWalletCreated(base::Bind(
&BraveRewardsGetWalletExistsFunction::OnGetWalletExists,
this));
return RespondLater();
}

void BraveRewardsGetWalletExistsFunction::OnGetWalletExists(
const bool exists) {
Respond(OneArgument(std::make_unique<base::Value>(exists)));
}

} // namespace api
} // namespace extensions
59 changes: 59 additions & 0 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef BRAVE_BROWSER_EXTENSIONS_API_BRAVE_REWARDS_API_H_
#define BRAVE_BROWSER_EXTENSIONS_API_BRAVE_REWARDS_API_H_

#include <map>
#include <memory>
#include <string>

Expand All @@ -14,6 +15,7 @@
#include "brave/components/brave_rewards/browser/content_site.h"
#include "brave/components/brave_rewards/browser/external_wallet.h"
#include "brave/components/brave_rewards/browser/publisher_banner.h"
#include "brave/components/brave_rewards/browser/balance_report.h"
#include "extensions/browser/extension_function.h"

namespace extensions {
Expand Down Expand Up @@ -371,6 +373,63 @@ class BraveRewardsOnlyAnonWalletFunction : public ExtensionFunction {
ResponseAction Run() override;
};

class BraveRewardsGetAdsEnabledFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getAdsEnabled", UNKNOWN)

protected:
~BraveRewardsGetAdsEnabledFunction() override;

ResponseAction Run() override;
};

class BraveRewardsGetAdsEstimatedEarningsFunction
: public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getAdsEstimatedEarnings", UNKNOWN)

protected:
~BraveRewardsGetAdsEstimatedEarningsFunction() override;

ResponseAction Run() override;

private:
void OnAdsEstimatedEarnings(
const double estimated_pending_rewards,
const uint64_t next_payment_date_in_seconds,
const uint64_t ad_notifications_received_this_month);
};

class BraveRewardsGetBalanceReportsFunction
: public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getBalanceReports", UNKNOWN)

protected:
~BraveRewardsGetBalanceReportsFunction() override;

ResponseAction Run() override;

private:
void OnGetBalanceReports(
const std::map<std::string,
::brave_rewards::BalanceReport>& reports);
};

class BraveRewardsGetWalletExistsFunction
: public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getWalletExists", UNKNOWN)

protected:
~BraveRewardsGetWalletExistsFunction() override;

ResponseAction Run() override;

private:
void OnGetWalletExists(const bool exists);
};

} // namespace api
} // namespace extensions

Expand Down
8 changes: 8 additions & 0 deletions browser/ui/webui/brave_new_tab_message_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ base::DictionaryValue GetPreferencesDictionary(PrefService* prefs) {
pref_data.SetBoolean(
"showStats",
prefs->GetBoolean(kNewTabPageShowStats));
pref_data.SetBoolean(
"showRewards",
prefs->GetBoolean(kNewTabPageShowRewards));
return pref_data;
}

Expand Down Expand Up @@ -168,6 +171,9 @@ void BraveNewTabMessageHandler::OnJavascriptAllowed() {
pref_change_registrar_.Add(kNewTabPageShowTopSites,
base::Bind(&BraveNewTabMessageHandler::OnPreferencesChanged,
base::Unretained(this)));
pref_change_registrar_.Add(kNewTabPageShowRewards,
base::Bind(&BraveNewTabMessageHandler::OnPreferencesChanged,
base::Unretained(this)));
}

void BraveNewTabMessageHandler::OnJavascriptDisallowed() {
Expand Down Expand Up @@ -229,6 +235,8 @@ void BraveNewTabMessageHandler::HandleSaveNewTabPagePref(
settingsKey = kNewTabPageShowTopSites;
} else if (settingsKeyInput == "showStats") {
settingsKey = kNewTabPageShowStats;
} else if (settingsKeyInput == "showRewards") {
settingsKey = kNewTabPageShowRewards;
} else {
LOG(ERROR) << "Invalid setting key";
return;
Expand Down
15 changes: 15 additions & 0 deletions browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ class RewardsDOMHandler : public WebUIMessageHandler,
int32_t result,
const std::string& wallet_type) override;

void OnAdsEnabled(
brave_rewards::RewardsService* rewards_service,
bool ads_enabled) override;

// RewardsNotificationsServiceObserver implementation
void OnNotificationAdded(
brave_rewards::RewardsNotificationService* rewards_notification_service,
Expand Down Expand Up @@ -1646,6 +1650,17 @@ void RewardsDOMHandler::OnDisconnectWallet(
data);
}

void RewardsDOMHandler::OnAdsEnabled(
brave_rewards::RewardsService* rewards_service,
bool ads_enabled) {
if (!web_ui()->CanCallJavascript()) {
return;
}

base::ListValue* emptyArgs = nullptr;
GetAdsData(emptyArgs);
}

void RewardsDOMHandler::OnlyAnonWallet(const base::ListValue* args) {
if (!rewards_service_) {
return;
Expand Down
27 changes: 26 additions & 1 deletion browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "showBraveStats", IDS_BRAVE_NEW_TAB_SHOW_BRAVE_STATS },
{ "showClock", IDS_BRAVE_NEW_TAB_SHOW_CLOCK },
{ "showTopSites", IDS_BRAVE_NEW_TAB_SHOW_TOP_SITES },
{ "showRewards", IDS_BRAVE_NEW_TAB_SHOW_REWARDS },

// Private Tab - General
{ "learnMore", IDS_BRAVE_PRIVATE_NEW_TAB_LEARN_MORE },
Expand Down Expand Up @@ -189,7 +190,31 @@ void CustomizeWebUIHTMLSource(const std::string &name,

// Private Tab - Private Window - Tor Box
{ "boxTorText2", IDS_BRAVE_PRIVATE_NEW_TAB_BOX_TOR_TEXT_2 },
{ "boxTorButton", IDS_BRAVE_PRIVATE_NEW_TAB_BOX_TOR_BUTTON }
{ "boxTorButton", IDS_BRAVE_PRIVATE_NEW_TAB_BOX_TOR_BUTTON },

// Rewards widget
{ "rewardsWidgetAnd", IDS_BRAVE_UI_AND },
{ "rewardsWidgetBat", IDS_BRAVE_UI_BAT_REWARDS_TEXT },
{ "rewardsWidgetBatPoints", IDS_BRAVE_UI_BAT_POINTS_TEXT },
{ "rewardsWidgetBraveRewards", IDS_BRAVE_UI_BRAVE_REWARDS },
{ "rewardsWidgetPrivacyPolicy", IDS_BRAVE_UI_PRIVACY_POLICY },
{ "rewardsWidgetTermsOfService", IDS_BRAVE_UI_TERMS_OF_SERVICE },
{ "rewardsWidgetTurnOnAds", IDS_BRAVE_UI_TURN_ON_ADS },
{ "rewardsWidgetClaimMyRewards", IDS_REWARDS_WIDGET_CLAIM_MY_REWARDS },
{ "rewardsWidgetWalletFailedButton", IDS_BRAVE_UI_WALLET_FAILED_BUTTON }, // NOLINT
{ "rewardsWidgetAboutRewards", IDS_REWARDS_WIDGET_ABOUT_REWARDS },
{ "rewardsWidgetServiceText", IDS_REWARDS_WIDGET_SERVICE_TEXT },
{ "rewardsWidgetEstimatedEarnings", IDS_REWARDS_WIDGET_ESTIMATED_EARNINGS }, // NOLINT
{ "rewardsWidgetMonthlyTips", IDS_REWARDS_WIDGET_MONTHLY_TIPS },
{ "rewardsWidgetTurningOn", IDS_REWARDS_WIDGET_TURNING_ON },
{ "rewardsWidgetTurnOnRewards", IDS_REWARDS_WIDGET_TURN_ON_REWARDS }, // NOLINT
{ "rewardsWidgetReEnableTitle", IDS_REWARDS_WIDGET_REENABLE_TITLE }, // NOLINT
{ "rewardsWidgetEnableTitle", IDS_REWARDS_WIDGET_ENABLE_TITLE },
{ "rewardsWidgetReEnableSubTitle", IDS_REWARDS_WIDGET_REENABLE_SUBTITLE }, // NOLINT
{ "rewardsWidgetEnableSubTitle", IDS_REWARDS_WIDGET_ENABLE_SUBTITLE }, // NOLINT
{ "rewardsWidgetNotificationTitle", IDS_REWARDS_WIDGET_NOTIFICATION_TITLE }, // NOLINT
{ "rewardsWidgetNotificationTextAds", IDS_REWARDS_WIDGET_NOTIFICATION_TEXT_ADS }, // NOLINT
{ "rewardsWidgetNotificationTextUGP", IDS_REWARDS_WIDGET_NOTIFICATION_TEXT_UGP } // NOLINT
}
}, {
std::string("welcome"), {
Expand Down
25 changes: 17 additions & 8 deletions common/extensions/api/_api_features.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,23 @@
"contexts": ["blessed_extension"],
"whitelist": ["3D9518A72EB02667A773B69DBA9E72E0F4A37423", "780BF954C0F7C586EA9662D4F967771F49CC2114", "FF32507DC3DB5DFFD1D6733187C84D4B74713D63"]
},
"braveRewards": {
"channel": "stable",
"dependencies": [],
"contexts": ["blessed_extension"],
"whitelist": [
"46E9817CBF915C0D1F6BCCF916C42CC666FF1D64"
]
},
"braveRewards": [
{
"channel": "stable",
"dependencies": [],
"contexts": ["blessed_extension"],
"whitelist": [
"46E9817CBF915C0D1F6BCCF916C42CC666FF1D64"
]
},
{
"channel": "stable",
"contexts": ["webui"],
"matches": [
"chrome://newtab/*"
]
}
],
"rewardsNotifications": {
"channel": "stable",
"dependencies": [],
Expand Down
Loading

0 comments on commit 763fc7e

Please sign in to comment.