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

[WIP] Adds wallet properties implementation #293

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions browser/payments/payments_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class PaymentsService : public KeyedService {
~PaymentsService() override;

virtual void CreateWallet() = 0;
virtual void GetWalletProperties() = 0;
virtual void GetContentSiteList(uint32_t start,
uint32_t limit,
const GetContentSiteListCallback& callback) = 0;
Expand Down
14 changes: 14 additions & 0 deletions browser/payments/payments_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "base/task_scheduler/post_task.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "bat/ledger/ledger.h"
#include "bat/ledger/wallet_info.h"
#include "brave/browser/payments/payments_service_observer.h"
#include "brave/browser/payments/publisher_info_backend.h"
#include "chrome/browser/browser_process_impl.h"
Expand Down Expand Up @@ -248,6 +249,10 @@ void PaymentsServiceImpl::OnWalletCreated(ledger::Result result) {
TriggerOnWalletCreated(result);
}

void PaymentsServiceImpl::OnWalletProperties(ledger::WalletInfo result) {
TriggerOnWalletProperties(result);
}

void PaymentsServiceImpl::OnReconcileComplete(ledger::Result result,
const std::string& viewing_id) {
LOG(ERROR) << "reconcile complete " << viewing_id;
Expand Down Expand Up @@ -466,4 +471,13 @@ void PaymentsServiceImpl::TriggerOnWalletCreated(int error_code) {
observer.OnWalletCreated(this, error_code);
}

void PaymentsServiceImpl::TriggerOnWalletProperties(ledger::WalletInfo result) {
for (auto& observer : observers_)
observer.OnWalletProperties(this, result);
}

void PaymentsServiceImpl::GetWalletProperties() {
ledger_->GetWalletProperties();
}

} // namespace payments
5 changes: 5 additions & 0 deletions browser/payments/payments_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <memory>

#include "bat/ledger/ledger.h"
#include "bat/ledger/wallet_info.h"
#include "base/files/file_path.h"
#include "base/observer_list.h"
#include "base/memory/weak_ptr.h"
Expand Down Expand Up @@ -50,6 +52,7 @@ class PaymentsServiceImpl : public PaymentsService,
void Shutdown() override;

void CreateWallet() override;
void GetWalletProperties() override;
void GetContentSiteList(uint32_t start,
uint32_t limit,
const GetContentSiteListCallback& callback) override;
Expand All @@ -75,6 +78,7 @@ class PaymentsServiceImpl : public PaymentsService,
void OnPublisherStateLoaded(ledger::LedgerCallbackHandler* handler,
const std::string& data);
void TriggerOnWalletCreated(int error_code);
void TriggerOnWalletProperties(ledger::WalletInfo result);
void OnPublisherInfoSaved(ledger::PublisherInfoCallback callback,
std::unique_ptr<ledger::PublisherInfo> info,
bool success);
Expand All @@ -88,6 +92,7 @@ class PaymentsServiceImpl : public PaymentsService,
// ledger::LedgerClient
std::string GenerateGUID() const override;
void OnWalletCreated(ledger::Result result) override;
void OnWalletProperties(ledger::WalletInfo result) override;
void OnReconcileComplete(ledger::Result result,
const std::string& viewing_id) override;
void LoadLedgerState(ledger::LedgerCallbackHandler* handler) override;
Expand Down
5 changes: 5 additions & 0 deletions browser/payments/payments_service_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#ifndef BRAVE_BROWSER_PAYMENTS_PAYMENTS_SERVICE_OBSERVER_H_
#define BRAVE_BROWSER_PAYMENTS_PAYMENTS_SERVICE_OBSERVER_H_

#include "bat/ledger/ledger.h"
#include "bat/ledger/wallet_info.h"

namespace payments {

class PaymentsService;
Expand All @@ -15,6 +18,8 @@ class PaymentsServiceObserver {

virtual void OnWalletCreated(PaymentsService* payment_service,
int error_code) {};
virtual void OnWalletProperties(PaymentsService* payment_service,
ledger::WalletInfo properties) {};
};

} // namespace payments
Expand Down
1 change: 1 addition & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ source_set("ui") {
]

deps = [
"//brave/vendor/bat-native-ledger",
"//brave/app:command_ids",
"//brave/app/theme:brave_unscaled_resources",
"//brave/app/theme:brave_theme_resources",
Expand Down
41 changes: 41 additions & 0 deletions browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <bat/ledger/wallet_info.h>
#include "brave/browser/ui/webui/brave_rewards_ui.h"

#include "brave/browser/payments/payments_service.h"
Expand All @@ -11,6 +12,10 @@
#include "chrome/browser/profiles/profile.h"
#include "components/grit/brave_components_resources.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/common/bindings_policy.h"


Expand All @@ -34,10 +39,14 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void HandleCreateWalletRequested(const base::ListValue* args);
void OnWalletCreated();
void OnWalletCreateFailed();
void GetWalletProperties(const base::ListValue* args);
void OnWalletProperties(ledger::WalletInfo result);

// PaymentServiceObserver implementation
void OnWalletCreated(payments::PaymentsService* payment_service,
int error_code) override;
void OnWalletProperties(payments::PaymentsService* payment_service,
ledger::WalletInfo result) override;

payments::PaymentsService* payments_service_; // NOT OWNED

Expand All @@ -53,6 +62,9 @@ void RewardsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("createWalletRequested",
base::BindRepeating(&RewardsDOMHandler::HandleCreateWalletRequested,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("getWalletProperties",
base::BindRepeating(&RewardsDOMHandler::GetWalletProperties,
base::Unretained(this)));
}

void RewardsDOMHandler::Init() {
Expand All @@ -70,6 +82,12 @@ void RewardsDOMHandler::HandleCreateWalletRequested(const base::ListValue* args)
}
}

void RewardsDOMHandler::GetWalletProperties(const base::ListValue* args) {
if (payments_service_) {
payments_service_->GetWalletProperties();
}
}

void RewardsDOMHandler::OnWalletCreated(
payments::PaymentsService* payment_service,
int error_code) {
Expand All @@ -91,6 +109,29 @@ void RewardsDOMHandler::OnWalletCreateFailed() {
}
}

void RewardsDOMHandler::OnWalletProperties(
payments::PaymentsService* payment_service,
ledger::WalletInfo result) {
OnWalletProperties(result);
}

void RewardsDOMHandler::OnWalletProperties(ledger::WalletInfo result) {
if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI)) {
auto* web_contents = web_ui()->GetWebContents();
if (web_contents) {
auto* render_view_host = web_contents->GetRenderViewHost();
if (render_view_host) {
render_view_host->SetWebUIProperty("rewards.walletInfo.probi", result.probi_);
render_view_host->SetWebUIProperty("rewards.walletInfo.balance", std::to_string(result.balance_));
for (auto const& rate : result.rates_) {
render_view_host->SetWebUIProperty("rewards.walletInfo.rates." + rate.first, base::NumberToString(rate.second));
}
}
}
web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.walletProperties");
}
}

} // namespace

