| 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> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "base/values.h" |
| 17 #include "components/ntp_snippets/pref_names.h" |
| 18 #include "components/prefs/pref_registry_simple.h" |
| 16 #include "ui/gfx/image/image.h" | 19 #include "ui/gfx/image/image.h" |
| 17 | 20 |
| 18 namespace ntp_snippets { | 21 namespace ntp_snippets { |
| 19 | 22 |
| 20 ContentSuggestionsService::ContentSuggestionsService( | 23 ContentSuggestionsService::ContentSuggestionsService( |
| 21 State state, | 24 State state, |
| 22 history::HistoryService* history_service, | 25 history::HistoryService* history_service, |
| 23 PrefService* pref_service) | 26 PrefService* pref_service) |
| 24 : state_(state), | 27 : state_(state), |
| 25 history_service_observer_(this), | 28 history_service_observer_(this), |
| 26 ntp_snippets_service_(nullptr), | 29 ntp_snippets_service_(nullptr), |
| 30 pref_service_(pref_service), |
| 27 user_classifier_(pref_service) { | 31 user_classifier_(pref_service) { |
| 28 // Can be null in tests. | 32 // Can be null in tests. |
| 29 if (history_service) | 33 if (history_service) |
| 30 history_service_observer_.Add(history_service); | 34 history_service_observer_.Add(history_service); |
| 35 |
| 36 RestoreDismissedCategoriesFromPrefs(); |
| 31 } | 37 } |
| 32 | 38 |
| 33 ContentSuggestionsService::~ContentSuggestionsService() = default; | 39 ContentSuggestionsService::~ContentSuggestionsService() = default; |
| 34 | 40 |
| 35 void ContentSuggestionsService::Shutdown() { | 41 void ContentSuggestionsService::Shutdown() { |
| 36 ntp_snippets_service_ = nullptr; | 42 ntp_snippets_service_ = nullptr; |
| 37 suggestions_by_category_.clear(); | 43 suggestions_by_category_.clear(); |
| 38 providers_by_category_.clear(); | 44 providers_by_category_.clear(); |
| 39 categories_.clear(); | 45 categories_.clear(); |
| 40 providers_.clear(); | 46 providers_.clear(); |
| 41 state_ = State::DISABLED; | 47 state_ = State::DISABLED; |
| 42 FOR_EACH_OBSERVER(Observer, observers_, ContentSuggestionsServiceShutdown()); | 48 FOR_EACH_OBSERVER(Observer, observers_, ContentSuggestionsServiceShutdown()); |
| 43 } | 49 } |
| 44 | 50 |
| 51 // static |
| 52 void ContentSuggestionsService::RegisterProfilePrefs( |
| 53 PrefRegistrySimple* registry) { |
| 54 registry->RegisterListPref(prefs::kDismissedCategories); |
| 55 } |
| 56 |
| 45 CategoryStatus ContentSuggestionsService::GetCategoryStatus( | 57 CategoryStatus ContentSuggestionsService::GetCategoryStatus( |
| 46 Category category) const { | 58 Category category) const { |
| 47 if (state_ == State::DISABLED) { | 59 if (state_ == State::DISABLED) |
| 48 return CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED; | 60 return CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED; |
| 49 } | 61 |
| 62 if (IsCategoryDismissed(category)) |
| 63 return CategoryStatus::NOT_PROVIDED; |
| 50 | 64 |
| 51 auto iterator = providers_by_category_.find(category); | 65 auto iterator = providers_by_category_.find(category); |
| 52 if (iterator == providers_by_category_.end()) | 66 if (iterator == providers_by_category_.end()) |
| 53 return CategoryStatus::NOT_PROVIDED; | 67 return CategoryStatus::NOT_PROVIDED; |
| 54 | 68 |
| 55 return iterator->second->GetCategoryStatus(category); | 69 return iterator->second->GetCategoryStatus(category); |
| 56 } | 70 } |
| 57 | 71 |
| 58 base::Optional<CategoryInfo> ContentSuggestionsService::GetCategoryInfo( | 72 base::Optional<CategoryInfo> ContentSuggestionsService::GetCategoryInfo( |
| 59 Category category) const { | 73 Category category) const { |
| 74 if (IsCategoryDismissed(category)) |
| 75 return base::Optional<CategoryInfo>(); |
| 76 |
| 60 auto iterator = providers_by_category_.find(category); | 77 auto iterator = providers_by_category_.find(category); |
| 61 if (iterator == providers_by_category_.end()) | 78 if (iterator == providers_by_category_.end()) |
| 62 return base::Optional<CategoryInfo>(); | 79 return base::Optional<CategoryInfo>(); |
| 63 return iterator->second->GetCategoryInfo(category); | 80 return iterator->second->GetCategoryInfo(category); |
| 64 } | 81 } |
| 65 | 82 |
| 66 const std::vector<ContentSuggestion>& | 83 const std::vector<ContentSuggestion>& |
| 67 ContentSuggestionsService::GetSuggestionsForCategory(Category category) const { | 84 ContentSuggestionsService::GetSuggestionsForCategory(Category category) const { |
| 85 if (IsCategoryDismissed(category)) |
| 86 return no_suggestions_; |
| 87 |
| 68 auto iterator = suggestions_by_category_.find(category); | 88 auto iterator = suggestions_by_category_.find(category); |
| 69 if (iterator == suggestions_by_category_.end()) | 89 if (iterator == suggestions_by_category_.end()) |
| 70 return no_suggestions_; | 90 return no_suggestions_; |
| 71 return iterator->second; | 91 return iterator->second; |
| 72 } | 92 } |
| 73 | 93 |
| 74 void ContentSuggestionsService::FetchSuggestionImage( | 94 void ContentSuggestionsService::FetchSuggestionImage( |
| 75 const ContentSuggestion::ID& suggestion_id, | 95 const ContentSuggestion::ID& suggestion_id, |
| 76 const ImageFetchedCallback& callback) { | 96 const ImageFetchedCallback& callback) { |
| 77 if (!providers_by_category_.count(suggestion_id.category())) { | 97 if (!providers_by_category_.count(suggestion_id.category())) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 DCHECK(removed) << "The dismissed suggestion " << suggestion_id | 163 DCHECK(removed) << "The dismissed suggestion " << suggestion_id |
| 144 << " has already been removed. Providers must not call" | 164 << " has already been removed. Providers must not call" |
| 145 << " OnNewSuggestions in response to DismissSuggestion."; | 165 << " OnNewSuggestions in response to DismissSuggestion."; |
| 146 } | 166 } |
| 147 | 167 |
| 148 void ContentSuggestionsService::DismissCategory(Category category) { | 168 void ContentSuggestionsService::DismissCategory(Category category) { |
| 149 auto providers_it = providers_by_category_.find(category); | 169 auto providers_it = providers_by_category_.find(category); |
| 150 if (providers_it == providers_by_category_.end()) | 170 if (providers_it == providers_by_category_.end()) |
| 151 return; | 171 return; |
| 152 | 172 |
| 153 providers_by_category_.erase(providers_it); | 173 dismissed_categories_.insert(category); |
| 154 categories_.erase( | 174 StoreDismissedCategoriesToPrefs(); |
| 155 std::find(categories_.begin(), categories_.end(), category)); | |
| 156 } | 175 } |
| 157 | 176 |
| 158 void ContentSuggestionsService::AddObserver(Observer* observer) { | 177 void ContentSuggestionsService::AddObserver(Observer* observer) { |
| 159 observers_.AddObserver(observer); | 178 observers_.AddObserver(observer); |
| 160 } | 179 } |
| 161 | 180 |
| 162 void ContentSuggestionsService::RemoveObserver(Observer* observer) { | 181 void ContentSuggestionsService::RemoveObserver(Observer* observer) { |
| 163 observers_.RemoveObserver(observer); | 182 observers_.RemoveObserver(observer); |
| 164 } | 183 } |
| 165 | 184 |
| 166 void ContentSuggestionsService::RegisterProvider( | 185 void ContentSuggestionsService::RegisterProvider( |
| 167 std::unique_ptr<ContentSuggestionsProvider> provider) { | 186 std::unique_ptr<ContentSuggestionsProvider> provider) { |
| 168 DCHECK(state_ == State::ENABLED); | 187 DCHECK(state_ == State::ENABLED); |
| 169 providers_.push_back(std::move(provider)); | 188 providers_.push_back(std::move(provider)); |
| 170 } | 189 } |
| 171 | 190 |
| 172 //////////////////////////////////////////////////////////////////////////////// | 191 //////////////////////////////////////////////////////////////////////////////// |
| 173 // Private methods | 192 // Private methods |
| 174 | 193 |
| 175 void ContentSuggestionsService::OnNewSuggestions( | 194 void ContentSuggestionsService::OnNewSuggestions( |
| 176 ContentSuggestionsProvider* provider, | 195 ContentSuggestionsProvider* provider, |
| 177 Category category, | 196 Category category, |
| 178 std::vector<ContentSuggestion> suggestions) { | 197 std::vector<ContentSuggestion> suggestions) { |
| 179 if (RegisterCategoryIfRequired(provider, category)) | 198 if (!suggestions.empty()) { |
| 180 NotifyCategoryStatusChanged(category); | 199 dismissed_categories_.erase(category); |
| 200 StoreDismissedCategoriesToPrefs(); |
| 201 } |
| 181 | 202 |
| 182 if (!IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) | 203 OnNewSuggestionsInternal(provider, category, std::move(suggestions)); |
| 183 return; | 204 } |
| 184 | 205 |
| 185 suggestions_by_category_[category] = std::move(suggestions); | 206 void ContentSuggestionsService::OnNewSuggestionBatch( |
| 207 ContentSuggestionsProvider* provider, |
| 208 SuggestionBatch suggestion_batch) { |
| 209 // Update the dismissed suggestions. Remote suggestions are received at once |
| 210 // from the server. Because of that, we always have the full list of valid |
| 211 // remote categories according to the server. We remove invalid categories or |
| 212 // the ones for which we got new suggestions to show. |
| 213 for (auto it = dismissed_categories_.begin(); |
| 214 it != dismissed_categories_.end();) { |
| 215 // Do nothing to the local sections, we only clean up the remote ones here. |
| 216 if (!it->IsRemote()) { |
| 217 ++it; |
| 218 continue; |
| 219 } |
| 186 | 220 |
| 187 // The positioning of the bookmarks category depends on whether it's empty. | 221 auto entry = suggestion_batch.find(*it); |
| 188 // TODO(treib): Remove this temporary hack, crbug.com/640568. | |
| 189 if (category.IsKnownCategory(KnownCategories::BOOKMARKS)) | |
| 190 SortCategories(); | |
| 191 | 222 |
| 192 FOR_EACH_OBSERVER(Observer, observers_, OnNewSuggestions(category)); | 223 // If the category is not included in the new bundle, it's not valid anymore |
| 224 // and we can just remove it. |
| 225 // TODO(dgn) Also remove all the related local data? |
| 226 if (entry == suggestion_batch.end()) { |
| 227 DVLOG(1) << "Dismissed category " << it->id() |
| 228 << " unknown on the server."; |
| 229 it = dismissed_categories_.erase(it); |
| 230 continue; |
| 231 } |
| 232 |
| 233 // The category has new suggestions to show, it's not dismissed anymore. |
| 234 if (!entry->second.empty()) { |
| 235 DVLOG(1) << "Dismissed category " << it->id() |
| 236 << " has new suggestions to show."; |
| 237 it = dismissed_categories_.erase(it); |
| 238 continue; |
| 239 } |
| 240 |
| 241 ++it; |
| 242 } |
| 243 |
| 244 StoreDismissedCategoriesToPrefs(); |
| 245 |
| 246 for (auto it = suggestion_batch.begin(); it != suggestion_batch.end(); ++it) { |
| 247 OnNewSuggestionsInternal(provider, it->first, std::move(it->second)); |
| 248 } |
| 193 } | 249 } |
| 194 | 250 |
| 195 void ContentSuggestionsService::OnCategoryStatusChanged( | 251 void ContentSuggestionsService::OnCategoryStatusChanged( |
| 196 ContentSuggestionsProvider* provider, | 252 ContentSuggestionsProvider* provider, |
| 197 Category category, | 253 Category category, |
| 198 CategoryStatus new_status) { | 254 CategoryStatus new_status) { |
| 199 if (!IsCategoryStatusAvailable(new_status)) { | 255 if (!IsCategoryStatusAvailable(new_status)) { |
| 200 suggestions_by_category_.erase(category); | 256 suggestions_by_category_.erase(category); |
| 201 } | 257 } |
| 202 if (new_status == CategoryStatus::NOT_PROVIDED) { | 258 if (new_status == CategoryStatus::NOT_PROVIDED) { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 providers_by_category_[category] = provider; | 333 providers_by_category_[category] = provider; |
| 278 categories_.push_back(category); | 334 categories_.push_back(category); |
| 279 SortCategories(); | 335 SortCategories(); |
| 280 if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { | 336 if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { |
| 281 suggestions_by_category_.insert( | 337 suggestions_by_category_.insert( |
| 282 std::make_pair(category, std::vector<ContentSuggestion>())); | 338 std::make_pair(category, std::vector<ContentSuggestion>())); |
| 283 } | 339 } |
| 284 return true; | 340 return true; |
| 285 } | 341 } |
| 286 | 342 |
| 343 void ContentSuggestionsService::OnNewSuggestionsInternal( |
| 344 ContentSuggestionsProvider* provider, |
| 345 Category category, |
| 346 std::vector<ContentSuggestion> suggestions) { |
| 347 // Ignore notifications concerning dismissed categories. |
| 348 if (IsCategoryDismissed(category)) |
| 349 return; |
| 350 |
| 351 if (RegisterCategoryIfRequired(provider, category)) |
| 352 NotifyCategoryStatusChanged(category); |
| 353 |
| 354 if (!IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) |
| 355 return; |
| 356 |
| 357 suggestions_by_category_[category] = std::move(suggestions); |
| 358 |
| 359 // The positioning of the bookmarks category depends on whether it's empty. |
| 360 // TODO(treib): Remove this temporary hack, crbug.com/640568. |
| 361 if (category.IsKnownCategory(KnownCategories::BOOKMARKS)) |
| 362 SortCategories(); |
| 363 |
| 364 FOR_EACH_OBSERVER(Observer, observers_, OnNewSuggestions(category)); |
| 365 } |
| 366 |
| 287 bool ContentSuggestionsService::RemoveSuggestionByID( | 367 bool ContentSuggestionsService::RemoveSuggestionByID( |
| 288 const ContentSuggestion::ID& suggestion_id) { | 368 const ContentSuggestion::ID& suggestion_id) { |
| 289 std::vector<ContentSuggestion>* suggestions = | 369 std::vector<ContentSuggestion>* suggestions = |
| 290 &suggestions_by_category_[suggestion_id.category()]; | 370 &suggestions_by_category_[suggestion_id.category()]; |
| 291 auto position = | 371 auto position = |
| 292 std::find_if(suggestions->begin(), suggestions->end(), | 372 std::find_if(suggestions->begin(), suggestions->end(), |
| 293 [&suggestion_id](const ContentSuggestion& suggestion) { | 373 [&suggestion_id](const ContentSuggestion& suggestion) { |
| 294 return suggestion_id == suggestion.id(); | 374 return suggestion_id == suggestion.id(); |
| 295 }); | 375 }); |
| 296 if (position == suggestions->end()) | 376 if (position == suggestions->end()) |
| 297 return false; | 377 return false; |
| 298 suggestions->erase(position); | 378 suggestions->erase(position); |
| 299 | 379 |
| 300 // The positioning of the bookmarks category depends on whether it's empty. | 380 // The positioning of the bookmarks category depends on whether it's empty. |
| 301 // TODO(treib): Remove this temporary hack, crbug.com/640568. | 381 // TODO(treib): Remove this temporary hack, crbug.com/640568. |
| 302 if (suggestion_id.category().IsKnownCategory(KnownCategories::BOOKMARKS)) | 382 if (suggestion_id.category().IsKnownCategory(KnownCategories::BOOKMARKS)) |
| 303 SortCategories(); | 383 SortCategories(); |
| 304 | 384 |
| 305 return true; | 385 return true; |
| 306 } | 386 } |
| 307 | 387 |
| 308 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { | 388 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { |
| 389 // Ignore notifications concerning dismissed categories. |
| 390 if (IsCategoryDismissed(category)) |
| 391 return; |
| 392 |
| 309 FOR_EACH_OBSERVER( | 393 FOR_EACH_OBSERVER( |
| 310 Observer, observers_, | 394 Observer, observers_, |
| 311 OnCategoryStatusChanged(category, GetCategoryStatus(category))); | 395 OnCategoryStatusChanged(category, GetCategoryStatus(category))); |
| 312 } | 396 } |
| 313 | 397 |
| 398 bool ContentSuggestionsService::IsCategoryDismissed(Category category) const { |
| 399 return dismissed_categories_.find(category) != dismissed_categories_.end(); |
| 400 } |
| 401 |
| 314 void ContentSuggestionsService::SortCategories() { | 402 void ContentSuggestionsService::SortCategories() { |
| 315 auto it = suggestions_by_category_.find( | 403 auto it = suggestions_by_category_.find( |
| 316 category_factory_.FromKnownCategory(KnownCategories::BOOKMARKS)); | 404 category_factory_.FromKnownCategory(KnownCategories::BOOKMARKS)); |
| 317 bool bookmarks_empty = | 405 bool bookmarks_empty = |
| 318 (it == suggestions_by_category_.end() || it->second.empty()); | 406 (it == suggestions_by_category_.end() || it->second.empty()); |
| 319 std::sort( | 407 std::sort( |
| 320 categories_.begin(), categories_.end(), | 408 categories_.begin(), categories_.end(), |
| 321 [this, bookmarks_empty](const Category& left, const Category& right) { | 409 [this, bookmarks_empty](const Category& left, const Category& right) { |
| 322 // If the bookmarks section is empty, put it at the end. | 410 // If the bookmarks section is empty, put it at the end. |
| 323 // TODO(treib): This is a temporary hack, see crbug.com/640568. | 411 // TODO(treib): This is a temporary hack, see crbug.com/640568. |
| 324 if (bookmarks_empty) { | 412 if (bookmarks_empty) { |
| 325 if (left.IsKnownCategory(KnownCategories::BOOKMARKS)) | 413 if (left.IsKnownCategory(KnownCategories::BOOKMARKS)) |
| 326 return false; | 414 return false; |
| 327 if (right.IsKnownCategory(KnownCategories::BOOKMARKS)) | 415 if (right.IsKnownCategory(KnownCategories::BOOKMARKS)) |
| 328 return true; | 416 return true; |
| 329 } | 417 } |
| 330 return category_factory_.CompareCategories(left, right); | 418 return category_factory_.CompareCategories(left, right); |
| 331 }); | 419 }); |
| 332 } | 420 } |
| 333 | 421 |
| 422 void ContentSuggestionsService::RestoreDismissedCategoriesFromPrefs() { |
| 423 // This must only be called at startup. |
| 424 DCHECK(dismissed_categories_.empty()); |
| 425 |
| 426 const base::ListValue* list = |
| 427 pref_service_->GetList(prefs::kDismissedCategories); |
| 428 for (const std::unique_ptr<base::Value>& entry : *list) { |
| 429 int id = 0; |
| 430 if (!entry->GetAsInteger(&id)) { |
| 431 DLOG(WARNING) << "Invalid category pref value: " << *entry; |
| 432 continue; |
| 433 } |
| 434 |
| 435 dismissed_categories_.insert(category_factory()->FromIDValue(id)); |
| 436 } |
| 437 } |
| 438 |
| 439 void ContentSuggestionsService::StoreDismissedCategoriesToPrefs() { |
| 440 base::ListValue list; |
| 441 for (Category category : dismissed_categories_) { |
| 442 list.AppendInteger(category.id()); |
| 443 } |
| 444 |
| 445 pref_service_->Set(prefs::kDismissedCategories, list); |
| 446 } |
| 447 |
| 334 } // namespace ntp_snippets | 448 } // namespace ntp_snippets |
| OLD | NEW |