Skip to content

Add NTP sponsored images frequency capping. #11945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,6 @@ base::android::ScopedJavaLocalRef<jobject>
NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) {
JNIEnv* env = AttachCurrentThread();

const std::string wallpaper_id = base::GenerateGUID();
view_counter_service_->BrandedWallpaperWillBeDisplayed(wallpaper_id);

auto* image_path =
data->FindStringKey(ntp_background_images::kWallpaperImagePathKey);
auto* logo_image_path =
Expand All @@ -170,6 +167,11 @@ NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) {
data->FindBoolKey(ntp_background_images::kIsSponsoredKey).value_or(false);
auto* creative_instance_id =
data->FindStringKey(ntp_background_images::kCreativeInstanceIDKey);
const std::string* wallpaper_id =
data->FindStringKey(ntp_background_images::kWallpaperIDKey);

view_counter_service_->BrandedWallpaperWillBeDisplayed(wallpaper_id,
creative_instance_id);

return Java_NTPBackgroundImagesBridge_createBrandedWallpaper(
env, ConvertUTF8ToJavaString(env, *image_path), focal_point_x,
Expand All @@ -179,7 +181,7 @@ NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) {
ConvertUTF8ToJavaString(env, *theme_name), is_sponsored,
ConvertUTF8ToJavaString(
env, creative_instance_id ? *creative_instance_id : ""),
ConvertUTF8ToJavaString(env, wallpaper_id));
ConvertUTF8ToJavaString(env, wallpaper_id ? *wallpaper_id : ""));
}

void NTPBackgroundImagesBridge::GetTopSites(
Expand Down
12 changes: 7 additions & 5 deletions browser/ui/webui/new_tab_page/brave_new_tab_message_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <memory>
#include <utility>

#include "base/guid.h"
#include "base/json/json_writer.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_macros.h"
Expand Down Expand Up @@ -590,7 +589,7 @@ void BraveNewTabMessageHandler::HandleGetWallpaperData(
return;
}

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

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

const std::string* creative_instance_id =
data.FindStringKey(ntp_background_images::kCreativeInstanceIDKey);
const std::string* wallpaper_id =
data.FindStringKey(ntp_background_images::kWallpaperIDKey);
service->BrandedWallpaperWillBeDisplayed(wallpaper_id, creative_instance_id);

constexpr char kBrandedWallpaperKey[] = "brandedWallpaper";
const std::string wallpaper_id = base::GenerateGUID();
data.SetStringKey(ntp_background_images::kWallpaperIDKey, wallpaper_id);
wallpaper.SetKey(kBrandedWallpaperKey, std::move(data));
service->BrandedWallpaperWillBeDisplayed(wallpaper_id);
ResolveJavascriptCallback(args[0], std::move(wallpaper));
}

Expand Down
16 changes: 11 additions & 5 deletions components/brave_ads/browser/ads_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@
#include "bat/ads/public/interfaces/ads.mojom.h"
#include "brave/components/brave_adaptive_captcha/buildflags/buildflags.h"
#include "brave/components/brave_ads/browser/ads_service_observer.h"
#include "brave/vendor/bat-native-ads/include/bat/ads/new_tab_page_ad_info.h"
#include "brave/vendor/bat-native-ads/include/bat/ads/public/interfaces/ads.mojom.h"
#include "build/build_config.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/sessions/core/session_id.h"

class GURL;

namespace ads {
struct HistoryInfo;
} // namespace ads

namespace base {
class DictionaryValue;
class ListValue;
Expand Down Expand Up @@ -66,6 +63,9 @@ using GetStatementOfAccountsCallback = base::OnceCallback<
using GetDiagnosticsCallback =
base::OnceCallback<void(const bool, const std::string&)>;

using PurgeOrphanedAdEventsForTypeCallback =
base::OnceCallback<void(const bool)>;

class AdsService : public KeyedService {
public:
AdsService();
Expand Down Expand Up @@ -136,6 +136,9 @@ class AdsService : public KeyedService {
const std::string& placement_id,
const std::string& creative_instance_id,
const ads::mojom::NewTabPageAdEventType event_type) = 0;
virtual void OnFailedToServeNewTabPageAd(
const std::string& placement_id,
const std::string& creative_instance_id) = 0;

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

virtual absl::optional<ads::NewTabPageAdInfo> GetPrefetchedNewTabPageAd() = 0;

virtual void PurgeOrphanedAdEventsForType(
const ads::mojom::AdType ad_type) = 0;
const ads::mojom::AdType ad_type,
PurgeOrphanedAdEventsForTypeCallback callback) = 0;

virtual void GetHistory(const base::Time from_time,
const base::Time to_time,
Expand Down
107 changes: 101 additions & 6 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -683,13 +683,27 @@ void AdsServiceImpl::OnInitialize(const bool success) {

StartCheckIdleStateTimer();

if (!deprecated_data_files_removed_) {
deprecated_data_files_removed_ = true;
file_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&RemoveDeprecatedAdsDataFiles, base_path_));
if (!is_setup_on_first_initialize_done_) {
SetupOnFirstInitialize();
is_setup_on_first_initialize_done_ = true;
}
}

void AdsServiceImpl::SetupOnFirstInitialize() {
DCHECK(!is_setup_on_first_initialize_done_);

file_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&RemoveDeprecatedAdsDataFiles, base_path_));

