From 79facd3c23843f72d6ed42b856f5ba233e6fe60b Mon Sep 17 00:00:00 2001 From: Ryan Lanese Date: Wed, 17 Apr 2019 13:55:26 -0700 Subject: [PATCH 1/2] Merge pull request #2232 from brave/issues/3967 Fix Ads enabled for unsupported regions and enabled by default after 0.63.x update --- browser/extensions/api/brave_rewards_api.cc | 2 +- browser/ui/webui/brave_rewards_ui.cc | 81 +++++------ components/brave_ads/browser/BUILD.gn | 8 ++ components/brave_ads/browser/ads_service.h | 17 +-- .../brave_ads/browser/ads_service_factory.cc | 20 +++ .../brave_ads/browser/ads_service_factory.h | 2 + .../brave_ads/browser/ads_service_impl.cc | 127 +++++++++++------- .../brave_ads/browser/ads_service_impl.h | 98 ++++++++------ .../brave_ads/browser/ads_tab_helper.cc | 4 +- components/brave_ads/browser/locale_helper.cc | 25 ++++ components/brave_ads/browser/locale_helper.h | 38 ++++++ .../brave_ads/browser/locale_helper_linux.cc | 46 +++++++ .../brave_ads/browser/locale_helper_linux.h | 33 +++++ .../brave_ads/browser/locale_helper_mac.h | 33 +++++ .../brave_ads/browser/locale_helper_mac.mm | 29 ++++ .../brave_ads/browser/locale_helper_win.cc | 48 +++++++ .../brave_ads/browser/locale_helper_win.h | 34 +++++ components/brave_ads/common/pref_names.cc | 4 + components/brave_ads/common/pref_names.h | 3 + .../resources/ui/actions/rewards_actions.ts | 6 - .../resources/ui/brave_rewards.tsx | 7 +- .../resources/ui/components/settingsPage.tsx | 1 - .../resources/ui/constants/rewards_types.ts | 4 +- .../resources/ui/reducers/rewards_reducer.ts | 9 +- components/services/bat_ads/bat_ads_impl.cc | 11 +- components/services/bat_ads/bat_ads_impl.h | 2 - .../services/bat_ads/bat_ads_service_impl.cc | 25 +++- .../services/bat_ads/bat_ads_service_impl.h | 33 +++-- .../bat_ads/public/interfaces/bat_ads.mojom | 2 +- vendor/bat-native-ads/README.md | 11 +- vendor/bat-native-ads/include/bat/ads/ads.h | 7 +- vendor/bat-native-ads/src/bat/ads/ads.cc | 13 ++ .../src/bat/ads/internal/ads_impl.cc | 14 -- .../src/bat/ads/internal/ads_impl.h | 2 - 34 files changed, 572 insertions(+), 227 deletions(-) create mode 100644 components/brave_ads/browser/locale_helper.cc create mode 100644 components/brave_ads/browser/locale_helper.h create mode 100644 components/brave_ads/browser/locale_helper_linux.cc create mode 100644 components/brave_ads/browser/locale_helper_linux.h create mode 100644 components/brave_ads/browser/locale_helper_mac.h create mode 100644 components/brave_ads/browser/locale_helper_mac.mm create mode 100644 components/brave_ads/browser/locale_helper_win.cc create mode 100644 components/brave_ads/browser/locale_helper_win.h diff --git a/browser/extensions/api/brave_rewards_api.cc b/browser/extensions/api/brave_rewards_api.cc index e9742365b499..8065cbff9608 100644 --- a/browser/extensions/api/brave_rewards_api.cc +++ b/browser/extensions/api/brave_rewards_api.cc @@ -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()); diff --git a/browser/ui/webui/brave_rewards_ui.cc b/browser/ui/webui/brave_rewards_ui.cc index 9a024eaa6e14..0dabd38b115e 100644 --- a/browser/ui/webui/brave_rewards_ui.cc +++ b/browser/ui/webui/brave_rewards_ui.cc @@ -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); @@ -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() { @@ -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) { @@ -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) diff --git a/components/brave_ads/browser/BUILD.gn b/components/brave_ads/browser/BUILD.gn index 34dfd172d52c..1bcefe836603 100644 --- a/components/brave_ads/browser/BUILD.gn +++ b/components/brave_ads/browser/BUILD.gn @@ -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", ] diff --git a/components/brave_ads/browser/ads_service.h b/components/brave_ads/browser/ads_service.h index 76034079c450..1f79210b81e0 100644 --- a/components/brave_ads/browser/ads_service.h +++ b/components/brave_ads/browser/ads_service.h @@ -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( @@ -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); diff --git a/components/brave_ads/browser/ads_service_factory.cc b/components/brave_ads/browser/ads_service_factory.cc index 22e90f7fe5db..abd26e4bfe68 100644 --- a/components/brave_ads/browser/ads_service_factory.cc +++ b/components/brave_ads/browser/ads_service_factory.cc @@ -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 @@ -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( @@ -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 diff --git a/components/brave_ads/browser/ads_service_factory.h b/components/brave_ads/browser/ads_service_factory.h index e99a26b0b84b..902292e1d4a5 100644 --- a/components/brave_ads/browser/ads_service_factory.h +++ b/components/brave_ads/browser/ads_service_factory.h @@ -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); }; diff --git a/components/brave_ads/browser/ads_service_impl.cc b/components/brave_ads/browser/ads_service_impl.cc index 5553475550d5..2e27daff4b1d 100644 --- a/components/brave_ads/browser/ads_service_impl.cc +++ b/components/brave_ads/browser/ads_service_impl.cc @@ -23,6 +23,7 @@ #include "bat/ads/notification_result_type.h" #include "bat/ads/resources/grit/bat_ads_resources.h" #include "brave/components/brave_ads/browser/ad_notification.h" +#include "brave/components/brave_ads/browser/locale_helper.h" #include "brave/components/brave_ads/browser/bundle_state_database.h" #include "brave/components/brave_ads/common/pref_names.h" #include "brave/components/brave_rewards/common/pref_names.h" @@ -206,8 +207,7 @@ void PostWriteCallback( bool success) { // We can't run |callback| on the current thread. Bounce back to // the |reply_task_runner| which is the correct sequenced thread. - reply_task_runner->PostTask(FROM_HERE, - base::Bind(callback, success)); + reply_task_runner->PostTask(FROM_HERE, base::Bind(callback, success)); } std::string LoadOnFileTaskRunner( @@ -258,6 +258,7 @@ AdsServiceImpl::AdsServiceImpl(Profile* profile) base::TaskShutdownBehavior::BLOCK_SHUTDOWN})), base_path_(profile_->GetPath().AppendASCII("ads_service")), next_timer_id_(0), + is_supported_region_(false), bundle_state_backend_( new BundleStateDatabase(base_path_.AppendASCII("bundle_state"))), display_service_(NotificationDisplayService::GetForProfile(profile_)), @@ -313,12 +314,32 @@ void AdsServiceImpl::OnCreate() { } } -void AdsServiceImpl::MaybeStart(bool restart) { - if (restart) +void AdsServiceImpl::MaybeStart(bool should_restart) { + if (should_restart) Shutdown(); - if (is_enabled()) { - if (restart) { + if (!StartService()) { + LOG(ERROR) << "Failed to start Ads service"; + return; + } + + bat_ads_service_->IsSupportedRegion(GetAdsLocale(), + base::BindOnce(&AdsServiceImpl::OnMaybeStartForRegion, + AsWeakPtr(), should_restart)); +} + +void AdsServiceImpl::OnMaybeStartForRegion( + bool should_restart, + bool is_supported_region) { + is_supported_region_ = is_supported_region; + + if (!is_supported_region_) { + Shutdown(); + return; + } + + if (IsAdsEnabled()) { + if (should_restart) { base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, base::BindOnce(&AdsServiceImpl::Start, AsWeakPtr()), base::TimeDelta::FromSeconds(1)); @@ -330,19 +351,15 @@ void AdsServiceImpl::MaybeStart(bool restart) { } } -void AdsServiceImpl::Start() { +bool AdsServiceImpl::StartService() { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); - DCHECK(is_enabled()); - DCHECK(!bat_ads_.is_bound()); - - bat_ads::mojom::BatAdsClientAssociatedPtrInfo client_ptr_info; - bat_ads_client_binding_.Bind(mojo::MakeRequest(&client_ptr_info)); + DCHECK(!connected()); content::ServiceManagerConnection* connection = content::ServiceManagerConnection::GetForProcess(); if (!connection) - return; + return false; connection->GetConnector()->BindInterface( bat_ads::mojom::kServiceName, &bat_ads_service_); @@ -380,10 +397,17 @@ void AdsServiceImpl::Start() { bat_ads_service_->SetDebug(is_debug, base::NullCallback()); bat_ads_service_->SetTesting(is_testing, base::NullCallback()); - bat_ads_service_->Create(std::move(client_ptr_info), MakeRequest(&bat_ads_), - base::BindOnce(&AdsServiceImpl::OnCreate, AsWeakPtr())); + return true; +} +void AdsServiceImpl::Start() { BackgroundHelper::GetInstance()->AddObserver(this); + + bat_ads::mojom::BatAdsClientAssociatedPtrInfo client_ptr_info; + bat_ads_client_binding_.Bind(mojo::MakeRequest(&client_ptr_info)); + + bat_ads_service_->Create(std::move(client_ptr_info), MakeRequest(&bat_ads_), + base::BindOnce(&AdsServiceImpl::OnCreate, AsWeakPtr())); } void AdsServiceImpl::Stop() { @@ -542,9 +566,9 @@ int AdsServiceImpl::GetPrefsVersion() const { void AdsServiceImpl::OnPrefsChanged(const std::string& pref) { if (pref == prefs::kBraveAdsEnabled || pref == brave_rewards::prefs::kBraveRewardsEnabled) { - if (is_enabled()) { - Start(); - } else if (!is_enabled()) { + if (IsAdsEnabled()) { + MaybeStart(false); + } else { Stop(); } } else if (pref == prefs::kBraveAdsIdleThreshold) { @@ -552,26 +576,50 @@ void AdsServiceImpl::OnPrefsChanged(const std::string& pref) { } } -bool AdsServiceImpl::is_enabled() const { - bool ads_enabled = profile_->GetPrefs()->GetBoolean( +bool AdsServiceImpl::IsSupportedRegion() const { + return is_supported_region_; +} + +bool AdsServiceImpl::IsAdsEnabled() const { + auto ads_enabled = profile_->GetPrefs()->GetBoolean( prefs::kBraveAdsEnabled); - bool rewards_enabled = profile_->GetPrefs()->GetBoolean( + + auto prefs_migrated_from_62 = profile_->GetPrefs()->GetBoolean( + prefs::kBraveAdsPrefsMigratedFrom62); + + if (ads_enabled && prefs_migrated_from_62) { + // Disable Ads by default when upgrading from 0.62.x to 0.63.x + ads_enabled = false; + + profile_->GetPrefs()->SetBoolean( + prefs::kBraveAdsEnabled, ads_enabled); + + profile_->GetPrefs()->SetBoolean( + prefs::kBraveAdsPrefsMigratedFrom62, false); + } + + auto rewards_enabled = profile_->GetPrefs()->GetBoolean( brave_rewards::prefs::kBraveRewardsEnabled); - return (ads_enabled && rewards_enabled); + + return IsSupportedRegion() && ads_enabled && rewards_enabled; } -bool AdsServiceImpl::IsAdsEnabled() const { - return is_enabled(); +void AdsServiceImpl::SetAdsEnabled(const bool is_enabled) { + profile_->GetPrefs()->SetBoolean(prefs::kBraveAdsEnabled, is_enabled); } -void AdsServiceImpl::set_ads_enabled(bool enabled) { - profile_->GetPrefs()->SetBoolean(prefs::kBraveAdsEnabled, enabled); +uint64_t AdsServiceImpl::GetAdsPerHour() const { + return profile_->GetPrefs()->GetUint64(prefs::kBraveAdsPerHour); } -void AdsServiceImpl::set_ads_per_hour(int ads_per_hour) { +void AdsServiceImpl::SetAdsPerHour(const uint64_t ads_per_hour) { profile_->GetPrefs()->SetUint64(prefs::kBraveAdsPerHour, ads_per_hour); } +uint64_t AdsServiceImpl::GetAdsPerDay() const { + return profile_->GetPrefs()->GetUint64(prefs::kBraveAdsPerDay); +} + bool AdsServiceImpl::IsForeground() const { return BackgroundHelper::GetInstance()->IsForeground(); } @@ -660,18 +708,6 @@ void AdsServiceImpl::OnMediaStop(SessionID tab_id) { bat_ads_->OnMediaStopped(tab_id.id()); } -uint64_t AdsServiceImpl::GetAdsPerHour() const { - return profile_->GetPrefs()->GetUint64(prefs::kBraveAdsPerHour); -} - -uint64_t AdsServiceImpl::ads_per_hour() const { - return GetAdsPerHour(); -} - -uint64_t AdsServiceImpl::GetAdsPerDay() const { - return profile_->GetPrefs()->GetUint64(prefs::kBraveAdsPerDay); -} - void AdsServiceImpl::ShowNotification( std::unique_ptr info) { std::string notification_id; @@ -954,7 +990,7 @@ const std::vector AdsServiceImpl::GetLocales() const { } const std::string AdsServiceImpl::GetAdsLocale() const { - return g_browser_process->GetApplicationLocale(); + return LocaleHelper::GetInstance()->GetLocale(); } void AdsServiceImpl::URLRequest( @@ -1047,8 +1083,7 @@ uint32_t AdsServiceImpl::SetTimer(const uint64_t time_offset) { timers_[timer_id] = std::make_unique(); timers_[timer_id]->Start(FROM_HERE, base::TimeDelta::FromSeconds(time_offset), - base::BindOnce( - &AdsServiceImpl::OnTimer, AsWeakPtr(), timer_id)); + base::BindOnce(&AdsServiceImpl::OnTimer, AsWeakPtr(), timer_id)); return timer_id; } @@ -1080,12 +1115,4 @@ bool AdsServiceImpl::connected() { return bat_ads_.is_bound(); } -void AdsServiceImpl::IsSupportedRegion( - IsSupportedRegionCallback callback) { - if (!connected()) - return; - - bat_ads_->IsSupportedRegion(std::move(callback)); -} - } // namespace brave_ads diff --git a/components/brave_ads/browser/ads_service_impl.h b/components/brave_ads/browser/ads_service_impl.h index 5cc58f302387..ac4acd126c6f 100644 --- a/components/brave_ads/browser/ads_service_impl.h +++ b/components/brave_ads/browser/ads_service_impl.h @@ -56,11 +56,11 @@ class AdsServiceImpl : public AdsService, ~AdsServiceImpl() override; // AdsService implementation - bool is_enabled() const override; - uint64_t ads_per_hour() const override; + bool IsSupportedRegion() const override; - void set_ads_enabled(bool enabled) override; - void set_ads_per_hour(int ads_per_hour) override; + void SetAdsEnabled(const bool is_enabled) override; + + void SetAdsPerHour(const uint64_t ads_per_hour) override; void TabUpdated( SessionID tab_id, @@ -71,11 +71,15 @@ class AdsServiceImpl : public AdsService, void OnMediaStop(SessionID tab_id) override; void ClassifyPage(const std::string& url, const std::string& page) override; void SetConfirmationsIsReady(const bool is_ready) override; - void IsSupportedRegion( - IsSupportedRegionCallback callback) override; void Shutdown() override; + // AdsClient implementation + bool IsAdsEnabled() const override; + + uint64_t GetAdsPerHour() const override; + uint64_t GetAdsPerDay() const override; + private: friend class AdsNotificationHandler; @@ -83,6 +87,7 @@ class AdsServiceImpl : public AdsService, NotificationInfoMap; void Start(); + bool StartService(); void Stop(); void ResetTimer(); void CheckIdleState(); @@ -91,22 +96,20 @@ class AdsServiceImpl : public AdsService, #endif int GetIdleThreshold(); void OnShow(Profile* profile, const std::string& notification_id); - void OnClose(Profile* profile, - const GURL& origin, - const std::string& notification_id, - bool by_user, - base::OnceClosure completed_closure); - void OpenSettings(Profile* profile, - const GURL& origin, - bool should_close); - + void OnClose( + Profile* profile, + const GURL& origin, + const std::string& notification_id, + bool by_user, + base::OnceClosure completed_closure); + void OpenSettings( + Profile* profile, + const GURL& origin, + bool should_close); // AdsClient implementation - bool IsAdsEnabled() const override; bool IsForeground() const override; const std::string GetAdsLocale() const override; - uint64_t GetAdsPerHour() const override; - uint64_t GetAdsPerDay() const override; void GetClientInfo(ads::ClientInfo* info) const override; const std::vector GetLocales() const override; const std::string GenerateUUID() const override; @@ -115,23 +118,27 @@ class AdsServiceImpl : public AdsService, void ConfirmAd(std::unique_ptr info) override; uint32_t SetTimer(const uint64_t time_offset) override; void KillTimer(uint32_t timer_id) override; - void URLRequest(const std::string& url, - const std::vector& headers, - const std::string& content, - const std::string& content_type, - ads::URLRequestMethod method, - ads::URLRequestCallback callback) override; - void Save(const std::string& name, - const std::string& value, - ads::OnSaveCallback callback) override; - void Load(const std::string& name, - ads::OnLoadCallback callback) override; + void URLRequest( + const std::string& url, + const std::vector& headers, + const std::string& content, + const std::string& content_type, + ads::URLRequestMethod method, + ads::URLRequestCallback callback) override; + void Save( + const std::string& name, + const std::string& value, + ads::OnSaveCallback callback) override; + void Load( + const std::string& name, + ads::OnLoadCallback callback) override; void SaveBundleState( std::unique_ptr bundle_state, ads::OnSaveCallback callback) override; const std::string LoadJsonSchema(const std::string& name) override; - void Reset(const std::string& name, - ads::OnResetCallback callback) override; + void Reset( + const std::string& name, + ads::OnResetCallback callback) override; void GetAds( const std::string& category, ads::OnGetAdsCallback callback) override; @@ -149,8 +156,9 @@ class AdsServiceImpl : public AdsService, bool IsNetworkConnectionAvailable() override; // history::HistoryServiceObserver - void OnURLsDeleted(history::HistoryService* history_service, - const history::DeletionInfo& deletion_info) override; + void OnURLsDeleted( + history::HistoryService* history_service, + const history::DeletionInfo& deletion_info) override; // URLFetcherDelegate impl void OnURLFetchComplete(const net::URLFetcher* source) override; @@ -159,12 +167,14 @@ class AdsServiceImpl : public AdsService, void OnBackground() override; void OnForeground() override; - void OnGetAdsForCategory(const ads::OnGetAdsCallback& callback, - const std::string& category, - const std::vector& ads); + void OnGetAdsForCategory( + const ads::OnGetAdsCallback& callback, + const std::string& category, + const std::vector& ads); void OnSaveBundleState(const ads::OnSaveCallback& callback, bool success); - void OnLoaded(const ads::OnLoadCallback& callback, - const std::string& value); + void OnLoaded( + const ads::OnLoadCallback& callback, + const std::string& value); void OnSaved(const ads::OnSaveCallback& callback, bool success); void OnReset(const ads::OnResetCallback& callback, bool success); void OnTimer(uint32_t timer_id); @@ -178,9 +188,13 @@ class AdsServiceImpl : public AdsService, void OnPrefsChanged(const std::string& pref); void OnCreate(); void OnInitialize(); - void MaybeStart(bool restart); - void NotificationTimedOut(uint32_t timer_id, - const std::string& notification_id); + void MaybeStart(bool should_restart); + void OnMaybeStartForRegion( + bool should_restart, + bool is_supported_region); + void NotificationTimedOut( + uint32_t timer_id, + const std::string& notification_id); uint32_t next_timer_id(); @@ -192,9 +206,11 @@ class AdsServiceImpl : public AdsService, const base::FilePath base_path_; std::map> timers_; uint32_t next_timer_id_; + bool is_supported_region_; std::unique_ptr bundle_state_backend_; NotificationDisplayService* display_service_; // NOT OWNED brave_rewards::RewardsService* rewards_service_; // NOT OWNED + #if !defined(OS_ANDROID) ui::IdleState last_idle_state_; bool is_foreground_; diff --git a/components/brave_ads/browser/ads_tab_helper.cc b/components/brave_ads/browser/ads_tab_helper.cc index d68f8eb25a10..ae792d21b14c 100644 --- a/components/brave_ads/browser/ads_tab_helper.cc +++ b/components/brave_ads/browser/ads_tab_helper.cc @@ -75,7 +75,7 @@ void AdsTabHelper::DidFinishNavigation( void AdsTabHelper::DocumentOnLoadCompletedInMainFrame() { // don't start distilling is the ad service isn't enabled - if (!ads_service_ || !ads_service_->is_enabled() || !run_distiller_) + if (!ads_service_ || !ads_service_->IsAdsEnabled() || !run_distiller_) return; auto* dom_distiller_service = @@ -87,7 +87,7 @@ void AdsTabHelper::DocumentOnLoadCompletedInMainFrame() { auto source_page_handle = std::make_unique( - web_contents(), false); + web_contents(), false); auto distiller_page = dom_distiller_service->CreateDefaultDistillerPageWithHandle( diff --git a/components/brave_ads/browser/locale_helper.cc b/components/brave_ads/browser/locale_helper.cc new file mode 100644 index 000000000000..16239d4cc282 --- /dev/null +++ b/components/brave_ads/browser/locale_helper.cc @@ -0,0 +1,25 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/components/brave_ads/browser/locale_helper.h" + +namespace brave_ads { + +LocaleHelper::LocaleHelper() = default; + +LocaleHelper::~LocaleHelper() = default; + +std::string LocaleHelper::GetLocale() const { + return kDefaultLocale; +} + +#if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_LINUX) +LocaleHelper* LocaleHelper::GetInstance() { + // just return a dummy locale helper for all other platforms + return base::Singleton::get(); +} +#endif + +} // namespace brave_ads diff --git a/components/brave_ads/browser/locale_helper.h b/components/brave_ads/browser/locale_helper.h new file mode 100644 index 000000000000..aa58fc73b71e --- /dev/null +++ b/components/brave_ads/browser/locale_helper.h @@ -0,0 +1,38 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_COMPONENTS_BROWSER_BRAVE_ADS_LOCALE_HELPER_H_ +#define BRAVE_COMPONENTS_BROWSER_BRAVE_ADS_LOCALE_HELPER_H_ + +#include + +#include "base/macros.h" +#include "base/memory/singleton.h" +#include "build/build_config.h" + +namespace brave_ads { + +const char kDefaultLocale[] = "en-cUS"; + +class LocaleHelper { + public: + static LocaleHelper* GetInstance(); + + // Should return the language based upon the tagging conventions of RFC 4646 + virtual std::string GetLocale() const; + + protected: + LocaleHelper(); + virtual ~LocaleHelper(); + + private: + friend struct base::DefaultSingletonTraits; + + DISALLOW_COPY_AND_ASSIGN(LocaleHelper); +}; + +} // namespace brave_ads + +#endif // BRAVE_COMPONENTS_BROWSER_BRAVE_ADS_LOCALE_HELPER_H_ diff --git a/components/brave_ads/browser/locale_helper_linux.cc b/components/brave_ads/browser/locale_helper_linux.cc new file mode 100644 index 000000000000..41c000f5ba33 --- /dev/null +++ b/components/brave_ads/browser/locale_helper_linux.cc @@ -0,0 +1,46 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include + +#include "brave/components/brave_ads/browser/locale_helper_linux.h" + +namespace brave_ads { + +LocaleHelperLinux::LocaleHelperLinux() = default; + +LocaleHelperLinux::~LocaleHelperLinux() = default; + +std::string LocaleHelperLinux::GetLocale() const { + char const *language = nullptr; + + if (!language || !*language) { + language = std::getenv("LC_CTYPE"); + } + + if (!language || !*language) { + language = std::getenv("LC_ALL"); + } + + if (!language || !*language) { + language = std::getenv("LANG"); + } + + if (!language || !*language) { + language = kDefaultLocale; + } + + return std::string(language); +} + +LocaleHelperLinux* LocaleHelperLinux::GetInstance() { + return base::Singleton::get(); +} + +LocaleHelper* LocaleHelper::GetInstance() { + return LocaleHelperLinux::GetInstance(); +} + +} // namespace brave_ads diff --git a/components/brave_ads/browser/locale_helper_linux.h b/components/brave_ads/browser/locale_helper_linux.h new file mode 100644 index 000000000000..a90a234dd64c --- /dev/null +++ b/components/brave_ads/browser/locale_helper_linux.h @@ -0,0 +1,33 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_LOCALE_HELPER_LINUX_H_ +#define BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_LOCALE_HELPER_LINUX_H_ + +#include + +#include "brave/components/brave_ads/browser/locale_helper.h" + +namespace brave_ads { + +class LocaleHelperLinux : public LocaleHelper { + public: + LocaleHelperLinux(); + ~LocaleHelperLinux() override; + + static LocaleHelperLinux* GetInstance(); + + private: + friend struct base::DefaultSingletonTraits; + + // LocaleHelper impl + std::string GetLocale() const override; + + DISALLOW_COPY_AND_ASSIGN(LocaleHelperLinux); +}; + +} // namespace brave_ads + +#endif // BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_LOCALE_HELPER_LINUX_H_ diff --git a/components/brave_ads/browser/locale_helper_mac.h b/components/brave_ads/browser/locale_helper_mac.h new file mode 100644 index 000000000000..c6a3195b0c37 --- /dev/null +++ b/components/brave_ads/browser/locale_helper_mac.h @@ -0,0 +1,33 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_COMPONENTS_BROWSER_BRAVE_ADS_LOCALE_HELPER_MAC_H_ +#define BRAVE_COMPONENTS_BROWSER_BRAVE_ADS_LOCALE_HELPER_MAC_H_ + +#include + +#include "brave/components/brave_ads/browser/locale_helper.h" + +namespace brave_ads { + +class LocaleHelperMac : public LocaleHelper { + public: + LocaleHelperMac(); + ~LocaleHelperMac() override; + + static LocaleHelperMac* GetInstance(); + + private: + friend struct base::DefaultSingletonTraits; + + // LocaleHelperMac impl + std::string GetLocale() const override; + + DISALLOW_COPY_AND_ASSIGN(LocaleHelperMac); +}; + +} // namespace brave_ads + +#endif // BRAVE_COMPONENTS_BROWSER_BRAVE_ADS_LOCALE_HELPER_MAC_H_ diff --git a/components/brave_ads/browser/locale_helper_mac.mm b/components/brave_ads/browser/locale_helper_mac.mm new file mode 100644 index 000000000000..c7d32e43a2c4 --- /dev/null +++ b/components/brave_ads/browser/locale_helper_mac.mm @@ -0,0 +1,29 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/components/brave_ads/browser/locale_helper_mac.h" + +#import + +namespace brave_ads { + +LocaleHelperMac::LocaleHelperMac() = default; + +LocaleHelperMac::~LocaleHelperMac() = default; + +std::string LocaleHelperMac::GetLocale() const { + NSString *locale = [[NSLocale preferredLanguages] firstObject]; + return std::string([locale UTF8String]); +} + +LocaleHelperMac* LocaleHelperMac::GetInstance() { + return base::Singleton::get(); +} + +LocaleHelper* LocaleHelper::GetInstance() { + return LocaleHelperMac::GetInstance(); +} + +} // namespace brave_ads diff --git a/components/brave_ads/browser/locale_helper_win.cc b/components/brave_ads/browser/locale_helper_win.cc new file mode 100644 index 000000000000..210be2e08152 --- /dev/null +++ b/components/brave_ads/browser/locale_helper_win.cc @@ -0,0 +1,48 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/components/brave_ads/browser/locale_helper_win.h" + +namespace brave_ads { + +LocaleHelperWin::LocaleHelperWin() = default; + +LocaleHelperWin::~LocaleHelperWin() = default; + +std::string LocaleHelperWin::GetLocale() const { + auto size = ::GetLocaleInfoEx(nullptr, LOCALE_SNAME, nullptr, 0); + if (size == 0) { + return kDefaultLocale; + } + + std::unique_ptrlocale_name(new wchar_t[size]); + if (!locale_name.get()) { + return kDefaultLocale; + } + + auto ret = ::GetLocaleInfoEx(nullptr, LOCALE_SNAME, locale_name.get(), size); + if (ret == 0) { + return kDefaultLocale; + } + + std::unique_ptrlocale(new char[size]); + if (!locale.get()) { + return kDefaultLocale; + } + + wcstombs(locale.get(), locale_name.get(), size); + + return std::string(locale.get(), size); +} + +LocaleHelperWin* LocaleHelperWin::GetInstance() { + return base::Singleton::get(); +} + +LocaleHelper* LocaleHelper::GetInstance() { + return LocaleHelperWin::GetInstance(); +} + +} // namespace brave_ads diff --git a/components/brave_ads/browser/locale_helper_win.h b/components/brave_ads/browser/locale_helper_win.h new file mode 100644 index 000000000000..b7399957aa6a --- /dev/null +++ b/components/brave_ads/browser/locale_helper_win.h @@ -0,0 +1,34 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_COMPONENTS_BROWSER_BRAVE_ADS_LOCALE_HELPER_WIN_H_ +#define BRAVE_COMPONENTS_BROWSER_BRAVE_ADS_LOCALE_HELPER_WIN_H_ + +#include +#include + +#include "brave/components/brave_ads/browser/locale_helper.h" + +namespace brave_ads { + +class LocaleHelperWin : public LocaleHelper { + public: + LocaleHelperWin(); + ~LocaleHelperWin() override; + + static LocaleHelperWin* GetInstance(); + + private: + friend struct base::DefaultSingletonTraits; + + // LocaleHelper impl + std::string GetLocale() const override; + + DISALLOW_COPY_AND_ASSIGN(LocaleHelperWin); +}; + +} // namespace brave_ads + +#endif // BRAVE_COMPONENTS_BROWSER_BRAVE_ADS_LOCALE_HELPER_WIN_H_ diff --git a/components/brave_ads/common/pref_names.cc b/components/brave_ads/common/pref_names.cc index b0c292d30316..78c71fc52501 100644 --- a/components/brave_ads/common/pref_names.cc +++ b/components/brave_ads/common/pref_names.cc @@ -10,12 +10,16 @@ namespace brave_ads { namespace prefs { const char kBraveAdsEnabled[] = "brave.brave_ads.enabled"; + const char kBraveAdsPerHour[] = "brave.brave_ads.ads_per_hour"; const char kBraveAdsPerDay[] = "brave.brave_ads.ads_per_day"; const char kBraveAdsIdleThreshold[] = "brave.brave_ads.idle_threshold"; + const int kBraveAdsPrefsDefaultVersion = 1; const int kBraveAdsPrefsCurrentVersion = 2; const char kBraveAdsPrefsVersion[] = "brave.brave_ads.prefs.version"; +const char kBraveAdsPrefsMigratedFrom62[] = + "brave.brave_ads.prefs.migrated.from_0_62.x"; } // namespace prefs diff --git a/components/brave_ads/common/pref_names.h b/components/brave_ads/common/pref_names.h index a6a5699ba78a..be25ef55c5af 100644 --- a/components/brave_ads/common/pref_names.h +++ b/components/brave_ads/common/pref_names.h @@ -11,12 +11,15 @@ namespace brave_ads { namespace prefs { extern const char kBraveAdsEnabled[]; + extern const char kBraveAdsPerHour[]; extern const char kBraveAdsPerDay[]; extern const char kBraveAdsIdleThreshold[]; + extern const int kBraveAdsPrefsDefaultVersion; extern const int kBraveAdsPrefsCurrentVersion; extern const char kBraveAdsPrefsVersion[]; +extern const char kBraveAdsPrefsMigratedFrom62[]; } // namespace prefs diff --git a/components/brave_rewards/resources/ui/actions/rewards_actions.ts b/components/brave_rewards/resources/ui/actions/rewards_actions.ts index 28e6dc60bf9b..8f50bb258f5f 100644 --- a/components/brave_rewards/resources/ui/actions/rewards_actions.ts +++ b/components/brave_rewards/resources/ui/actions/rewards_actions.ts @@ -185,9 +185,3 @@ export const getConfirmationsHistory = () => action(types.GET_CONFIRMATIONS_HIST export const onConfirmationsHistoryChanged = () => action(types.ON_CONFIRMATIONS_HISTORY_CHANGED) export const getExcludedPublishersNumber = () => action(types.GET_EXCLUDED_PUBLISHERS_NUMBER) - -export const getAdsIsSupportedRegion = () => action(types.GET_ADS_IS_SUPPORTED_REGION) - -export const onAdsIsSupportedRegion = (supported: boolean) => action(types.ON_ADS_IS_SUPPORTED_REGION, { - supported -}) diff --git a/components/brave_rewards/resources/ui/brave_rewards.tsx b/components/brave_rewards/resources/ui/brave_rewards.tsx index ef9a96eda186..a28ed35d95d9 100644 --- a/components/brave_rewards/resources/ui/brave_rewards.tsx +++ b/components/brave_rewards/resources/ui/brave_rewards.tsx @@ -155,10 +155,6 @@ window.cr.define('brave_rewards', function () { getActions().onConfirmationsHistoryChanged() } - function adsIsSupportedRegion (supported: boolean) { - getActions().onAdsIsSupportedRegion(supported) - } - return { initialize, walletCreated, @@ -186,8 +182,7 @@ window.cr.define('brave_rewards', function () { rewardsEnabled, addressesForPaymentId, confirmationsHistory, - confirmationsHistoryChanged, - adsIsSupportedRegion + confirmationsHistoryChanged } }) diff --git a/components/brave_rewards/resources/ui/components/settingsPage.tsx b/components/brave_rewards/resources/ui/components/settingsPage.tsx index 84a228dbceac..566a90b94ff6 100644 --- a/components/brave_rewards/resources/ui/components/settingsPage.tsx +++ b/components/brave_rewards/resources/ui/components/settingsPage.tsx @@ -63,7 +63,6 @@ class SettingsPage extends React.Component { } this.actions.checkImported() this.actions.getGrants() - this.actions.getAdsIsSupportedRegion() // one time check (legacy fix) // more info here https://github.com/brave/brave-browser/issues/2172 diff --git a/components/brave_rewards/resources/ui/constants/rewards_types.ts b/components/brave_rewards/resources/ui/constants/rewards_types.ts index 89d2b4b8d096..622874ef61aa 100644 --- a/components/brave_rewards/resources/ui/constants/rewards_types.ts +++ b/components/brave_rewards/resources/ui/constants/rewards_types.ts @@ -57,7 +57,5 @@ export const enum types { GET_CONFIRMATIONS_HISTORY = '@@rewards/GET_CONFIRMATIONS_HISTORY', ON_CONFIRMATIONS_HISTORY = '@@rewards/ON_CONFIRMATIONS_HISTORY', ON_CONFIRMATIONS_HISTORY_CHANGED = '@@rewards/ON_CONFIRMATIONS_HISTORY_CHANGED', - GET_EXCLUDED_PUBLISHERS_NUMBER = '@@rewards/GET_EXCLUDED_PUBLISHERS_NUMBER', - GET_ADS_IS_SUPPORTED_REGION = '@@rewards/GET_ADS_IS_SUPPORTED_REGION', - ON_ADS_IS_SUPPORTED_REGION = '@@rewards/ON_ADS_IS_SUPPORTED_REGION' + GET_EXCLUDED_PUBLISHERS_NUMBER = '@@rewards/GET_EXCLUDED_PUBLISHERS_NUMBER' } diff --git a/components/brave_rewards/resources/ui/reducers/rewards_reducer.ts b/components/brave_rewards/resources/ui/reducers/rewards_reducer.ts index 071c3355759c..b700de18e225 100644 --- a/components/brave_rewards/resources/ui/reducers/rewards_reducer.ts +++ b/components/brave_rewards/resources/ui/reducers/rewards_reducer.ts @@ -121,6 +121,7 @@ const rewardsReducer: Reducer = (state: Rewards.State state.adsData.adsEnabled = action.payload.adsData.adsEnabled state.adsData.adsPerHour = action.payload.adsData.adsPerHour state.adsData.adsUIEnabled = action.payload.adsData.adsUIEnabled + state.adsData.adsIsSupported = action.payload.adsData.adsIsSupported break } case types.ON_ADS_SETTING_SAVE: { @@ -154,14 +155,6 @@ const rewardsReducer: Reducer = (state: Rewards.State state.adsData.adsEstimatedEarnings = data.adsEstimatedEarnings break } - case types.GET_ADS_IS_SUPPORTED_REGION: { - chrome.send('brave_rewards.getAdsIsSupportedRegion', []) - break - } - case types.ON_ADS_IS_SUPPORTED_REGION: { - state.adsData.adsIsSupported = action.payload.supported - break - } } return state diff --git a/components/services/bat_ads/bat_ads_impl.cc b/components/services/bat_ads/bat_ads_impl.cc index 82510f3a64db..1fe88b8d1571 100644 --- a/components/services/bat_ads/bat_ads_impl.cc +++ b/components/services/bat_ads/bat_ads_impl.cc @@ -10,8 +10,6 @@ #include "bat/ads/ads.h" #include "brave/components/services/bat_ads/bat_ads_client_mojo_bridge.h" -using namespace std::placeholders; - namespace bat_ads { namespace { @@ -31,7 +29,7 @@ BatAdsImpl::BatAdsImpl(mojom::BatAdsClientAssociatedPtrInfo client_info) BatAdsImpl::~BatAdsImpl() {} void BatAdsImpl::Initialize(InitializeCallback callback) { - // TODO - Initialize needs a real callback + // TODO(Terry Mancey): Initialize needs a real callback ads_->Initialize(); std::move(callback).Run(); } @@ -81,7 +79,7 @@ void BatAdsImpl::TabUpdated(int32_t tab_id, } void BatAdsImpl::RemoveAllHistory(RemoveAllHistoryCallback callback) { - // TODO - RemoveAllHistory needs a real callback + // TODO(Terry Mancey): RemoveAllHistory needs a real callback ads_->RemoveAllHistory(); std::move(callback).Run(); } @@ -113,9 +111,4 @@ void BatAdsImpl::GenerateAdReportingNotificationResultEvent( } } -void BatAdsImpl::IsSupportedRegion( - IsSupportedRegionCallback callback) { - std::move(callback).Run(ads_->IsSupportedRegion()); -} - } // namespace bat_ads diff --git a/components/services/bat_ads/bat_ads_impl.h b/components/services/bat_ads/bat_ads_impl.h index 9d895f430114..6874b05c036f 100644 --- a/components/services/bat_ads/bat_ads_impl.h +++ b/components/services/bat_ads/bat_ads_impl.h @@ -49,8 +49,6 @@ class BatAdsImpl : public mojom::BatAds { void GenerateAdReportingNotificationResultEvent( const std::string& notification_info, int32_t event_type) override; - void IsSupportedRegion( - IsSupportedRegionCallback callback) override; private: std::unique_ptr bat_ads_client_mojo_proxy_; diff --git a/components/services/bat_ads/bat_ads_service_impl.cc b/components/services/bat_ads/bat_ads_service_impl.cc index 727341719534..2eaec4792a73 100644 --- a/components/services/bat_ads/bat_ads_service_impl.cc +++ b/components/services/bat_ads/bat_ads_service_impl.cc @@ -1,4 +1,5 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ @@ -30,25 +31,35 @@ void BatAdsServiceImpl::Create( std::move(callback).Run(); } -void BatAdsServiceImpl::SetProduction(bool is_production, - SetProductionCallback callback) { +void BatAdsServiceImpl::SetProduction( + const bool is_production, + SetProductionCallback callback) { DCHECK(!has_initialized_ || ads::_is_production == is_production); ads::_is_production = is_production; std::move(callback).Run(); } -void BatAdsServiceImpl::SetTesting(bool is_testing, - SetTestingCallback callback) { +void BatAdsServiceImpl::SetTesting( + const bool is_testing, + SetTestingCallback callback) { DCHECK(!has_initialized_ || ads::_is_testing == is_testing); ads::_is_testing = is_testing; std::move(callback).Run(); } -void BatAdsServiceImpl::SetDebug(bool is_debug, - SetDebugCallback callback) { +void BatAdsServiceImpl::SetDebug( + const bool is_debug, + SetDebugCallback callback) { DCHECK(!has_initialized_ || ads::_is_debug == is_debug); ads::_is_debug = is_debug; std::move(callback).Run(); } +void BatAdsServiceImpl::IsSupportedRegion( + const std::string& locale, + IsSupportedRegionCallback callback) { + DCHECK(!has_initialized_); + std::move(callback).Run(ads::Ads::IsSupportedRegion(locale)); +} + } // namespace bat_ads diff --git a/components/services/bat_ads/bat_ads_service_impl.h b/components/services/bat_ads/bat_ads_service_impl.h index 00e0b9879073..3116fe8240ac 100644 --- a/components/services/bat_ads/bat_ads_service_impl.h +++ b/components/services/bat_ads/bat_ads_service_impl.h @@ -1,10 +1,12 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ #ifndef BRAVE_COMPONENTS_SERVICES_BAT_ADS_BAT_ADS_SERVICE_IMPL_H_ #define BRAVE_COMPONENTS_SERVICES_BAT_ADS_BAT_ADS_SERVICE_IMPL_H_ +#include #include #include "base/memory/ref_counted.h" @@ -18,17 +20,30 @@ class BatAdsServiceImpl : public mojom::BatAdsService { public: explicit BatAdsServiceImpl( std::unique_ptr service_ref); + ~BatAdsServiceImpl() override; // Overridden from BatAdsService: - void Create(mojom::BatAdsClientAssociatedPtrInfo client_info, - mojom::BatAdsAssociatedRequest bat_ads, - CreateCallback callback) override; - - void SetProduction(bool is_production, - SetProductionCallback callback) override; - void SetTesting(bool is_testing, SetTestingCallback callback) override; - void SetDebug(bool is_debug, SetDebugCallback callback) override; + void Create( + mojom::BatAdsClientAssociatedPtrInfo client_info, + mojom::BatAdsAssociatedRequest bat_ads, + CreateCallback callback) override; + + void SetProduction( + const bool is_production, + SetProductionCallback callback) override; + + void SetTesting( + const bool is_testing, + SetTestingCallback callback) override; + + void SetDebug( + const bool is_debug, + SetDebugCallback callback) override; + + void IsSupportedRegion( + const std::string& locale, + IsSupportedRegionCallback callback) override; private: const std::unique_ptr service_ref_; diff --git a/components/services/bat_ads/public/interfaces/bat_ads.mojom b/components/services/bat_ads/public/interfaces/bat_ads.mojom index fdb7bd0dbcf0..b011a3782c49 100644 --- a/components/services/bat_ads/public/interfaces/bat_ads.mojom +++ b/components/services/bat_ads/public/interfaces/bat_ads.mojom @@ -12,6 +12,7 @@ interface BatAdsService { SetProduction(bool is_production) => (); SetTesting(bool is_testing) => (); SetDebug(bool is_debug) => (); + IsSupportedRegion(string locale) => (bool is_supported); }; interface BatAdsClient { @@ -77,5 +78,4 @@ interface BatAds { GenerateAdReportingNotificationShownEvent(string notification_info); GenerateAdReportingNotificationResultEvent( string notification_info, int32 result_type); - IsSupportedRegion() => (bool is_supported); }; diff --git a/vendor/bat-native-ads/README.md b/vendor/bat-native-ads/README.md index 546ec8039cee..be08e2a485c5 100644 --- a/vendor/bat-native-ads/README.md +++ b/vendor/bat-native-ads/README.md @@ -30,6 +30,12 @@ Initialize Ads by calling `Initialize` when Ads are enabled or disabled on the C void Initialize() ``` +`IsSupportedRegion` should be called to determine if Ads are supported for the specified region +``` +static bool IsSupportedRegion( + const std::string& locale) +``` + `OnForeground` should be called when the browser enters the foreground ``` void OnForeground() @@ -82,11 +88,6 @@ void TabClosed( void RemoveAllHistory() ``` -`IsSupportedRegion` should be called to determine if Ads are supported for this operating system's region -``` -bool IsSupportedRegion() -``` - `SetConfirmationsIsReady` should be called to inform Ads if Confirmations is ready ``` void SetConfirmationsIsReady( diff --git a/vendor/bat-native-ads/include/bat/ads/ads.h b/vendor/bat-native-ads/include/bat/ads/ads.h index d2b0854d02eb..d3ec9c43c9a4 100644 --- a/vendor/bat-native-ads/include/bat/ads/ads.h +++ b/vendor/bat-native-ads/include/bat/ads/ads.h @@ -38,6 +38,9 @@ class ADS_EXPORT Ads { static Ads* CreateInstance(AdsClient* ads_client); + // Should be called to determine if Ads are supported for the specified locale + static bool IsSupportedRegion(const std::string& locale); + // Should be called when Ads are enabled or disabled on the Client virtual void Initialize() = 0; @@ -76,10 +79,6 @@ class ADS_EXPORT Ads { // Should be called to remove all cached history virtual void RemoveAllHistory() = 0; - // Shhould be called to determine if Ads are supported for this operating - // system's region - virtual bool IsSupportedRegion() = 0; - // Should be called to inform Ads if Confirmations is ready virtual void SetConfirmationsIsReady(const bool is_ready) = 0; diff --git a/vendor/bat-native-ads/src/bat/ads/ads.cc b/vendor/bat-native-ads/src/bat/ads/ads.cc index 7f4b132c8808..a9fad93a1c1e 100644 --- a/vendor/bat-native-ads/src/bat/ads/ads.cc +++ b/vendor/bat-native-ads/src/bat/ads/ads.cc @@ -6,6 +6,7 @@ #include "bat/ads/ads.h" #include "bat/ads/internal/ads_impl.h" +#include "bat/ads/internal/locale_helper.h" namespace ads { @@ -23,4 +24,16 @@ Ads* Ads::CreateInstance(AdsClient* ads_client) { return new AdsImpl(ads_client); } +bool Ads::IsSupportedRegion(const std::string& locale) { + auto region = helper::Locale::GetCountryCode(locale); + + auto supported_regions = {"US", "CA", "DE", "FR", "GB"}; + if (std::find(supported_regions.begin(), supported_regions.end(), region) + == supported_regions.end()) { + return false; + } + + return true; +} + } // namespace ads diff --git a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc index e7cea4776900..f9492da49b28 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc @@ -321,20 +321,6 @@ void AdsImpl::ConfirmAdUUIDIfAdEnabled() { } } -bool AdsImpl::IsSupportedRegion() { - auto supported_regions = {"US", "CA", "DE", "FR", "GB"}; - - auto locale = ads_client_->GetAdsLocale(); - auto region = helper::Locale::GetCountryCode(locale); - - if (std::find(supported_regions.begin(), supported_regions.end(), region) - == supported_regions.end()) { - return false; - } - - return true; -} - void AdsImpl::SetConfirmationsIsReady(const bool is_ready) { is_confirmations_ready_ = is_ready; } diff --git a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h index c8b712006c45..37d0255d12e1 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h @@ -80,8 +80,6 @@ class AdsImpl : public Ads { void ConfirmAdUUIDIfAdEnabled(); - bool IsSupportedRegion() override; - void SetConfirmationsIsReady(const bool is_ready) override; void ChangeLocale(const std::string& locale) override; From 7a2fbfc93b4ba4499ffd2194975315f22c2e5dce Mon Sep 17 00:00:00 2001 From: Ryan Lanese Date: Thu, 18 Apr 2019 04:14:29 -0700 Subject: [PATCH 2/2] Merge pull request #2263 from brave/fix-4154 Fix Shows supported region correctly for Ads on first load after upgrade. --- .../brave_rewards/resources/ui/components/settingsPage.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/components/brave_rewards/resources/ui/components/settingsPage.tsx b/components/brave_rewards/resources/ui/components/settingsPage.tsx index 566a90b94ff6..e29d9904c9cc 100644 --- a/components/brave_rewards/resources/ui/components/settingsPage.tsx +++ b/components/brave_rewards/resources/ui/components/settingsPage.tsx @@ -60,7 +60,10 @@ class SettingsPage extends React.Component { if (this.props.rewardsData.firstLoad === false) { this.refreshActions() + } else { + this.actions.getAdsData() } + this.actions.checkImported() this.actions.getGrants()