Skip to content

Commit

Permalink
Adds option to reset rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Jul 7, 2020
1 parent ad3b5bd commit 0e0915b
Show file tree
Hide file tree
Showing 49 changed files with 463 additions and 180 deletions.
22 changes: 5 additions & 17 deletions browser/brave_rewards/android/brave_rewards_native_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -486,35 +486,23 @@ void BraveRewardsNativeWorker::GetReconcileStamp(JNIEnv* env,

void BraveRewardsNativeWorker::ResetTheWholeState(JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj) {
auto* ads_service_ = brave_ads::AdsServiceFactory::GetForProfile(
ProfileManager::GetActiveUserProfile()->GetOriginalProfile());
if (ads_service_) {
ads_service_->ResetTheWholeState(base::Bind(
&BraveRewardsNativeWorker::OnAdsResetTheWholeState,
weak_factory_.GetWeakPtr()));
} else {
OnAdsResetTheWholeState(true);
}
}

void BraveRewardsNativeWorker::OnAdsResetTheWholeState(bool sucess) {
if (sucess && brave_rewards_service_) {
brave_rewards_service_->ResetTheWholeState(base::Bind(
if (brave_rewards_service_) {
brave_rewards_service_->CompleteReset(base::Bind(
&BraveRewardsNativeWorker::OnResetTheWholeState,
weak_factory_.GetWeakPtr()));
} else {
JNIEnv* env = base::android::AttachCurrentThread();

Java_BraveRewardsNativeWorker_OnResetTheWholeState(env,
weak_java_brave_rewards_native_worker_.get(env), sucess);
weak_java_brave_rewards_native_worker_.get(env), false);
}
}

void BraveRewardsNativeWorker::OnResetTheWholeState(bool sucess) {
void BraveRewardsNativeWorker::OnResetTheWholeState(const bool success) {
JNIEnv* env = base::android::AttachCurrentThread();

Java_BraveRewardsNativeWorker_OnResetTheWholeState(env,
weak_java_brave_rewards_native_worker_.get(env), sucess);
weak_java_brave_rewards_native_worker_.get(env), success);
}

double BraveRewardsNativeWorker::GetPublisherRecurrentDonationAmount(
Expand Down
4 changes: 1 addition & 3 deletions browser/brave_rewards/android/brave_rewards_native_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,7 @@ class BraveRewardsNativeWorker : public brave_rewards::RewardsServiceObserver,
const base::android::JavaParamRef<jobject>& obj,
const base::android::JavaParamRef<jstring>& pass_phrase);

void OnAdsResetTheWholeState(bool sucess);

void OnResetTheWholeState(bool sucess);
void OnResetTheWholeState(const bool success);

void OnGetGetReconcileStamp(uint64_t timestamp);

Expand Down
19 changes: 19 additions & 0 deletions browser/brave_rewards/extension_rewards_service_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,23 @@ void ExtensionRewardsServiceObserver::OnUnblindedTokensReady(
event_router->BroadcastEvent(std::move(event));
}

void ExtensionRewardsServiceObserver::OnCompleteReset(const bool success) {
auto* event_router = extensions::EventRouter::Get(profile_);
if (!event_router) {
return;
}

extensions::api::brave_rewards::OnCompleteReset::Properties properties;
properties.success = success;

std::unique_ptr<base::ListValue> args(
extensions::api::brave_rewards::OnCompleteReset::Create(properties)
.release());
std::unique_ptr<extensions::Event> event(new extensions::Event(
extensions::events::BRAVE_START,
extensions::api::brave_rewards::OnCompleteReset::kEventName,
std::move(args)));
event_router->BroadcastEvent(std::move(event));
}

} // namespace brave_rewards
2 changes: 2 additions & 0 deletions browser/brave_rewards/extension_rewards_service_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class ExtensionRewardsServiceObserver : public RewardsServiceObserver,
void OnAdsEnabled(RewardsService* rewards_service,
bool ads_enabled) override;

void OnCompleteReset(const bool success) override;

private:
Profile* profile_;

Expand Down
2 changes: 1 addition & 1 deletion browser/extensions/api/rewards_notifications_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ RewardsNotificationsDeleteAllNotificationsFunction::Run() {
RewardsNotificationService* rewards_notification_service =
RewardsServiceFactory::GetForProfile(profile)->GetNotificationService();
if (rewards_notification_service) {
rewards_notification_service->DeleteAllNotifications();
rewards_notification_service->DeleteAllNotifications(false);
}
return RespondNow(NoArguments());
}
Expand Down
23 changes: 23 additions & 0 deletions browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnGetRewardsParameters(
std::unique_ptr<brave_rewards::RewardsParameters> parameters);

void CompleteReset(const base::ListValue* args);

// RewardsServiceObserver implementation
void OnWalletInitialized(brave_rewards::RewardsService* rewards_service,
int32_t result) override;
Expand Down Expand Up @@ -279,6 +281,8 @@ class RewardsDOMHandler : public WebUIMessageHandler,

void ReconcileStampReset() override;

void OnCompleteReset(const bool success) override;

// RewardsNotificationsServiceObserver implementation
void OnNotificationAdded(
brave_rewards::RewardsNotificationService* rewards_notification_service,
Expand Down Expand Up @@ -480,6 +484,9 @@ void RewardsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("brave_rewards.getCountryCode",
base::BindRepeating(&RewardsDOMHandler::GetCountryCode,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.completeReset",
base::BindRepeating(&RewardsDOMHandler::CompleteReset,
base::Unretained(this)));
}

void RewardsDOMHandler::Init() {
Expand Down Expand Up @@ -1917,6 +1924,22 @@ void RewardsDOMHandler::GetCountryCode(const base::ListValue* args) {
"brave_rewards.countryCode", base::Value(country_code));
}

void RewardsDOMHandler::CompleteReset(const base::ListValue* args) {
if (!rewards_service_) {
return;
}

rewards_service_->CompleteReset(base::DoNothing());
}

void RewardsDOMHandler::OnCompleteReset(const bool success) {
if (!web_ui()->CanCallJavascript()) {
return;
}

web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.completeReset", base::Value(success));
}

} // namespace

Expand Down
5 changes: 5 additions & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "reservedAmountText", IDS_BRAVE_UI_RESERVED_AMOUNT_TEXT },
{ "reservedMoreLink", IDS_BRAVE_UI_RESERVED_MORE_LINK },
{ "reservedAllLink", IDS_BRAVE_UI_RESERVED_ALL_LINK },
{ "reset", IDS_BRAVE_UI_RESET },
{ "restore", IDS_BRAVE_UI_RESTORE },
{ "restoreAll", IDS_BRAVE_UI_RESTORE_ALL },
{ "reviewSitesMsg", IDS_BRAVE_UI_REVIEW_SITE_MSG },
Expand Down Expand Up @@ -619,6 +620,10 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "rewardsRestoreText2", IDS_BRAVE_UI_REWARDS_RESTORE_TEXT2 },
{ "rewardsRestoreText3", IDS_BRAVE_UI_REWARDS_RESTORE_TEXT3 },
{ "rewardsRestoreText4", IDS_BRAVE_UI_REWARDS_RESTORE_TEXT4 },
{ "rewardsRestoreWarning", IDS_BRAVE_UI_REWARDS_RESTORE_WARNING },
{ "rewardsResetConfirmation", IDS_BRAVE_UI_REWARDS_RESET_CONFIRMATION },
{ "rewardsResetTextFunds", IDS_BRAVE_UI_REWARDS_RESET_TEXT_FUNDS },
{ "rewardsResetTextNoFunds", IDS_BRAVE_UI_REWARDS_RESET_TEXT_NO_FUNDS },
{ "rewardsSummary", IDS_BRAVE_UI_REWARDS_SUMMARY },
{ "rewardsWhy", IDS_BRAVE_UI_REWARDS_WHY },
{ "saved", IDS_BRAVE_UI_ADS_SAVED },
Expand Down
17 changes: 17 additions & 0 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,23 @@
"type": "function",
"description": "Fired when we get unblinded tokens",
"parameters": []
},
{
"name": "onCompleteReset",
"type": "function",
"description": "Fired when rewards was reset",
"parameters": [
{
"name": "properties",
"type": "object",
"properties": {
"success": {
"type": "boolean",
"description": "was reset successful"
}
}
}
]
}
],
"functions": [
Expand Down
3 changes: 1 addition & 2 deletions components/brave_ads/browser/ads_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ class AdsService : public KeyedService {
const bool flagged,
OnToggleFlagAdCallback callback) = 0;

virtual void ResetTheWholeState(
const base::Callback<void(bool)>& callback) = 0;
virtual void ResetAllState() = 0;
};

} // namespace brave_ads
Expand Down
19 changes: 2 additions & 17 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,8 @@ void AdsServiceImpl::OnResetAllState(
return;
}

