Skip to content

Commit 8482f44

Browse files
committed
Handle SI by bat-native-ads library.
1 parent a7d384b commit 8482f44

21 files changed

+310
-32
lines changed

browser/brave_ads/ads_service_impl.cc

+34
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"
@@ -1107,6 +1108,30 @@ void AdsServiceImpl::OnOpenNewTabWithAd(const std::string& json) {
11071108
OpenNewTabWithUrl(notification.target_url);
11081109
}
11091110

1111+
void AdsServiceImpl::GetNewTabPageAd() {
1112+
if (!connected()) {
1113+
cached_new_tab_page_ad_info_.reset();
1114+
return;
1115+
}
1116+
1117+
bat_ads_->GetNewTabPageAd(
1118+
base::BindOnce(&AdsServiceImpl::OnGetNewTabPageAd, AsWeakPtr()));
1119+
}
1120+
1121+
absl::optional<std::string> AdsServiceImpl::GetCachedNewTabPageAd() {
1122+
if (!connected()) {
1123+
cached_new_tab_page_ad_info_.reset();
1124+
}
1125+
1126+
if (!cached_new_tab_page_ad_info_) {
1127+
return absl::nullopt;
1128+
}
1129+
1130+
absl::optional<std::string> ad_info = cached_new_tab_page_ad_info_;
1131+
cached_new_tab_page_ad_info_.reset();
1132+
return ad_info;
1133+
}
1134+
11101135
void AdsServiceImpl::OnNewTabPageAdEvent(
11111136
const std::string& uuid,
11121137
const std::string& creative_instance_id,
@@ -1267,6 +1292,15 @@ void AdsServiceImpl::OnURLRequestComplete(
12671292
callback(url_response);
12681293
}
12691294

1295+
void AdsServiceImpl::OnGetNewTabPageAd(bool success, const std::string& json) {
1296+
if (!success) {
1297+
cached_new_tab_page_ad_info_.reset();
1298+
return;
1299+
}
1300+
1301+
cached_new_tab_page_ad_info_ = json;
1302+
}
1303+
12701304
void AdsServiceImpl::OnGetInlineContentAd(OnGetInlineContentAdCallback callback,
12711305
const bool success,
12721306
const std::string& dimensions,

browser/brave_ads/ads_service_impl.h

+9
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"
@@ -145,6 +147,9 @@ class AdsServiceImpl : public AdsService,
145147

146148
void OnResourceComponentUpdated(const std::string& id) override;
147149

150+
void GetNewTabPageAd() override;
151+
absl::optional<std::string> GetCachedNewTabPageAd() override;
152+
148153
void OnNewTabPageAdEvent(
149154
const std::string& uuid,
150155
const std::string& creative_instance_id,
@@ -264,6 +269,8 @@ class AdsServiceImpl : public AdsService,
264269

265270
void OnGetBraveWallet(ledger::type::BraveWalletPtr wallet);
266271

272+
void OnGetNewTabPageAd(bool success, const std::string& json);
273+
267274
void OnGetInlineContentAd(OnGetInlineContentAdCallback callback,
268275
const bool success,
269276
const std::string& dimensions,
@@ -484,6 +491,8 @@ class AdsServiceImpl : public AdsService,
484491

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

494+
absl::optional<std::string> cached_new_tab_page_ad_info_;
495+
487496
ui::IdleState last_idle_state_;
488497
int last_idle_time_;
489498

browser/brave_ads/ads_tab_helper.cc

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "base/hash/hash.h"
1212
#include "brave/browser/brave_ads/ads_service_factory.h"
1313
#include "chrome/browser/profiles/profile.h"
14+
#include "chrome/common/webui_url_constants.h"
1415
#include "components/dom_distiller/content/browser/distiller_javascript_utils.h"
1516
#include "components/dom_distiller/content/browser/distiller_page_web_contents.h"
1617
#include "components/sessions/content/session_tab_helper.h"
@@ -126,6 +127,10 @@ void AdsTabHelper::DidFinishNavigation(
126127
return;
127128
}
128129

130+
if (navigation_handle->GetURL() == GURL(chrome::kChromeUINewTabURL)) {
131+
ads_service_->GetNewTabPageAd();
132+
}
133+
129134
if (navigation_handle->HasUserGesture()) {
130135
const int32_t page_transition =
131136
static_cast<int32_t>(navigation_handle->GetPageTransition());

browser/ntp_background_images/android/ntp_background_images_bridge.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,6 @@ base::android::ScopedJavaLocalRef<jobject>
149149
NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) {
150150
JNIEnv* env = AttachCurrentThread();
151151

152-
// TODO(tmancey): HERE
153-
const std::string wallpaper_id = base::GenerateGUID();
154-
view_counter_service_->BrandedWallpaperWillBeDisplayed(wallpaper_id);
155-
156152
auto* image_path =
157153
data->FindStringKey(ntp_background_images::kWallpaperImagePathKey);
158154
auto* logo_image_path =
@@ -174,6 +170,11 @@ NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) {
174170
auto* creative_instance_id =
175171
data->FindStringKey(ntp_background_images::kCreativeInstanceIDKey);
176172

173+
// TODO(tmancey): HERE
174+
const std::string wallpaper_id = base::GenerateGUID();
175+
view_counter_service_->BrandedWallpaperWillBeDisplayed(wallpaper_id,
176+
creative_instance_id);
177+
177178
return Java_NTPBackgroundImagesBridge_createBrandedWallpaper(
178179
env, ConvertUTF8ToJavaString(env, *image_path), focal_point_x,
179180
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
@@ -574,7 +574,7 @@ void BraveNewTabMessageHandler::HandleGetWallpaperData(
574574
}
575575

576576
// TODO(tmancey): HERE (For display on desktop)
577-
auto data = service->GetCurrentWallpaperForDisplay();
577+
base::Value data = service->GetCurrentWallpaperForDisplay();
578578

579579
if (!data.is_dict()) {
580580
ResolveJavascriptCallback(args[0], std::move(wallpaper));
@@ -593,11 +593,14 @@ void BraveNewTabMessageHandler::HandleGetWallpaperData(
593593
}
594594

595595
// TODO(tmancey): HERE
596-
constexpr char kBrandedWallpaperKey[] = "brandedWallpaper";
596+
const std::string* creative_instance_id =
597+
data.FindStringKey(ntp_background_images::kCreativeInstanceIDKey);
597598
const std::string wallpaper_id = base::GenerateGUID();
599+
service->BrandedWallpaperWillBeDisplayed(wallpaper_id, creative_instance_id);
600+
601+
constexpr char kBrandedWallpaperKey[] = "brandedWallpaper";
598602
data.SetStringKey(ntp_background_images::kWallpaperIDKey, wallpaper_id);
599603
wallpaper.SetKey(kBrandedWallpaperKey, std::move(data));
600-
service->BrandedWallpaperWillBeDisplayed(wallpaper_id);
601604
ResolveJavascriptCallback(args[0], std::move(wallpaper));
602605
}
603606

browser/ui/webui/new_tab_page/brave_new_tab_message_handler.h

+1
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

+4-4
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,9 @@ class AdsService : public KeyedService {
142139
const std::string& creative_instance_id,
143140
const ads::mojom::InlineContentAdEventType event_type) = 0;
144141

142+
virtual void GetNewTabPageAd() = 0;
143+
virtual absl::optional<std::string> GetCachedNewTabPageAd() = 0;
144+
145145
virtual void PurgeOrphanedAdEventsForType(
146146
const ads::mojom::AdType ad_type) = 0;
147147

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

+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 kNewTabPageSponsoredImageAds;
45+
4446
} // namespace features
4547
} // namespace brave_ads
4648

components/ntp_background_images/browser/ntp_sponsored_images_data.cc

+40
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);
@@ -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

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

112+
void ViewCounterModel::OnBrandedWallpaperNotPermitted() {
113+
// Increase background image index
114+
current_wallpaper_image_index_++;
115+
current_wallpaper_image_index_ %= total_image_count_;
116+
}
117+
112118
void ViewCounterModel::Reset() {
113119
current_wallpaper_image_index_ = 0;
114120
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 OnBrandedWallpaperNotPermitted();
4748

4849
private:
4950
static const int kInitialCountToBrandedWallpaper = 1;

0 commit comments

Comments
 (0)