| 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 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 608 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1028 FetchedCategoriesVector* categories) { | 1026 FetchedCategoriesVector* categories) { |
| 1029 const base::DictionaryValue* top_dict = nullptr; | 1027 const base::DictionaryValue* top_dict = nullptr; |
| 1030 if (!parsed.GetAsDictionary(&top_dict)) { | 1028 if (!parsed.GetAsDictionary(&top_dict)) { |
| 1031 return false; | 1029 return false; |
| 1032 } | 1030 } |
| 1033 | 1031 |
| 1034 switch (fetch_api_) { | 1032 switch (fetch_api_) { |
| 1035 case FetchAPI::CHROME_READER_API: { | 1033 case FetchAPI::CHROME_READER_API: { |
| 1036 const int kUnusedRemoteCategoryId = -1; | 1034 const int kUnusedRemoteCategoryId = -1; |
| 1037 categories->push_back(FetchedCategory( | 1035 categories->push_back(FetchedCategory( |
| 1038 category_factory_->FromKnownCategory(KnownCategories::ARTICLES), | 1036 Category::FromKnownCategory(KnownCategories::ARTICLES), |
| 1039 BuildArticleCategoryInfo(base::nullopt))); | 1037 BuildArticleCategoryInfo(base::nullopt))); |
| 1040 | 1038 |
| 1041 const base::ListValue* recos = nullptr; | 1039 const base::ListValue* recos = nullptr; |
| 1042 return top_dict->GetList("recos", &recos) && | 1040 return top_dict->GetList("recos", &recos) && |
| 1043 AddSnippetsFromListValue(/*content_suggestions_api=*/false, | 1041 AddSnippetsFromListValue(/*content_suggestions_api=*/false, |
| 1044 kUnusedRemoteCategoryId, *recos, | 1042 kUnusedRemoteCategoryId, *recos, |
| 1045 &categories->back().snippets); | 1043 &categories->back().snippets); |
| 1046 } | 1044 } |
| 1047 | 1045 |
| 1048 case FetchAPI::CHROME_CONTENT_SUGGESTIONS_API: { | 1046 case FetchAPI::CHROME_CONTENT_SUGGESTIONS_API: { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1066 const base::ListValue* suggestions = nullptr; | 1064 const base::ListValue* suggestions = nullptr; |
| 1067 // Absence of a list of suggestions is treated as an empty list, which | 1065 // Absence of a list of suggestions is treated as an empty list, which |
| 1068 // is permissible. | 1066 // is permissible. |
| 1069 if (category_value->GetList("suggestions", &suggestions)) { | 1067 if (category_value->GetList("suggestions", &suggestions)) { |
| 1070 if (!AddSnippetsFromListValue( | 1068 if (!AddSnippetsFromListValue( |
| 1071 /*content_suggestions_api=*/true, remote_category_id, | 1069 /*content_suggestions_api=*/true, remote_category_id, |
| 1072 *suggestions, &snippets)) { | 1070 *suggestions, &snippets)) { |
| 1073 return false; | 1071 return false; |
| 1074 } | 1072 } |
| 1075 } | 1073 } |
| 1076 Category category = | 1074 Category category = Category::FromRemoteCategory(remote_category_id); |
| 1077 category_factory_->FromRemoteCategory(remote_category_id); | |
| 1078 if (category.IsKnownCategory(KnownCategories::ARTICLES)) { | 1075 if (category.IsKnownCategory(KnownCategories::ARTICLES)) { |
| 1079 categories->push_back(FetchedCategory( | 1076 categories->push_back(FetchedCategory( |
| 1080 category, | 1077 category, |
| 1081 BuildArticleCategoryInfo(base::UTF8ToUTF16(utf8_title)))); | 1078 BuildArticleCategoryInfo(base::UTF8ToUTF16(utf8_title)))); |
| 1082 } else { | 1079 } else { |
| 1083 // TODO(tschumann): Right now, the backend does not yet populate this | 1080 // TODO(tschumann): Right now, the backend does not yet populate this |
| 1084 // field. Make it mandatory once the backends provide it. | 1081 // field. Make it mandatory once the backends provide it. |
| 1085 bool allow_fetching_more_results = false; | 1082 bool allow_fetching_more_results = false; |
| 1086 category_value->GetBoolean("allowFetchingMoreResults", | 1083 category_value->GetBoolean("allowFetchingMoreResults", |
| 1087 &allow_fetching_more_results); | 1084 &allow_fetching_more_results); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1113 NOTREACHED(); | 1110 NOTREACHED(); |
| 1114 return false; | 1111 return false; |
| 1115 } | 1112 } |
| 1116 | 1113 |
| 1117 bool NTPSnippetsFetcher::NeedsAuthentication() const { | 1114 bool NTPSnippetsFetcher::NeedsAuthentication() const { |
| 1118 return (personalization_ == Personalization::kPersonal || | 1115 return (personalization_ == Personalization::kPersonal || |
| 1119 personalization_ == Personalization::kBoth); | 1116 personalization_ == Personalization::kBoth); |
| 1120 } | 1117 } |
| 1121 | 1118 |
| 1122 } // namespace ntp_snippets | 1119 } // namespace ntp_snippets |
| OLD | NEW |