profile_->GetPrefs()->ClearPrefsWithPrefixSilently("brave.brave_ads");

VLOG(1) << "Successfully reset ads state";
}

Expand Down Expand Up @@ -1676,23 +1678,6 @@ std::string AdsServiceImpl::GetStringPref(
return value;
}

void AdsServiceImpl::ResetTheWholeState(
const base::Callback<void(bool)>& callback) {
SetEnabled(false);
base::PostTaskAndReplyWithResult(
file_task_runner_.get(), FROM_HERE,
base::BindOnce(&ResetOnFileTaskRunner,
base_path_),
base::BindOnce(&AdsServiceImpl::OnResetTheWholeState,
AsWeakPtr(), std::move(callback)));
}

void AdsServiceImpl::OnResetTheWholeState(
base::Callback<void(bool)> callback,
bool success) {
callback.Run(success);
}

void AdsServiceImpl::SetStringPref(
const std::string& path,
const std::string& value) {
Expand Down
6 changes: 1 addition & 5 deletions components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class AdsServiceImpl : public AdsService,
void Start();
void Stop();

void ResetAllState();
void ResetAllState() override;
void OnResetAllState(
const bool success);

Expand Down Expand Up @@ -218,7 +218,6 @@ class AdsServiceImpl : public AdsService,
const std::string& json);
void RetryViewingAdNotification(
const std::string& uuid);
void ResetTheWholeState(const base::Callback<void(bool)>& callback) override;

