11
11
#include " base/logging.h"
12
12
#include " base/notreached.h"
13
13
#include " base/strings/stringprintf.h"
14
+ #include " bat/ads/new_tab_page_ad_info.h"
14
15
#include " brave/components/ntp_background_images/browser/url_constants.h"
15
16
#include " content/public/common/url_constants.h"
16
17
17
18
/* Sample photo.json.
18
19
{
19
20
"schemaVersion": 1,
21
+ "campaignId": "fb7ee174-5430-4fb9-8e97-29bf14e8d828",
20
22
"logo": {
21
23
"imageUrl": "logo.png",
22
24
"alt": "Visit Brave Software",
@@ -196,6 +198,10 @@ Campaign NTPSponsoredImagesData::GetCampaignFromValue(
196
198
197
199
Campaign campaign;
198
200
201
+ if (const std::string* campaign_id = value.FindStringKey (kCampaignIdKey )) {
202
+ campaign.campaign_id = *campaign_id;
203
+ }
204
+
199
205
Logo default_logo;
200
206
if (auto * logo = value.FindDictKey (kLogoKey )) {
201
207
default_logo = GetLogoFromValue (installed_dir, url_prefix, logo);
@@ -287,11 +293,11 @@ base::Value NTPSponsoredImagesData::GetBackgroundAt(size_t campaign_index,
287
293
DCHECK (campaign_index < campaigns.size () && background_index >= 0 &&
288
294
background_index < campaigns[campaign_index].backgrounds .size ());
289
295
290
- base::Value data (base::Value::Type::DICTIONARY);
291
296
const auto campaign = campaigns[campaign_index];
292
297
if (!campaign.IsValid ())
293
- return data ;
298
+ return base::Value () ;
294
299
300
+ base::Value data (base::Value::Type::DICTIONARY);
295
301
data.SetStringKey (kThemeNameKey , theme_name);
296
302
data.SetBoolKey (kIsSponsoredKey , !IsSuperReferral ());
297
303
data.SetBoolKey (kIsBackgroundKey , false );
@@ -324,6 +330,45 @@ base::Value NTPSponsoredImagesData::GetBackgroundAt(size_t campaign_index,
324
330
return data;
325
331
}
326
332
333
+ base::Value NTPSponsoredImagesData::GetBackgroundByAdInfo (
334
+ const ads::NewTabPageAdInfo& ad_info) {
335
+ // Find campaign
336
+ size_t campaign_index = 0 ;
337
+ for (; campaign_index != campaigns.size (); ++campaign_index) {
338
+ if (campaigns[campaign_index].campaign_id == ad_info.campaign_id ) {
339
+ break ;
340
+ }
341
+ }
342
+ if (campaign_index == campaigns.size ()) {
343
+ LOG (ERROR) << " Ad campaign wasn't found in NTP sposored images data: "
344
+ << ad_info.campaign_id ;
345
+ return base::Value ();
346
+ }
347
+
348
+ const auto & sponsored_backgrounds = campaigns[campaign_index].backgrounds ;
349
+ size_t background_index = 0 ;
350
+ for (; background_index != sponsored_backgrounds.size (); ++background_index) {
351
+ if (sponsored_backgrounds[background_index].creative_instance_id ==
352
+ ad_info.creative_instance_id ) {
353
+ break ;
354
+ }
355
+ }
356
+ if (background_index == sponsored_backgrounds.size ()) {
357
+ LOG (ERROR) << " Creative instance wasn't found in NTP sposored images data: "
358
+ << ad_info.creative_instance_id ;
359
+ return base::Value ();
360
+ }
361
+
362
+ if (!AdInfoMatchesSponsoredImage (ad_info, campaign_index, background_index)) {
363
+ LOG (WARNING) << " Served creative info does not fully match with sponsored "
364
+ " image metadata. Campaign id: "
365
+ << ad_info.campaign_id
366
+ << " . Creative instance id: " << ad_info.creative_instance_id ;
367
+ }
368
+
369
+ return GetBackgroundAt (campaign_index, background_index);
370
+ }
371
+
327
372
void NTPSponsoredImagesData::PrintCampaignsParsingResult () const {
328
373
VLOG (2 ) << __func__ << " : This is "
329
374
<< (IsSuperReferral () ? " NTP SR Data" : " NTP SI Data" );
@@ -339,4 +384,57 @@ void NTPSponsoredImagesData::PrintCampaignsParsingResult() const {
339
384
}
340
385
}
341
386
387
+ bool NTPSponsoredImagesData::AdInfoMatchesSponsoredImage (
388
+ const ads::NewTabPageAdInfo& ad_info,
389
+ size_t campaign_index,
390
+ size_t background_index) const {
391
+ DCHECK (campaign_index < campaigns.size () && background_index >= 0 &&
392
+ background_index < campaigns[campaign_index].backgrounds .size ());
393
+
394
+ const Campaign& campaign = campaigns[campaign_index];
395
+ if (!campaign.IsValid ()) {
396
+ return false ;
397
+ }
398
+ const SponsoredBackground& background =
399
+ campaign.backgrounds [background_index];
400
+
401
+ if (ad_info.campaign_id != campaign.campaign_id ) {
402
+ return false ;
403
+ }
404
+
405
+ if (ad_info.creative_instance_id != background.creative_instance_id ) {
406
+ return false ;
407
+ }
408
+
409
+ if (ad_info.target_url != background.logo .destination_url ) {
410
+ return false ;
411
+ }
412
+
413
+ if (base::FilePath::FromUTF8Unsafe (ad_info.image_url ).BaseName () !=
414
+ background.logo .image_file .BaseName ()) {
415
+ return false ;
416
+ }
417
+
418
+ if (ad_info.alt != background.logo .alt_text ) {
419
+ return false ;
420
+ }
421
+
422
+ if (ad_info.company_name != background.logo .company_name ) {
423
+ return false ;
424
+ }
425
+
426
+ const auto it = std::find_if (
427
+ ad_info.wallpapers .begin (), ad_info.wallpapers .end (),
428
+ [&background](const auto & wallpaper_info) {
429
+ if (base::FilePath::FromUTF8Unsafe (wallpaper_info.image_url )
430
+ .BaseName () != background.image_file .BaseName ()) {
431
+ return false ;
432
+ }
433
+ return wallpaper_info.focal_point .x == background.focal_point .x () &&
434
+ wallpaper_info.focal_point .y == background.focal_point .y ();
435
+ });
436
+
437
+ return it != ad_info.wallpapers .end ();
438
+ }
439
+
342
440
} // namespace ntp_background_images
0 commit comments