Skip to content

Commit

Permalink
Fixes ads enabled by default on upgrade for new ad regions
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey committed Jan 19, 2020
1 parent 737e7b8 commit 632f17b
Show file tree
Hide file tree
Showing 9 changed files with 1,393 additions and 25 deletions.
4 changes: 3 additions & 1 deletion browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,9 @@ ExtensionFunction::ResponseAction BraveRewardsSaveAdsSettingFunction::Run() {
AdsService* ads_service_ = AdsServiceFactory::GetForProfile(profile);
if (ads_service_) {
if (params->key == "adsEnabled") {
ads_service_->SetEnabled(params->value == "true");
const auto is_enabled =
params->value == "true" && ads_service_->IsSupportedLocale();
ads_service_->SetEnabled(is_enabled);
} else if (params->key == "shouldAllowAdConversionTracking") {
ads_service_->SetAllowAdConversionTracking(params->value == "true");
}
Expand Down
69 changes: 68 additions & 1 deletion components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,8 @@ bool AdsServiceImpl::MigratePrefs(
{{2, 3}, &AdsServiceImpl::MigratePrefsVersion2To3},
{{3, 4}, &AdsServiceImpl::MigratePrefsVersion3To4},
{{4, 5}, &AdsServiceImpl::MigratePrefsVersion4To5},
{{5, 6}, &AdsServiceImpl::MigratePrefsVersion5To6}
{{5, 6}, &AdsServiceImpl::MigratePrefsVersion5To6},
{{6, 7}, &AdsServiceImpl::MigratePrefsVersion6To7}
};

// Cycle through migration paths, i.e. if upgrading from version 2 to 5 we
Expand Down Expand Up @@ -1422,6 +1423,72 @@ void AdsServiceImpl::MigratePrefsVersion5To6() {
SetUint64Pref(prefs::kAdsPerDay, 20);
}

void AdsServiceImpl::MigratePrefsVersion6To7() {
// Disable ads for newly supported regions due to a bug where ads were enabled
// even if the users region was not supported

const auto locale = GetLocale();
const auto region = ads::Ads::GetRegion(locale);

const std::vector<std::string> legacy_regions = {
"US", // United States of America
"CA", // Canada
"GB", // United Kingdom (Great Britain and Northern Ireland)
"DE", // Germany
"FR", // France
"AU", // Australia
"NZ", // New Zealand
"IE", // Ireland
"AR", // Argentina
"AT", // Austria
"BR", // Brazil
"CH", // Switzerland
"CL", // Chile
"CO", // Colombia
"DK", // Denmark
"EC", // Ecuador
"IL", // Israel
"IN", // India
"IT", // Italy
"JP", // Japan
"KR", // Korea
"MX", // Mexico
"NL", // Netherlands
"PE", // Peru
"PH", // Philippines
"PL", // Poland
"SE", // Sweden
"SG", // Singapore
"VE", // Venezuela
"ZA", // South Africa
"KY" // Cayman Islands
};

const bool is_a_legacy_region = std::find(legacy_regions.begin(),
legacy_regions.end(), region) != legacy_regions.end();

if (is_a_legacy_region) {
// Do not disable Brave Ads for legacy regions introduced before version
// 1.3.x
return;
}

const int last_schema_version =
GetIntegerPref(prefs::kSupportedRegionsLastSchemaVersion);

if (last_schema_version >= 4) {
// Do not disable Brave Ads if |prefs::kSupportedRegionsLastSchemaVersion|
// is newer than or equal to schema version 4. This can occur if a user is
// upgrading from an older version of 1.3.x or above
return;
}

SetEnabled(false);

SetBooleanPref(prefs::kShouldShowOnboarding, true);
SetUint64Pref(prefs::kOnboardingTimestamp, 0);
}

int AdsServiceImpl::GetPrefsVersion() const {
return GetIntegerPref(prefs::kVersion);
}
Expand Down
1 change: 1 addition & 0 deletions components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ class AdsServiceImpl : public AdsService,
void MigratePrefsVersion3To4();
void MigratePrefsVersion4To5();
void MigratePrefsVersion5To6();
void MigratePrefsVersion6To7();
int GetPrefsVersion() const;

bool IsUpgradingFromPreBraveAdsBuild();
Expand Down
2 changes: 1 addition & 1 deletion components/brave_ads/common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const int kSupportedRegionsSchemaVersionNumber = 5;
// Stores the preferences version number
const char kVersion[] = "brave.brave_ads.prefs.version";

const int kCurrentVersionNumber = 6;
const int kCurrentVersionNumber = 7;

} // namespace prefs

Expand Down
Loading

0 comments on commit 632f17b

Please sign in to comment.