Skip to content

Commit

Permalink
Merge pull request #11216 from brave/rewards-fix-brave-settings-state…
Browse files Browse the repository at this point in the history
…-level-targeting

Fix state-level ad targeting shown for non-US region
  • Loading branch information
emerick committed Nov 29, 2021
1 parent cb7d933 commit cad23a3
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 10 deletions.
50 changes: 50 additions & 0 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ using brave_ads::AdsServiceFactory;
using brave_rewards::RewardsService;
using brave_rewards::RewardsServiceFactory;

namespace {

const char kShouldAllowAdsSubdivisionTargeting[] =
"shouldAllowAdsSubdivisionTargeting";
const char kAdsSubdivisionTargeting[] = "adsSubdivisionTargeting";
const char kAutoDetectedAdsSubdivisionTargeting[] =
"automaticallyDetectedAdsSubdivisionTargeting";

} // namespace

namespace extensions {
namespace api {

Expand Down Expand Up @@ -1179,6 +1189,46 @@ BraveRewardsGetAdsSupportedFunction::Run() {
return RespondNow(OneArgument(base::Value(supported)));
}

BraveRewardsGetAdsDataFunction::~BraveRewardsGetAdsDataFunction() {}

ExtensionFunction::ResponseAction BraveRewardsGetAdsDataFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
AdsService* ads_service = AdsServiceFactory::GetForProfile(profile);

if (!ads_service) {
return RespondNow(Error("Ads service is not initialized"));
}

base::DictionaryValue ads_data;

auto is_supported_locale = ads_service->IsSupportedLocale();
ads_data.SetBoolean("adsIsSupported", is_supported_locale);

auto is_enabled = ads_service->IsEnabled();
ads_data.SetBoolean("adsEnabled", is_enabled);

auto ads_per_hour = ads_service->GetAdsPerHour();
ads_data.SetInteger("adsPerHour", ads_per_hour);

const std::string subdivision_targeting_code =
ads_service->GetAdsSubdivisionTargetingCode();
ads_data.SetString(kAdsSubdivisionTargeting, subdivision_targeting_code);

const std::string auto_detected_subdivision_targeting_code =
ads_service->GetAutoDetectedAdsSubdivisionTargetingCode();
ads_data.SetString(kAutoDetectedAdsSubdivisionTargeting,
auto_detected_subdivision_targeting_code);

const bool should_allow_subdivision_ad_targeting =
ads_service->ShouldAllowAdsSubdivisionTargeting();
ads_data.SetBoolean(kShouldAllowAdsSubdivisionTargeting,
should_allow_subdivision_ad_targeting);

ads_data.SetBoolean("adsUIEnabled", true);

return RespondNow(OneArgument(std::move(ads_data)));
}

BraveRewardsGetAnonWalletStatusFunction::
~BraveRewardsGetAnonWalletStatusFunction() = default;

Expand Down
10 changes: 10 additions & 0 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,16 @@ class BraveRewardsGetAdsSupportedFunction : public ExtensionFunction {
ResponseAction Run() override;
};

class BraveRewardsGetAdsDataFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getAdsData", UNKNOWN)

protected:
~BraveRewardsGetAdsDataFunction() override;

ResponseAction Run() override;
};

class BraveRewardsGetAnonWalletStatusFunction
: public ExtensionFunction {
public:
Expand Down
2 changes: 2 additions & 0 deletions browser/extensions/api/settings_private/brave_prefs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetAllowlistedKeys() {
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_allowlist)[ads::prefs::kAdsPerHour] =
settings_api::PrefType::PREF_TYPE_NUMBER;
(*s_brave_allowlist)[ads::prefs::kShouldAllowAdsSubdivisionTargeting] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_allowlist)[ads::prefs::kAdsSubdivisionTargetingCode] =
settings_api::PrefType::PREF_TYPE_STRING;
(*s_brave_allowlist)[brave_rewards::prefs::kEnabled] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {addSingletonGetter} from 'chrome://resources/js/cr.m.js';

