Skip to content

Commit f98053f

Browse files
committed
Handle SI by bat-native-ads library.
1 parent a7d384b commit f98053f

File tree

17 files changed

+247
-46
lines changed

17 files changed

+247
-46
lines changed

browser/brave_ads/ads_service_impl.cc

+29
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "bat/ads/ads.h"
3838
#include "bat/ads/ads_history_info.h"
3939
#include "bat/ads/inline_content_ad_info.h"
40+
#include "bat/ads/new_tab_page_ad_info.h"
4041
#include "bat/ads/pref_names.h"
4142
#include "bat/ads/resources/grit/bat_ads_resources.h"
4243
#include "bat/ads/statement_info.h"
@@ -1107,6 +1108,16 @@ void AdsServiceImpl::OnOpenNewTabWithAd(const std::string& json) {
11071108
OpenNewTabWithUrl(notification.target_url);
11081109
}
11091110

1111+
void AdsServiceImpl::GetNewTabPageAd(OnGetNewTabPageAdCallback callback) {
1112+
if (!connected()) {
1113+
std::move(callback).Run(false, base::DictionaryValue());
1114+
return;
1115+
}
1116+
1117+
bat_ads_->GetNewTabPageAd(base::BindOnce(&AdsServiceImpl::OnGetNewTabPageAd,
1118+
AsWeakPtr(), std::move(callback)));
1119+
}
1120+
11101121
void AdsServiceImpl::OnNewTabPageAdEvent(
11111122
const std::string& uuid,
11121123
const std::string& creative_instance_id,
@@ -1267,6 +1278,24 @@ void AdsServiceImpl::OnURLRequestComplete(
12671278
callback(url_response);
12681279
}
12691280

1281+
void AdsServiceImpl::OnGetNewTabPageAd(OnGetNewTabPageAdCallback callback,
1282+
bool success,
1283+
const std::string& json) {
1284+
if (!success) {
1285+
std::move(callback).Run(false, base::DictionaryValue());
1286+
return;
1287+
}
1288+
1289+
ads::NewTabPageAdInfo ad_info;
1290+
if (!ad_info.FromJson(json)) {
1291+
std::move(callback).Run(false, base::DictionaryValue());
1292+
return;
1293+
}
1294+
1295+
base::DictionaryValue ad_info_dictionary = ad_info.ToValue();
1296+
std::move(callback).Run(true, std::move(ad_info_dictionary));
1297+
}
1298+
12701299
void AdsServiceImpl::OnGetInlineContentAd(OnGetInlineContentAdCallback callback,
12711300
const bool success,
12721301
const std::string& dimensions,

browser/brave_ads/ads_service_impl.h

+6
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ class AdsServiceImpl : public AdsService,
145145

146146
void OnResourceComponentUpdated(const std::string& id) override;
147147

148+
void GetNewTabPageAd(OnGetNewTabPageAdCallback callback) override;
149+
148150
void OnNewTabPageAdEvent(
149151
const std::string& uuid,
150152
const std::string& creative_instance_id,
@@ -264,6 +266,10 @@ class AdsServiceImpl : public AdsService,
264266

265267
void OnGetBraveWallet(ledger::type::BraveWalletPtr wallet);
266268

269+
void OnGetNewTabPageAd(OnGetNewTabPageAdCallback callback,
270+
bool success,
271+
const std::string& json);
272+
267273
void OnGetInlineContentAd(OnGetInlineContentAdCallback callback,
268274
const bool success,
269275
const std::string& dimensions,

browser/ntp_background_images/android/ntp_background_images_bridge.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,6 @@ base::android::ScopedJavaLocalRef<jobject>
149149
NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) {
150150
JNIEnv* env = AttachCurrentThread();
151151

152-
// TODO(tmancey): HERE
153-
const std::string wallpaper_id = base::GenerateGUID();
154-
view_counter_service_->BrandedWallpaperWillBeDisplayed(wallpaper_id);
155-
156152
auto* image_path =
157153
data->FindStringKey(ntp_background_images::kWallpaperImagePathKey);
158154
auto* logo_image_path =
@@ -174,6 +170,11 @@ NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) {
174170
auto* creative_instance_id =
175171
data->FindStringKey(ntp_background_images::kCreativeInstanceIDKey);
176172

173+
// TODO(tmancey): HERE
174+
const std::string wallpaper_id = base::GenerateGUID();
175+
view_counter_service_->BrandedWallpaperWillBeDisplayed(wallpaper_id,
176+
creative_instance_id);
177+
177178
return Java_NTPBackgroundImagesBridge_createBrandedWallpaper(
178179
env, ConvertUTF8ToJavaString(env, *image_path), focal_point_x,
179180
focal_point_y, ConvertUTF8ToJavaString(env, *logo_image_path),

browser/ui/webui/new_tab_page/brave_new_tab_message_handler.cc

+41-26
Original file line numberDiff line numberDiff line change
@@ -565,40 +565,20 @@ void BraveNewTabMessageHandler::HandleGetWallpaperData(
565565
base::Value::ConstListView args) {
566566
AllowJavascript();
567567

568+
base::Value callback_id = args[0].Clone();
569+
568570
auto* service = ViewCounterServiceFactory::GetForProfile(profile_);
569571
base::Value wallpaper(base::Value::Type::DICTIONARY);
570572

571573
if (!service) {
572-
ResolveJavascriptCallback(args[0], std::move(wallpaper));
574+
ResolveJavascriptCallback(callback_id, std::move(wallpaper));
573575
return;
574576
}
575577

576578
// TODO(tmancey): HERE (For display on desktop)
577-
auto data = service->GetCurrentWallpaperForDisplay();
578-
579-
if (!data.is_dict()) {
580-
ResolveJavascriptCallback(args[0], std::move(wallpaper));
581-
return;
582-
}
583-
584-
const auto is_background =
585-
data.FindBoolKey(ntp_background_images::kIsBackgroundKey);
586-
DCHECK(is_background);
587-
588-
if (is_background.value()) {
589-
constexpr char kBackgroundWallpaperKey[] = "backgroundWallpaper";
590-
wallpaper.SetKey(kBackgroundWallpaperKey, std::move(data));
591-
ResolveJavascriptCallback(args[0], std::move(wallpaper));
592-
return;
593-
}
594-
595-
// TODO(tmancey): HERE
596-
constexpr char kBrandedWallpaperKey[] = "brandedWallpaper";
597-
const std::string wallpaper_id = base::GenerateGUID();
598-
data.SetStringKey(ntp_background_images::kWallpaperIDKey, wallpaper_id);
599-
wallpaper.SetKey(kBrandedWallpaperKey, std::move(data));
600-
service->BrandedWallpaperWillBeDisplayed(wallpaper_id);
601-
ResolveJavascriptCallback(args[0], std::move(wallpaper));
579+
service->GetCurrentWallpaperForDisplay(base::BindOnce(
580+
&BraveNewTabMessageHandler::OnGetCurrentWallpaperForDisplay,
581+
weak_ptr_factory_.GetWeakPtr(), std::move(callback_id)));
602582
}
603583

604584
void BraveNewTabMessageHandler::HandleCustomizeClicked(
@@ -627,6 +607,41 @@ void BraveNewTabMessageHandler::OnPreferencesChanged() {
627607
FireWebUIListener("preferences-changed", data);
628608
}
629609

610+
void BraveNewTabMessageHandler::OnGetCurrentWallpaperForDisplay(
611+
base::Value callback_id, base::Value data) {
612+
AllowJavascript();
613+
614+
auto* service = ViewCounterServiceFactory::GetForProfile(profile_);
615+
base::Value wallpaper(base::Value::Type::DICTIONARY);
616+
617+
if (!data.is_dict()) {
618+
ResolveJavascriptCallback(callback_id, std::move(wallpaper));
619+
return;
620+
}
621+
622+
const auto is_background =
623+
data.FindBoolKey(ntp_background_images::kIsBackgroundKey);
624+
DCHECK(is_background);
625+
626+
if (is_background.value()) {
627+
constexpr char kBackgroundWallpaperKey[] = "backgroundWallpaper";
628+
wallpaper.SetKey(kBackgroundWallpaperKey, std::move(data));
629+
ResolveJavascriptCallback(callback_id, std::move(wallpaper));
630+
return;
631+
}
632+
633+
// TODO(tmancey): HERE
634+
const std::string* creative_instance_id =
635+
data.FindStringKey(ntp_background_images::kCreativeInstanceIDKey);
636+
const std::string wallpaper_id = base::GenerateGUID();
637+
service->BrandedWallpaperWillBeDisplayed(wallpaper_id, creative_instance_id);
638+
639+
constexpr char kBrandedWallpaperKey[] = "brandedWallpaper";
640+
data.SetStringKey(ntp_background_images::kWallpaperIDKey, wallpaper_id);
641+
wallpaper.SetKey(kBrandedWallpaperKey, std::move(data));
642+
ResolveJavascriptCallback(callback_id, std::move(wallpaper));
643+
}
644+
630645
void BraveNewTabMessageHandler::OnTorCircuitEstablished(bool result) {
631646
auto data = GetTorPropertiesDictionary(result, "");
632647
FireWebUIListener("tor-tab-data-updated", data);

browser/ui/webui/new_tab_page/brave_new_tab_message_handler.h

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <string>
1010

1111
#include "base/memory/weak_ptr.h"
12+
#include "base/values.h"
1213
#include "brave/components/tor/buildflags/buildflags.h"
1314
#include "brave/components/tor/tor_launcher_observer.h"
1415
#include "components/prefs/pref_change_registrar.h"
@@ -67,6 +68,8 @@ class BraveNewTabMessageHandler : public content::WebUIMessageHandler,
6768
void OnStatsChanged();
6869
void OnPreferencesChanged();
6970
void OnPrivatePropertiesChanged();
71+
void OnGetCurrentWallpaperForDisplay(base::Value callback_id,
72+
base::Value data);
7073

7174
// TorLauncherObserver:
7275
void OnTorCircuitEstablished(bool result) override;

components/brave_ads/browser/ads_service.h

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ using OnToggleFlaggedAdCallback = base::OnceCallback<void(const std::string&)>;
5454
using OnGetInlineContentAdCallback = base::OnceCallback<
5555
void(const bool, const std::string&, const base::DictionaryValue&)>;
5656

57+
using OnGetNewTabPageAdCallback =
58+
base::OnceCallback<void(const bool, const base::DictionaryValue&)>;
59+
5760
using GetAccountStatementCallback = base::OnceCallback<
5861
void(const bool, const double, const int, const double, const double)>;
5962

@@ -142,6 +145,8 @@ class AdsService : public KeyedService {
142145
const std::string& creative_instance_id,
143146
const ads::mojom::InlineContentAdEventType event_type) = 0;
144147

148+
virtual void GetNewTabPageAd(OnGetNewTabPageAdCallback callback) = 0;
149+
145150
virtual void PurgeOrphanedAdEventsForType(
146151
const ads::mojom::AdType ad_type) = 0;
147152

components/brave_ads/common/features.cc

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ const base::Feature kAllowedToFallbackToCustomAdNotifications{
2323
const base::Feature kRequestAdsEnabledApi{"RequestAdsEnabledApi",
2424
base::FEATURE_DISABLED_BY_DEFAULT};
2525

26+
const base::Feature kNewTabPageSponsoredImageAds{
27+
"NewTabPageSponsoredImageAds", base::FEATURE_ENABLED_BY_DEFAULT};
28+
2629
namespace {
2730

2831
// Set to true to support multiple displays or false to only support the primary

components/brave_ads/common/features.h

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ bool IsAllowedToFallbackToCustomAdNotificationsEnabled();
4141
extern const base::Feature kRequestAdsEnabledApi;
4242
bool IsRequestAdsEnabledApiEnabled();
4343

44+
extern const base::Feature kNewTabPageSponsoredImageAds;
45+
4446
} // namespace features
4547
} // namespace brave_ads
4648

components/ntp_background_images/browser/ntp_sponsored_images_data.cc

+5
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ base::Value NTPSponsoredImagesData::GetBackgroundAt(size_t campaign_index,
324324
return data;
325325
}
326326

327+
base::Value NTPSponsoredImagesData::GetBackgroundByAdInfo(
328+
const base::Value& ad_info) {
329+
return base::Value();
330+
}
331+
327332
void NTPSponsoredImagesData::PrintCampaignsParsingResult() const {
328333
VLOG(2) << __func__ << ": This is "
329334
<< (IsSuperReferral() ? " NTP SR Data" : " NTP SI Data");

components/ntp_background_images/browser/ntp_sponsored_images_data.h

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ struct NTPSponsoredImagesData {
103103
const base::FilePath& installed_dir);
104104

105105
base::Value GetBackgroundAt(size_t campaign_index, size_t background_index);
106+
base::Value GetBackgroundByAdInfo(const base::Value& ad_info);
106107

107108
bool IsSuperReferral() const;
108109
void PrintCampaignsParsingResult() const;

components/ntp_background_images/browser/view_counter_service.cc

+46-13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "bat/ads/pref_names.h"
1919
#include "bat/ads/public/interfaces/ads.mojom.h"
2020
#include "brave/components/brave_ads/browser/ads_service.h"
21+
#include "brave/components/brave_ads/common/features.h"
2122
#include "brave/components/brave_referrals/buildflags/buildflags.h"
2223
#include "brave/components/brave_rewards/common/pref_names.h"
2324
#include "brave/components/ntp_background_images/browser/features.h"
@@ -103,15 +104,16 @@ ViewCounterService::~ViewCounterService() = default;
103104

104105
// TODO(tmancey): HERE
105106
void ViewCounterService::BrandedWallpaperWillBeDisplayed(
106-
const std::string& wallpaper_id) {
107+
const std::string& wallpaper_id,
108+
const std::string* creative_instance_id) {
107109
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());
111113

112114
// 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);
115117
ads_service_->OnNewTabPageAdEvent(
116118
wallpaper_id, creative_instance_id ? *creative_instance_id : "",
117119
ads::mojom::NewTabPageAdEventType::kViewed);
@@ -136,11 +138,13 @@ NTPSponsoredImagesData* ViewCounterService::GetCurrentBrandedWallpaperData()
136138
}
137139

138140
// TODO(tmancey): HERE (Fetches wallpaper for display)
139-
base::Value ViewCounterService::GetCurrentWallpaperForDisplay() const {
141+
void ViewCounterService::GetCurrentWallpaperForDisplay(
142+
GetCurrentWallpaperForDisplayCallback callback) const {
143+
DCHECK(callback);
140144
if (ShouldShowBrandedWallpaper()) {
141-
return GetCurrentBrandedWallpaper();
145+
GetCurrentBrandedWallpaper(std::move(callback));
142146
} else {
143-
return GetCurrentWallpaper();
147+
std::move(callback).Run(GetCurrentWallpaper());
144148
}
145149
}
146150

@@ -154,17 +158,46 @@ base::Value ViewCounterService::GetCurrentWallpaper() const {
154158
}
155159

156160
// 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_) {
159174
size_t current_campaign_index;
160175
size_t current_background_index;
161176
std::tie(current_campaign_index, current_background_index) =
162177
model_.GetCurrentBrandedImageIndex();
163-
return GetCurrentBrandedWallpaperData()->GetBackgroundAt(
178+
base::Value background = GetCurrentBrandedWallpaperData()->GetBackgroundAt(
164179
current_campaign_index, current_background_index);
180+
std::move(callback).Run(std::move(background));
181+
return;
165182
}
166183

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));
168201
}
169202

170203
std::vector<TopSite> ViewCounterService::GetTopSitesData() const {

components/ntp_background_images/browser/view_counter_service.h

+13-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ struct NTPBackgroundImagesData;
3838
struct NTPSponsoredImagesData;
3939
struct TopSite;
4040

41+
using GetCurrentWallpaperForDisplayCallback =
42+
base::OnceCallback<void(base::Value)>;
43+
4144
class ViewCounterService : public KeyedService,
4245
public NTPBackgroundImagesService::Observer {
4346
public:
@@ -63,16 +66,19 @@ class ViewCounterService : public KeyedService,
6366
const std::string& destination_url,
6467
const std::string& wallpaper_id);
6568

66-
base::Value GetCurrentWallpaperForDisplay() const;
69+
void GetCurrentWallpaperForDisplay(
70+
GetCurrentWallpaperForDisplayCallback callback) const;
6771
base::Value GetCurrentWallpaper() const;
68-
base::Value GetCurrentBrandedWallpaper() const;
72+
void GetCurrentBrandedWallpaper(
73+
GetCurrentWallpaperForDisplayCallback callback) const;
6974
std::vector<TopSite> GetTopSitesData() const;
7075

7176
bool IsSuperReferral() const;
7277
std::string GetSuperReferralThemeName() const;
7378
std::string GetSuperReferralCode() const;
7479

75-
void BrandedWallpaperWillBeDisplayed(const std::string& wallpaper_id);
80+
void BrandedWallpaperWillBeDisplayed(const std::string& wallpaper_id,
81+
const std::string* creative_instance_id);
7682

7783
NTPBackgroundImagesData* GetCurrentWallpaperData() const;
7884
// Gets the current data for branded wallpaper, if there
@@ -122,6 +128,10 @@ class ViewCounterService : public KeyedService,
122128
void OnUpdated(NTPSponsoredImagesData* data) override;
123129
void OnSuperReferralEnded() override;
124130

131+
void OnGetNewTabPageAd(GetCurrentWallpaperForDisplayCallback callback,
132+
bool success,
133+
const base::DictionaryValue& ad) const;
134+
125135
void ResetNotificationState();
126136
bool IsSponsoredImagesWallpaperOptedIn() const;
127137
bool IsSuperReferralWallpaperOptedIn() const;

0 commit comments

Comments
 (0)