Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: components/ntp_snippets/remote/ntp_snippets_fetcher.cc

Issue 2568033005: [NTP::SectionOrder] Replace CategoryFactory with a category ranker. (Closed)
Patch Set: treib@ nits. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/remote/ntp_snippets_fetcher.h" 5 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cstdlib> 8 #include <cstdlib>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
14 #include "base/json/json_writer.h" 14 #include "base/json/json_writer.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/metrics/histogram_macros.h" 16 #include "base/metrics/histogram_macros.h"
17 #include "base/metrics/sparse_histogram.h" 17 #include "base/metrics/sparse_histogram.h"
18 #include "base/path_service.h" 18 #include "base/path_service.h"
19 #include "base/strings/string_number_conversions.h" 19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/string_util.h" 20 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h" 21 #include "base/strings/stringprintf.h"
22 #include "base/strings/utf_string_conversions.h" 22 #include "base/strings/utf_string_conversions.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/ntp_snippets/category_factory.h" 27 #include "components/ntp_snippets/category.h"
28 #include "components/ntp_snippets/features.h" 28 #include "components/ntp_snippets/features.h"
29 #include "components/ntp_snippets/ntp_snippets_constants.h" 29 #include "components/ntp_snippets/ntp_snippets_constants.h"
30 #include "components/ntp_snippets/user_classifier.h" 30 #include "components/ntp_snippets/user_classifier.h"
31 #include "components/signin/core/browser/profile_oauth2_token_service.h" 31 #include "components/signin/core/browser/profile_oauth2_token_service.h"
32 #include "components/signin/core/browser/signin_manager.h" 32 #include "components/signin/core/browser/signin_manager.h"
33 #include "components/signin/core/browser/signin_manager_base.h" 33 #include "components/signin/core/browser/signin_manager_base.h"
34 #include "components/variations/net/variations_http_headers.h" 34 #include "components/variations/net/variations_http_headers.h"
35 #include "components/variations/variations_associated_data.h" 35 #include "components/variations/variations_associated_data.h"
36 #include "grit/components_strings.h" 36 #include "grit/components_strings.h"
37 #include "net/base/load_flags.h" 37 #include "net/base/load_flags.h"
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 390
391 NTPSnippetsFetcher::Params::Params() = default; 391 NTPSnippetsFetcher::Params::Params() = default;
392 NTPSnippetsFetcher::Params::Params(const Params&) = default; 392 NTPSnippetsFetcher::Params::Params(const Params&) = default;
393 NTPSnippetsFetcher::Params::~Params() = default; 393 NTPSnippetsFetcher::Params::~Params() = default;
394 394
395 NTPSnippetsFetcher::NTPSnippetsFetcher( 395 NTPSnippetsFetcher::NTPSnippetsFetcher(
396 SigninManagerBase* signin_manager, 396 SigninManagerBase* signin_manager,
397 OAuth2TokenService* token_service, 397 OAuth2TokenService* token_service,
398 scoped_refptr<URLRequestContextGetter> url_request_context_getter, 398 scoped_refptr<URLRequestContextGetter> url_request_context_getter,
399 PrefService* pref_service, 399 PrefService* pref_service,
400 CategoryFactory* category_factory,
401 LanguageModel* language_model, 400 LanguageModel* language_model,
402 const ParseJSONCallback& parse_json_callback, 401 const ParseJSONCallback& parse_json_callback,
403 const std::string& api_key, 402 const std::string& api_key,
404 const UserClassifier* user_classifier) 403 const UserClassifier* user_classifier)
405 : OAuth2TokenService::Consumer("ntp_snippets"), 404 : OAuth2TokenService::Consumer("ntp_snippets"),
406 signin_manager_(signin_manager), 405 signin_manager_(signin_manager),
407 token_service_(token_service), 406 token_service_(token_service),
408 url_request_context_getter_(std::move(url_request_context_getter)), 407 url_request_context_getter_(std::move(url_request_context_getter)),
409 category_factory_(category_factory),
410 language_model_(language_model), 408 language_model_(language_model),
411 parse_json_callback_(parse_json_callback), 409 parse_json_callback_(parse_json_callback),
412 fetch_url_(GetFetchEndpoint()), 410 fetch_url_(GetFetchEndpoint()),
413 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_) 411 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_)
414 ? FetchAPI::CHROME_CONTENT_SUGGESTIONS_API 412 ? FetchAPI::CHROME_CONTENT_SUGGESTIONS_API
415 : FetchAPI::CHROME_READER_API), 413 : FetchAPI::CHROME_READER_API),
416 api_key_(api_key), 414 api_key_(api_key),
417 tick_clock_(new base::DefaultTickClock()), 415 tick_clock_(new base::DefaultTickClock()),
418 user_classifier_(user_classifier), 416 user_classifier_(user_classifier),
419 request_throttler_rare_ntp_user_( 417 request_throttler_rare_ntp_user_(
(...skipping 593 matching lines...) Expand 10 before | Expand all | Expand 10 after
1013 FetchedCategoriesVector* categories) { 1011 FetchedCategoriesVector* categories) {
1014 const base::DictionaryValue* top_dict = nullptr; 1012 const base::DictionaryValue* top_dict = nullptr;
1015 if (!parsed.GetAsDictionary(&top_dict)) { 1013 if (!parsed.GetAsDictionary(&top_dict)) {
1016 return false; 1014 return false;
1017 } 1015 }
1018 1016
1019 switch (fetch_api_) { 1017 switch (fetch_api_) {
1020 case FetchAPI::CHROME_READER_API: { 1018 case FetchAPI::CHROME_READER_API: {
1021 const int kUnusedRemoteCategoryId = -1; 1019 const int kUnusedRemoteCategoryId = -1;
1022 categories->push_back(FetchedCategory( 1020 categories->push_back(FetchedCategory(
1023 category_factory_->FromKnownCategory(KnownCategories::ARTICLES), 1021 Category::FromKnownCategory(KnownCategories::ARTICLES),
1024 BuildArticleCategoryInfo(base::nullopt))); 1022 BuildArticleCategoryInfo(base::nullopt)));
1025 1023
1026 const base::ListValue* recos = nullptr; 1024 const base::ListValue* recos = nullptr;
1027 return top_dict->GetList("recos", &recos) && 1025 return top_dict->GetList("recos", &recos) &&
1028 AddSnippetsFromListValue(/*content_suggestions_api=*/false, 1026 AddSnippetsFromListValue(/*content_suggestions_api=*/false,
1029 kUnusedRemoteCategoryId, *recos, 1027 kUnusedRemoteCategoryId, *recos,
1030 &categories->back().snippets); 1028 &categories->back().snippets);
1031 } 1029 }
1032 1030
1033 case FetchAPI::CHROME_CONTENT_SUGGESTIONS_API: { 1031 case FetchAPI::CHROME_CONTENT_SUGGESTIONS_API: {
(...skipping 17 matching lines...) Expand all
1051 const base::ListValue* suggestions = nullptr; 1049 const base::ListValue* suggestions = nullptr;
1052 // Absence of a list of suggestions is treated as an empty list, which 1050 // Absence of a list of suggestions is treated as an empty list, which
1053 // is permissible. 1051 // is permissible.
1054 if (category_value->GetList("suggestions", &suggestions)) { 1052 if (category_value->GetList("suggestions", &suggestions)) {
1055 if (!AddSnippetsFromListValue( 1053 if (!AddSnippetsFromListValue(
1056 /*content_suggestions_api=*/true, remote_category_id, 1054 /*content_suggestions_api=*/true, remote_category_id,
1057 *suggestions, &snippets)) { 1055 *suggestions, &snippets)) {
1058 return false; 1056 return false;
1059 } 1057 }
1060 } 1058 }
1061 Category category = 1059 Category category = Category::FromRemoteCategory(remote_category_id);
1062 category_factory_->FromRemoteCategory(remote_category_id);
1063 if (category.IsKnownCategory(KnownCategories::ARTICLES)) { 1060 if (category.IsKnownCategory(KnownCategories::ARTICLES)) {
1064 categories->push_back(FetchedCategory( 1061 categories->push_back(FetchedCategory(
1065 category, 1062 category,
1066 BuildArticleCategoryInfo(base::UTF8ToUTF16(utf8_title)))); 1063 BuildArticleCategoryInfo(base::UTF8ToUTF16(utf8_title))));
1067 } else { 1064 } else {
1068 // TODO(tschumann): Right now, the backend does not yet populate this 1065 // TODO(tschumann): Right now, the backend does not yet populate this
1069 // field. Make it mandatory once the backends provide it. 1066 // field. Make it mandatory once the backends provide it.
1070 bool allow_fetching_more_results = false; 1067 bool allow_fetching_more_results = false;
1071 category_value->GetBoolean("allowFetchingMoreResults", 1068 category_value->GetBoolean("allowFetchingMoreResults",
1072 &allow_fetching_more_results); 1069 &allow_fetching_more_results);
(...skipping 25 matching lines...) Expand all
1098 NOTREACHED(); 1095 NOTREACHED();
1099 return false; 1096 return false;
1100 } 1097 }
1101 1098
1102 bool NTPSnippetsFetcher::NeedsAuthentication() const { 1099 bool NTPSnippetsFetcher::NeedsAuthentication() const {
1103 return (personalization_ == Personalization::kPersonal || 1100 return (personalization_ == Personalization::kPersonal ||
1104 personalization_ == Personalization::kBoth); 1101 personalization_ == Personalization::kBoth);
1105 } 1102 }
1106 1103
1107 } // namespace ntp_snippets 1104 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698