BraveRewardsUI::BraveRewardsUI(content::WebUI* web_ui, const std::string& name)
Expand Down
4 changes: 4 additions & 0 deletions components/brave_rewards_ui/actions/rewards_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ export const createWalletRequested = () => action(types.CREATE_WALLET_REQUESTED)
export const walletCreated = () => action(types.WALLET_CREATED)

export const walletCreateFailed = () => action(types.WALLET_CREATE_FAILED)

export const getWalletProperties = () => action(types.GET_WALLET_PROPERTIES)

export const onWalletProperties = () => action(types.ON_WALLET_PROPERTIES)
7 changes: 6 additions & 1 deletion components/brave_rewards_ui/brave_rewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ window.cr.define('brave_rewards', function () {
getActions().walletCreateFailed()
}

function walletProperties () {
getActions().onWalletProperties()
}

return {
initialize,
walletCreated,
walletCreateFailed
walletCreateFailed,
walletProperties
}
})

Expand Down
15 changes: 13 additions & 2 deletions components/brave_rewards_ui/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export class RewardsPage extends React.Component<Props, {}> {
this.actions.createWalletRequested()
}

getWalletProperties = () => {
this.actions.getWalletProperties()
}

get actions () {
return this.props.actions
}
Expand All @@ -28,11 +32,18 @@ export class RewardsPage extends React.Component<Props, {}> {
return (
<div id='rewardsPage'>
<div>
<a href='#' onClick={this.onCreateWalletClicked}>Create Wallet</a>
<button onClick={this.onCreateWalletClicked}>
Create Wallet
</button>
</div>
{
rewardsData.walletCreated
? <div>Wallet Created!</div>
? <div>
Wallet Created!
<button onClick={this.getWalletProperties}>
Get wallet data
</button>
</div>
: null
}
{
Expand Down
4 changes: 3 additions & 1 deletion components/brave_rewards_ui/constants/rewards_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
export const enum types {
CREATE_WALLET_REQUESTED = '@@rewards/CREATE_WALLET_REQUESTED',
WALLET_CREATED = '@@rewards/WALLET_CREATED',
WALLET_CREATE_FAILED = '@@rewards/WALLET_CREATE_FAILED'
WALLET_CREATE_FAILED = '@@rewards/WALLET_CREATE_FAILED',
ON_WALLET_PROPERTIES = '@@rewards/ON_WALLET_PROPERTIES',
GET_WALLET_PROPERTIES = '@@rewards/GET_WALLET_PROPERTIES'
}
9 changes: 9 additions & 0 deletions components/brave_rewards_ui/reducers/rewards_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ const rewardsReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State
state = { ...state }
state.walletCreateFailed = true
break
case types.GET_WALLET_PROPERTIES:
chrome.send('getWalletProperties', [])
break
case types.ON_WALLET_PROPERTIES:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so you're treating the response as an action to update the store? That seems like a reasonable route to go as well. I was thinking thinking something more like writing webui properties as store.propertyname and using a generic mechanism to write those to the actual store for the page, but either way works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so to basically have 1:1 mapping with redux store? this is how I did so far https://github.com/brave/brave-core/pull/293/files#diff-aa3505dbf36b5d03d8ba0751e0c99904R124 which I think it's quite the same as you were thinking.

state = { ...state }
console.log(chrome.getVariableValue('rewards.walletInfo.probi'))
console.log(chrome.getVariableValue('rewards.walletInfo.rates.USD'))
console.log(chrome.getVariableValue('rewards.walletInfo.rates.EUR'))
break
}

if (state !== startingState) {
Expand Down