diff --git a/browser/extensions/api/binance_api.cc b/browser/extensions/api/binance_api.cc index 81bb7f169a94..6d78733e7d8f 100644 --- a/browser/extensions/api/binance_api.cc +++ b/browser/extensions/api/binance_api.cc @@ -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" @@ -39,5 +40,30 @@ BinanceGetUserTLDFunction::Run() { std::make_unique(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(!is_blacklisted))); +} + } // namespace api } // namespace extensions diff --git a/browser/extensions/api/binance_api.h b/browser/extensions/api/binance_api.h index d5910b29e6b4..b1d21d879965 100644 --- a/browser/extensions/api/binance_api.h +++ b/browser/extensions/api/binance_api.h @@ -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 diff --git a/common/extensions/api/binance.json b/common/extensions/api/binance.json index 09006963b949..b6921a1e7071 100644 --- a/common/extensions/api/binance.json +++ b/common/extensions/api/binance.json @@ -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": [ diff --git a/components/binance/browser/BUILD.gn b/components/binance/browser/BUILD.gn new file mode 100644 index 000000000000..af956be9fad4 --- /dev/null +++ b/components/binance/browser/BUILD.gn @@ -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", + ] +} diff --git a/components/binance/browser/static_values.h b/components/binance/browser/static_values.h new file mode 100644 index 000000000000..edf828b2113b --- /dev/null +++ b/components/binance/browser/static_values.h @@ -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 +#include + +namespace binance { + +const std::vector 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_ diff --git a/components/brave_new_tab_ui/actions/new_tab_actions.ts b/components/brave_new_tab_ui/actions/new_tab_actions.ts index 83b8c9280693..d2330d94cba3 100644 --- a/components/brave_new_tab_ui/actions/new_tab_actions.ts +++ b/components/brave_new_tab_ui/actions/new_tab_actions.ts @@ -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 +}) diff --git a/components/brave_new_tab_ui/api/initialData.ts b/components/brave_new_tab_ui/api/initialData.ts index 2a382e1872d4..df0a75dceb0b 100644 --- a/components/brave_new_tab_ui/api/initialData.ts +++ b/components/brave_new_tab_ui/api/initialData.ts @@ -24,7 +24,6 @@ export type PreInitialRewardsData = { } export type InitialRewardsData = { - onlyAnonWallet: boolean adsEstimatedEarnings: number report: NewTab.RewardsBalanceReport balance: NewTab.RewardsBalance @@ -63,6 +62,28 @@ export async function getInitialData (): Promise { } } +export async function getBinanceBlackList (): Promise { + 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 { try { const [ @@ -93,14 +114,10 @@ export async function getRewardsPreInitialData (): Promise { 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) })), @@ -116,7 +133,6 @@ export async function getRewardsInitialData (): Promise { }) ]) return { - onlyAnonWallet, adsEstimatedEarnings, report, balance diff --git a/components/brave_new_tab_ui/apiEventsToStore.ts b/components/brave_new_tab_ui/apiEventsToStore.ts index e2e5d9178612..774fc82a42e6 100644 --- a/components/brave_new_tab_ui/apiEventsToStore.ts +++ b/components/brave_new_tab_ui/apiEventsToStore.ts @@ -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) @@ -37,6 +37,7 @@ export function wireApiEventsToStore () { rewardsInitData() setRewardsFetchInterval() } + binanceInitData() getActions().setInitialData(initialData) getActions().setFirstRenderGridSitesData(initialData) getActions().updateGridSitesBookmarkInfo(initialData.topSites) @@ -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) => { diff --git a/components/brave_new_tab_ui/constants/new_tab_types.ts b/components/brave_new_tab_ui/constants/new_tab_types.ts index 380ac34d3279..37d22675cfeb 100644 --- a/components/brave_new_tab_ui/constants/new_tab_types.ts +++ b/components/brave_new_tab_ui/constants/new_tab_types.ts @@ -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 = { diff --git a/components/brave_new_tab_ui/containers/newTab/footerInfo.tsx b/components/brave_new_tab_ui/containers/newTab/footerInfo.tsx index b8aef67428db..e1eccb0c4dcb 100644 --- a/components/brave_new_tab_ui/containers/newTab/footerInfo.tsx +++ b/components/brave_new_tab_ui/containers/newTab/footerInfo.tsx @@ -37,6 +37,7 @@ interface Props { showBinance: boolean brandedWallpaperOptIn: boolean allowBrandedWallpaperUI: boolean + binanceSupported: boolean } export default class FooterInfo extends React.PureComponent { @@ -63,7 +64,8 @@ export default class FooterInfo extends React.PureComponent { toggleShowRewards, showRewards, toggleShowBinance, - showBinance + showBinance, + binanceSupported } = this.props return ( @@ -100,6 +102,7 @@ export default class FooterInfo extends React.PureComponent { showRewards={showRewards} toggleShowBinance={toggleShowBinance} showBinance={showBinance} + binanceSupported={binanceSupported} /> diff --git a/components/brave_new_tab_ui/containers/newTab/index.tsx b/components/brave_new_tab_ui/containers/newTab/index.tsx index 1995381a4e26..72721bf8dc09 100644 --- a/components/brave_new_tab_ui/containers/newTab/index.tsx +++ b/components/brave_new_tab_ui/containers/newTab/index.tsx @@ -189,7 +189,12 @@ class NewTabPage extends React.Component { } 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() @@ -383,7 +388,7 @@ class NewTabPage extends React.Component { const { newTabData } = this.props const { binanceState, showBinance } = newTabData - if (!showBinance) { + if (!showBinance || !binanceState.binanceSupported) { return null } @@ -405,6 +410,7 @@ class NewTabPage extends React.Component { render () { const { newTabData, gridSitesData, actions } = this.props const { showSettingsMenu } = this.state + const { binanceState } = newTabData if (!newTabData) { return null @@ -513,6 +519,7 @@ class NewTabPage extends React.Component { allowBrandedWallpaperUI={newTabData.featureFlagBraveNTPBrandedWallpaper} toggleShowRewards={this.toggleShowRewards} toggleShowBinance={this.toggleShowBinance} + binanceSupported={binanceState.binanceSupported} /> diff --git a/components/brave_new_tab_ui/containers/newTab/settings.tsx b/components/brave_new_tab_ui/containers/newTab/settings.tsx index 3d914b78553d..393b180f3d4f 100644 --- a/components/brave_new_tab_ui/containers/newTab/settings.tsx +++ b/components/brave_new_tab_ui/containers/newTab/settings.tsx @@ -31,6 +31,7 @@ export interface Props { allowBrandedWallpaperUI: boolean showRewards: boolean showBinance: boolean + binanceSupported: boolean } export default class Settings extends React.PureComponent { @@ -82,7 +83,8 @@ export default class Settings extends React.PureComponent { allowBrandedWallpaperUI, onClick, toggleShowBinance, - showBinance + showBinance, + binanceSupported } = this.props return ( @@ -117,14 +119,18 @@ export default class Settings extends React.PureComponent { size='small' /> - - {getLocale('showBinance')} - - + { + binanceSupported + ? + {getLocale('showBinance')} + + + : null + } {getLocale('showBraveStats')} = (state: NewTab.S case types.SET_INITIAL_REWARDS_DATA: const initialRewardsDataPayload = payload as InitialRewardsData const newRewardsState = { - onlyAnonWallet: initialRewardsDataPayload.onlyAnonWallet, balance: initialRewardsDataPayload.balance, totalContribution: getTotalContributions(initialRewardsDataPayload.report), adsEstimatedEarnings: initialRewardsDataPayload.adsEstimatedEarnings @@ -363,6 +362,28 @@ export const newTabReducer: Reducer = (state: NewTab.S } break + case types.SET_ONLY_ANON_WALLET: + state = { ...state } + state = { + ...state, + rewardsState: { + ...state.rewardsState, + onlyAnonWallet: payload.onlyAnonWallet + } + } + break + + case types.SET_BINANCE_SUPPORTED: + state = { ...state } + state = { + ...state, + binanceState: { + ...state.binanceState, + binanceSupported: payload.supported + } + } + break + default: break } diff --git a/components/brave_new_tab_ui/storage/new_tab_storage.ts b/components/brave_new_tab_ui/storage/new_tab_storage.ts index 3426d0ca14b5..a6c385471f5f 100644 --- a/components/brave_new_tab_ui/storage/new_tab_storage.ts +++ b/components/brave_new_tab_ui/storage/new_tab_storage.ts @@ -56,7 +56,8 @@ export const defaultState: NewTab.State = { initialFiat: 'USD', initialAmount: '', initialAsset: 'BTC', - userTLDAutoSet: false + userTLDAutoSet: false, + binanceSupported: false } } diff --git a/components/definitions/chromel.d.ts b/components/definitions/chromel.d.ts index aa1d35ac4c1f..9436af1d94fc 100644 --- a/components/definitions/chromel.d.ts +++ b/components/definitions/chromel.d.ts @@ -154,6 +154,7 @@ declare namespace chrome.braveRewards { declare namespace chrome.binance { const getUserTLD: (callback: (userTLD: string) => void) => {} + const isSupportedRegion: (callback: (supported: boolean) => void) => {} } declare namespace chrome.rewardsNotifications { diff --git a/components/definitions/newTab.d.ts b/components/definitions/newTab.d.ts index ff8ac88d1cdd..e7817b981910 100644 --- a/components/definitions/newTab.d.ts +++ b/components/definitions/newTab.d.ts @@ -146,6 +146,7 @@ declare namespace NewTab { initialAmount: string initialAsset: string userTLDAutoSet: boolean + binanceSupported: boolean } export type BinanceTLD = 'us' | 'com'