| 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/time/default_clock.h" | 21 #include "base/time/default_clock.h" |
| 22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 23 #include "base/values.h" | 23 #include "base/values.h" |
| 24 #include "components/data_use_measurement/core/data_use_user_data.h" | 24 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 25 #include "components/history/core/browser/history_service.h" | 25 #include "components/history/core/browser/history_service.h" |
| 26 #include "components/image_fetcher/image_decoder.h" | 26 #include "components/image_fetcher/image_decoder.h" |
| 27 #include "components/image_fetcher/image_fetcher.h" | 27 #include "components/image_fetcher/image_fetcher.h" |
| 28 #include "components/ntp_snippets/category_rankers/category_ranker.h" |
| 28 #include "components/ntp_snippets/features.h" | 29 #include "components/ntp_snippets/features.h" |
| 29 #include "components/ntp_snippets/pref_names.h" | 30 #include "components/ntp_snippets/pref_names.h" |
| 30 #include "components/ntp_snippets/remote/remote_suggestions_database.h" | 31 #include "components/ntp_snippets/remote/remote_suggestions_database.h" |
| 31 #include "components/ntp_snippets/switches.h" | 32 #include "components/ntp_snippets/switches.h" |
| 32 #include "components/ntp_snippets/user_classifier.h" | 33 #include "components/ntp_snippets/user_classifier.h" |
| 33 #include "components/prefs/pref_registry_simple.h" | 34 #include "components/prefs/pref_registry_simple.h" |
| 34 #include "components/prefs/pref_service.h" | 35 #include "components/prefs/pref_service.h" |
| 35 #include "components/variations/variations_associated_data.h" | 36 #include "components/variations/variations_associated_data.h" |
| 36 #include "grit/components_strings.h" | 37 #include "grit/components_strings.h" |
| 37 #include "ui/base/l10n/l10n_util.h" | 38 #include "ui/base/l10n/l10n_util.h" |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 } | 316 } |
| 316 | 317 |
| 317 image_fetcher_->StartOrQueueNetworkRequest( | 318 image_fetcher_->StartOrQueueNetworkRequest( |
| 318 suggestion_id.id_within_category(), url, | 319 suggestion_id.id_within_category(), url, |
| 319 base::Bind(&CachedImageFetcher::OnImageDecodingDone, | 320 base::Bind(&CachedImageFetcher::OnImageDecodingDone, |
| 320 base::Unretained(this), callback)); | 321 base::Unretained(this), callback)); |
| 321 } | 322 } |
| 322 | 323 |
| 323 RemoteSuggestionsProvider::RemoteSuggestionsProvider( | 324 RemoteSuggestionsProvider::RemoteSuggestionsProvider( |
| 324 Observer* observer, | 325 Observer* observer, |
| 325 CategoryFactory* category_factory, | |
| 326 PrefService* pref_service, | 326 PrefService* pref_service, |
| 327 const std::string& application_language_code, | 327 const std::string& application_language_code, |
| 328 CategoryRanker* category_ranker, |
| 328 const UserClassifier* user_classifier, | 329 const UserClassifier* user_classifier, |
| 329 NTPSnippetsScheduler* scheduler, | 330 NTPSnippetsScheduler* scheduler, |
| 330 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, | 331 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, |
| 331 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, | 332 std::unique_ptr<image_fetcher::ImageFetcher> image_fetcher, |
| 332 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder, | 333 std::unique_ptr<image_fetcher::ImageDecoder> image_decoder, |
| 333 std::unique_ptr<RemoteSuggestionsDatabase> database, | 334 std::unique_ptr<RemoteSuggestionsDatabase> database, |
| 334 std::unique_ptr<RemoteSuggestionsStatusService> status_service) | 335 std::unique_ptr<RemoteSuggestionsStatusService> status_service) |
| 335 : ContentSuggestionsProvider(observer, category_factory), | 336 : ContentSuggestionsProvider(observer), |
| 336 state_(State::NOT_INITED), | 337 state_(State::NOT_INITED), |
| 337 pref_service_(pref_service), | 338 pref_service_(pref_service), |
| 338 articles_category_( | 339 articles_category_( |
| 339 category_factory->FromKnownCategory(KnownCategories::ARTICLES)), | 340 Category::FromKnownCategory(KnownCategories::ARTICLES)), |
| 340 application_language_code_(application_language_code), | 341 application_language_code_(application_language_code), |
| 342 category_ranker_(category_ranker), |
| 341 user_classifier_(user_classifier), | 343 user_classifier_(user_classifier), |
| 342 scheduler_(scheduler), | 344 scheduler_(scheduler), |
| 343 snippets_fetcher_(std::move(snippets_fetcher)), | 345 snippets_fetcher_(std::move(snippets_fetcher)), |
| 344 database_(std::move(database)), | 346 database_(std::move(database)), |
| 345 image_fetcher_(std::move(image_fetcher), | 347 image_fetcher_(std::move(image_fetcher), |
| 346 std::move(image_decoder), | 348 std::move(image_decoder), |
| 347 pref_service, | 349 pref_service, |
| 348 database_.get()), | 350 database_.get()), |
| 349 status_service_(std::move(status_service)), | 351 status_service_(std::move(status_service)), |
| 350 fetch_when_ready_(false), | 352 fetch_when_ready_(false), |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 637 DCHECK(base::ContainsKey(category_contents_, articles_category_)); | 639 DCHECK(base::ContainsKey(category_contents_, articles_category_)); |
| 638 | 640 |
| 639 base::TimeDelta database_load_time = | 641 base::TimeDelta database_load_time = |
| 640 base::TimeTicks::Now() - database_load_start_; | 642 base::TimeTicks::Now() - database_load_start_; |
| 641 UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.Snippets.DatabaseLoadTime", | 643 UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.Snippets.DatabaseLoadTime", |
| 642 database_load_time); | 644 database_load_time); |
| 643 | 645 |
| 644 NTPSnippet::PtrVector to_delete; | 646 NTPSnippet::PtrVector to_delete; |
| 645 for (std::unique_ptr<NTPSnippet>& snippet : snippets) { | 647 for (std::unique_ptr<NTPSnippet>& snippet : snippets) { |
| 646 Category snippet_category = | 648 Category snippet_category = |
| 647 category_factory()->FromRemoteCategory(snippet->remote_category_id()); | 649 Category::FromRemoteCategory(snippet->remote_category_id()); |
| 648 auto content_it = category_contents_.find(snippet_category); | 650 auto content_it = category_contents_.find(snippet_category); |
| 649 // We should already know about the category. | 651 // We should already know about the category. |
| 650 if (content_it == category_contents_.end()) { | 652 if (content_it == category_contents_.end()) { |
| 651 DLOG(WARNING) << "Loaded a suggestion for unknown category " | 653 DLOG(WARNING) << "Loaded a suggestion for unknown category " |
| 652 << snippet_category << " from the DB; deleting"; | 654 << snippet_category << " from the DB; deleting"; |
| 653 to_delete.emplace_back(std::move(snippet)); | 655 to_delete.emplace_back(std::move(snippet)); |
| 654 continue; | 656 continue; |
| 655 } | 657 } |
| 656 CategoryContent* content = &content_it->second; | 658 CategoryContent* content = &content_it->second; |
| 657 if (snippet->is_dismissed()) { | 659 if (snippet->is_dismissed()) { |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 783 for (NTPSnippetsFetcher::FetchedCategory& fetched_category : | 785 for (NTPSnippetsFetcher::FetchedCategory& fetched_category : |
| 784 *fetched_categories) { | 786 *fetched_categories) { |
| 785 // TODO(tschumann): Remove this histogram once we only talk to the content | 787 // TODO(tschumann): Remove this histogram once we only talk to the content |
| 786 // suggestions cloud backend. | 788 // suggestions cloud backend. |
| 787 if (fetched_category.category == articles_category_) { | 789 if (fetched_category.category == articles_category_) { |
| 788 UMA_HISTOGRAM_SPARSE_SLOWLY( | 790 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 789 "NewTabPage.Snippets.NumArticlesFetched", | 791 "NewTabPage.Snippets.NumArticlesFetched", |
| 790 std::min(fetched_category.snippets.size(), | 792 std::min(fetched_category.snippets.size(), |
| 791 static_cast<size_t>(kMaxSnippetCount + 1))); | 793 static_cast<size_t>(kMaxSnippetCount + 1))); |
| 792 } | 794 } |
| 795 category_ranker_->AppendCategoryIfNecessary(fetched_category.category); |
| 793 CategoryContent* content = | 796 CategoryContent* content = |
| 794 UpdateCategoryInfo(fetched_category.category, fetched_category.info); | 797 UpdateCategoryInfo(fetched_category.category, fetched_category.info); |
| 795 content->included_in_last_server_response = true; | 798 content->included_in_last_server_response = true; |
| 796 SanitizeReceivedSnippets(content->dismissed, &fetched_category.snippets); | 799 SanitizeReceivedSnippets(content->dismissed, &fetched_category.snippets); |
| 797 IntegrateSnippets(content, std::move(fetched_category.snippets)); | 800 IntegrateSnippets(content, std::move(fetched_category.snippets)); |
| 798 } | 801 } |
| 799 } | 802 } |
| 800 | 803 |
| 801 // TODO(tschumann): The snippets fetcher needs to signal errors so that we | 804 // TODO(tschumann): The snippets fetcher needs to signal errors so that we |
| 802 // know why we received no data. If an error occured, none of the following | 805 // know why we received no data. If an error occured, none of the following |
| (...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1273 &included_in_last_server_response)) { | 1276 &included_in_last_server_response)) { |
| 1274 DLOG(WARNING) << "Invalid category pref value, missing '" | 1277 DLOG(WARNING) << "Invalid category pref value, missing '" |
| 1275 << kCategoryContentProvidedByServer << "': " << *entry; | 1278 << kCategoryContentProvidedByServer << "': " << *entry; |
| 1276 continue; | 1279 continue; |
| 1277 } | 1280 } |
| 1278 bool allow_fetching_more_results = false; | 1281 bool allow_fetching_more_results = false; |
| 1279 // This wasn't always around, so it's okay if it's missing. | 1282 // This wasn't always around, so it's okay if it's missing. |
| 1280 dict->GetBoolean(kCategoryContentAllowFetchingMore, | 1283 dict->GetBoolean(kCategoryContentAllowFetchingMore, |
| 1281 &allow_fetching_more_results); | 1284 &allow_fetching_more_results); |
| 1282 | 1285 |
| 1283 Category category = category_factory()->FromIDValue(id); | 1286 Category category = Category::FromIDValue(id); |
| 1287 // The ranker may not persist the order of remote categories. |
| 1288 category_ranker_->AppendCategoryIfNecessary(category); |
| 1284 // TODO(tschumann): The following has a bad smell that category | 1289 // TODO(tschumann): The following has a bad smell that category |
| 1285 // serialization / deserialization should not be done inside this | 1290 // serialization / deserialization should not be done inside this |
| 1286 // class. We should move that into a central place that also knows how to | 1291 // class. We should move that into a central place that also knows how to |
| 1287 // parse data we received from remote backends. | 1292 // parse data we received from remote backends. |
| 1288 CategoryInfo info = | 1293 CategoryInfo info = |
| 1289 category == articles_category_ | 1294 category == articles_category_ |
| 1290 ? BuildArticleCategoryInfo(title) | 1295 ? BuildArticleCategoryInfo(title) |
| 1291 : BuildRemoteCategoryInfo(title, allow_fetching_more_results); | 1296 : BuildRemoteCategoryInfo(title, allow_fetching_more_results); |
| 1292 CategoryContent* content = UpdateCategoryInfo(category, info); | 1297 CategoryContent* content = UpdateCategoryInfo(category, info); |
| 1293 content->included_in_last_server_response = | 1298 content->included_in_last_server_response = |
| 1294 included_in_last_server_response; | 1299 included_in_last_server_response; |
| 1295 } | 1300 } |
| 1296 } | 1301 } |
| 1297 | 1302 |
| 1298 void RemoteSuggestionsProvider::StoreCategoriesToPrefs() { | 1303 void RemoteSuggestionsProvider::StoreCategoriesToPrefs() { |
| 1299 // Collect all the CategoryContents. | 1304 // Collect all the CategoryContents. |
| 1300 std::vector<std::pair<Category, const CategoryContent*>> to_store; | 1305 std::vector<std::pair<Category, const CategoryContent*>> to_store; |
| 1301 for (const auto& entry : category_contents_) { | 1306 for (const auto& entry : category_contents_) { |
| 1302 to_store.emplace_back(entry.first, &entry.second); | 1307 to_store.emplace_back(entry.first, &entry.second); |
| 1303 } | 1308 } |
| 1304 // Sort them into the proper category order. | 1309 // The ranker may not persist the order, thus, it is stored by the provider. |
| 1305 std::sort(to_store.begin(), to_store.end(), | 1310 std::sort(to_store.begin(), to_store.end(), |
| 1306 [this](const std::pair<Category, const CategoryContent*>& left, | 1311 [this](const std::pair<Category, const CategoryContent*>& left, |
| 1307 const std::pair<Category, const CategoryContent*>& right) { | 1312 const std::pair<Category, const CategoryContent*>& right) { |
| 1308 return category_factory()->CompareCategories(left.first, | 1313 return category_ranker_->Compare(left.first, right.first); |
| 1309 right.first); | |
| 1310 }); | 1314 }); |
| 1311 // Convert the relevant info into a base::ListValue for storage. | 1315 // Convert the relevant info into a base::ListValue for storage. |
| 1312 base::ListValue list; | 1316 base::ListValue list; |
| 1313 for (const auto& entry : to_store) { | 1317 for (const auto& entry : to_store) { |
| 1314 const Category& category = entry.first; | 1318 const Category& category = entry.first; |
| 1315 const CategoryContent& content = *entry.second; | 1319 const CategoryContent& content = *entry.second; |
| 1316 auto dict = base::MakeUnique<base::DictionaryValue>(); | 1320 auto dict = base::MakeUnique<base::DictionaryValue>(); |
| 1317 dict->SetInteger(kCategoryContentId, category.id()); | 1321 dict->SetInteger(kCategoryContentId, category.id()); |
| 1318 // TODO(tschumann): Persist other properties of the CategoryInfo. | 1322 // TODO(tschumann): Persist other properties of the CategoryInfo. |
| 1319 dict->SetString(kCategoryContentTitle, content.info.title()); | 1323 dict->SetString(kCategoryContentTitle, content.info.title()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1334 RemoteSuggestionsProvider::CategoryContent::CategoryContent(CategoryContent&&) = | 1338 RemoteSuggestionsProvider::CategoryContent::CategoryContent(CategoryContent&&) = |
| 1335 default; | 1339 default; |
| 1336 | 1340 |
| 1337 RemoteSuggestionsProvider::CategoryContent::~CategoryContent() = default; | 1341 RemoteSuggestionsProvider::CategoryContent::~CategoryContent() = default; |
| 1338 | 1342 |
| 1339 RemoteSuggestionsProvider::CategoryContent& | 1343 RemoteSuggestionsProvider::CategoryContent& |
| 1340 RemoteSuggestionsProvider::CategoryContent::operator=(CategoryContent&&) = | 1344 RemoteSuggestionsProvider::CategoryContent::operator=(CategoryContent&&) = |
| 1341 default; | 1345 default; |
| 1342 | 1346 |
| 1343 } // namespace ntp_snippets | 1347 } // namespace ntp_snippets |
| OLD | NEW |