Skip to content

Commit 381f064

Browse files
committed
Handle SI by bat-native-ads library.
fix brave/brave-browser#14015
1 parent 269833e commit 381f064

File tree

22 files changed

+345
-57
lines changed

22 files changed

+345
-57
lines changed

browser/brave_ads/ads_service_impl.cc

+56-6
Original file line numberDiff line numberDiff line change
@@ -692,13 +692,23 @@ void AdsServiceImpl::OnInitialize(const bool success) {
692692

693693
StartCheckIdleStateTimer();
694694

695-
if (!deprecated_data_files_removed_) {
696-
deprecated_data_files_removed_ = true;
697-
file_task_runner_->PostTask(
698-
FROM_HERE, base::BindOnce(&RemoveDeprecatedAdsDataFiles, base_path_));
695+
if (!is_setup_on_first_initialize_done_) {
696+
SetupOnFirstInitialize();
697+
is_setup_on_first_initialize_done_ = true;
699698
}
700699
}
701700

701+
void AdsServiceImpl::SetupOnFirstInitialize() {
702+
DCHECK(!is_setup_on_first_initialize_done_);
703+
704+
PurgeOrphanedAdEventsForType(
705+
ads::mojom::AdType::kNewTabPageAd,
706+
base::BindOnce(&AdsServiceImpl::PrefetchNewTabPageAd, AsWeakPtr()));
707+
708+
file_task_runner_->PostTask(
709+
FROM_HERE, base::BindOnce(&RemoveDeprecatedAdsDataFiles, base_path_));
710+
}
711+
702712
void AdsServiceImpl::ShutdownBatAds() {
703713
if (!connected()) {
704714
return;
@@ -1116,6 +1126,23 @@ void AdsServiceImpl::OnOpenNewTabWithAd(const std::string& json) {
11161126
OpenNewTabWithUrl(notification.target_url);
11171127
}
11181128

1129+
absl::optional<ads::NewTabPageAdInfo>
1130+
AdsServiceImpl::GetPrefetchedNewTabPageAd() {
1131+
if (!connected()) {
1132+
return absl::nullopt;
1133+
}
1134+
1135+
absl::optional<ads::NewTabPageAdInfo> ad_info;
1136+
if (prefetched_new_tab_page_ad_info_) {
1137+
ad_info = prefetched_new_tab_page_ad_info_;
1138+
prefetched_new_tab_page_ad_info_.reset();
1139+
}
1140+
1141+
PrefetchNewTabPageAd();
1142+
1143+
return ad_info;
1144+
}
1145+
11191146
void AdsServiceImpl::OnNewTabPageAdEvent(
11201147
const std::string& uuid,
11211148
const std::string& creative_instance_id,
@@ -1162,12 +1189,13 @@ void AdsServiceImpl::OnInlineContentAdEvent(
11621189
}
11631190

11641191
void AdsServiceImpl::PurgeOrphanedAdEventsForType(
1165-
const ads::mojom::AdType ad_type) {
1192+
const ads::mojom::AdType ad_type,
1193+
base::OnceClosure callback) {
11661194
if (!connected()) {
11671195
return;
11681196
}
11691197

1170-
bat_ads_->PurgeOrphanedAdEventsForType(ad_type);
1198+
bat_ads_->PurgeOrphanedAdEventsForType(ad_type, std::move(callback));
11711199
}
11721200

11731201
void AdsServiceImpl::RetryOpeningNewTabWithAd(const std::string& uuid) {
@@ -1221,6 +1249,28 @@ void AdsServiceImpl::RegisterResourceComponentsForLocale(
12211249
locale);
12221250
}
12231251

1252+
void AdsServiceImpl::PrefetchNewTabPageAd() {
1253+
if (!connected()) {
1254+
prefetched_new_tab_page_ad_info_.reset();
1255+
return;
1256+
}
1257+
1258+
bat_ads_->GetNewTabPageAd(
1259+
base::BindOnce(&AdsServiceImpl::OnPrefetchNewTabPageAd, AsWeakPtr()));
1260+
}
1261+
1262+
void AdsServiceImpl::OnPrefetchNewTabPageAd(bool success,
1263+
const std::string& json) {
1264+
if (!success) {
1265+
prefetched_new_tab_page_ad_info_.reset();
1266+
return;
1267+
}
1268+
1269+
ads::NewTabPageAdInfo ad_info;
1270+
ad_info.FromJson(json);
1271+
prefetched_new_tab_page_ad_info_ = ad_info;
1272+
}
1273+
12241274
void AdsServiceImpl::OnURLRequestStarted(
12251275
const GURL& final_url,
12261276
const network::mojom::URLResponseHead& response_head) {

browser/brave_ads/ads_service_impl.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ class AdsServiceImpl : public AdsService,
175175
const std::string& creative_instance_id,
176176
const ads::mojom::InlineContentAdEventType event_type) override;
177177

178-
void PurgeOrphanedAdEventsForType(const ads::mojom::AdType ad_type) override;
178+
absl::optional<ads::NewTabPageAdInfo> GetPrefetchedNewTabPageAd() override;
179+
180+
void PurgeOrphanedAdEventsForType(const ads::mojom::AdType ad_type,
181+
base::OnceClosure callback) override;
179182

180183
void GetAdsHistory(const double from_timestamp,
181184
const double to_timestamp,
@@ -219,6 +222,7 @@ class AdsServiceImpl : public AdsService,
219222
void OnCreate();
220223

221224
void OnInitialize(const bool success);
225+
void SetupOnFirstInitialize();
222226

223227
void ShutdownBatAds();
224228
void OnShutdownBatAds(const bool success);
@@ -266,6 +270,10 @@ class AdsServiceImpl : public AdsService,
266270

267271
void RegisterResourceComponentsForLocale(const std::string& locale);
268272

273+
void PrefetchNewTabPageAd();
274+
275+
void OnPrefetchNewTabPageAd(bool success, const std::string& json);
276+
269277
void OnURLRequestStarted(
270278
const GURL& final_url,
271279
const network::mojom::URLResponseHead& response_head);
@@ -480,7 +488,7 @@ class AdsServiceImpl : public AdsService,
480488

481489
bool is_initialized_ = false;
482490

483-
bool deprecated_data_files_removed_ = false;
491+
bool is_setup_on_first_initialize_done_ = false;
484492

485493
bool is_upgrading_from_pre_brave_ads_build_;
486494

@@ -501,6 +509,8 @@ class AdsServiceImpl : public AdsService,
501509

502510
std::unique_ptr<ads::Database> database_;
503511

512+
absl::optional<ads::NewTabPageAdInfo> prefetched_new_tab_page_ad_info_;
513+
504514
ui::IdleState last_idle_state_;
505515
int last_idle_time_;
506516

browser/notifications/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ source_set("notifications") {
1515
deps = [
1616
"//base",
1717
"//brave/components/brave_adaptive_captcha/buildflags",
18+
"//brave/vendor/bat-native-ads",
1819
"//chrome/browser/notifications",
1920
"//third_party/abseil-cpp:absl",
2021
"//url",

browser/ntp_background_images/android/ntp_background_images_bridge.cc

+6-4
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 =
@@ -171,6 +168,11 @@ NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) {
171168
data->FindBoolKey(ntp_background_images::kIsSponsoredKey).value_or(false);
172169
auto* creative_instance_id =
173170
data->FindStringKey(ntp_background_images::kCreativeInstanceIDKey);
171+
const std::string* wallpaper_id =
172+
data->FindStringKey(ntp_background_images::kWallpaperIDKey);
173+
174+
view_counter_service_->BrandedWallpaperWillBeDisplayed(wallpaper_id,
175+
creative_instance_id);
174176

175177
return Java_NTPBackgroundImagesBridge_createBrandedWallpaper(
176178
env, ConvertUTF8ToJavaString(env, *image_path), focal_point_x,
@@ -180,7 +182,7 @@ NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) {
180182
ConvertUTF8ToJavaString(env, *theme_name), is_sponsored,
181183
ConvertUTF8ToJavaString(
182184
env, creative_instance_id ? *creative_instance_id : ""),
183-
ConvertUTF8ToJavaString(env, wallpaper_id));
185+
ConvertUTF8ToJavaString(env, wallpaper_id ? *wallpaper_id : ""));
184186
}
185187

186188
void NTPBackgroundImagesBridge::GetTopSites(

browser/ui/webui/new_tab_page/brave_new_tab_message_handler.cc

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

1111
#include "base/cxx17_backports.h"
12-
#include "base/guid.h"
1312
#include "base/json/json_writer.h"
1413
#include "base/memory/weak_ptr.h"
1514
#include "base/metrics/histogram_macros.h"
@@ -567,7 +566,7 @@ void BraveNewTabMessageHandler::HandleGetWallpaperData(
567566
return;
568567
}
569568

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

572571
if (!data.is_dict()) {
573572
ResolveJavascriptCallback(args[0], std::move(wallpaper));
@@ -585,11 +584,14 @@ void BraveNewTabMessageHandler::HandleGetWallpaperData(
585584
return;
586585
}
587586

587+
const std::string* creative_instance_id =
588+
data.FindStringKey(ntp_background_images::kCreativeInstanceIDKey);
589+
const std::string* wallpaper_id =
590+
data.FindStringKey(ntp_background_images::kWallpaperIDKey);
591+
service->BrandedWallpaperWillBeDisplayed(wallpaper_id, creative_instance_id);
592+
588593
constexpr char kBrandedWallpaperKey[] = "brandedWallpaper";
589-
const std::string wallpaper_id = base::GenerateGUID();
590-
data.SetStringKey(ntp_background_images::kWallpaperIDKey, wallpaper_id);
591594
wallpaper.SetKey(kBrandedWallpaperKey, std::move(data));
592-
service->BrandedWallpaperWillBeDisplayed(wallpaper_id);
593595
ResolveJavascriptCallback(args[0], std::move(wallpaper));
594596
}
595597

components/brave_ads/browser/ads_service.h

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@
1313
#include "base/observer_list.h"
1414
#include "brave/components/brave_adaptive_captcha/buildflags/buildflags.h"
1515
#include "brave/components/brave_ads/browser/ads_service_observer.h"
16+
#include "brave/vendor/bat-native-ads/include/bat/ads/new_tab_page_ad_info.h"
1617
#include "brave/vendor/bat-native-ads/include/bat/ads/public/interfaces/ads.mojom.h"
1718
#include "build/build_config.h"
1819
#include "components/keyed_service/core/keyed_service.h"
1920
#include "components/sessions/core/session_id.h"
2021
#include "url/gurl.h"
2122

22-
namespace ads {
23-
struct AdsHistoryInfo;
24-
}
25-
2623
namespace base {
2724
class DictionaryValue;
2825
class ListValue;
@@ -141,8 +138,10 @@ class AdsService : public KeyedService {
141138
const std::string& creative_instance_id,
142139
const ads::mojom::InlineContentAdEventType event_type) = 0;
143140

144-
virtual void PurgeOrphanedAdEventsForType(
145-
const ads::mojom::AdType ad_type) = 0;
141+
virtual absl::optional<ads::NewTabPageAdInfo> GetPrefetchedNewTabPageAd() = 0;
142+
143+
virtual void PurgeOrphanedAdEventsForType(const ads::mojom::AdType ad_type,
144+
base::OnceClosure callback) = 0;
146145

147146
virtual void GetAdsHistory(const double from_timestamp,
148147
const double to_timestamp,

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",

0 commit comments

Comments
 (0)