Skip to content

Commit 81564f0

Browse files
authored
Merge pull request #11945 from brave/issues/14015_wallpapers
Add NTP sponsored images frequency capping.
2 parents 939568e + 25c169e commit 81564f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1450
-200
lines changed

browser/ntp_background_images/android/ntp_background_images_bridge.cc

+6-4
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,6 @@ base::android::ScopedJavaLocalRef<jobject>
147147
NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) {
148148
JNIEnv* env = AttachCurrentThread();
149149

150-
const std::string wallpaper_id = base::GenerateGUID();
151-
view_counter_service_->BrandedWallpaperWillBeDisplayed(wallpaper_id);
152-
153150
auto* image_path =
154151
data->FindStringKey(ntp_background_images::kWallpaperImagePathKey);
155152
auto* logo_image_path =
@@ -170,6 +167,11 @@ NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) {
170167
data->FindBoolKey(ntp_background_images::kIsSponsoredKey).value_or(false);
171168
auto* creative_instance_id =
172169
data->FindStringKey(ntp_background_images::kCreativeInstanceIDKey);
170+
const std::string* wallpaper_id =
171+
data->FindStringKey(ntp_background_images::kWallpaperIDKey);
172+
173+
view_counter_service_->BrandedWallpaperWillBeDisplayed(wallpaper_id,
174+
creative_instance_id);
173175

174176
return Java_NTPBackgroundImagesBridge_createBrandedWallpaper(
175177
env, ConvertUTF8ToJavaString(env, *image_path), focal_point_x,
@@ -179,7 +181,7 @@ NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) {
179181
ConvertUTF8ToJavaString(env, *theme_name), is_sponsored,
180182
ConvertUTF8ToJavaString(
181183
env, creative_instance_id ? *creative_instance_id : ""),
182-
ConvertUTF8ToJavaString(env, wallpaper_id));
184+
ConvertUTF8ToJavaString(env, wallpaper_id ? *wallpaper_id : ""));
183185
}
184186

185187
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
@@ -8,7 +8,6 @@
88
#include <memory>
99
#include <utility>
1010

11-
#include "base/guid.h"
1211
#include "base/json/json_writer.h"
1312
#include "base/memory/weak_ptr.h"
1413
#include "base/metrics/histogram_macros.h"
@@ -590,7 +589,7 @@ void BraveNewTabMessageHandler::HandleGetWallpaperData(
590589
return;
591590
}
592591

593-
auto data = service->GetCurrentWallpaperForDisplay();
592+
base::Value data = service->GetCurrentWallpaperForDisplay();
594593

595594
if (!data.is_dict()) {
596595
ResolveJavascriptCallback(args[0], std::move(wallpaper));
@@ -608,11 +607,14 @@ void BraveNewTabMessageHandler::HandleGetWallpaperData(
608607
return;
609608
}
610609

610+
const std::string* creative_instance_id =
611+
data.FindStringKey(ntp_background_images::kCreativeInstanceIDKey);
612+
const std::string* wallpaper_id =
613+
data.FindStringKey(ntp_background_images::kWallpaperIDKey);
614+
service->BrandedWallpaperWillBeDisplayed(wallpaper_id, creative_instance_id);
615+
611616
constexpr char kBrandedWallpaperKey[] = "brandedWallpaper";
612-
const std::string wallpaper_id = base::GenerateGUID();
613-
data.SetStringKey(ntp_background_images::kWallpaperIDKey, wallpaper_id);
614617
wallpaper.SetKey(kBrandedWallpaperKey, std::move(data));
615-
service->BrandedWallpaperWillBeDisplayed(wallpaper_id);
616618
ResolveJavascriptCallback(args[0], std::move(wallpaper));
617619
}
618620

components/brave_ads/browser/ads_service.h

