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 Jun 29, 2020
1 parent af2ee0c commit ed62cbf
Show file tree
Hide file tree
Showing 40 changed files with 344 additions and 148 deletions.
16 changes: 2 additions & 14 deletions browser/brave_rewards/android/brave_rewards_native_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -486,20 +486,8 @@ 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 {
Expand Down
2 changes: 0 additions & 2 deletions browser/brave_rewards/android/brave_rewards_native_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ 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 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
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();
}

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

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

} // namespace

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 @@ -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 @@ -613,6 +614,8 @@ 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 },
{ "rewardsResetText", IDS_BRAVE_UI_REWARDS_RESET_TEXT },
{ "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
20 changes: 3 additions & 17 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,9 @@ void AdsServiceImpl::OnResetAllState(
return;
}

// TODO enable with 84
// profile_->GetPrefs()->ClearPrefsWithPrefixSilently("brave.brave_ads");

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

Expand Down Expand Up @@ -1748,23 +1751,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 @@ -292,9 +291,6 @@ class AdsServiceImpl : public AdsService,
const ads::ResultCallback& callback,
const bool success);

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

void MigratePrefs();
bool MigratePrefs(
const int source_version,
Expand Down
2 changes: 0 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
4 changes: 2 additions & 2 deletions components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,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 +348,8 @@ class RewardsService : public KeyedService {
virtual void ClearDiagnosticLog(
ClearDiagnosticLogCallback callback) = 0;

virtual void CompleteReset() = 0;

protected:
base::ObserverList<RewardsServiceObserver> observers_;

Expand Down
Loading

0 comments on commit ed62cbf

Please sign in to comment.