Skip to content
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

Fixes site banner formatting for media publisher #800

Merged
merged 1 commit into from
Nov 5, 2018
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
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ deps = {
"vendor/python-patch": "https://github.com/svn2github/python-patch@a336a458016ced89aba90dfc3f4c8222ae3b1403",
"vendor/omaha": "https://github.com/brave/omaha.git@5c633e867efafb9013c57ca830212d1ff6ea5076",
"vendor/sparkle": "https://github.com/brave/Sparkle.git@c0759cce415d7c0feae45005c8a013b1898711f0",
"vendor/bat-native-ledger": "https://github.com/brave-intl/bat-native-ledger@a8550e6bf36eba0d62b3f24409d1cf4edb20827b",
"vendor/bat-native-ledger": "https://github.com/brave-intl/bat-native-ledger@2912c1f5b59338c139c41339be0f3f8d815e45d4",
"vendor/bat-native-rapidjson": "https://github.com/brave-intl/bat-native-rapidjson.git@86aafe2ef89835ae71c9ed7c2527e3bb3000930e",
"vendor/bip39wally-core-native": "https://github.com/brave-intl/bip39wally-core-native.git@9b119931c702d55be994117eb505d56310720b1d",
"vendor/bat-native-anonize": "https://github.com/brave-intl/bat-native-anonize.git@adeff3254bb90ccdc9699040d5a4e1cd6b8393b7",
Expand Down
58 changes: 32 additions & 26 deletions browser/ui/webui/brave_donate_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class RewardsDonateDOMHandler : public WebUIMessageHandler,
std::unique_ptr<brave_rewards::WalletProperties> wallet_properties) override;
void OnRecurringDonationUpdated(brave_rewards::RewardsService* rewards_service,
brave_rewards::ContentSiteList) override;
void OnPublisherBanner(brave_rewards::RewardsService* rewards_service,
const brave_rewards::PublisherBanner banner) override;

brave_rewards::RewardsService* rewards_service_; // NOT OWNED

Expand Down Expand Up @@ -87,34 +89,9 @@ void RewardsDonateDOMHandler::RegisterMessages() {
}

void RewardsDonateDOMHandler::GetPublisherDonateData(const base::ListValue* args) {
if (!web_ui()->CanCallJavascript()) {
return;
}

std::string publisher_key;
args->GetString(0, &publisher_key);
brave_rewards::PublisherBanner banner = rewards_service_->GetPublisherBanner(publisher_key);

base::DictionaryValue result;
result.SetString("publisherKey", publisher_key);
result.SetString("title", banner.title);
result.SetString("description", banner.description);
result.SetString("background", banner.background);
result.SetString("logo", banner.logo);

auto amounts = std::make_unique<base::ListValue>();
for (int const& value : banner.amounts) {
amounts->AppendInteger(value);
}
result.SetList("amounts", std::move(amounts));

auto social = std::make_unique<base::DictionaryValue>();
for (auto const& item : banner.social) {
social->SetString(item.first, item.second);
}
result.SetDictionary("social", std::move(social));

web_ui()->CallJavascriptFunctionUnsafe("brave_rewards_donate.publisherBanner", result);
rewards_service_->GetPublisherBanner(publisher_key);
}

void RewardsDonateDOMHandler::GetWalletProperties(const base::ListValue* args) {
Expand Down Expand Up @@ -211,6 +188,35 @@ void RewardsDonateDOMHandler::OnRecurringDonationUpdated(brave_rewards::RewardsS
}
}

void RewardsDonateDOMHandler::OnPublisherBanner(brave_rewards::RewardsService* rewards_service,
const brave_rewards::PublisherBanner banner) {
if (!web_ui()->CanCallJavascript()) {
return;
}

base::DictionaryValue result;
result.SetString("publisherKey", banner.publisher_key);
result.SetString("title", banner.title);
result.SetString("name", banner.name);
result.SetString("description", banner.description);
result.SetString("background", banner.background);
result.SetString("logo", banner.logo);

auto amounts = std::make_unique<base::ListValue>();
for (int const& value : banner.amounts) {
amounts->AppendInteger(value);
}
result.SetList("amounts", std::move(amounts));

auto social = std::make_unique<base::DictionaryValue>();
for (auto const& item : banner.social) {
social->SetString(item.first, item.second);
}
result.SetDictionary("social", std::move(social));

web_ui()->CallJavascriptFunctionUnsafe("brave_rewards_donate.publisherBanner", result);
}

} // namespace

