From 0fec67c66312eb35b2a05f3fa5f259b592fd22b0 Mon Sep 17 00:00:00 2001 From: Terry Mancey Date: Tue, 12 Mar 2019 02:57:43 +0000 Subject: [PATCH] Fix URL comparison for landed ad, easter egg and search engines --- .../src/bat/ads/internal/ads_impl.cc | 71 +++++++++++-------- .../src/bat/ads/internal/ads_impl.h | 6 +- .../src/bat/ads/internal/search_providers.cc | 10 +-- .../src/bat/ads/internal/search_providers.h | 54 +++++++------- .../src/bat/ads/internal/static_values.h | 4 +- .../bat/confirmations/confirmation_type.h | 4 -- 6 files changed, 80 insertions(+), 69 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 3db15f9f27cc..673c859f4bea 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 @@ -44,6 +44,7 @@ AdsImpl::AdsImpl(AdsClient* ads_client) : media_playing_({}), last_shown_tab_id_(0), last_shown_tab_url_(""), + previous_tab_url_(""), page_score_cache_({}), last_shown_notification_info_(NotificationInfo()), collect_activity_timer_id_(0), @@ -271,6 +272,7 @@ void AdsImpl::TabUpdated( << " and url: " << url; last_shown_tab_id_ = tab_id; + previous_tab_url_ = last_shown_tab_url_; last_shown_tab_url_ = url; TestShoppingData(url); @@ -373,22 +375,21 @@ void AdsImpl::ClassifyPage(const std::string& url, const std::string& html) { return; } - if (IsUrlFromLastShownNotification(url)) { + if (UrlHostsMatch(url, last_shown_notification_info_.url)) { BLOG(INFO) << "Site visited " << url << ", URL is from last shown notification"; return; } - auto gurl = GURL(url); - if (!gurl.SchemeIsHTTPOrHTTPS()) { - BLOG(INFO) << "Site visited " << url << ", invalid URL scheme"; + if (!IsSupportedUrl(url)) { + BLOG(INFO) << "Site visited " << url << ", unsupported URL"; return; } if (TestSearchState(url)) { - BLOG(INFO) << "Site visited " << url << ", testing search state"; + BLOG(INFO) << "Site visited " << url << ", URL is a search engine"; return; } @@ -418,7 +419,7 @@ void AdsImpl::ClassifyPage(const std::string& url, const std::string& html) { BLOG(INFO) << "Site visited " << url << ", immediateWinner is " << winning_category << " and winnerOverTime is " << winner_over_time_category << ", previous tab url " - << last_shown_tab_url_; + << previous_tab_url_; if (last_shown_tab_url_ == url) { LoadInfo load_info; @@ -427,6 +428,8 @@ void AdsImpl::ClassifyPage(const std::string& url, const std::string& html) { load_info.tab_classification = winning_category; GenerateAdReportingLoadEvent(load_info); } + + CheckEasterEgg(url); } std::string AdsImpl::GetWinnerOverTimeCategory() { @@ -481,8 +484,7 @@ void AdsImpl::TestShoppingData(const std::string& url) { return; } - auto gurl = GURL(url); - if (gurl.DomainIs("amazon.com")) { + if (UrlHostsMatch(url, kShoppingStateUrl)) { client_->FlagShoppingState(url, 1.0); } else { client_->UnflagShoppingState(); @@ -582,8 +584,7 @@ void AdsImpl::CheckEasterEgg(const std::string& url) { auto now_in_seconds = helper::Time::NowInSeconds(); - auto gurl = GURL(url); - if (gurl.DomainIs(kEasterEggUrl) && + if (UrlHostsMatch(url, kEasterEggUrl) && next_easter_egg_timestamp_in_seconds_ < now_in_seconds) { BLOG(INFO) << "Collect easter egg"; @@ -591,6 +592,7 @@ void AdsImpl::CheckEasterEgg(const std::string& url) { next_easter_egg_timestamp_in_seconds_ = now_in_seconds + kNextEasterEggStartsInSeconds; + BLOG(INFO) << "Next easter egg available in " << next_easter_egg_timestamp_in_seconds_ << " seconds"; } @@ -1072,15 +1074,11 @@ bool AdsImpl::IsSustainingAdInteraction() const { } bool AdsImpl::IsStillViewingAd() const { - auto last_shown_notification_info_url = - GURL(last_shown_notification_info_.url); - - if (last_shown_notification_info_url.DomainIs(last_shown_tab_url_)) { - auto last_shown_tab_url = GURL(last_shown_tab_url_); - + if (!UrlHostsMatch(last_shown_tab_url_, last_shown_notification_info_.url)) { BLOG(INFO) << "IsStillViewingAd last_shown_notification_info_url: " - << last_shown_notification_info_url.host() - << " does not match last_shown_tab_url:" << last_shown_tab_url.host(); + << last_shown_notification_info_.url + << " does not match last_shown_tab_url: " << last_shown_tab_url_; + return false; } @@ -1090,6 +1088,12 @@ bool AdsImpl::IsStillViewingAd() const { void AdsImpl::ConfirmAd( const NotificationInfo& info, const ConfirmationType type) { + if (IsNotificationFromSampleCatalog(info)) { + BLOG(INFO) << "Confirmation not made: Sample Ad"; + + return; + } + auto notification_info = std::make_unique(info); notification_info->type = type; @@ -1155,7 +1159,7 @@ void AdsImpl::GenerateAdReportingNotificationShownEvent( writer.EndArray(); writer.String("notificationCatalog"); - if (info.creative_set_id.empty()) { + if (IsNotificationFromSampleCatalog(info)) { writer.String("sample-catalog"); } else { writer.String(info.creative_set_id.c_str()); @@ -1204,17 +1208,20 @@ void AdsImpl::GenerateAdReportingNotificationResultEvent( writer.String("clicked"); client_->UpdateAdsUUIDSeen(info.uuid, 1); StartSustainingAdInteraction(kSustainAdInteractionAfterSeconds); + break; } case NotificationResultInfoResultType::DISMISSED: { writer.String("dismissed"); client_->UpdateAdsUUIDSeen(info.uuid, 1); + break; } case NotificationResultInfoResultType::TIMEOUT: { writer.String("timeout"); + break; } } @@ -1229,7 +1236,7 @@ void AdsImpl::GenerateAdReportingNotificationResultEvent( writer.EndArray(); writer.String("notificationCatalog"); - if (info.creative_set_id.empty()) { + if (IsNotificationFromSampleCatalog(info)) { writer.String("sample-catalog"); } else { writer.String(info.creative_set_id.c_str()); @@ -1323,8 +1330,7 @@ void AdsImpl::GenerateAdReportingConfirmationEvent( void AdsImpl::GenerateAdReportingLoadEvent( const LoadInfo& info) { - auto gurl = GURL(info.tab_url); - if (!gurl.SchemeIsHTTPOrHTTPS()) { + if (!IsSupportedUrl(info.tab_url)) { return; } @@ -1382,8 +1388,6 @@ void AdsImpl::GenerateAdReportingLoadEvent( auto* json = buffer.GetString(); ads_client_->EventLog(json); - - CheckEasterEgg(info.tab_url); } void AdsImpl::GenerateAdReportingBackgroundEvent() { @@ -1597,12 +1601,21 @@ void AdsImpl::GenerateAdReportingSettingsEvent() { ads_client_->EventLog(json); } -bool AdsImpl::IsUrlFromLastShownNotification(const std::string& url) { - if (url != last_shown_notification_info_.url) { - return false; - } +bool AdsImpl::IsNotificationFromSampleCatalog( + const NotificationInfo& info) const { + return info.creative_set_id.empty(); +} - return true; +bool AdsImpl::IsSupportedUrl(const std::string& url) const { + DCHECK(!url.empty()) << "Invalid URL"; + + return GURL(url).SchemeIsHTTPOrHTTPS(); +} + +bool AdsImpl::UrlHostsMatch( + const std::string& url_1, + const std::string& url_2) const { + return GURL(url_1).DomainIs(GURL(url_2).host_piece()); } } // namespace ads 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 b78b639a3264..4df20f637e2c 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 @@ -68,6 +68,7 @@ class AdsImpl : public Ads { int32_t last_shown_tab_id_; std::string last_shown_tab_url_; + std::string previous_tab_url_; void TabUpdated( const int32_t tab_id, const std::string& url, @@ -164,7 +165,10 @@ class AdsImpl : public Ads { void GenerateAdReportingRestartEvent(); void GenerateAdReportingSettingsEvent(); - bool IsUrlFromLastShownNotification(const std::string& url); + bool IsNotificationFromSampleCatalog(const NotificationInfo& info) const; + + bool IsSupportedUrl(const std::string& url) const; + bool UrlHostsMatch(const std::string& url_1, const std::string& url_2) const; std::unique_ptr client_; std::unique_ptr bundle_; diff --git a/vendor/bat-native-ads/src/bat/ads/internal/search_providers.cc b/vendor/bat-native-ads/src/bat/ads/internal/search_providers.cc index aa9d9959d32f..dd9eb9220eda 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/search_providers.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/search_providers.cc @@ -13,21 +13,21 @@ SearchProviders::SearchProviders() = default; SearchProviders::~SearchProviders() = default; bool SearchProviders::IsSearchEngine(const std::string& url) { - auto gurl = GURL(url); - - if (!gurl.has_host()) { + auto visited_url = GURL(url); + if (!visited_url.has_host()) { return false; } auto is_a_search = false; for (const auto& search_provider : _search_providers) { - if (search_provider.hostname.empty()) { + auto search_provider_hostname = GURL(search_provider.hostname); + if (!search_provider_hostname.is_valid()) { continue; } if (search_provider.is_always_classed_as_a_search && - gurl.DomainIs(search_provider.hostname)) { + visited_url.DomainIs(search_provider_hostname.host_piece())) { is_a_search = true; break; } diff --git a/vendor/bat-native-ads/src/bat/ads/internal/search_providers.h b/vendor/bat-native-ads/src/bat/ads/internal/search_providers.h index b47f9420f8db..863980204fcb 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/search_providers.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/search_providers.h @@ -16,111 +16,107 @@ namespace ads { static const std::vector _search_providers = { SearchProviderInfo( "Amazon", - "amazon.com", - "https://www.amazon.com/exec/obidos/external-search/" - "?field-keywords={searchTerms}&mode=blended", + "https://amazon.com", + "https://www.amazon.com/exec/obidos/external-search/?field-keywords={searchTerms}&mode=blended", // NOLINT false), SearchProviderInfo( "Bing", - "bing.com", + "https://bing.com", "https://www.bing.com/search?q={searchTerms}", true), SearchProviderInfo( "DuckDuckGo", - "duckduckgo.com", + "https://duckduckgo.com", "https://duckduckgo.com/?q={searchTerms}&t=brave", true), SearchProviderInfo( "Fireball", - "fireball.com", + "https://fireball.com", "https://fireball.com/?q={searchTerms}", true), SearchProviderInfo( "GitHub", - "github.com/search", + "https://github.com", "https://github.com/search?q={searchTerms}", false), SearchProviderInfo( "Google", - "google.com", + "https://google.com", "https://www.google.com/search?q={searchTerms}", true), SearchProviderInfo( "Stack Overflow", - "stackoverflow.com/search", + "https://stackoverflow.com", "https://stackoverflow.com/search?q={searchTerms}", false), SearchProviderInfo( "MDN Web Docs", - "developer.mozilla.org/search", + "https://developer.mozilla.org", "https://developer.mozilla.org/search?q={searchTerms}", false), SearchProviderInfo( "Twitter", - "twitter.com", + "https://twitter.com", "https://twitter.com/search?q={searchTerms}&source=desktop-search", false), SearchProviderInfo( "Wikipedia", - "en.wikipedia.org", + "https://en.wikipedia.org", "https://en.wikipedia.org/wiki/Special:Search?search={searchTerms}", false), SearchProviderInfo( "Yahoo", - "search.yahoo.com", + "https://search.yahoo.com", "https://search.yahoo.com/search?p={searchTerms}&fr=opensearch", true), SearchProviderInfo( "YouTube", - "youtube.com", - "https://www.youtube.com/results?search_type=search_videos&search_" - "query={searchTerms}&search_sort=relevance&search_category=0&page=", + "https://youtube.com", + "https://www.youtube.com/results?search_type=search_videos&search_query={searchTerms}&search_sort=relevance&search_category=0&page=", // NOLINT false), SearchProviderInfo( "StartPage", - "startpage.com", - "https://www.startpage.com/do/dsearch?" - "query={searchTerms}&cat=web&pl=opensearch", + "https://startpage.com", + "https://www.startpage.com/do/dsearch?query={searchTerms}&cat=web&pl=opensearch", // NOLINT true), SearchProviderInfo( "Infogalactic", - "infogalactic.com", - "https://infogalactic.com/w/index.php?title=" - "Special:Search&search={searchTerms}", + "https://infogalactic.com", + "https://infogalactic.com/w/index.php?title=Special:Search&search={searchTerms}", // NOLINT false), SearchProviderInfo( "Wolfram Alpha", - "wolframalpha.com", + "https://wolframalpha.com", "https://www.wolframalpha.com/input/?i={searchTerms}", false), SearchProviderInfo( "Semantic Scholar", - "semanticscholar.org", + "https://semanticscholar.org", "https://www.semanticscholar.org/search?q={searchTerms}", true), SearchProviderInfo( "Qwant", - "qwant.com", + "https://qwant.com", "https://www.qwant.com/?q={searchTerms}&client=brave", true), SearchProviderInfo( "Yandex", - "yandex.com", + "https://yandex.com", "https://yandex.com/search/?text={searchTerms}&clid=2274777", true), SearchProviderInfo( "Ecosia", - "ecosia.org", + "https://ecosia.org", "https://www.ecosia.org/search?q={searchTerms}", true), SearchProviderInfo( "searx", - "searx.me", + "https://searx.me", "https://searx.me/?q={searchTerms}&categories=general", true), SearchProviderInfo( "findx", - "findx.com", + "https://findx.com", "https://www.findx.com/search?q={searchTerms}&type=web", true) }; 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 649d710fb2c3..2d1d11fda48d 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 @@ -24,9 +24,11 @@ static const uint64_t kMaximumEntriesInAdsShownHistory = 99; static const uint64_t kDebugOneHourInSeconds = 25; -static char kEasterEggUrl[] = "iab.com"; +static char kEasterEggUrl[] = "https://iab.com"; static const uint64_t kNextEasterEggStartsInSeconds = 30; +static char kShoppingStateUrl[] = "https://amazon.com"; + static const uint64_t kSustainAdInteractionAfterSeconds = 10; static const uint64_t kDeliverNotificationsAfterSeconds = diff --git a/vendor/bat-native-confirmations/include/bat/confirmations/confirmation_type.h b/vendor/bat-native-confirmations/include/bat/confirmations/confirmation_type.h index 7bb24582b8ce..0bdf524e0e1f 100644 --- a/vendor/bat-native-confirmations/include/bat/confirmations/confirmation_type.h +++ b/vendor/bat-native-confirmations/include/bat/confirmations/confirmation_type.h @@ -6,10 +6,6 @@ #ifndef BAT_CONFIRMATIONS_CONFIRMATION_TYPE_H_ #define BAT_CONFIRMATIONS_CONFIRMATION_TYPE_H_ -#include - -#include "bat/confirmations/export.h" - namespace confirmations { static char kConfirmationTypeClick[] = "click";