void SetAdsServiceForNotificationHandler();
void ClearAdsServiceForNotificationHandler();
Expand Down Expand Up @@ -280,9 +279,6 @@ class AdsServiceImpl : public AdsService,
const ads::ResultCallback& callback,
const bool success);

void OnResetTheWholeState(base::Callback<void(bool)> callback,
bool success);

void OnRunDBTransaction(
ads::RunDBTransactionCallback callback,
ads::DBCommandResponsePtr response);
Expand Down
4 changes: 2 additions & 2 deletions components/brave_ads/browser/ads_service_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ class MockRewardsService : public RewardsService {
void(RewardsServicePrivateObserver* observer));
MOCK_METHOD1(RemovePrivateObserver,
void(RewardsServicePrivateObserver* observer));
MOCK_METHOD1(ResetTheWholeState,
void(const base::Callback<void(bool)>& callback));

MOCK_METHOD1(GetAnonWalletStatus,
void(brave_rewards::GetAnonWalletStatusCallback callback));
Expand Down Expand Up @@ -230,6 +228,8 @@ class MockRewardsService : public RewardsService {

MOCK_METHOD1(ClearDiagnosticLog, void(
brave_rewards::ClearDiagnosticLogCallback callback));

MOCK_METHOD1(CompleteReset, void(brave_rewards::SuccessCallback callback));
};

class AdsServiceTest : public testing::Test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class RewardsNotificationService {
RewardsNotificationID id = "",
bool only_once = false) = 0;
virtual void DeleteNotification(RewardsNotificationID id) = 0;
virtual void DeleteAllNotifications() = 0;
virtual void DeleteAllNotifications(const bool delete_displayed) = 0;
virtual void GetNotification(RewardsNotificationID id) = 0;
virtual void GetNotifications() = 0;
virtual const RewardsNotificationsMap& GetAllNotifications() const = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,19 @@ void RewardsNotificationServiceImpl::DeleteNotification(
OnNotificationDeleted(rewards_notification);
}

void RewardsNotificationServiceImpl::DeleteAllNotifications() {
void RewardsNotificationServiceImpl::DeleteAllNotifications(
const bool delete_displayed) {
bool displayed = delete_displayed;

#if defined(OS_ANDROID)
displayed = true;
#endif

if (displayed) {
rewards_notifications_displayed_.clear();
}

rewards_notifications_.clear();
#if defined(OS_ANDROID)
rewards_notifications_displayed_.clear();
#endif
StoreRewardsNotifications();
OnAllNotificationsDeleted();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class RewardsNotificationServiceImpl
RewardsNotificationID id = "",
bool only_once = false) override;
void DeleteNotification(RewardsNotificationID id) override;
void DeleteAllNotifications() override;
void DeleteAllNotifications(const bool delete_displayed) override;
void GetNotification(RewardsNotificationID id) override;
void GetNotifications() override;
const RewardsNotificationsMap& GetAllNotifications() const override;
Expand Down
6 changes: 4 additions & 2 deletions components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ using LoadDiagnosticLogCallback = base::OnceCallback<void(const std::string&)>;

using ClearDiagnosticLogCallback = base::OnceCallback<void(const bool success)>;

using SuccessCallback = base::OnceCallback<void(const bool success)>;

class RewardsService : public KeyedService {
public:
RewardsService();
Expand Down Expand Up @@ -273,8 +275,6 @@ class RewardsService : public KeyedService {

virtual void RemovePendingContribution(const uint64_t id) = 0;
virtual void RemoveAllPendingContributions() = 0;
virtual void ResetTheWholeState(
const base::Callback<void(bool)>& callback) = 0;

void AddObserver(RewardsServiceObserver* observer);
void RemoveObserver(RewardsServiceObserver* observer);
Expand Down Expand Up @@ -350,6 +350,8 @@ class RewardsService : public KeyedService {
virtual void ClearDiagnosticLog(
ClearDiagnosticLogCallback callback) = 0;

virtual void CompleteReset(SuccessCallback callback) = 0;

protected:
base::ObserverList<RewardsServiceObserver> observers_;

Expand Down
Loading

0 comments on commit 0e0915b

Please sign in to comment.