// Initiate prefetching of the new tab page ad. Also need to purge orphaned
// new tab page ad events which may have remained from the previous browser
// startup.
PurgeOrphanedAdEventsForType(
ads::mojom::AdType::kNewTabPageAd,
base::BindOnce(&AdsServiceImpl::OnPurgeOrphanedAdEventsForNewTabPageAds,
AsWeakPtr()));
}

void AdsServiceImpl::ShutdownBatAds() {
if (!connected()) {
return;
Expand Down Expand Up @@ -1136,6 +1150,15 @@ void AdsServiceImpl::TriggerNewTabPageAdEvent(
event_type);
}

void AdsServiceImpl::OnFailedToServeNewTabPageAd(
const std::string& placement_id,
const std::string& creative_instance_id) {
if (!purge_orphaned_new_tab_page_ad_events_time_) {
purge_orphaned_new_tab_page_ad_events_time_ =
base::Time::Now() + base::Hours(1);
}
}

void AdsServiceImpl::TriggerPromotedContentAdEvent(
const std::string& placement_id,
const std::string& creative_instance_id,
Expand Down Expand Up @@ -1188,13 +1211,40 @@ void AdsServiceImpl::TriggerSearchResultAdEvent(
std::move(callback)));
}

absl::optional<ads::NewTabPageAdInfo>
AdsServiceImpl::GetPrefetchedNewTabPageAd() {
if (!connected()) {
return absl::nullopt;
}

absl::optional<ads::NewTabPageAdInfo> ad_info;
if (prefetched_new_tab_page_ad_info_) {
ad_info = prefetched_new_tab_page_ad_info_;
prefetched_new_tab_page_ad_info_.reset();
}

if (purge_orphaned_new_tab_page_ad_events_time_ &&
*purge_orphaned_new_tab_page_ad_events_time_ <= base::Time::Now()) {
purge_orphaned_new_tab_page_ad_events_time_.reset();
PurgeOrphanedAdEventsForType(
ads::mojom::AdType::kNewTabPageAd,
base::BindOnce(&AdsServiceImpl::OnPurgeOrphanedAdEventsForNewTabPageAds,
AsWeakPtr()));
} else {
PrefetchNewTabPageAd();
}

return ad_info;
}

void AdsServiceImpl::PurgeOrphanedAdEventsForType(
const ads::mojom::AdType ad_type) {
const ads::mojom::AdType ad_type,
PurgeOrphanedAdEventsForTypeCallback callback) {
if (!connected()) {
return;
}

bat_ads_->PurgeOrphanedAdEventsForType(ad_type);
bat_ads_->PurgeOrphanedAdEventsForType(ad_type, std::move(callback));
}

void AdsServiceImpl::RetryOpeningNewTabWithAd(const std::string& placement_id) {
Expand Down Expand Up @@ -1247,6 +1297,40 @@ void AdsServiceImpl::RegisterResourceComponentsForLocale(
locale);
}

void AdsServiceImpl::PrefetchNewTabPageAd() {
if (!connected()) {
return;
}

// The previous prefetched new tab page ad is available. No need to do
// prefetch again.
if (prefetched_new_tab_page_ad_info_) {
return;
}

bat_ads_->GetNewTabPageAd(
base::BindOnce(&AdsServiceImpl::OnPrefetchNewTabPageAd, AsWeakPtr()));
}

void AdsServiceImpl::OnPrefetchNewTabPageAd(bool success,
const std::string& json) {
// The previous prefetched new tab page ad was not served.
if (prefetched_new_tab_page_ad_info_ &&
!purge_orphaned_new_tab_page_ad_events_time_) {
purge_orphaned_new_tab_page_ad_events_time_ =
base::Time::Now() + base::Hours(1);
}

if (!success) {
prefetched_new_tab_page_ad_info_.reset();
return;
}

ads::NewTabPageAdInfo ad_info;
ad_info.FromJson(json);
prefetched_new_tab_page_ad_info_ = ad_info;
}

