Skip to content

Commit

Permalink
Merge pull request #5602 from /issues/9200
Browse files Browse the repository at this point in the history
Implement state level ads delivery
  • Loading branch information
tmancey authored Jun 11, 2020
2 parents 3a7ba2e + 2a13e5e commit ed2e0e6
Show file tree
Hide file tree
Showing 43 changed files with 1,335 additions and 23 deletions.
2 changes: 2 additions & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ source_set("ui") {
"//brave/components/brave_wallet/browser/buildflags:buildflags",
"//brave/components/brave_wayback_machine:buildflags",
"//brave/components/brave_welcome_ui:generated_resources",
"//brave/components/l10n/browser",
"//brave/components/l10n/common",
"//brave/components/ntp_background_images/browser",
"//brave/components/ntp_background_images/common",
"//brave/components/p3a:buildflags",
Expand Down
32 changes: 30 additions & 2 deletions browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "brave/browser/brave_rewards/rewards_service_factory.h"
#include "brave/components/brave_rewards/browser/rewards_service_observer.h"
#include "brave/components/brave_rewards/browser/rewards_parameters.h"
#include "brave/components/l10n/browser/locale_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
Expand Down Expand Up @@ -307,6 +308,12 @@ namespace {

const int kDaysOfAdsHistory = 7;

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

} // namespace

RewardsDOMHandler::RewardsDOMHandler() : weak_factory_(this) {}
Expand Down Expand Up @@ -1107,6 +1114,20 @@ void RewardsDOMHandler::GetAdsData(const base::ListValue *args) {
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 automatically_detected_subdivision_targeting_code =
ads_service_->GetAutomaticallyDetectedAdsSubdivisionTargetingCode();
ads_data.SetString(kAutomaticallyDetectedAdsSubdivisionTargeting,
automatically_detected_subdivision_targeting_code);

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

#if BUILDFLAG(BRAVE_ADS_ENABLED)
auto ads_ui_enabled = true;
#else
Expand Down Expand Up @@ -1326,6 +1347,10 @@ void RewardsDOMHandler::SaveAdsSetting(const base::ListValue* args) {
ads_service_->SetEnabled(is_enabled);
} else if (key == "adsPerHour") {
ads_service_->SetAdsPerHour(std::stoull(value));
} else if (key == kAdsSubdivisionTargeting) {
ads_service_->SetAdsSubdivisionTargetingCode(value);
} else if (key == kAutomaticallyDetectedAdsSubdivisionTargeting) {
ads_service_->SetAutomaticallyDetectedAdsSubdivisionTargetingCode(value);
}

base::ListValue* emptyArgs = nullptr;
Expand Down Expand Up @@ -1868,11 +1893,14 @@ void RewardsDOMHandler::GetAllMonthlyReportIds(const base::ListValue* args) {
}

void RewardsDOMHandler::GetCountryCode(const base::ListValue* args) {
if (!ads_service_ || !web_ui()->CanCallJavascript()) {
if (!web_ui()->CanCallJavascript()) {
return;
}

const std::string country_code = ads_service_->GetCountryCode();
const std::string locale =
brave_l10n::LocaleHelper::GetInstance()->GetLocale();
const std::string country_code =
brave_l10n::LocaleHelper::GetInstance()->GetCountryCode(locale);

web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.countryCode", base::Value(country_code));
Expand Down
7 changes: 7 additions & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,13 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "adsPerHour3", IDS_BRAVE_REWARDS_LOCAL_ADS_PER_HOUR_3 },
{ "adsPerHour4", IDS_BRAVE_REWARDS_LOCAL_ADS_PER_HOUR_4 },
{ "adsPerHour5", IDS_BRAVE_REWARDS_LOCAL_ADS_PER_HOUR_5 },
{ "adsSubdivisionTargetingTitle", IDS_BRAVE_REWARDS_LOCAL_ADS_SUBDIVISION_TARGETING_TITLE }, // NOLINT
{ "adsSubdivisionTargetingDescription", IDS_BRAVE_REWARDS_LOCAL_ADS_SUBDIVISION_TARGETING_DESCRIPTION }, // NOLINT
{ "adsSubdivisionTargetingLearn", IDS_BRAVE_REWARDS_LOCAL_ADS_SUBDIVISION_TARGETING_LEARN }, // NOLINT
{ "adsSubdivisionTargetingAutomaticallyDetectedAs", IDS_BRAVE_REWARDS_LOCAL_ADS_SUBDIVISION_TARGETING_AUTOMATICALLY_DETECTED_AS }, // NOLINT
{ "adsSubdivisionTargetingAutomaticallyDetect", IDS_BRAVE_REWARDS_LOCAL_ADS_SUBDIVISION_TARGETING_AUTOMATICALLY_DETECT }, // NOLINT
{ "adsSubdivisionTargetingDisable", IDS_BRAVE_REWARDS_LOCAL_ADS_SUBDIVISION_TARGETING_DISABLE }, // NOLINT
{ "adsSubdivisionTargetingDisabled", IDS_BRAVE_REWARDS_LOCAL_ADS_SUBDIVISION_TARGETING_DISABLED }, // NOLINT
{ "adsTitle", IDS_BRAVE_REWARDS_LOCAL_ADS_TITLE },

{ "bat", IDS_BRAVE_UI_BAT_REWARDS_TEXT },
Expand Down
14 changes: 13 additions & 1 deletion components/brave_ads/browser/ads_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class AdsService : public KeyedService {
virtual bool IsEnabled() const = 0;
virtual void SetEnabled(
const bool is_enabled) = 0;
virtual const std::string GetCountryCode() const = 0;

virtual bool ShouldAllowAdConversionTracking() const = 0;
virtual void SetAllowAdConversionTracking(
Expand All @@ -65,6 +64,19 @@ class AdsService : public KeyedService {
virtual void SetAdsPerHour(
const uint64_t ads_per_hour) = 0;

virtual bool ShouldAllowAdsSubdivisionTargeting() const = 0;
virtual void SetAllowAdsSubdivisionTargeting(
const bool should_allow) = 0;

virtual std::string GetAdsSubdivisionTargetingCode() const = 0;
virtual void SetAdsSubdivisionTargetingCode(
const std::string& subdivision_targeting_code) = 0;

virtual std::string
GetAutomaticallyDetectedAdsSubdivisionTargetingCode() const = 0;
virtual void SetAutomaticallyDetectedAdsSubdivisionTargetingCode(
const std::string& subdivision_targeting_code) = 0;

virtual void SetConfirmationsIsReady(
const bool is_ready) = 0;

Expand Down
6 changes: 6 additions & 0 deletions components/brave_ads/browser/ads_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ void AdsServiceFactory::RegisterProfilePrefs(
registry->RegisterUint64Pref(prefs::kAdsPerHour, 2);
registry->RegisterUint64Pref(prefs::kAdsPerDay, 20);

registry->RegisterBooleanPref(prefs::kShouldAllowAdsSubdivisionTargeting,
false);
registry->RegisterStringPref(prefs::kAdsSubdivisionTargetingCode, "AUTO");
registry->RegisterStringPref(
prefs::kAutomaticallyDetectedAdsSubdivisionTargetingCode, "");

registry->RegisterIntegerPref(prefs::kIdleThreshold, 15);
registry->RegisterBooleanPref(prefs::kAdsWereDisabled, false);
registry->RegisterBooleanPref(prefs::kHasAdsP3AState, false);
Expand Down
50 changes: 43 additions & 7 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,35 @@ void AdsServiceImpl::SetAdsPerHour(
SetUint64Pref(prefs::kAdsPerHour, ads_per_hour);
}

void AdsServiceImpl::SetAllowAdsSubdivisionTargeting(
const bool should_allow) {
SetBooleanPref(prefs::kShouldAllowAdsSubdivisionTargeting, should_allow);
}

void AdsServiceImpl::SetAdsSubdivisionTargetingCode(
const std::string& subdivision_targeting_code) {
const auto last_subdivision_targeting_code = GetAdsSubdivisionTargetingCode();

SetStringPref(prefs::kAdsSubdivisionTargetingCode,
subdivision_targeting_code);

if (last_subdivision_targeting_code == subdivision_targeting_code) {
return;
}

if (!connected()) {
return;
}

bat_ads_->OnAdsSubdivisionTargetingCodeHasChanged();
}

void AdsServiceImpl::SetAutomaticallyDetectedAdsSubdivisionTargetingCode(
const std::string& subdivision_targeting_code) {
SetStringPref(prefs::kAutomaticallyDetectedAdsSubdivisionTargetingCode,
subdivision_targeting_code);
}

void AdsServiceImpl::SetConfirmationsIsReady(
const bool is_ready) {
if (!connected()) {
Expand Down Expand Up @@ -493,6 +522,20 @@ uint64_t AdsServiceImpl::GetAdsPerDay() const {
return GetUint64Pref(prefs::kAdsPerDay);
}

bool AdsServiceImpl::ShouldAllowAdsSubdivisionTargeting() const {
return GetBooleanPref(prefs::kShouldAllowAdsSubdivisionTargeting);
}

std::string AdsServiceImpl::GetAdsSubdivisionTargetingCode() const {
return GetStringPref(prefs::kAdsSubdivisionTargetingCode);
}

std::string AdsServiceImpl::
GetAutomaticallyDetectedAdsSubdivisionTargetingCode() const {
return GetStringPref(
prefs::kAutomaticallyDetectedAdsSubdivisionTargetingCode);
}

///////////////////////////////////////////////////////////////////////////////

void AdsServiceImpl::Shutdown() {
Expand Down Expand Up @@ -1813,13 +1856,6 @@ std::string AdsServiceImpl::GetLocale() const {
return brave_l10n::LocaleHelper::GetInstance()->GetLocale();
}

const std::string AdsServiceImpl::GetCountryCode() const {
const std::string locale = GetLocale();
const std::string country_code =
brave_l10n::LocaleHelper::GetInstance()->GetCountryCode(locale);
return country_code;
}

bool AdsServiceImpl::IsNetworkConnectionAvailable() const {
return !net::NetworkChangeNotifier::IsOffline();
}
Expand Down
15 changes: 13 additions & 2 deletions components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,24 @@ class AdsServiceImpl : public AdsService,
// AdsClient implementation
bool IsEnabled() const override;

const std::string GetCountryCode() const override;

bool ShouldAllowAdConversionTracking() const override;

uint64_t GetAdsPerHour() const override;
uint64_t GetAdsPerDay() const override;

bool ShouldAllowAdsSubdivisionTargeting() const override;
void SetAllowAdsSubdivisionTargeting(
const bool should_allow) override;

std::string GetAdsSubdivisionTargetingCode() const override;
void SetAdsSubdivisionTargetingCode(
const std::string& subdivision_targeting_code) override;

std::string
GetAutomaticallyDetectedAdsSubdivisionTargetingCode() const override;
void SetAutomaticallyDetectedAdsSubdivisionTargetingCode(
const std::string& subdivision_targeting_code) override;

// KeyedService implementation
void Shutdown() override;

Expand Down
12 changes: 12 additions & 0 deletions components/brave_ads/common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ const char kHasAdsP3AState[] = "brave.brave_ads.has_p3a_state";
// Stores the maximum amount of ads per hour
const char kAdsPerHour[] = "brave.brave_ads.ads_per_hour";

// Stores whether Brave ads should allow subdivision ad targeting
const char kShouldAllowAdsSubdivisionTargeting[] =
"brave.brave_ads.should_allow_ads_subdivision_targeting";

// Stores the selected ads subdivision targeting code
const char kAdsSubdivisionTargetingCode[] =
"brave.brave_ads.ads_subdivision_targeting_code";

// Stores the automatically detected ads subdivision targeting code
const char kAutomaticallyDetectedAdsSubdivisionTargetingCode[] =
"brave.brave_ads.automatically_detected_ads_subdivision_targeting_code";

// Stores the maximum amount of ads per day
const char kAdsPerDay[] = "brave.brave_ads.ads_per_day";

Expand Down
5 changes: 5 additions & 0 deletions components/brave_ads/common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ extern const char kShouldAllowAdConversionTracking[];

extern const char kAdsPerHour[];
extern const char kAdsPerDay[];

extern const char kShouldAllowAdsSubdivisionTargeting[];
extern const char kAdsSubdivisionTargetingCode[];
extern const char kAutomaticallyDetectedAdsSubdivisionTargetingCode[];

extern const char kAdsWereDisabled[];
extern const char kHasAdsP3AState[];

Expand Down
Loading

0 comments on commit ed2e0e6

Please sign in to comment.