Skip to content

Commit

Permalink
Fixes application crashes when calculating the winning page classific…
Browse files Browse the repository at this point in the history
…ation category
  • Loading branch information
tmancey committed Feb 27, 2020
1 parent 7283b1e commit 9874b93
Showing 1 changed file with 12 additions and 3 deletions.
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 @@ -834,6 +834,7 @@ bool AdsImpl::ShouldClassifyPagesIfTargeted() const {
std::string AdsImpl::ClassifyPage(
const std::string& url,
const std::string& content) {
DCHECK(user_model_);
auto page_score = user_model_->ClassifyPage(content);

auto winning_category = GetWinningCategory(page_score);
Expand All @@ -859,9 +860,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 @@ -872,6 +879,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 @@ -890,7 +898,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 @@ -899,6 +906,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 @@ -921,6 +929,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

0 comments on commit 9874b93

Please sign in to comment.