From 4c189eccce0e0aaa94bbbe51aaa89262122cb9a2 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Sun, 4 Nov 2018 20:44:33 +0100 Subject: [PATCH] Fixes site banner formatting for media publisher Resolves https://github.com/brave/brave-browser/issues/1924 --- DEPS | 2 +- browser/ui/webui/brave_donate_ui.cc | 58 ++++++++++--------- .../brave_rewards/browser/publisher_banner.cc | 4 ++ .../brave_rewards/browser/publisher_banner.h | 2 + .../brave_rewards/browser/rewards_service.h | 2 +- .../browser/rewards_service_impl.cc | 29 +++++++--- .../browser/rewards_service_impl.h | 3 +- .../browser/rewards_service_observer.h | 4 +- .../donate/components/siteBanner.tsx | 8 +++ .../brave_rewards/components/panel.tsx | 4 +- components/definitions/rewardsDonate.ts | 1 + package-lock.json | 6 +- package.json | 2 +- 13 files changed, 80 insertions(+), 45 deletions(-) diff --git a/DEPS b/DEPS index 47e7bcc536fb..e37b1bbeb218 100644 --- a/DEPS +++ b/DEPS @@ -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", diff --git a/browser/ui/webui/brave_donate_ui.cc b/browser/ui/webui/brave_donate_ui.cc index 8142132edd0d..d476958bb0c1 100644 --- a/browser/ui/webui/brave_donate_ui.cc +++ b/browser/ui/webui/brave_donate_ui.cc @@ -52,6 +52,8 @@ class RewardsDonateDOMHandler : public WebUIMessageHandler, std::unique_ptr 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 @@ -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(); - for (int const& value : banner.amounts) { - amounts->AppendInteger(value); - } - result.SetList("amounts", std::move(amounts)); - - auto social = std::make_unique(); - 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) { @@ -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(); + for (int const& value : banner.amounts) { + amounts->AppendInteger(value); + } + result.SetList("amounts", std::move(amounts)); + + auto social = std::make_unique(); + 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) diff --git a/components/brave_rewards/browser/publisher_banner.cc b/components/brave_rewards/browser/publisher_banner.cc index 5881f01b16f9..9cbef19fb18c 100644 --- a/components/brave_rewards/browser/publisher_banner.cc +++ b/components/brave_rewards/browser/publisher_banner.cc @@ -10,7 +10,9 @@ namespace brave_rewards { PublisherBanner::PublisherBanner() : + publisher_key(""), title(""), + name(""), description(""), background(""), logo(""), @@ -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; diff --git a/components/brave_rewards/browser/publisher_banner.h b/components/brave_rewards/browser/publisher_banner.h index b1d36caff113..ea53debdf018 100644 --- a/components/brave_rewards/browser/publisher_banner.h +++ b/components/brave_rewards/browser/publisher_banner.h @@ -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; diff --git a/components/brave_rewards/browser/rewards_service.h b/components/brave_rewards/browser/rewards_service.h index 7d4319baf68b..954adea8d0fb 100644 --- a/components/brave_rewards/browser/rewards_service.h +++ b/components/brave_rewards/browser/rewards_service.h @@ -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; diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 1f9297bc06fd..194ef50d324e 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -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" @@ -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 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) { diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index 5004872f35e2..39550329b993 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -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 banner); void RemoveRecurring(const std::string& publisher_key) override; void UpdateRecurringDonationsList() override; void UpdateTipsList() override; diff --git a/components/brave_rewards/browser/rewards_service_observer.h b/components/brave_rewards/browser/rewards_service_observer.h index 6396054d5557..cb6aa4715b3c 100644 --- a/components/brave_rewards/browser/rewards_service_observer.h +++ b/components/brave_rewards/browser/rewards_service_observer.h @@ -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 diff --git a/components/brave_rewards/resources/donate/components/siteBanner.tsx b/components/brave_rewards/resources/donate/components/siteBanner.tsx index b46d34143f21..0839eba57e2c 100644 --- a/components/brave_rewards/resources/donate/components/siteBanner.tsx +++ b/components/brave_rewards/resources/donate/components/siteBanner.tsx @@ -109,6 +109,7 @@ class Banner extends React.Component { let logo = '' let publisherKey = '' let description = '' + let name = '' if (publisher) { title = publisher.title @@ -117,6 +118,12 @@ class Banner extends React.Component { // 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 @@ -125,6 +132,7 @@ class Banner extends React.Component { { 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 ( diff --git a/components/definitions/rewardsDonate.ts b/components/definitions/rewardsDonate.ts index 22e9d29e608d..6a82c955e221 100644 --- a/components/definitions/rewardsDonate.ts +++ b/components/definitions/rewardsDonate.ts @@ -18,6 +18,7 @@ declare namespace RewardsDonate { interface Publisher { publisherKey: string + name: string title: string description: string background: string diff --git a/package-lock.json b/package-lock.json index 499c35bac94d..526fd1b1e52b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2215,9 +2215,9 @@ } }, "brave-ui": { - "version": "0.30.6", - "resolved": "https://registry.npmjs.org/brave-ui/-/brave-ui-0.30.6.tgz", - "integrity": "sha512-LBgjDAnlv6Fp2BEvCy6miI49dZGw1Dr1mJqSH/qL4IJh286DRsC8gzKexgtvq7mRBhOwqNgSkfCYoDSsQjQ7+Q==", + "version": "0.30.8", + "resolved": "https://registry.npmjs.org/brave-ui/-/brave-ui-0.30.8.tgz", + "integrity": "sha512-0cpSA8IjpNsP6N0yPFjVSVYEL/yt0UNNIT9vtUpTxb7uoREZ6hdzRCc8ddMXIjQ9ML+5bHG6w6KB22vJRUOmkA==", "dev": true, "requires": { "emptykit.css": "^1.0.1", diff --git a/package.json b/package.json index bfb90c52e18e..5fa664dea839 100644 --- a/package.json +++ b/package.json @@ -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",