Skip to content

Commit

Permalink
Fixes brave/brave-browser#8849 - Hides Binance Widget for JP Users (A…
Browse files Browse the repository at this point in the history
…nonWallet) and OFAC countries (IsSupportedLocale)
  • Loading branch information
ryanml committed Mar 31, 2020
1 parent dafa0cd commit 3724c74
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 22 deletions.
26 changes: 26 additions & 0 deletions browser/extensions/api/binance_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "base/environment.h"
#include "brave/browser/profiles/profile_util.h"
#include "brave/components/binance/browser/static_values.h"
#include "chrome/browser/profiles/profile.h"
#include "components/country_codes/country_codes.h"

Expand Down Expand Up @@ -39,5 +40,30 @@ BinanceGetUserTLDFunction::Run() {
std::make_unique<base::Value>(user_TLD)));
}

ExtensionFunction::ResponseAction
BinanceIsSupportedRegionFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
if (brave::IsTorProfile(profile)) {
return RespondNow(Error("Not available in Tor profile"));
}

bool is_blacklisted = false;
const int32_t user_country_id =
country_codes::GetCountryIDFromPrefs(profile->GetPrefs());

for (const auto& country : binance::kBinanceBlacklistRegions) {
const int id = country_codes::CountryCharsToCountryID(
country.at(0), country.at(1));

if (id == user_country_id) {
is_blacklisted = true;
break;
}
}

return RespondNow(OneArgument(
std::make_unique<base::Value>(!is_blacklisted)));
}

} // namespace api
} // namespace extensions
10 changes: 10 additions & 0 deletions browser/extensions/api/binance_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class BinanceGetUserTLDFunction :
ResponseAction Run() override;
};

class BinanceIsSupportedRegionFunction :
public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("binance.isSupportedRegion", UNKNOWN)

protected:
~BinanceIsSupportedRegionFunction() override {}
ResponseAction Run() override;
};

} // namespace api
} // namespace extensions

Expand Down
17 changes: 17 additions & 0 deletions common/extensions/api/binance.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@
]
}
]
},
{
"name": "isSupportedRegion",
"type": "function",
"description": "Checks if region is supported to show the widget",
"parameters": [
{
"type": "function",
"name": "callback",
"parameters": [
{
"name": "supported",
"type": "boolean"
}
]
}
]
}
],
"types": [
Expand Down
13 changes: 13 additions & 0 deletions components/binance/browser/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import("//brave/build/config.gni")
import("//brave/components/binance/browser/buildflags/buildflags.gni")

assert(binance_enabled)

source_set("browser") {
public_deps = [
"buildflags"
]
sources = [
"static_values.h",
]
}
37 changes: 37 additions & 0 deletions components/binance/browser/static_values.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */

#ifndef BRAVE_COMPONENTS_BINANCE_BROWSER_STATIC_VALUES_H_
#define BRAVE_COMPONENTS_BINANCE_BROWSER_STATIC_VALUES_H_

#include <string>
#include <vector>

namespace binance {

const std::vector<std::string> kBinanceBlacklistRegions = {
"BY",
"BI",
"CF",
"CD",
"UA",
"CU",
"IR",
"LB",
"LY",
"KP",
"RU",
"SO",
"SS",
"SD",
"SY",
"VE",
"YE",
"ZW"
};

} // namespace binance

#endif // BRAVE_COMPONENTS_BINANCE_BROWSER_STATIC_VALUES_H_
8 changes: 8 additions & 0 deletions components/brave_new_tab_ui/actions/new_tab_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,11 @@ export const setInitialAsset = (initialAsset: string) => action(types.SET_INITIA
})

export const setUserTLDAutoSet = () => action(types.SET_USER_TLD_AUTO_SET)

export const setOnlyAnonWallet = (onlyAnonWallet: boolean) => action(types.SET_ONLY_ANON_WALLET, {
onlyAnonWallet
})

export const setBinanceSupported = (supported: boolean) => action(types.SET_BINANCE_SUPPORTED, {
supported
})
28 changes: 22 additions & 6 deletions components/brave_new_tab_ui/api/initialData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export type PreInitialRewardsData = {
}

export type InitialRewardsData = {
onlyAnonWallet: boolean
adsEstimatedEarnings: number
report: NewTab.RewardsBalanceReport
balance: NewTab.RewardsBalance
Expand Down Expand Up @@ -63,6 +62,28 @@ export async function getInitialData (): Promise<InitialData> {
}
}

