Skip to content

Commit

Permalink
Removes observable
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Mar 25, 2019
1 parent ebdb5b4 commit 968a7cf
Show file tree
Hide file tree
Showing 20 changed files with 171 additions and 137 deletions.
48 changes: 41 additions & 7 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,14 @@ void BraveRewardsGetRecurringDonationsFunction::OnGetRecurringDonations(
Respond(OneArgument(std::move(result)));
}

BraveRewardsGetPublisherDonationAmountsFunction::
~BraveRewardsGetPublisherDonationAmountsFunction() {
BraveRewardsGetPublisherBannerFunction::
~BraveRewardsGetPublisherBannerFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsGetPublisherDonationAmountsFunction::Run() {
std::unique_ptr<brave_rewards::GetPublisherDonationAmounts::Params> params(
brave_rewards::GetPublisherDonationAmounts::Params::Create(*args_));
BraveRewardsGetPublisherBannerFunction::Run() {
std::unique_ptr<brave_rewards::GetPublisherBanner::Params> params(
brave_rewards::GetPublisherBanner::Params::Create(*args_));

Profile* profile = Profile::FromBrowserContext(browser_context());
RewardsService* rewards_service =
Expand All @@ -368,8 +368,42 @@ BraveRewardsGetPublisherDonationAmountsFunction::Run() {
return RespondNow(Error("Rewards service is not initialized"));
}

rewards_service->GetPublisherBanner(params->publisher_key);
return RespondNow(NoArguments());
rewards_service->GetPublisherBanner(
params->publisher_key,
base::Bind(
&BraveRewardsGetPublisherBannerFunction::OnPublisherBanner,
this));
return RespondLater();
}

void BraveRewardsGetPublisherBannerFunction::OnPublisherBanner(
std::unique_ptr<::brave_rewards::PublisherBanner> banner) {
std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());

if (banner) {
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);
result->SetString("provider", banner->provider);
result->SetBoolean("verified", banner->verified);

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));
}

Respond(OneArgument(std::move(result)));
}

} // namespace api
Expand Down
13 changes: 10 additions & 3 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
#ifndef BRAVE_BROWSER_EXTENSIONS_API_BRAVE_REWARDS_API_H_
#define BRAVE_BROWSER_EXTENSIONS_API_BRAVE_REWARDS_API_H_

#include <memory>

#include "extensions/browser/extension_function.h"
#include "brave/components/brave_rewards/browser/content_site.h"
#include "brave/components/brave_rewards/browser/publisher_banner.h"

namespace extensions {
namespace api {
Expand Down Expand Up @@ -193,16 +196,20 @@ class BraveRewardsGetRecurringDonationsFunction :
std::unique_ptr<brave_rewards::ContentSiteList> list);
};

class BraveRewardsGetPublisherDonationAmountsFunction :
class BraveRewardsGetPublisherBannerFunction :
public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION(
"braveRewards.getPublisherDonationAmounts", UNKNOWN)
"braveRewards.getPublisherBanner", UNKNOWN)

protected:
~BraveRewardsGetPublisherDonationAmountsFunction() override;
~BraveRewardsGetPublisherBannerFunction() override;

ResponseAction Run() override;

private:
void OnPublisherBanner(
std::unique_ptr<::brave_rewards::PublisherBanner> banner);
};

} // namespace api
Expand Down
50 changes: 27 additions & 23 deletions browser/ui/webui/brave_donate_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class RewardsDonateDOMHandler : public WebUIMessageHandler,
void GetRecurringDonations(const base::ListValue* args);
void GetReconcileStamp(const base::ListValue* args);
void OnReconcileStamp(uint64_t reconcile_stamp);
void OnPublisherBanner(
std::unique_ptr<brave_rewards::PublisherBanner> banner);

// RewardsServiceObserver implementation
void OnWalletProperties(
Expand All @@ -64,8 +66,6 @@ class RewardsDonateDOMHandler : public WebUIMessageHandler,
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
base::WeakPtrFactory<RewardsDonateDOMHandler> weak_factory_;
Expand Down Expand Up @@ -115,7 +115,10 @@ void RewardsDonateDOMHandler::GetPublisherDonateData(
const base::ListValue* args) {
std::string publisher_key;
args->GetString(0, &publisher_key);
rewards_service_->GetPublisherBanner(publisher_key);
rewards_service_->GetPublisherBanner(
publisher_key,
base::Bind(&RewardsDonateDOMHandler::OnPublisherBanner,
weak_factory_.GetWeakPtr()));
}

void RewardsDonateDOMHandler::GetWalletProperties(const base::ListValue* args) {
Expand Down Expand Up @@ -222,33 +225,34 @@ void RewardsDonateDOMHandler::OnRecurringDonationUpdated(
}

void RewardsDonateDOMHandler::OnPublisherBanner(
brave_rewards::RewardsService* rewards_service,
const brave_rewards::PublisherBanner banner) {
std::unique_ptr<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);
result.SetString("provider", banner.provider);
result.SetBoolean("verified", banner.verified);

auto amounts = std::make_unique<base::ListValue>();
for (int const& value : banner.amounts) {
amounts->AppendInteger(value);
}
result.SetList("amounts", std::move(amounts));
if (banner) {
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);
result.SetString("provider", banner->provider);
result.SetBoolean("verified", banner->verified);

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);
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));
}
result.SetDictionary("social", std::move(social));

