| OLD | NEW |
| 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 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 | 416 |
| 417 NTPSnippetsFetcher::Params::Params() = default; | 417 NTPSnippetsFetcher::Params::Params() = default; |
| 418 NTPSnippetsFetcher::Params::Params(const Params&) = default; | 418 NTPSnippetsFetcher::Params::Params(const Params&) = default; |
| 419 NTPSnippetsFetcher::Params::~Params() = default; | 419 NTPSnippetsFetcher::Params::~Params() = default; |
| 420 | 420 |
| 421 NTPSnippetsFetcher::NTPSnippetsFetcher( | 421 NTPSnippetsFetcher::NTPSnippetsFetcher( |
| 422 SigninManagerBase* signin_manager, | 422 SigninManagerBase* signin_manager, |
| 423 OAuth2TokenService* token_service, | 423 OAuth2TokenService* token_service, |
| 424 scoped_refptr<URLRequestContextGetter> url_request_context_getter, | 424 scoped_refptr<URLRequestContextGetter> url_request_context_getter, |
| 425 PrefService* pref_service, | 425 PrefService* pref_service, |
| 426 CategoryFactory* category_factory, | |
| 427 LanguageModel* language_model, | 426 LanguageModel* language_model, |
| 428 const ParseJSONCallback& parse_json_callback, | 427 const ParseJSONCallback& parse_json_callback, |
| 429 const std::string& api_key, | 428 const std::string& api_key, |
| 430 const UserClassifier* user_classifier) | 429 const UserClassifier* user_classifier) |
| 431 : OAuth2TokenService::Consumer("ntp_snippets"), | 430 : OAuth2TokenService::Consumer("ntp_snippets"), |
| 432 signin_manager_(signin_manager), | 431 signin_manager_(signin_manager), |
| 433 token_service_(token_service), | 432 token_service_(token_service), |
| 434 url_request_context_getter_(std::move(url_request_context_getter)), | 433 url_request_context_getter_(std::move(url_request_context_getter)), |
| 435 category_factory_(category_factory), | |
| 436 language_model_(language_model), | 434 language_model_(language_model), |
| 437 parse_json_callback_(parse_json_callback), | 435 parse_json_callback_(parse_json_callback), |
| 438 fetch_url_(GetFetchEndpoint()), | 436 fetch_url_(GetFetchEndpoint()), |
| 439 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_) | 437 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_) |
| 440 ? FetchAPI::CHROME_CONTENT_SUGGESTIONS_API | 438 ? FetchAPI::CHROME_CONTENT_SUGGESTIONS_API |
| 441 : FetchAPI::CHROME_READER_API), | 439 : FetchAPI::CHROME_READER_API), |
| 442 api_key_(api_key), | 440 api_key_(api_key), |
| 443 tick_clock_(new base::DefaultTickClock()), | 441 tick_clock_(new base::DefaultTickClock()), |
| 444 user_classifier_(user_classifier), | 442 user_classifier_(user_classifier), |
| 445 request_throttler_rare_ntp_user_( | 443 request_throttler_rare_ntp_user_( |
| (...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1040 FetchedCategoriesVector* categories) { | 1038 FetchedCategoriesVector* categories) { |
| 1041 const base::DictionaryValue* top_dict = nullptr; | 1039 const base::DictionaryValue* top_dict = nullptr; |
| 1042 if (!parsed.GetAsDictionary(&top_dict)) { | 1040 if (!parsed.GetAsDictionary(&top_dict)) { |
| 1043 return false; | 1041 return false; |
| 1044 } | 1042 } |
| 1045 | 1043 |
| 1046 switch (fetch_api_) { | 1044 switch (fetch_api_) { |
| 1047 case FetchAPI::CHROME_READER_API: { | 1045 case FetchAPI::CHROME_READER_API: { |
| 1048 const int kUnusedRemoteCategoryId = -1; | 1046 const int kUnusedRemoteCategoryId = -1; |
| 1049 categories->push_back(FetchedCategory( | 1047 categories->push_back(FetchedCategory( |
| 1050 category_factory_->FromKnownCategory(KnownCategories::ARTICLES), | 1048 Category::FromKnownCategory(KnownCategories::ARTICLES), |
| 1051 BuildArticleCategoryInfo(base::nullopt))); | 1049 BuildArticleCategoryInfo(base::nullopt))); |
| 1052 | 1050 |
| 1053 const base::ListValue* recos = nullptr; | 1051 const base::ListValue* recos = nullptr; |
| 1054 return top_dict->GetList("recos", &recos) && | 1052 return top_dict->GetList("recos", &recos) && |
| 1055 AddSnippetsFromListValue(/*content_suggestions_api=*/false, | 1053 AddSnippetsFromListValue(/*content_suggestions_api=*/false, |
| 1056 kUnusedRemoteCategoryId, *recos, | 1054 kUnusedRemoteCategoryId, *recos, |
| 1057 &categories->back().snippets); | 1055 &categories->back().snippets); |
| 1058 } | 1056 } |
| 1059 | 1057 |
| 1060 case FetchAPI::CHROME_CONTENT_SUGGESTIONS_API: { | 1058 case FetchAPI::CHROME_CONTENT_SUGGESTIONS_API: { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1078 const base::ListValue* suggestions = nullptr; | 1076 const base::ListValue* suggestions = nullptr; |
| 1079 // Absence of a list of suggestions is treated as an empty list, which | 1077 // Absence of a list of suggestions is treated as an empty list, which |
| 1080 // is permissible. | 1078 // is permissible. |
| 1081 if (category_value->GetList("suggestions", &suggestions)) { | 1079 if (category_value->GetList("suggestions", &suggestions)) { |
| 1082 if (!AddSnippetsFromListValue( | 1080 if (!AddSnippetsFromListValue( |
| 1083 /*content_suggestions_api=*/true, remote_category_id, | 1081 /*content_suggestions_api=*/true, remote_category_id, |
| 1084 *suggestions, &snippets)) { | 1082 *suggestions, &snippets)) { |
| 1085 return false; | 1083 return false; |
| 1086 } | 1084 } |
| 1087 } | 1085 } |
| 1088 Category category = | 1086 Category category = Category::FromRemoteCategory(remote_category_id); |
| 1089 category_factory_->FromRemoteCategory(remote_category_id); | |
| 1090 if (category.IsKnownCategory(KnownCategories::ARTICLES)) { | 1087 if (category.IsKnownCategory(KnownCategories::ARTICLES)) { |
| 1091 categories->push_back(FetchedCategory( | 1088 categories->push_back(FetchedCategory( |
| 1092 category, | 1089 category, |
| 1093 BuildArticleCategoryInfo(base::UTF8ToUTF16(utf8_title)))); | 1090 BuildArticleCategoryInfo(base::UTF8ToUTF16(utf8_title)))); |
| 1094 } else { | 1091 } else { |
| 1095 // TODO(tschumann): Right now, the backend does not yet populate this | 1092 // TODO(tschumann): Right now, the backend does not yet populate this |
| 1096 // field. Make it mandatory once the backends provide it. | 1093 // field. Make it mandatory once the backends provide it. |
| 1097 bool allow_fetching_more_results = false; | 1094 bool allow_fetching_more_results = false; |
| 1098 category_value->GetBoolean("allowFetchingMoreResults", | 1095 category_value->GetBoolean("allowFetchingMoreResults", |
| 1099 &allow_fetching_more_results); | 1096 &allow_fetching_more_results); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1125 NOTREACHED(); | 1122 NOTREACHED(); |
| 1126 return false; | 1123 return false; |
| 1127 } | 1124 } |
| 1128 | 1125 |
| 1129 bool NTPSnippetsFetcher::NeedsAuthentication() const { | 1126 bool NTPSnippetsFetcher::NeedsAuthentication() const { |
| 1130 return (personalization_ == Personalization::kPersonal || | 1127 return (personalization_ == Personalization::kPersonal || |
| 1131 personalization_ == Personalization::kBoth); | 1128 personalization_ == Personalization::kBoth); |
| 1132 } | 1129 } |
| 1133 | 1130 |
| 1134 } // namespace ntp_snippets | 1131 } // namespace ntp_snippets |
| OLD | NEW |