| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/ntp_snippets/content_suggestions_service.h" | 5 #include "components/ntp_snippets/content_suggestions_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 | 65 |
| 66 const std::vector<ContentSuggestion>& | 66 const std::vector<ContentSuggestion>& |
| 67 ContentSuggestionsService::GetSuggestionsForCategory(Category category) const { | 67 ContentSuggestionsService::GetSuggestionsForCategory(Category category) const { |
| 68 auto iterator = suggestions_by_category_.find(category); | 68 auto iterator = suggestions_by_category_.find(category); |
| 69 if (iterator == suggestions_by_category_.end()) | 69 if (iterator == suggestions_by_category_.end()) |
| 70 return no_suggestions_; | 70 return no_suggestions_; |
| 71 return iterator->second; | 71 return iterator->second; |
| 72 } | 72 } |
| 73 | 73 |
| 74 void ContentSuggestionsService::FetchSuggestionImage( | 74 void ContentSuggestionsService::FetchSuggestionImage( |
| 75 const std::string& suggestion_id, | 75 const ContentSuggestion::ID& suggestion_id, |
| 76 const ImageFetchedCallback& callback) { | 76 const ImageFetchedCallback& callback) { |
| 77 Category category = category_factory_.GetCategoryFromUniqueID(suggestion_id); | 77 if (!providers_by_category_.count(suggestion_id.category())) { |
| 78 if (!providers_by_category_.count(category)) { | |
| 79 LOG(WARNING) << "Requested image for suggestion " << suggestion_id | 78 LOG(WARNING) << "Requested image for suggestion " << suggestion_id |
| 80 << " for unavailable category " << category; | 79 << " for unavailable category " << suggestion_id.category(); |
| 81 base::ThreadTaskRunnerHandle::Get()->PostTask( | 80 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 82 FROM_HERE, base::Bind(callback, gfx::Image())); | 81 FROM_HERE, base::Bind(callback, gfx::Image())); |
| 83 return; | 82 return; |
| 84 } | 83 } |
| 85 providers_by_category_[category]->FetchSuggestionImage(suggestion_id, | 84 providers_by_category_[suggestion_id.category()]->FetchSuggestionImage( |
| 86 callback); | 85 suggestion_id, callback); |
| 87 } | 86 } |
| 88 | 87 |
| 89 void ContentSuggestionsService::ClearHistory( | 88 void ContentSuggestionsService::ClearHistory( |
| 90 base::Time begin, | 89 base::Time begin, |
| 91 base::Time end, | 90 base::Time end, |
| 92 const base::Callback<bool(const GURL& url)>& filter) { | 91 const base::Callback<bool(const GURL& url)>& filter) { |
| 93 for (const auto& provider : providers_) { | 92 for (const auto& provider : providers_) { |
| 94 provider->ClearHistory(begin, end, filter); | 93 provider->ClearHistory(begin, end, filter); |
| 95 } | 94 } |
| 96 } | 95 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 123 } | 122 } |
| 124 | 123 |
| 125 void ContentSuggestionsService::ClearDismissedSuggestionsForDebugging( | 124 void ContentSuggestionsService::ClearDismissedSuggestionsForDebugging( |
| 126 Category category) { | 125 Category category) { |
| 127 auto iterator = providers_by_category_.find(category); | 126 auto iterator = providers_by_category_.find(category); |
| 128 if (iterator != providers_by_category_.end()) | 127 if (iterator != providers_by_category_.end()) |
| 129 iterator->second->ClearDismissedSuggestionsForDebugging(category); | 128 iterator->second->ClearDismissedSuggestionsForDebugging(category); |
| 130 } | 129 } |
| 131 | 130 |
| 132 void ContentSuggestionsService::DismissSuggestion( | 131 void ContentSuggestionsService::DismissSuggestion( |
| 133 const std::string& suggestion_id) { | 132 const ContentSuggestion::ID& suggestion_id) { |
| 134 Category category = category_factory_.GetCategoryFromUniqueID(suggestion_id); | 133 if (!providers_by_category_.count(suggestion_id.category())) { |
| 135 if (!providers_by_category_.count(category)) { | |
| 136 LOG(WARNING) << "Dismissed suggestion " << suggestion_id | 134 LOG(WARNING) << "Dismissed suggestion " << suggestion_id |
| 137 << " for unavailable category " << category; | 135 << " for unavailable category " << suggestion_id.category(); |
| 138 return; | 136 return; |
| 139 } | 137 } |
| 140 providers_by_category_[category]->DismissSuggestion(suggestion_id); | 138 providers_by_category_[suggestion_id.category()]->DismissSuggestion( |
| 139 suggestion_id); |
| 141 | 140 |
| 142 // Remove the suggestion locally. | 141 // Remove the suggestion locally. |
| 143 bool removed = RemoveSuggestionByID(category, suggestion_id); | 142 bool removed = RemoveSuggestionByID(suggestion_id); |
| 144 DCHECK(removed) << "The dismissed suggestion " << suggestion_id | 143 DCHECK(removed) << "The dismissed suggestion " << suggestion_id |
| 145 << " has already been removed. Providers must not call" | 144 << " has already been removed. Providers must not call" |
| 146 << " OnNewSuggestions in response to DismissSuggestion."; | 145 << " OnNewSuggestions in response to DismissSuggestion."; |
| 147 } | 146 } |
| 148 | 147 |
| 149 void ContentSuggestionsService::DismissCategory(Category category) { | 148 void ContentSuggestionsService::DismissCategory(Category category) { |
| 150 auto providers_it = providers_by_category_.find(category); | 149 auto providers_it = providers_by_category_.find(category); |
| 151 if (providers_it == providers_by_category_.end()) | 150 if (providers_it == providers_by_category_.end()) |
| 152 return; | 151 return; |
| 153 | 152 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 DismissCategory(category); | 206 DismissCategory(category); |
| 208 } else { | 207 } else { |
| 209 RegisterCategoryIfRequired(provider, category); | 208 RegisterCategoryIfRequired(provider, category); |
| 210 DCHECK_EQ(new_status, provider->GetCategoryStatus(category)); | 209 DCHECK_EQ(new_status, provider->GetCategoryStatus(category)); |
| 211 } | 210 } |
| 212 NotifyCategoryStatusChanged(category); | 211 NotifyCategoryStatusChanged(category); |
| 213 } | 212 } |
| 214 | 213 |
| 215 void ContentSuggestionsService::OnSuggestionInvalidated( | 214 void ContentSuggestionsService::OnSuggestionInvalidated( |
| 216 ContentSuggestionsProvider* provider, | 215 ContentSuggestionsProvider* provider, |
| 217 Category category, | 216 const ContentSuggestion::ID& suggestion_id) { |
| 218 const std::string& suggestion_id) { | 217 RemoveSuggestionByID(suggestion_id); |
| 219 RemoveSuggestionByID(category, suggestion_id); | |
| 220 FOR_EACH_OBSERVER(Observer, observers_, | 218 FOR_EACH_OBSERVER(Observer, observers_, |
| 221 OnSuggestionInvalidated(category, suggestion_id)); | 219 OnSuggestionInvalidated(suggestion_id)); |
| 222 } | 220 } |
| 223 | 221 |
| 224 // history::HistoryServiceObserver implementation. | 222 // history::HistoryServiceObserver implementation. |
| 225 void ContentSuggestionsService::OnURLsDeleted( | 223 void ContentSuggestionsService::OnURLsDeleted( |
| 226 history::HistoryService* history_service, | 224 history::HistoryService* history_service, |
| 227 bool all_history, | 225 bool all_history, |
| 228 bool expired, | 226 bool expired, |
| 229 const history::URLRows& deleted_rows, | 227 const history::URLRows& deleted_rows, |
| 230 const std::set<GURL>& favicon_urls) { | 228 const std::set<GURL>& favicon_urls) { |
| 231 // We don't care about expired entries. | 229 // We don't care about expired entries. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 categories_.push_back(category); | 278 categories_.push_back(category); |
| 281 SortCategories(); | 279 SortCategories(); |
| 282 if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { | 280 if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { |
| 283 suggestions_by_category_.insert( | 281 suggestions_by_category_.insert( |
| 284 std::make_pair(category, std::vector<ContentSuggestion>())); | 282 std::make_pair(category, std::vector<ContentSuggestion>())); |
| 285 } | 283 } |
| 286 return true; | 284 return true; |
| 287 } | 285 } |
| 288 | 286 |
| 289 bool ContentSuggestionsService::RemoveSuggestionByID( | 287 bool ContentSuggestionsService::RemoveSuggestionByID( |
| 290 Category category, | 288 const ContentSuggestion::ID& suggestion_id) { |
| 291 const std::string& suggestion_id) { | |
| 292 std::vector<ContentSuggestion>* suggestions = | 289 std::vector<ContentSuggestion>* suggestions = |
| 293 &suggestions_by_category_[category]; | 290 &suggestions_by_category_[suggestion_id.category()]; |
| 294 auto position = | 291 auto position = |
| 295 std::find_if(suggestions->begin(), suggestions->end(), | 292 std::find_if(suggestions->begin(), suggestions->end(), |
| 296 [&suggestion_id](const ContentSuggestion& suggestion) { | 293 [&suggestion_id](const ContentSuggestion& suggestion) { |
| 297 return suggestion_id == suggestion.id(); | 294 return suggestion_id == suggestion.id(); |
| 298 }); | 295 }); |
| 299 if (position == suggestions->end()) | 296 if (position == suggestions->end()) |
| 300 return false; | 297 return false; |
| 301 suggestions->erase(position); | 298 suggestions->erase(position); |
| 302 | 299 |
| 303 // The positioning of the bookmarks category depends on whether it's empty. | 300 // The positioning of the bookmarks category depends on whether it's empty. |
| 304 // TODO(treib): Remove this temporary hack, crbug.com/640568. | 301 // TODO(treib): Remove this temporary hack, crbug.com/640568. |
| 305 if (category.IsKnownCategory(KnownCategories::BOOKMARKS)) | 302 if (suggestion_id.category().IsKnownCategory(KnownCategories::BOOKMARKS)) |
| 306 SortCategories(); | 303 SortCategories(); |
| 307 | 304 |
| 308 return true; | 305 return true; |
| 309 } | 306 } |
| 310 | 307 |
| 311 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { | 308 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { |
| 312 FOR_EACH_OBSERVER( | 309 FOR_EACH_OBSERVER( |
| 313 Observer, observers_, | 310 Observer, observers_, |
| 314 OnCategoryStatusChanged(category, GetCategoryStatus(category))); | 311 OnCategoryStatusChanged(category, GetCategoryStatus(category))); |
| 315 } | 312 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 328 if (left.IsKnownCategory(KnownCategories::BOOKMARKS)) | 325 if (left.IsKnownCategory(KnownCategories::BOOKMARKS)) |
| 329 return false; | 326 return false; |
| 330 if (right.IsKnownCategory(KnownCategories::BOOKMARKS)) | 327 if (right.IsKnownCategory(KnownCategories::BOOKMARKS)) |
| 331 return true; | 328 return true; |
| 332 } | 329 } |
| 333 return category_factory_.CompareCategories(left, right); | 330 return category_factory_.CompareCategories(left, right); |
| 334 }); | 331 }); |
| 335 } | 332 } |
| 336 | 333 |
| 337 } // namespace ntp_snippets | 334 } // namespace ntp_snippets |
| OLD | NEW |