Skip to content

Commit

Permalink
move PublisherBanner to mojo type
Browse files Browse the repository at this point in the history
  • Loading branch information
cg505 committed Jun 28, 2019
1 parent a12b28c commit cafa58a
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 117 deletions.
38 changes: 19 additions & 19 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2088,31 +2088,24 @@ void RewardsServiceImpl::GetPublisherBanner(

void RewardsServiceImpl::OnPublisherBanner(
GetPublisherBannerCallback callback,
const std::string& banner) {
ledger::PublisherBannerPtr banner) {
std::unique_ptr<brave_rewards::PublisherBanner> new_banner;
new_banner.reset(new brave_rewards::PublisherBanner());

std::unique_ptr<ledger::PublisherBanner> publisher_banner;
publisher_banner.reset(new ledger::PublisherBanner());

if (!banner.empty()) {
publisher_banner->loadFromJson(banner);
}

if (!publisher_banner) {
if (!banner) {
return;
}

new_banner->publisher_key = publisher_banner->publisher_key;
new_banner->title = publisher_banner->title;
new_banner->name = publisher_banner->name;
new_banner->description = publisher_banner->description;
new_banner->background = publisher_banner->background;
new_banner->logo = publisher_banner->logo;
new_banner->amounts = publisher_banner->amounts;
new_banner->social = publisher_banner->social;
new_banner->provider = publisher_banner->provider;
new_banner->verified = publisher_banner->verified;
new_banner->publisher_key = banner->publisher_key;
new_banner->title = banner->title;
new_banner->name = banner->name;
new_banner->description = banner->description;
new_banner->background = banner->background;
new_banner->logo = banner->logo;
new_banner->amounts = banner->amounts;
new_banner->social = mojo::FlatMapToMap(banner->social);
new_banner->provider = banner->provider;
new_banner->verified = banner->verified;

std::move(callback).Run(std::move(new_banner));
}
Expand Down Expand Up @@ -3252,6 +3245,13 @@ void RewardsServiceImpl::OnFetchBalance(FetchBalanceCallback callback,
ledger::BalancePtr balance) {
auto new_balance = std::make_unique<brave_rewards::Balance>();

LOG(INFO) << "BALANCE REPORT";
LOG(INFO) << "total: " << balance->total;
for (auto const& rate : balance->rates) {
LOG(INFO) << "rate: " << rate.first << " - " << rate.second;
}


if (balance) {
new_balance->total = balance->total;
new_balance->rates = mojo::FlatMapToMap(balance->rates);
Expand Down
2 changes: 1 addition & 1 deletion components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class RewardsServiceImpl : public RewardsService,
void GetPublisherBanner(const std::string& publisher_id,
GetPublisherBannerCallback callback) override;
void OnPublisherBanner(GetPublisherBannerCallback callback,
const std::string& banner);
ledger::PublisherBannerPtr banner);
void RemoveRecurringTip(const std::string& publisher_key) override;
void OnGetRecurringTipsUI(
GetRecurringTipsCallback callback,
Expand Down
5 changes: 2 additions & 3 deletions components/services/bat_ledger/bat_ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,9 @@ void BatLedgerImpl::GetPublisherActivityFromUrl(
// static
void BatLedgerImpl::OnGetPublisherBanner(
CallbackHolder<GetPublisherBannerCallback>* holder,
std::unique_ptr<ledger::PublisherBanner> banner) {
std::string json_banner = banner.get() ? banner->ToJson() : "";
ledger::PublisherBannerPtr banner) {
if (holder->is_valid())
std::move(holder->get()).Run(json_banner);
std::move(holder->get()).Run(std::move(banner));
delete holder;
}

Expand Down
2 changes: 1 addition & 1 deletion components/services/bat_ledger/bat_ledger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class BatLedgerImpl : public mojom::BatLedger,

static void OnGetPublisherBanner(
CallbackHolder<GetPublisherBannerCallback>* holder,
std::unique_ptr<ledger::PublisherBanner> banner);
ledger::PublisherBannerPtr banner);

static void OnAddressesForPaymentId(
CallbackHolder<GetAddressesForPaymentIdCallback>* holder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ interface BatLedger {
ledger.mojom.VisitData visit_data,
string publisher_blob);
GetContributionAmount() => (double contribution_amount);
GetPublisherBanner(string publisher_id) => (string banner);
GetPublisherBanner(string publisher_id) =>
(ledger.mojom.PublisherBanner banner);

DoDirectTip(string publisher_id,
int32 amount,
Expand Down
2 changes: 1 addition & 1 deletion vendor/bat-native-ledger/include/bat/ledger/ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extern int reconcile_time; // minutes
extern bool short_retries;

using PublisherBannerCallback =
std::function<void(std::unique_ptr<ledger::PublisherBanner> banner)>;
std::function<void(ledger::PublisherBannerPtr banner)>;
using WalletAddressesCallback =
std::function<void(std::map<std::string, std::string> addresses)>;
using GetTransactionHistoryCallback =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ struct PublisherInfo {
array<ContributionInfo> contributions;
};

struct PublisherBanner {
string publisher_key;
string title;
string name;
string description;
string background;
string logo;
array<int32> amounts;
string provider;
map<string, string> social;
bool verified;
};

struct PendingContribution {
string publisher_key;
double amount;
Expand Down
22 changes: 2 additions & 20 deletions vendor/bat-native-ledger/include/bat/ledger/publisher_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace ledger {
using PublisherInfo = mojom::PublisherInfo;
using PublisherInfoPtr = mojom::PublisherInfoPtr;
using PublisherInfoList = std::vector<PublisherInfoPtr>;
using PublisherBanner = mojom::PublisherBanner;
using PublisherBannerPtr = mojom::PublisherBannerPtr;

const char kClearFavicon[] = "clear";
const char kIgnorePublisherBlob[] = "ignore";
Expand Down Expand Up @@ -93,26 +95,6 @@ LEDGER_EXPORT struct ContributionInfo {
uint64_t date;
};

LEDGER_EXPORT struct PublisherBanner {
PublisherBanner();
PublisherBanner(const PublisherBanner& info);
~PublisherBanner();

const std::string ToJson() const;
bool loadFromJson(const std::string& json);

std::string publisher_key;
std::string title;
std::string name;
std::string description;
std::string background;
std::string logo;
std::vector<int> amounts;
std::string provider;
std::map<std::string, std::string> social;
bool verified;
};

} // namespace ledger

#endif // BAT_LEDGER_PUBLISHER_INFO_HANDLER_
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "bat/ledger/internal/ledger_impl.h"
#include "bat/ledger/internal/rapidjson_bat_helper.h"
#include "bat/ledger/internal/static_values.h"
#include "mojo/public/cpp/bindings/map.h"

/* foo.bar.example.com
QLD = 'bar'
Expand Down Expand Up @@ -854,7 +855,7 @@ void BatPublishers::getPublisherBanner(
banner.title = values.banner.title_;
banner.description = values.banner.description_;
banner.amounts = values.banner.amounts_;
banner.social = values.banner.social_;
banner.social = mojo::MapToFlatMap(values.banner.social_);

// WebUI must not make external network requests, so map
// external resopurces to chrome://rewards-image and handle them
Expand All @@ -874,7 +875,7 @@ void BatPublishers::getPublisherBanner(
std::bind(&BatPublishers::onPublisherBanner,
this,
callback,
banner,
std::move(banner),
_1,
_2);

Expand All @@ -887,7 +888,7 @@ void BatPublishers::onPublisherBanner(
ledger::Result result,
ledger::PublisherInfoPtr publisher_info) {

auto new_banner = std::make_unique<ledger::PublisherBanner>(banner);
auto new_banner = ledger::PublisherBanner::New(banner);

if (!publisher_info || result != ledger::Result::LEDGER_OK) {
callback(std::move(new_banner));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace ledger {
struct AutoContributeProps;
struct BalanceReportInfo;
struct ReconcileInfo;
struct PublisherBanner;
struct ActivityInfoFilter;
struct RewardsInternalsInfo;

Expand Down Expand Up @@ -60,7 +59,6 @@ void saveToJson(JsonWriter* writer, const GRANTS_PROPERTIES_ST&);
void saveToJson(JsonWriter* writer, const ledger::AutoContributeProps&);
void saveToJson(JsonWriter* writer, const ledger::BalanceReportInfo&);
void saveToJson(JsonWriter* writer, const ledger::Grant&);
void saveToJson(JsonWriter* writer, const ledger::PublisherBanner&);
void saveToJson(JsonWriter* writer, const ledger::ActivityInfoFilter&);
void saveToJson(JsonWriter* writer, const ledger::ReconcileInfo&);
void saveToJson(JsonWriter* writer, const ledger::RewardsInternalsInfo&);
Expand Down
66 changes: 0 additions & 66 deletions vendor/bat-native-ledger/src/bat/ledger/ledger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,72 +84,6 @@ bool ActivityInfoFilter::loadFromJson(const std::string& json) {
return !error;
}

PublisherBanner::PublisherBanner() {}

PublisherBanner::PublisherBanner(const PublisherBanner& info) :
publisher_key(info.publisher_key),
title(info.title),
name(info.name),
description(info.description),
background(info.background),
logo(info.logo),
amounts(info.amounts),
provider(info.provider),
social(info.social),
verified(info.verified) {}

PublisherBanner::~PublisherBanner() {}

const std::string PublisherBanner::ToJson() const {
std::string json;
braveledger_bat_helper::saveToJsonString(*this, &json);
return json;
}

bool PublisherBanner::loadFromJson(const std::string& json) {
rapidjson::Document d;
d.Parse(json.c_str());
// has parser errors or wrong types
bool error = d.HasParseError();

if (!error) {
error = !(d.HasMember("publisher_key") && d["publisher_key"].IsString() &&
d.HasMember("title") && d["title"].IsString() &&
d.HasMember("name") && d["name"].IsString() &&
d.HasMember("description") && d["description"].IsString() &&
d.HasMember("background") && d["name"].IsString() &&
d.HasMember("logo") && d["logo"].IsString() &&
d.HasMember("amounts") && d["amounts"].IsArray() &&
d.HasMember("social") && d["social"].IsObject() &&
d.HasMember("verified") && d["verified"].IsBool());
}

if (!error) {
publisher_key = d["publisher_key"].GetString();
title = d["title"].GetString();
name = d["name"].GetString();
description = d["description"].GetString();
background = d["background"].GetString();
logo = d["logo"].GetString();
verified = d["verified"].GetBool();

for (const auto& amount : d["amounts"].GetArray()) {
amounts.push_back(amount.GetInt());
}

for (const auto& i : d["social"].GetObject()) {
social.insert(std::make_pair(i.name.GetString(),
i.value.GetString()));
}

if (d.HasMember("provider")) {
provider = d["provider"].GetString();
}
}

return !error;
}

MediaEventInfo::MediaEventInfo() {}

MediaEventInfo::MediaEventInfo(const MediaEventInfo& info):
Expand Down

0 comments on commit cafa58a

Please sign in to comment.