void AdsServiceImpl::OnURLRequestStarted(
const GURL& final_url,
const network::mojom::URLResponseHead& response_head) {
Expand Down Expand Up @@ -1331,6 +1415,17 @@ void AdsServiceImpl::OnTriggerSearchResultAdEvent(
std::move(callback).Run(success, placement_id, event_type);
}

void AdsServiceImpl::OnPurgeOrphanedAdEventsForNewTabPageAds(
const bool success) {
if (!success) {
VLOG(0) << "Failed to purge orphaned ad events for new tab page ads";
return;
}

VLOG(0) << "Successfully purged orphaned ad events for new tab page ads";
PrefetchNewTabPageAd();
}

void AdsServiceImpl::OnGetHistory(OnGetHistoryCallback callback,
const std::string& json) {
ads::HistoryInfo history;
Expand Down
20 changes: 18 additions & 2 deletions components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ class AdsServiceImpl : public AdsService,
const std::string& placement_id,
const std::string& creative_instance_id,
const ads::mojom::NewTabPageAdEventType event_type) override;
void OnFailedToServeNewTabPageAd(
const std::string& placement_id,
const std::string& creative_instance_id) override;

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

void PurgeOrphanedAdEventsForType(const ads::mojom::AdType ad_type) override;
absl::optional<ads::NewTabPageAdInfo> GetPrefetchedNewTabPageAd() override;

void PurgeOrphanedAdEventsForType(
const ads::mojom::AdType ad_type,
PurgeOrphanedAdEventsForTypeCallback callback) override;

void GetHistory(const base::Time from_time,
const base::Time to_time,
Expand Down Expand Up @@ -224,6 +231,7 @@ class AdsServiceImpl : public AdsService,
void OnCreate();

void OnInitialize(const bool success);
void SetupOnFirstInitialize();

void ShutdownBatAds();
void OnShutdownBatAds(const bool success);
Expand Down Expand Up @@ -270,6 +278,9 @@ class AdsServiceImpl : public AdsService,

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

void PrefetchNewTabPageAd();
void OnPrefetchNewTabPageAd(bool success, const std::string& json);

void OnURLRequestStarted(
const GURL& final_url,
const network::mojom::URLResponseHead& response_head);
Expand All @@ -291,6 +302,8 @@ class AdsServiceImpl : public AdsService,
const std::string& placement_id,
const ads::mojom::SearchResultAdEventType event_type);

void OnPurgeOrphanedAdEventsForNewTabPageAds(const bool success);

void OnGetHistory(OnGetHistoryCallback callback, const std::string& json);

void OnGetStatementOfAccounts(GetStatementOfAccountsCallback callback,
Expand Down Expand Up @@ -479,7 +492,7 @@ class AdsServiceImpl : public AdsService,

bool is_initialized_ = false;

bool deprecated_data_files_removed_ = false;
bool is_setup_on_first_initialize_done_ = false;

bool needs_browser_update_to_see_ads_ = false;

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

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

absl::optional<ads::NewTabPageAdInfo> prefetched_new_tab_page_ad_info_;
absl::optional<base::Time> purge_orphaned_new_tab_page_ad_events_time_;

ui::IdleState last_idle_state_;
int last_idle_time_;

Expand Down
8 changes: 7 additions & 1 deletion components/brave_ads/browser/mock_ads_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class MockAdsService : public AdsService {
void(const std::string&,
const std::string&,
ads::mojom::NewTabPageAdEventType));
MOCK_METHOD2(OnFailedToServeNewTabPageAd,
void(const std::string&, const std::string&));

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

MOCK_METHOD1(PurgeOrphanedAdEventsForType, void(const ads::mojom::AdType));
MOCK_METHOD0(GetPrefetchedNewTabPageAd,
absl::optional<ads::NewTabPageAdInfo>());

MOCK_METHOD2(PurgeOrphanedAdEventsForType,
void(ads::mojom::AdType, PurgeOrphanedAdEventsForTypeCallback));

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

Expand Down
2 changes: 2 additions & 0 deletions components/brave_ads/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ source_set("brave_ads_unit_tests") {
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_events/ad_events_database_table_unittest_util.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_events/ad_events_database_table_unittest_util.h",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_events/inline_content_ads/inline_content_ad_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_events/new_tab_page_ads/new_tab_page_ad_if_ads_disabled_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_events/new_tab_page_ads/new_tab_page_ad_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_events/notification_ads/notification_ad_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_events/promoted_content_ads/promoted_content_ad_unittest.cc",
Expand Down Expand Up @@ -343,6 +344,7 @@ source_set("brave_ads_unit_tests") {
"//brave/vendor/bat-native-ads/src/bat/ads/internal/user_interaction/browsing/user_activity_scoring_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/user_interaction/browsing/user_activity_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/user_interaction/idle_detection/idle_time_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/new_tab_page_ad_info_unittest.cc",
]

deps = [
Expand Down
3 changes: 2 additions & 1 deletion components/brave_today/browser/brave_news_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <vector>

#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/guid.h"
#include "base/time/time.h"
#include "base/values.h"
Expand Down Expand Up @@ -445,7 +446,7 @@ void BraveNewsController::OnDisplayAdPurgeOrphanedEvents() {
return;
}
ads_service_->PurgeOrphanedAdEventsForType(
ads::mojom::AdType::kInlineContentAd);
ads::mojom::AdType::kInlineContentAd, base::DoNothing());
}

void BraveNewsController::CheckForPublishersUpdate() {
Expand Down
3 changes: 1 addition & 2 deletions components/ntp_background_images/browser/DEPS
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
include_rules = [
"+bat/ads/pref_names.h",
"+bat/ads/public",
"+bat/ads",
"+content/public/browser",
"+content/public/common",
"+third_party/skia",
Expand Down
Loading