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

Fixes application crashes when calculating the winning page classification category - 1.4.x #4791

Merged
merged 1 commit into from
Mar 2, 2020
Merged
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
15 changes: 12 additions & 3 deletions vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ bool AdsImpl::ShouldClassifyPagesIfTargeted() const {
std::string AdsImpl::ClassifyPage(
const std::string& url,
const std::string& html) {
DCHECK(user_model_);
auto page_score = user_model_->ClassifyPage(html);

auto winning_category = GetWinningCategory(page_score);
Expand All @@ -775,9 +776,15 @@ std::string AdsImpl::ClassifyPage(
}

std::vector<std::string> AdsImpl::GetWinningCategories() {
std::vector<std::string> winning_categories;

if (!ShouldClassifyPagesIfTargeted()) {
return winning_categories;
}

auto page_score_history = client_->GetPageScoreHistory();
if (page_score_history.size() == 0) {
return {};
if (page_score_history.empty()) {
return winning_categories;
}

uint64_t count = page_score_history.front().size();
Expand All @@ -788,6 +795,7 @@ std::vector<std::string> AdsImpl::GetWinningCategories() {

for (const auto& page_score : page_score_history) {
DCHECK(page_score.size() == count);
DCHECK(user_model_);

for (size_t i = 0; i < page_score.size(); i++) {
auto taxonomy = user_model_->GetTaxonomyAtIndex(i);
Expand All @@ -806,7 +814,6 @@ std::vector<std::string> AdsImpl::GetWinningCategories() {
std::sort(sorted_winning_category_page_scores.begin(),
sorted_winning_category_page_scores.end(), std::greater<double>());

std::vector<std::string> winning_categories;
for (const auto& page_score : sorted_winning_category_page_scores) {
if (page_score == 0.0) {
continue;
Expand All @@ -815,6 +822,7 @@ std::vector<std::string> AdsImpl::GetWinningCategories() {
auto it = std::find(winning_category_page_scores.begin(),
winning_category_page_scores.end(), page_score);
const int index = std::distance(winning_category_page_scores.begin(), it);
DCHECK(user_model_);
const std::string category = user_model_->GetTaxonomyAtIndex(index);
if (category.empty()) {
continue;
Expand All @@ -837,6 +845,7 @@ std::vector<std::string> AdsImpl::GetWinningCategories() {

std::string AdsImpl::GetWinningCategory(
const std::vector<double>& page_score) {
DCHECK(user_model_);
return user_model_->GetWinningCategory(page_score);
}

Expand Down