| 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 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 640 DCHECK(base::ContainsKey(category_contents_, articles_category_)); | 642 DCHECK(base::ContainsKey(category_contents_, articles_category_)); |
| 641 | 643 |
| 642 base::TimeDelta database_load_time = | 644 base::TimeDelta database_load_time = |
| 643 base::TimeTicks::Now() - database_load_start_; | 645 base::TimeTicks::Now() - database_load_start_; |
| 644 UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.Snippets.DatabaseLoadTime", | 646 UMA_HISTOGRAM_MEDIUM_TIMES("NewTabPage.Snippets.DatabaseLoadTime", |
| 645 database_load_time); | 647 database_load_time); |
| 646 | 648 |
| 647 NTPSnippet::PtrVector to_delete; | 649 NTPSnippet::PtrVector to_delete; |
| 648 for (std::unique_ptr<NTPSnippet>& snippet : snippets) { | 650 for (std::unique_ptr<NTPSnippet>& snippet : snippets) { |
| 649 Category snippet_category = | 651 Category snippet_category = |
| 650 category_factory()->FromRemoteCategory(snippet->remote_category_id()); | 652 Category::FromRemoteCategory(snippet->remote_category_id()); |
| 651 auto content_it = category_contents_.find(snippet_category); | 653 auto content_it = category_contents_.find(snippet_category); |
| 652 // We should already know about the category. | 654 // We should already know about the category. |
| 653 if (content_it == category_contents_.end()) { | 655 if (content_it == category_contents_.end()) { |
| 654 DLOG(WARNING) << "Loaded a suggestion for unknown category " | 656 DLOG(WARNING) << "Loaded a suggestion for unknown category " |
| 655 << snippet_category << " from the DB; deleting"; | 657 << snippet_category << " from the DB; deleting"; |
| 656 to_delete.emplace_back(std::move(snippet)); | 658 to_delete.emplace_back(std::move(snippet)); |
| 657 continue; | 659 continue; |
| 658 } | 660 } |
| 659 CategoryContent* content = &content_it->second; | 661 CategoryContent* content = &content_it->second; |
| 660 if (snippet->is_dismissed()) { | 662 if (snippet->is_dismissed()) { |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 790 for (NTPSnippetsFetcher::FetchedCategory& fetched_category : | 792 for (NTPSnippetsFetcher::FetchedCategory& fetched_category : |
| 791 *fetched_categories) { | 793 *fetched_categories) { |
| 792 // TODO(tschumann): Remove this histogram once we only talk to the content | 794 // TODO(tschumann): Remove this histogram once we only talk to the content |
| 793 // suggestions cloud backend. | 795 // suggestions cloud backend. |
| 794 if (fetched_category.category == articles_category_) { | 796 if (fetched_category.category == articles_category_) { |
| 795 UMA_HISTOGRAM_SPARSE_SLOWLY( | 797 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 796 "NewTabPage.Snippets.NumArticlesFetched", | 798 "NewTabPage.Snippets.NumArticlesFetched", |
| 797 std::min(fetched_category.snippets.size(), | 799 std::min(fetched_category.snippets.size(), |
| 798 static_cast<size_t>(kMaxSnippetCount + 1))); | 800 static_cast<size_t>(kMaxSnippetCount + 1))); |
| 799 } | 801 } |
| 802 category_ranker_->AppendCategoryIfNecessary(fetched_category.category); |
| 800 CategoryContent* content = | 803 CategoryContent* content = |
| 801 UpdateCategoryInfo(fetched_category.category, fetched_category.info); | 804 UpdateCategoryInfo(fetched_category.category, fetched_category.info); |
| 802 content->included_in_last_server_response = true; | 805 content->included_in_last_server_response = true; |
| 803 SanitizeReceivedSnippets(content->dismissed, &fetched_category.snippets); | 806 SanitizeReceivedSnippets(content->dismissed, &fetched_category.snippets); |
| 804 IntegrateSnippets(content, std::move(fetched_category.snippets)); | 807 IntegrateSnippets(content, std::move(fetched_category.snippets)); |
| 805 } | 808 } |
| 806 } | 809 } |
| 807 | 810 |
| 808 // TODO(tschumann): The snippets fetcher needs to signal errors so that we | 811 // TODO(tschumann): The snippets fetcher needs to signal errors so that we |
| 809 // know why we received no data. If an error occured, none of the following | 812 // 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... |
| 1280 &included_in_last_server_response)) { | 1283 &included_in_last_server_response)) { |
| 1281 DLOG(WARNING) << "Invalid category pref value, missing '" | 1284 DLOG(WARNING) << "Invalid category pref value, missing '" |
| 1282 << kCategoryContentProvidedByServer << "': " << *entry; | 1285 << kCategoryContentProvidedByServer << "': " << *entry; |
| 1283 continue; | 1286 continue; |
| 1284 } | 1287 } |
| 1285 bool allow_fetching_more_results = false; | 1288 bool allow_fetching_more_results = false; |
| 1286 // This wasn't always around, so it's okay if it's missing. | 1289 // This wasn't always around, so it's okay if it's missing. |
| 1287 dict->GetBoolean(kCategoryContentAllowFetchingMore, | 1290 dict->GetBoolean(kCategoryContentAllowFetchingMore, |
| 1288 &allow_fetching_more_results); | 1291 &allow_fetching_more_results); |
| 1289 | 1292 |
| 1290 Category category = category_factory()->FromIDValue(id); | 1293 Category category = Category::FromIDValue(id); |
| 1294 // The ranker may not persist the order of remote categories. |
| 1295 category_ranker_->AppendCategoryIfNecessary(category); |
| 1291 // TODO(tschumann): The following has a bad smell that category | 1296 // TODO(tschumann): The following has a bad smell that category |
| 1292 // serialization / deserialization should not be done inside this | 1297 // serialization / deserialization should not be done inside this |
| 1293 // class. We should move that into a central place that also knows how to | 1298 // class. We should move that into a central place that also knows how to |
| 1294 // parse data we received from remote backends. | 1299 // parse data we received from remote backends. |
| 1295 CategoryInfo info = | 1300 CategoryInfo info = |
| 1296 category == articles_category_ | 1301 category == articles_category_ |
| 1297 ? BuildArticleCategoryInfo(title) | 1302 ? BuildArticleCategoryInfo(title) |
| 1298 : BuildRemoteCategoryInfo(title, allow_fetching_more_results); | 1303 : BuildRemoteCategoryInfo(title, allow_fetching_more_results); |
| 1299 CategoryContent* content = UpdateCategoryInfo(category, info); | 1304 CategoryContent* content = UpdateCategoryInfo(category, info); |
| 1300 content->included_in_last_server_response = | 1305 content->included_in_last_server_response = |
| 1301 included_in_last_server_response; | 1306 included_in_last_server_response; |
| 1302 } | 1307 } |
| 1303 } | 1308 } |
| 1304 | 1309 |
| 1305 void RemoteSuggestionsProvider::StoreCategoriesToPrefs() { | 1310 void RemoteSuggestionsProvider::StoreCategoriesToPrefs() { |
| 1306 // Collect all the CategoryContents. | 1311 // Collect all the CategoryContents. |
| 1307 std::vector<std::pair<Category, const CategoryContent*>> to_store; | 1312 std::vector<std::pair<Category, const CategoryContent*>> to_store; |
| 1308 for (const auto& entry : category_contents_) { | 1313 for (const auto& entry : category_contents_) { |
| 1309 to_store.emplace_back(entry.first, &entry.second); | 1314 to_store.emplace_back(entry.first, &entry.second); |
| 1310 } | 1315 } |
| 1311 // Sort them into the proper category order. | 1316 // The ranker may not persist the order, thus, it is stored by the provider. |
| 1312 std::sort(to_store.begin(), to_store.end(), | 1317 std::sort(to_store.begin(), to_store.end(), |
| 1313 [this](const std::pair<Category, const CategoryContent*>& left, | 1318 [this](const std::pair<Category, const CategoryContent*>& left, |
| 1314 const std::pair<Category, const CategoryContent*>& right) { | 1319 const std::pair<Category, const CategoryContent*>& right) { |
| 1315 return category_factory()->CompareCategories(left.first, | 1320 return category_ranker_->Compare(left.first, right.first); |
| 1316 right.first); | |
| 1317 }); | 1321 }); |
| 1318 // Convert the relevant info into a base::ListValue for storage. | 1322 // Convert the relevant info into a base::ListValue for storage. |
| 1319 base::ListValue list; | 1323 base::ListValue list; |
| 1320 for (const auto& entry : to_store) { | 1324 for (const auto& entry : to_store) { |
| 1321 const Category& category = entry.first; | 1325 const Category& category = entry.first; |
| 1322 const CategoryContent& content = *entry.second; | 1326 const CategoryContent& content = *entry.second; |
| 1323 auto dict = base::MakeUnique<base::DictionaryValue>(); | 1327 auto dict = base::MakeUnique<base::DictionaryValue>(); |
| 1324 dict->SetInteger(kCategoryContentId, category.id()); | 1328 dict->SetInteger(kCategoryContentId, category.id()); |
| 1325 // TODO(tschumann): Persist other properties of the CategoryInfo. | 1329 // TODO(tschumann): Persist other properties of the CategoryInfo. |
| 1326 dict->SetString(kCategoryContentTitle, content.info.title()); | 1330 dict->SetString(kCategoryContentTitle, content.info.title()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1341 RemoteSuggestionsProvider::CategoryContent::CategoryContent(CategoryContent&&) = | 1345 RemoteSuggestionsProvider::CategoryContent::CategoryContent(CategoryContent&&) = |
| 1342 default; | 1346 default; |
| 1343 | 1347 |
| 1344 RemoteSuggestionsProvider::CategoryContent::~CategoryContent() = default; | 1348 RemoteSuggestionsProvider::CategoryContent::~CategoryContent() = default; |
| 1345 | 1349 |
| 1346 RemoteSuggestionsProvider::CategoryContent& | 1350 RemoteSuggestionsProvider::CategoryContent& |
| 1347 RemoteSuggestionsProvider::CategoryContent::operator=(CategoryContent&&) = | 1351 RemoteSuggestionsProvider::CategoryContent::operator=(CategoryContent&&) = |
| 1348 default; | 1352 default; |
| 1349 | 1353 |
| 1350 } // namespace ntp_snippets | 1354 } // namespace ntp_snippets |
| OLD | NEW |