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

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: 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 342 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 380
381 NTPSnippetsFetcher::Params::Params() = default; 381 NTPSnippetsFetcher::Params::Params() = default;
382 NTPSnippetsFetcher::Params::Params(const Params&) = default; 382 NTPSnippetsFetcher::Params::Params(const Params&) = default;
383 NTPSnippetsFetcher::Params::~Params() = default; 383 NTPSnippetsFetcher::Params::~Params() = default;
384 384
385 NTPSnippetsFetcher::NTPSnippetsFetcher( 385 NTPSnippetsFetcher::NTPSnippetsFetcher(
386 SigninManagerBase* signin_manager, 386 SigninManagerBase* signin_manager,
387 OAuth2TokenService* token_service, 387 OAuth2TokenService* token_service,
388 scoped_refptr<URLRequestContextGetter> url_request_context_getter, 388 scoped_refptr<URLRequestContextGetter> url_request_context_getter,
389 PrefService* pref_service, 389 PrefService* pref_service,
390 CategoryFactory* category_factory,
391 LanguageModel* language_model, 390 LanguageModel* language_model,
392 const ParseJSONCallback& parse_json_callback, 391 const ParseJSONCallback& parse_json_callback,
393 const std::string& api_key, 392 const std::string& api_key,
394 const UserClassifier* user_classifier) 393 const UserClassifier* user_classifier)
395 : OAuth2TokenService::Consumer("ntp_snippets"), 394 : OAuth2TokenService::Consumer("ntp_snippets"),
396 signin_manager_(signin_manager), 395 signin_manager_(signin_manager),
397 token_service_(token_service), 396 token_service_(token_service),
398 url_request_context_getter_(std::move(url_request_context_getter)), 397 url_request_context_getter_(std::move(url_request_context_getter)),
399 category_factory_(category_factory),
400 language_model_(language_model), 398 language_model_(language_model),
401 parse_json_callback_(parse_json_callback), 399 parse_json_callback_(parse_json_callback),
402 fetch_url_(GetFetchEndpoint()), 400 fetch_url_(GetFetchEndpoint()),
403 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_) 401 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_)
404 ? FetchAPI::CHROME_CONTENT_SUGGESTIONS_API 402 ? FetchAPI::CHROME_CONTENT_SUGGESTIONS_API
405 : FetchAPI::CHROME_READER_API), 403 : FetchAPI::CHROME_READER_API),
406 api_key_(api_key), 404 api_key_(api_key),
407 tick_clock_(new base::DefaultTickClock()), 405 tick_clock_(new base::DefaultTickClock()),
408 user_classifier_(user_classifier), 406 user_classifier_(user_classifier),
409 request_throttler_rare_ntp_user_( 407 request_throttler_rare_ntp_user_(
(...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after
1025 FetchedCategoriesVector* categories) { 1023 FetchedCategoriesVector* categories) {
1026 const base::DictionaryValue* top_dict = nullptr; 1024 const base::DictionaryValue* top_dict = nullptr;
1027 if (!parsed.GetAsDictionary(&top_dict)) { 1025 if (!parsed.GetAsDictionary(&top_dict)) {
1028 return false; 1026 return false;
1029 } 1027 }
1030 1028
1031 switch (fetch_api_) { 1029 switch (fetch_api_) {
1032 case FetchAPI::CHROME_READER_API: { 1030 case FetchAPI::CHROME_READER_API: {
1033 const int kUnusedRemoteCategoryId = -1; 1031 const int kUnusedRemoteCategoryId = -1;
1034 categories->push_back(FetchedCategory( 1032 categories->push_back(FetchedCategory(
1035 category_factory_->FromKnownCategory(KnownCategories::ARTICLES), 1033 Category::FromKnownCategory(KnownCategories::ARTICLES),
1036 BuildArticleCategoryInfo(base::nullopt))); 1034 BuildArticleCategoryInfo(base::nullopt)));
1037 1035
1038 const base::ListValue* recos = nullptr; 1036 const base::ListValue* recos = nullptr;
1039 return top_dict->GetList("recos", &recos) && 1037 return top_dict->GetList("recos", &recos) &&
1040 AddSnippetsFromListValue(/*content_suggestions_api=*/false, 1038 AddSnippetsFromListValue(/*content_suggestions_api=*/false,
1041 kUnusedRemoteCategoryId, *recos, 1039 kUnusedRemoteCategoryId, *recos,
1042 &categories->back().snippets); 1040 &categories->back().snippets);
1043 } 1041 }
1044 1042
1045 case FetchAPI::CHROME_CONTENT_SUGGESTIONS_API: { 1043 case FetchAPI::CHROME_CONTENT_SUGGESTIONS_API: {
(...skipping 17 matching lines...) Expand all
1063 const base::ListValue* suggestions = nullptr; 1061 const base::ListValue* suggestions = nullptr;
1064 // Absence of a list of suggestions is treated as an empty list, which 1062 // Absence of a list of suggestions is treated as an empty list, which
1065 // is permissible. 1063 // is permissible.
1066 if (category_value->GetList("suggestions", &suggestions)) { 1064 if (category_value->GetList("suggestions", &suggestions)) {
1067 if (!AddSnippetsFromListValue( 1065 if (!AddSnippetsFromListValue(
1068 /*content_suggestions_api=*/true, remote_category_id, 1066 /*content_suggestions_api=*/true, remote_category_id,
1069 *suggestions, &snippets)) { 1067 *suggestions, &snippets)) {
1070 return false; 1068 return false;
1071 } 1069 }
1072 } 1070 }
1073 Category category = 1071 Category category = Category::FromRemoteCategory(remote_category_id);
1074 category_factory_->FromRemoteCategory(remote_category_id);
1075 if (category.IsKnownCategory(KnownCategories::ARTICLES)) { 1072 if (category.IsKnownCategory(KnownCategories::ARTICLES)) {
1076 categories->push_back(FetchedCategory( 1073 categories->push_back(FetchedCategory(
1077 category, 1074 category,
1078 BuildArticleCategoryInfo(base::UTF8ToUTF16(utf8_title)))); 1075 BuildArticleCategoryInfo(base::UTF8ToUTF16(utf8_title))));
1079 } else { 1076 } else {
1080 // TODO(tschumann): Right now, the backend does not yet populate this 1077 // TODO(tschumann): Right now, the backend does not yet populate this
1081 // field. Make it mandatory once the backends provide it. 1078 // field. Make it mandatory once the backends provide it.
1082 bool allow_fetching_more_results = false; 1079 bool allow_fetching_more_results = false;
1083 category_value->GetBoolean("allowFetchingMoreResults", 1080 category_value->GetBoolean("allowFetchingMoreResults",
1084 &allow_fetching_more_results); 1081 &allow_fetching_more_results);
(...skipping 25 matching lines...) Expand all
1110 NOTREACHED(); 1107 NOTREACHED();
1111 return false; 1108 return false;
1112 } 1109 }
1113 1110
1114 bool NTPSnippetsFetcher::NeedsAuthentication() const { 1111 bool NTPSnippetsFetcher::NeedsAuthentication() const {
1115 return (personalization_ == Personalization::kPersonal || 1112 return (personalization_ == Personalization::kPersonal ||
1116 personalization_ == Personalization::kBoth); 1113 personalization_ == Personalization::kBoth);
1117 } 1114 }
1118 1115
1119 } // namespace ntp_snippets 1116 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698