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

Fix Ads locale on Windows 0.63.x #2288

Merged
merged 5 commits into from
Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ ExtensionFunction::ResponseAction BraveRewardsSaveAdsSettingFunction::Run() {
AdsService* ads_service_ = AdsServiceFactory::GetForProfile(profile);
if (ads_service_) {
if (params->key == "adsEnabled") {
ads_service_->set_ads_enabled(params->value == "true");
ads_service_->SetAdsEnabled(params->value == "true");
}
}
return RespondNow(NoArguments());
Expand Down
81 changes: 33 additions & 48 deletions browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,8 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void GetConfirmationsHistory(const base::ListValue* args);
void GetRewardsMainEnabled(const base::ListValue* args);
void OnGetRewardsMainEnabled(bool enabled);
void OnAdsIsSupportedRegion(bool is_supported);

void GetExcludedPublishersNumber(const base::ListValue* args);
void AdsIsSupportedRegion(const base::ListValue* args);

void OnConfirmationsHistory(int total_viewed, double estimated_earnings);

Expand Down Expand Up @@ -285,9 +283,6 @@ void RewardsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("brave_rewards.getExcludedPublishersNumber",
base::BindRepeating(&RewardsDOMHandler::GetExcludedPublishersNumber,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.getAdsIsSupportedRegion",
base::BindRepeating(&RewardsDOMHandler::AdsIsSupportedRegion,
base::Unretained(this)));
}

void RewardsDOMHandler::Init() {
Expand Down Expand Up @@ -873,45 +868,50 @@ void RewardsDOMHandler::CheckImported(const base::ListValue *args) {
}

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

bool ads_ui_enabled;
bool ads_enabled = ads_service_->is_enabled();
int ads_per_hour = ads_service_->ads_per_hour();
base::DictionaryValue ads_data;

#if BUILDFLAG(BRAVE_ADS_ENABLED)
ads_ui_enabled = true;
#else
ads_ui_enabled = false;
#endif
auto is_supported_region = ads_service_->IsSupportedRegion();
ads_data.SetBoolean("adsIsSupported", is_supported_region);

adsData.SetBoolean("adsEnabled", ads_enabled);
adsData.SetInteger("adsPerHour", ads_per_hour);
adsData.SetBoolean("adsUIEnabled", ads_ui_enabled);
auto is_ads_enabled = ads_service_->IsAdsEnabled();
ads_data.SetBoolean("adsEnabled", is_ads_enabled);

web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.adsData", adsData);
}
auto ads_per_hour = ads_service_->GetAdsPerHour();
ads_data.SetInteger("adsPerHour", ads_per_hour);

#if BUILDFLAG(BRAVE_ADS_ENABLED)
auto ads_ui_enabled = true;
#else
auto ads_ui_enabled = false;
#endif
ads_data.SetBoolean("adsUIEnabled", ads_ui_enabled);

web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.adsData", ads_data);
}

void RewardsDOMHandler::SaveAdsSetting(const base::ListValue* args) {
if (ads_service_) {
std::string key;
std::string value;
args->GetString(0, &key);
args->GetString(1, &value);
if (!ads_service_) {
return;
}

if (key == "adsEnabled") {
ads_service_->set_ads_enabled(value == "true");
}
std::string key;
args->GetString(0, &key);

if (key == "adsPerHour") {
ads_service_->set_ads_per_hour(std::stoi(value));
}
std::string value;
args->GetString(1, &value);

base::ListValue* emptyArgs;
GetAdsData(emptyArgs);
if (key == "adsEnabled") {
ads_service_->SetAdsEnabled(value == "true");
} else if (key == "adsPerHour") {
ads_service_->SetAdsPerHour(std::stoull(value));
}

base::ListValue* emptyArgs = nullptr;
GetAdsData(emptyArgs);
}

void RewardsDOMHandler::SetBackupCompleted(const base::ListValue *args) {
Expand Down Expand Up @@ -1030,21 +1030,6 @@ void RewardsDOMHandler::GetExcludedPublishersNumber(
}
}

void RewardsDOMHandler::AdsIsSupportedRegion(
const base::ListValue* args) {
ads_service_->IsSupportedRegion(base::BindOnce(
&RewardsDOMHandler::OnAdsIsSupportedRegion,
weak_factory_.GetWeakPtr()));
}

void RewardsDOMHandler::OnAdsIsSupportedRegion(
bool is_supported) {
if (web_ui()->CanCallJavascript()) {
web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.adsIsSupportedRegion",
base::Value(is_supported));
}
}

} // namespace

