Skip to content
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

Fix URL comparison for landed ad, easter egg and search engine state #1914

Merged
merged 1 commit into from
Mar 12, 2019
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
71 changes: 42 additions & 29 deletions vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -582,15 +584,15 @@ 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";

CheckReadyAdServe(true);

next_easter_egg_timestamp_in_seconds_ =
now_in_seconds + kNextEasterEggStartsInSeconds;

BLOG(INFO) << "Next easter egg available in "
<< next_easter_egg_timestamp_in_seconds_ << " seconds";
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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<NotificationInfo>(info);

notification_info->type = type;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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());
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -1382,8 +1388,6 @@ void AdsImpl::GenerateAdReportingLoadEvent(

auto* json = buffer.GetString();
ads_client_->EventLog(json);

CheckEasterEgg(info.tab_url);
}

void AdsImpl::GenerateAdReportingBackgroundEvent() {
Expand Down Expand Up @@ -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
6 changes: 5 additions & 1 deletion vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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> client_;
std::unique_ptr<Bundle> bundle_;
Expand Down
10 changes: 5 additions & 5 deletions vendor/bat-native-ads/src/bat/ads/internal/search_providers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
54 changes: 25 additions & 29 deletions vendor/bat-native-ads/src/bat/ads/internal/search_providers.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,111 +16,107 @@ namespace ads {
static const std::vector<SearchProviderInfo> _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)
};
Expand Down
Loading