| 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/remote_suggestions_fetcher.h" | 5 #include "components/ntp_snippets/remote/remote_suggestions_fetcher.h" |
| 6 | 6 |
| 7 #include <cstdlib> | 7 #include <cstdlib> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 case FetchResult::INVALID_SNIPPET_CONTENT_ERROR: | 94 case FetchResult::INVALID_SNIPPET_CONTENT_ERROR: |
| 95 case FetchResult::JSON_PARSE_ERROR: | 95 case FetchResult::JSON_PARSE_ERROR: |
| 96 return Status(StatusCode::TEMPORARY_ERROR, FetchResultToString(result)); | 96 return Status(StatusCode::TEMPORARY_ERROR, FetchResultToString(result)); |
| 97 case FetchResult::RESULT_MAX: | 97 case FetchResult::RESULT_MAX: |
| 98 break; | 98 break; |
| 99 } | 99 } |
| 100 NOTREACHED(); | 100 NOTREACHED(); |
| 101 return Status(StatusCode::PERMANENT_ERROR, std::string()); | 101 return Status(StatusCode::PERMANENT_ERROR, std::string()); |
| 102 } | 102 } |
| 103 | 103 |
| 104 std::string GetFetchEndpoint() { | |
| 105 std::string endpoint = variations::GetVariationParamValueByFeature( | |
| 106 ntp_snippets::kArticleSuggestionsFeature, kContentSuggestionsBackend); | |
| 107 return endpoint.empty() ? kContentSuggestionsServer : endpoint; | |
| 108 } | |
| 109 | |
| 110 bool UsesChromeContentSuggestionsAPI(const GURL& endpoint) { | 104 bool UsesChromeContentSuggestionsAPI(const GURL& endpoint) { |
| 111 if (endpoint == kChromeReaderServer) { | 105 if (endpoint == kChromeReaderServer) { |
| 112 return false; | 106 return false; |
| 113 } | 107 } |
| 114 | 108 |
| 115 if (endpoint != kContentSuggestionsServer && | 109 if (endpoint != kContentSuggestionsServer && |
| 116 endpoint != kContentSuggestionsStagingServer && | 110 endpoint != kContentSuggestionsStagingServer && |
| 117 endpoint != kContentSuggestionsAlphaServer) { | 111 endpoint != kContentSuggestionsAlphaServer) { |
| 118 LOG(WARNING) << "Unknown value for " << kContentSuggestionsBackend << ": " | 112 LOG(WARNING) << "Unknown value for " << kContentSuggestionsBackend << ": " |
| 119 << endpoint << "; assuming chromecontentsuggestions-style API"; | 113 << endpoint << "; assuming chromecontentsuggestions-style API"; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 categories->clear(); | 178 categories->clear(); |
| 185 return; | 179 return; |
| 186 } | 180 } |
| 187 RemoteSuggestionsFetcher::FetchedCategory category = std::move(*category_it); | 181 RemoteSuggestionsFetcher::FetchedCategory category = std::move(*category_it); |
| 188 categories->clear(); | 182 categories->clear(); |
| 189 categories->push_back(std::move(category)); | 183 categories->push_back(std::move(category)); |
| 190 } | 184 } |
| 191 | 185 |
| 192 } // namespace | 186 } // namespace |
| 193 | 187 |
| 188 GURL GetFetchEndpoint(version_info::Channel channel) { |
| 189 std::string endpoint = variations::GetVariationParamValueByFeature( |
| 190 ntp_snippets::kArticleSuggestionsFeature, kContentSuggestionsBackend); |
| 191 if (!endpoint.empty()) { |
| 192 return GURL{endpoint}; |
| 193 } |
| 194 |
| 195 switch (channel) { |
| 196 case version_info::Channel::STABLE: |
| 197 case version_info::Channel::BETA: |
| 198 return GURL{kContentSuggestionsServer}; |
| 199 |
| 200 case version_info::Channel::DEV: |
| 201 case version_info::Channel::CANARY: |
| 202 case version_info::Channel::UNKNOWN: |
| 203 return GURL{kContentSuggestionsStagingServer}; |
| 204 } |
| 205 NOTREACHED(); |
| 206 return GURL{kContentSuggestionsStagingServer}; |
| 207 } |
| 208 |
| 194 CategoryInfo BuildArticleCategoryInfo( | 209 CategoryInfo BuildArticleCategoryInfo( |
| 195 const base::Optional<base::string16>& title) { | 210 const base::Optional<base::string16>& title) { |
| 196 return CategoryInfo( | 211 return CategoryInfo( |
| 197 title.has_value() ? title.value() | 212 title.has_value() ? title.value() |
| 198 : l10n_util::GetStringUTF16( | 213 : l10n_util::GetStringUTF16( |
| 199 IDS_NTP_ARTICLE_SUGGESTIONS_SECTION_HEADER), | 214 IDS_NTP_ARTICLE_SUGGESTIONS_SECTION_HEADER), |
| 200 ContentSuggestionsCardLayout::FULL_CARD, | 215 ContentSuggestionsCardLayout::FULL_CARD, |
| 201 /*has_fetch_action=*/true, | 216 /*has_fetch_action=*/true, |
| 202 /*has_view_all_action=*/false, | 217 /*has_view_all_action=*/false, |
| 203 /*show_if_empty=*/true, | 218 /*show_if_empty=*/true, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 230 RemoteSuggestionsFetcher::FetchedCategory::operator=(FetchedCategory&&) = | 245 RemoteSuggestionsFetcher::FetchedCategory::operator=(FetchedCategory&&) = |
| 231 default; | 246 default; |
| 232 | 247 |
| 233 RemoteSuggestionsFetcher::RemoteSuggestionsFetcher( | 248 RemoteSuggestionsFetcher::RemoteSuggestionsFetcher( |
| 234 SigninManagerBase* signin_manager, | 249 SigninManagerBase* signin_manager, |
| 235 OAuth2TokenService* token_service, | 250 OAuth2TokenService* token_service, |
| 236 scoped_refptr<URLRequestContextGetter> url_request_context_getter, | 251 scoped_refptr<URLRequestContextGetter> url_request_context_getter, |
| 237 PrefService* pref_service, | 252 PrefService* pref_service, |
| 238 LanguageModel* language_model, | 253 LanguageModel* language_model, |
| 239 const ParseJSONCallback& parse_json_callback, | 254 const ParseJSONCallback& parse_json_callback, |
| 255 const GURL& api_endpoint, |
| 240 const std::string& api_key, | 256 const std::string& api_key, |
| 241 const UserClassifier* user_classifier) | 257 const UserClassifier* user_classifier) |
| 242 : OAuth2TokenService::Consumer("ntp_snippets"), | 258 : OAuth2TokenService::Consumer("ntp_snippets"), |
| 243 signin_manager_(signin_manager), | 259 signin_manager_(signin_manager), |
| 244 token_service_(token_service), | 260 token_service_(token_service), |
| 245 url_request_context_getter_(std::move(url_request_context_getter)), | 261 url_request_context_getter_(std::move(url_request_context_getter)), |
| 246 language_model_(language_model), | 262 language_model_(language_model), |
| 247 parse_json_callback_(parse_json_callback), | 263 parse_json_callback_(parse_json_callback), |
| 248 fetch_url_(GetFetchEndpoint()), | 264 fetch_url_(api_endpoint), |
| 249 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_) | 265 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_) |
| 250 ? FetchAPI::CHROME_CONTENT_SUGGESTIONS_API | 266 ? FetchAPI::CHROME_CONTENT_SUGGESTIONS_API |
| 251 : FetchAPI::CHROME_READER_API), | 267 : FetchAPI::CHROME_READER_API), |
| 252 api_key_(api_key), | 268 api_key_(api_key), |
| 253 clock_(new base::DefaultClock()), | 269 clock_(new base::DefaultClock()), |
| 254 user_classifier_(user_classifier) {} | 270 user_classifier_(user_classifier) {} |
| 255 | 271 |
| 256 RemoteSuggestionsFetcher::~RemoteSuggestionsFetcher() { | 272 RemoteSuggestionsFetcher::~RemoteSuggestionsFetcher() { |
| 257 if (waiting_for_refresh_token_) { | 273 if (waiting_for_refresh_token_) { |
| 258 token_service_->RemoveObserver(this); | 274 token_service_->RemoveObserver(this); |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 535 categories->back().suggestions = std::move(suggestions); | 551 categories->back().suggestions = std::move(suggestions); |
| 536 } | 552 } |
| 537 return true; | 553 return true; |
| 538 } | 554 } |
| 539 } | 555 } |
| 540 NOTREACHED(); | 556 NOTREACHED(); |
| 541 return false; | 557 return false; |
| 542 } | 558 } |
| 543 | 559 |
| 544 } // namespace ntp_snippets | 560 } // namespace ntp_snippets |
| OLD | NEW |