Skip to content

Commit

Permalink
Merge pull request #1941 from brave/ads-supported-region
Browse files Browse the repository at this point in the history
Disabling Ads Panel for disallowed regions
  • Loading branch information
ryanml committed Mar 21, 2019
2 parents 5921617 + 0ff512b commit a8b9794
Show file tree
Hide file tree
Showing 20 changed files with 95 additions and 12 deletions.
20 changes: 20 additions & 0 deletions browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void GetConfirmationsHistory(const base::ListValue* args);
void GetRewardsMainEnabled(const base::ListValue* args);
void OnGetRewardsMainEnabled(bool enabled);
void OnAdsIsSupportedRegion(bool is_supported);

void GetExcludedPublishersNumber(const base::ListValue* args);
void AdsIsSupportedRegion(const base::ListValue* args);

void OnConfirmationsHistory(int total_viewed, double estimated_earnings);

Expand Down Expand Up @@ -283,6 +285,9 @@ void RewardsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("brave_rewards.getExcludedPublishersNumber",
base::BindRepeating(&RewardsDOMHandler::GetExcludedPublishersNumber,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.getAdsIsSupportedRegion",
base::BindRepeating(&RewardsDOMHandler::AdsIsSupportedRegion,
base::Unretained(this)));
}

void RewardsDOMHandler::Init() {
Expand Down Expand Up @@ -1024,6 +1029,21 @@ void RewardsDOMHandler::GetExcludedPublishersNumber(
}
}

void RewardsDOMHandler::AdsIsSupportedRegion(
const base::ListValue* args) {
ads_service_->IsSupportedRegion(base::BindOnce(
&RewardsDOMHandler::OnAdsIsSupportedRegion,
weak_factory_.GetWeakPtr()));
}

void RewardsDOMHandler::OnAdsIsSupportedRegion(
bool is_supported) {
if (web_ui()->CanCallJavascript()) {
web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.adsIsSupportedRegion",
base::Value(is_supported));
}
}

} // namespace

