From a4efeae8e89ae4ea02bfaac975b35182c447a2fb Mon Sep 17 00:00:00 2001 From: Terry Mancey Date: Tue, 13 Aug 2019 10:53:04 +0100 Subject: [PATCH 1/5] Refactor ClassifyPage to OnPageLoaded --- components/brave_ads/browser/ads_service.h | 4 ++-- components/brave_ads/browser/ads_service_impl.cc | 10 ++++++---- components/brave_ads/browser/ads_service_impl.h | 2 +- components/brave_ads/browser/ads_tab_helper.cc | 2 +- components/services/bat_ads/bat_ads_impl.cc | 6 +++--- components/services/bat_ads/bat_ads_impl.h | 2 +- .../services/bat_ads/public/interfaces/bat_ads.mojom | 2 +- vendor/bat-native-ads/README.md | 2 +- vendor/bat-native-ads/include/bat/ads/ads.h | 2 +- vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc | 5 +++-- vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h | 5 ++++- vendor/brave-ios/Ads/BATBraveAds.mm | 2 +- 12 files changed, 25 insertions(+), 19 deletions(-) diff --git a/components/brave_ads/browser/ads_service.h b/components/brave_ads/browser/ads_service.h index 161ccff0185a..ca6840981518 100644 --- a/components/brave_ads/browser/ads_service.h +++ b/components/brave_ads/browser/ads_service.h @@ -83,9 +83,9 @@ class AdsService : public KeyedService { // ads::Ads proxy virtual void SetConfirmationsIsReady(const bool is_ready) = 0; virtual void ChangeLocale(const std::string& locale) = 0; - virtual void ClassifyPage( + virtual void OnPageLoaded( const std::string& url, - const std::string& page) = 0; + const std::string& html) = 0; virtual void OnMediaStart(SessionID tab_id) = 0; virtual void OnMediaStop(SessionID tab_id) = 0; virtual void OnTabUpdated( diff --git a/components/brave_ads/browser/ads_service_impl.cc b/components/brave_ads/browser/ads_service_impl.cc index 4c61d512e3d0..bb0733e1972f 100644 --- a/components/brave_ads/browser/ads_service_impl.cc +++ b/components/brave_ads/browser/ads_service_impl.cc @@ -1360,12 +1360,14 @@ void AdsServiceImpl::ChangeLocale(const std::string& locale) { bat_ads_->ChangeLocale(locale); } -void AdsServiceImpl::ClassifyPage(const std::string& url, - const std::string& page) { - if (!connected()) +void AdsServiceImpl::OnPageLoaded( + const std::string& url, + const std::string& html) { + if (!connected()) { return; + } - bat_ads_->ClassifyPage(url, page); + bat_ads_->OnPageLoaded(url, html); } int AdsServiceImpl::GetIdleThreshold() { diff --git a/components/brave_ads/browser/ads_service_impl.h b/components/brave_ads/browser/ads_service_impl.h index c848c6b2c758..b109fb693809 100644 --- a/components/brave_ads/browser/ads_service_impl.h +++ b/components/brave_ads/browser/ads_service_impl.h @@ -72,7 +72,7 @@ class AdsServiceImpl : public AdsService, void SetConfirmationsIsReady(const bool is_ready) override; void ChangeLocale(const std::string& locale) override; - void ClassifyPage(const std::string& url, const std::string& page) override; + void OnPageLoaded(const std::string& url, const std::string& html) override; void OnMediaStart(SessionID tab_id) override; void OnMediaStop(SessionID tab_id) override; void OnTabUpdated( diff --git a/components/brave_ads/browser/ads_tab_helper.cc b/components/brave_ads/browser/ads_tab_helper.cc index 4b3175f996e2..3d82e1be3230 100644 --- a/components/brave_ads/browser/ads_tab_helper.cc +++ b/components/brave_ads/browser/ads_tab_helper.cc @@ -120,7 +120,7 @@ void AdsTabHelper::OnWebContentsDistillationDone( distiller_result->has_distilled_content() && distiller_result->has_markup_info() && distiller_result->distilled_content().has_html()) { - ads_service_->ClassifyPage(url.spec(), + ads_service_->OnPageLoaded(url.spec(), distiller_result->distilled_content().html()); } else { // TODO(bridiver) - fall back to web_contents()->GenerateMHTML or ignore? diff --git a/components/services/bat_ads/bat_ads_impl.cc b/components/services/bat_ads/bat_ads_impl.cc index 26690479dd00..07ed4bd94f1c 100644 --- a/components/services/bat_ads/bat_ads_impl.cc +++ b/components/services/bat_ads/bat_ads_impl.cc @@ -92,10 +92,10 @@ void BatAdsImpl::ChangeLocale(const std::string& locale) { ads_->ChangeLocale(locale); } -void BatAdsImpl::ClassifyPage( +void BatAdsImpl::OnPageLoaded( const std::string& url, - const std::string& page) { - ads_->ClassifyPage(url, page); + const std::string& html) { + ads_->OnPageLoaded(url, html); } void BatAdsImpl::ServeSampleAd() { diff --git a/components/services/bat_ads/bat_ads_impl.h b/components/services/bat_ads/bat_ads_impl.h index c45bb266b6fb..6581ca733154 100644 --- a/components/services/bat_ads/bat_ads_impl.h +++ b/components/services/bat_ads/bat_ads_impl.h @@ -37,7 +37,7 @@ class BatAdsImpl : void Shutdown(ShutdownCallback callback) override; void SetConfirmationsIsReady(const bool is_ready) override; void ChangeLocale(const std::string& locale) override; - void ClassifyPage(const std::string& url, const std::string& page) override; + void OnPageLoaded(const std::string& url, const std::string& html) override; void ServeSampleAd() override; void OnTimer(const uint32_t timer_id) override; void OnUnIdle() override; diff --git a/components/services/bat_ads/public/interfaces/bat_ads.mojom b/components/services/bat_ads/public/interfaces/bat_ads.mojom index 4afa8bd93f2a..392c86d3d14d 100644 --- a/components/services/bat_ads/public/interfaces/bat_ads.mojom +++ b/components/services/bat_ads/public/interfaces/bat_ads.mojom @@ -66,7 +66,7 @@ interface BatAds { Shutdown() => (int32 result); SetConfirmationsIsReady(bool is_ready); ChangeLocale(string locale); - ClassifyPage(string url, string page); + OnPageLoaded(string url, string html); ServeSampleAd(); OnTimer(uint32 timer_id); OnUnIdle(); diff --git a/vendor/bat-native-ads/README.md b/vendor/bat-native-ads/README.md index 8127a2b76d31..2527b9cf633d 100644 --- a/vendor/bat-native-ads/README.md +++ b/vendor/bat-native-ads/README.md @@ -62,7 +62,7 @@ void ChangeLocale( `ClassifyPage` should be called when a page has loaded in the current browser tab, and the HTML is available for analysis ``` -void ClassifyPage( +void OnPageLoaded( const std::string& url, const std::string& html) ``` diff --git a/vendor/bat-native-ads/include/bat/ads/ads.h b/vendor/bat-native-ads/include/bat/ads/ads.h index aee5ff860e70..34e3cf48e66e 100644 --- a/vendor/bat-native-ads/include/bat/ads/ads.h +++ b/vendor/bat-native-ads/include/bat/ads/ads.h @@ -73,7 +73,7 @@ class ADS_EXPORT Ads { // Should be called when a page has loaded in the current browser tab, and the // HTML is available for analysis - virtual void ClassifyPage( + virtual void OnPageLoaded( const std::string& url, const std::string& html) = 0; diff --git a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc index 222568bbe1f3..5939f3fd0e9e 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc @@ -538,10 +538,11 @@ void AdsImpl::ChangeLocale(const std::string& locale) { LoadUserModel(); } -void AdsImpl::ClassifyPage(const std::string& url, const std::string& html) { +void AdsImpl::OnPageLoaded( + const std::string& url, + const std::string& html) { if (!IsInitialized()) { BLOG(INFO) << "Site visited " << url << ", not initialized"; - return; } diff --git a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h index bd186aea70ab..ecc22dd0dd2f 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h @@ -131,7 +131,10 @@ class AdsImpl : public Ads { void ChangeLocale(const std::string& locale) override; - void ClassifyPage(const std::string& url, const std::string& html) override; + void OnPageLoaded( + const std::string& url, + const std::string& html) override; + std::string GetWinnerOverTimeCategory(); std::string GetWinningCategory(const std::vector& page_score); std::string GetWinningCategory(const std::string& html); diff --git a/vendor/brave-ios/Ads/BATBraveAds.mm b/vendor/brave-ios/Ads/BATBraveAds.mm index 059b4a5f0827..a42994ad849f 100644 --- a/vendor/brave-ios/Ads/BATBraveAds.mm +++ b/vendor/brave-ios/Ads/BATBraveAds.mm @@ -206,7 +206,7 @@ - (void)applicationDidBackground - (void)reportLoadedPageWithURL:(NSURL *)url html:(NSString *)html { const auto urlString = std::string(url.absoluteString.UTF8String); - ads->ClassifyPage(urlString, std::string(html.UTF8String)); + ads->OnPageLoaded(urlString, std::string(html.UTF8String)); } - (void)reportMediaStartedWithTabId:(NSInteger)tabId From 75417636151020fd60a5a4ef0ec034e7f2bcbb0a Mon Sep 17 00:00:00 2001 From: Terry Mancey Date: Tue, 13 Aug 2019 10:59:00 +0100 Subject: [PATCH 2/5] Decouple supported regions --- vendor/bat-native-ads/src/bat/ads/ads.cc | 19 +++++-------------- .../src/bat/ads/internal/static_values.h | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/vendor/bat-native-ads/src/bat/ads/ads.cc b/vendor/bat-native-ads/src/bat/ads/ads.cc index ae7711355df4..eab21f31d9c8 100644 --- a/vendor/bat-native-ads/src/bat/ads/ads.cc +++ b/vendor/bat-native-ads/src/bat/ads/ads.cc @@ -7,6 +7,7 @@ #include "bat/ads/internal/ads_impl.h" #include "bat/ads/internal/locale_helper.h" +#include "bat/ads/internal/static_values.h" namespace ads { @@ -25,20 +26,10 @@ Ads* Ads::CreateInstance(AdsClient* ads_client) { } bool Ads::IsSupportedRegion(const std::string& locale) { - auto region = helper::Locale::GetCountryCode(locale); - - std::vector regions = { - "US", // United States of America - "CA", // Canada - "GB", // United Kingdom (Great Britain and Northern Ireland) - "DE", // Germany - "FR", // France - "AU", // Australia - "NZ", // New Zealand - "IE" // Ireland - }; - - if (std::find(regions.begin(), regions.end(), region) == regions.end()) { + auto region = GetRegion(locale); + + auto it = kSupportedRegions.find(region); + if (it == kSupportedRegions.end()) { return false; } diff --git a/vendor/bat-native-ads/src/bat/ads/internal/static_values.h b/vendor/bat-native-ads/src/bat/ads/internal/static_values.h index 106733711928..79778eb6caa3 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/static_values.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/static_values.h @@ -7,6 +7,8 @@ #define BAT_ADS_INTERNAL_STATIC_VALUES_H_ #include +#include +#include #include "base/time/time.h" @@ -37,6 +39,19 @@ static const uint64_t kDebugCatalogPing = 15 * base::Time::kSecondsPerMinute; static char kDefaultLanguageCode[] = "en"; static char kDefaultCountryCode[] = "US"; +static const std::map kSupportedRegions = { + // {{region, targeted}} + + { "US", true }, // United States of America + { "CA", true }, // Canada + { "GB", true }, // United Kingdom (Great Britain and Northern Ireland) + { "DE", true }, // Germany + { "FR", true }, // France + { "AU", true }, // Australia + { "NZ", true }, // New Zealand + { "IE", true } // Ireland +}; + } // namespace ads #endif // BAT_ADS_INTERNAL_STATIC_VALUES_H_ From aedd42208bbfafe35cf6911ebe5cbdfaf746b65b Mon Sep 17 00:00:00 2001 From: Terry Mancey Date: Tue, 13 Aug 2019 11:00:05 +0100 Subject: [PATCH 3/5] Ad matching for ads that are not categorized --- .../src/bat/ads/internal/ads_impl.cc | 283 ++++++++++-------- .../src/bat/ads/internal/ads_impl.h | 44 ++- .../src/bat/ads/internal/static_values.h | 2 + 3 files changed, 198 insertions(+), 131 deletions(-) diff --git a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc index 5939f3fd0e9e..618105085939 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc @@ -61,8 +61,8 @@ AdsImpl::AdsImpl(AdsClient* ads_client) : is_first_run_(true), is_foreground_(false), media_playing_({}), - last_shown_tab_id_(0), - last_shown_tab_url_(""), + active_tab_id_(0), + active_tab_url_(""), previous_tab_url_(""), page_score_cache_({}), last_shown_notification_info_(NotificationInfo()), @@ -157,8 +157,11 @@ void AdsImpl::InitializeStep4(const Result result) { bool AdsImpl::IsInitialized() { if (!is_initialized_ || - !ads_client_->IsAdsEnabled() || - !user_model_->IsInitialized()) { + !ads_client_->IsAdsEnabled()) { + return false; + } + + if (ShouldClassifyPages() && !user_model_->IsInitialized()) { return false; } @@ -301,9 +304,9 @@ void AdsImpl::OnMediaStopped(const int32_t tab_id) { } bool AdsImpl::IsMediaPlaying() const { - auto tab = media_playing_.find(last_shown_tab_id_); + auto tab = media_playing_.find(active_tab_id_); if (tab == media_playing_.end()) { - // Media is not playing in the last shown tab + // Media is not playing in the active tab return false; } @@ -398,9 +401,9 @@ void AdsImpl::OnTabUpdated( BLOG(INFO) << "OnTabUpdated.IsFocused for tab id: " << tab_id << " and url: " << url; - last_shown_tab_id_ = tab_id; - previous_tab_url_ = last_shown_tab_url_; - last_shown_tab_url_ = url; + active_tab_id_ = tab_id; + previous_tab_url_ = active_tab_url_; + active_tab_url_ = url; TestShoppingData(url); TestSearchState(url); @@ -523,10 +526,18 @@ bool AdsImpl::ToggleFlagAd(const std::string& id, void AdsImpl::ChangeLocale(const std::string& locale) { auto language_code = helper::Locale::GetLanguageCode(locale); - auto locales = ads_client_->GetLocales(); + if (!ShouldClassifyPages()) { + client_->SetLocale(language_code); + + InitializeStep4(SUCCESS); + return; + } + + auto locales = client_->GetLocales(); if (std::find(locales.begin(), locales.end(), language_code) != locales.end()) { BLOG(INFO) << "Changed locale to " << language_code; + client_->SetLocale(language_code); } else { BLOG(INFO) << language_code << " locale not found, so changed locale to " @@ -563,52 +574,71 @@ void AdsImpl::OnPageLoaded( if (!IsSupportedUrl(url)) { BLOG(INFO) << "Site visited " << url << ", unsupported URL"; - return; } if (TestSearchState(url)) { BLOG(INFO) << "Site visited " << url << ", URL is a search engine"; - return; } TestShoppingData(url); - auto page_score = user_model_->ClassifyPage(html); - auto winning_category = GetWinningCategory(page_score); - if (winning_category.empty()) { - BLOG(INFO) << "Site visited " << url - << ", not enough content to classify page"; + MaybeClassifyPage(url, html); + CheckEasterEgg(url); + + BLOG(INFO) << "Site visited " << url << ", previous tab url was " + << previous_tab_url_; +} + +void AdsImpl::MaybeClassifyPage( + const std::string& url, + const std::string& html) { + if (!ShouldClassifyPages()) { + MaybeGenerateAdReportingLoadEvent(url, kUntargetedPageClassification); return; } - client_->SetLastPageClassification(winning_category); - - client_->AppendPageScoreToPageScoreHistory(page_score); + auto classification = ClassifyPage(url, html); + MaybeGenerateAdReportingLoadEvent(url, classification); +} - CachePageScore(last_shown_tab_url_, page_score); +bool AdsImpl::ShouldClassifyPages() const { + auto locale = ads_client_->GetAdsLocale(); + auto region = helper::Locale::GetCountryCode(locale); - // TODO(Terry Mancey): Implement Log (#44) - // 'Site visited', { url, immediateWinner, winnerOverTime } + auto it = kSupportedRegions.find(region); + if (it == kSupportedRegions.end()) { + return false; + } - auto winner_over_time_category = GetWinnerOverTimeCategory(); + return it->second; +} - BLOG(INFO) << "Site visited " << url << ", immediateWinner is " - << winning_category << " and winnerOverTime is " - << winner_over_time_category << ", previous tab url " - << previous_tab_url_; +std::string AdsImpl::ClassifyPage( + const std::string& url, + const std::string& html) { + auto page_score = user_model_->ClassifyPage(html); - if (last_shown_tab_url_ == url) { - LoadInfo load_info; - load_info.tab_id = last_shown_tab_id_; - load_info.tab_url = last_shown_tab_url_; - load_info.tab_classification = winning_category; - GenerateAdReportingLoadEvent(load_info); + auto winning_category = GetWinningCategory(page_score); + if (winning_category.empty()) { + BLOG(INFO) << "Failed to classify page at " << url + << " as not enough content"; + return ""; } - CheckEasterEgg(url); + client_->SetLastPageClassification(winning_category); + + client_->AppendPageScoreToPageScoreHistory(page_score); + + CachePageScore(active_tab_url_, page_score); + + BLOG(INFO) << "Successfully classified page at " << url << " as " + << winning_category << ". Winning category over time is " + << GetWinnerOverTimeCategory(); + + return winning_category; } std::string AdsImpl::GetWinnerOverTimeCategory() { @@ -624,9 +654,7 @@ std::string AdsImpl::GetWinnerOverTimeCategory() { winner_over_time_page_score.end(), 0); for (const auto& page_score : page_score_history) { - if (page_score.size() != count) { - return ""; - } + DCHECK(page_score.size() == count); for (size_t i = 0; i < page_score.size(); i++) { auto taxonomy = user_model_->GetTaxonomyAtIndex(i); @@ -649,11 +677,6 @@ std::string AdsImpl::GetWinningCategory( return user_model_->GetWinningCategory(page_score); } -std::string AdsImpl::GetWinningCategory(const std::string& html) { - auto page_score = user_model_->ClassifyPage(html); - return GetWinningCategory(page_score); -} - void AdsImpl::CachePageScore( const std::string& url, const std::vector& page_score) { @@ -763,7 +786,6 @@ void AdsImpl::OnLoadSampleBundle( auto ad_rand = base::RandInt(0, ads_count - 1); auto ad = ads.at(ad_rand); - ShowAd(ad, category); } @@ -788,44 +810,31 @@ void AdsImpl::CheckEasterEgg(const std::string& url) { } } -void AdsImpl::CheckReadyAdServe(const bool forced) { +void AdsImpl::CheckReadyAdServe( + const bool forced) { if (!IsInitialized() || !bundle_->IsReady()) { BLOG(INFO) << "Notification not made: Not initialized"; - return; } if (!forced) { if (!is_confirmations_ready_) { BLOG(INFO) << "Notification not made: Confirmations not ready"; - return; } if (!IsForeground()) { - // TODO(Terry Mancey): Implement Log (#44) - // 'Notification not made', { reason: 'not in foreground' } - BLOG(INFO) << "Notification not made: Not in foreground"; - return; } if (IsMediaPlaying()) { - // TODO(Terry Mancey): Implement Log (#44) - // 'Notification not made', { reason: 'media playing in browser' } - BLOG(INFO) << "Notification not made: Media playing in browser"; - return; } - if (!IsAllowedToShowAds()) { - // TODO(Terry Mancey): Implement Log (#44) - // 'Notification not made', { reason: 'not allowed based on history' } - + if (!IsAllowedToServeAds()) { BLOG(INFO) << "Notification not made: Not allowed based on history"; - return; } } @@ -834,105 +843,121 @@ void AdsImpl::CheckReadyAdServe(const bool forced) { ServeAdFromCategory(category); } -void AdsImpl::ServeAdFromCategory(const std::string& category) { +void AdsImpl::ServeAdFromCategory( + const std::string& category) { BLOG(INFO) << "Notification for category " << category; std::string catalog_id = bundle_->GetCatalogId(); if (catalog_id.empty()) { - // TODO(Terry Mancey): Implement Log (#44) - // 'Notification not made', { reason: 'no ad catalog' } - BLOG(INFO) << "Notification not made: No ad catalog"; - return; } - if (category.empty()) { - // TODO(Terry Mancey): Implement Log (#44) - // 'Notification not made', { reason: 'no classified pages) for - // winnerOverTime', category, winnerOverTime, arbitraryKey } - - BLOG(INFO) << "Notification not made: category is empty"; - + if (!category.empty()) { + auto callback = + std::bind(&AdsImpl::OnServeAdFromCategory, this, _1, _2, _3); + ads_client_->GetAds(category, callback); return; } - auto callback = std::bind(&AdsImpl::OnGetAds, this, _1, _2, _3); - ads_client_->GetAds(category, callback); + BLOG(INFO) << "Notification not made: Category is empty, trying " + << "again with untargeted category"; + + ServeUntargetedAd(); } -void AdsImpl::OnGetAds( +void AdsImpl::OnServeAdFromCategory( const Result result, const std::string& category, const std::vector& ads) { - if (result != SUCCESS) { - auto pos = category.find_last_of('-'); - if (pos != std::string::npos) { - std::string new_category = category.substr(0, pos); - - BLOG(INFO) << "Notification not made: No ads found in \"" << category - << "\" category, trying again with \"" << new_category - << "\" category"; - - auto callback = std::bind(&AdsImpl::OnGetAds, this, _1, _2, _3); - ads_client_->GetAds(new_category, callback); + auto eligible_ads = GetEligibleAds(ads); + if (!eligible_ads.empty()) { + ServeAd(category, eligible_ads); + return; + } - return; - } + if (ServeAdFromParentCategory(category, eligible_ads)) { + return; + } - if (ads.empty()) { - // TODO(Terry Mancey): Implement Log (#44) - // 'Notification not made', { reason: 'no ads for category', category } + BLOG(INFO) << "Notification not made: No ads found in \"" << category + << "\" category, trying again with untargeted category"; - BLOG(INFO) << "Notification not made: No ads found in \"" << category - << "\" category"; + ServeUntargetedAd(); +} - return; - } +bool AdsImpl::ServeAdFromParentCategory( + const std::string& category, + const std::vector& ads) { + auto pos = category.find_last_of('-'); + if (pos == std::string::npos) { + return false; } - auto available_ads = GetAvailableAds(ads); + std::string parent_category = category.substr(0, pos); - BLOG(INFO) << "Found " << available_ads.size() << " out of " << ads.size() - << " availables ads for \"" << category << "\" category"; + BLOG(INFO) << "Notification not made: No ads found in \"" << category + << "\" category, trying again with \"" << parent_category + << "\" category"; - if (available_ads.empty()) { - // TODO(Terry Mancey): Implement Log (#44) - // 'Notification not made', { reason: 'no ad (or permitted ad) for - // winnerOverTime', category, winnerOverTime, arbitraryKey } + auto callback = + std::bind(&AdsImpl::OnServeAdFromCategory, this, _1, _2, _3); + ads_client_->GetAds(parent_category, callback); - BLOG(INFO) << "Notification not made: No ad (or permitted ad) for \"" - << category << "\" category"; + return true; +} +void AdsImpl::ServeUntargetedAd() { + auto callback = std::bind(&AdsImpl::OnServeUntargetedAd, this, _1, _2, _3); + ads_client_->GetAds(kUntargetedPageClassification, callback); +} + +void AdsImpl::OnServeUntargetedAd( + const Result result, + const std::string& category, + const std::vector& ads) { + auto eligible_ads = GetEligibleAds(ads); + if (!eligible_ads.empty()) { + ServeAd(category, eligible_ads); return; } - auto rand = base::RandInt(0, available_ads.size() - 1); - auto ad = available_ads.at(rand); + BLOG(INFO) << "Notification not made: No ad (or eligible ad) for \"" + << category << "\" category"; +} + +void AdsImpl::ServeAd( + const std::string& category, + const std::vector& ads) { + BLOG(INFO) << "Found " << ads.size() << " eligible ads for \"" << category + << "\" category"; + + auto rand = base::RandInt(0, ads.size() - 1); + auto ad = ads.at(rand); ShowAd(ad, category); } -std::vector AdsImpl::GetAvailableAds( +std::vector AdsImpl::GetEligibleAds( const std::vector& ads) { - std::vector available_ads = {}; + std::vector eligible_ads = {}; for (const auto& ad : ads) { if (!AdRespectsTotalMaxFrequencyCapping(ad)) { - BLOG(INFO) << "creativeSetId " << ad.creative_set_id + BLOG(WARNING) << "creativeSetId " << ad.creative_set_id << " has exceeded the totalMax"; continue; } if (!AdRespectsPerDayFrequencyCapping(ad)) { - BLOG(INFO) << "creativeSetId " << ad.creative_set_id + BLOG(WARNING) << "creativeSetId " << ad.creative_set_id << " has exceeded the perDay"; continue; } if (!AdRespectsDailyCapFrequencyCapping(ad)) { - BLOG(INFO) << "creativeSetId " << ad.creative_set_id + BLOG(WARNING) << "creativeSetId " << ad.creative_set_id << " has exceeded the dailyCap"; continue; @@ -950,10 +975,10 @@ std::vector AdsImpl::GetAvailableAds( continue; } - available_ads.push_back(ad); + eligible_ads.push_back(ad); } - return available_ads; + return eligible_ads; } bool AdsImpl::AdRespectsTotalMaxFrequencyCapping(const AdInfo& ad) { @@ -1016,8 +1041,8 @@ bool AdsImpl::IsAdValid(const AdInfo& ad_info) { << std::endl << " advertiser: " << ad_info.advertiser << std::endl << " notificationText: " << ad_info.notification_text << std::endl << " notificationUrl: " << ad_info.notification_url - << std::endl << " creativeSetId: " << ad_info.notification_url - << std::endl << " uuid: " << ad_info.notification_url; + << std::endl << " creativeSetId: " << ad_info.creative_set_id + << std::endl << " uuid: " << ad_info.uuid; return false; } @@ -1106,19 +1131,19 @@ bool AdsImpl::HistoryRespectsRollingTimeConstraint( return false; } -bool AdsImpl::IsAllowedToShowAds() { +bool AdsImpl::IsAllowedToServeAds() { auto does_history_respect_ads_per_day_limit = DoesHistoryRespectAdsPerDayLimit(); bool does_history_respect_minimum_wait_time; if (!IsMobile()) { does_history_respect_minimum_wait_time = - DoesHistoryRespectMinimumWaitTimeToShowAds(); + DoesHistoryRespectMinimumWaitTimeToServeAds(); } else { does_history_respect_minimum_wait_time = true; } - BLOG(INFO) << "IsAllowedToShowAds:"; + BLOG(INFO) << "IsAllowedToServeAds:"; BLOG(INFO) << " does_history_respect_minimum_wait_time: " << does_history_respect_minimum_wait_time; BLOG(INFO) << " does_history_respect_ads_per_day_limit: " @@ -1128,7 +1153,7 @@ bool AdsImpl::IsAllowedToShowAds() { does_history_respect_ads_per_day_limit; } -bool AdsImpl::DoesHistoryRespectMinimumWaitTimeToShowAds() { +bool AdsImpl::DoesHistoryRespectMinimumWaitTimeToServeAds() { auto ads_shown_history = client_->GetAdsShownHistory(); auto hour_window = base::Time::kSecondsPerHour; @@ -1140,7 +1165,7 @@ bool AdsImpl::DoesHistoryRespectMinimumWaitTimeToShowAds() { auto respects_minimum_wait_time = HistoryRespectsRollingTimeConstraint( ads_shown_history, minimum_wait_time, 0); - BLOG(INFO) << "DoesHistoryRespectMinimumWaitTimeToShowAds:"; + BLOG(INFO) << "DoesHistoryRespectMinimumWaitTimeToServeAds:"; BLOG(INFO) << " respects_hour_limit: " << respects_hour_limit; BLOG(INFO) << " respects_minimum_wait_time: " @@ -1383,10 +1408,10 @@ bool AdsImpl::IsSustainingAdInteraction() const { } bool AdsImpl::IsStillViewingAd() const { - if (!UrlHostsMatch(last_shown_tab_url_, last_shown_notification_info_.url)) { + if (!UrlHostsMatch(active_tab_url_, last_shown_notification_info_.url)) { BLOG(INFO) << "IsStillViewingAd last_shown_notification_info_url: " << last_shown_notification_info_.url - << " does not match last_shown_tab_url: " << last_shown_tab_url_; + << " does not match last_shown_tab_url: " << active_tab_url_; return false; } @@ -1614,6 +1639,20 @@ void AdsImpl::GenerateAdReportingConfirmationEvent( ads_client_->EventLog(json); } +void AdsImpl::MaybeGenerateAdReportingLoadEvent( + const std::string& url, + const std::string& classification) { + if (active_tab_url_ != url) { + return; + } + + LoadInfo load_info; + load_info.tab_id = active_tab_id_; + load_info.tab_url = active_tab_url_; + load_info.tab_classification = classification; + GenerateAdReportingLoadEvent(load_info); +} + void AdsImpl::GenerateAdReportingLoadEvent( const LoadInfo& info) { if (!IsSupportedUrl(info.tab_url)) { diff --git a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h index ecc22dd0dd2f..82bcafcc3d7d 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h @@ -93,8 +93,8 @@ class AdsImpl : public Ads { const std::string& id, const NotificationInfo& notification); - int32_t last_shown_tab_id_; - std::string last_shown_tab_url_; + int32_t active_tab_id_; + std::string active_tab_url_; std::string previous_tab_url_; void OnTabUpdated( const int32_t tab_id, @@ -135,9 +135,16 @@ class AdsImpl : public Ads { const std::string& url, const std::string& html) override; + void MaybeClassifyPage( + const std::string& url, + const std::string& html); + bool ShouldClassifyPages() const; + std::string ClassifyPage( + const std::string& url, + const std::string& html); + std::string GetWinnerOverTimeCategory(); std::string GetWinningCategory(const std::vector& page_score); - std::string GetWinningCategory(const std::string& html); std::map> page_score_cache_; void CachePageScore( @@ -153,13 +160,28 @@ class AdsImpl : public Ads { const std::string& json); void CheckEasterEgg(const std::string& url); - void CheckReadyAdServe(const bool forced); - void ServeAdFromCategory(const std::string& category); - void OnGetAds( + + void CheckReadyAdServe( + const bool forced); + void ServeAdFromCategory( + const std::string& category); + void OnServeAdFromCategory( + const Result result, + const std::string& category, + const std::vector& ads); + bool ServeAdFromParentCategory( + const std::string& category, + const std::vector& ads); + void ServeUntargetedAd(); + void OnServeUntargetedAd( const Result result, const std::string& category, const std::vector& ads); - std::vector GetAvailableAds(const std::vector& ads); + void ServeAd( + const std::string& category, + const std::vector& ads); + std::vector GetEligibleAds( + const std::vector& ads); bool AdRespectsTotalMaxFrequencyCapping(const AdInfo& ad); bool AdRespectsPerDayFrequencyCapping(const AdInfo& ad); @@ -177,8 +199,8 @@ class AdsImpl : public Ads { const std::deque history, const uint64_t seconds_window, const uint64_t allowable_ad_count) const; - bool IsAllowedToShowAds(); - bool DoesHistoryRespectMinimumWaitTimeToShowAds(); + bool IsAllowedToServeAds(); + bool DoesHistoryRespectMinimumWaitTimeToServeAds(); bool DoesHistoryRespectAdsPerDayLimit(); uint32_t collect_activity_timer_id_; @@ -216,6 +238,10 @@ class AdsImpl : public Ads { void GenerateAdReportingConfirmationEvent(const NotificationInfo& info); void GenerateAdReportingConfirmationEvent(const std::string& uuid, const ConfirmationType& type); + + void MaybeGenerateAdReportingLoadEvent( + const std::string& url, + const std::string& classification); void GenerateAdReportingLoadEvent(const LoadInfo& info); void GenerateAdReportingBackgroundEvent(); void GenerateAdReportingForegroundEvent(); diff --git a/vendor/bat-native-ads/src/bat/ads/internal/static_values.h b/vendor/bat-native-ads/src/bat/ads/internal/static_values.h index 79778eb6caa3..9f5aec011c6d 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/static_values.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/static_values.h @@ -52,6 +52,8 @@ static const std::map kSupportedRegions = { { "IE", true } // Ireland }; +static char kUntargetedPageClassification[] = "untargeted"; + } // namespace ads #endif // BAT_ADS_INTERNAL_STATIC_VALUES_H_ From 83243f29285ea862b0b35fa5a194f013a904b7c5 Mon Sep 17 00:00:00 2001 From: Terry Mancey Date: Tue, 13 Aug 2019 14:25:51 +0100 Subject: [PATCH 4/5] Updated README.md --- vendor/bat-native-ads/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/bat-native-ads/README.md b/vendor/bat-native-ads/README.md index 2527b9cf633d..b4ba09427cd9 100644 --- a/vendor/bat-native-ads/README.md +++ b/vendor/bat-native-ads/README.md @@ -60,7 +60,7 @@ void ChangeLocale( const std::string& locale) ``` -`ClassifyPage` should be called when a page has loaded in the current browser tab, and the HTML is available for analysis +`OnPageLoaded` should be called when a page has loaded in the current browser tab, and the HTML is available for analysis ``` void OnPageLoaded( const std::string& url, From d6b9b6270a5ec7b1faf20587b18a4508e8ec647f Mon Sep 17 00:00:00 2001 From: Terry Mancey Date: Tue, 13 Aug 2019 20:22:30 +0100 Subject: [PATCH 5/5] Added support for Argentina, Austria, Brazil, Switzerland, Chile, Colombia, Denmark, Ecuador, Israel, India, Italy, Korea, Mexico, Netherlands, Peru, Philippines, Poland, Sweden, Singapore, Venezuela and South Africa to Ads --- .../src/bat/ads/internal/static_values.h | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/vendor/bat-native-ads/src/bat/ads/internal/static_values.h b/vendor/bat-native-ads/src/bat/ads/internal/static_values.h index 9f5aec011c6d..97b0f6d98f03 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/static_values.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/static_values.h @@ -49,7 +49,28 @@ static const std::map kSupportedRegions = { { "FR", true }, // France { "AU", true }, // Australia { "NZ", true }, // New Zealand - { "IE", true } // Ireland + { "IE", true }, // Ireland + { "AR", false }, // Argentina + { "AT", false }, // Austria + { "BR", false }, // Brazil + { "CH", false }, // Switzerland + { "CL", false }, // Chile + { "CO", false }, // Colombia + { "DK", false }, // Denmark + { "EC", false }, // Ecuador + { "IL", false }, // Israel + { "IN", false }, // India + { "IT", false }, // Italy + { "KR", false }, // Korea + { "MX", false }, // Mexico + { "NL", false }, // Netherlands + { "PE", false }, // Peru + { "PH", false }, // Philippines + { "PL", false }, // Poland + { "SE", false }, // Sweden + { "SG", false }, // Singapore + { "VE", false }, // Venezuela + { "ZA", false } // South Africa }; static char kUntargetedPageClassification[] = "untargeted";