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

[Rewards] Do not start utility process until user opts in. #15146

Merged
merged 5 commits into from
Oct 5, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -483,19 +483,6 @@ public void getAutoContributionAmount() {
}
}

public void StartProcess() {
synchronized (lock) {
BraveRewardsNativeWorkerJni.get().startProcess(mNativeBraveRewardsNativeWorker);
}
}

@CalledByNative
public void OnStartProcess() {
for (BraveRewardsObserver observer : mObservers) {
observer.OnStartProcess();
}
}

@CalledByNative
public void OnRefreshPublisher(int status, String publisherKey) {
for (BraveRewardsObserver observer : mObservers) {
Expand Down Expand Up @@ -739,7 +726,6 @@ void setAutoContributeEnabled(
long nativeBraveRewardsNativeWorker, boolean isSetAutoContributeEnabled);
void setAutoContributionAmount(long nativeBraveRewardsNativeWorker, double amount);
void getAutoContributionAmount(long nativeBraveRewardsNativeWorker);
void startProcess(long nativeBraveRewardsNativeWorker);
void getAdsAccountStatement(long nativeBraveRewardsNativeWorker);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ default public void onReconcileComplete(int resultCode, int rewardsType, double
default public void OnRefreshPublisher(int status, String publisherKey){};
default public void OnOneTimeTip(int result){};
default public void OnPendingContributionSaved(int result){};
default public void OnStartProcess(){};
default public void OnGetAdsAccountStatement(boolean success, double nextPaymentDate,
int adsReceivedThisMonth, double earningsThisMonth, double earningsLastMonth){};
default public void onPublisherBanner(String jsonBannerInfo){};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,19 +756,6 @@ public void showLikePopDownMenu() {
mPopupWindow.showAsDropDown(mAnchorView, 0, 0);

checkForRewardsOnboarding();
BraveRewardsNativeWorker.getInstance().StartProcess();
}

private void checkForRewardsOnboarding() {
if (mPopupView != null && ChromeFeatureList.isEnabled(BraveFeatureList.BRAVE_REWARDS)
&& BraveRewardsHelper.shouldShowBraveRewardsOnboardingOnce()) {
showBraveRewardsOnboarding(mPopupView, false);
BraveRewardsHelper.setShowBraveRewardsOnboardingOnce(false);
}
}

@Override
public void OnStartProcess() {
requestPublisherInfo();
fetchRewardsData();
setNotificationsControls();
Expand All @@ -782,6 +769,14 @@ public void OnStartProcess() {
}
}

private void checkForRewardsOnboarding() {
if (mPopupView != null && ChromeFeatureList.isEnabled(BraveFeatureList.BRAVE_REWARDS)
&& BraveRewardsHelper.shouldShowBraveRewardsOnboardingOnce()) {
showBraveRewardsOnboarding(mPopupView, false);
BraveRewardsHelper.setShowBraveRewardsOnboardingOnce(false);
}
}

private void showBraveRewardsOnboardingModal(View root) {
braveRewardsOnboardingModalView = root.findViewById(R.id.brave_rewards_onboarding_modal_id);
braveRewardsOnboardingModalView.setVisibility(View.VISIBLE);
Expand Down
59 changes: 2 additions & 57 deletions browser/brave_ads/ads_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/common/pref_names.h"
#include "brave/components/brave_rewards/browser/rewards_service_impl.h"
#include "brave/components/brave_rewards/browser/rewards_service_observer.h"
#include "brave/components/brave_rewards/browser/test/common/rewards_browsertest_util.h"
#include "brave/components/constants/brave_paths.h"
#include "brave/components/l10n/browser/locale_helper_mock.h"
Expand Down Expand Up @@ -87,34 +86,6 @@ std::unique_ptr<net::test_server::HttpResponse> HandleRequest(

} // namespace

class TestRewardsServiceObserver
: public brave_rewards::RewardsServiceObserver {
public:
TestRewardsServiceObserver() = default;
~TestRewardsServiceObserver() override = default;

void WaitForRewardsInitialization() {
if (rewards_initialized_) {
return;
}

run_loop_ = std::make_unique<base::RunLoop>();
run_loop_->Run();
}

// RewardsServiceObserver:
void OnRewardsInitialized(brave_rewards::RewardsService* service) override {
rewards_initialized_ = true;
if (run_loop_) {
run_loop_->Quit();
}
}

private:
std::unique_ptr<base::RunLoop> run_loop_;
bool rewards_initialized_ = false;
};

class BraveAdsBrowserTest : public InProcessBrowserTest,
public base::SupportsWeakPtr<BraveAdsBrowserTest> {
public:
Expand Down Expand Up @@ -149,26 +120,19 @@ class BraveAdsBrowserTest : public InProcessBrowserTest,

rewards_service_ = static_cast<brave_rewards::RewardsServiceImpl*>(
brave_rewards::RewardsServiceFactory::GetForProfile(browser_profile));
rewards_service_->AddObserver(&rewards_service_observer_);

rewards_service_->ForTestingSetTestResponseCallback(base::BindRepeating(
&BraveAdsBrowserTest::GetTestResponse, base::Unretained(this)));

rewards_service_->SetLedgerEnvForTesting();

rewards_browsertest_util::StartProcess(rewards_service_);

ads_service_ = static_cast<brave_ads::AdsService*>(
brave_ads::AdsServiceFactory::GetForProfile(browser_profile));
ASSERT_NE(nullptr, ads_service_);
}

void TearDownOnMainThread() override {
// Code here will be called immediately after each test (right before the
// destructor)
rewards_service_->RemoveObserver(&rewards_service_observer_);

InProcessBrowserTest::TearDownOnMainThread();
}

void GetTestDataDir(base::FilePath* test_data_dir) {
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_TRUE(base::PathService::Get(brave::DIR_TEST_DATA, test_data_dir));
Expand Down Expand Up @@ -379,8 +343,6 @@ class BraveAdsBrowserTest : public InProcessBrowserTest,

raw_ptr<brave_ads::AdsService> ads_service_ = nullptr;

TestRewardsServiceObserver rewards_service_observer_;

std::unique_ptr<brave_l10n::LocaleHelperMock> locale_helper_mock_;
const std::string newly_supported_locale_ = "en_830";

Expand Down Expand Up @@ -1062,23 +1024,6 @@ const BraveAdsUpgradePathParamInfo kTests[] = {
false /*ads_enabled*/
}};

IN_PROC_BROWSER_TEST_P(BraveAdsUpgradeBrowserTest, PRE_UpgradePath) {
// Handled in |MaybeMockLocaleHelperForBraveAdsUpgradePath|

const ::testing::TestInfo* const test_info =
::testing::UnitTest::GetInstance()->current_test_info();
ASSERT_NE(nullptr, test_info);
const std::string test_name = test_info->name();

// Wait for Brave Rewards to be initialized before proceeding with
// tests that rely on Rewards
if ((test_name.find("WithRewardsEnabled_") != std::string::npos ||
test_name.find("WithRewardsAndAdsEnabled_") != std::string::npos) &&
!rewards_service_->IsInitialized()) {
rewards_service_observer_.WaitForRewardsInitialization();
}
}

IN_PROC_BROWSER_TEST_P(BraveAdsUpgradeBrowserTest, UpgradePath) {
BraveAdsUpgradePathParamInfo param(GetParam());

Expand Down
13 changes: 0 additions & 13 deletions browser/brave_rewards/android/brave_rewards_native_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -354,19 +354,6 @@ void BraveRewardsNativeWorker::FetchGrants(JNIEnv* env) {
}
}

void BraveRewardsNativeWorker::StartProcess(JNIEnv* env) {
if (brave_rewards_service_) {
brave_rewards_service_->StartProcess(base::BindOnce(
&BraveRewardsNativeWorker::OnStartProcess, weak_factory_.GetWeakPtr()));
}
}

void BraveRewardsNativeWorker::OnStartProcess() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_BraveRewardsNativeWorker_OnStartProcess(
env, weak_java_brave_rewards_native_worker_.get(env));
}

void BraveRewardsNativeWorker::GetCurrentBalanceReport(JNIEnv* env) {
if (brave_rewards_service_) {
auto now = base::Time::Now();
Expand Down
Loading