Skip to content

Commit 3a4b39a

Browse files
committed
Handle SI by bat-native-ads library.
1 parent 0229c66 commit 3a4b39a

File tree

20 files changed

+339
-39
lines changed

20 files changed

+339
-39
lines changed

browser/brave_ads/ads_service_impl.cc

Lines changed: 50 additions & 4 deletions
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"
@@ -683,13 +684,23 @@ void AdsServiceImpl::OnInitialize(const bool success) {
683684

684685
StartCheckIdleStateTimer();
685686

686-
if (!deprecated_data_files_removed_) {
687-
deprecated_data_files_removed_ = true;
688-
file_task_runner_->PostTask(
689-
FROM_HERE, base::BindOnce(&RemoveDeprecatedAdsDataFiles, base_path_));
687+
if (!is_setup_on_first_initialize_done_) {
688+
SetupOnFirstInitialize();
689+
is_setup_on_first_initialize_done_ = true;
690690
}
691691
}
692692

693+
void AdsServiceImpl::SetupOnFirstInitialize() {
694+
DCHECK(!is_setup_on_first_initialize_done_);
695+
696+
PrefetchNewTabPageAd();
697+
698+
PurgeOrphanedAdEventsForType(ads::mojom::AdType::kNewTabPageAd);
699+
700+
file_task_runner_->PostTask(
701+
FROM_HERE, base::BindOnce(&RemoveDeprecatedAdsDataFiles, base_path_));
702+
}
703+
693704
void AdsServiceImpl::ShutdownBatAds() {
694705
if (!connected()) {
695706
return;
@@ -1107,6 +1118,22 @@ void AdsServiceImpl::OnOpenNewTabWithAd(const std::string& json) {
11071118
OpenNewTabWithUrl(notification.target_url);
11081119
}
11091120

1121+
absl::optional<std::string> AdsServiceImpl::GetPrefetchedNewTabPageAd() {
1122+
if (!connected()) {
1123+
return absl::nullopt;
1124+
}
1125+
1126+
absl::optional<std::string> ad_info;
1127+
if (prefetched_new_tab_page_ad_info_) {
1128+
ad_info = prefetched_new_tab_page_ad_info_;
1129+
prefetched_new_tab_page_ad_info_.reset();
1130+
}
1131+
1132+
PrefetchNewTabPageAd();
1133+
1134+
return ad_info;
1135+
}
1136+
11101137
void AdsServiceImpl::OnNewTabPageAdEvent(
11111138
const std::string& uuid,
11121139
const std::string& creative_instance_id,
@@ -1212,6 +1239,25 @@ void AdsServiceImpl::RegisterResourceComponentsForLocale(
12121239
locale);
12131240
}
12141241

1242+
void AdsServiceImpl::PrefetchNewTabPageAd() {
1243+
if (!connected()) {
1244+
prefetched_new_tab_page_ad_info_.reset();
1245+
return;
1246+
}
1247+
1248+
bat_ads_->GetNewTabPageAd(
1249+
base::BindOnce(&AdsServiceImpl::OnPrefetchNewTabPageAd, AsWeakPtr()));
1250+
}
1251+
1252+
void AdsServiceImpl::OnPrefetchNewTabPageAd(bool success, const std::string& json) {
1253+
if (!success) {
1254+
prefetched_new_tab_page_ad_info_.reset();
1255+
return;
1256+
}
1257+
1258+
prefetched_new_tab_page_ad_info_ = json;
1259+
}
1260+
12151261
void AdsServiceImpl::OnURLRequestStarted(
12161262
const GURL& final_url,
12171263
const network::mojom::URLResponseHead& response_head) {

browser/brave_ads/ads_service_impl.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
#include "base/memory/weak_ptr.h"
1818
#include "base/task/cancelable_task_tracker.h"
1919
#include "base/timer/timer.h"
20+
#include "base/values.h"
2021
#include "bat/ads/ads.h"
2122
#include "bat/ads/ads_client.h"
2223
#include "bat/ads/database.h"
24+
#include "bat/ads/new_tab_page_ad_info.h"
2325
#include "bat/ads/public/interfaces/ads.mojom.h"
2426
#include "bat/ledger/mojom_structs.h"
2527
#include "brave/browser/brave_ads/background_helper/background_helper.h"
@@ -163,6 +165,8 @@ class AdsServiceImpl : public AdsService,
163165
const std::string& creative_instance_id,
164166
const ads::mojom::InlineContentAdEventType event_type) override;
165167

168+
absl::optional<std::string> GetPrefetchedNewTabPageAd() override;
169+
166170
void PurgeOrphanedAdEventsForType(const ads::mojom::AdType ad_type) override;
167171

168172
void GetAdsHistory(const double from_timestamp,
@@ -207,6 +211,7 @@ class AdsServiceImpl : public AdsService,
207211
void OnCreate();
208212

209213
void OnInitialize(const bool success);
214+
void SetupOnFirstInitialize();
210215

211216
void ShutdownBatAds();
212217
void OnShutdownBatAds(const bool success);
@@ -254,6 +259,10 @@ class AdsServiceImpl : public AdsService,
254259

255260
void RegisterResourceComponentsForLocale(const std::string& locale);
256261

262+
void PrefetchNewTabPageAd();
263+
264+
void OnPrefetchNewTabPageAd(bool success, const std::string& json);
265+
257266
void OnURLRequestStarted(
258267
const GURL& final_url,
259268
const network::mojom::URLResponseHead& response_head);
@@ -463,7 +472,7 @@ class AdsServiceImpl : public AdsService,
463472

464473
bool is_initialized_ = false;
465474

466-
bool deprecated_data_files_removed_ = false;
475+
bool is_setup_on_first_initialize_done_ = false;
467476

468477
bool is_upgrading_from_pre_brave_ads_build_;
469478

@@ -484,6 +493,8 @@ class AdsServiceImpl : public AdsService,
484493

485494
std::unique_ptr<ads::Database> database_;
486495

496+
absl::optional<std::string> prefetched_new_tab_page_ad_info_;
497+
487498
ui::IdleState last_idle_state_;
488499
int last_idle_time_;
489500

browser/ntp_background_images/android/ntp_background_images_bridge.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,6 @@ base::android::ScopedJavaLocalRef<jobject>
148148
NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) {
149149
JNIEnv* env = AttachCurrentThread();
150150

151-
const std::string wallpaper_id = base::GenerateGUID();
152-
view_counter_service_->BrandedWallpaperWillBeDisplayed(wallpaper_id);
153-
154151
auto* image_path =
155152
data->FindStringKey(ntp_background_images::kWallpaperImagePathKey);
156153
auto* logo_image_path =
@@ -172,6 +169,10 @@ NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) {
172169
auto* creative_instance_id =
173170
data->FindStringKey(ntp_background_images::kCreativeInstanceIDKey);
174171

172+
const std::string wallpaper_id = base::GenerateGUID();
173+
view_counter_service_->BrandedWallpaperWillBeDisplayed(wallpaper_id,
174+
creative_instance_id);
175+
175176
return Java_NTPBackgroundImagesBridge_createBrandedWallpaper(
176177
env, ConvertUTF8ToJavaString(env, *image_path), focal_point_x,
177178
focal_point_y, ConvertUTF8ToJavaString(env, *logo_image_path),

browser/ui/webui/new_tab_page/brave_new_tab_message_handler.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ void BraveNewTabMessageHandler::HandleGetWallpaperData(
573573
return;
574574
}
575575

576-
auto data = service->GetCurrentWallpaperForDisplay();
576+
base::Value data = service->GetCurrentWallpaperForDisplay();
577577

578578
if (!data.is_dict()) {
579579
ResolveJavascriptCallback(args[0], std::move(wallpaper));
@@ -591,11 +591,14 @@ void BraveNewTabMessageHandler::HandleGetWallpaperData(
591591
return;
592592
}
593593

594-
constexpr char kBrandedWallpaperKey[] = "brandedWallpaper";
594+
const std::string* creative_instance_id =
595+
data.FindStringKey(ntp_background_images::kCreativeInstanceIDKey);
595596
const std::string wallpaper_id = base::GenerateGUID();
597+
service->BrandedWallpaperWillBeDisplayed(wallpaper_id, creative_instance_id);
598+
599+
constexpr char kBrandedWallpaperKey[] = "brandedWallpaper";
596600
data.SetStringKey(ntp_background_images::kWallpaperIDKey, wallpaper_id);
597601
wallpaper.SetKey(kBrandedWallpaperKey, std::move(data));
598-
service->BrandedWallpaperWillBeDisplayed(wallpaper_id);
599602
ResolveJavascriptCallback(args[0], std::move(wallpaper));
600603
}
601604

browser/ui/webui/new_tab_page/brave_new_tab_message_handler.h

Lines changed: 1 addition & 0 deletions
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"

components/brave_ads/browser/ads_service.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@
2020
#include "components/sessions/core/session_id.h"
2121
#include "url/gurl.h"
2222

23-
namespace ads {
24-
struct AdsHistoryInfo;
25-
}
26-
2723
namespace base {
2824
class DictionaryValue;
2925
class ListValue;
26+
class Value;
3027
}
3128

3229
namespace user_prefs {
@@ -142,6 +139,8 @@ class AdsService : public KeyedService {
142139
const std::string& creative_instance_id,
143140
const ads::mojom::InlineContentAdEventType event_type) = 0;
144141

142+
virtual absl::optional<std::string> GetPrefetchedNewTabPageAd() = 0;
143+
145144
virtual void PurgeOrphanedAdEventsForType(
146145
const ads::mojom::AdType ad_type) = 0;
147146

components/brave_ads/common/features.cc

Lines changed: 3 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "base/logging.h"
1212
#include "base/notreached.h"
1313
#include "base/strings/stringprintf.h"
14+
#include "bat/ads/new_tab_page_ad_info.h"
1415
#include "brave/components/ntp_background_images/browser/url_constants.h"
1516
#include "content/public/common/url_constants.h"
1617

@@ -196,6 +197,10 @@ Campaign NTPSponsoredImagesData::GetCampaignFromValue(
196197

197198
Campaign campaign;
198199

200+
if (const std::string* campaign_id = value.FindStringKey(kCampaignIdKey)) {
201+
campaign.campaign_id = *campaign_id;
202+
}
203+
199204
Logo default_logo;
200205
if (auto* logo = value.FindDictKey(kLogoKey)) {
201206
default_logo = GetLogoFromValue(installed_dir, url_prefix, logo);
@@ -287,11 +292,11 @@ base::Value NTPSponsoredImagesData::GetBackgroundAt(size_t campaign_index,
287292
DCHECK(campaign_index < campaigns.size() && background_index >= 0 &&
288293
background_index < campaigns[campaign_index].backgrounds.size());
289294

290-
base::Value data(base::Value::Type::DICTIONARY);
291295
const auto campaign = campaigns[campaign_index];
292296
if (!campaign.IsValid())
293-
return data;
297+
return base::Value();
294298

299+
base::Value data(base::Value::Type::DICTIONARY);
295300
data.SetStringKey(kThemeNameKey, theme_name);
296301
data.SetBoolKey(kIsSponsoredKey, !IsSuperReferral());
297302
data.SetBoolKey(kIsBackgroundKey, false);
@@ -324,6 +329,41 @@ base::Value NTPSponsoredImagesData::GetBackgroundAt(size_t campaign_index,
324329
return data;
325330
}
326331

332+
base::Value NTPSponsoredImagesData::GetBackgroundByAdInfo(
333+
const ads::NewTabPageAdInfo& ad_info) {
334+
// Find campaign
335+
LOG(ERROR) << ad_info.campaign_id;
336+
size_t campaign_index = 0;
337+
for (; campaign_index != campaigns.size(); ++campaign_index) {
338+
LOG(ERROR) << campaigns[campaign_index].campaign_id;
339+
if (campaigns[campaign_index].campaign_id == ad_info.campaign_id) {
340+
break;
341+
}
342+
}
343+
if (campaign_index == campaigns.size()) {
344+
LOG(ERROR) << "Ad campaign wasn't found in NTP sposored images data: "
345+
<< ad_info.campaign_id;
346+
return base::Value();
347+
}
348+
349+
const auto& sponsored_backgrounds = campaigns[campaign_index].backgrounds;
350+
size_t background_index = 0;
351+
for (; background_index != sponsored_backgrounds.size(); ++background_index) {
352+
LOG(ERROR) << sponsored_backgrounds[background_index].creative_instance_id;
353+
if (sponsored_backgrounds[background_index].creative_instance_id ==
354+
ad_info.creative_instance_id) {
355+
break;
356+
}
357+
}
358+
if (background_index == sponsored_backgrounds.size()) {
359+
LOG(ERROR) << "Creative instance wasn't found in NTP sposored images data: "
360+
<< ad_info.creative_instance_id;
361+
return base::Value();
362+
}
363+
364+
return GetBackgroundAt(campaign_index, background_index);
365+
}
366+
327367
void NTPSponsoredImagesData::PrintCampaignsParsingResult() const {
328368
VLOG(2) << __func__ << ": This is "
329369
<< (IsSuperReferral() ? " NTP SR Data" : " NTP SI Data");

components/ntp_background_images/browser/ntp_sponsored_images_data.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#include "ui/gfx/geometry/point.h"
1717
#include "ui/gfx/geometry/rect.h"
1818

19+
namespace ads {
20+
struct NewTabPageAdInfo;
21+
} // namespace ads
22+
1923
namespace ntp_background_images {
2024

2125
struct TopSite {
@@ -78,6 +82,7 @@ struct Campaign {
7882

7983
bool IsValid() const;
8084

85+
std::string campaign_id;
8186
std::vector<SponsoredBackground> backgrounds;
8287
};
8388

@@ -103,6 +108,7 @@ struct NTPSponsoredImagesData {
103108
const base::FilePath& installed_dir);
104109

105110
base::Value GetBackgroundAt(size_t campaign_index, size_t background_index);
111+
base::Value GetBackgroundByAdInfo(const ads::NewTabPageAdInfo& ad_info);
106112

107113
bool IsSuperReferral() const;
108114
void PrintCampaignsParsingResult() const;

0 commit comments

Comments
 (0)