Chromium Code Reviews| Index: components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider.cc |
| diff --git a/components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider.cc b/components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider.cc |
| index c612b36191aad7426b62886cdeabefd1890e3739..8e8a8c946c048c27ce5199a802a4e363abc5efb9 100644 |
| --- a/components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider.cc |
| +++ b/components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider.cc |
| @@ -152,11 +152,13 @@ void PhysicalWebPageSuggestionsProvider::Fetch( |
| const std::set<std::string>& known_suggestion_ids, |
| const FetchDoneCallback& callback) { |
| DCHECK_EQ(category, provided_category_); |
| + std::vector<ContentSuggestion> suggestions = |
| + GetMostRecentPhysicalWebPagesWithFilter(kMaxSuggestionsCount, |
| + known_suggestion_ids); |
| + AppendToShownScannedUrls(suggestions); |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| - FROM_HERE, |
| - base::Bind(callback, Status::Success(), |
| - base::Passed(GetMostRecentPhysicalWebPagesWithFilter( |
| - kMaxSuggestionsCount, known_suggestion_ids)))); |
| + FROM_HERE, base::Bind(callback, Status::Success(), |
| + base::Passed(std::move(suggestions)))); |
| } |
| void PhysicalWebPageSuggestionsProvider::ClearHistory( |
| @@ -215,21 +217,25 @@ void PhysicalWebPageSuggestionsProvider::NotifyStatusChanged( |
| void PhysicalWebPageSuggestionsProvider::FetchPhysicalWebPages() { |
| DCHECK_EQ(CategoryStatus::AVAILABLE, category_status_); |
| + std::vector<ContentSuggestion> suggestions = |
| + GetMostRecentPhysicalWebPagesWithFilter( |
| + kMaxSuggestionsCount, |
| + /*excluded_ids=*/std::set<std::string>()); |
| + shown_scanned_urls_.clear(); |
| + AppendToShownScannedUrls(suggestions); |
| observer()->OnNewSuggestions(this, provided_category_, |
| - GetMostRecentPhysicalWebPagesWithFilter( |
| - kMaxSuggestionsCount, |
| - /*excluded_ids=*/std::set<std::string>())); |
| + std::move(suggestions)); |
| } |
| std::vector<ContentSuggestion> |
| PhysicalWebPageSuggestionsProvider::GetMostRecentPhysicalWebPagesWithFilter( |
| - int max_quantity, |
| + int max_count, |
| const std::set<std::string>& excluded_ids) { |
| std::unique_ptr<physical_web::MetadataList> page_metadata_list = |
| physical_web_data_source_->GetMetadataList(); |
| // These is to filter out dismissed suggestions and at the same time prune the |
| - // dismissed IDs list removing nonavailable pages (this is need since some |
| + // dismissed IDs list removing nonavailable pages (this is needed since some |
| // OnLost() calls may have been missed). |
| const std::set<std::string> old_dismissed_ids = ReadDismissedIDsFromPrefs(); |
| std::set<std::string> new_dismissed_ids; |
| @@ -256,7 +262,7 @@ PhysicalWebPageSuggestionsProvider::GetMostRecentPhysicalWebPagesWithFilter( |
| std::vector<ContentSuggestion> suggestions; |
| for (const auto& page_metadata : filtered_metadata_list) { |
| - if (static_cast<int>(suggestions.size()) == max_quantity) { |
| + if (static_cast<int>(suggestions.size()) == max_count) { |
| break; |
| } |
| suggestions.push_back(ConvertPhysicalWebPage(page_metadata)); |
| @@ -283,7 +289,24 @@ void PhysicalWebPageSuggestionsProvider::OnFound(const GURL& url) { |
| } |
| void PhysicalWebPageSuggestionsProvider::OnLost(const GURL& url) { |
| - InvalidateSuggestion(url.spec()); |
| + const auto& it = shown_scanned_urls_.find(url); |
|
Marc Treib
2017/01/31 11:02:41
nit: just "auto it", no need for reference to an i
vitaliii
2017/01/31 12:33:49
Done.
|
| + if (it == shown_scanned_urls_.end()) { |
| + // The suggestion may be shown on old NTPs. |
| + // TODO(vitaliii): Use |resolved_url| here when it is available. |
|
Marc Treib
2017/01/31 11:02:41
What does this mean? It looks like sometimes we in
vitaliii
2017/01/31 12:33:49
We should invalidate by resolved URL, because the
Marc Treib
2017/01/31 13:31:04
But that generally won't do anything, right? (Exce
vitaliii
2017/01/31 13:57:03
True.
Marc Treib
2017/01/31 14:25:58
Then I'd say, rather than doing this half-fix, we
vitaliii
2017/01/31 15:10:57
Please see below why I think that this is 99% fix.
Marc Treib
2017/01/31 15:38:31
The (presumed) 99% fix is the thing below - invali
vitaliii
2017/01/31 16:15:02
That was my assumption that it is better than not
|
| + InvalidateSuggestion(url.spec()); |
| + return; |
| + } |
| + |
| + const GURL lost_resolved_url = it->second; |
|
Marc Treib
2017/01/31 11:02:41
nit: ref?
vitaliii
2017/01/31 12:33:49
the multimap pair is removed in the next line.
Marc Treib
2017/01/31 13:31:04
Ah, good point. Worth a comment maybe :)
vitaliii
2017/01/31 13:57:03
Acknowledged.
Marc Treib
2017/01/31 14:25:58
...but not done? :P
vitaliii
2017/01/31 15:10:57
Done.
"Maybe" suggested that you did not insist a
Marc Treib
2017/01/31 15:38:31
Thanks!
I wouldn't have insisted, but if you choos
vitaliii
2017/01/31 16:15:02
I see, fair enough. My understanding of "ack" is "
|
| + shown_scanned_urls_.erase(it); |
| + for (const auto& pair : shown_scanned_urls_) { |
|
Marc Treib
2017/01/31 11:02:41
optional: Could probably be a find_if, since you a
vitaliii
2017/01/31 12:33:49
Done.
|
| + const GURL& resolved_url = pair.second; |
| + if (resolved_url == lost_resolved_url) { |
| + // There are remaining beacons transmiting this URL. |
| + return; |
| + } |
| + } |
| + InvalidateSuggestion(lost_resolved_url.spec()); |
| } |
| void PhysicalWebPageSuggestionsProvider::OnDistanceChanged( |
| @@ -306,6 +329,21 @@ void PhysicalWebPageSuggestionsProvider::InvalidateSuggestion( |
| } |
| } |
| +void PhysicalWebPageSuggestionsProvider::AppendToShownScannedUrls( |
| + const std::vector<ContentSuggestion>& suggestions) { |
| + std::unique_ptr<physical_web::MetadataList> page_metadata_list = |
| + physical_web_data_source_->GetMetadataList(); |
| + for (const auto& page_metadata : *page_metadata_list) { |
|
Marc Treib
2017/01/31 11:02:41
This seems quite convoluted. Isn't there a way to
vitaliii
2017/01/31 12:33:49
There may a way. However, if there are multiple sc
Marc Treib
2017/01/31 13:31:04
I'm not sure I follow. It seems to me that GetMost
vitaliii
2017/01/31 13:57:03
GetMostRecentPhysicalWebPagesWithFilter was create
Marc Treib
2017/01/31 14:25:58
The different treatment of the cache is easy to re
vitaliii
2017/01/31 15:10:57
I see, fair enough.
Marc Treib
2017/01/31 15:38:31
Ah, so the problem is that we filter out duplicate
vitaliii
2017/01/31 16:15:02
Yes, indeed.
|
| + if (std::find_if(suggestions.begin(), suggestions.end(), |
| + [page_metadata](const ContentSuggestion& suggestion) { |
| + return suggestion.url() == page_metadata.resolved_url; |
| + }) != suggestions.end()) { |
| + shown_scanned_urls_.insert(std::make_pair(page_metadata.scanned_url, |
| + page_metadata.resolved_url)); |
| + } |
| + } |
| +} |
| + |
| std::set<std::string> |
| PhysicalWebPageSuggestionsProvider::ReadDismissedIDsFromPrefs() const { |
| return prefs::ReadDismissedIDsFromPrefs( |