Skip to content

Enable Greaselion features only after rules are ready #10001

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

Merged
merged 1 commit into from
Sep 9, 2021
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
39 changes: 27 additions & 12 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@
#include "url/url_canon_stdstring.h"
#include "url/url_util.h"

#if BUILDFLAG(ENABLE_GREASELION)
#include "brave/components/greaselion/browser/greaselion_service.h"
#endif

#if BUILDFLAG(ENABLE_IPFS)
#include "brave/components/ipfs/ipfs_constants.h"
#include "brave/components/ipfs/ipfs_utils.h"
Expand Down Expand Up @@ -340,10 +336,23 @@ RewardsServiceImpl::RewardsServiceImpl(Profile* profile)

if (base::FeatureList::IsEnabled(features::kVerboseLoggingFeature))
persist_log_level_ = kDiagnosticLogMaxVerboseLevel;

#if BUILDFLAG(ENABLE_GREASELION)
if (greaselion_service_) {
greaselion_service_->AddObserver(this);
}
#endif
}

RewardsServiceImpl::~RewardsServiceImpl() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

#if BUILDFLAG(ENABLE_GREASELION)
if (greaselion_service_) {
greaselion_service_->RemoveObserver(this);
}
#endif

if (ledger_database_) {
file_task_runner_->DeleteSoon(FROM_HERE, ledger_database_.release());
}
Expand Down Expand Up @@ -381,7 +390,6 @@ void RewardsServiceImpl::Init(

CheckPreferences();
InitPrefChangeRegistrar();
EnableGreaseLion();
}

