Skip to content

Commit

Permalink
Merge pull request #15199 from brave/ksmith-country-select
Browse files Browse the repository at this point in the history
Add country selection to Rewards onboarding
  • Loading branch information
zenparsing authored Oct 5, 2022
2 parents db7bbae + d53be29 commit 9629bac
Show file tree
Hide file tree
Showing 72 changed files with 1,348 additions and 310 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void BraveRewardsNativeWorker::Destroy(JNIEnv* env) {

void BraveRewardsNativeWorker::CreateRewardsWallet(JNIEnv* env) {
if (brave_rewards_service_) {
brave_rewards_service_->CreateRewardsWallet(base::DoNothing());
brave_rewards_service_->CreateRewardsWallet("", base::DoNothing());
}
}

Expand Down
69 changes: 50 additions & 19 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ RewardsPanelCoordinator* GetPanelCoordinator(ExtensionFunction* function) {
return browser ? RewardsPanelCoordinator::FromBrowser(browser) : nullptr;
}

std::string StringifyResult(ledger::mojom::CreateRewardsWalletResult result) {
switch (result) {
case ledger::mojom::CreateRewardsWalletResult::kSuccess:
return "success";
case ledger::mojom::CreateRewardsWalletResult::kWalletGenerationDisabled:
return "wallet-generation-disabled";
case ledger::mojom::CreateRewardsWalletResult::kGeoCountryAlreadyDeclared:
return "country-already-declared";
case ledger::mojom::CreateRewardsWalletResult::kUnexpected:
return "unexpected-error";
}
}

} // namespace

namespace extensions {
Expand Down Expand Up @@ -587,47 +600,65 @@ BraveRewardsCreateRewardsWalletFunction::

ExtensionFunction::ResponseAction
BraveRewardsCreateRewardsWalletFunction::Run() {
auto params = brave_rewards::CreateRewardsWallet::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);

auto* profile = Profile::FromBrowserContext(browser_context());
auto* rewards_service = RewardsServiceFactory::GetForProfile(profile);
if (!rewards_service) {
return RespondNow(Error("RewardsService not available"));
}

rewards_service->CreateRewardsWallet(base::BindOnce(
&BraveRewardsCreateRewardsWalletFunction::CreateRewardsWalletCallback,
this));
rewards_service->CreateRewardsWallet(
params->country,
base::BindOnce(
&BraveRewardsCreateRewardsWalletFunction::CreateRewardsWalletCallback,
this));

return RespondLater();
}

void BraveRewardsCreateRewardsWalletFunction::CreateRewardsWalletCallback(
ledger::mojom::Result result) {
Respond(OneArgument(base::Value(static_cast<int>(result))));
ledger::mojom::CreateRewardsWalletResult result) {
Respond(OneArgument(base::Value(StringifyResult(result))));
}

BraveRewardsGetRewardsWalletFunction::~BraveRewardsGetRewardsWalletFunction() =
default;
BraveRewardsGetAvailableCountriesFunction::
~BraveRewardsGetAvailableCountriesFunction() = default;

ExtensionFunction::ResponseAction BraveRewardsGetRewardsWalletFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
ExtensionFunction::ResponseAction
BraveRewardsGetAvailableCountriesFunction::Run() {
auto* profile = Profile::FromBrowserContext(browser_context());
auto* rewards_service = RewardsServiceFactory::GetForProfile(profile);

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

rewards_service->GetRewardsWallet(base::BindOnce(
&BraveRewardsGetRewardsWalletFunction::GetRewardsWalletCallback, this));
rewards_service->GetAvailableCountries(base::BindOnce(
&BraveRewardsGetAvailableCountriesFunction::GetAvailableCountriesCallback,
this));

return RespondLater();
}

