Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rewards everywhere onboarding #7050

Merged
merged 1 commit into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1166,5 +1166,49 @@ BraveRewardsIsInitializedFunction::Run() {
OneArgument(std::make_unique<base::Value>(initialized)));
}

BraveRewardsShouldShowOnboardingFunction::
~BraveRewardsShouldShowOnboardingFunction() = default;

ExtensionFunction::ResponseAction
BraveRewardsShouldShowOnboardingFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
auto* rewards_service = RewardsServiceFactory::GetForProfile(profile);
if (!rewards_service) {
return RespondNow(Error("Rewards service is not initialized"));
}

const bool should_show = rewards_service->ShouldShowOnboarding();
return RespondNow(
OneArgument(std::make_unique<base::Value>(should_show)));
}

BraveRewardsSaveOnboardingResultFunction::
~BraveRewardsSaveOnboardingResultFunction() = default;

ExtensionFunction::ResponseAction
BraveRewardsSaveOnboardingResultFunction::Run() {
using ::brave_rewards::OnboardingResult;

std::unique_ptr<brave_rewards::SaveOnboardingResult::Params> params(
brave_rewards::SaveOnboardingResult::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());

Profile* profile = Profile::FromBrowserContext(browser_context());
auto* rewards_service = RewardsServiceFactory::GetForProfile(profile);
if (!rewards_service) {
return RespondNow(Error("Rewards service is not initialized"));
}

if (params->result == "opted-in") {
rewards_service->SaveOnboardingResult(OnboardingResult::kOptedIn);
} else if (params->result == "dismissed") {
rewards_service->SaveOnboardingResult(OnboardingResult::kDismissed);
} else {
NOTREACHED();
}

return RespondNow(NoArguments());
}

} // namespace api
} // namespace extensions
20 changes: 20 additions & 0 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,26 @@ class BraveRewardsIsInitializedFunction : public ExtensionFunction {
ResponseAction Run() override;
};

class BraveRewardsShouldShowOnboardingFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.shouldShowOnboarding", UNKNOWN)

protected:
~BraveRewardsShouldShowOnboardingFunction() override;

ResponseAction Run() override;
};

class BraveRewardsSaveOnboardingResultFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.saveOnboardingResult", UNKNOWN)

protected:
~BraveRewardsSaveOnboardingResultFunction() override;

ResponseAction Run() override;
};

} // namespace api
} // namespace extensions

Expand Down
38 changes: 38 additions & 0 deletions browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ class RewardsDOMHandler : public WebUIMessageHandler,

void OnGetWalletPassphrase(const std::string& pass);

void GetOnboardingStatus(const base::ListValue* args);
void SaveOnboardingResult(const base::ListValue* args);

// RewardsServiceObserver implementation
void OnFetchPromotions(
brave_rewards::RewardsService* rewards_service,
Expand Down Expand Up @@ -480,6 +483,12 @@ void RewardsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("brave_rewards.getWalletPassphrase",
base::BindRepeating(&RewardsDOMHandler::GetWalletPassphrase,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.getOnboardingStatus",
base::BindRepeating(&RewardsDOMHandler::GetOnboardingStatus,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.saveOnboardingResult",
base::BindRepeating(&RewardsDOMHandler::SaveOnboardingResult,
base::Unretained(this)));
}

void RewardsDOMHandler::Init() {
Expand Down Expand Up @@ -1922,6 +1931,35 @@ void RewardsDOMHandler::OnGetWalletPassphrase(const std::string& passphrase) {
base::Value(passphrase));
}

void RewardsDOMHandler::GetOnboardingStatus(const base::ListValue* args) {
if (!rewards_service_ || !web_ui()->CanCallJavascript()) {
return;
}
base::Value data(base::Value::Type::DICTIONARY);
data.SetBoolKey("showOnboarding", rewards_service_->ShouldShowOnboarding());
web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.onboardingStatus",
data);
}

void RewardsDOMHandler::SaveOnboardingResult(const base::ListValue* args) {
using brave_rewards::OnboardingResult;

CHECK_EQ(1U, args->GetSize());
if (!rewards_service_) {
return;
}

const std::string result_type = args->GetList()[0].GetString();
if (result_type == "opted-in") {
rewards_service_->SaveOnboardingResult(OnboardingResult::kOptedIn);
} else if (result_type == "dismissed") {
rewards_service_->SaveOnboardingResult(OnboardingResult::kDismissed);
} else {
NOTREACHED();
}
}

} // namespace

