| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/remote/remote_suggestions_provider.h" | 5 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/feature_list.h" | 12 #include "base/feature_list.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/metrics/sparse_histogram.h" | 16 #include "base/metrics/sparse_histogram.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 21 #include "base/task_runner_util.h" | 21 #include "base/task_runner_util.h" |
| 22 #include "base/time/default_clock.h" | 22 #include "base/time/default_clock.h" |
| 23 #include "base/time/default_tick_clock.h" | 23 #include "base/time/default_tick_clock.h" |
| 24 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 25 #include "base/values.h" | 25 #include "base/values.h" |
| 26 #include "components/data_use_measurement/core/data_use_user_data.h" | 26 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 27 #include "components/history/core/browser/history_service.h" | 27 #include "components/history/core/browser/history_service.h" |
| 28 #include "components/image_fetcher/image_decoder.h" | 28 #include "components/image_fetcher/image_decoder.h" |
| 29 #include "components/image_fetcher/image_fetcher.h" | 29 #include "components/image_fetcher/image_fetcher.h" |
| 30 #include "components/ntp_snippets/category_rankers/category_ranker.h" |
| 30 #include "components/ntp_snippets/features.h" | 31 #include "components/ntp_snippets/features.h" |
| 31 #include "components/ntp_snippets/pref_names.h" | 32 #include "components/ntp_snippets/pref_names.h" |
| 32 #include "components/ntp_snippets/remote/remote_suggestions_database.h" | 33 #include "components/ntp_snippets/remote/remote_suggestions_database.h" |
| 33 #include "components/ntp_snippets/switches.h" | 34 #include "components/ntp_snippets/switches.h" |
| 34 #include "components/ntp_snippets/user_classifier.h" | 35 #include "components/ntp_snippets/user_classifier.h" |
| 35 #include "components/prefs/pref_registry_simple.h" | 36 #include "components/prefs/pref_registry_simple.h" |
| 36 #include "components/prefs/pref_service.h" | 37 #include "components/prefs/pref_service.h" |
| 37 #include "components/variations/variations_associated_data.h" | 38 #include "components/variations/variations_associated_data.h" |
| 38 #include "grit/components_strings.h" | 39 #include "grit/components_strings.h" |
| 39 #include "ui/base/l10n/l10n_util.h" | 40 #include "ui/base/l10n/l10n_util.h" |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 if (callback.is_null()) { | 218 if (callback.is_null()) { |
| 218 return; | 219 return; |
| 219 } | 220 } |
| 220 callback.Run(status, std::vector<ContentSuggestion>()); | 221 callback.Run(status, std::vector<ContentSuggestion>()); |
| 221 } | 222 } |
| 222 | 223 |
| 223 } // namespace | 224 } // namespace |
| 224 | 225 |
| 225 RemoteSuggestionsProvider::RemoteSuggestionsProvider( | 226 RemoteSuggestionsProvider::RemoteSuggestionsProvider( |
| 226 Observer* observer, | 227 Observer* observer, |
| 227 CategoryFactory* category_factory, | |
| 228 PrefService* pref_service, | 228 PrefService* pref_service, |
| 229 const std::string& application_language_code, | 229 const std::string& application_language_code, |
| 230 CategoryRanker* category_ranker, |
| 230 const UserClassifier* user_classifier, | 231 const UserClassifier* user_classifier, |
| 231 NTPSnippetsScheduler* scheduler, | 232 NTPSnippetsScheduler* scheduler, |
| 232 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 233 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
| 233 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, | 234 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, |
| 234 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder, | 235 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder, |
| 235 std::unique_ptr<RemoteSuggestionsDatabase> database, | 236 std::unique_ptr<RemoteSuggestionsDatabase> database, |
| 236 std::unique_ptr<RemoteSuggestionsStatusService> status_service) | 237 std::unique_ptr<RemoteSuggestionsStatusService> status_service) |
| 237 : ContentSuggestionsProvider(observer, category_factory), | 238 : ContentSuggestionsProvider(observer), |
| 238 state_(State::NOT_INITED), | 239 state_(State::NOT_INITED), |
| 239 pref_service_(pref_service), | 240 pref_service_(pref_service), |
| 240 articles_category_( | 241 articles_category_( |
| 241 category_factory->FromKnownCategory(KnownCategories::ARTICLES)), | 242 Category::FromKnownCategory(KnownCategories::ARTICLES)), |
| 242 application_language_code_(application_language_code), | 243 application_language_code_(application_language_code), |
| 244 category_ranker_(category_ranker), |
| 243 user_classifier_(user_classifier), | 245 user_classifier_(user_classifier), |
| 244 scheduler_(scheduler), | 246 scheduler_(scheduler), |
| 245 snippets_fetcher_(std::move(snippets_fetcher)), | 247 snippets_fetcher_(std::move(snippets_fetcher)), |
| 246 image_fetcher_(std::move(image_fetcher)), | 248 image_fetcher_(std::move(image_fetcher)), |
| 247 image_decoder_(std::move(image_decoder)), | 249 image_decoder_(std::move(image_decoder)), |
| 248 database_(std::move(database)), | 250 database_(std::move(database)), |
| 249 status_service_(std::move(status_service)), | 251 status_service_(std::move(status_service)), |
| 250 fetch_when_ready_(false), | 252 fetch_when_ready_(false), |
| 251 nuke_when_initialized_(false), | 253 nuke_when_initialized_(false), |
| 252 thumbnail_requests_throttler_( | 254 thumbnail_requests_throttler_( |
| (...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 DCHECK(base::ContainsKey(category_contents_, articles_category_)); | 593 DCHECK(base::ContainsKey(category_contents_, articles_category_)); |
| 592 | 594 |
| 593 base::TimeDelta database_load_time = | 595 base::TimeDelta database_load_time = |
| 594 base::TimeTicks::Now() - database_load_start_; | 596 base::TimeTicks::Now() - database_load_start_; |
| 595 UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.Snippets.DatabaseLoadTime", | 597 UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.Snippets.DatabaseLoadTime", |
| 596 database_load_time); | 598 database_load_time); |
| 597 | 599 |
| 598 NTPSnippet::PtrVector to_delete; | 600 NTPSnippet::PtrVector to_delete; |
| 599 for (std::unique_ptr<NTPSnippet>& snippet : snippets) { | 601 for (std::unique_ptr<NTPSnippet>& snippet : snippets) { |
| 600 Category snippet_category = | 602 Category snippet_category = |
| 601 category_factory()->FromRemoteCategory(snippet->remote_category_id()); | 603 Category::FromRemoteCategory(snippet->remote_category_id()); |
| 604 // The category is added to the ranker to enforce the order among remote |
| 605 // categories. |
| 606 category_ranker_->AppendCategoryIfNecessary(snippet_category); |
| 602 auto content_it = category_contents_.find(snippet_category); | 607 auto content_it = category_contents_.find(snippet_category); |
| 603 // We should already know about the category. | 608 // We should already know about the category. |
| 604 if (content_it == category_contents_.end()) { | 609 if (content_it == category_contents_.end()) { |
| 605 DLOG(WARNING) << "Loaded a suggestion for unknown category " | 610 DLOG(WARNING) << "Loaded a suggestion for unknown category " |
| 606 << snippet_category << " from the DB; deleting"; | 611 << snippet_category << " from the DB; deleting"; |
| 607 to_delete.emplace_back(std::move(snippet)); | 612 to_delete.emplace_back(std::move(snippet)); |
| 608 continue; | 613 continue; |
| 609 } | 614 } |
| 610 CategoryContent* content = &content_it->second; | 615 CategoryContent* content = &content_it->second; |
| 611 if (snippet->is_dismissed()) { | 616 if (snippet->is_dismissed()) { |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 741 for (NTPSnippetsFetcher::FetchedCategory& fetched_category : | 746 for (NTPSnippetsFetcher::FetchedCategory& fetched_category : |
| 742 *fetched_categories) { | 747 *fetched_categories) { |
| 743 // TODO(tschumann): Remove this histogram once we only talk to the content | 748 // TODO(tschumann): Remove this histogram once we only talk to the content |
| 744 // suggestions cloud backend. | 749 // suggestions cloud backend. |
| 745 if (fetched_category.category == articles_category_) { | 750 if (fetched_category.category == articles_category_) { |
| 746 UMA_HISTOGRAM_SPARSE_SLOWLY( | 751 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 747 "NewTabPage.Snippets.NumArticlesFetched", | 752 "NewTabPage.Snippets.NumArticlesFetched", |
| 748 std::min(fetched_category.snippets.size(), | 753 std::min(fetched_category.snippets.size(), |
| 749 static_cast<size_t>(kMaxSnippetCount + 1))); | 754 static_cast<size_t>(kMaxSnippetCount + 1))); |
| 750 } | 755 } |
| 756 category_ranker_->AppendCategoryIfNecessary(fetched_category.category); |
| 751 CategoryContent* content = | 757 CategoryContent* content = |
| 752 UpdateCategoryInfo(fetched_category.category, fetched_category.info); | 758 UpdateCategoryInfo(fetched_category.category, fetched_category.info); |
| 753 content->included_in_last_server_response = true; | 759 content->included_in_last_server_response = true; |
| 754 SanitizeReceivedSnippets(content->dismissed, &fetched_category.snippets); | 760 SanitizeReceivedSnippets(content->dismissed, &fetched_category.snippets); |
| 755 IntegrateSnippets(content, std::move(fetched_category.snippets)); | 761 IntegrateSnippets(content, std::move(fetched_category.snippets)); |
| 756 } | 762 } |
| 757 } | 763 } |
| 758 | 764 |
| 759 // TODO(tschumann): The snippets fetcher needs to signal errors so that we | 765 // TODO(tschumann): The snippets fetcher needs to signal errors so that we |
| 760 // know why we received no data. If an error occured, none of the following | 766 // know why we received no data. If an error occured, none of the following |
| (...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1285 &included_in_last_server_response)) { | 1291 &included_in_last_server_response)) { |
| 1286 DLOG(WARNING) << "Invalid category pref value, missing '" | 1292 DLOG(WARNING) << "Invalid category pref value, missing '" |
| 1287 << kCategoryContentProvidedByServer << "': " << *entry; | 1293 << kCategoryContentProvidedByServer << "': " << *entry; |
| 1288 continue; | 1294 continue; |
| 1289 } | 1295 } |
| 1290 bool allow_fetching_more_results = false; | 1296 bool allow_fetching_more_results = false; |
| 1291 // This wasn't always around, so it's okay if it's missing. | 1297 // This wasn't always around, so it's okay if it's missing. |
| 1292 dict->GetBoolean(kCategoryContentAllowFetchingMore, | 1298 dict->GetBoolean(kCategoryContentAllowFetchingMore, |
| 1293 &allow_fetching_more_results); | 1299 &allow_fetching_more_results); |
| 1294 | 1300 |
| 1295 Category category = category_factory()->FromIDValue(id); | 1301 Category category = Category::FromIDValue(id); |
| 1302 // The ranker may not persist the order of remote categories. |
| 1303 category_ranker_->AppendCategoryIfNecessary(category); |
| 1296 // TODO(tschumann): The following has a bad smell that category | 1304 // TODO(tschumann): The following has a bad smell that category |
| 1297 // serialization / deserialization should not be done inside this | 1305 // serialization / deserialization should not be done inside this |
| 1298 // class. We should move that into a central place that also knows how to | 1306 // class. We should move that into a central place that also knows how to |
| 1299 // parse data we received from remote backends. | 1307 // parse data we received from remote backends. |
| 1300 CategoryInfo info = | 1308 CategoryInfo info = |
| 1301 category == articles_category_ | 1309 category == articles_category_ |
| 1302 ? BuildArticleCategoryInfo(title) | 1310 ? BuildArticleCategoryInfo(title) |
| 1303 : BuildRemoteCategoryInfo(title, allow_fetching_more_results); | 1311 : BuildRemoteCategoryInfo(title, allow_fetching_more_results); |
| 1304 CategoryContent* content = UpdateCategoryInfo(category, info); | 1312 CategoryContent* content = UpdateCategoryInfo(category, info); |
| 1305 content->included_in_last_server_response = | 1313 content->included_in_last_server_response = |
| 1306 included_in_last_server_response; | 1314 included_in_last_server_response; |
| 1307 } | 1315 } |
| 1308 } | 1316 } |
| 1309 | 1317 |
| 1310 void RemoteSuggestionsProvider::StoreCategoriesToPrefs() { | 1318 void RemoteSuggestionsProvider::StoreCategoriesToPrefs() { |
| 1311 // Collect all the CategoryContents. | 1319 // Collect all the CategoryContents. |
| 1312 std::vector<std::pair<Category, const CategoryContent*>> to_store; | 1320 std::vector<std::pair<Category, const CategoryContent*>> to_store; |
| 1313 for (const auto& entry : category_contents_) { | 1321 for (const auto& entry : category_contents_) { |
| 1314 to_store.emplace_back(entry.first, &entry.second); | 1322 to_store.emplace_back(entry.first, &entry.second); |
| 1315 } | 1323 } |
| 1316 // Sort them into the proper category order. | 1324 // The ranker may not persist the order, thus, it is stored by the provider. |
| 1317 std::sort(to_store.begin(), to_store.end(), | 1325 std::sort(to_store.begin(), to_store.end(), |
| 1318 [this](const std::pair<Category, const CategoryContent*>& left, | 1326 [this](const std::pair<Category, const CategoryContent*>& left, |
| 1319 const std::pair<Category, const CategoryContent*>& right) { | 1327 const std::pair<Category, const CategoryContent*>& right) { |
| 1320 return category_factory()->CompareCategories(left.first, | 1328 return category_ranker_->Compare(left.first, right.first); |
| 1321 right.first); | |
| 1322 }); | 1329 }); |
| 1323 // Convert the relevant info into a base::ListValue for storage. | 1330 // Convert the relevant info into a base::ListValue for storage. |
| 1324 base::ListValue list; | 1331 base::ListValue list; |
| 1325 for (const auto& entry : to_store) { | 1332 for (const auto& entry : to_store) { |
| 1326 const Category& category = entry.first; | 1333 const Category& category = entry.first; |
| 1327 const CategoryContent& content = *entry.second; | 1334 const CategoryContent& content = *entry.second; |
| 1328 auto dict = base::MakeUnique<base::DictionaryValue>(); | 1335 auto dict = base::MakeUnique<base::DictionaryValue>(); |
| 1329 dict->SetInteger(kCategoryContentId, category.id()); | 1336 dict->SetInteger(kCategoryContentId, category.id()); |
| 1330 // TODO(tschumann): Persist other properties of the CategoryInfo. | 1337 // TODO(tschumann): Persist other properties of the CategoryInfo. |
| 1331 dict->SetString(kCategoryContentTitle, content.info.title()); | 1338 dict->SetString(kCategoryContentTitle, content.info.title()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1346 RemoteSuggestionsProvider::CategoryContent::CategoryContent(CategoryContent&&) = | 1353 RemoteSuggestionsProvider::CategoryContent::CategoryContent(CategoryContent&&) = |
| 1347 default; | 1354 default; |
| 1348 | 1355 |
| 1349 RemoteSuggestionsProvider::CategoryContent::~CategoryContent() = default; | 1356 RemoteSuggestionsProvider::CategoryContent::~CategoryContent() = default; |
| 1350 | 1357 |
| 1351 RemoteSuggestionsProvider::CategoryContent& | 1358 RemoteSuggestionsProvider::CategoryContent& |
| 1352 RemoteSuggestionsProvider::CategoryContent::operator=(CategoryContent&&) = | 1359 RemoteSuggestionsProvider::CategoryContent::operator=(CategoryContent&&) = |
| 1353 default; | 1360 default; |
| 1354 | 1361 |
| 1355 } // namespace ntp_snippets | 1362 } // namespace ntp_snippets |
| OLD | NEW |