Skip to content

Commit

Permalink
Uplift of #3848 (squashed) to dev
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-browser-releases committed Dec 18, 2019
1 parent 668fa84 commit 282e30b
Show file tree
Hide file tree
Showing 27 changed files with 482 additions and 123 deletions.
13 changes: 13 additions & 0 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,19 @@ ExtensionFunction::ResponseAction BraveRewardsGetPublisherDataFunction::Run() {
return RespondNow(NoArguments());
}

BraveRewardsGetWalletPropertiesFunction::
~BraveRewardsGetWalletPropertiesFunction() = default;

ExtensionFunction::ResponseAction
BraveRewardsGetWalletPropertiesFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
auto* rewards_service = RewardsServiceFactory::GetForProfile(profile);
if (rewards_service) {
rewards_service->FetchWalletProperties();
}
return RespondNow(NoArguments());
}

BraveRewardsGetCurrentReportFunction::~BraveRewardsGetCurrentReportFunction() {
}

Expand Down
10 changes: 10 additions & 0 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ class BraveRewardsGetPublisherDataFunction : public ExtensionFunction {
ResponseAction Run() override;
};

class BraveRewardsGetWalletPropertiesFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getWalletProperties", UNKNOWN)

protected:
~BraveRewardsGetWalletPropertiesFunction() override;

ResponseAction Run() override;
};

class BraveRewardsGetCurrentReportFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getCurrentReport", UNKNOWN)
Expand Down
21 changes: 16 additions & 5 deletions browser/ui/webui/brave_tip_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/memory/weak_ptr.h"
#include "base/strings/utf_string_conversions.h"
Expand Down Expand Up @@ -177,6 +178,15 @@ void RewardsTipDOMHandler::GetWalletProperties(const base::ListValue* args) {
rewards_service_->FetchWalletProperties();
}

static std::unique_ptr<base::ListValue> CreateListOfDoubles(
const std::vector<double>& items) {
auto result = std::make_unique<base::ListValue>();
for (double const& item : items) {
result->AppendDouble(item);
}
return result;
}

void RewardsTipDOMHandler::OnWalletProperties(
brave_rewards::RewardsService* rewards_service,
int error_code,
Expand All @@ -191,11 +201,12 @@ void RewardsTipDOMHandler::OnWalletProperties(
auto walletInfo = std::make_unique<base::DictionaryValue>();

if (error_code == 0 && wallet_properties) {
auto choices = std::make_unique<base::ListValue>();
for (double const& choice : wallet_properties->parameters_choices) {
choices->AppendDouble(choice);
}
walletInfo->SetList("choices", std::move(choices));
walletInfo->SetList("choices",
CreateListOfDoubles(wallet_properties->parameters_choices));
walletInfo->SetList("defaultTipChoices",
CreateListOfDoubles(wallet_properties->default_tip_choices));
walletInfo->SetList("defaultMonthlyTipChoices",
CreateListOfDoubles(wallet_properties->default_monthly_tip_choices));
}

result.SetDictionary("wallet", std::move(walletInfo));
Expand Down
46 changes: 19 additions & 27 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,34 +156,20 @@
"name": "properties",
"type": "object",
"properties": {
"probi": {
"type": "string",
"description": "balance represented in probis"
},
"balance": {
"type": "double",
"description": "balance"
"defaultTipChoices": {
"type": "array",
"description": "default tip choices for one-time tips",
"items": {
"type": "number",
"minimum": 0
}
},
"rates": {
"type": "object",
"description": "rates for different currencies",
"properties": {
"BTC": {
"type": "double",
"description": "BTC rate"
},
"ETH": {
"type": "double",
"description": "ETH rate"
},
"USD": {
"type": "double",
"description": "USD rate"
},
"EUR": {
"type": "double",
"description": "EUR rate"
}
"defaultMonthlyTipChoices": {
"type": "array",
"description": "default tip choices for recurring tips",
"items": {
"type": "number",
"minimum": 0
}
}
}
Expand Down Expand Up @@ -566,6 +552,12 @@
}
]
},
{
"name": "getWalletProperties",
"type": "function",
"description": "Get default values that we get from the server",
"parameters": []
},
{
"name": "getCurrentReport",
"type": "function",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ void ExtensionRewardsServiceObserver::OnWalletProperties(
std::unique_ptr<brave_rewards::WalletProperties> wallet_properties) {
auto* event_router =
extensions::EventRouter::Get(profile_);
if (!event_router) {
return;
}

if (error_code == 17) { // ledger::Result::CORRUPT_WALLET
std::unique_ptr<base::ListValue> args(
extensions::api::brave_rewards::OnWalletInitialized::Create(
Expand All @@ -64,6 +68,26 @@ void ExtensionRewardsServiceObserver::OnWalletProperties(
event_router->BroadcastEvent(std::move(event));
return;
}

if (!wallet_properties) {
return;
}

extensions::api::brave_rewards::OnWalletProperties::Properties properties;

properties.default_tip_choices = wallet_properties->default_tip_choices;
properties.default_monthly_tip_choices =
wallet_properties->default_monthly_tip_choices;

std::unique_ptr<base::ListValue> args(
extensions::api::brave_rewards::OnWalletProperties::Create(properties)
.release());

std::unique_ptr<extensions::Event> event(new extensions::Event(
extensions::events::BRAVE_ON_WALLET_PROPERTIES,
extensions::api::brave_rewards::OnWalletProperties::kEventName,
std::move(args)));
event_router->BroadcastEvent(std::move(event));
}

void ExtensionRewardsServiceObserver::OnGetCurrentBalanceReport(
Expand Down
Loading

0 comments on commit 282e30b

Please sign in to comment.