BraveRewardsPageUI::BraveRewardsPageUI(content::WebUI* web_ui,
Expand Down
42 changes: 42 additions & 0 deletions browser/ui/webui/brave_tip_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class TipMessageHandler : public WebUIMessageHandler,
void DialogReady(const base::ListValue* args);
void GetPublisherBanner(const base::ListValue* args);
void GetRewardsParameters(const base::ListValue* args);
void GetOnboardingStatus(const base::ListValue* args);
void SaveOnboardingResult(const base::ListValue* args);
void OnTip(const base::ListValue* args);
void GetRecurringTips(const base::ListValue* args);
void GetReconcileStamp(const base::ListValue* args);
Expand Down Expand Up @@ -133,6 +135,18 @@ void TipMessageHandler::RegisterMessages() {
&TipMessageHandler::GetRewardsParameters,
base::Unretained(this)));

web_ui()->RegisterMessageCallback(
"getOnboardingStatus",
base::BindRepeating(
&TipMessageHandler::GetOnboardingStatus,
base::Unretained(this)));

web_ui()->RegisterMessageCallback(
"saveOnboardingResult",
base::BindRepeating(
&TipMessageHandler::SaveOnboardingResult,
base::Unretained(this)));

web_ui()->RegisterMessageCallback(
"onTip",
base::BindRepeating(
Expand Down Expand Up @@ -269,6 +283,34 @@ void TipMessageHandler::GetRewardsParameters(const base::ListValue* args) {
weak_factory_.GetWeakPtr()));
}

void TipMessageHandler::GetOnboardingStatus(const base::ListValue* args) {
if (!rewards_service_) {
return;
}
AllowJavascript();
base::Value data(base::Value::Type::DICTIONARY);
data.SetBoolKey("showOnboarding", rewards_service_->ShouldShowOnboarding());
FireWebUIListener("onboardingStatusUpdated", data);
}

void TipMessageHandler::SaveOnboardingResult(const base::ListValue* args) {
using brave_rewards::OnboardingResult;

CHECK_EQ(1U, args->GetSize());
if (!rewards_service_) {
return;
}

const std::string result_type = args->GetList()[0].GetString();
if (result_type == "opted-in") {
rewards_service_->SaveOnboardingResult(OnboardingResult::kOptedIn);
} else if (result_type == "dismissed") {
rewards_service_->SaveOnboardingResult(OnboardingResult::kDismissed);
} else {
NOTREACHED();
}
}