BraveRewardsUI::BraveRewardsUI(content::WebUI* web_ui, const std::string& name)
Expand Down
8 changes: 8 additions & 0 deletions components/brave_ads/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ source_set("browser") {
"background_helper_mac.h",
"background_helper_win.cc",
"background_helper_win.h",
"locale_helper.cc",
"locale_helper.h",
"locale_helper_linux.cc",
"locale_helper_linux.h",
"locale_helper_mac.mm",
"locale_helper_mac.h",
"locale_helper_win.cc",
"locale_helper_win.h",
"bundle_state_database.cc",
"bundle_state_database.h",
]
Expand Down
17 changes: 9 additions & 8 deletions components/brave_ads/browser/ads_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ class AdsService : public KeyedService {
public:
AdsService() = default;

virtual bool is_enabled() const = 0;
virtual uint64_t ads_per_hour() const = 0;
virtual bool IsSupportedRegion() const = 0;

virtual void set_ads_enabled(bool enabled) = 0;
virtual void set_ads_per_hour(int ads_per_hour) = 0;
virtual bool IsAdsEnabled() const = 0;
virtual void SetAdsEnabled(const bool is_enabled) = 0;

virtual uint64_t GetAdsPerHour() const = 0;
virtual void SetAdsPerHour(const uint64_t ads_per_hour) = 0;

// ads::Ads proxy
virtual void TabUpdated(
Expand All @@ -36,11 +38,10 @@ class AdsService : public KeyedService {
virtual void TabClosed(SessionID tab_id) = 0;
virtual void OnMediaStart(SessionID tab_id) = 0;
virtual void OnMediaStop(SessionID tab_id) = 0;
virtual void ClassifyPage(const std::string& url,
const std::string& page) = 0;
virtual void ClassifyPage(
const std::string& url,
const std::string& page) = 0;
virtual void SetConfirmationsIsReady(const bool is_ready) = 0;
virtual void IsSupportedRegion(
IsSupportedRegionCallback callback) = 0;

private:
DISALLOW_COPY_AND_ASSIGN(AdsService);
Expand Down
20 changes: 20 additions & 0 deletions components/brave_ads/browser/ads_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ bool AdsServiceFactory::ServiceIsNULLWhileTesting() const {

void AdsServiceFactory::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
auto should_migrate_prefs_from_62 = ShouldMigratePrefsFrom62(registry);

if (ShouldMigratePrefs(registry)) {
// prefs::kBraveAdsPrefsVersion should default to 1 for legacy installations
// so that preferences are migrated from version 1 to the current version
Expand All @@ -101,6 +103,10 @@ void AdsServiceFactory::RegisterProfilePrefs(
#endif

registry->RegisterIntegerPref(prefs::kBraveAdsIdleThreshold, 15);

if (should_migrate_prefs_from_62) {
registry->RegisterBooleanPref(prefs::kBraveAdsPrefsMigratedFrom62, true);
}
}

bool AdsServiceFactory::ShouldMigratePrefs(
Expand All @@ -117,4 +123,18 @@ bool AdsServiceFactory::ShouldMigratePrefs(
return true;
}

bool AdsServiceFactory::ShouldMigratePrefsFrom62(
user_prefs::PrefRegistrySyncable* registry) const {
// prefs::kBraveAdsPrefsVersion has existed since 0.63.45 so if this key does
// not exist then this must be an upgrade from 0.62.x so we should migrate
auto pref_store = registry->defaults();

const base::Value* value = nullptr;
if (pref_store->GetValue(prefs::kBraveAdsPrefsVersion, &value)) {
return false;
}

return true;
}

} // namespace brave_ads
2 changes: 2 additions & 0 deletions components/brave_ads/browser/ads_service_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class AdsServiceFactory : public BrowserContextKeyedServiceFactory {
void RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) override;
bool ShouldMigratePrefs(user_prefs::PrefRegistrySyncable* registry) const;
bool ShouldMigratePrefsFrom62(
user_prefs::PrefRegistrySyncable* registry) const;

DISALLOW_COPY_AND_ASSIGN(AdsServiceFactory);
};
Expand Down
Loading