export async function getBinanceBlackList (): Promise<any> {
try {
const [
onlyAnonWallet,
isSupportedRegion
] = await Promise.all([
new Promise(resolve => chrome.braveRewards.onlyAnonWallet((onlyAnonWallet: boolean) => {
resolve(!!onlyAnonWallet)
})),
new Promise(resolve => chrome.binance.isSupportedRegion((supported: boolean) => {
resolve(!!supported)
}))
])
return {
isSupportedRegion,
onlyAnonWallet
}
} catch (err) {
throw Error(err)
}
}

export async function getRewardsPreInitialData (): Promise<PreInitialRewardsData> {
try {
const [
Expand Down Expand Up @@ -93,14 +114,10 @@ export async function getRewardsPreInitialData (): Promise<PreInitialRewardsData
export async function getRewardsInitialData (): Promise<InitialRewardsData> {
try {
const [
onlyAnonWallet,
adsEstimatedEarnings,
report,
balance
] = await Promise.all([
new Promise(resolve => chrome.braveRewards.onlyAnonWallet((onlyAnonWallet: boolean) => {
resolve(!!onlyAnonWallet)
})),
new Promise(resolve => chrome.braveRewards.getAdsEstimatedEarnings((adsEstimatedEarnings: number) => {
resolve(adsEstimatedEarnings)
})),
Expand All @@ -116,7 +133,6 @@ export async function getRewardsInitialData (): Promise<InitialRewardsData> {
})
])
return {
onlyAnonWallet,
adsEstimatedEarnings,
report,
balance
Expand Down
17 changes: 16 additions & 1 deletion components/brave_new_tab_ui/apiEventsToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import getActions from './api/getActions'
import * as preferencesAPI from './api/preferences'
import * as statsAPI from './api/stats'
import * as privateTabDataAPI from './api/privateTabData'
import { getInitialData, getRewardsInitialData, getRewardsPreInitialData } from './api/initialData'
import { getInitialData, getRewardsInitialData, getRewardsPreInitialData, getBinanceBlackList } from './api/initialData'

async function updatePreferences (prefData: preferencesAPI.Preferences) {
getActions().preferencesUpdated(prefData)
Expand Down Expand Up @@ -37,6 +37,7 @@ export function wireApiEventsToStore () {
rewardsInitData()
setRewardsFetchInterval()
}
binanceInitData()
getActions().setInitialData(initialData)
getActions().setFirstRenderGridSitesData(initialData)
getActions().updateGridSitesBookmarkInfo(initialData.topSites)
Expand Down Expand Up @@ -67,6 +68,20 @@ export function rewardsInitData () {
})
}

function binanceInitData () {
getBinanceBlackList()
.then(({ isSupportedRegion, onlyAnonWallet }) => {
if (onlyAnonWallet || !isSupportedRegion) {
getActions().setCurrentStackWidget('rewards')
}
getActions().setOnlyAnonWallet(onlyAnonWallet)
getActions().setBinanceSupported(isSupportedRegion && !onlyAnonWallet)
})
.catch(e => {
console.error('Error fetching binance init data')
})
}

function setRewardsFetchInterval () {
window.setInterval(() => {
chrome.braveRewards.getWalletExists((exists: boolean) => {
Expand Down
4 changes: 3 additions & 1 deletion components/brave_new_tab_ui/constants/new_tab_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ export const enum types {
SET_PRE_INITIAL_REWARDS_DATA = '@@newtab/SET_PRE_INITIAL_REWARDS_DATA',
ON_WIDGET_POSITION_CHANGED = '@@newtab/ON_WIDGET_POSITION_CHANGED',
SET_CURRENT_STACK_WIDGET = '@@newtab/SET_CURRENT_STACK_WIDGET',
SET_ONLY_ANON_WALLET = '@@newtab/SET_ONLY_ANON_WALLET',
// Binance Widget
ON_BINANCE_USER_TLD = '@@newtab/ON_BINANCE_USER_TLD',
SET_INITIAL_ASSET = '@@newtab/SET_INITIAL_ASSET',
SET_INITIAL_FIAT = '@@newtab/SET_INITIAL_FIAT',
SET_INITIAL_AMOUNT = '@@newtab/SET_INITIAL_AMOUNT',
SET_USER_TLD_AUTO_SET = '@@newtab/SET_USER_TLD_AUTO_SET'
SET_USER_TLD_AUTO_SET = '@@newtab/SET_USER_TLD_AUTO_SET',
SET_BINANCE_SUPPORTED = '@@newtab/SET_BINANCE_SUPPORTED'
}

export type DismissBrandedWallpaperNotificationPayload = {
Expand Down
5 changes: 4 additions & 1 deletion components/brave_new_tab_ui/containers/newTab/footerInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface Props {
showBinance: boolean
brandedWallpaperOptIn: boolean
allowBrandedWallpaperUI: boolean
binanceSupported: boolean
}

export default class FooterInfo extends React.PureComponent<Props, {}> {
Expand All @@ -63,7 +64,8 @@ export default class FooterInfo extends React.PureComponent<Props, {}> {
toggleShowRewards,
showRewards,
toggleShowBinance,
showBinance
showBinance,
binanceSupported
} = this.props

return (
Expand Down Expand Up @@ -100,6 +102,7 @@ export default class FooterInfo extends React.PureComponent<Props, {}> {
showRewards={showRewards}
toggleShowBinance={toggleShowBinance}
showBinance={showBinance}
binanceSupported={binanceSupported}
/>
<IconLink title={getLocale('preferencesPageTitle')} href='chrome://settings'>
<SettingsAdvancedIcon />
Expand Down
11 changes: 9 additions & 2 deletions components/brave_new_tab_ui/containers/newTab/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ class NewTabPage extends React.Component<Props, State> {
}

toggleShowCrypto = () => {
const { currentStackWidget } = this.props.newTabData
const { currentStackWidget, binanceState, showRewards } = this.props.newTabData

if (!binanceState.binanceSupported) {
this.props.saveShowRewards(!showRewards)
return
}

if (currentStackWidget === 'rewards') {
this.toggleShowRewards()
Expand Down Expand Up @@ -383,7 +388,7 @@ class NewTabPage extends React.Component<Props, State> {
const { newTabData } = this.props
const { binanceState, showBinance } = newTabData

if (!showBinance) {
if (!showBinance || !binanceState.binanceSupported) {
return null
}

Expand All @@ -405,6 +410,7 @@ class NewTabPage extends React.Component<Props, State> {
render () {
const { newTabData, gridSitesData, actions } = this.props
const { showSettingsMenu } = this.state
const { binanceState } = newTabData

if (!newTabData) {
return null
Expand Down Expand Up @@ -513,6 +519,7 @@ class NewTabPage extends React.Component<Props, State> {
allowBrandedWallpaperUI={newTabData.featureFlagBraveNTPBrandedWallpaper}
toggleShowRewards={this.toggleShowRewards}
toggleShowBinance={this.toggleShowBinance}
binanceSupported={binanceState.binanceSupported}
/>
</Page.FooterContent>
</Page.Footer>
Expand Down
24 changes: 15 additions & 9 deletions components/brave_new_tab_ui/containers/newTab/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface Props {
allowBrandedWallpaperUI: boolean
showRewards: boolean
showBinance: boolean
binanceSupported: boolean
}

export default class Settings extends React.PureComponent<Props, {}> {
Expand Down Expand Up @@ -82,7 +83,8 @@ export default class Settings extends React.PureComponent<Props, {}> {
allowBrandedWallpaperUI,
onClick,
toggleShowBinance,
showBinance
showBinance,
binanceSupported
} = this.props
return (
<SettingsWrapper title={getLocale('dashboardSettingsTitle')} innerRef={this.settingsMenuRef}>
Expand Down Expand Up @@ -117,14 +119,18 @@ export default class Settings extends React.PureComponent<Props, {}> {
size='small'
/>
</SettingsRow>
<SettingsRow>
<SettingsText>{getLocale('showBinance')}</SettingsText>
<Toggle
onChange={toggleShowBinance}
checked={showBinance}
size='small'
/>
</SettingsRow>
{
binanceSupported
? <SettingsRow>
<SettingsText>{getLocale('showBinance')}</SettingsText>
<Toggle
onChange={toggleShowBinance}
checked={showBinance}
size='small'
/>
</SettingsRow>
: null
}
<SettingsRow>
<SettingsText>{getLocale('showBraveStats')}</SettingsText>
<Toggle
Expand Down
Loading

0 comments on commit 3724c74

Please sign in to comment.