+11-5
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,14 @@
1515
#include "bat/ads/public/interfaces/ads.mojom.h"
1616
#include "brave/components/brave_adaptive_captcha/buildflags/buildflags.h"
1717
#include "brave/components/brave_ads/browser/ads_service_observer.h"
18+
#include "brave/vendor/bat-native-ads/include/bat/ads/new_tab_page_ad_info.h"
1819
#include "brave/vendor/bat-native-ads/include/bat/ads/public/interfaces/ads.mojom.h"
1920
#include "build/build_config.h"
2021
#include "components/keyed_service/core/keyed_service.h"
2122
#include "components/sessions/core/session_id.h"
2223

2324
class GURL;
2425

25-
namespace ads {
26-
struct HistoryInfo;
27-
} // namespace ads
28-
2926
namespace base {
3027
class DictionaryValue;
3128
class ListValue;
@@ -66,6 +63,9 @@ using GetStatementOfAccountsCallback = base::OnceCallback<
6663
using GetDiagnosticsCallback =
6764
base::OnceCallback<void(const bool, const std::string&)>;
6865

66+
using PurgeOrphanedAdEventsForTypeCallback =
67+
base::OnceCallback<void(const bool)>;
68+
6969
class AdsService : public KeyedService {
7070
public:
7171
AdsService();
@@ -136,6 +136,9 @@ class AdsService : public KeyedService {
136136
const std::string& placement_id,
137137
const std::string& creative_instance_id,
138138
const ads::mojom::NewTabPageAdEventType event_type) = 0;
139+
virtual void OnFailedToServeNewTabPageAd(
140+
const std::string& placement_id,
141+
const std::string& creative_instance_id) = 0;
139142

140143
virtual void TriggerPromotedContentAdEvent(
141144
const std::string& placement_id,
@@ -154,8 +157,11 @@ class AdsService : public KeyedService {
154157
const ads::mojom::SearchResultAdEventType event_type,
155158
TriggerSearchResultAdEventCallback callback) = 0;
156159

160+
virtual absl::optional<ads::NewTabPageAdInfo> GetPrefetchedNewTabPageAd() = 0;
161+
157162
virtual void PurgeOrphanedAdEventsForType(
158-
const ads::mojom::AdType ad_type) = 0;
163+
const ads::mojom::AdType ad_type,
164+
PurgeOrphanedAdEventsForTypeCallback callback) = 0;
159165

160166
virtual void GetHistory(const base::Time from_time,
161167
const base::Time to_time,

components/brave_ads/browser/ads_service_impl.cc

+101-6
Original file line numberDiff line numberDiff line change
@@ -683,13 +683,27 @@ void AdsServiceImpl::OnInitialize(const bool success) {
683683

684684
StartCheckIdleStateTimer();
685685

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_));
686+
if (!is_setup_on_first_initialize_done_) {
687+
SetupOnFirstInitialize();
688+
is_setup_on_first_initialize_done_ = true;
690689
}
691690
}
692691

692+
void AdsServiceImpl::SetupOnFirstInitialize() {
693+
DCHECK(!is_setup_on_first_initialize_done_);
694+
695+
file_task_runner_->PostTask(
696+
FROM_HERE, base::BindOnce(&RemoveDeprecatedAdsDataFiles, base_path_));
697+
698+
// Initiate prefetching of the new tab page ad. Also need to purge orphaned
699+
// new tab page ad events which may have remained from the previous browser
700+
// startup.
701+
PurgeOrphanedAdEventsForType(
702+
ads::mojom::AdType::kNewTabPageAd,
703+
base::BindOnce(&AdsServiceImpl::OnPurgeOrphanedAdEventsForNewTabPageAds,
704+
AsWeakPtr()));
705+
}
706+
693707
void AdsServiceImpl::ShutdownBatAds() {
694708
if (!connected()) {
695709
return;
@@ -1136,6 +1150,15 @@ void AdsServiceImpl::TriggerNewTabPageAdEvent(
11361150
event_type);
11371151
}
11381152

1153+
void AdsServiceImpl::OnFailedToServeNewTabPageAd(
1154+
const std::string& placement_id,
1155+
const std::string& creative_instance_id) {
1156+
if (!purge_orphaned_new_tab_page_ad_events_time_) {
1157+
purge_orphaned_new_tab_page_ad_events_time_ =
1158+
base::Time::Now() + base::Hours(1);
1159+
}
1160+
}
1161+
11391162
void AdsServiceImpl::TriggerPromotedContentAdEvent(
11401163
const std::string& placement_id,
11411164
const std::string& creative_instance_id,
@@ -1188,13 +1211,40 @@ void AdsServiceImpl::TriggerSearchResultAdEvent(
11881211
std::move(callback)));
11891212
}
11901213

1214+
absl::optional<ads::NewTabPageAdInfo>
1215+
AdsServiceImpl::GetPrefetchedNewTabPageAd() {
1216+
if (!connected()) {
1217+
return absl::nullopt;
1218+
}
1219+
1220+
absl::optional<ads::NewTabPageAdInfo> ad_info;
1221+
if (prefetched_new_tab_page_ad_info_) {
1222+
ad_info = prefetched_new_tab_page_ad_info_;
1223+
prefetched_new_tab_page_ad_info_.reset();
1224+
}
1225+
1226+
if (purge_orphaned_new_tab_page_ad_events_time_ &&
1227+
*purge_orphaned_new_tab_page_ad_events_time_ <= base::Time::Now()) {
1228+
purge_orphaned_new_tab_page_ad_events_time_.reset();
1229+
PurgeOrphanedAdEventsForType(
1230+
ads::mojom::AdType::kNewTabPageAd,
1231+
base::BindOnce(&AdsServiceImpl::OnPurgeOrphanedAdEventsForNewTabPageAds,
1232+
AsWeakPtr()));
1233+
} else {
1234+
PrefetchNewTabPageAd();
1235+
}
1236+
1237+
return ad_info;
1238+
}
1239+
11911240
void AdsServiceImpl::PurgeOrphanedAdEventsForType(
1192-
const ads::mojom::AdType ad_type) {
1241+
const ads::mojom::AdType ad_type,
1242+
PurgeOrphanedAdEventsForTypeCallback callback) {
11931243
if (!connected()) {
11941244
return;
11951245
}
11961246

1197-
bat_ads_->PurgeOrphanedAdEventsForType(ad_type);
1247+
bat_ads_->PurgeOrphanedAdEventsForType(ad_type, std::move(callback));
11981248
}
11991249

12001250
void AdsServiceImpl::RetryOpeningNewTabWithAd(const std::string& placement_id) {
@@ -1247,6 +1297,40 @@ void AdsServiceImpl::RegisterResourceComponentsForLocale(
12471297
locale);
12481298
}
12491299

1300+
void AdsServiceImpl::PrefetchNewTabPageAd() {
1301+
if (!connected()) {
1302+
return;
1303+
}
1304+
1305+
// The previous prefetched new tab page ad is available. No need to do
1306+
// prefetch again.
1307+
if (prefetched_new_tab_page_ad_info_) {
1308+
return;
1309+
}
1310+
1311+
bat_ads_->GetNewTabPageAd(
1312+
base::BindOnce(&AdsServiceImpl::OnPrefetchNewTabPageAd, AsWeakPtr()));
1313+
}
1314+
1315+
void AdsServiceImpl::OnPrefetchNewTabPageAd(bool success,
1316+
const std::string& json) {
1317+
// The previous prefetched new tab page ad was not served.
1318+
if (prefetched_new_tab_page_ad_info_ &&
1319+
!purge_orphaned_new_tab_page_ad_events_time_) {
1320+
purge_orphaned_new_tab_page_ad_events_time_ =
1321+
base::Time::Now() + base::Hours(1);
1322+
}
1323+
1324+
if (!success) {
1325+
prefetched_new_tab_page_ad_info_.reset();
1326+
return;
1327+
}
1328+
1329+
ads::NewTabPageAdInfo ad_info;
1330+
ad_info.FromJson(json);
1331+
prefetched_new_tab_page_ad_info_ = ad_info;
1332+
}
1333+
12501334
void AdsServiceImpl::OnURLRequestStarted(
12511335
const GURL& final_url,
12521336
const network::mojom::URLResponseHead& response_head) {
@@ -1331,6 +1415,17 @@ void AdsServiceImpl::OnTriggerSearchResultAdEvent(
13311415
std::move(callback).Run(success, placement_id, event_type);
13321416
}
13331417

1418+
void AdsServiceImpl::OnPurgeOrphanedAdEventsForNewTabPageAds(
1419+
const bool success) {
1420+
if (!success) {
1421+
VLOG(0) << "Failed to purge orphaned ad events for new tab page ads";
1422+
return;
1423+
}
1424+
1425+
VLOG(0) << "Successfully purged orphaned ad events for new tab page ads";
1426+
PrefetchNewTabPageAd();
1427+
}
1428+
13341429
void AdsServiceImpl::OnGetHistory(OnGetHistoryCallback callback,
13351430
const std::string& json) {
13361431
ads::HistoryInfo history;

components/brave_ads/browser/ads_service_impl.h

+18-2
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ class AdsServiceImpl : public AdsService,
161161
const std::string& placement_id,
162162
const std::string& creative_instance_id,
163163
const ads::mojom::NewTabPageAdEventType event_type) override;
164+
void OnFailedToServeNewTabPageAd(
165+
const std::string& placement_id,
166+
const std::string& creative_instance_id) override;
164167

165168
void TriggerPromotedContentAdEvent(
166169
const std::string& placement_id,
@@ -180,7 +183,11 @@ class AdsServiceImpl : public AdsService,
180183
const ads::mojom::SearchResultAdEventType event_type,
181184
TriggerSearchResultAdEventCallback callback) override;
182185

183-
void PurgeOrphanedAdEventsForType(const ads::mojom::AdType ad_type) override;
186+
absl::optional<ads::NewTabPageAdInfo> GetPrefetchedNewTabPageAd() override;
187+
188+
void PurgeOrphanedAdEventsForType(
189+
const ads::mojom::AdType ad_type,
190+
PurgeOrphanedAdEventsForTypeCallback callback) override;
184191

185192
void GetHistory(const base::Time from_time,
186193
const base::Time to_time,
@@ -224,6 +231,7 @@ class AdsServiceImpl : public AdsService,
224231
void OnCreate();
225232

226233
void OnInitialize(const bool success);
234+
void SetupOnFirstInitialize();
227235

228236
void ShutdownBatAds();
229237
void OnShutdownBatAds(const bool success);
@@ -270,6 +278,9 @@ class AdsServiceImpl : public AdsService,
270278

271279
void RegisterResourceComponentsForLocale(const std::string& locale);
272280

281+
void PrefetchNewTabPageAd();
282+
void OnPrefetchNewTabPageAd(bool success, const std::string& json);
283+
273284
void OnURLRequestStarted(
274285
const GURL& final_url,
275286
const network::mojom::URLResponseHead& response_head);
@@ -291,6 +302,8 @@ class AdsServiceImpl : public AdsService,
291302
const std::string& placement_id,
292303
const ads::mojom::SearchResultAdEventType event_type);
293304

305+
void OnPurgeOrphanedAdEventsForNewTabPageAds(const bool success);
306+
294307
void OnGetHistory(OnGetHistoryCallback callback, const std::string& json);
295308

296309
void OnGetStatementOfAccounts(GetStatementOfAccountsCallback callback,
@@ -479,7 +492,7 @@ class AdsServiceImpl : public AdsService,
479492

480493
bool is_initialized_ = false;
481494

482-
bool deprecated_data_files_removed_ = false;
495+
bool is_setup_on_first_initialize_done_ = false;
483496

484497
bool needs_browser_update_to_see_ads_ = false;
485498

@@ -502,6 +515,9 @@ class AdsServiceImpl : public AdsService,
502515

503516
std::unique_ptr<ads::Database> database_;
504517

518+
absl::optional<ads::NewTabPageAdInfo> prefetched_new_tab_page_ad_info_;
519+
absl::optional<base::Time> purge_orphaned_new_tab_page_ad_events_time_;
520+
505521
ui::IdleState last_idle_state_;
506522
int last_idle_time_;
507523

components/brave_ads/browser/mock_ads_service.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class MockAdsService : public AdsService {
7878
void(const std::string&,
7979
const std::string&,
8080
ads::mojom::NewTabPageAdEventType));
81+
MOCK_METHOD2(OnFailedToServeNewTabPageAd,
82+
void(const std::string&, const std::string&));
8183

8284
MOCK_METHOD3(TriggerPromotedContentAdEvent,
8385
void(const std::string&,
@@ -96,7 +98,11 @@ class MockAdsService : public AdsService {
9698
const ads::mojom::SearchResultAdEventType,
9799
TriggerSearchResultAdEventCallback));
98100

99-
MOCK_METHOD1(PurgeOrphanedAdEventsForType, void(const ads::mojom::AdType));
101+
MOCK_METHOD0(GetPrefetchedNewTabPageAd,
102+
absl::optional<ads::NewTabPageAdInfo>());
103+
104+
MOCK_METHOD2(PurgeOrphanedAdEventsForType,
105+
void(ads::mojom::AdType, PurgeOrphanedAdEventsForTypeCallback));
100106

101107
MOCK_METHOD3(GetHistory, void(base::Time, base::Time, OnGetHistoryCallback));
102108

components/brave_ads/test/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ source_set("brave_ads_unit_tests") {
8282
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_events/ad_events_database_table_unittest_util.cc",
8383
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_events/ad_events_database_table_unittest_util.h",
8484
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_events/inline_content_ads/inline_content_ad_unittest.cc",
85+
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_events/new_tab_page_ads/new_tab_page_ad_if_ads_disabled_unittest.cc",
8586
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_events/new_tab_page_ads/new_tab_page_ad_unittest.cc",
8687
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_events/notification_ads/notification_ad_unittest.cc",
8788
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_events/promoted_content_ads/promoted_content_ad_unittest.cc",
@@ -343,6 +344,7 @@ source_set("brave_ads_unit_tests") {
343344
"//brave/vendor/bat-native-ads/src/bat/ads/internal/user_interaction/browsing/user_activity_scoring_util_unittest.cc",
344345
"//brave/vendor/bat-native-ads/src/bat/ads/internal/user_interaction/browsing/user_activity_util_unittest.cc",
345346
"//brave/vendor/bat-native-ads/src/bat/ads/internal/user_interaction/idle_detection/idle_time_unittest.cc",
347+
"//brave/vendor/bat-native-ads/src/bat/ads/new_tab_page_ad_info_unittest.cc",
346348
]
347349

348350
deps = [

components/brave_today/browser/brave_news_controller.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <vector>
1313

1414
#include "base/bind.h"
15+
#include "base/callback_helpers.h"
1516
#include "base/guid.h"
1617
#include "base/time/time.h"
1718
#include "base/values.h"
@@ -445,7 +446,7 @@ void BraveNewsController::OnDisplayAdPurgeOrphanedEvents() {
445446
return;
446447
}
447448
ads_service_->PurgeOrphanedAdEventsForType(
448-
ads::mojom::AdType::kInlineContentAd);
449+
ads::mojom::AdType::kInlineContentAd, base::DoNothing());
449450
}
450451

451452
void BraveNewsController::CheckForPublishersUpdate() {

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)