Skip to content

Commit

Permalink
Fixes twitter tips precondition in GL; Adds other inline tip providers
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Sep 11, 2020
1 parent c7da37f commit d3618b9
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 11 deletions.
49 changes: 44 additions & 5 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,36 @@ void RewardsServiceImpl::Init(
if (profile_->GetPrefs()->GetBoolean(prefs::kEnabled)) {
StartLedger();
}

InitPrefChangeRegistrar();
}

void RewardsServiceImpl::InitPrefChangeRegistrar() {
profile_pref_change_registrar_.Init(profile_->GetPrefs());
profile_pref_change_registrar_.Add(
prefs::kEnabled,
base::Bind(
&RewardsServiceImpl::OnPreferenceChanged,
base::Unretained(this)));
profile_pref_change_registrar_.Add(
prefs::kInlineTipTwitterEnabled,
base::Bind(
&RewardsServiceImpl::OnPreferenceChanged,
base::Unretained(this)));
profile_pref_change_registrar_.Add(
prefs::kInlineTipRedditEnabled,
base::Bind(
&RewardsServiceImpl::OnPreferenceChanged,
base::Unretained(this)));
profile_pref_change_registrar_.Add(
prefs::kInlineTipGithubEnabled,
base::Bind(
&RewardsServiceImpl::OnPreferenceChanged,
base::Unretained(this)));
}

void RewardsServiceImpl::OnPreferenceChanged(const std::string& key) {
EnableGreaseLion();
}

void RewardsServiceImpl::StartLedger() {
Expand Down Expand Up @@ -832,8 +862,7 @@ void RewardsServiceImpl::OnWalletInitialized(ledger::type::Result result) {
#endif
}

EnableGreaseLion(profile_->GetPrefs()->GetBoolean(
prefs::kEnabled));
EnableGreaseLion();

for (auto& observer : observers_) {
observer.OnWalletInitialized(this, result);
Expand Down Expand Up @@ -1361,11 +1390,21 @@ void RewardsServiceImpl::SetRewardsMainEnabled(bool enabled) {
}
}

void RewardsServiceImpl::EnableGreaseLion(const bool enabled) {
void RewardsServiceImpl::EnableGreaseLion() {
#if BUILDFLAG(ENABLE_GREASELION)
if (greaselion_service_) {
greaselion_service_->SetFeatureEnabled(greaselion::REWARDS, enabled);
greaselion_service_->SetFeatureEnabled(greaselion::TWITTER_TIPS, enabled);
greaselion_service_->SetFeatureEnabled(
greaselion::REWARDS,
profile_->GetPrefs()->GetBoolean(prefs::kEnabled));
greaselion_service_->SetFeatureEnabled(
greaselion::TWITTER_TIPS,
profile_->GetPrefs()->GetBoolean(prefs::kInlineTipTwitterEnabled));
greaselion_service_->SetFeatureEnabled(
greaselion::REDDIT_TIPS,
profile_->GetPrefs()->GetBoolean(prefs::kInlineTipRedditEnabled));
greaselion_service_->SetFeatureEnabled(
greaselion::GITHUB_TIPS,
profile_->GetPrefs()->GetBoolean(prefs::kInlineTipGithubEnabled));
}
#endif
}
Expand Down
18 changes: 12 additions & 6 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@
#include <vector>

#include "base/containers/flat_set.h"
#include "bat/ledger/ledger.h"
#include "base/files/file_path.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/one_shot_event.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "bat/ledger/ledger.h"
#include "bat/ledger/ledger_client.h"
#include "brave/components/services/bat_ledger/public/interfaces/bat_ledger.mojom.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "brave/components/brave_rewards/browser/rewards_service_private_observer.h"
#include "brave/components/greaselion/browser/buildflags/buildflags.h"
#include "brave/components/services/bat_ledger/public/interfaces/bat_ledger.mojom.h"
#include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service.h"
#include "components/prefs/pref_change_registrar.h"
#include "content/public/browser/browser_thread.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "ui/gfx/image/image.h"
#include "brave/components/brave_rewards/browser/rewards_service_private_observer.h"

#if defined(OS_ANDROID)
#include "brave/components/safetynet/safetynet_check.h"
Expand Down Expand Up @@ -334,7 +335,11 @@ class RewardsServiceImpl : public RewardsService,
private:
friend class ::RewardsFlagBrowserTest;

void EnableGreaseLion(const bool enabled);
void InitPrefChangeRegistrar();

void OnPreferenceChanged(const std::string& key);

void EnableGreaseLion();

void OnStopLedger(
StopLedgerCallback callback,
Expand Down Expand Up @@ -747,6 +752,7 @@ class RewardsServiceImpl : public RewardsService,
current_media_fetchers_;
std::unique_ptr<base::OneShotTimer> notification_startup_timer_;
std::unique_ptr<base::RepeatingTimer> notification_periodic_timer_;
PrefChangeRegistrar profile_pref_change_registrar_;

uint32_t next_timer_id_;
bool reset_states_;
Expand Down
12 changes: 12 additions & 0 deletions components/greaselion/browser/greaselion_download_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const char kRunAt[] = "run_at";
// precondition keys
const char kRewards[] = "rewards-enabled";
const char kTwitterTips[] = "twitter-tips-enabled";
const char kRedditTips[] = "reddit-tips-enabled";
const char kGithubTips[] = "github-tips-enabled";

GreaselionPreconditionValue GreaselionRule::ParsePrecondition(
const base::Value& value) {
Expand All @@ -65,6 +67,10 @@ void GreaselionRule::Parse(base::DictionaryValue* preconditions_value,
preconditions_.rewards_enabled = condition;
} else if (kv.first == kTwitterTips) {
preconditions_.twitter_tips_enabled = condition;
} else if (kv.first == kRedditTips) {
preconditions_.reddit_tips_enabled = condition;
} else if (kv.first == kGithubTips) {
preconditions_.github_tips_enabled = condition;
} else {
LOG(INFO) << "Greaselion encountered an unknown precondition: "
<< kv.first;
Expand Down Expand Up @@ -117,6 +123,12 @@ bool GreaselionRule::Matches(GreaselionFeatures state) const {
if (!PreconditionFulfilled(preconditions_.twitter_tips_enabled,
state[greaselion::TWITTER_TIPS]))
return false;
if (!PreconditionFulfilled(preconditions_.reddit_tips_enabled,
state[greaselion::REDDIT_TIPS]))
return false;
if (!PreconditionFulfilled(preconditions_.github_tips_enabled,
state[greaselion::GITHUB_TIPS]))
return false;
return true;
}

Expand Down
2 changes: 2 additions & 0 deletions components/greaselion/browser/greaselion_download_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ enum GreaselionPreconditionValue { kMustBeFalse, kMustBeTrue, kAny };
struct GreaselionPreconditions {
GreaselionPreconditionValue rewards_enabled = kAny;
GreaselionPreconditionValue twitter_tips_enabled = kAny;
GreaselionPreconditionValue reddit_tips_enabled = kAny;
GreaselionPreconditionValue github_tips_enabled = kAny;
};

class GreaselionRule {
Expand Down
2 changes: 2 additions & 0 deletions components/greaselion/browser/greaselion_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ enum GreaselionFeature {
FIRST_FEATURE = 0,
REWARDS = FIRST_FEATURE,
TWITTER_TIPS,
REDDIT_TIPS,
GITHUB_TIPS,
LAST_FEATURE
};

Expand Down

0 comments on commit d3618b9

Please sign in to comment.