void BraveRewardsGetRewardsWalletFunction::GetRewardsWalletCallback(
ledger::mojom::RewardsWalletPtr rewards_wallet) {
if (!rewards_wallet) {
return Respond(NoArguments());
void BraveRewardsGetAvailableCountriesFunction::GetAvailableCountriesCallback(
std::vector<std::string> countries) {
base::Value::List country_list;
for (auto& country : countries) {
country_list.Append(std::move(country));
}
base::Value::Dict dict;
dict.Set("paymentId", rewards_wallet->payment_id);
Respond(OneArgument(base::Value(std::move(dict))));
Respond(OneArgument(base::Value(std::move(country_list))));
}

BraveRewardsGetDeclaredCountryFunction::
~BraveRewardsGetDeclaredCountryFunction() = default;

ExtensionFunction::ResponseAction
BraveRewardsGetDeclaredCountryFunction::Run() {
auto* prefs = Profile::FromBrowserContext(browser_context())->GetPrefs();
std::string country = prefs->GetString(::brave_rewards::prefs::kDeclaredGeo);
return RespondNow(OneArgument(base::Value(std::move(country))));
}

BraveRewardsGetBalanceReportFunction::~BraveRewardsGetBalanceReportFunction() =
Expand Down
21 changes: 15 additions & 6 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,20 +191,29 @@ class BraveRewardsCreateRewardsWalletFunction : public ExtensionFunction {
ResponseAction Run() override;

private:
void CreateRewardsWalletCallback(ledger::mojom::Result result);
void CreateRewardsWalletCallback(
ledger::mojom::CreateRewardsWalletResult result);
};

class BraveRewardsGetRewardsWalletFunction : public ExtensionFunction {
class BraveRewardsGetAvailableCountriesFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getRewardsWallet", UNKNOWN)
DECLARE_EXTENSION_FUNCTION("braveRewards.getAvailableCountries", UNKNOWN)

protected:
~BraveRewardsGetRewardsWalletFunction() override;
~BraveRewardsGetAvailableCountriesFunction() override;

private:
void GetAvailableCountriesCallback(std::vector<std::string> countries);

ResponseAction Run() override;
};

private:
void GetRewardsWalletCallback(ledger::mojom::RewardsWalletPtr rewards_wallet);
class BraveRewardsGetDeclaredCountryFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getDeclaredCountry", UNKNOWN)
protected:
~BraveRewardsGetDeclaredCountryFunction() override;
ResponseAction Run() override;
};

class BraveRewardsGetBalanceReportFunction : public ExtensionFunction {
Expand Down
21 changes: 19 additions & 2 deletions browser/ui/views/brave_actions/brave_rewards_action_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ BraveRewardsActionView::BraveRewardsActionView(Browser* browser)
brave_rewards::prefs::kBadgeText,
base::BindRepeating(&BraveRewardsActionView::OnPreferencesChanged,
base::Unretained(this)));
pref_change_registrar_.Add(
brave_rewards::prefs::kDeclaredGeo,
base::BindRepeating(&BraveRewardsActionView::OnPreferencesChanged,
base::Unretained(this)));

browser_->tab_strip_model()->AddObserver(this);

Expand Down Expand Up @@ -361,8 +365,21 @@ BraveRewardsActionView::GetBadgeTextAndBackground() {
}

size_t BraveRewardsActionView::GetRewardsNotificationCount() {
auto* service = GetNotificationService();
return service ? service->GetAllNotifications().size() : 0;
size_t count = 0;

if (auto* service = GetNotificationService()) {
count += service->GetAllNotifications().size();
}

// Increment the notification count if the user has enabled Rewards but has
// not declared a country.
auto* prefs = browser_->profile()->GetPrefs();
if (prefs->GetBoolean(brave_rewards::prefs::kEnabled) &&
prefs->GetString(brave_rewards::prefs::kDeclaredGeo).empty()) {
++count;
}

return count;
}

bool BraveRewardsActionView::UpdatePublisherStatus() {
Expand Down
21 changes: 21 additions & 0 deletions browser/ui/webui/brave_rewards/rewards_panel_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,27 @@ static constexpr webui::LocalizedString kStrings[] = {
IDS_REWARDS_NOTIFICATION_WALLET_DISCONNECTED_TITLE},
{"notificationTokenGrantTitle", IDS_REWARDS_NOTIFICATION_TOKEN_GRANT_TITLE},
{"ok", IDS_REWARDS_PANEL_OK},
{"onboardingClose", IDS_BRAVE_REWARDS_ONBOARDING_CLOSE},
{"onboardingContinue", IDS_BRAVE_REWARDS_ONBOARDING_CONTINUE},
{"onboardingEarnHeader", IDS_BRAVE_REWARDS_ONBOARDING_EARN_HEADER},
{"onboardingEarnText", IDS_BRAVE_REWARDS_ONBOARDING_EARN_TEXT},
{"onboardingErrorHeader", IDS_BRAVE_REWARDS_ONBOARDING_ERROR_HEADER},
{"onboardingErrorHeaderDisabled",
IDS_BRAVE_REWARDS_ONBOARDING_ERROR_HEADER_DISABLED},
{"onboardingErrorText", IDS_BRAVE_REWARDS_ONBOARDING_ERROR_TEXT},
{"onboardingErrorTextAlreadyDeclared",
IDS_BRAVE_REWARDS_ONBOARDING_ERROR_TEXT_ALREADY_DECLARED},
{"onboardingErrorTextDeclareCountry",
IDS_BRAVE_REWARDS_ONBOARDING_ERROR_TEXT_DECLARE_COUNTRY},
{"onboardingErrorTextDisabled",
IDS_BRAVE_REWARDS_ONBOARDING_ERROR_TEXT_DISABLED},
{"onboardingGeoHeader", IDS_BRAVE_REWARDS_ONBOARDING_GEO_HEADER},
{"onboardingGeoSuccessHeader",
IDS_BRAVE_REWARDS_ONBOARDING_GEO_SUCCESS_HEADER},
{"onboardingGeoSuccessText", IDS_BRAVE_REWARDS_ONBOARDING_GEO_SUCCESS_TEXT},
{"onboardingGeoText", IDS_BRAVE_REWARDS_ONBOARDING_GEO_TEXT},
{"onboardingGeoTextDeclareCountry",
IDS_BRAVE_REWARDS_ONBOARDING_GEO_TEXT_DECLARE_COUNTRY},
{"onboardingPanelAcHeader", IDS_BRAVE_REWARDS_ONBOARDING_PANEL_AC_HEADER},
{"onboardingPanelAcText", IDS_BRAVE_REWARDS_ONBOARDING_PANEL_AC_TEXT},
{"onboardingPanelAdsHeader", IDS_BRAVE_REWARDS_ONBOARDING_PANEL_ADS_HEADER},
Expand Down Expand Up @@ -137,6 +156,8 @@ static constexpr webui::LocalizedString kStrings[] = {
IDS_BRAVE_REWARDS_ONBOARDING_PANEL_WELCOME_HEADER},
{"onboardingPanelWelcomeText",
IDS_BRAVE_REWARDS_ONBOARDING_PANEL_WELCOME_TEXT},
{"onboardingSave", IDS_BRAVE_REWARDS_ONBOARDING_SAVE},
{"onboardingSelectCountry", IDS_BRAVE_REWARDS_ONBOARDING_SELECT_COUNTRY},
{"onboardingSetupAdsHeader", IDS_BRAVE_REWARDS_ONBOARDING_SETUP_ADS_HEADER},
{"onboardingSetupAdsSubheader",
IDS_BRAVE_REWARDS_ONBOARDING_SETUP_ADS_SUBHEADER},
Expand Down
13 changes: 7 additions & 6 deletions browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,8 @@ void RewardsDOMHandler::OnRewardsWalletUpdated() {
GetAdsData(base::Value::List());
GetAutoContributeProperties(base::Value::List());
GetOnboardingStatus(base::Value::List());
GetExternalWallet(base::Value::List());
GetCountryCode(base::Value::List());
}

void RewardsDOMHandler::OnUnblindedTokensReady(
Expand Down Expand Up @@ -1946,14 +1948,13 @@ void RewardsDOMHandler::GetAllMonthlyReportIds(const base::Value::List& args) {
}

void RewardsDOMHandler::GetCountryCode(const base::Value::List& args) {
AllowJavascript();

const std::string locale =
brave_l10n::LocaleHelper::GetInstance()->GetLocale();
const std::string country_code = brave_l10n::GetCountryCode(locale);
if (!rewards_service_) {
return;
}

AllowJavascript();
CallJavascriptFunction("brave_rewards.countryCode",
base::Value(country_code));
base::Value(rewards_service_->GetCountryCode()));
}

void RewardsDOMHandler::CompleteReset(const base::Value::List& args) {
Expand Down
3 changes: 3 additions & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ void CustomizeWebUIHTMLSource(content::WebUI* web_ui,
{ "rewardsBraveRewards", IDS_REWARDS_WIDGET_BRAVE_REWARDS },
{ "rewardsClaimRewards", IDS_REWARDS_WIDGET_CLAIM_REWARDS },
{ "rewardsClaimTokens", IDS_REWARDS_WIDGET_CLAIM_TOKENS },
{ "rewardsContinue", IDS_REWARDS_WIDGET_CONTINUE},
{ "rewardsEarning", IDS_REWARDS_WIDGET_EARNING },
{ "rewardsEarningInfoText", IDS_REWARDS_WIDGET_EARNING_INFO_TEXT },
{ "rewardsExchangeValueNote", IDS_REWARDS_WIDGET_EXCHANGE_VALUE_NOTE },
Expand All @@ -277,6 +278,8 @@ void CustomizeWebUIHTMLSource(content::WebUI* web_ui,
{ "rewardsPaymentPending", IDS_REWARDS_PAYMENT_PENDING },
{ "rewardsPaymentProcessing", IDS_REWARDS_PAYMENT_PROCESSING },
{ "rewardsProgress", IDS_REWARDS_WIDGET_PROGRESS },
{ "rewardsSelectCountryHeader", IDS_REWARDS_WIDGET_SELECT_COUNTRY_HEADER}, // NOLINT
{ "rewardsSelectCountryText", IDS_REWARDS_WIDGET_SELECT_COUNTRY_TEXT},
{ "rewardsSettings", IDS_REWARDS_WIDGET_SETTINGS },
{ "rewardsSponsoredImageEarningText", IDS_REWARDS_WIDGET_SPONSORED_IMAGE_EARNING_TEXT }, // NOLINT
{ "rewardsSponsoredImageOptInText", IDS_REWARDS_WIDGET_SPONSORED_IMAGE_OPT_IN_TEXT }, // NOLINT
Expand Down
45 changes: 30 additions & 15 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -775,44 +775,59 @@
"type": "function",
"description": "Creates a Rewards wallet for the current profile",
"parameters": [
{
"type": "string",
"name": "country"
},
{
"type": "function",
"name": "callback",
"parameters": [
{
"type": "number",
"name": "errorCode"
"type": "string",
"name": "result"
}
]
}
]
},
{
"name": "getRewardsWallet",
"name": "getAvailableCountries",
"type": "function",
"description": "Returns the Rewards wallet information for the current profile",
"description": "",
"parameters": [
{
"type": "function",
"name": "callback",
"type": "function",
"parameters": [
{
"name": "wallet",
"type": "object",
"optional": true,
"properties": {
"paymentId": {
"type": "string"
},
"country": {
"type": "string"
}
"name": "countries",
"type": "array",
"items": {
"type": "string"
}
}
]
}
]
},
{
"name": "getDeclaredCountry",
"type": "function",
"description": "",
"parameters": [
{
"name": "callback",
"type": "function",
"parameters": [
{
"name": "country",
"type": "string"
}
]
}
]
},
{
"name": "getBalanceReport",
"type": "function",
Expand Down
5 changes: 5 additions & 0 deletions components/brave_new_tab_ui/api/initialData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type InitialData = {

export type PreInitialRewardsData = {
rewardsEnabled: boolean
declaredCountry: string
enabledAds: boolean
adsSupported: boolean
needsBrowserUpgradeToServeAds: boolean
Expand Down Expand Up @@ -134,12 +135,15 @@ export async function getInitialData (): Promise<InitialData> {
export async function getRewardsPreInitialData (): Promise<PreInitialRewardsData> {
const [
rewardsEnabled,
declaredCountry,
enabledAds,
adsSupported,
adsData
] = await Promise.all([
new Promise<boolean>(
(resolve) => chrome.braveRewards.getRewardsEnabled(resolve)),
new Promise<string>(
(resolve) => chrome.braveRewards.getDeclaredCountry(resolve)),
new Promise<boolean>(
(resolve) => chrome.braveRewards.getAdsEnabled(resolve)),
new Promise<boolean>(
Expand All @@ -151,6 +155,7 @@ export async function getRewardsPreInitialData (): Promise<PreInitialRewardsData

return {
rewardsEnabled,
declaredCountry,
enabledAds,
adsSupported,
needsBrowserUpgradeToServeAds
Expand Down
Loading

0 comments on commit 9629bac

Please sign in to comment.