web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards_donate.publisherBanner", result);
Expand Down
30 changes: 13 additions & 17 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,6 @@
}
}
]
},
{
"name": "onPublisherDonationAmounts",
"type": "function",
"description": "Fired when publisher donation info is retreived",
"parameters": [
{
"name": "amounts",
"type": "array",
"items": {
"type": "number"
}
}
]
}
],
"functions": [
Expand Down Expand Up @@ -513,21 +499,31 @@
"name": "callback",
"parameters": [
{
"name": "siteList",
"name": "donations",
"type": "any"
}
]
}
]
},
{
"name": "getPublisherDonationAmounts",
"name": "getPublisherBanner",
"type": "function",
"description": "Retreives custom donation amounts",
"description": "Retrieves data for publisher banner",
"parameters": [
{
"name": "publisher_key",
"type": "string"
},
{
"name": "callback",
"type": "function",
"parameters": [
{
"name": "banner",
"type": "any"
}
]
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "chrome/browser/profiles/profile.h"
#include "extensions/browser/event_router.h"
#include "bat/ledger/ledger_callback_handler.h"
#include "brave/components/brave_rewards/browser/publisher_banner.h"

namespace brave_rewards {

Expand Down Expand Up @@ -326,30 +325,4 @@ void ExtensionRewardsServiceObserver::OnExcludedSitesChanged(
event_router->BroadcastEvent(std::move(event));
}

void ExtensionRewardsServiceObserver::OnPublisherBanner(
RewardsService* rewards_service,
const brave_rewards::PublisherBanner banner) {
auto* event_router = extensions::EventRouter::Get(profile_);

if (!event_router) {
return;
}

std::vector<double> amounts;
for (int const& value : banner.amounts) {
amounts.push_back(value);
}

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

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

} // namespace brave_rewards
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ class ExtensionRewardsServiceObserver : public RewardsServiceObserver,
void OnPendingContributionSaved(RewardsService* rewards_service,
int result) override;

void OnPublisherBanner(
RewardsService* rewards_service,
const brave_rewards::PublisherBanner banner) override;

private:
Profile* profile_;

Expand Down
5 changes: 4 additions & 1 deletion components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ using GetRewardsInternalsInfoCallback = base::OnceCallback<void(
std::unique_ptr<brave_rewards::RewardsInternalsInfo>)>;
using GetRecurringDonationsListCallback =
base::OnceCallback<void(std::unique_ptr<brave_rewards::ContentSiteList>)>;
using GetPublisherBannerCallback =
base::OnceCallback<void(std::unique_ptr<brave_rewards::PublisherBanner>)>;

class RewardsService : public KeyedService {
public:
Expand Down Expand Up @@ -154,7 +156,8 @@ class RewardsService : public KeyedService {
const std::string& publisher_blob) = 0;
virtual void GetContributionAmount(
const GetContributionAmountCallback& callback) = 0;
virtual void GetPublisherBanner(const std::string& publisher_id) = 0;
virtual void GetPublisherBanner(const std::string& publisher_id,
GetPublisherBannerCallback callback) = 0;
virtual void OnDonate(const std::string& publisher_key, int amount,
bool recurring, const ledger::PublisherInfo* publisher_info = NULL) = 0;
virtual void OnDonate(const std::string& publisher_key, int amount,
Expand Down
49 changes: 25 additions & 24 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2038,46 +2038,47 @@ void RewardsServiceImpl::OnSetOnDemandFaviconComplete(
callback(success, favicon_url);
}

void RewardsServiceImpl::GetPublisherBanner(const std::string& publisher_id) {
void RewardsServiceImpl::GetPublisherBanner(
const std::string& publisher_id,
GetPublisherBannerCallback callback) {
if (!Connected())
return;

bat_ledger_->GetPublisherBanner(publisher_id,
base::BindOnce(&RewardsServiceImpl::OnPublisherBannerMojoProxy,
AsWeakPtr()));
base::BindOnce(&RewardsServiceImpl::OnPublisherBanner,
AsWeakPtr(),
std::move(callback)));
}

void RewardsServiceImpl::OnPublisherBannerMojoProxy(
void RewardsServiceImpl::OnPublisherBanner(
GetPublisherBannerCallback callback,
const std::string& 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.reset(new ledger::PublisherBanner());
publisher_banner->loadFromJson(banner);
}
OnPublisherBanner(std::move(publisher_banner));
}

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

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

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;
new_banner.provider = banner->provider;
new_banner.verified = banner->verified;
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;

for (auto& observer : observers_)
observer.OnPublisherBanner(this, new_banner);
std::move(callback).Run(std::move(new_banner));
}

void RewardsServiceImpl::OnDonate_PublisherInfoSaved(ledger::Result result,
Expand Down
Loading

0 comments on commit 968a7cf

Please sign in to comment.