BraveDonateUI::BraveDonateUI(content::WebUI* web_ui, const std::string& name)
Expand Down
4 changes: 4 additions & 0 deletions components/brave_rewards/browser/publisher_banner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
namespace brave_rewards {

PublisherBanner::PublisherBanner() :
publisher_key(""),
title(""),
name(""),
description(""),
background(""),
logo(""),
Expand All @@ -20,7 +22,9 @@ namespace brave_rewards {
PublisherBanner::~PublisherBanner() { }

PublisherBanner::PublisherBanner(const PublisherBanner &properties) {
publisher_key = properties.publisher_key;
title = properties.title;
name = properties.name;
description = properties.description;
background = properties.background;
logo = properties.logo;
Expand Down
2 changes: 2 additions & 0 deletions components/brave_rewards/browser/publisher_banner.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ namespace brave_rewards {
~PublisherBanner();
PublisherBanner(const PublisherBanner& properties);

std::string publisher_key;
std::string title;
std::string name;
std::string description;
std::string background;
std::string logo;
Expand Down
2 changes: 1 addition & 1 deletion components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class RewardsService : public KeyedService {
virtual bool IsWalletCreated() = 0;
virtual void GetPublisherActivityFromUrl(uint64_t windowId, const std::string& url, const std::string& favicon_url) = 0;
virtual double GetContributionAmount() = 0;
virtual brave_rewards::PublisherBanner GetPublisherBanner(const std::string& publisher_id) = 0;
virtual void GetPublisherBanner(const std::string& publisher_id) = 0;
virtual void OnDonate(const std::string& publisher_key, int amount, bool recurring) = 0;
virtual void RemoveRecurring(const std::string& publisher_key) = 0;
virtual void UpdateRecurringDonationsList() = 0;
Expand Down
29 changes: 20 additions & 9 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/base/url_util.h"
#include "net/url_request/url_fetcher.h"
#include "publisher_banner.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "url/gurl.h"
Expand Down Expand Up @@ -1385,19 +1386,29 @@ void RewardsServiceImpl::OnSetOnDemandFaviconComplete(const std::string& favicon
callback(success, favicon_url);
}

brave_rewards::PublisherBanner RewardsServiceImpl::GetPublisherBanner(const std::string& publisher_id) {
ledger::PublisherBanner banner = ledger_->GetPublisherBanner(publisher_id);
void RewardsServiceImpl::GetPublisherBanner(const std::string& publisher_id) {
ledger_->GetPublisherBanner(publisher_id,
std::bind(&RewardsServiceImpl::OnPublisherBanner, this, _1));
}

void RewardsServiceImpl::OnPublisherBanner(std::unique_ptr<ledger::PublisherBanner> banner) {
brave_rewards::PublisherBanner new_banner;

new_banner.title = banner.title;
new_banner.description = banner.description;
new_banner.background = banner.background;
new_banner.logo = banner.logo;
new_banner.amounts = banner.amounts;
new_banner.social = banner.social;
if (!banner) {
return;
}

return new_banner;
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 = banner->social;

for (auto& observer : observers_)
observer.OnPublisherBanner(this, new_banner);
}

void RewardsServiceImpl::OnDonate(const std::string& publisher_key, int amount, bool recurring) {
Expand Down
3 changes: 2 additions & 1 deletion components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ class RewardsServiceImpl : public RewardsService,
bool IsWalletCreated() override;
void GetPublisherActivityFromUrl(uint64_t windowId, const std::string& url, const std::string& favicon_url) override;
double GetContributionAmount() override;
brave_rewards::PublisherBanner GetPublisherBanner(const std::string& publisher_id) override;
void GetPublisherBanner(const std::string& publisher_id) override;
void OnPublisherBanner(std::unique_ptr<ledger::PublisherBanner> banner);
void RemoveRecurring(const std::string& publisher_key) override;
void UpdateRecurringDonationsList() override;
void UpdateTipsList() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class RewardsServiceObserver : public base::CheckedObserver {
virtual void OnRecurringDonationUpdated(RewardsService* rewards_service,
brave_rewards::ContentSiteList) {};
virtual void OnCurrentTips(RewardsService* rewards_service,
brave_rewards::ContentSiteList) {};
brave_rewards::ContentSiteList) {};
virtual void OnPublisherBanner(brave_rewards::RewardsService* rewards_service,
const brave_rewards::PublisherBanner banner) {};
};

} // namespace brave_rewards
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class Banner extends React.Component<Props, State> {
let logo = ''
let publisherKey = ''
let description = ''
let name = ''

if (publisher) {
title = publisher.title
Expand All @@ -117,6 +118,12 @@ class Banner extends React.Component<Props, State> {
// logo = publisher.logo
publisherKey = publisher.publisherKey
description = publisher.description
name = publisher.name

const internalFavicon = /^https:\/\/[a-z0-9-]+\.invalid(\/)?$/
if (internalFavicon.test(publisher.logo)) {
logo = `chrome://favicon/size/160@2x/${publisher.logo}`
}
}

// TODO we need to use title and not publisherKey for domain for media publishers
Expand All @@ -125,6 +132,7 @@ class Banner extends React.Component<Props, State> {
<SiteBanner
domain={publisherKey}
title={title}
name={name}
recurringDonation={recurringList && recurringList.includes(publisherKey)}
balance={balance.toString() || '0'}
bgImage={background}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ export class Panel extends React.Component<Props, State> {

let faviconUrl
if (publisher && publisher.url) {
faviconUrl = `chrome://favicon/size/48@1x/${publisher.url}`
faviconUrl = `chrome://favicon/size/48@2x/${publisher.url}`
if (publisher.favicon_url) {
faviconUrl = `chrome://favicon/size/48@1x/${publisher.favicon_url}`
faviconUrl = `chrome://favicon/size/48@2x/${publisher.favicon_url}`
}
}
return (
Expand Down
1 change: 1 addition & 0 deletions components/definitions/rewardsDonate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ declare namespace RewardsDonate {

interface Publisher {
publisherKey: string
name: string
title: string
description: string
background: string
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@
"babel-preset-react": "^6.24.1",
"babel-preset-react-optimize": "^1.0.1",
"babel-preset-stage-0": "^6.24.1",
"brave-ui": "^0.30.6",
"brave-ui": "^0.30.8",
"css-loader": "^0.28.9",
"csstype": "^2.5.5",
"emptykit.css": "^1.0.1",
Expand Down