Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes ads enabled by default on upgrade of 1.2 to 1.3 for new ad regions #4392

Merged
merged 1 commit into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Collaborator Author

@tmancey tmancey Jan 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug was caused by this line, due to a legacy issue where the ads flag was enabled even if the user's locale was unsupported

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