Skip to content

Commit 8c5df23

Browse files
committed
Handle SI by bat-native-ads library.
1 parent 3c8aa9f commit 8c5df23

21 files changed

+276
-43
lines changed

browser/brave_ads/ads_service_impl.cc

+50-4
Original file line numberDiff line numberDiff line change
@@ -682,13 +682,23 @@ void AdsServiceImpl::OnInitialize(const bool success) {
682682

683683
StartCheckIdleStateTimer();
684684

685-
if (!deprecated_data_files_removed_) {
686-
deprecated_data_files_removed_ = true;
687-
file_task_runner_->PostTask(
688-
FROM_HERE, base::BindOnce(&RemoveDeprecatedAdsDataFiles, base_path_));
685+
if (!is_setup_on_first_initialize_done_) {
686+
SetupOnFirstInitialize();
687+
is_setup_on_first_initialize_done_ = true;
689688
}
690689
}
691690

691+
void AdsServiceImpl::SetupOnFirstInitialize() {
692+
DCHECK(!is_setup_on_first_initialize_done_);
693+
694+
PrefetchNewTabPageAd();
695+
696+
PurgeOrphanedAdEventsForType(ads::mojom::AdType::kNewTabPageAd);
697+
698+
file_task_runner_->PostTask(
699+
FROM_HERE, base::BindOnce(&RemoveDeprecatedAdsDataFiles, base_path_));
700+
}
701+
692702
void AdsServiceImpl::ShutdownBatAds() {
693703
if (!connected()) {
694704
return;
@@ -1106,6 +1116,22 @@ void AdsServiceImpl::OnOpenNewTabWithAd(const std::string& json) {
11061116
OpenNewTabWithUrl(notification.target_url);
11071117
}
11081118

1119+
absl::optional<std::string> AdsServiceImpl::GetPrefetchedNewTabPageAd() {
1120+
if (!connected()) {
1121+
return absl::nullopt;
1122+
}
1123+
1124+
absl::optional<std::string> ad_info;
1125+
if (prefetched_new_tab_page_ad_info_) {
1126+
ad_info = prefetched_new_tab_page_ad_info_;
1127+
prefetched_new_tab_page_ad_info_.reset();
1128+
}
1129+
1130+
PrefetchNewTabPageAd();
1131+
1132+
return ad_info;
1133+
}
1134+
11091135
void AdsServiceImpl::OnNewTabPageAdEvent(
11101136
const std::string& uuid,
11111137
const std::string& creative_instance_id,
@@ -1211,6 +1237,26 @@ void AdsServiceImpl::RegisterResourceComponentsForLocale(
12111237
locale);
12121238
}
12131239

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

browser/brave_ads/ads_service_impl.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ class AdsServiceImpl : public AdsService,
164164
const std::string& creative_instance_id,
165165
const ads::mojom::InlineContentAdEventType event_type) override;
166166

167+
absl::optional<std::string> GetPrefetchedNewTabPageAd() override;
168+
167169
void PurgeOrphanedAdEventsForType(const ads::mojom::AdType ad_type) override;
168170

169171
void GetAdsHistory(const double from_timestamp,
@@ -208,6 +210,7 @@ class AdsServiceImpl : public AdsService,
208210
void OnCreate();
209211

210212
void OnInitialize(const bool success);
213+
void SetupOnFirstInitialize();
211214

212215
void ShutdownBatAds();
213216
void OnShutdownBatAds(const bool success);
@@ -255,6 +258,10 @@ class AdsServiceImpl : public AdsService,
255258

256259
void RegisterResourceComponentsForLocale(const std::string& locale);
257260

261+
void PrefetchNewTabPageAd();
262+
263+
void OnPrefetchNewTabPageAd(bool success, const std::string& json);
264+
258265
void OnURLRequestStarted(
259266
const GURL& final_url,
260267
const network::mojom::URLResponseHead& response_head);
@@ -464,7 +471,7 @@ class AdsServiceImpl : public AdsService,
464471

465472
bool is_initialized_ = false;
466473

467-
bool deprecated_data_files_removed_ = false;
474+
bool is_setup_on_first_initialize_done_ = false;
468475

469476
bool is_upgrading_from_pre_brave_ads_build_;
470477

@@ -485,6 +492,8 @@ class AdsServiceImpl : public AdsService,
485492

486493
std::unique_ptr<ads::Database> database_;
487494

495+
absl::optional<std::string> prefetched_new_tab_page_ad_info_;
496+
488497
ui::IdleState last_idle_state_;
489498
int last_idle_time_;
490499

browser/ntp_background_images/android/ntp_background_images_bridge.cc

+4-3
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

+6-3
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ void BraveNewTabMessageHandler::HandleGetWallpaperData(
567567
return;
568568
}
569569

570-
auto data = service->GetCurrentWallpaperForDisplay();
570+
base::Value data = service->GetCurrentWallpaperForDisplay();
571571

572572
if (!data.is_dict()) {
573573
ResolveJavascriptCallback(args[0], std::move(wallpaper));
@@ -585,11 +585,14 @@ void BraveNewTabMessageHandler::HandleGetWallpaperData(
585585
return;
586586
}
587587

588-
constexpr char kBrandedWallpaperKey[] = "brandedWallpaper";
588+
const std::string* creative_instance_id =
589+
data.FindStringKey(ntp_background_images::kCreativeInstanceIDKey);
589590
const std::string wallpaper_id = base::GenerateGUID();
591+
service->BrandedWallpaperWillBeDisplayed(wallpaper_id, creative_instance_id);
592+
593+
constexpr char kBrandedWallpaperKey[] = "brandedWallpaper";
590594
data.SetStringKey(ntp_background_images::kWallpaperIDKey, wallpaper_id);
591595
wallpaper.SetKey(kBrandedWallpaperKey, std::move(data));
592-
service->BrandedWallpaperWillBeDisplayed(wallpaper_id);
593596
ResolveJavascriptCallback(args[0], std::move(wallpaper));
594597
}
595598

browser/ui/webui/new_tab_page/brave_new_tab_message_handler.h

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "base/memory/raw_ptr.h"
1212
#include "base/memory/weak_ptr.h"
13+
#include "base/values.h"
1314
#include "brave/components/tor/buildflags/buildflags.h"
1415
#include "brave/components/tor/tor_launcher_observer.h"
1516
#include "components/prefs/pref_change_registrar.h"

components/brave_ads/browser/ads_service.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
#include "components/sessions/core/session_id.h"
2020
#include "url/gurl.h"
2121

22-
namespace ads {
23-
struct AdsHistoryInfo;
24-
}
25-
2622
namespace base {
2723
class DictionaryValue;
2824
class ListValue;
@@ -141,6 +137,8 @@ class AdsService : public KeyedService {
141137
const std::string& creative_instance_id,
142138
const ads::mojom::InlineContentAdEventType event_type) = 0;
143139

140+
virtual absl::optional<std::string> GetPrefetchedNewTabPageAd() = 0;
141+
144142
virtual void PurgeOrphanedAdEventsForType(
145143
const ads::mojom::AdType ad_type) = 0;
146144

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 kNewTabPageAdFrequencyCap{
27+
"NewTabPageAdFrequencyCap", base::FEATURE_DISABLED_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 kNewTabPageAdFrequencyCap;
45+
4446
} // namespace features
4547
} // namespace brave_ads
4648

components/ntp_background_images/browser/DEPS

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
include_rules = [
2-
"+bat/ads/pref_names.h",
3-
"+bat/ads/public",
2+
"+bat/ads",
43
"+content/public/browser",
54
"+content/public/common",
65
"+third_party/skia",

components/ntp_background_images/browser/ntp_sponsored_images_data.cc

+42-2
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

+6
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;

components/ntp_background_images/browser/url_constants.h

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ constexpr char kThemeNameKey[] = "themeName";
2222

2323
constexpr char kLogoKey[] = "logo";
2424

25+
constexpr char kCampaignIdKey[] = "campaignId";
26+
2527
constexpr char kWallpapersKey[] = "wallpapers";
2628
constexpr char kWallpaperFocalPointKey[] = "focalPoint";
2729

components/ntp_background_images/browser/view_counter_model.cc

+13
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ void ViewCounterModel::RegisterPageViewForBackgroundImages() {
109109
current_wallpaper_image_index_ %= total_image_count_;
110110
}
111111

112+
void ViewCounterModel::SetBrandedWallpaperRetrieveFailed() {
113+
// NTP BI component is not ready.
114+
if (total_image_count_ == 0)
115+
return;
116+
117+
if (!show_wallpaper_)
118+
return;
119+
120+
// Increase background image index
121+
current_wallpaper_image_index_++;
122+
current_wallpaper_image_index_ %= total_image_count_;
123+
}
124+
112125
void ViewCounterModel::Reset() {
113126
current_wallpaper_image_index_ = 0;
114127
total_image_count_ = 0;

components/ntp_background_images/browser/view_counter_model.h

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class ViewCounterModel {
4444
bool ShouldShowBrandedWallpaper() const;
4545
void RegisterPageView();
4646
void Reset();
47+
void SetBrandedWallpaperRetrieveFailed();
4748

4849
private:
4950
static const int kInitialCountToBrandedWallpaper = 1;

0 commit comments

Comments
 (0)