void TipMessageHandler::OnTip(const base::ListValue* args) {
CHECK_EQ(3U, args->GetSize());
const std::string publisher_key = args->GetList()[0].GetString();
Expand Down
12 changes: 12 additions & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,12 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "off", IDS_BRAVE_UI_OFF },
{ "ok", IDS_BRAVE_UI_OK },
{ "on", IDS_BRAVE_UI_ON },
{ "onboardingEarnHeader", IDS_BRAVE_REWARDS_ONBOARDING_EARN_HEADER },
{ "onboardingEarnText", IDS_BRAVE_REWARDS_ONBOARDING_EARN_TEXT },
{ "onboardingStartUsingRewards", IDS_BRAVE_REWARDS_ONBOARDING_START_USING_REWARDS }, // NOLINT
{ "onboardingTerms", IDS_BRAVE_REWARDS_ONBOARDING_TERMS },
{ "onboardingTipHeader", IDS_BRAVE_REWARDS_ONBOARDING_TIP_HEADER },
{ "onboardingTipText", IDS_BRAVE_REWARDS_ONBOARDING_TIP_TEXT },
{ "oneTime", IDS_BRAVE_UI_ONE_TIME },
{ "oneTimeDonation", IDS_BRAVE_UI_ONE_TIME_DONATION },
{ "openBalance", IDS_BRAVE_UI_OPEN_BALANCE },
Expand Down Expand Up @@ -850,8 +856,14 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "notEnoughTokens", IDS_BRAVE_UI_NOT_ENOUGH_TOKENS },
{ "notEnoughTokensLink", IDS_BRAVE_UI_NOT_ENOUGH_TOKENS_LINK },
{ "on", IDS_BRAVE_UI_ON },
{ "onboardingMaybeLater", IDS_BRAVE_REWARDS_ONBOARDING_MAYBE_LATER },
{ "onboardingStartUsingRewards", IDS_BRAVE_REWARDS_ONBOARDING_START_USING_REWARDS }, // NOLINT
{ "onboardingTerms", IDS_BRAVE_REWARDS_ONBOARDING_TERMS },
{ "onboardingTipHeader", IDS_BRAVE_REWARDS_ONBOARDING_TIP_HEADER },
{ "onboardingTipText", IDS_BRAVE_REWARDS_ONBOARDING_TIP_TEXT },
{ "oneTimeTip", IDS_BRAVE_REWARDS_TIP_ONE_TIME_TIP },
{ "oneTimeTipAmount", IDS_BRAVE_REWARDS_TIP_ONE_TIME_TIP_AMOUNT },
{ "optInRequired", IDS_BRAVE_REWARDS_TIP_OPT_IN_REQUIRED },
{ "points", IDS_BRAVE_UI_POINTS },
{ "postHeader", IDS_BRAVE_REWARDS_TIP_POST_HEADER },
{ "postHeaderTwitter", IDS_BRAVE_REWARDS_TIP_POST_HEADER_TWITTER },
Expand Down
28 changes: 28 additions & 0 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,34 @@
]
}
]
},
{
"name": "shouldShowOnboarding",
"type": "function",
"description": "Gets a value indicating whether rewards onboarding should be shown to the user.",
"parameters": [
{
"type": "function",
"name": "callback",
"parameters": [
{
"name": "showOnboarding",
"type": "boolean"
}
]
}
]
},
{
"name": "saveOnboardingResult",
"type": "function",
"description": "Saves the rewards user onboarding result.",
"parameters": [
{
"type": "string",
"name": "result"
}
]
}
]
}
Expand Down
2 changes: 2 additions & 0 deletions components/brave_ads/browser/ads_service_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class MockRewardsService : public RewardsService {
MOCK_METHOD1(GetAutoContributeEnabled,
void(brave_rewards::GetAutoContributeEnabledCallback));
MOCK_METHOD1(SetAutoContributeEnabled, void(bool));
MOCK_CONST_METHOD0(ShouldShowOnboarding, bool());
MOCK_METHOD1(SaveOnboardingResult, void(brave_rewards::OnboardingResult));
MOCK_METHOD2(SetTimer, void(uint64_t, uint32_t*));
MOCK_METHOD4(GetPublisherActivityFromUrl, void(uint64_t,
const std::string&,
Expand Down
1 change: 1 addition & 0 deletions components/brave_rewards/browser/rewards_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void RewardsService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
#if defined(OS_ANDROID)
registry->RegisterBooleanPref(prefs::kUseRewardsStagingServer, false);
#endif
registry->RegisterTimePref(prefs::kOnboarded, base::Time());
registry->RegisterUint64Pref(prefs::kPromotionLastFetchStamp, 0ull);
registry->RegisterBooleanPref(prefs::kPromotionCorruptedMigrated, false);
registry->RegisterBooleanPref(prefs::kAnonTransferChecked, false);
Expand Down
7 changes: 7 additions & 0 deletions components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ using StartProcessCallback =

using GetWalletPassphraseCallback = base::Callback<void(const std::string&)>;

enum class OnboardingResult {
kOptedIn,
kDismissed
};

class RewardsService : public KeyedService {
public:
RewardsService();
Expand Down Expand Up @@ -200,6 +205,8 @@ class RewardsService : public KeyedService {
virtual void GetAutoContributeEnabled(
GetAutoContributeEnabledCallback callback) = 0;
virtual void SetAutoContributeEnabled(bool enabled) = 0;
virtual bool ShouldShowOnboarding() const = 0;
virtual void SaveOnboardingResult(OnboardingResult result) = 0;
virtual void GetBalanceReport(
const uint32_t month,
const uint32_t year,
Expand Down
27 changes: 27 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,33 @@ void RewardsServiceImpl::SetAutoContributeEnabled(bool enabled) {
}
}

bool RewardsServiceImpl::ShouldShowOnboarding() const {
PrefService* prefs = profile_->GetPrefs();
const base::Time onboard_time = prefs->GetTime(prefs::kOnboarded);

bool ads_enabled = false;
bool ads_supported = true;
auto* ads_service = brave_ads::AdsServiceFactory::GetForProfile(profile_);
if (ads_service) {
ads_enabled = ads_service->IsEnabled();
ads_supported = ads_service->IsSupportedLocale();
}

return onboard_time.is_null() && !ads_enabled && ads_supported;
}

void RewardsServiceImpl::SaveOnboardingResult(OnboardingResult result) {
PrefService* prefs = profile_->GetPrefs();
prefs->SetTime(prefs::kOnboarded, base::Time::Now());
if (result == OnboardingResult::kOptedIn) {
SetAutoContributeEnabled(true);
auto* ads_service = brave_ads::AdsServiceFactory::GetForProfile(profile_);
if (ads_service) {
ads_service->SetEnabled(true);
}
}
}

void RewardsServiceImpl::OnAdsEnabled(bool ads_enabled) {
if (ads_enabled) {
StartLedger(base::DoNothing());
Expand Down
4 changes: 4 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ class RewardsServiceImpl : public RewardsService,

void SetAutoContributeEnabled(bool enabled) override;

bool ShouldShowOnboarding() const override;

void SaveOnboardingResult(OnboardingResult result) override;

void GetMonthlyReport(
const uint32_t month,
const uint32_t year,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class RewardsBrowserTest : public InProcessBrowserTest {
// Other
contribution_->Initialize(browser(), rewards_service_);
promotion_->Initialize(browser(), rewards_service_);

// Bypass onboarding UX by default
rewards_service_->SaveOnboardingResult(
brave_rewards::OnboardingResult::kDismissed);
}

void TearDown() override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class RewardsContributionBrowserTest : public InProcessBrowserTest {
// Other
promotion_->Initialize(browser(), rewards_service_);
contribution_->Initialize(browser(), rewards_service_);

// Bypass onboarding UX by default
rewards_service_->SaveOnboardingResult(
brave_rewards::OnboardingResult::kDismissed);
}

void TearDown() override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class RewardsFlagBrowserTest : public InProcessBrowserTest {
&RewardsFlagBrowserTest::GetTestResponse,
base::Unretained(this)));
rewards_service_->SetLedgerEnvForTesting();

// Bypass onboarding UX by default
rewards_service_->SaveOnboardingResult(
brave_rewards::OnboardingResult::kDismissed);
}

void GetTestResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class RewardsNotificationBrowserTest
contribution_->Initialize(browser(), rewards_service_);
rewards_notification_service_ = rewards_service_->GetNotificationService();
rewards_notification_service_->AddObserver(this);

// Bypass onboarding UX by default
rewards_service_->SaveOnboardingResult(
brave_rewards::OnboardingResult::kDismissed);
}

void TearDown() override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class RewardsPromotionBrowserTest : public InProcessBrowserTest {

// Other
promotion_->Initialize(browser(), rewards_service_);

// Bypass onboarding UX by default
rewards_service_->SaveOnboardingResult(
brave_rewards::OnboardingResult::kDismissed);
}

void TearDown() override {
Expand Down
Loading