18
18
#include " bat/ads/pref_names.h"
19
19
#include " bat/ads/public/interfaces/ads.mojom.h"
20
20
#include " brave/components/brave_ads/browser/ads_service.h"
21
+ #include " brave/components/brave_ads/common/features.h"
21
22
#include " brave/components/brave_referrals/buildflags/buildflags.h"
22
23
#include " brave/components/brave_rewards/common/pref_names.h"
23
24
#include " brave/components/ntp_background_images/browser/features.h"
@@ -103,15 +104,16 @@ ViewCounterService::~ViewCounterService() = default;
103
104
104
105
// TODO(tmancey): HERE
105
106
void ViewCounterService::BrandedWallpaperWillBeDisplayed (
106
- const std::string& wallpaper_id) {
107
+ const std::string& wallpaper_id,
108
+ const std::string* creative_instance_id) {
107
109
if (ads_service_) {
108
- // TODO(tmancey): HERE (For triggering ad events)
109
- base::Value data = ViewCounterService::GetCurrentWallpaperForDisplay ();
110
- DCHECK (!data.is_none ());
110
+ // // TODO(tmancey): HERE (For triggering ad events)
111
+ // base::Value data = ViewCounterService::GetCurrentWallpaperForDisplay();
112
+ // DCHECK(!data.is_none());
111
113
112
114
// TODO(tmancey): HERE
113
- const std::string* creative_instance_id =
114
- data.FindStringKey (kCreativeInstanceIDKey );
115
+ // const std::string* creative_instance_id =
116
+ // data.FindStringKey(kCreativeInstanceIDKey);
115
117
ads_service_->OnNewTabPageAdEvent (
116
118
wallpaper_id, creative_instance_id ? *creative_instance_id : " " ,
117
119
ads::mojom::NewTabPageAdEventType::kViewed );
@@ -136,11 +138,13 @@ NTPSponsoredImagesData* ViewCounterService::GetCurrentBrandedWallpaperData()
136
138
}
137
139
138
140
// TODO(tmancey): HERE (Fetches wallpaper for display)
139
- base::Value ViewCounterService::GetCurrentWallpaperForDisplay () const {
141
+ void ViewCounterService::GetCurrentWallpaperForDisplay (
142
+ GetCurrentWallpaperForDisplayCallback callback) const {
143
+ DCHECK (callback);
140
144
if (ShouldShowBrandedWallpaper ()) {
141
- return GetCurrentBrandedWallpaper ();
145
+ GetCurrentBrandedWallpaper (std::move (callback) );
142
146
} else {
143
- return GetCurrentWallpaper ();
147
+ std::move (callback). Run ( GetCurrentWallpaper () );
144
148
}
145
149
}
146
150
@@ -154,17 +158,46 @@ base::Value ViewCounterService::GetCurrentWallpaper() const {
154
158
}
155
159
156
160
// TODO(tmancey): HERE (Get wallpaper for display)
157
- base::Value ViewCounterService::GetCurrentBrandedWallpaper () const {
158
- if (GetCurrentBrandedWallpaperData ()) {
161
+ void ViewCounterService::GetCurrentBrandedWallpaper (
162
+ GetCurrentWallpaperForDisplayCallback callback) const {
163
+ DCHECK (callback);
164
+ NTPSponsoredImagesData* images_data = GetCurrentBrandedWallpaperData ();
165
+ if (!images_data) {
166
+ std::move (callback).Run (base::Value ());
167
+ return ;
168
+ }
169
+
170
+ const bool ntp_ads_enabled = base::FeatureList::IsEnabled (
171
+ brave_ads::features::kNewTabPageSponsoredImageAds );
172
+
173
+ if (!ntp_ads_enabled || images_data->IsSuperReferral () || !ads_service_) {
159
174
size_t current_campaign_index;
160
175
size_t current_background_index;
161
176
std::tie (current_campaign_index, current_background_index) =
162
177
model_.GetCurrentBrandedImageIndex ();
163
- return GetCurrentBrandedWallpaperData ()->GetBackgroundAt (
178
+ base::Value background = GetCurrentBrandedWallpaperData ()->GetBackgroundAt (
164
179
current_campaign_index, current_background_index);
180
+ std::move (callback).Run (std::move (background));
181
+ return ;
165
182
}
166
183
167
- return base::Value ();
184
+ ads_service_->GetNewTabPageAd (
185
+ base::BindOnce (&ViewCounterService::OnGetNewTabPageAd,
186
+ base::Unretained (this ), std::move (callback)));
187
+ }
188
+
189
+ void ViewCounterService::OnGetNewTabPageAd (
190
+ GetCurrentWallpaperForDisplayCallback callback,
191
+ bool success,
192
+ const base::DictionaryValue& ad_info) const {
193
+ if (success) {
194
+ background =
195
+ GetCurrentBrandedWallpaperData ()->GetBackgroundByAdInfo (ad_info);
196
+ }
197
+ if (background.is_none ()) {
198
+ background = GetCurrentWallpaper ();
199
+ }
200
+ std::move (callback).Run (std::move (background));
168
201
}
169
202
170
203
std::vector<TopSite> ViewCounterService::GetTopSitesData () const {
0 commit comments