Decouple subdivision targeting codes from Rewards.
fix https://github.com/brave/brave-browser/issues/25249
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
include_rules = [
|
||||
"+bat/ads",
|
||||
]
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "base/strings/string_number_conversions.h"
|
||||
#include "bat/ads/supported_subdivisions.h"
|
||||
#include "brave/browser/brave_ads/ads_service_factory.h"
|
||||
#include "brave/browser/brave_rewards/rewards_panel/rewards_panel_coordinator.h"
|
||||
#include "brave/browser/brave_rewards/rewards_service_factory.h"
|
||||
@@ -51,12 +52,6 @@ using brave_rewards::RewardsTabHelper;
|
||||
|
||||
namespace {
|
||||
|
||||
const char kShouldAllowAdsSubdivisionTargeting[] =
|
||||
"shouldAllowAdsSubdivisionTargeting";
|
||||
const char kAdsSubdivisionTargeting[] = "adsSubdivisionTargeting";
|
||||
const char kAutoDetectedAdsSubdivisionTargeting[] =
|
||||
"automaticallyDetectedAdsSubdivisionTargeting";
|
||||
|
||||
RewardsTabHelper* GetRewardsTabHelperForTabId(
|
||||
int tab_id,
|
||||
content::BrowserContext* browser_context) {
|
||||
@@ -1263,15 +1258,23 @@ ExtensionFunction::ResponseAction BraveRewardsGetAdsDataFunction::Run() {
|
||||
ads_data.Set(
|
||||
"adsPerHour",
|
||||
static_cast<int>(ads_service->GetMaximumNotificationAdsPerHour()));
|
||||
ads_data.Set(kAdsSubdivisionTargeting,
|
||||
ads_data.Set("adsSubdivisionTargeting",
|
||||
ads_service->GetSubdivisionTargetingCode());
|
||||
ads_data.Set(kAutoDetectedAdsSubdivisionTargeting,
|
||||
ads_data.Set("automaticallyDetectedAdsSubdivisionTargeting",
|
||||
ads_service->GetAutoDetectedSubdivisionTargetingCode());
|
||||
ads_data.Set(kShouldAllowAdsSubdivisionTargeting,
|
||||
ads_data.Set("shouldAllowAdsSubdivisionTargeting",
|
||||
ads_service->ShouldAllowSubdivisionTargeting());
|
||||
ads_data.Set("adsUIEnabled", true);
|
||||
|
||||
ads_data.Set("countryCode", brave_l10n::GetDefaultISOCountryCodeString());
|
||||
base::Value::List subdivisions;
|
||||
const auto subdivision_infos = ads::GetSupportedSubdivisions();
|
||||
for (const auto& info : subdivision_infos) {
|
||||
base::Value::Dict subdivision;
|
||||
subdivision.Set("value", info.first);
|
||||
subdivision.Set("name", info.second);
|
||||
subdivisions.Append(std::move(subdivision));
|
||||
}
|
||||
ads_data.Set("subdivisions", std::move(subdivisions));
|
||||
|
||||
return RespondNow(OneArgument(base::Value(std::move(ads_data))));
|
||||
}
|
||||
|
||||
@@ -152,6 +152,7 @@ class SettingsBraveRewardsPage extends SettingsBraveRewardsPageBase {
|
||||
this.browserProxy_.getAdsData().then((adsData) => {
|
||||
this.shouldAllowAdsSubdivisionTargeting_ = adsData.shouldAllowAdsSubdivisionTargeting
|
||||
this.countryCode_ = adsData.countryCode
|
||||
this.subdivisions_ = adsData.subdivisions
|
||||
})
|
||||
}
|
||||
|
||||
@@ -200,87 +201,14 @@ class SettingsBraveRewardsPage extends SettingsBraveRewardsPageBase {
|
||||
}
|
||||
|
||||
adsSubdivisionTargetingCodes_() {
|
||||
if (this.countryCode_ == 'US') {
|
||||
return this.adsUSASubdivisionTargetingCodes_()
|
||||
if (!this.subdivisions_ || !this.subdivisions_.length) {
|
||||
return []
|
||||
}
|
||||
|
||||
if (this.countryCode_ == 'CA') {
|
||||
return this.adsCanadaSubdivisionTargetingCodes_()
|
||||
}
|
||||
|
||||
return []
|
||||
}
|
||||
|
||||
adsUSASubdivisionTargetingCodes_() {
|
||||
return [
|
||||
{ name: loadTimeData.getString('braveRewardsAutoDetectedItem'), value: 'AUTO' },
|
||||
{ name: loadTimeData.getString('braveRewardsDisabledItem'), value: 'DISABLED' },
|
||||
{ name: 'Alabama', value: 'US-AL' },
|
||||
{ name: 'Alaska', value: 'US-AK' },
|
||||
{ name: 'Arizona', value: 'US-AZ' },
|
||||
{ name: 'Arkansas', value: 'US-AR' },
|
||||
{ name: 'California', value: 'US-CA' },
|
||||
{ name: 'Colorado', value: 'US-CO' },
|
||||
{ name: 'Connecticut', value: 'US-CT' },
|
||||
{ name: 'Delaware', value: 'US-DE' },
|
||||
{ name: 'Florida', value: 'US-FL' },
|
||||
{ name: 'Georgia', value: 'US-GA' },
|
||||
{ name: 'Hawaii', value: 'US-HI' },
|
||||
{ name: 'Idaho', value: 'US-ID' },
|
||||
{ name: 'Illinois', value: 'US-IL' },
|
||||
{ name: 'Indiana', value: 'US-IN' },
|
||||
{ name: 'Iowa', value: 'US-IA' },
|
||||
{ name: 'Kansas', value: 'US-KS' },
|
||||
{ name: 'Kentucky', value: 'US-KY' },
|
||||
{ name: 'Louisiana', value: 'US-LA' },
|
||||
{ name: 'Maine', value: 'US-ME' },
|
||||
{ name: 'Maryland', value: 'US-MD' },
|
||||
{ name: 'Massachusetts', value: 'US-MA' },
|
||||
{ name: 'Michigan', value: 'US-MI' },
|
||||
{ name: 'Minnesota', value: 'US-MN' },
|
||||
{ name: 'Mississippi', value: 'US-MS' },
|
||||
{ name: 'Missouri', value: 'US-MO' },
|
||||
{ name: 'Montana', value: 'US-MT' },
|
||||
{ name: 'Nebraska', value: 'US-NE' },
|
||||
{ name: 'Nevada', value: 'US-NV' },
|
||||
{ name: 'New Hampshire', value: 'US-NH' },
|
||||
{ name: 'New Jersey', value: 'US-NJ' },
|
||||
{ name: 'New Mexico', value: 'US-NM' },
|
||||
{ name: 'New York', value: 'US-NY' },
|
||||
{ name: 'North Carolina', value: 'US-NC' },
|
||||
{ name: 'North Dakota', value: 'US-ND' },
|
||||
{ name: 'Ohio', value: 'US-OH' },
|
||||
{ name: 'Oklahoma', value: 'US-OK' },
|
||||
{ name: 'Oregon', value: 'US-OR' },
|
||||
{ name: 'Pennsylvania', value: 'US-PA' },
|
||||
{ name: 'Rhode Island', value: 'US-RI' },
|
||||
{ name: 'South Carolina', value: 'US-SC' },
|
||||
{ name: 'South Dakota', value: 'US-SD' },
|
||||
{ name: 'Tennessee', value: 'US-TN' },
|
||||
{ name: 'Texas', value: 'US-TX' },
|
||||
{ name: 'Utah', value: 'US-UT' },
|
||||
{ name: 'Vermont', value: 'US-VT' },
|
||||
{ name: 'Virginia', value: 'US-VA' },
|
||||
{ name: 'Washington', value: 'US-WA' },
|
||||
{ name: 'West Virginia', value: 'US-WV' },
|
||||
{ name: 'Wisconsin', value: 'US-WI' },
|
||||
{ name: 'Wyoming', value: 'US-WY' }
|
||||
]
|
||||
}
|
||||
|
||||
adsCanadaSubdivisionTargetingCodes_() {
|
||||
return [
|
||||
{ name: loadTimeData.getString('braveRewardsAutoDetectedItem'), value: 'AUTO' },
|
||||
{ name: loadTimeData.getString('braveRewardsDisabledItem'), value: 'DISABLED' },
|
||||
{ name: 'Alberta', value: 'CA-AB' },
|
||||
{ name: 'British Columbia', value: 'CA-BC' },
|
||||
{ name: 'Manitoba', value: 'CA-MB' },
|
||||
{ name: 'New Brunswick', value: 'CA-NB' },
|
||||
{ name: 'Nova Scotia', value: 'CA-NS' },
|
||||
{ name: 'Ontario', value: 'CA-ON' },
|
||||
{ name: 'Quebec', value: 'CA-QC' },
|
||||
{ name: 'Saskatchewan', value: 'CA-SK' }
|
||||
]
|
||||
let subdivisions = this.subdivisions_.map(val => ({ ...val }))
|
||||
subdivisions.unshift({ name: loadTimeData.getString('braveRewardsDisabledItem'), value: 'DISABLED' })
|
||||
subdivisions.unshift({ name: loadTimeData.getString('braveRewardsAutoDetectedItem'), value: 'AUTO' })
|
||||
return subdivisions
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
include_rules = [
|
||||
"+chrome/services/qrcode_generator/public/mojom",
|
||||
"+chrome/services/qrcode_generator/public/cpp",
|
||||
"+bat/ads/supported_subdivisions.h",
|
||||
"+bat/ledger/mojom_structs.h",
|
||||
]
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "base/strings/string_number_conversions.h"
|
||||
#include "base/time/time.h"
|
||||
#include "bat/ads/pref_names.h"
|
||||
#include "bat/ads/supported_subdivisions.h"
|
||||
#include "bat/ledger/mojom_structs.h"
|
||||
#include "brave/browser/brave_ads/ads_service_factory.h"
|
||||
#include "brave/browser/brave_rewards/rewards_service_factory.h"
|
||||
@@ -303,14 +304,11 @@ class RewardsDOMHandler
|
||||
|
||||
namespace {
|
||||
|
||||
const int kDaysOfAdsHistory = 30;
|
||||
constexpr int kDaysOfAdsHistory = 30;
|
||||
|
||||
const char kShouldAllowAdsSubdivisionTargeting[] =
|
||||
"shouldAllowAdsSubdivisionTargeting";
|
||||
const char kAdsSubdivisionTargeting[] = "adsSubdivisionTargeting";
|
||||
const char kAutoDetectedSubdivisionTargeting[] =
|
||||
constexpr char kAdsSubdivisionTargeting[] = "adsSubdivisionTargeting";
|
||||
constexpr char kAutoDetectedSubdivisionTargeting[] =
|
||||
"automaticallyDetectedAdsSubdivisionTargeting";
|
||||
const char kNeedsBrowserUpgradeToServeAds[] = "needsBrowserUpgradeToServeAds";
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -1196,11 +1194,22 @@ void RewardsDOMHandler::GetAdsData(const base::Value::List& args) {
|
||||
ads_service_->GetSubdivisionTargetingCode());
|
||||
ads_data.Set(kAutoDetectedSubdivisionTargeting,
|
||||
ads_service_->GetAutoDetectedSubdivisionTargetingCode());
|
||||
ads_data.Set(kShouldAllowAdsSubdivisionTargeting,
|
||||
ads_data.Set("shouldAllowAdsSubdivisionTargeting",
|
||||
ads_service_->ShouldAllowSubdivisionTargeting());
|
||||
ads_data.Set("adsUIEnabled", true);
|
||||
ads_data.Set(kNeedsBrowserUpgradeToServeAds,
|
||||
ads_data.Set("needsBrowserUpgradeToServeAds",
|
||||
ads_service_->NeedsBrowserUpgradeToServeAds());
|
||||
|
||||
base::Value::List subdivisions;
|
||||
const auto supported_subdivisions = ads::GetSupportedSubdivisions();
|
||||
for (const auto& subdivision : supported_subdivisions) {
|
||||
base::Value::Dict subdivision_dict;
|
||||
subdivision_dict.Set("code", subdivision.first);
|
||||
subdivision_dict.Set("name", subdivision.second);
|
||||
subdivisions.Append(std::move(subdivision_dict));
|
||||
}
|
||||
|
||||
ads_data.Set("subdivisions", std::move(subdivisions));
|
||||
CallJavascriptFunction("brave_rewards.adsData",
|
||||
base::Value(std::move(ads_data)));
|
||||
}
|
||||
|
||||
@@ -1091,8 +1091,21 @@
|
||||
"adsUIEnabled": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"countryCode": {
|
||||
"type": "string"
|
||||
"subdivisions": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
"description": "subdivision name"
|
||||
},
|
||||
"value": {
|
||||
"type": "string",
|
||||
"description": "subdivision code"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ import { Grid, Column, ControlWrapper } from 'brave-ui/components'
|
||||
import { AlertCircleIcon } from 'brave-ui/components/icons'
|
||||
|
||||
import { LayoutKind } from '../lib/layout_context'
|
||||
import { getAdsSubdivisions } from '../../shared/lib/ads_subdivisions'
|
||||
import { externalWalletProviderFromString } from '../../shared/lib/external_wallet'
|
||||
import { getProviderPayoutStatus } from '../../shared/lib/provider_payout_status'
|
||||
import { PaymentStatusView } from '../../shared/components/payment_status_view'
|
||||
@@ -151,10 +150,10 @@ class AdsBox extends React.Component<Props, State> {
|
||||
onChange={selectChangeHandler('adsSubdivisionTargeting')}
|
||||
>
|
||||
{
|
||||
getAdsSubdivisions(this.props.rewardsData).map((subdivision) => {
|
||||
this.getAdsSubdivisions().map((subdivision) => {
|
||||
return (
|
||||
<option key={subdivision[0]} value={subdivision[0]}>
|
||||
{subdivision[1]}
|
||||
<option key={subdivision.code} value={subdivision.code}>
|
||||
{subdivision.name}
|
||||
</option>
|
||||
)
|
||||
})
|
||||
@@ -260,6 +259,36 @@ class AdsBox extends React.Component<Props, State> {
|
||||
return groupedAdsHistory
|
||||
}
|
||||
|
||||
getAdsSubdivisions = (): Rewards.Subdivision[] => {
|
||||
const {
|
||||
adsSubdivisionTargeting,
|
||||
automaticallyDetectedAdsSubdivisionTargeting,
|
||||
subdivisions
|
||||
} = this.props.rewardsData.adsData
|
||||
|
||||
if (!subdivisions || !subdivisions.length) {
|
||||
return []
|
||||
}
|
||||
|
||||
let adsSubdivisionsList: Rewards.Subdivision[] = subdivisions.map(val => ({ ...val }))
|
||||
|
||||
if (adsSubdivisionTargeting === 'DISABLED') {
|
||||
adsSubdivisionsList.unshift({ code: 'DISABLED', name: getLocale('adsSubdivisionTargetingDisabled') })
|
||||
} else {
|
||||
adsSubdivisionsList.unshift({ code: 'DISABLED', name: getLocale('adsSubdivisionTargetingDisable') })
|
||||
}
|
||||
|
||||
const subdivisionMap = new Map<string, string>(subdivisions.map(v => [v.code, v.name]))
|
||||
const subdivision = subdivisionMap.get(automaticallyDetectedAdsSubdivisionTargeting)
|
||||
if (subdivision && adsSubdivisionTargeting === 'AUTO') {
|
||||
adsSubdivisionsList.unshift({ code: 'AUTO', name: getLocale('adsSubdivisionTargetingAutoDetectedAs', { adsSubdivisionTarget: subdivision }) })
|
||||
} else {
|
||||
adsSubdivisionsList.unshift({ code: 'AUTO', name: getLocale('adsSubdivisionTargetingAutoDetect') })
|
||||
}
|
||||
|
||||
return adsSubdivisionsList
|
||||
}
|
||||
|
||||
getAdDetailRow = (adHistory: Rewards.AdHistory) => {
|
||||
let brand = adHistory.adContent.brand
|
||||
if (brand.length > 50) {
|
||||
|
||||
@@ -29,6 +29,7 @@ export function defaultState (): Rewards.State {
|
||||
adsSubdivisionTargeting: '',
|
||||
automaticallyDetectedAdsSubdivisionTargeting: '',
|
||||
shouldAllowAdsSubdivisionTargeting: true,
|
||||
subdivisions: [],
|
||||
adsUIEnabled: false,
|
||||
adsIsSupported: false,
|
||||
needsBrowserUpgradeToServeAds: false,
|
||||
|
||||
@@ -128,6 +128,7 @@ const rewardsReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State
|
||||
state.adsData.adsSubdivisionTargeting = action.payload.adsData.adsSubdivisionTargeting
|
||||
state.adsData.automaticallyDetectedAdsSubdivisionTargeting = action.payload.adsData.automaticallyDetectedAdsSubdivisionTargeting
|
||||
state.adsData.shouldAllowAdsSubdivisionTargeting = action.payload.adsData.shouldAllowAdsSubdivisionTargeting
|
||||
state.adsData.subdivisions = action.payload.adsData.subdivisions
|
||||
state.adsData.adsUIEnabled = action.payload.adsData.adsUIEnabled
|
||||
state.adsData.adsIsSupported = action.payload.adsData.adsIsSupported
|
||||
state.adsData.needsBrowserUpgradeToServeAds = action.payload.adsData.needsBrowserUpgradeToServeAds
|
||||
|
||||
@@ -33,6 +33,7 @@ export const reduxState: Rewards.ApplicationState = {
|
||||
adsSubdivisionTargeting: '',
|
||||
automaticallyDetectedAdsSubdivisionTargeting: '',
|
||||
shouldAllowAdsSubdivisionTargeting: true,
|
||||
subdivisions: [],
|
||||
adsUIEnabled: true,
|
||||
adsIsSupported: true,
|
||||
needsBrowserUpgradeToServeAds: false,
|
||||
|
||||
@@ -1,111 +0,0 @@
|
||||
/* 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/. */
|
||||
|
||||
import { getLocale } from '../../../../common/locale'
|
||||
|
||||
export function getAdsSubdivisions (
|
||||
rewardsData: Rewards.State
|
||||
): Array<[string, string]> {
|
||||
const {
|
||||
currentCountryCode
|
||||
} = rewardsData
|
||||
|
||||
const {
|
||||
adsSubdivisionTargeting,
|
||||
automaticallyDetectedAdsSubdivisionTargeting
|
||||
} = rewardsData.adsData
|
||||
|
||||
let subdivisions: Array<[string, string]> = []
|
||||
if (currentCountryCode === 'US') {
|
||||
subdivisions = getUnitedStatesSubdivisions()
|
||||
} else if (currentCountryCode === 'CA') {
|
||||
subdivisions = getCanadaSubdivisions()
|
||||
} else {
|
||||
return subdivisions
|
||||
}
|
||||
|
||||
if (adsSubdivisionTargeting === 'DISABLED') {
|
||||
subdivisions.unshift(['DISABLED', getLocale('adsSubdivisionTargetingDisabled')])
|
||||
} else {
|
||||
subdivisions.unshift(['DISABLED', getLocale('adsSubdivisionTargetingDisable')])
|
||||
}
|
||||
|
||||
const subdivisionMap = new Map<string, string>(subdivisions)
|
||||
const subdivision = subdivisionMap.get(automaticallyDetectedAdsSubdivisionTargeting)
|
||||
if (subdivision && adsSubdivisionTargeting === 'AUTO') {
|
||||
subdivisions.unshift(['AUTO', getLocale('adsSubdivisionTargetingAutoDetectedAs', { adsSubdivisionTarget: subdivision })])
|
||||
} else {
|
||||
subdivisions.unshift(['AUTO', getLocale('adsSubdivisionTargetingAutoDetect')])
|
||||
}
|
||||
|
||||
return subdivisions
|
||||
}
|
||||
|
||||
function getUnitedStatesSubdivisions (): Array<[string, string]> {
|
||||
return [
|
||||
['US-AL', 'Alabama'],
|
||||
['US-AK', 'Alaska'],
|
||||
['US-AZ', 'Arizona'],
|
||||
['US-AR', 'Arkansas'],
|
||||
['US-CA', 'California'],
|
||||
['US-CO', 'Colorado'],
|
||||
['US-CT', 'Connecticut'],
|
||||
['US-DE', 'Delaware'],
|
||||
['US-FL', 'Florida'],
|
||||
['US-GA', 'Georgia'],
|
||||
['US-HI', 'Hawaii'],
|
||||
['US-ID', 'Idaho'],
|
||||
['US-IL', 'Illinois'],
|
||||
['US-IN', 'Indiana'],
|
||||
['US-IA', 'Iowa'],
|
||||
['US-KS', 'Kansas'],
|
||||
['US-KY', 'Kentucky'],
|
||||
['US-LA', 'Louisiana'],
|
||||
['US-ME', 'Maine'],
|
||||
['US-MD', 'Maryland'],
|
||||
['US-MA', 'Massachusetts'],
|
||||
['US-MI', 'Michigan'],
|
||||
['US-MN', 'Minnesota'],
|
||||
['US-MS', 'Mississippi'],
|
||||
['US-MO', 'Missouri'],
|
||||
['US-MT', 'Montana'],
|
||||
['US-NE', 'Nebraska'],
|
||||
['US-NV', 'Nevada'],
|
||||
['US-NH', 'New Hampshire'],
|
||||
['US-NJ', 'New Jersey'],
|
||||
['US-NM', 'New Mexico'],
|
||||
['US-NY', 'New York'],
|
||||
['US-NC', 'North Carolina'],
|
||||
['US-ND', 'North Dakota'],
|
||||
['US-OH', 'Ohio'],
|
||||
['US-OK', 'Oklahoma'],
|
||||
['US-OR', 'Oregon'],
|
||||
['US-PA', 'Pennsylvania'],
|
||||
['US-RI', 'Rhode Island'],
|
||||
['US-SC', 'South Carolina'],
|
||||
['US-SD', 'South Dakota'],
|
||||
['US-TN', 'Tennessee'],
|
||||
['US-TX', 'Texas'],
|
||||
['US-UT', 'Utah'],
|
||||
['US-VT', 'Vermont'],
|
||||
['US-VA', 'Virginia'],
|
||||
['US-WA', 'Washington'],
|
||||
['US-WV', 'West Virginia'],
|
||||
['US-WI', 'Wisconsin'],
|
||||
['US-WY', 'Wyoming']
|
||||
]
|
||||
}
|
||||
|
||||
function getCanadaSubdivisions (): Array<[string, string]> {
|
||||
return [
|
||||
['CA-AB', 'Alberta'],
|
||||
['CA-BC', 'British Columbia'],
|
||||
['CA-MB', 'Manitoba'],
|
||||
['CA-NB', 'New Brunswick'],
|
||||
['CA-NS', 'Nova Scotia'],
|
||||
['CA-ON', 'Ontario'],
|
||||
['CA-QC', 'Quebec'],
|
||||
['CA-SK', 'Saskatchewan']
|
||||
]
|
||||
}
|
||||
Vendored
+6
@@ -226,12 +226,18 @@ declare namespace Rewards {
|
||||
hint: string
|
||||
}
|
||||
|
||||
export interface Subdivision {
|
||||
name: string
|
||||
code: string
|
||||
}
|
||||
|
||||
export interface AdsData {
|
||||
adsEnabled: boolean
|
||||
adsPerHour: number
|
||||
adsSubdivisionTargeting: string
|
||||
automaticallyDetectedAdsSubdivisionTargeting: string
|
||||
shouldAllowAdsSubdivisionTargeting: boolean
|
||||
subdivisions: Subdivision[]
|
||||
adsUIEnabled: boolean
|
||||
adsIsSupported: boolean
|
||||
needsBrowserUpgradeToServeAds: boolean
|
||||
|
||||
@@ -95,6 +95,7 @@ describe('rewards reducer', () => {
|
||||
adsSubdivisionTargeting: 'US-CA',
|
||||
automaticallyDetectedAdsSubdivisionTargeting: 'US-FL',
|
||||
shouldAllowAdsSubdivisionTargeting: true,
|
||||
subdivisions: [],
|
||||
isAdsSubdivisionTargetingRegion: true,
|
||||
adsUIEnabled: false,
|
||||
adsIsSupported: false,
|
||||
@@ -110,6 +111,7 @@ describe('rewards reducer', () => {
|
||||
adsSubdivisionTargeting: 'US-CA',
|
||||
automaticallyDetectedAdsSubdivisionTargeting: 'US-FL',
|
||||
shouldAllowAdsSubdivisionTargeting: true,
|
||||
subdivisions: [],
|
||||
isAdsSubdivisionTargetingRegion: true,
|
||||
adsUIEnabled: true,
|
||||
adsIsSupported: true,
|
||||
@@ -129,6 +131,7 @@ describe('rewards reducer', () => {
|
||||
adsSubdivisionTargeting: 'US-CA',
|
||||
automaticallyDetectedAdsSubdivisionTargeting: 'US-FL',
|
||||
shouldAllowAdsSubdivisionTargeting: true,
|
||||
subdivisions: [],
|
||||
adsUIEnabled: true,
|
||||
adsIsSupported: true
|
||||
}
|
||||
|
||||
Vendored
+3
@@ -92,6 +92,7 @@ source_set("headers") {
|
||||
"include/bat/ads/pref_names.h",
|
||||
"include/bat/ads/promoted_content_ad_info.h",
|
||||
"include/bat/ads/promoted_content_ad_value_util.h",
|
||||
"include/bat/ads/supported_subdivisions.h",
|
||||
"include/bat/ads/sys_info.h",
|
||||
]
|
||||
|
||||
@@ -856,6 +857,7 @@ source_set("ads") {
|
||||
"src/bat/ads/internal/geographic/subdivision/get_subdivision_url_request_builder.h",
|
||||
"src/bat/ads/internal/geographic/subdivision/subdivision_targeting.cc",
|
||||
"src/bat/ads/internal/geographic/subdivision/subdivision_targeting.h",
|
||||
"src/bat/ads/internal/geographic/subdivision/supported_subdivision_codes.cc",
|
||||
"src/bat/ads/internal/geographic/subdivision/supported_subdivision_codes.h",
|
||||
"src/bat/ads/internal/history/ad_content_util.cc",
|
||||
"src/bat/ads/internal/history/ad_content_util.h",
|
||||
@@ -1165,6 +1167,7 @@ source_set("ads") {
|
||||
"src/bat/ads/pref_names.cc",
|
||||
"src/bat/ads/promoted_content_ad_info.cc",
|
||||
"src/bat/ads/promoted_content_ad_value_util.cc",
|
||||
"src/bat/ads/supported_subdivisions.cc",
|
||||
"src/bat/ads/sys_info.cc",
|
||||
]
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
/* Copyright (c) 2022 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_VENDOR_BAT_NATIVE_ADS_INCLUDE_BAT_ADS_SUPPORTED_SUBDIVISIONS_H_
|
||||
#define BRAVE_VENDOR_BAT_NATIVE_ADS_INCLUDE_BAT_ADS_SUPPORTED_SUBDIVISIONS_H_
|
||||
|
||||
#include "base/containers/flat_map.h"
|
||||
#include "base/strings/string_piece_forward.h"
|
||||
#include "bat/ads/export.h"
|
||||
|
||||
namespace ads {
|
||||
|
||||
using SupportedSubdivisions =
|
||||
base::flat_map<base::StringPiece, base::StringPiece>;
|
||||
|
||||
ADS_EXPORT SupportedSubdivisions GetSupportedSubdivisions();
|
||||
|
||||
} // namespace ads
|
||||
|
||||
#endif // BRAVE_VENDOR_BAT_NATIVE_ADS_INCLUDE_BAT_ADS_SUPPORTED_SUBDIVISIONS_H_
|
||||
+10
@@ -8,10 +8,20 @@
|
||||
#include <vector>
|
||||
|
||||
#include "base/check_op.h"
|
||||
#include "base/containers/contains.h"
|
||||
#include "base/strings/string_split.h"
|
||||
#include "bat/ads/internal/geographic/subdivision/supported_subdivision_codes.h"
|
||||
#include "brave/components/l10n/common/locale_util.h"
|
||||
|
||||
namespace ads::locale {
|
||||
|
||||
bool IsSupportedCountryCodeForSubdivisionTargeting(
|
||||
const std::string& country_code) {
|
||||
const auto& supported_subdivision_codes =
|
||||
::ads::geographic::GetSupportedSubdivisionCodes();
|
||||
return base::Contains(supported_subdivision_codes, country_code);
|
||||
}
|
||||
|
||||
std::string GetCountryCode(const std::string& code) {
|
||||
const std::vector<std::string> components =
|
||||
base::SplitString(code, "-", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
|
||||
|
||||
+3
@@ -10,6 +10,9 @@
|
||||
|
||||
namespace ads::locale {
|
||||
|
||||
bool IsSupportedCountryCodeForSubdivisionTargeting(
|
||||
const std::string& country_code);
|
||||
|
||||
std::string GetCountryCode(const std::string& code);
|
||||
|
||||
std::string GetSubdivisionCode(const std::string& code);
|
||||
|
||||
+12
@@ -31,4 +31,16 @@ TEST(BatAdsSubdivisionCodeUtilTest, GetSubdivisionCode) {
|
||||
EXPECT_EQ("CA", subdivision_code);
|
||||
}
|
||||
|
||||
TEST(BatAdsSubdivisionCodeUtilTest,
|
||||
IsSupportedCountryCodeForSubdivisionTargeting) {
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
|
||||
// Assert
|
||||
EXPECT_TRUE(locale::IsSupportedCountryCodeForSubdivisionTargeting("US"));
|
||||
EXPECT_TRUE(locale::IsSupportedCountryCodeForSubdivisionTargeting("CA"));
|
||||
EXPECT_FALSE(locale::IsSupportedCountryCodeForSubdivisionTargeting("ES"));
|
||||
}
|
||||
|
||||
} // namespace ads
|
||||
|
||||
@@ -61,4 +61,17 @@ See [ISO_3166-2](https://en.wikipedia.org/wiki/ISO_3166-2) for a list of assigne
|
||||
| WI | Wisconsin |
|
||||
| WY | Wyoming |
|
||||
|
||||
### Canada
|
||||
|
||||
| code | subdivision |
|
||||
|---|---|
|
||||
| AB | Alberta |
|
||||
| BC | British Columbia |
|
||||
| MB | Manitoba |
|
||||
| NB | New Brunswick |
|
||||
| NS | Nova Scotia |
|
||||
| ON | Ontario |
|
||||
| QC | Quebec |
|
||||
| SK | Saskatchewan |
|
||||
|
||||
Please add to it!
|
||||
|
||||
+7
-13
@@ -118,15 +118,10 @@ const std::string& SubdivisionTargeting::GetLazySubdivisionCode() const {
|
||||
return *subdivision_code_;
|
||||
}
|
||||
|
||||
bool SubdivisionTargeting::IsSupportedLocale(const std::string& locale) const {
|
||||
const std::string country_code = brave_l10n::GetISOCountryCode(locale);
|
||||
|
||||
const auto iter = kSupportedSubdivisionCodes.find(country_code);
|
||||
return iter != kSupportedSubdivisionCodes.cend();
|
||||
}
|
||||
|
||||
void SubdivisionTargeting::MaybeAllowForLocale(const std::string& locale) {
|
||||
if (!IsSupportedLocale(locale)) {
|
||||
const std::string country_code = brave_l10n::GetISOCountryCode(locale);
|
||||
if (!::ads::locale::IsSupportedCountryCodeForSubdivisionTargeting(
|
||||
country_code)) {
|
||||
AdsClientHelper::GetInstance()->SetBooleanPref(
|
||||
prefs::kShouldAllowSubdivisionTargeting, false);
|
||||
return;
|
||||
@@ -138,7 +133,6 @@ void SubdivisionTargeting::MaybeAllowForLocale(const std::string& locale) {
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string country_code = brave_l10n::GetISOCountryCode(locale);
|
||||
const std::string& subdivision_code = GetSubdivisionCode();
|
||||
|
||||
std::string subdivision_country_code;
|
||||
@@ -152,9 +146,7 @@ void SubdivisionTargeting::MaybeAllowForLocale(const std::string& locale) {
|
||||
return;
|
||||
}
|
||||
|
||||
const SupportedSubdivisionCodesSet& subdivision_codes =
|
||||
kSupportedSubdivisionCodes.at(country_code);
|
||||
if (subdivision_codes.find(subdivision_code) == subdivision_codes.cend()) {
|
||||
if (!IsSupportedSubdivisionCode(country_code, subdivision_code)) {
|
||||
BLOG(1, "Unknown subdivision code " << subdivision_code << " for " << locale
|
||||
<< " locale ");
|
||||
MaybeResetSubdivisionCodeToDisabled();
|
||||
@@ -193,7 +185,9 @@ void SubdivisionTargeting::MaybeFetchForLocale(const std::string& locale) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!IsSupportedLocale(locale)) {
|
||||
const std::string country_code = brave_l10n::GetISOCountryCode(locale);
|
||||
if (!::ads::locale::IsSupportedCountryCodeForSubdivisionTargeting(
|
||||
country_code)) {
|
||||
BLOG(1, "Ads subdivision targeting is not supported for " << locale
|
||||
<< " locale");
|
||||
|
||||
|
||||
-1
@@ -50,7 +50,6 @@ class SubdivisionTargeting final : public LocaleManagerObserver,
|
||||
const std::string& GetLazyAutoDetectedSubdivisionCode() const;
|
||||
const std::string& GetLazySubdivisionCode() const;
|
||||
|
||||
bool IsSupportedLocale(const std::string& locale) const;
|
||||
void MaybeAllowForLocale(const std::string& locale);
|
||||
void MaybeResetSubdivisionCodeToAutoDetect();
|
||||
void MaybeResetSubdivisionCodeToDisabled();
|
||||
|
||||
Vendored
+71
@@ -0,0 +1,71 @@
|
||||
/* Copyright (c) 2022 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/. */
|
||||
|
||||
#include "bat/ads/internal/geographic/subdivision/supported_subdivision_codes.h"
|
||||
|
||||
#include "base/no_destructor.h"
|
||||
#include "base/strings/string_piece.h"
|
||||
#include "brave/components/l10n/common/locale_util.h"
|
||||
|
||||
namespace ads::geographic {
|
||||
|
||||
const SupportedSubdivisionCodesMap& GetSupportedSubdivisionCodes() {
|
||||
// Format: { ISO Country Code, { ISO country subdivision codes as defined
|
||||
// in ISO 3166-2 https://en.wikipedia.org/wiki/ISO_3166-2, ... } }
|
||||
static const base::NoDestructor<SupportedSubdivisionCodesMap>
|
||||
supported_subdivision_codes(
|
||||
{{"US",
|
||||
{// United States of America
|
||||
{"US-AL", "Alabama"}, {"US-AK", "Alaska"},
|
||||
{"US-AZ", "Arizona"}, {"US-AR", "Arkansas"},
|
||||
{"US-CA", "California"}, {"US-CO", "Colorado"},
|
||||
{"US-CT", "Connecticut"}, {"US-DE", "Delaware"},
|
||||
{"US-FL", "Florida"}, {"US-GA", "Georgia"},
|
||||
{"US-HI", "Hawaii"}, {"US-ID", "Idaho"},
|
||||
{"US-IL", "Illinois"}, {"US-IN", "Indiana"},
|
||||
{"US-IA", "Iowa"}, {"US-KS", "Kansas"},
|
||||
{"US-KY", "Kentucky"}, {"US-LA", "Louisiana"},
|
||||
{"US-ME", "Maine"}, {"US-MD", "Maryland"},
|
||||
{"US-MA", "Massachusetts"}, {"US-MI", "Michigan"},
|
||||
{"US-MN", "Minnesota"}, {"US-MS", "Mississippi"},
|
||||
{"US-MO", "Missouri"}, {"US-MT", "Montana"},
|
||||
{"US-NE", "Nebraska"}, {"US-NV", "Nevada"},
|
||||
{"US-NH", "New Hampshire"}, {"US-NJ", "New Jersey"},
|
||||
{"US-NM", "New Mexico"}, {"US-NY", "New York"},
|
||||
{"US-NC", "North Carolina"}, {"US-ND", "North Dakota"},
|
||||
{"US-OH", "Ohio"}, {"US-OK", "Oklahoma"},
|
||||
{"US-OR", "Oregon"}, {"US-PA", "Pennsylvania"},
|
||||
{"US-RI", "Rhode Island"}, {"US-SC", "South Carolina"},
|
||||
{"US-SD", "South Dakota"}, {"US-TN", "Tennessee"},
|
||||
{"US-TX", "Texas"}, {"US-UT", "Utah"},
|
||||
{"US-VT", "Vermont"}, {"US-VA", "Virginia"},
|
||||
{"US-WA", "Washington"}, {"US-WV", "West Virginia"},
|
||||
{"US-WI", "Wisconsin"}, {"US-WY", "Wyoming"}}},
|
||||
{"CA",
|
||||
{// Canada
|
||||
{"CA-AB", "Alberta"},
|
||||
{"CA-BC", "British Columbia"},
|
||||
{"CA-MB", "Manitoba"},
|
||||
{"CA-NB", "New Brunswick"},
|
||||
{"CA-NS", "Nova Scotia"},
|
||||
{"CA-ON", "Ontario"},
|
||||
{"CA-QC", "Quebec"},
|
||||
{"CA-SK", "Saskatchewan"}}}});
|
||||
|
||||
return *supported_subdivision_codes;
|
||||
}
|
||||
|
||||
bool IsSupportedSubdivisionCode(const std::string& country_code,
|
||||
const std::string& subdivision_code) {
|
||||
const auto iter = GetSupportedSubdivisionCodes().find(country_code);
|
||||
if (iter == GetSupportedSubdivisionCodes().cend()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto& subdivisions = iter->second;
|
||||
return subdivisions.find(subdivision_code) != subdivisions.cend();
|
||||
}
|
||||
|
||||
} // namespace ads::geographic
|
||||
Vendored
+8
-73
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
|
||||
/* Copyright (c) 2022 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/. */
|
||||
@@ -9,83 +9,18 @@
|
||||
#include <string>
|
||||
|
||||
#include "base/containers/flat_map.h"
|
||||
#include "base/containers/flat_set.h"
|
||||
#include "base/strings/string_piece_forward.h"
|
||||
#include "bat/ads/supported_subdivisions.h"
|
||||
|
||||
namespace ads::geographic {
|
||||
|
||||
using SupportedSubdivisionCodesSet = base::flat_set<std::string>;
|
||||
using SupportedSubdivisionCodesMap =
|
||||
base::flat_map<std::string, SupportedSubdivisionCodesSet>;
|
||||
base::flat_map<base::StringPiece, SupportedSubdivisions>;
|
||||
|
||||
const SupportedSubdivisionCodesMap kSupportedSubdivisionCodes = {
|
||||
// Format: { ISO Country Code, { ISO country subdivision codes as defined
|
||||
// in ISO 3166-2 https://en.wikipedia.org/wiki/ISO_3166-2, ... } }
|
||||
{"US",
|
||||
{
|
||||
// United States of America
|
||||
"US-AL", // Alabama
|
||||
"US-AK", // Alaska
|
||||
"US-AZ", // Arizona
|
||||
"US-AR", // Arkansas
|
||||
"US-CA", // California
|
||||
"US-CO", // Colorado
|
||||
"US-CT", // Connecticut
|
||||
"US-DE", // Delaware
|
||||
"US-FL", // Florida
|
||||
"US-GA", // Georgia
|
||||
"US-HI", // Hawaii
|
||||
"US-ID", // Idaho
|
||||
"US-IL", // Illinois
|
||||
"US-IN", // Indiana
|
||||
"US-IA", // Iowa
|
||||
"US-KS", // Kansas
|
||||
"US-KY", // Kentucky
|
||||
"US-LA", // Louisiana
|
||||
"US-ME", // Maine
|
||||
"US-MD", // Maryland
|
||||
"US-MA", // Massachusetts
|
||||
"US-MI", // Michigan
|
||||
"US-MN", // Minnesota
|
||||
"US-MS", // Mississippi
|
||||
"US-MO", // Missouri
|
||||
"US-MT", // Montana
|
||||
"US-NE", // Nebraska
|
||||
"US-NV", // Nevada
|
||||
"US-NH", // New Hampshire
|
||||
"US-NJ", // New Jersey
|
||||
"US-NM", // New Mexico
|
||||
"US-NY", // New York
|
||||
"US-NC", // North Carolina
|
||||
"US-ND", // North Dakota
|
||||
"US-OH", // Ohio
|
||||
"US-OK", // Oklahoma
|
||||
"US-OR", // Oregon
|
||||
"US-PA", // Pennsylvania
|
||||
"US-RI", // Rhode Island
|
||||
"US-SC", // South Carolina
|
||||
"US-SD", // South Dakota
|
||||
"US-TN", // Tennessee
|
||||
"US-TX", // Texas
|
||||
"US-UT", // Utah
|
||||
"US-VT", // Vermont
|
||||
"US-VA", // Virginia
|
||||
"US-WA", // Washington
|
||||
"US-WV", // West Virginia
|
||||
"US-WI", // Wisconsin
|
||||
"US-WY", // Wyoming
|
||||
}},
|
||||
{"CA",
|
||||
{
|
||||
// Canada
|
||||
"CA-AB", // Alberta
|
||||
"CA-BC", // British Columbia
|
||||
"CA-MB", // Manitoba
|
||||
"CA-NB", // New Brunswick
|
||||
"CA-NS", // Nova Scotia
|
||||
"CA-ON", // Ontario
|
||||
"CA-QC", // Quebec
|
||||
"CA-SK", // Saskatchewan
|
||||
}}};
|
||||
const SupportedSubdivisionCodesMap& GetSupportedSubdivisionCodes();
|
||||
|
||||
bool IsSupportedSubdivisionCode(const std::string& country_code,
|
||||
const std::string& subdivision_code);
|
||||
|
||||
} // namespace ads::geographic
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/* Copyright (c) 2022 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/. */
|
||||
|
||||
#include "bat/ads/supported_subdivisions.h"
|
||||
|
||||
#include "base/strings/string_piece.h"
|
||||
#include "bat/ads/internal/geographic/subdivision/supported_subdivision_codes.h"
|
||||
#include "brave/components/l10n/common/locale_util.h"
|
||||
|
||||
namespace ads {
|
||||
|
||||
SupportedSubdivisions GetSupportedSubdivisions() {
|
||||
const std::string country_code = brave_l10n::GetDefaultISOCountryCodeString();
|
||||
|
||||
const auto& subdivision_codes = geographic::GetSupportedSubdivisionCodes();
|
||||
const auto iter = subdivision_codes.find(country_code);
|
||||
if (iter == subdivision_codes.cend()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
} // namespace ads
|
||||
Reference in New Issue
Block a user