/** @interface */
export class BraveRewardsBrowserProxy {
getLocale() { /* Intentionally empty */ }
getAdsData() { /* Intentionally empty */ }
getRewardsEnabled() { /* Intentionally empty */ }
getRewardsParameters() { /* Intentionally empty */ }
isAutoContributeSupported() { /* Intentionally empty */ }
Expand All @@ -18,9 +18,9 @@ export class BraveRewardsBrowserProxy {
*/
export class BraveRewardsBrowserProxyImpl {
/** @override */
getLocale () {
return new Promise((resolve) => chrome.braveRewards.getLocale(
(locale) => { resolve(locale) }))
getAdsData () {
return new Promise((resolve) => chrome.braveRewards.getAdsData(
(data) => { resolve(data) }))
}
/** @override */
getRewardsEnabled () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@
disabled="[[!prefs.brave.brave_ads.enabled.value]]">
</settings-dropdown-menu>
</div>
<div class="settings-box continuation">
<p>$i18nRaw{braveRewardsStateLevelAdTargetingDescLabel}</p>
</div>
</template>
<div class="settings-box continuation">
<p>$i18nRaw{braveRewardsStateLevelAdTargetingDescLabel}</p>
</div>
<template is="dom-if" if="[[shouldShowAutoContributeSettings_]]">
<div class="settings-box space-between">
<div class="label rewards-primary-title">$i18n{braveRewardsAutoContributeTitle}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@ class SettingsBraveRewardsPage extends SettingsBraveRewardsPageBase {
chrome.braveRewards.openBrowserActionUI('brave_rewards_panel.html')
this.isAutoContributeSupported_()
}
this.browserProxy_.getLocale().then((locale) => {
this.shouldAllowAdsSubdivisionTargeting_ = locale === 'en-US'
})
this.browserProxy_.getRewardsEnabled().then((enabled) => {
if (enabled) {
this.onRewardsEnabled_()
Expand All @@ -182,6 +179,13 @@ class SettingsBraveRewardsPage extends SettingsBraveRewardsPageBase {
this.onRewardsEnabled_()
}
})
chrome.settingsPrivate.onPrefsChanged.addListener((prefs) => {
prefs.forEach((pref) => {
if (pref.key === 'brave.brave_ads.should_allow_ads_subdivision_targeting') {
this.getAdsDataForSubdivisionTargeting_()
}
}, this)
})
}

isAutoContributeSupported_() {
Expand All @@ -198,6 +202,13 @@ class SettingsBraveRewardsPage extends SettingsBraveRewardsPageBase {
this.wasInlineTippingForGithubEnabledOnStartup_ = this.getPref('brave.rewards.inline_tip.github').value
this.isAutoContributeSupported_()
this.populateAutoContributeAmountDropdown_()
this.getAdsDataForSubdivisionTargeting_()
}

getAdsDataForSubdivisionTargeting_() {
this.browserProxy_.getAdsData().then((adsData) => {
this.shouldAllowAdsSubdivisionTargeting_ = adsData.shouldAllowAdsSubdivisionTargeting
})
}

populateAutoContributeAmountDropdown_() {
Expand Down
40 changes: 40 additions & 0 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,46 @@
}
]
},
{
"name": "getAdsData",
"type": "function",
"description": "Gets whether ads is supported in the user's region",
"parameters": [
{
"type": "function",
"name": "callback",
"parameters": [
{
"name": "adsData",
"type": "object",
"properties": {
"adsIsSupported": {
"type": "boolean"
},
"adsEnabled": {
"type": "boolean"
},
"adsPerHour": {
"type": "number"
},
"adsSubdivisionTargeting": {
"type": "string"
},
"automaticallyDetectedAdsSubdivisionTargeting": {
"type": "string"
},
"shouldAllowAdsSubdivisionTargeting": {
"type": "boolean"
},
"adsUIEnabled": {
"type": "boolean"
}
}
}
]
}
]
},
{
"name": "getACEnabled",
"type": "function",
Expand Down

0 comments on commit cad23a3

Please sign in to comment.