Skip to content

Commit

Permalink
Inline tip
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed May 26, 2020
1 parent 96f11af commit 69c4697
Show file tree
Hide file tree
Showing 36 changed files with 219 additions and 73 deletions.
19 changes: 11 additions & 8 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -875,14 +875,15 @@ BraveRewardsGetAllNotificationsFunction::Run() {
return RespondNow(OneArgument(std::move(list)));
}

BraveRewardsGetInlineTipSettingFunction::
~BraveRewardsGetInlineTipSettingFunction() {
BraveRewardsGetInlineTippingPlatformEnabledFunction::
~BraveRewardsGetInlineTippingPlatformEnabledFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsGetInlineTipSettingFunction::Run() {
std::unique_ptr<brave_rewards::GetInlineTipSetting::Params> params(
brave_rewards::GetInlineTipSetting::Params::Create(*args_));
BraveRewardsGetInlineTippingPlatformEnabledFunction::Run() {
std::unique_ptr<brave_rewards::GetInlineTippingPlatformEnabled::Params>
params(brave_rewards::GetInlineTippingPlatformEnabled::Params::Create(
*args_));

Profile* profile = Profile::FromBrowserContext(browser_context());
RewardsService* rewards_service =
Expand All @@ -891,15 +892,17 @@ BraveRewardsGetInlineTipSettingFunction::Run() {
return RespondNow(OneArgument(std::make_unique<base::Value>(false)));
}

rewards_service->GetInlineTipSetting(
rewards_service->GetInlineTippingPlatformEnabled(
params->key,
base::BindOnce(
&BraveRewardsGetInlineTipSettingFunction::OnInlineTipSetting,
&BraveRewardsGetInlineTippingPlatformEnabledFunction::
OnInlineTipSetting,
this));
return RespondLater();
}

void BraveRewardsGetInlineTipSettingFunction::OnInlineTipSetting(bool value) {
void BraveRewardsGetInlineTippingPlatformEnabledFunction::OnInlineTipSetting(
bool value) {
Respond(OneArgument(std::make_unique<base::Value>(value)));
}

Expand Down
9 changes: 6 additions & 3 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,15 @@ class BraveRewardsGetAllNotificationsFunction : public ExtensionFunction {
ResponseAction Run() override;
};

class BraveRewardsGetInlineTipSettingFunction : public ExtensionFunction {
class BraveRewardsGetInlineTippingPlatformEnabledFunction :
public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getInlineTipSetting", UNKNOWN)
DECLARE_EXTENSION_FUNCTION(
"braveRewards.getInlineTippingPlatformEnabled",
UNKNOWN)

protected:
~BraveRewardsGetInlineTipSettingFunction() override;
~BraveRewardsGetInlineTippingPlatformEnabledFunction() override;

ResponseAction Run() override;

Expand Down
12 changes: 7 additions & 5 deletions browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnGetOneTimeTips(
std::unique_ptr<brave_rewards::ContentSiteList> list);

void SetInlineTipSetting(const base::ListValue* args);
void SetInlineTippingPlatformEnabled(const base::ListValue* args);

void GetPendingContributions(const base::ListValue* args);
void OnGetPendingContributions(
Expand Down Expand Up @@ -425,8 +425,9 @@ void RewardsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("brave_rewards.getRewardsMainEnabled",
base::BindRepeating(&RewardsDOMHandler::GetRewardsMainEnabled,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.setInlineTipSetting",
base::BindRepeating(&RewardsDOMHandler::SetInlineTipSetting,
web_ui()->RegisterMessageCallback(
"brave_rewards.setInlineTippingPlatformEnabled",
base::BindRepeating(&RewardsDOMHandler::SetInlineTippingPlatformEnabled,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.getPendingContributions",
base::BindRepeating(&RewardsDOMHandler::GetPendingContributions,
Expand Down Expand Up @@ -1451,15 +1452,16 @@ void RewardsDOMHandler::OnRecurringTipRemoved(
"brave_rewards.recurringTipRemoved", base::Value(success));
}

void RewardsDOMHandler::SetInlineTipSetting(const base::ListValue* args) {
void RewardsDOMHandler::SetInlineTippingPlatformEnabled(
const base::ListValue* args) {
std::string key;
args->GetString(0, &key);

std::string value;
args->GetString(1, &value);

if (rewards_service_) {
rewards_service_->SetInlineTipSetting(key, value == "true");
rewards_service_->SetInlineTippingPlatformEnabled(key, value == "true");
}
}

Expand Down
2 changes: 1 addition & 1 deletion common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@
]
},
{
"name": "getInlineTipSetting",
"name": "getInlineTippingPlatformEnabled",
"type": "function",
"description": "Gets setting for inline tip for provided key",
"parameters": [
Expand Down
6 changes: 3 additions & 3 deletions components/brave_ads/browser/ads_service_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ class MockRewardsService : public RewardsService {
void(const std::string&,
const std::map<std::string, std::string>&,
brave_rewards::SaveMediaInfoCallback));
MOCK_METHOD2(SetInlineTipSetting,
MOCK_METHOD2(SetInlineTippingPlatformEnabled,
void(const std::string& key, bool enabled));
MOCK_METHOD2(GetInlineTipSetting,
MOCK_METHOD2(GetInlineTippingPlatformEnabled,
void(const std::string& key,
brave_rewards::GetInlineTipSettingCallback callback));
brave_rewards::GetInlineTippingPlatformEnabledCallback));
MOCK_METHOD3(GetShareURL,
void(const std::string& type,
const std::map<std::string, std::string>& args,
Expand Down
3 changes: 3 additions & 0 deletions components/brave_rewards/browser/rewards_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ void RewardsService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterStringPref(prefs::kStateAnonymousCardId, "");
registry->RegisterStringPref(prefs::kStateRecoverySeed, "");
registry->RegisterStringPref(prefs::kStatePaymentId, "");
registry->RegisterBooleanPref(prefs::kStateInlineTipRedditEnabled, false);
registry->RegisterBooleanPref(prefs::kStateInlineTipTwitterEnabled, false);
registry->RegisterBooleanPref(prefs::kStateInlineTipGithubEnabled, false);
}

} // namespace brave_rewards
10 changes: 6 additions & 4 deletions components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ using RefreshPublisherCallback =
base::OnceCallback<void(uint32_t, const std::string&)>;
using SaveMediaInfoCallback =
base::OnceCallback<void(std::unique_ptr<brave_rewards::ContentSite>)>;
using GetInlineTipSettingCallback = base::OnceCallback<void(bool)>;
using GetInlineTippingPlatformEnabledCallback = base::OnceCallback<void(bool)>;
using GetShareURLCallback = base::OnceCallback<void(const std::string&)>;
using GetPendingContributionsCallback = base::OnceCallback<void(
std::unique_ptr<brave_rewards::PendingContributionInfoList>)>;
Expand Down Expand Up @@ -282,11 +282,13 @@ class RewardsService : public KeyedService {
const std::map<std::string, std::string>& args,
SaveMediaInfoCallback callback) = 0;

virtual void SetInlineTipSetting(const std::string& key, bool enabled) = 0;
virtual void SetInlineTippingPlatformEnabled(
const std::string& key,
bool enabled) = 0;

virtual void GetInlineTipSetting(
virtual void GetInlineTippingPlatformEnabled(
const std::string& key,
GetInlineTipSettingCallback callback) = 0;
GetInlineTippingPlatformEnabledCallback callback) = 0;

virtual void GetShareURL(
const std::string& type,
Expand Down
35 changes: 27 additions & 8 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,22 @@ net::NetworkTrafficAnnotationTag GetNetworkTrafficAnnotationTagForURLLoad() {
})");
}

ledger::InlineTipsPlatforms ConvertInlineTipStringToPlatform(
const std::string& key) {
if (key == "reddit") {
return ledger::InlineTipsPlatforms::REDDIT;
}
if (key == "twitter") {
return ledger::InlineTipsPlatforms::TWITTER;
}
if (key == "github") {
return ledger::InlineTipsPlatforms::GITHUB;
}

NOTREACHED();
return ledger::InlineTipsPlatforms::TWITTER;
}

const char pref_prefix[] = "brave.rewards.";

} // namespace
Expand Down Expand Up @@ -2542,23 +2558,26 @@ RewardsServiceImpl::GetAllNotifications() {
return notification_service_->GetAllNotifications();
}

void RewardsServiceImpl::SetInlineTipSetting(const std::string& key,
bool enabled) {
bat_ledger_->SetInlineTipSetting(key, enabled);
void RewardsServiceImpl::SetInlineTippingPlatformEnabled(
const std::string& key,
bool enabled) {
const auto platform = ConvertInlineTipStringToPlatform(key);
bat_ledger_->SetInlineTippingPlatformEnabled(platform, enabled);
}

void RewardsServiceImpl::GetInlineTipSetting(
void RewardsServiceImpl::GetInlineTippingPlatformEnabled(
const std::string& key,
GetInlineTipSettingCallback callback) {
bat_ledger_->GetInlineTipSetting(
key,
GetInlineTippingPlatformEnabledCallback callback) {
const auto platform = ConvertInlineTipStringToPlatform(key);
bat_ledger_->GetInlineTippingPlatformEnabled(
platform,
base::BindOnce(&RewardsServiceImpl::OnInlineTipSetting,
AsWeakPtr(),
std::move(callback)));
}

void RewardsServiceImpl::OnInlineTipSetting(
GetInlineTipSettingCallback callback,
GetInlineTippingPlatformEnabledCallback callback,
bool enabled) {
std::move(callback).Run(enabled);
}
Expand Down
12 changes: 8 additions & 4 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,13 @@ class RewardsServiceImpl : public RewardsService,
const std::map<std::string, std::string>& args,
SaveMediaInfoCallback callback) override;

void SetInlineTipSetting(const std::string& key, bool enabled) override;
void SetInlineTippingPlatformEnabled(
const std::string& key,
bool enabled) override;

void GetInlineTipSetting(
void GetInlineTippingPlatformEnabled(
const std::string& key,
GetInlineTipSettingCallback callback) override;
GetInlineTippingPlatformEnabledCallback callback) override;

void GetShareURL(
const std::string& type,
Expand Down Expand Up @@ -378,7 +380,9 @@ class RewardsServiceImpl : public RewardsService,
GetRecurringTipsCallback callback,
ledger::PublisherInfoList list);

void OnInlineTipSetting(GetInlineTipSettingCallback callback, bool enabled);
void OnInlineTipSetting(
GetInlineTippingPlatformEnabledCallback callback,
bool enabled);

void OnShareURL(GetShareURLCallback callback, const std::string& url);

Expand Down
9 changes: 9 additions & 0 deletions components/brave_rewards/browser/rewards_state_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,13 @@ IN_PROC_BROWSER_TEST_F(RewardsStateBrowserTest, State_2) {
EXPECT_EQ(
profile_->GetPrefs()->GetBoolean("brave.rewards.ac.enabled"),
true);
EXPECT_EQ(
profile_->GetPrefs()->GetBoolean("brave.rewards.inline_tip.reddit"),
true);
EXPECT_EQ(
profile_->GetPrefs()->GetBoolean("brave.rewards.inline_tip.twitter"),
false);
EXPECT_EQ(
profile_->GetPrefs()->GetBoolean("brave.rewards.inline_tip.github"),
false);
}
3 changes: 3 additions & 0 deletions components/brave_rewards/common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@ const char kStateCreationStamp[] = "brave.rewards.creation_stamp";
const char kStateAnonymousCardId[] = "brave.rewards.wallet.anonymous_card_id";
const char kStateRecoverySeed[] = "brave.rewards.wallet.seed";
const char kStatePaymentId[] = "brave.rewards.wallet.payment_id";
const char kStateInlineTipRedditEnabled[] = "brave.rewards.inline_tip.reddit";
const char kStateInlineTipTwitterEnabled[] = "brave.rewards.inline_tip.twitter";
const char kStateInlineTipGithubEnabled[] = "brave.rewards.inline_tip.github";
} // namespace prefs
} // namespace brave_rewards
3 changes: 3 additions & 0 deletions components/brave_rewards/common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ extern const char kStateCreationStamp[];
extern const char kStateAnonymousCardId[];
extern const char kStateRecoverySeed[];
extern const char kStatePaymentId[];
extern const char kStateInlineTipRedditEnabled[];
extern const char kStateInlineTipTwitterEnabled[];
extern const char kStateInlineTipGithubEnabled[];

extern const char kUseRewardsStagingServer[];
} // namespace prefs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ const rewardsReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State
let inlineTip = state.inlineTip

inlineTip[key] = value
chrome.send('brave_rewards.setInlineTipSetting', [key, value.toString()])
chrome.send('brave_rewards.setInlineTippingPlatformEnabled', [key, value.toString()])

state = {
...state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
// Must return true for asynchronous calls to sendResponse
return true
}
case 'inlineTipSetting': {
case 'inlineTippingPlatformEnabled': {
// Check if inline tip is enabled
chrome.braveRewards.getInlineTipSetting(msg.key, function (enabled: boolean) {
chrome.braveRewards.getInlineTippingPlatformEnabled(msg.key, function (enabled: boolean) {
sendResponse({ enabled })
})
// Must return true for asynchronous calls to sendResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ const configureBraveTipAction = () => {
clearTimeout(timeout)
chrome.runtime.sendMessage('rewardsEnabled', function (rewards) {
const msg = {
type: 'inlineTipSetting',
type: 'inlineTippingPlatformEnabled',
key: 'github'
}
chrome.runtime.sendMessage(msg, function (inlineTip) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ const configureBraveTipAction = () => {
clearTimeout(timeout)
chrome.runtime.sendMessage('rewardsEnabled', function (rewards) {
const msg = {
type: 'inlineTipSetting',
type: 'inlineTippingPlatformEnabled',
key: 'reddit'
}
chrome.runtime.sendMessage(msg, function (inlineTip) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ const configureBraveTipAction = () => {
clearTimeout(timeout)
chrome.runtime.sendMessage('rewardsEnabled', function (rewards) {
const msg = {
type: 'inlineTipSetting',
type: 'inlineTippingPlatformEnabled',
key: 'twitter'
}
chrome.runtime.sendMessage(msg, function (inlineTip) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ const rewardsReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State
let inlineTip = state.inlineTip

inlineTip[key] = value
chrome.send('brave_rewards.setInlineTipSetting', [key, value.toString()])
chrome.send('brave_rewards.setInlineTippingPlatformEnabled', [key, value.toString()])

state = {
...state,
Expand Down
2 changes: 1 addition & 1 deletion components/definitions/chromel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ declare namespace chrome.braveRewards {
}
const refreshPublisher: (publisherKey: string, callback: (status: number, publisherKey: string) => void) => {}
const getAllNotifications: (callback: (list: RewardsExtension.Notification[]) => void) => {}
const getInlineTipSetting: (key: string, callback: (enabled: boolean) => void) => {}
const getInlineTippingPlatformEnabled: (key: string, callback: (enabled: boolean) => void) => {}
const fetchBalance: (callback: (balance: RewardsExtension.Balance) => void) => {}
const onReconcileComplete: {
addListener: (callback: (result: number, type: number) => void) => void
Expand Down
14 changes: 8 additions & 6 deletions components/services/bat_ledger/bat_ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -677,14 +677,16 @@ void BatLedgerImpl::StartMonthlyContribution() {
ledger_->StartMonthlyContribution();
}

void BatLedgerImpl::SetInlineTipSetting(const std::string& key, bool enabled) {
ledger_->SetInlineTipSetting(key, enabled);
void BatLedgerImpl::SetInlineTippingPlatformEnabled(
const ledger::InlineTipsPlatforms platform,
bool enabled) {
ledger_->SetInlineTippingPlatformEnabled(platform, enabled);
}

void BatLedgerImpl::GetInlineTipSetting(
const std::string& key,
GetInlineTipSettingCallback callback) {
std::move(callback).Run(ledger_->GetInlineTipSetting(key));
void BatLedgerImpl::GetInlineTippingPlatformEnabled(
const ledger::InlineTipsPlatforms platform,
GetInlineTippingPlatformEnabledCallback callback) {
std::move(callback).Run(ledger_->GetInlineTippingPlatformEnabled(platform));
}

void BatLedgerImpl::GetShareURL(
Expand Down
10 changes: 6 additions & 4 deletions components/services/bat_ledger/bat_ledger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,13 @@ class BatLedgerImpl : public mojom::BatLedger,
const base::flat_map<std::string, std::string>& args,
SaveMediaInfoCallback callback) override;

void SetInlineTipSetting(const std::string& key, bool enabled) override;
void SetInlineTippingPlatformEnabled(
const ledger::InlineTipsPlatforms platform,
bool enabled) override;

void GetInlineTipSetting(
const std::string& key,
GetInlineTipSettingCallback callback) override;
void GetInlineTippingPlatformEnabled(
const ledger::InlineTipsPlatforms platform,
GetInlineTippingPlatformEnabledCallback callback) override;

void GetShareURL(
const std::string& type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ interface BatLedger {
SaveMediaInfo(string type, map<string, string> args) =>
(ledger.mojom.Result result, ledger.mojom.PublisherInfo? publisher_info);

SetInlineTipSetting(string key, bool enabled);
SetInlineTippingPlatformEnabled(ledger.mojom.InlineTipsPlatforms platform, bool enabled);

GetInlineTipSetting(string key) => (bool enabled);
GetInlineTippingPlatformEnabled(ledger.mojom.InlineTipsPlatforms platform) => (bool enabled);

GetShareURL(string type, map<string, string> args) =>
(string url);
Expand Down
Loading

0 comments on commit 69c4697

Please sign in to comment.