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

Fixes when rewards is reset ads service should shut down - 1.13.x #6286

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion components/brave_ads/browser/ads_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ class AdsService : public KeyedService {
const bool flagged,
OnToggleFlagAdCallback callback) = 0;

virtual void ResetAllState() = 0;
virtual void ResetAllState(
const bool should_shutdown) = 0;

virtual void OnUserModelUpdated(
const std::string& id) = 0;
Expand Down
51 changes: 47 additions & 4 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ AdsServiceImpl::AdsServiceImpl(Profile* profile) :
base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::BLOCK_SHUTDOWN})),
base_path_(profile_->GetPath().AppendASCII("ads_service")),
database_(new ads::Database(base_path_.AppendASCII("database.sqlite"))),
last_idle_state_(ui::IdleState::IDLE_STATE_ACTIVE),
display_service_(NotificationDisplayService::GetForProfile(profile_)),
rewards_service_(brave_rewards::RewardsServiceFactory::GetForProfile(
Expand Down Expand Up @@ -547,6 +546,10 @@ void AdsServiceImpl::OnInitialize(
void AdsServiceImpl::ShutdownBatAds() {
VLOG(1) << "Shutting down ads";

const bool success = file_task_runner_->DeleteSoon(FROM_HERE,
database_.release());
VLOG_IF(1, !success) << "Failed to release database";

bat_ads_->Shutdown(base::BindOnce(&AdsServiceImpl::OnShutdownBatAds,
AsWeakPtr()));
}
Expand Down Expand Up @@ -633,22 +636,57 @@ void AdsServiceImpl::Stop() {
ShutdownBatAds();
}

void AdsServiceImpl::ResetAllState() {
void AdsServiceImpl::ResetState() {
VLOG(1) << "Resetting ads state";

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

base::PostTaskAndReplyWithResult(
file_task_runner_.get(), FROM_HERE,
base::BindOnce(&ResetOnFileTaskRunner, base_path_),
base::BindOnce(&AdsServiceImpl::OnResetAllState, AsWeakPtr()));
}

void AdsServiceImpl::ResetAllState(
const bool should_shutdown) {
if (!should_shutdown) {
ResetState();
return;
}

VLOG(1) << "Shutting down and resetting ads state";

const bool success = file_task_runner_->DeleteSoon(FROM_HERE,
database_.release());
VLOG_IF(1, !success) << "Failed to release database";

bat_ads_->Shutdown(base::BindOnce(&AdsServiceImpl::OnShutdownAndResetBatAds,
AsWeakPtr()));
}

void AdsServiceImpl::OnShutdownAndResetBatAds(
const int32_t result) {
DCHECK(is_initialized_);

if (result != ads::Result::SUCCESS) {
VLOG(0) << "Failed to shutdown and reset ads state";
return;
}

Shutdown();

VLOG(1) << "Successfully shutdown ads";

ResetState();
}

void AdsServiceImpl::OnResetAllState(
const bool success) {
if (!success) {
VLOG(0) << "Failed to reset ads state";
return;
}

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

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

Expand All @@ -674,6 +712,9 @@ void AdsServiceImpl::OnEnsureBaseDirectoryExists(
bat_ads_.BindNewEndpointAndPassReceiver(),
base::BindOnce(&AdsServiceImpl::OnCreate, AsWeakPtr()));

database_ = std::make_unique<ads::Database>(
base_path_.AppendASCII("database.sqlite"));

const std::string locale = GetLocale();
RegisterUserModelComponentsForLocale(locale);

Expand Down Expand Up @@ -2008,6 +2049,8 @@ std::string AdsServiceImpl::LoadResourceForId(
ads::DBCommandResponsePtr RunDBTransactionOnFileTaskRunner(
ads::DBTransactionPtr transaction,
ads::Database* database) {
DCHECK(database);

auto response = ads::DBCommandResponse::New();

if (!database) {
Expand Down
7 changes: 6 additions & 1 deletion components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ class AdsServiceImpl : public AdsService,
const bool flagged,
OnToggleFlagAdCallback callback) override;

void ResetAllState(
const bool should_shutdown) override;

// AdsClient implementation
bool IsEnabled() const override;

Expand Down Expand Up @@ -189,7 +192,9 @@ class AdsServiceImpl : public AdsService,
void Start();
void Stop();

void ResetAllState() override;
void ResetState();
void OnShutdownAndResetBatAds(
const int32_t result);
void OnResetAllState(
const bool success);

Expand Down
9 changes: 5 additions & 4 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,6 @@ RewardsServiceImpl::RewardsServiceImpl(Profile* profile)
rewards_base_path_(profile_->GetPath().Append(kRewardsStatePath)),
notification_service_(new RewardsNotificationServiceImpl(profile)),
next_timer_id_(0) {
file_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&EnsureRewardsBaseDirectoryExists,
rewards_base_path_));
// Set up the rewards data source
content::URLDataSource::Add(profile_,
std::make_unique<BraveRewardsSource>(profile_));
Expand Down Expand Up @@ -429,6 +426,10 @@ void RewardsServiceImpl::StartLedger() {
return;
}

file_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&EnsureRewardsBaseDirectoryExists,
rewards_base_path_));

rewards_database_ =
std::make_unique<RewardsDatabase>(publisher_info_db_path_);

Expand Down Expand Up @@ -3919,7 +3920,7 @@ void RewardsServiceImpl::OnCompleteReset(

auto* ads_service = brave_ads::AdsServiceFactory::GetForProfile(profile_);
if (ads_service) {
ads_service->ResetAllState();
ads_service->ResetAllState(/* should_shutdown */ true);
}

for (auto& observer : observers_) {
Expand Down