From 811b39e82bddf151c24313a62a19ac701f512524 Mon Sep 17 00:00:00 2001 From: Terry Mancey Date: Mon, 24 Feb 2025 15:59:58 -0500 Subject: [PATCH] [ads] Suppress RichNTT if brave://settings/content/javascript is disabled --- browser/ntp_background/BUILD.gn | 1 + browser/ntp_background/ntp_p3a_helper_impl.cc | 4 +- .../view_counter_service_factory.cc | 9 ++-- .../ntp_background_images/browser/BUILD.gn | 2 + .../browser/ntp_background_images_service.cc | 29 +++++++++-- .../browser/ntp_background_images_service.h | 5 +- .../ntp_background_images_service_unittest.cc | 51 ++++++++++++------- .../browser/ntp_sponsored_image_source.cc | 6 ++- .../ntp_sponsored_rich_media_source.cc | 10 ++-- .../browser/ntp_sponsored_rich_media_source.h | 6 ++- ...tp_sponsored_rich_media_source_unittest.cc | 2 +- .../browser/view_counter_service.cc | 39 +++++++++++--- .../browser/view_counter_service.h | 17 +++++-- .../browser/view_counter_service_unittest.cc | 18 +++++-- .../ntp_background_images_service_ios.mm | 6 ++- 15 files changed, 154 insertions(+), 51 deletions(-) diff --git a/browser/ntp_background/BUILD.gn b/browser/ntp_background/BUILD.gn index 190ee39cbe8e..f0130f45b830 100644 --- a/browser/ntp_background/BUILD.gn +++ b/browser/ntp_background/BUILD.gn @@ -28,6 +28,7 @@ source_set("ntp_background") { "//brave/components/ntp_background_images/buildflags", "//brave/components/p3a", "//chrome/browser:browser_process", + "//chrome/browser/content_settings:content_settings_factory", "//chrome/browser/profiles:profile", "//components/keyed_service/content", "//components/pref_registry", diff --git a/browser/ntp_background/ntp_p3a_helper_impl.cc b/browser/ntp_background/ntp_p3a_helper_impl.cc index 14aa8bc47ff8..eab752d8f756 100644 --- a/browser/ntp_background/ntp_p3a_helper_impl.cc +++ b/browser/ntp_background/ntp_p3a_helper_impl.cc @@ -137,12 +137,12 @@ NTPP3AHelperImpl::NTPP3AHelperImpl( if (ntp_background_images_service) { if (const auto* sr_data = ntp_background_images_service->GetBrandedImagesData( - /*super_referral=*/true)) { + /*super_referral=*/true, /*supports_rich_media=*/true)) { CheckLoadedCampaigns(*sr_data); } if (const auto* si_data = ntp_background_images_service->GetBrandedImagesData( - /*super_referral=*/false)) { + /*super_referral=*/false, /*supports_rich_media=*/true)) { CheckLoadedCampaigns(*si_data); } ntp_background_images_service_observation_.Observe( diff --git a/browser/ntp_background/view_counter_service_factory.cc b/browser/ntp_background/view_counter_service_factory.cc index 38aa255df961..96be047147b0 100644 --- a/browser/ntp_background/view_counter_service_factory.cc +++ b/browser/ntp_background/view_counter_service_factory.cc @@ -20,6 +20,7 @@ #include "brave/components/ntp_background_images/browser/view_counter_service.h" #include "brave/components/ntp_background_images/buildflags/buildflags.h" #include "chrome/browser/browser_process.h" +#include "chrome/browser/content_settings/host_content_settings_map_factory.h" #include "chrome/browser/profiles/profile.h" #include "components/keyed_service/content/browser_context_dependency_manager.h" #include "components/keyed_service/content/browser_context_keyed_service_factory.h" @@ -78,9 +79,9 @@ ViewCounterServiceFactory::BuildServiceInstanceForBrowserContext( browser_context, std::make_unique(service)); content::URLDataSource::Add( browser_context, std::make_unique(service)); - content::URLDataSource::Add( - browser_context, - std::make_unique(service)); + content::URLDataSource::Add(browser_context, + std::make_unique( + service, profile->GetPrefs())); std::unique_ptr ntp_p3a_helper; if (g_brave_browser_process->p3a_service() != nullptr) { @@ -92,7 +93,7 @@ ViewCounterServiceFactory::BuildServiceInstanceForBrowserContext( } return std::make_unique( - service, + HostContentSettingsMapFactory::GetForProfile(profile), service, #if BUILDFLAG(ENABLE_CUSTOM_BACKGROUND) BraveNTPCustomBackgroundServiceFactory::GetForContext(profile), #else diff --git a/components/ntp_background_images/browser/BUILD.gn b/components/ntp_background_images/browser/BUILD.gn index 6d175bcd4f3a..3eae8f769f88 100644 --- a/components/ntp_background_images/browser/BUILD.gn +++ b/components/ntp_background_images/browser/BUILD.gn @@ -59,6 +59,8 @@ static_library("browser") { "//brave/components/p3a_utils", "//brave/components/time_period_storage", "//components/component_updater", + "//components/content_settings/core/browser", + "//components/content_settings/core/common", "//components/keyed_service/core", "//components/pref_registry:pref_registry", "//components/prefs", diff --git a/components/ntp_background_images/browser/ntp_background_images_service.cc b/components/ntp_background_images/browser/ntp_background_images_service.cc index d0f276942651..c33a85be9091 100644 --- a/components/ntp_background_images/browser/ntp_background_images_service.cc +++ b/components/ntp_background_images/browser/ntp_background_images_service.cc @@ -470,7 +470,8 @@ NTPBackgroundImagesData* NTPBackgroundImagesService::GetBackgroundImagesData() } NTPSponsoredImagesData* NTPBackgroundImagesService::GetBrandedImagesData( - bool super_referral) const { + bool super_referral, + bool supports_rich_media) const { const bool is_super_referrals_enabled = base::FeatureList::IsEnabled(features::kBraveNTPSuperReferralWallpaper); if (is_super_referrals_enabled) { @@ -496,11 +497,14 @@ NTPSponsoredImagesData* NTPBackgroundImagesService::GetBrandedImagesData( } } - if (sponsored_images_data_ && sponsored_images_data_->IsValid()) { - return sponsored_images_data_.get(); + NTPSponsoredImagesData* const sponsored_images_data = + supports_rich_media ? sponsored_images_data_.get() + : sponsored_images_data_excluding_rich_media_.get(); + if (!sponsored_images_data || !sponsored_images_data->IsValid()) { + return nullptr; } - return nullptr; + return sponsored_images_data; } void NTPBackgroundImagesService::OnComponentReady( @@ -574,6 +578,23 @@ void NTPBackgroundImagesService::OnGetSponsoredComponentJsonData( } else { sponsored_images_data_ = std::make_unique( data, sponsored_images_installed_dir_); + sponsored_images_data_excluding_rich_media_ = + std::make_unique( + data, sponsored_images_installed_dir_); + for (auto& campaign : + sponsored_images_data_excluding_rich_media_->campaigns) { + std::erase_if(campaign.creatives, [](const auto& creative) { + return creative.wallpaper_type == WallpaperType::kRichMedia; + }); + } + std::erase_if( + sponsored_images_data_excluding_rich_media_->campaigns, + [](const auto& campaign) { return campaign.creatives.empty(); }); + if (!sponsored_images_data_excluding_rich_media_) { + sponsored_images_data_excluding_rich_media_ = + std::make_unique(); + } + for (auto& observer : observers_) { observer.OnSponsoredContentDidUpdate(data); } diff --git a/components/ntp_background_images/browser/ntp_background_images_service.h b/components/ntp_background_images/browser/ntp_background_images_service.h index ae9b8bf7d672..0d5c1b211d42 100644 --- a/components/ntp_background_images/browser/ntp_background_images_service.h +++ b/components/ntp_background_images/browser/ntp_background_images_service.h @@ -71,7 +71,8 @@ class NTPBackgroundImagesService { bool HasObserver(Observer* observer); NTPBackgroundImagesData* GetBackgroundImagesData() const; - NTPSponsoredImagesData* GetBrandedImagesData(bool super_referral) const; + NTPSponsoredImagesData* GetBrandedImagesData(bool super_referral, + bool supports_rich_media) const; bool test_data_used() const { return test_data_used_; } @@ -195,6 +196,8 @@ class NTPBackgroundImagesService { std::optional sponsored_images_component_id_; base::FilePath sponsored_images_installed_dir_; std::unique_ptr sponsored_images_data_; + std::unique_ptr + sponsored_images_data_excluding_rich_media_; base::FilePath super_referrals_installed_dir_; std::unique_ptr super_referrals_images_data_; diff --git a/components/ntp_background_images/browser/ntp_background_images_service_unittest.cc b/components/ntp_background_images/browser/ntp_background_images_service_unittest.cc index 33062aa57e08..06e0be431c19 100644 --- a/components/ntp_background_images/browser/ntp_background_images_service_unittest.cc +++ b/components/ntp_background_images/browser/ntp_background_images_service_unittest.cc @@ -458,7 +458,8 @@ TEST_F(NTPBackgroundImagesServiceTest, DISABLED_InternalDataTest) { // Check with json file w/o schema version with empty object. service_->sponsored_images_data_.reset(); service_->OnGetSponsoredComponentJsonData(/*is_super_referral=*/false, "{}"); - EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false)); + EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false, + /*supports_rich_media=*/true)); service_->background_images_data_.reset(); service_->OnGetComponentJsonData("{}"); EXPECT_FALSE(service_->GetBackgroundImagesData()); @@ -469,7 +470,8 @@ TEST_F(NTPBackgroundImagesServiceTest, DISABLED_InternalDataTest) { observer_.on_sponsored_images_updated = false; service_->OnGetSponsoredComponentJsonData(/*is_super_referral=*/false, kTestEmptyComponent); - EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false)); + EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false, + /*supports_rich_media=*/true)); EXPECT_TRUE(observer_.on_sponsored_images_updated); EXPECT_THAT(observer_.sponsored_images_data->campaigns, ::testing::IsEmpty()); observer_.background_images_data = nullptr; @@ -490,7 +492,8 @@ TEST_F(NTPBackgroundImagesServiceTest, DISABLED_InternalDataTest) { // Mark this is not SR to get SI data. service_->MarkThisInstallIsNotSuperReferralForever(); NTPSponsoredImagesData* const images_data = - service_->GetBrandedImagesData(/*super_referral=*/false); + service_->GetBrandedImagesData(/*super_referral=*/false, + /*supports_rich_media=*/true); EXPECT_TRUE(images_data); EXPECT_TRUE(images_data->IsValid()); EXPECT_FALSE(images_data->IsSuperReferral()); @@ -600,7 +603,8 @@ TEST_F(NTPBackgroundImagesServiceTest, DISABLED_InternalDataTest) { observer_.on_sponsored_images_updated = false; service_->OnGetSponsoredComponentJsonData(/*is_super_referral=*/false, test_json_string_higher_schema); - EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false)); + EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false, + /*supports_rich_media=*/true)); constexpr char kTestBackgroundJsonStringHigherSchema[] = R"( { @@ -638,7 +642,8 @@ TEST_F(NTPBackgroundImagesServiceTest, MultipleCampaignsTest) { // Mark this is not SR to get SI data. service_->MarkThisInstallIsNotSuperReferralForever(); const NTPSponsoredImagesData* const images_data = - service_->GetBrandedImagesData(/*super_referral=*/false); + service_->GetBrandedImagesData(/*super_referral=*/false, + /*supports_rich_media=*/true); EXPECT_TRUE(images_data); EXPECT_TRUE(images_data->IsValid()); EXPECT_FALSE(images_data->IsSuperReferral()); @@ -677,7 +682,8 @@ TEST_F(NTPBackgroundImagesServiceTest, // Mark this is not SR to get SI data. service_->MarkThisInstallIsNotSuperReferralForever(); - EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false)); + EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false, + /*supports_rich_media=*/true)); EXPECT_TRUE(observer_.on_sponsored_images_updated); EXPECT_THAT(observer_.sponsored_images_data->campaigns, ::testing::IsEmpty()); EXPECT_THAT(service_->sponsored_images_data_->campaigns, @@ -699,7 +705,8 @@ TEST_F(NTPBackgroundImagesServiceTest, // Mark this is not SR to get SI data. service_->MarkThisInstallIsNotSuperReferralForever(); - EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false)); + EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false, + /*supports_rich_media=*/true)); EXPECT_TRUE(observer_.on_sponsored_images_updated); EXPECT_THAT(observer_.sponsored_images_data->campaigns, ::testing::IsEmpty()); EXPECT_THAT(service_->sponsored_images_data_->campaigns, @@ -723,7 +730,8 @@ TEST_F( // Mark this is not SR to get SI data. service_->MarkThisInstallIsNotSuperReferralForever(); - EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false)); + EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false, + /*supports_rich_media=*/true)); EXPECT_TRUE(observer_.on_sponsored_images_updated); EXPECT_THAT(observer_.sponsored_images_data->campaigns, ::testing::IsEmpty()); EXPECT_THAT(service_->sponsored_images_data_->campaigns, @@ -747,7 +755,8 @@ TEST_F( // Mark this is not SR to get SI data. service_->MarkThisInstallIsNotSuperReferralForever(); - EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false)); + EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false, + /*supports_rich_media=*/true)); EXPECT_TRUE(observer_.on_sponsored_images_updated); EXPECT_THAT(observer_.sponsored_images_data->campaigns, ::testing::IsEmpty()); EXPECT_THAT(service_->sponsored_images_data_->campaigns, @@ -768,7 +777,8 @@ TEST_F(NTPBackgroundImagesServiceTest, SponsoredImageWithMissingImageUrlTest) { // Mark this is not SR to get SI data. service_->MarkThisInstallIsNotSuperReferralForever(); - EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false)); + EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false, + /*supports_rich_media=*/true)); EXPECT_TRUE(observer_.on_sponsored_images_updated); EXPECT_THAT(observer_.sponsored_images_data->campaigns, ::testing::IsEmpty()); EXPECT_THAT(service_->sponsored_images_data_->campaigns, @@ -873,7 +883,8 @@ TEST_F(NTPBackgroundImagesServiceTest, BasicSuperReferralTest) { service_->OnGetSponsoredComponentJsonData(/*is_super_referral=*/true, kTestSuperReferral); const NTPSponsoredImagesData* const images_data = - service_->GetBrandedImagesData(/*super_referral=*/true); + service_->GetBrandedImagesData(/*super_referral=*/true, + /*supports_rich_media=*/true); EXPECT_TRUE(images_data); EXPECT_THAT(images_data->campaigns[0].creatives, ::testing::SizeIs(1)); EXPECT_THAT(images_data->top_sites, ::testing::SizeIs(3)); @@ -943,7 +954,8 @@ TEST_F(NTPBackgroundImagesServiceTest, WithNonSuperReferralCodeTest) { kTestSponsoredImages); // NTP SI data is ready but don't give data until NTP SR initialization is // complete. Only gives NTP SI data when browser confirms this is not NTP SR. - EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false)); + EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false, + /*supports_rich_media=*/true)); observer_.on_super_referral_ended = false; service_->OnGetMappingTableData(kTestMappingTable); @@ -1000,7 +1012,8 @@ TEST_F(NTPBackgroundImagesServiceTest, WithSuperReferralCodeTest) { EXPECT_FALSE(pref_service_.GetBoolean( prefs::kNewTabPageGetInitialSuperReferralComponentInProgress)); const NTPSponsoredImagesData* const data = - service_->GetBrandedImagesData(/*super_referral=*/true); + service_->GetBrandedImagesData(/*super_referral=*/true, + /*supports_rich_media=*/true); EXPECT_TRUE(service_->IsValidSuperReferralComponentInfo(pref_service_.GetDict( prefs::kNewTabPageCachedSuperReferralComponentInfo))); EXPECT_TRUE(data->IsSuperReferral()); @@ -1029,19 +1042,23 @@ TEST_F(NTPBackgroundImagesServiceTest, CheckReferralServiceInitStatusTest) { Init(); // Initially, data is not available. - EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/true)); - EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false)); + EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/true, + /*supports_rich_media=*/true)); + EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false, + /*supports_rich_media=*/true)); // Simulate SI data is initialized first before referral service is // initialized. // Check SI data is not available before referrals service is initialized. service_->OnGetSponsoredComponentJsonData(/*is_super_referral=*/false, kTestSponsoredImages); - EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false)); + EXPECT_FALSE(service_->GetBrandedImagesData(/*super_referral=*/false, + /*supports_rich_media=*/true)); // Simulate that this install is not SR. Then, SI data is returned properly. service_->MarkThisInstallIsNotSuperReferralForever(); - EXPECT_TRUE(service_->GetBrandedImagesData(/*super_referral=*/false)); + EXPECT_TRUE(service_->GetBrandedImagesData(/*super_referral=*/false, + /*supports_rich_media=*/true)); } TEST_F(NTPBackgroundImagesServiceTest, diff --git a/components/ntp_background_images/browser/ntp_sponsored_image_source.cc b/components/ntp_background_images/browser/ntp_sponsored_image_source.cc index a8cc49fb364c..6d0d1a581e14 100644 --- a/components/ntp_background_images/browser/ntp_sponsored_image_source.cc +++ b/components/ntp_background_images/browser/ntp_sponsored_image_source.cc @@ -113,7 +113,8 @@ base::FilePath NTPSponsoredImageSource::GetLocalFilePathFor( const std::string& path) { const bool is_super_referral_path = IsSuperReferralPath(path); const NTPSponsoredImagesData* const images_data = - service_->GetBrandedImagesData(is_super_referral_path); + service_->GetBrandedImagesData(is_super_referral_path, + /*supports_rich_media=*/false); CHECK(images_data); const base::FilePath basename_from_path = @@ -150,7 +151,8 @@ base::FilePath NTPSponsoredImageSource::GetLocalFilePathFor( bool NTPSponsoredImageSource::IsValidPath(const std::string& path) const { const bool is_super_referral_path = IsSuperReferralPath(path); const NTPSponsoredImagesData* const images_data = - service_->GetBrandedImagesData(is_super_referral_path); + service_->GetBrandedImagesData(is_super_referral_path, + /*supports_rich_media=*/false); if (!images_data) { return false; } diff --git a/components/ntp_background_images/browser/ntp_sponsored_rich_media_source.cc b/components/ntp_background_images/browser/ntp_sponsored_rich_media_source.cc index ca93782d73ba..8e015ff5523f 100644 --- a/components/ntp_background_images/browser/ntp_sponsored_rich_media_source.cc +++ b/components/ntp_background_images/browser/ntp_sponsored_rich_media_source.cc @@ -37,8 +37,11 @@ std::optional ReadFileToString(const base::FilePath& path) { } // namespace NTPSponsoredRichMediaSource::NTPSponsoredRichMediaSource( - NTPBackgroundImagesService* service) - : service_(service), weak_factory_(this) {} + NTPBackgroundImagesService* service, + const PrefService* pref_service) + : service_(service), pref_service_(pref_service), weak_factory_(this) { + CHECK(pref_service_); +} NTPSponsoredRichMediaSource::~NTPSponsoredRichMediaSource() = default; @@ -57,7 +60,8 @@ void NTPSponsoredRichMediaSource::StartDataRequest( } const NTPSponsoredImagesData* const images_data = - service_->GetBrandedImagesData(/*super_referral=*/false); + service_->GetBrandedImagesData(/*super_referral=*/false, + /*supports_rich_media=*/true); if (!images_data) { return DenyAccess(std::move(callback)); } diff --git a/components/ntp_background_images/browser/ntp_sponsored_rich_media_source.h b/components/ntp_background_images/browser/ntp_sponsored_rich_media_source.h index bb2cf3a2ac74..1485b02337d0 100644 --- a/components/ntp_background_images/browser/ntp_sponsored_rich_media_source.h +++ b/components/ntp_background_images/browser/ntp_sponsored_rich_media_source.h @@ -10,6 +10,7 @@ #include #include "base/memory/weak_ptr.h" +#include "components/prefs/pref_service.h" #include "content/public/browser/url_data_source.h" class GURL; @@ -23,7 +24,8 @@ class NTPBackgroundImagesService; class NTPSponsoredRichMediaSource : public content::URLDataSource { public: - explicit NTPSponsoredRichMediaSource(NTPBackgroundImagesService* service); + explicit NTPSponsoredRichMediaSource(NTPBackgroundImagesService* service, + const PrefService* pref_service); NTPSponsoredRichMediaSource(const NTPSponsoredRichMediaSource&) = delete; NTPSponsoredRichMediaSource& operator=(const NTPSponsoredRichMediaSource&) = @@ -49,6 +51,8 @@ class NTPSponsoredRichMediaSource : public content::URLDataSource { const raw_ptr service_; // not owned. + const raw_ptr pref_service_; // not owned. + base::WeakPtrFactory weak_factory_; }; diff --git a/components/ntp_background_images/browser/ntp_sponsored_rich_media_source_unittest.cc b/components/ntp_background_images/browser/ntp_sponsored_rich_media_source_unittest.cc index efe338b59842..0f4a3b84752e 100644 --- a/components/ntp_background_images/browser/ntp_sponsored_rich_media_source_unittest.cc +++ b/components/ntp_background_images/browser/ntp_sponsored_rich_media_source_unittest.cc @@ -73,7 +73,7 @@ class NTPSponsoredRichMediaSourceTest : public testing::Test { background_images_service_ = std::make_unique( /*component_update_service=*/nullptr, &pref_service_); url_data_source_ = std::make_unique( - background_images_service_.get()); + background_images_service_.get(), &pref_service_); } void SetupSponsoredComponent() { diff --git a/components/ntp_background_images/browser/view_counter_service.cc b/components/ntp_background_images/browser/view_counter_service.cc index 2ef4c509314b..92ccb867f8d4 100644 --- a/components/ntp_background_images/browser/view_counter_service.cc +++ b/components/ntp_background_images/browser/view_counter_service.cc @@ -29,6 +29,9 @@ #include "brave/components/ntp_background_images/common/pref_names.h" #include "brave/components/p3a_utils/bucket.h" #include "brave/components/time_period_storage/weekly_storage.h" +#include "components/content_settings/core/browser/host_content_settings_map.h" +#include "components/content_settings/core/common/content_settings.h" +#include "components/content_settings/core/common/content_settings_pattern.h" #include "components/pref_registry/pref_registry_syncable.h" #include "components/prefs/pref_service.h" #include "content/public/browser/web_ui_data_source.h" @@ -91,6 +94,7 @@ void ViewCounterService::MigrateObsoleteProfilePrefs(PrefService* prefs) { } ViewCounterService::ViewCounterService( + HostContentSettingsMap* host_content_settings_map, NTPBackgroundImagesService* background_images_service, BraveNTPCustomBackgroundService* custom_background_service, brave_ads::AdsService* ads_service, @@ -98,7 +102,8 @@ ViewCounterService::ViewCounterService( PrefService* local_state, std::unique_ptr ntp_p3a_helper, bool is_supported_locale) - : background_images_service_(background_images_service), + : host_content_settings_map_(host_content_settings_map), + background_images_service_(background_images_service), ads_service_(ads_service), prefs_(prefs), local_state_prefs_(local_state), @@ -117,6 +122,8 @@ ViewCounterService::ViewCounterService( ResetModel(); + host_content_settings_map_->AddObserver(this); + pref_change_registrar_.Init(prefs_); pref_change_registrar_.Add( brave_rewards::prefs::kEnabled, @@ -140,7 +147,18 @@ ViewCounterService::ViewCounterService( UpdateP3AValues(); } -ViewCounterService::~ViewCounterService() = default; +ViewCounterService::~ViewCounterService() { + host_content_settings_map_->RemoveObserver(this); +} + +void ViewCounterService::OnContentSettingChanged( + const ContentSettingsPattern& /*primary_pattern*/, + const ContentSettingsPattern& /*secondary_pattern*/, + ContentSettingsTypeSet content_type_set) { + if (content_type_set.Contains(ContentSettingsType::JAVASCRIPT)) { + ResetModel(); + } +} void ViewCounterService::BrandedWallpaperWillBeDisplayed( const std::string& wallpaper_id, @@ -165,14 +183,21 @@ NTPBackgroundImagesData* ViewCounterService::GetCurrentWallpaperData() const { NTPSponsoredImagesData* ViewCounterService::GetCurrentBrandedWallpaperData() const { - NTPSponsoredImagesData* const images_data = - background_images_service_->GetBrandedImagesData(/*super_referral=*/true); - if (images_data && IsSuperReferralWallpaperOptedIn()) { - return images_data; + const ContentSetting content_setting = + host_content_settings_map_->GetContentSetting( + /*primary_url=*/GURL(), /*secondary_url=*/GURL(), + ContentSettingsType::JAVASCRIPT); + const bool supports_rich_media = content_setting == CONTENT_SETTING_ALLOW; + + NTPSponsoredImagesData* sponsored_images_data = + background_images_service_->GetBrandedImagesData(/*super_referral=*/true, + supports_rich_media); + if (sponsored_images_data && IsSuperReferralWallpaperOptedIn()) { + return sponsored_images_data; } return background_images_service_->GetBrandedImagesData( - /*super_referral=*/false); + /*super_referral=*/false, supports_rich_media); } std::optional diff --git a/components/ntp_background_images/browser/view_counter_service.h b/components/ntp_background_images/browser/view_counter_service.h index 13e8739ceff3..daf9eae1fbed 100644 --- a/components/ntp_background_images/browser/view_counter_service.h +++ b/components/ntp_background_images/browser/view_counter_service.h @@ -21,6 +21,7 @@ #include "brave/components/ntp_background_images/browser/ntp_background_images_service.h" #include "brave/components/ntp_background_images/browser/view_counter_model.h" #include "brave/components/ntp_background_images/buildflags/buildflags.h" +#include "components/content_settings/core/browser/content_settings_observer.h" #include "components/keyed_service/core/keyed_service.h" #include "components/prefs/pref_change_registrar.h" @@ -40,6 +41,7 @@ namespace user_prefs { class PrefRegistrySyncable; } // namespace user_prefs +class HostContentSettingsMap; class WeeklyStorage; namespace ntp_background_images { @@ -52,9 +54,11 @@ struct NTPSponsoredImagesData; struct TopSite; class ViewCounterService : public KeyedService, + public content_settings::Observer, public NTPBackgroundImagesService::Observer { public: - ViewCounterService(NTPBackgroundImagesService* background_images_service, + ViewCounterService(HostContentSettingsMap* host_content_settings, + NTPBackgroundImagesService* background_images_service, BraveNTPCustomBackgroundService* custom_background_service, brave_ads::AdsService* ads_service, PrefService* prefs, @@ -149,10 +153,16 @@ class ViewCounterService : public KeyedService, void OnPreferenceChanged(const std::string& pref_name); - // KeyedService + // content_settings::Observer: + void OnContentSettingChanged( + const ContentSettingsPattern& primary_pattern, + const ContentSettingsPattern& secondary_pattern, + ContentSettingsTypeSet content_type_set) override; + + // KeyedService: void Shutdown() override; - // NTPBackgroundImagesService::Observer + // NTPBackgroundImagesService::Observer: void OnBackgroundImagesDataDidUpdate(NTPBackgroundImagesData* data) override; void OnSponsoredImagesDataDidUpdate(NTPSponsoredImagesData* data) override; void OnSponsoredContentDidUpdate(const base::Value::Dict& data) override; @@ -179,6 +189,7 @@ class ViewCounterService : public KeyedService, void UpdateP3AValues(); + raw_ptr host_content_settings_map_ = nullptr; raw_ptr background_images_service_ = nullptr; raw_ptr ads_service_ = nullptr; raw_ptr prefs_ = nullptr; diff --git a/components/ntp_background_images/browser/view_counter_service_unittest.cc b/components/ntp_background_images/browser/view_counter_service_unittest.cc index 34a9df12181d..85638a7661a6 100644 --- a/components/ntp_background_images/browser/view_counter_service_unittest.cc +++ b/components/ntp_background_images/browser/view_counter_service_unittest.cc @@ -12,7 +12,6 @@ #include "base/files/file_path.h" #include "base/memory/raw_ptr.h" -#include "base/test/task_environment.h" #include "brave/components/brave_ads/browser/ads_service_mock.h" #include "brave/components/brave_ads/core/public/ad_units/new_tab_page_ad/new_tab_page_ad_info.h" #include "brave/components/brave_referrals/browser/brave_referrals_service.h" @@ -29,8 +28,12 @@ #include "brave/components/ntp_background_images/buildflags/buildflags.h" #include "brave/components/ntp_background_images/common/pref_names.h" #include "build/build_config.h" +#include "chrome/browser/content_settings/host_content_settings_map_factory.h" +#include "chrome/test/base/testing_profile.h" +#include "components/content_settings/core/browser/host_content_settings_map.h" #include "components/prefs/testing_pref_service.h" #include "components/sync_preferences/testing_pref_service_syncable.h" +#include "content/public/test/browser_task_environment.h" #include "testing/gtest/include/gtest/gtest.h" #if BUILDFLAG(ENABLE_CUSTOM_BACKGROUND) @@ -156,6 +159,7 @@ class NTPBackgroundImagesViewCounterTest : public testing::Test { brave::RegisterPrefsForBraveReferralsService(local_registry); NTPBackgroundImagesService::RegisterLocalStatePrefs(local_registry); ViewCounterService::RegisterLocalStatePrefs(local_registry); + HostContentSettingsMap::RegisterProfilePrefs(prefs_.registry()); service_ = std::make_unique( /*component_updater_service=*/nullptr, &pref_service_); @@ -165,8 +169,9 @@ class NTPBackgroundImagesViewCounterTest : public testing::Test { custom_background_service_ = std::make_unique(std::move(delegate)); view_counter_ = std::make_unique( - service_.get(), custom_background_service_.get(), &ads_service_mock_, - prefs(), &pref_service_, + host_content_settings_map(), service_.get(), + custom_background_service_.get(), &ads_service_mock_, prefs(), + &pref_service_, // don't need to test p3a, so passing nullptr std::unique_ptr(), /* is_supported_locale */ true); @@ -182,6 +187,10 @@ class NTPBackgroundImagesViewCounterTest : public testing::Test { base::Value::Dict()); } + HostContentSettingsMap* host_content_settings_map() { + return HostContentSettingsMapFactory::GetForProfile(&profile_); + } + void EnableSIPref(bool enable) { prefs()->SetBoolean( prefs::kNewTabPageShowSponsoredImagesBackgroundImage, enable); @@ -244,10 +253,11 @@ class NTPBackgroundImagesViewCounterTest : public testing::Test { } protected: - base::test::SingleThreadTaskEnvironment task_environment_; + content::BrowserTaskEnvironment task_environment_; TestingPrefServiceSimple pref_service_; sync_preferences::TestingPrefServiceSyncable prefs_; std::unique_ptr service_; + TestingProfile profile_; #if BUILDFLAG(ENABLE_CUSTOM_BACKGROUND) std::unique_ptr custom_background_service_; diff --git a/ios/browser/api/ntp_background_images/ntp_background_images_service_ios.mm b/ios/browser/api/ntp_background_images/ntp_background_images_service_ios.mm index 15172d329144..b9396533396d 100644 --- a/ios/browser/api/ntp_background_images/ntp_background_images_service_ios.mm +++ b/ios/browser/api/ntp_background_images/ntp_background_images_service_ios.mm @@ -88,7 +88,8 @@ - (void)dealloc { } - (NTPSponsoredImageData*)sponsoredImageData { - auto* data = _service->GetBrandedImagesData(/* super_referral */ false); + auto* data = _service->GetBrandedImagesData(/*super_referral=*/false, + /*supports_rich_media=*/false); if (data == nullptr) { return nil; } @@ -96,7 +97,8 @@ - (NTPSponsoredImageData*)sponsoredImageData { } - (NTPSponsoredImageData*)superReferralImageData { - auto* data = _service->GetBrandedImagesData(/* super_referral */ true); + auto* data = _service->GetBrandedImagesData(/* super_referral=*/true, + /*supports_rich_media=*/false); if (data == nullptr) { return nil; }