BraveRewardsUI::BraveRewardsUI(content::WebUI* web_ui, const std::string& name)
Expand Down
1 change: 1 addition & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "adsDisabledTextOne", IDS_BRAVE_REWARDS_LOCAL_ADS_DISABLED_TEXT_ONE }, // NOLINT
{ "adsDisabledTextTwo", IDS_BRAVE_REWARDS_LOCAL_ADS_DISABLED_TEXT_TWO }, // NOLINT
{ "adsNotificationsReceived", IDS_BRAVE_REWARDS_LOCAL_ADS_NOTIFICATIONS_RECEIVED }, // NOLINT
{ "adsNotSupported", IDS_BRAVE_REWARDS_LOCAL_ADS_NOT_SUPPORTED },
{ "adsPaymentDate", IDS_BRAVE_REWARDS_LOCAL_ADS_PAYMENT_DATE },
{ "adsPagesViewed", IDS_BRAVE_REWARDS_LOCAL_ADS_PAGES_VIEWED },
{ "adsPerHour", IDS_BRAVE_REWARDS_LOCAL_ADS_PER_HOUR },
Expand Down
4 changes: 4 additions & 0 deletions components/brave_ads/browser/ads_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace brave_ads {

using IsSupportedRegionCallback = base::OnceCallback<void(bool)>;

class AdsService : public KeyedService {
public:
AdsService() = default;
Expand All @@ -37,6 +39,8 @@ class AdsService : public KeyedService {
virtual void ClassifyPage(const std::string& url,
const std::string& page) = 0;
virtual void SetConfirmationsIsReady(const bool is_ready) = 0;
virtual void IsSupportedRegion(
IsSupportedRegionCallback callback) = 0;

private:
DISALLOW_COPY_AND_ASSIGN(AdsService);
Expand Down
8 changes: 8 additions & 0 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -998,4 +998,12 @@ bool AdsServiceImpl::connected() {
return bat_ads_.is_bound();
}

void AdsServiceImpl::IsSupportedRegion(
IsSupportedRegionCallback callback) {
if (!connected())
return;

bat_ads_->IsSupportedRegion(std::move(callback));
}

} // namespace brave_ads
2 changes: 2 additions & 0 deletions components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class AdsServiceImpl : public AdsService,
void OnMediaStop(SessionID tab_id) override;
void ClassifyPage(const std::string& url, const std::string& page) override;
void SetConfirmationsIsReady(const bool is_ready) override;
void IsSupportedRegion(
IsSupportedRegionCallback callback) override;

void Shutdown() override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,9 @@ export const getConfirmationsHistory = () => action(types.GET_CONFIRMATIONS_HIST
export const onConfirmationsHistoryChanged = () => action(types.ON_CONFIRMATIONS_HISTORY_CHANGED)

export const getExcludedPublishersNumber = () => action(types.GET_EXCLUDED_PUBLISHERS_NUMBER)

export const getAdsIsSupportedRegion = () => action(types.GET_ADS_IS_SUPPORTED_REGION)

export const onAdsIsSupportedRegion = (supported: boolean) => action(types.ON_ADS_IS_SUPPORTED_REGION, {
supported
})
7 changes: 6 additions & 1 deletion components/brave_rewards/resources/ui/brave_rewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ window.cr.define('brave_rewards', function () {
getActions().onConfirmationsHistoryChanged()
}

function adsIsSupportedRegion (supported: boolean) {
getActions().onAdsIsSupportedRegion(supported)
}

return {
initialize,
walletCreated,
Expand Down Expand Up @@ -182,7 +186,8 @@ window.cr.define('brave_rewards', function () {
rewardsEnabled,
addressesForPaymentId,
confirmationsHistory,
confirmationsHistoryChanged
confirmationsHistoryChanged,
adsIsSupportedRegion
}
})

Expand Down
23 changes: 19 additions & 4 deletions components/brave_rewards/resources/ui/components/adsBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { connect } from 'react-redux'
// Components
import {
Box,
BoxAlert,
DisabledContent,
List,
NextContribution,
Expand Down Expand Up @@ -56,6 +57,16 @@ class AdsBox extends React.Component<Props, State> {
)
}

adsNotSupportedAlert = (supported: boolean) => {
if (supported) {
return null
}

return (
<BoxAlert type={'ads'} />
)
}

onAdsSettingChange = (key: string, value: string) => {
let newValue: any = value
const { adsEnabled } = this.props.rewardsData.adsData
Expand Down Expand Up @@ -106,6 +117,7 @@ class AdsBox extends React.Component<Props, State> {
let adsUIEnabled = false
let notificationsReceived = 0
let estimatedEarnings = '0'
let adsIsSupported = false

const {
adsData,
Expand All @@ -119,24 +131,27 @@ class AdsBox extends React.Component<Props, State> {
adsUIEnabled = adsData.adsUIEnabled
notificationsReceived = adsData.adsNotificationsReceived || 0
estimatedEarnings = (adsData.adsEstimatedEarnings || 0).toFixed(2)
adsIsSupported = adsData.adsIsSupported
}

const toggle = !(!enabledMain || !adsUIEnabled)
const showDisabled = firstLoad !== false || !toggle || !adsEnabled
const enabled = adsEnabled && adsIsSupported
const toggle = !(!enabledMain || !adsUIEnabled || !adsIsSupported)
const showDisabled = firstLoad !== false || !toggle || !adsEnabled || !adsIsSupported

return (
<Box
title={getLocale('adsTitle')}
type={'ads'}
description={getLocale('adsDesc')}
toggle={toggle}
checked={adsEnabled}
settingsChild={this.adsSettings(adsEnabled && enabledMain)}
checked={enabled}
settingsChild={this.adsSettings(enabled && enabledMain)}
testId={'braveAdsSettings'}
disabledContent={showDisabled ? this.adsDisabled() : null}
onToggle={this.onAdsSettingChange.bind(this, 'adsEnabled', '')}
settingsOpened={this.state.settings}
onSettingsClick={this.onSettingsToggle}
attachedAlert={this.adsNotSupportedAlert(adsIsSupported)}
>
<List title={getLocale('adsCurrentEarnings')}>
<Tokens
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { connect } from 'react-redux'
import {
DisabledContent,
Box,
BoxAlert,
TableDonation,
List,
Tokens,
ModalDonation,
TipsMigrationAlert,
NextContribution
} from 'brave-ui/features/rewards'
import { Provider } from 'brave-ui/features/rewards/profile'
Expand Down Expand Up @@ -147,7 +147,7 @@ class DonationBox extends React.Component<Props, State> {
importAlert = (walletImported: boolean) => {
return (
walletImported
? <TipsMigrationAlert onReview={this.doNothing} />
? <BoxAlert type={'tips'} onReview={this.doNothing} />
: null
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class SettingsPage extends React.Component<Props, {}> {
}
this.actions.checkImported()
this.actions.getGrants()
this.actions.getAdsIsSupportedRegion()

// one time check (legacy fix)
// more info here https://github.com/brave/brave-browser/issues/2172
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,7 @@ export const enum types {
GET_CONFIRMATIONS_HISTORY = '@@rewards/GET_CONFIRMATIONS_HISTORY',
ON_CONFIRMATIONS_HISTORY = '@@rewards/ON_CONFIRMATIONS_HISTORY',
ON_CONFIRMATIONS_HISTORY_CHANGED = '@@rewards/ON_CONFIRMATIONS_HISTORY_CHANGED',
GET_EXCLUDED_PUBLISHERS_NUMBER = '@@rewards/GET_EXCLUDED_PUBLISHERS_NUMBER'
GET_EXCLUDED_PUBLISHERS_NUMBER = '@@rewards/GET_EXCLUDED_PUBLISHERS_NUMBER',
GET_ADS_IS_SUPPORTED_REGION = '@@rewards/GET_ADS_IS_SUPPORTED_REGION',
ON_ADS_IS_SUPPORTED_REGION = '@@rewards/ON_ADS_IS_SUPPORTED_REGION'
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ const rewardsReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State
state.adsData.adsEstimatedEarnings = data.adsEstimatedEarnings
break
}
case types.GET_ADS_IS_SUPPORTED_REGION: {
chrome.send('brave_rewards.getAdsIsSupportedRegion', [])
break
}
case types.ON_ADS_IS_SUPPORTED_REGION: {
state.adsData.adsIsSupported = action.payload.supported
break
}
}

return state
Expand Down
3 changes: 2 additions & 1 deletion components/brave_rewards/resources/ui/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export const defaultState: Rewards.State = {
adsPerHour: 0,
adsUIEnabled: false,
adsNotificationsReceived: 0,
adsEstimatedEarnings: 0
adsEstimatedEarnings: 0,
adsIsSupported: false
},
pendingContributionTotal: 0,
grants: [],
Expand Down
1 change: 1 addition & 0 deletions components/definitions/rewards.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,6 @@ declare namespace Rewards {
adsUIEnabled: boolean
adsNotificationsReceived: number
adsEstimatedEarnings: number
adsIsSupported: boolean
}
}
1 change: 1 addition & 0 deletions components/resources/brave_components_strings.grd
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
<message name="IDS_BRAVE_REWARDS_LOCAL_ADS_CURRENT_EARNINGS" desc="">Current earnings this month (Estimated)</message>
<message name="IDS_BRAVE_REWARDS_LOCAL_ADS_PAYMENT_DATE" desc="">Payment date</message>
<message name="IDS_BRAVE_REWARDS_LOCAL_ADS_NOTIFICATIONS_RECEIVED" desc="">Ad notifications received</message>
<message name="IDS_BRAVE_REWARDS_LOCAL_ADS_NOT_SUPPORTED" desc="">Sorry! Ads are not yet available in your region.</message>
<message name="IDS_BRAVE_REWARDS_LOCAL_ADS_PAGES_VIEWED" desc="">Ad pages viewed</message>
<message name="IDS_BRAVE_REWARDS_LOCAL_ADS_PER_HOUR" desc="">Maximum number of ads displayed</message>
<message name="IDS_BRAVE_REWARDS_LOCAL_ADS_PER_HOUR_1" desc="">1 ad per hour</message>
Expand Down
5 changes: 5 additions & 0 deletions components/services/bat_ads/bat_ads_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,9 @@ void BatAdsImpl::GenerateAdReportingNotificationResultEvent(
}
}

void BatAdsImpl::IsSupportedRegion(
IsSupportedRegionCallback callback) {
std::move(callback).Run(ads_->IsSupportedRegion());
}

} // namespace bat_ads
2 changes: 2 additions & 0 deletions components/services/bat_ads/bat_ads_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class BatAdsImpl : public mojom::BatAds {
void GenerateAdReportingNotificationResultEvent(
const std::string& notification_info,
int32_t event_type) override;
void IsSupportedRegion(
IsSupportedRegionCallback callback) override;

private:
std::unique_ptr<BatAdsClientMojoBridge> bat_ads_client_mojo_proxy_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@ interface BatAds {
GenerateAdReportingNotificationShownEvent(string notification_info);
GenerateAdReportingNotificationResultEvent(
string notification_info, int32 result_type);
IsSupportedRegion() => (bool is_supported);
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@
"@types/react-redux": "6.0.4",
"@types/redux-logger": "^3.0.7",
"awesome-typescript-loader": "^5.2.1",
"brave-ui": "github:brave/brave-ui#918e8578c9607e458103f444ef98794cf8dbfebd",
"brave-ui": "github:brave/brave-ui#d364c5edc26fb1946bf628499dc3237a6456c856",
"css-loader": "^0.28.9",
"csstype": "^2.5.5",
"deep-freeze-node": "^1.1.3",
Expand Down

0 comments on commit a8b9794

Please sign in to comment.