void RewardsServiceImpl::InitPrefChangeRegistrar() {
Expand Down Expand Up @@ -1373,8 +1381,8 @@ void RewardsServiceImpl::GetReconcileStamp(GetReconcileStampCallback callback) {
bat_ledger_->GetReconcileStamp(std::move(callback));
}

void RewardsServiceImpl::EnableGreaseLion() {
#if BUILDFLAG(ENABLE_GREASELION)
void RewardsServiceImpl::EnableGreaseLion() {
if (!greaselion_service_) {
return;
}
Expand All @@ -1399,9 +1407,14 @@ void RewardsServiceImpl::EnableGreaseLion() {
greaselion::GITHUB_TIPS,
profile_->GetPrefs()->GetBoolean(prefs::kInlineTipGithubEnabled) &&
!hide_button);
#endif
}

void RewardsServiceImpl::OnRulesReady(
greaselion::GreaselionService* greaselion_service) {
EnableGreaseLion();
}
#endif

void RewardsServiceImpl::StopLedger(StopLedgerCallback callback) {
BLOG(1, "Shutting down ledger process");
if (!Connected()) {
Expand Down Expand Up @@ -1729,11 +1742,13 @@ void RewardsServiceImpl::OnFetchBalanceForEnableRewards(
}

void RewardsServiceImpl::OnAdsEnabled(bool ads_enabled) {
#if BUILDFLAG(ENABLE_GREASELION)
greaselion_service_->SetFeatureEnabled(
greaselion::ADS,
profile_->GetPrefs()->GetBoolean(ads::prefs::kEnabled));
#endif
#if BUILDFLAG(ENABLE_GREASELION)
if (greaselion_service_) {
greaselion_service_->SetFeatureEnabled(
greaselion::ADS,
profile_->GetPrefs()->GetBoolean(ads::prefs::kEnabled));
}
#endif

for (auto& observer : observers_) {
observer.OnAdsEnabled(this, ads_enabled);
Expand Down
22 changes: 14 additions & 8 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
#include "brave/components/safetynet/safetynet_check.h"
#endif

#if BUILDFLAG(ENABLE_GREASELION)
#include "brave/components/greaselion/browser/greaselion_service.h"
#endif

namespace base {
class OneShotTimer;
class RepeatingTimer;
Expand All @@ -61,12 +65,6 @@ namespace network {
class SimpleURLLoader;
} // namespace network

#if BUILDFLAG(ENABLE_GREASELION)
namespace greaselion {
class GreaselionService;
} // namespace greaselion
#endif

class Profile;
class RewardsFlagBrowserTest;

Expand Down Expand Up @@ -96,6 +94,9 @@ using StopLedgerCallback = base::OnceCallback<void(ledger::type::Result)>;

class RewardsServiceImpl : public RewardsService,
public ledger::LedgerClient,
#if BUILDFLAG(ENABLE_GREASELION)
public greaselion::GreaselionService::Observer,
#endif
public base::SupportsWeakPtr<RewardsServiceImpl> {
public:
#if BUILDFLAG(ENABLE_GREASELION)
Expand Down Expand Up @@ -361,6 +362,13 @@ class RewardsServiceImpl : public RewardsService,
using SimpleURLLoaderList =
std::list<std::unique_ptr<network::SimpleURLLoader>>;

#if BUILDFLAG(ENABLE_GREASELION)
void EnableGreaseLion();

// GreaselionService::Observer:
void OnRulesReady(greaselion::GreaselionService* greaselion_service) override;
#endif

void OnConnectionClosed(const ledger::type::Result result);

void InitPrefChangeRegistrar();
Expand All @@ -371,8 +379,6 @@ class RewardsServiceImpl : public RewardsService,

void StartLedgerProcessIfNecessary();

void EnableGreaseLion();

void OnStopLedger(
StopLedgerCallback callback,
const ledger::type::Result result);
Expand Down
3 changes: 2 additions & 1 deletion components/greaselion/browser/greaselion_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ class GreaselionService : public KeyedService,
// implementation of our own observers
class Observer : public base::CheckedObserver {
public:
virtual void OnRulesReady(GreaselionService* greaselion_service) {}
virtual void OnExtensionsReady(GreaselionService* greaselion_service,
bool success) = 0;
bool success) {}
};
virtual void AddObserver(Observer* observer) = 0;
virtual void RemoveObserver(Observer* observer) = 0;
Expand Down
16 changes: 13 additions & 3 deletions components/greaselion/browser/greaselion_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ GreaselionServiceImpl::GreaselionServiceImpl(
browser_version_(
version_info::GetBraveVersionWithoutChromiumMajorVersion()),
weak_factory_(this) {
download_service_->AddObserver(this);
extension_registry_->AddObserver(this);
for (int i = FIRST_FEATURE; i != LAST_FEATURE; i++)
state_[static_cast<GreaselionFeature>(i)] = false;
Expand All @@ -241,6 +242,7 @@ GreaselionServiceImpl::GreaselionServiceImpl(
}

GreaselionServiceImpl::~GreaselionServiceImpl() {
download_service_->RemoveObserver(this);
extension_registry_->RemoveObserver(this);
}

Expand Down Expand Up @@ -367,11 +369,12 @@ void GreaselionServiceImpl::OnExtensionUnloaded(
}
}

void GreaselionServiceImpl::AddObserver(Observer* observer) {
void GreaselionServiceImpl::AddObserver(GreaselionService::Observer* observer) {
observers_.AddObserver(observer);
}

void GreaselionServiceImpl::RemoveObserver(Observer* observer) {
void GreaselionServiceImpl::RemoveObserver(
GreaselionService::Observer* observer) {
observers_.RemoveObserver(observer);
}

Expand All @@ -382,12 +385,19 @@ void GreaselionServiceImpl::MaybeNotifyObservers() {
update_pending_ = false;
UpdateInstalledExtensions();
} else {
for (Observer& observer : observers_)
for (auto& observer : observers_)
observer.OnExtensionsReady(this, all_rules_installed_successfully_);
}
}
}

void GreaselionServiceImpl::OnRulesReady(
GreaselionDownloadService* download_service) {
for (auto& observer : observers_) {
observer.OnRulesReady(this);
}
}

void GreaselionServiceImpl::SetFeatureEnabled(GreaselionFeature feature,
bool enabled) {
DCHECK(feature >= 0 && feature < LAST_FEATURE);
Expand Down
15 changes: 9 additions & 6 deletions components/greaselion/browser/greaselion_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "base/memory/weak_ptr.h"
#include "base/path_service.h"
#include "base/version.h"
#include "brave/components/greaselion/browser/greaselion_download_service.h"
#include "brave/components/greaselion/browser/greaselion_service.h"
#include "extensions/common/extension_id.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
Expand All @@ -36,9 +37,8 @@ class ExtensionSystem;

namespace greaselion {

class GreaselionDownloadService;

class GreaselionServiceImpl : public GreaselionService {
class GreaselionServiceImpl : public GreaselionService,
public GreaselionDownloadService::Observer {
public:
explicit GreaselionServiceImpl(
GreaselionDownloadService* download_service,
Expand All @@ -54,8 +54,8 @@ class GreaselionServiceImpl : public GreaselionService {
bool IsGreaselionExtension(const std::string& id) override;
std::vector<extensions::ExtensionId> GetExtensionIdsForTesting() override;
bool ready() override;
void AddObserver(Observer* observer) override;
void RemoveObserver(Observer* observer) override;
void AddObserver(GreaselionService::Observer* observer) override;
void RemoveObserver(GreaselionService::Observer* observer) override;

// ExtensionRegistryObserver overrides
void OnExtensionReady(content::BrowserContext* browser_context,
Expand All @@ -75,6 +75,9 @@ class GreaselionServiceImpl : public GreaselionService {
void Install(scoped_refptr<extensions::Extension> extension);
void MaybeNotifyObservers();

// GreaselionDownloadService::Observer:
void OnRulesReady(GreaselionDownloadService* download_service) override;

GreaselionDownloadService* download_service_; // NOT OWNED
GreaselionFeatures state_;
const base::FilePath install_directory_;
Expand All @@ -86,7 +89,7 @@ class GreaselionServiceImpl : public GreaselionService {
bool update_pending_;
int pending_installs_;
scoped_refptr<base::SequencedTaskRunner> task_runner_;
base::ObserverList<Observer> observers_;
base::ObserverList<GreaselionService::Observer> observers_;
std::vector<extensions::ExtensionId> greaselion_extensions_;
std::vector<base::ScopedTempDir> extension_dirs_;
base::Version browser_version_;
Expand Down