diff --git a/browser/brave_ads/ads_service_impl.cc b/browser/brave_ads/ads_service_impl.cc index 2224f7d5a89e..9349dea3f217 100644 --- a/browser/brave_ads/ads_service_impl.cc +++ b/browser/brave_ads/ads_service_impl.cc @@ -682,13 +682,23 @@ void AdsServiceImpl::OnInitialize(const bool success) { StartCheckIdleStateTimer(); - if (!deprecated_data_files_removed_) { - deprecated_data_files_removed_ = true; - file_task_runner_->PostTask( - FROM_HERE, base::BindOnce(&RemoveDeprecatedAdsDataFiles, base_path_)); + if (!is_setup_on_first_initialize_done_) { + SetupOnFirstInitialize(); + is_setup_on_first_initialize_done_ = true; } } +void AdsServiceImpl::SetupOnFirstInitialize() { + DCHECK(!is_setup_on_first_initialize_done_); + + PurgeOrphanedAdEventsForType( + ads::mojom::AdType::kNewTabPageAd, + base::BindOnce(&AdsServiceImpl::PrefetchNewTabPageAd, AsWeakPtr())); + + file_task_runner_->PostTask( + FROM_HERE, base::BindOnce(&RemoveDeprecatedAdsDataFiles, base_path_)); +} + void AdsServiceImpl::ShutdownBatAds() { if (!connected()) { return; @@ -1106,6 +1116,23 @@ void AdsServiceImpl::OnOpenNewTabWithAd(const std::string& json) { OpenNewTabWithUrl(notification.target_url); } +absl::optional +AdsServiceImpl::GetPrefetchedNewTabPageAd() { + if (!connected()) { + return absl::nullopt; + } + + absl::optional ad_info; + if (prefetched_new_tab_page_ad_info_) { + ad_info = prefetched_new_tab_page_ad_info_; + prefetched_new_tab_page_ad_info_.reset(); + } + + PrefetchNewTabPageAd(); + + return ad_info; +} + void AdsServiceImpl::OnNewTabPageAdEvent( const std::string& uuid, const std::string& creative_instance_id, @@ -1152,12 +1179,13 @@ void AdsServiceImpl::OnInlineContentAdEvent( } void AdsServiceImpl::PurgeOrphanedAdEventsForType( - const ads::mojom::AdType ad_type) { + const ads::mojom::AdType ad_type, + base::OnceClosure callback) { if (!connected()) { return; } - bat_ads_->PurgeOrphanedAdEventsForType(ad_type); + bat_ads_->PurgeOrphanedAdEventsForType(ad_type, std::move(callback)); } void AdsServiceImpl::RetryOpeningNewTabWithAd(const std::string& uuid) { @@ -1211,6 +1239,28 @@ void AdsServiceImpl::RegisterResourceComponentsForLocale( locale); } +void AdsServiceImpl::PrefetchNewTabPageAd() { + if (!connected()) { + prefetched_new_tab_page_ad_info_.reset(); + return; + } + + bat_ads_->GetNewTabPageAd( + base::BindOnce(&AdsServiceImpl::OnPrefetchNewTabPageAd, AsWeakPtr())); +} + +void AdsServiceImpl::OnPrefetchNewTabPageAd(bool success, + const std::string& json) { + if (!success) { + prefetched_new_tab_page_ad_info_.reset(); + return; + } + + ads::NewTabPageAdInfo ad_info; + ad_info.FromJson(json); + prefetched_new_tab_page_ad_info_ = ad_info; +} + void AdsServiceImpl::OnURLRequestStarted( const GURL& final_url, const network::mojom::URLResponseHead& response_head) { diff --git a/browser/brave_ads/ads_service_impl.h b/browser/brave_ads/ads_service_impl.h index 4c7433c95f10..7f1a73375197 100644 --- a/browser/brave_ads/ads_service_impl.h +++ b/browser/brave_ads/ads_service_impl.h @@ -164,7 +164,10 @@ class AdsServiceImpl : public AdsService, const std::string& creative_instance_id, const ads::mojom::InlineContentAdEventType event_type) override; - void PurgeOrphanedAdEventsForType(const ads::mojom::AdType ad_type) override; + absl::optional GetPrefetchedNewTabPageAd() override; + + void PurgeOrphanedAdEventsForType(const ads::mojom::AdType ad_type, + base::OnceClosure callback) override; void GetAdsHistory(const double from_timestamp, const double to_timestamp, @@ -208,6 +211,7 @@ class AdsServiceImpl : public AdsService, void OnCreate(); void OnInitialize(const bool success); + void SetupOnFirstInitialize(); void ShutdownBatAds(); void OnShutdownBatAds(const bool success); @@ -255,6 +259,10 @@ class AdsServiceImpl : public AdsService, void RegisterResourceComponentsForLocale(const std::string& locale); + void PrefetchNewTabPageAd(); + + void OnPrefetchNewTabPageAd(bool success, const std::string& json); + void OnURLRequestStarted( const GURL& final_url, const network::mojom::URLResponseHead& response_head); @@ -464,7 +472,7 @@ class AdsServiceImpl : public AdsService, bool is_initialized_ = false; - bool deprecated_data_files_removed_ = false; + bool is_setup_on_first_initialize_done_ = false; bool is_upgrading_from_pre_brave_ads_build_; @@ -485,6 +493,8 @@ class AdsServiceImpl : public AdsService, std::unique_ptr database_; + absl::optional prefetched_new_tab_page_ad_info_; + ui::IdleState last_idle_state_; int last_idle_time_; diff --git a/browser/notifications/BUILD.gn b/browser/notifications/BUILD.gn index 7c5675cc7182..1b79c8ff6ec8 100644 --- a/browser/notifications/BUILD.gn +++ b/browser/notifications/BUILD.gn @@ -15,6 +15,7 @@ source_set("notifications") { deps = [ "//base", "//brave/components/brave_adaptive_captcha/buildflags", + "//brave/vendor/bat-native-ads", "//chrome/browser/notifications", "//third_party/abseil-cpp:absl", "//url", diff --git a/browser/ntp_background_images/android/ntp_background_images_bridge.cc b/browser/ntp_background_images/android/ntp_background_images_bridge.cc index b52a64c212ba..2c0599ea7ffc 100644 --- a/browser/ntp_background_images/android/ntp_background_images_bridge.cc +++ b/browser/ntp_background_images/android/ntp_background_images_bridge.cc @@ -148,9 +148,6 @@ base::android::ScopedJavaLocalRef NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) { JNIEnv* env = AttachCurrentThread(); - const std::string wallpaper_id = base::GenerateGUID(); - view_counter_service_->BrandedWallpaperWillBeDisplayed(wallpaper_id); - auto* image_path = data->FindStringKey(ntp_background_images::kWallpaperImagePathKey); auto* logo_image_path = @@ -171,6 +168,11 @@ NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) { data->FindBoolKey(ntp_background_images::kIsSponsoredKey).value_or(false); auto* creative_instance_id = data->FindStringKey(ntp_background_images::kCreativeInstanceIDKey); + const std::string* wallpaper_id = + data->FindStringKey(ntp_background_images::kWallpaperIDKey); + + view_counter_service_->BrandedWallpaperWillBeDisplayed(wallpaper_id, + creative_instance_id); return Java_NTPBackgroundImagesBridge_createBrandedWallpaper( env, ConvertUTF8ToJavaString(env, *image_path), focal_point_x, @@ -180,7 +182,7 @@ NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) { ConvertUTF8ToJavaString(env, *theme_name), is_sponsored, ConvertUTF8ToJavaString( env, creative_instance_id ? *creative_instance_id : ""), - ConvertUTF8ToJavaString(env, wallpaper_id)); + ConvertUTF8ToJavaString(env, wallpaper_id ? *wallpaper_id : "")); } void NTPBackgroundImagesBridge::GetTopSites( diff --git a/browser/ui/webui/new_tab_page/brave_new_tab_message_handler.cc b/browser/ui/webui/new_tab_page/brave_new_tab_message_handler.cc index a44250161985..e44ee2f09f7f 100644 --- a/browser/ui/webui/new_tab_page/brave_new_tab_message_handler.cc +++ b/browser/ui/webui/new_tab_page/brave_new_tab_message_handler.cc @@ -9,7 +9,6 @@ #include #include "base/cxx17_backports.h" -#include "base/guid.h" #include "base/json/json_writer.h" #include "base/memory/weak_ptr.h" #include "base/metrics/histogram_macros.h" @@ -567,7 +566,7 @@ void BraveNewTabMessageHandler::HandleGetWallpaperData( return; } - auto data = service->GetCurrentWallpaperForDisplay(); + base::Value data = service->GetCurrentWallpaperForDisplay(); if (!data.is_dict()) { ResolveJavascriptCallback(args[0], std::move(wallpaper)); @@ -585,11 +584,14 @@ void BraveNewTabMessageHandler::HandleGetWallpaperData( return; } + const std::string* creative_instance_id = + data.FindStringKey(ntp_background_images::kCreativeInstanceIDKey); + const std::string* wallpaper_id = + data.FindStringKey(ntp_background_images::kWallpaperIDKey); + service->BrandedWallpaperWillBeDisplayed(wallpaper_id, creative_instance_id); + constexpr char kBrandedWallpaperKey[] = "brandedWallpaper"; - const std::string wallpaper_id = base::GenerateGUID(); - data.SetStringKey(ntp_background_images::kWallpaperIDKey, wallpaper_id); wallpaper.SetKey(kBrandedWallpaperKey, std::move(data)); - service->BrandedWallpaperWillBeDisplayed(wallpaper_id); ResolveJavascriptCallback(args[0], std::move(wallpaper)); } diff --git a/components/brave_ads/browser/ads_service.h b/components/brave_ads/browser/ads_service.h index 31e4d0c40d5f..481232989b7b 100644 --- a/components/brave_ads/browser/ads_service.h +++ b/components/brave_ads/browser/ads_service.h @@ -13,16 +13,13 @@ #include "base/observer_list.h" #include "brave/components/brave_adaptive_captcha/buildflags/buildflags.h" #include "brave/components/brave_ads/browser/ads_service_observer.h" +#include "brave/vendor/bat-native-ads/include/bat/ads/new_tab_page_ad_info.h" #include "brave/vendor/bat-native-ads/include/bat/ads/public/interfaces/ads.mojom.h" #include "build/build_config.h" #include "components/keyed_service/core/keyed_service.h" #include "components/sessions/core/session_id.h" #include "url/gurl.h" -namespace ads { -struct AdsHistoryInfo; -} - namespace base { class DictionaryValue; class ListValue; @@ -141,8 +138,10 @@ class AdsService : public KeyedService { const std::string& creative_instance_id, const ads::mojom::InlineContentAdEventType event_type) = 0; - virtual void PurgeOrphanedAdEventsForType( - const ads::mojom::AdType ad_type) = 0; + virtual absl::optional GetPrefetchedNewTabPageAd() = 0; + + virtual void PurgeOrphanedAdEventsForType(const ads::mojom::AdType ad_type, + base::OnceClosure callback) = 0; virtual void GetAdsHistory(const double from_timestamp, const double to_timestamp, diff --git a/components/ntp_background_images/browser/DEPS b/components/ntp_background_images/browser/DEPS index 4ce913506708..57d41c5236e7 100644 --- a/components/ntp_background_images/browser/DEPS +++ b/components/ntp_background_images/browser/DEPS @@ -1,6 +1,5 @@ include_rules = [ - "+bat/ads/pref_names.h", - "+bat/ads/public", + "+bat/ads", "+content/public/browser", "+content/public/common", "+third_party/skia", diff --git a/components/ntp_background_images/browser/ntp_sponsored_images_data.cc b/components/ntp_background_images/browser/ntp_sponsored_images_data.cc index c02caa4cff7d..1c148f35b2ed 100644 --- a/components/ntp_background_images/browser/ntp_sponsored_images_data.cc +++ b/components/ntp_background_images/browser/ntp_sponsored_images_data.cc @@ -7,16 +7,19 @@ #include +#include "base/guid.h" #include "base/json/json_reader.h" #include "base/logging.h" #include "base/notreached.h" #include "base/strings/stringprintf.h" #include "brave/components/ntp_background_images/browser/url_constants.h" +#include "brave/vendor/bat-native-ads/include/bat/ads/new_tab_page_ad_info.h" #include "content/public/common/url_constants.h" /* Sample photo.json. { "schemaVersion": 1, + "campaignId": "fb7ee174-5430-4fb9-8e97-29bf14e8d828", "logo": { "imageUrl": "logo.png", "alt": "Visit Brave Software", @@ -196,6 +199,10 @@ Campaign NTPSponsoredImagesData::GetCampaignFromValue( Campaign campaign; + if (const std::string* campaign_id = value.FindStringKey(kCampaignIdKey)) { + campaign.campaign_id = *campaign_id; + } + Logo default_logo; if (auto* logo = value.FindDictKey(kLogoKey)) { default_logo = GetLogoFromValue(installed_dir, url_prefix, logo); @@ -287,14 +294,15 @@ base::Value NTPSponsoredImagesData::GetBackgroundAt(size_t campaign_index, DCHECK(campaign_index < campaigns.size() && background_index >= 0 && background_index < campaigns[campaign_index].backgrounds.size()); - base::Value data(base::Value::Type::DICTIONARY); const auto campaign = campaigns[campaign_index]; if (!campaign.IsValid()) - return data; + return base::Value(); + base::Value data(base::Value::Type::DICTIONARY); data.SetStringKey(kThemeNameKey, theme_name); data.SetBoolKey(kIsSponsoredKey, !IsSuperReferral()); data.SetBoolKey(kIsBackgroundKey, false); + data.SetStringKey(kWallpaperIDKey, base::GenerateGUID()); const auto background_file_path = campaign.backgrounds[background_index].image_file; @@ -324,6 +332,49 @@ base::Value NTPSponsoredImagesData::GetBackgroundAt(size_t campaign_index, return data; } +base::Value NTPSponsoredImagesData::GetBackgroundByAdInfo( + const ads::NewTabPageAdInfo& ad_info) { + // Find campaign + size_t campaign_index = 0; + for (; campaign_index != campaigns.size(); ++campaign_index) { + if (campaigns[campaign_index].campaign_id == ad_info.campaign_id) { + break; + } + } + if (campaign_index == campaigns.size()) { + LOG(ERROR) << "Ad campaign wasn't found in NTP sposored images data: " + << ad_info.campaign_id; + return base::Value(); + } + + const auto& sponsored_backgrounds = campaigns[campaign_index].backgrounds; + size_t background_index = 0; + for (; background_index != sponsored_backgrounds.size(); ++background_index) { + if (sponsored_backgrounds[background_index].creative_instance_id == + ad_info.creative_instance_id) { + break; + } + } + if (background_index == sponsored_backgrounds.size()) { + LOG(ERROR) << "Creative instance wasn't found in NTP sposored images data: " + << ad_info.creative_instance_id; + return base::Value(); + } + + if (!AdInfoMatchesSponsoredImage(ad_info, campaign_index, background_index)) { + LOG(WARNING) << "Served creative info does not fully match with NTP " + "sponsored images metadata. Campaign id: " + << ad_info.campaign_id + << ". Creative instance id: " << ad_info.creative_instance_id; + } + + base::Value data = GetBackgroundAt(campaign_index, background_index); + if (data.is_dict()) { + data.SetStringKey(kWallpaperIDKey, ad_info.uuid); + } + return data; +} + void NTPSponsoredImagesData::PrintCampaignsParsingResult() const { VLOG(2) << __func__ << ": This is " << (IsSuperReferral() ? " NTP SR Data" : " NTP SI Data"); @@ -339,4 +390,57 @@ void NTPSponsoredImagesData::PrintCampaignsParsingResult() const { } } +bool NTPSponsoredImagesData::AdInfoMatchesSponsoredImage( + const ads::NewTabPageAdInfo& ad_info, + size_t campaign_index, + size_t background_index) const { + DCHECK(campaign_index < campaigns.size() && background_index >= 0 && + background_index < campaigns[campaign_index].backgrounds.size()); + + const Campaign& campaign = campaigns[campaign_index]; + if (!campaign.IsValid()) { + return false; + } + + if (ad_info.campaign_id != campaign.campaign_id) { + return false; + } + + const SponsoredBackground& background = + campaign.backgrounds[background_index]; + if (ad_info.creative_instance_id != background.creative_instance_id) { + return false; + } + + if (ad_info.target_url != background.logo.destination_url) { + return false; + } + + if (base::FilePath::FromUTF8Unsafe(ad_info.image_url).BaseName() != + background.logo.image_file.BaseName()) { + return false; + } + + if (ad_info.alt != background.logo.alt_text) { + return false; + } + + if (ad_info.company_name != background.logo.company_name) { + return false; + } + + const auto it = std::find_if( + ad_info.wallpapers.begin(), ad_info.wallpapers.end(), + [&background](const auto& wallpaper_info) { + if (base::FilePath::FromUTF8Unsafe(wallpaper_info.image_url) + .BaseName() != background.image_file.BaseName()) { + return false; + } + return wallpaper_info.focal_point.x == background.focal_point.x() && + wallpaper_info.focal_point.y == background.focal_point.y(); + }); + + return it != ad_info.wallpapers.end(); +} + } // namespace ntp_background_images diff --git a/components/ntp_background_images/browser/ntp_sponsored_images_data.h b/components/ntp_background_images/browser/ntp_sponsored_images_data.h index 65b106c5ac81..26b4c844b707 100644 --- a/components/ntp_background_images/browser/ntp_sponsored_images_data.h +++ b/components/ntp_background_images/browser/ntp_sponsored_images_data.h @@ -16,6 +16,10 @@ #include "ui/gfx/geometry/point.h" #include "ui/gfx/geometry/rect.h" +namespace ads { +struct NewTabPageAdInfo; +} // namespace ads + namespace ntp_background_images { struct TopSite { @@ -78,6 +82,7 @@ struct Campaign { bool IsValid() const; + std::string campaign_id; std::vector backgrounds; }; @@ -103,10 +108,15 @@ struct NTPSponsoredImagesData { const base::FilePath& installed_dir); base::Value GetBackgroundAt(size_t campaign_index, size_t background_index); + base::Value GetBackgroundByAdInfo(const ads::NewTabPageAdInfo& ad_info); bool IsSuperReferral() const; void PrintCampaignsParsingResult() const; + bool AdInfoMatchesSponsoredImage(const ads::NewTabPageAdInfo& ad_info, + size_t campaign_index, + size_t background_index) const; + std::string url_prefix; std::vector campaigns; diff --git a/components/ntp_background_images/browser/url_constants.h b/components/ntp_background_images/browser/url_constants.h index 29d02f3e262b..a90c16069575 100644 --- a/components/ntp_background_images/browser/url_constants.h +++ b/components/ntp_background_images/browser/url_constants.h @@ -27,6 +27,8 @@ constexpr char kThemeNameKey[] = "themeName"; constexpr char kLogoKey[] = "logo"; +constexpr char kCampaignIdKey[] = "campaignId"; + constexpr char kWallpapersKey[] = "wallpapers"; constexpr char kWallpaperFocalPointKey[] = "focalPoint"; diff --git a/components/ntp_background_images/browser/view_counter_model.cc b/components/ntp_background_images/browser/view_counter_model.cc index 85bef0f58db1..73a0c68ffef6 100644 --- a/components/ntp_background_images/browser/view_counter_model.cc +++ b/components/ntp_background_images/browser/view_counter_model.cc @@ -109,6 +109,19 @@ void ViewCounterModel::RegisterPageViewForBackgroundImages() { current_wallpaper_image_index_ %= total_image_count_; } +void ViewCounterModel::IncreaseBackgroundWallpaperImageIndex() { + // NTP BI component is not ready. + if (total_image_count_ == 0) + return; + + if (!show_wallpaper_) + return; + + // Increase background image index + current_wallpaper_image_index_++; + current_wallpaper_image_index_ %= total_image_count_; +} + void ViewCounterModel::Reset() { current_wallpaper_image_index_ = 0; total_image_count_ = 0; diff --git a/components/ntp_background_images/browser/view_counter_model.h b/components/ntp_background_images/browser/view_counter_model.h index 9e1aa9558804..362f5e0fe3a5 100644 --- a/components/ntp_background_images/browser/view_counter_model.h +++ b/components/ntp_background_images/browser/view_counter_model.h @@ -44,6 +44,7 @@ class ViewCounterModel { bool ShouldShowBrandedWallpaper() const; void RegisterPageView(); void Reset(); + void IncreaseBackgroundWallpaperImageIndex(); private: static const int kInitialCountToBrandedWallpaper = 1; diff --git a/components/ntp_background_images/browser/view_counter_service.cc b/components/ntp_background_images/browser/view_counter_service.cc index 383855ef6596..64a601e65de5 100644 --- a/components/ntp_background_images/browser/view_counter_service.cc +++ b/components/ntp_background_images/browser/view_counter_service.cc @@ -109,15 +109,12 @@ ViewCounterService::ViewCounterService( ViewCounterService::~ViewCounterService() = default; void ViewCounterService::BrandedWallpaperWillBeDisplayed( - const std::string& wallpaper_id) { + const std::string* wallpaper_id, + const std::string* creative_instance_id) { if (ads_service_) { - base::Value data = ViewCounterService::GetCurrentWallpaperForDisplay(); - DCHECK(!data.is_none()); - - const std::string* creative_instance_id = - data.FindStringKey(kCreativeInstanceIDKey); ads_service_->OnNewTabPageAdEvent( - wallpaper_id, creative_instance_id ? *creative_instance_id : "", + wallpaper_id ? *wallpaper_id : "", + creative_instance_id ? *creative_instance_id : "", ads::mojom::NewTabPageAdEventType::kViewed); } @@ -138,12 +135,20 @@ NTPSponsoredImagesData* ViewCounterService::GetCurrentBrandedWallpaperData() return service_->GetBrandedImagesData(false); } -base::Value ViewCounterService::GetCurrentWallpaperForDisplay() const { +base::Value ViewCounterService::GetCurrentWallpaperForDisplay() { if (ShouldShowBrandedWallpaper()) { - return GetCurrentBrandedWallpaper(); - } else { - return GetCurrentWallpaper(); + base::Value branded_wallpaper = GetCurrentBrandedWallpaper(); + if (branded_wallpaper.is_dict()) { + return branded_wallpaper; + } + // Failed to get branded wallpaper as it was frequency capped by ads + // service. In this case next background wallpaper should be shown on NTP. + // To do this we need to increment background wallpaper index as it wasn't + // incremented during the last RegisterPageView() call. + model_.IncreaseBackgroundWallpaperImageIndex(); } + + return GetCurrentWallpaper(); } base::Value ViewCounterService::GetCurrentWallpaper() const { @@ -160,16 +165,39 @@ base::Value ViewCounterService::GetCurrentWallpaper() const { } base::Value ViewCounterService::GetCurrentBrandedWallpaper() const { - if (GetCurrentBrandedWallpaperData()) { - size_t current_campaign_index; - size_t current_background_index; - std::tie(current_campaign_index, current_background_index) = - model_.GetCurrentBrandedImageIndex(); - return GetCurrentBrandedWallpaperData()->GetBackgroundAt( - current_campaign_index, current_background_index); + NTPSponsoredImagesData* images_data = GetCurrentBrandedWallpaperData(); + if (!images_data) { + return base::Value(); + } + + const bool should_ads_frequency_cap = + ads_service_ && ads_service_->IsEnabled(); + if (should_ads_frequency_cap && !images_data->IsSuperReferral()) { + return GetCurrentBrandedWallpaperByAdInfo(); } - return base::Value(); + return GetCurrentBrandedWallpaperFromModel(); +} + +base::Value ViewCounterService::GetCurrentBrandedWallpaperByAdInfo() const { + DCHECK(ads_service_); + + absl::optional ad_info = + ads_service_->GetPrefetchedNewTabPageAd(); + if (!ad_info) { + return base::Value(); + } + + return GetCurrentBrandedWallpaperData()->GetBackgroundByAdInfo(*ad_info); +} + +base::Value ViewCounterService::GetCurrentBrandedWallpaperFromModel() const { + size_t current_campaign_index; + size_t current_background_index; + std::tie(current_campaign_index, current_background_index) = + model_.GetCurrentBrandedImageIndex(); + return GetCurrentBrandedWallpaperData()->GetBackgroundAt( + current_campaign_index, current_background_index); } std::vector ViewCounterService::GetTopSitesData() const { diff --git a/components/ntp_background_images/browser/view_counter_service.h b/components/ntp_background_images/browser/view_counter_service.h index f5915d195284..0b7786b5cbd9 100644 --- a/components/ntp_background_images/browser/view_counter_service.h +++ b/components/ntp_background_images/browser/view_counter_service.h @@ -67,16 +67,19 @@ class ViewCounterService : public KeyedService, const std::string& destination_url, const std::string& wallpaper_id); - base::Value GetCurrentWallpaperForDisplay() const; + base::Value GetCurrentWallpaperForDisplay(); base::Value GetCurrentWallpaper() const; base::Value GetCurrentBrandedWallpaper() const; + base::Value GetCurrentBrandedWallpaperByAdInfo() const; + base::Value GetCurrentBrandedWallpaperFromModel() const; std::vector GetTopSitesData() const; bool IsSuperReferral() const; std::string GetSuperReferralThemeName() const; std::string GetSuperReferralCode() const; - void BrandedWallpaperWillBeDisplayed(const std::string& wallpaper_id); + void BrandedWallpaperWillBeDisplayed(const std::string* wallpaper_id, + const std::string* creative_instance_id); NTPBackgroundImagesData* GetCurrentWallpaperData() const; // Gets the current data for branded wallpaper, if there diff --git a/components/services/bat_ads/bat_ads_impl.cc b/components/services/bat_ads/bat_ads_impl.cc index 66193a447e79..08a35c3ee6de 100644 --- a/components/services/bat_ads/bat_ads_impl.cc +++ b/components/services/bat_ads/bat_ads_impl.cc @@ -16,6 +16,7 @@ #include "bat/ads/category_content_info.h" #include "bat/ads/confirmation_type.h" #include "bat/ads/inline_content_ad_info.h" +#include "bat/ads/new_tab_page_ad_info.h" #include "brave/components/services/bat_ads/bat_ads_client_mojo_bridge.h" using std::placeholders::_1; @@ -138,6 +139,15 @@ void BatAdsImpl::OnAdNotificationEvent( ads_->OnAdNotificationEvent(uuid, event_type); } +void BatAdsImpl::GetNewTabPageAd(GetNewTabPageAdCallback callback) { + auto* holder = new CallbackHolder( + AsWeakPtr(), std::move(callback)); + + auto get_new_tab_page_ad_callback = + std::bind(BatAdsImpl::OnGetNewTabPageAd, holder, _1, _2); + ads_->GetNewTabPageAd(get_new_tab_page_ad_callback); +} + void BatAdsImpl::OnNewTabPageAdEvent( const std::string& uuid, const std::string& creative_instance_id, @@ -170,8 +180,16 @@ void BatAdsImpl::OnInlineContentAdEvent( } void BatAdsImpl::PurgeOrphanedAdEventsForType( - const ads::mojom::AdType ad_type) { - ads_->PurgeOrphanedAdEventsForType(ad_type); + const ads::mojom::AdType ad_type, + PurgeOrphanedAdEventsForTypeCallback callback) { + auto* holder = new CallbackHolder( + AsWeakPtr(), std::move(callback)); + + auto purge_ad_events_for_type_callback = + std::bind(BatAdsImpl::OnPurgeOrphanedAdEventsForType, holder); + + ads_->PurgeOrphanedAdEventsForType(ad_type, + purge_ad_events_for_type_callback); } void BatAdsImpl::RemoveAllHistory( @@ -294,6 +312,19 @@ void BatAdsImpl::OnShutdown(CallbackHolder* holder, delete holder; } +// static +void BatAdsImpl::OnGetNewTabPageAd( + CallbackHolder* holder, + const bool success, + const ads::NewTabPageAdInfo& ad) { + DCHECK(holder); + if (holder->is_valid()) { + std::move(holder->get()).Run(success, ad.ToJson()); + } + + delete holder; +} + void BatAdsImpl::OnGetInlineContentAd( CallbackHolder* holder, const bool success, @@ -306,6 +337,15 @@ void BatAdsImpl::OnGetInlineContentAd( delete holder; } +void BatAdsImpl::OnPurgeOrphanedAdEventsForType( + CallbackHolder* holder) { + if (holder->is_valid()) { + std::move(holder->get()).Run(); + } + + delete holder; +} + void BatAdsImpl::OnRemoveAllHistory( CallbackHolder* holder, const bool success) { diff --git a/components/services/bat_ads/bat_ads_impl.h b/components/services/bat_ads/bat_ads_impl.h index c730915db863..31c91b9d4cd2 100644 --- a/components/services/bat_ads/bat_ads_impl.h +++ b/components/services/bat_ads/bat_ads_impl.h @@ -86,6 +86,8 @@ class BatAdsImpl : const std::string& uuid, const ads::mojom::AdNotificationEventType event_type) override; + void GetNewTabPageAd(GetNewTabPageAdCallback callback) override; + void OnNewTabPageAdEvent( const std::string& uuid, const std::string& creative_instance_id, @@ -104,7 +106,9 @@ class BatAdsImpl : const std::string& creative_instance_id, const ads::mojom::InlineContentAdEventType event_type) override; - void PurgeOrphanedAdEventsForType(const ads::mojom::AdType ad_type) override; + void PurgeOrphanedAdEventsForType( + const ads::mojom::AdType ad_type, + PurgeOrphanedAdEventsForTypeCallback callback) override; void RemoveAllHistory( RemoveAllHistoryCallback callback) override; @@ -170,12 +174,20 @@ class BatAdsImpl : static void OnShutdown(CallbackHolder* holder, const bool success); + static void OnGetNewTabPageAd( + CallbackHolder* holder, + const bool success, + const ads::NewTabPageAdInfo& ad); + static void OnGetInlineContentAd( CallbackHolder* holder, const bool success, const std::string& dimensions, const ads::InlineContentAdInfo& ad); + static void OnPurgeOrphanedAdEventsForType( + CallbackHolder* holder); + static void OnRemoveAllHistory( CallbackHolder* holder, const bool success); diff --git a/components/services/bat_ads/public/interfaces/bat_ads.mojom b/components/services/bat_ads/public/interfaces/bat_ads.mojom index 1de1de0b6b46..2f090bee0e99 100644 --- a/components/services/bat_ads/public/interfaces/bat_ads.mojom +++ b/components/services/bat_ads/public/interfaces/bat_ads.mojom @@ -87,11 +87,12 @@ interface BatAds { OnTabClosed(int32 tab_id); GetAdNotification(string uuid) => (string json); OnAdNotificationEvent(string uuid, ads.mojom.AdNotificationEventType event_type); + GetNewTabPageAd() => (bool success, string json); OnNewTabPageAdEvent(string uuid, string creative_instance_id, ads.mojom.NewTabPageAdEventType event_type); OnPromotedContentAdEvent(string uuid, string creative_instance_id, ads.mojom.PromotedContentAdEventType event_type); GetInlineContentAd(string size) => (bool success, string dimensions, string ad); OnInlineContentAdEvent(string uuid, string creative_instance_id, ads.mojom.InlineContentAdEventType event_type); - PurgeOrphanedAdEventsForType(ads.mojom.AdType ad_type); + PurgeOrphanedAdEventsForType(ads.mojom.AdType ad_type) => (); RemoveAllHistory() => (bool success); OnWalletUpdated(string payment_id, string seed); GetAdsHistory(double from_timestamp, double to_timestamp) => (string json); diff --git a/ios/browser/api/ads/brave_ads.mm b/ios/browser/api/ads/brave_ads.mm index a12d2e0c8782..06540fc15ab1 100644 --- a/ios/browser/api/ads/brave_ads.mm +++ b/ios/browser/api/ads/brave_ads.mm @@ -655,7 +655,8 @@ - (void)purgeOrphanedAdEvents:(AdsAdType)adType { if (![self isAdsServiceRunning]) { return; } - ads->PurgeOrphanedAdEventsForType(static_cast(adType)); + ads->PurgeOrphanedAdEventsForType(static_cast(adType), ^{ + }); } - (void)detailsForCurrentCycle:(void (^)(NSInteger adsReceived, diff --git a/vendor/bat-native-ads/include/bat/ads/ads.h b/vendor/bat-native-ads/include/bat/ads/ads.h index 770f05e3d542..532e748fccd6 100644 --- a/vendor/bat-native-ads/include/bat/ads/ads.h +++ b/vendor/bat-native-ads/include/bat/ads/ads.h @@ -176,7 +176,9 @@ class ADS_EXPORT Ads { const mojom::InlineContentAdEventType event_type) = 0; // Purge orphaned ad events for the specified |ad_type| - virtual void PurgeOrphanedAdEventsForType(const mojom::AdType ad_type) = 0; + virtual void PurgeOrphanedAdEventsForType( + const mojom::AdType ad_type, + PurgeOrphanedAdEventsForTypeCallback callback) = 0; // Should be called to remove all cached history. The callback takes one // argument - |bool| should be set to |true| if successful otherwise should be diff --git a/vendor/bat-native-ads/include/bat/ads/ads_aliases.h b/vendor/bat-native-ads/include/bat/ads/ads_aliases.h index 9e0d78be92e1..7846ef8c54a2 100644 --- a/vendor/bat-native-ads/include/bat/ads/ads_aliases.h +++ b/vendor/bat-native-ads/include/bat/ads/ads_aliases.h @@ -32,6 +32,8 @@ using GetAccountStatementCallback = using GetAdDiagnosticsCallback = std::function; +using PurgeOrphanedAdEventsForTypeCallback = std::function; + } // namespace ads #endif // BRAVE_VENDOR_BAT_NATIVE_ADS_INCLUDE_BAT_ADS_ADS_ALIASES_H_ 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 134a4e811bdc..f3999937de36 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 @@ -396,14 +396,18 @@ void AdsImpl::OnInlineContentAdEvent( inline_content_ad_->FireEvent(uuid, creative_instance_id, event_type); } -void AdsImpl::PurgeOrphanedAdEventsForType(const mojom::AdType ad_type) { - PurgeOrphanedAdEvents(ad_type, [ad_type](const bool success) { +void AdsImpl::PurgeOrphanedAdEventsForType( + const mojom::AdType ad_type, + PurgeOrphanedAdEventsForTypeCallback callback) { + PurgeOrphanedAdEvents(ad_type, [ad_type, callback](const bool success) { if (!success) { BLOG(0, "Failed to purge orphaned ad events for " << ad_type); return; } BLOG(1, "Successfully purged orphaned ad events for " << ad_type); + + callback(); }); } 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 5573aa0553db..cacacf828440 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 @@ -191,7 +191,9 @@ class AdsImpl final : public Ads, const std::string& creative_instance_id, const mojom::InlineContentAdEventType event_type) override; - void PurgeOrphanedAdEventsForType(const mojom::AdType ad_type) override; + void PurgeOrphanedAdEventsForType( + const mojom::AdType ad_type, + PurgeOrphanedAdEventsForTypeCallback callback) override; void RemoveAllHistory(RemoveAllHistoryCallback callback) override;