Chromium Code Reviews| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 | 45 |
| 46 namespace { | 46 namespace { |
| 47 | 47 |
| 48 const char kChromeReaderApiScope[] = | 48 const char kChromeReaderApiScope[] = |
| 49 "https://www.googleapis.com/auth/webhistory"; | 49 "https://www.googleapis.com/auth/webhistory"; |
| 50 const char kContentSuggestionsApiScope[] = | 50 const char kContentSuggestionsApiScope[] = |
| 51 "https://www.googleapis.com/auth/chrome-content-suggestions"; | 51 "https://www.googleapis.com/auth/chrome-content-suggestions"; |
| 52 const char kSnippetsServerNonAuthorizedFormat[] = "%s?key=%s"; | 52 const char kSnippetsServerNonAuthorizedFormat[] = "%s?key=%s"; |
| 53 const char kAuthorizationRequestHeaderFormat[] = "Bearer %s"; | 53 const char kAuthorizationRequestHeaderFormat[] = "Bearer %s"; |
| 54 | 54 |
| 55 // Variation parameter for personalizing fetching of suggestions. | |
| 56 const char kPersonalizationName[] = "fetching_personalization"; | |
| 57 | |
| 58 // Variation parameter for chrome-content-suggestions backend. | 55 // Variation parameter for chrome-content-suggestions backend. |
| 59 const char kContentSuggestionsBackend[] = "content_suggestions_backend"; | 56 const char kContentSuggestionsBackend[] = "content_suggestions_backend"; |
| 60 | 57 |
| 61 // Constants for possible values of the "fetching_personalization" parameter. | |
| 62 const char kPersonalizationPersonalString[] = "personal"; | |
| 63 const char kPersonalizationNonPersonalString[] = "non_personal"; | |
| 64 const char kPersonalizationBothString[] = "both"; // the default value | |
| 65 | |
| 66 const int kFetchTimeHistogramResolution = 5; | 58 const int kFetchTimeHistogramResolution = 5; |
| 67 | 59 |
| 68 std::string FetchResultToString(FetchResult result) { | 60 std::string FetchResultToString(FetchResult result) { |
| 69 switch (result) { | 61 switch (result) { |
| 70 case FetchResult::SUCCESS: | 62 case FetchResult::SUCCESS: |
| 71 return "OK"; | 63 return "OK"; |
| 72 case FetchResult::DEPRECATED_EMPTY_HOSTS: | 64 case FetchResult::DEPRECATED_EMPTY_HOSTS: |
| 73 return "Cannot fetch for empty hosts list."; | 65 return "Cannot fetch for empty hosts list."; |
| 74 case FetchResult::URL_REQUEST_STATUS_ERROR: | 66 case FetchResult::URL_REQUEST_STATUS_ERROR: |
| 75 return "URLRequestStatus error"; | 67 return "URLRequestStatus error"; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 112 case FetchResult::JSON_PARSE_ERROR: | 104 case FetchResult::JSON_PARSE_ERROR: |
| 113 return Status(StatusCode::TEMPORARY_ERROR, FetchResultToString(result)); | 105 return Status(StatusCode::TEMPORARY_ERROR, FetchResultToString(result)); |
| 114 case FetchResult::RESULT_MAX: | 106 case FetchResult::RESULT_MAX: |
| 115 break; | 107 break; |
| 116 } | 108 } |
| 117 NOTREACHED(); | 109 NOTREACHED(); |
| 118 return Status(StatusCode::PERMANENT_ERROR, std::string()); | 110 return Status(StatusCode::PERMANENT_ERROR, std::string()); |
| 119 } | 111 } |
| 120 | 112 |
| 121 std::string GetFetchEndpoint() { | 113 std::string GetFetchEndpoint() { |
| 122 std::string endpoint = variations::GetVariationParamValue( | 114 std::string endpoint = variations::GetVariationParamValueByFeature( |
| 123 ntp_snippets::kStudyName, kContentSuggestionsBackend); | 115 ntp_snippets::kArticleSuggestionsFeature, kContentSuggestionsBackend); |
|
Marc Treib
2017/01/30 17:02:57
This will break the current configs, right?
Shoul
jkrcal
2017/01/30 17:11:01
I would handle it in the configs, server-side.
Af
Marc Treib
2017/01/31 09:01:43
Actually on second thought, since the ZineServer e
| |
| 124 return endpoint.empty() ? kContentSuggestionsServer : endpoint; | 116 return endpoint.empty() ? kContentSuggestionsServer : endpoint; |
| 125 } | 117 } |
| 126 | 118 |
| 127 bool UsesChromeContentSuggestionsAPI(const GURL& endpoint) { | 119 bool UsesChromeContentSuggestionsAPI(const GURL& endpoint) { |
| 128 if (endpoint == kChromeReaderServer) { | 120 if (endpoint == kChromeReaderServer) { |
| 129 return false; | 121 return false; |
| 130 } | 122 } |
| 131 | 123 |
| 132 if (endpoint != kContentSuggestionsServer && | 124 if (endpoint != kContentSuggestionsServer && |
| 133 endpoint != kContentSuggestionsStagingServer && | 125 endpoint != kContentSuggestionsStagingServer && |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 277 RequestThrottler::RequestType:: | 269 RequestThrottler::RequestType:: |
| 278 CONTENT_SUGGESTION_FETCHER_RARE_NTP_USER), | 270 CONTENT_SUGGESTION_FETCHER_RARE_NTP_USER), |
| 279 request_throttler_active_ntp_user_( | 271 request_throttler_active_ntp_user_( |
| 280 pref_service, | 272 pref_service, |
| 281 RequestThrottler::RequestType:: | 273 RequestThrottler::RequestType:: |
| 282 CONTENT_SUGGESTION_FETCHER_ACTIVE_NTP_USER), | 274 CONTENT_SUGGESTION_FETCHER_ACTIVE_NTP_USER), |
| 283 request_throttler_active_suggestions_consumer_( | 275 request_throttler_active_suggestions_consumer_( |
| 284 pref_service, | 276 pref_service, |
| 285 RequestThrottler::RequestType:: | 277 RequestThrottler::RequestType:: |
| 286 CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTIONS_CONSUMER), | 278 CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTIONS_CONSUMER), |
| 287 weak_ptr_factory_(this) { | 279 weak_ptr_factory_(this) {} |
| 288 std::string personalization = variations::GetVariationParamValue( | |
| 289 ntp_snippets::kStudyName, kPersonalizationName); | |
| 290 if (personalization == kPersonalizationNonPersonalString) { | |
| 291 personalization_ = Personalization::kNonPersonal; | |
| 292 } else if (personalization == kPersonalizationPersonalString) { | |
| 293 personalization_ = Personalization::kPersonal; | |
| 294 } else { | |
| 295 personalization_ = Personalization::kBoth; | |
| 296 LOG_IF(WARNING, !personalization.empty() && | |
| 297 personalization != kPersonalizationBothString) | |
| 298 << "Unknown value for " << kPersonalizationName << ": " | |
| 299 << personalization; | |
| 300 } | |
| 301 } | |
| 302 | 280 |
| 303 RemoteSuggestionsFetcher::~RemoteSuggestionsFetcher() { | 281 RemoteSuggestionsFetcher::~RemoteSuggestionsFetcher() { |
| 304 if (waiting_for_refresh_token_) { | 282 if (waiting_for_refresh_token_) { |
| 305 token_service_->RemoveObserver(this); | 283 token_service_->RemoveObserver(this); |
| 306 } | 284 } |
| 307 } | 285 } |
| 308 | 286 |
| 309 void RemoteSuggestionsFetcher::FetchSnippets( | 287 void RemoteSuggestionsFetcher::FetchSnippets( |
| 310 const RequestParams& params, | 288 const RequestParams& params, |
| 311 SnippetsAvailableCallback callback) { | 289 SnippetsAvailableCallback callback) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 326 GetMinuteOfTheDay(/*local_time=*/false, | 304 GetMinuteOfTheDay(/*local_time=*/false, |
| 327 /*reduced_resolution=*/true)); | 305 /*reduced_resolution=*/true)); |
| 328 } | 306 } |
| 329 | 307 |
| 330 JsonRequest::Builder builder; | 308 JsonRequest::Builder builder; |
| 331 builder.SetFetchAPI(fetch_api_) | 309 builder.SetFetchAPI(fetch_api_) |
| 332 .SetFetchAPI(fetch_api_) | 310 .SetFetchAPI(fetch_api_) |
| 333 .SetLanguageModel(language_model_) | 311 .SetLanguageModel(language_model_) |
| 334 .SetParams(params) | 312 .SetParams(params) |
| 335 .SetParseJsonCallback(parse_json_callback_) | 313 .SetParseJsonCallback(parse_json_callback_) |
| 336 .SetPersonalization(personalization_) | |
| 337 .SetTickClock(tick_clock_.get()) | 314 .SetTickClock(tick_clock_.get()) |
| 338 .SetUrlRequestContextGetter(url_request_context_getter_) | 315 .SetUrlRequestContextGetter(url_request_context_getter_) |
| 339 .SetUserClassifier(*user_classifier_); | 316 .SetUserClassifier(*user_classifier_); |
| 340 | 317 |
| 341 if (NeedsAuthentication() && signin_manager_->IsAuthenticated()) { | 318 if (signin_manager_->IsAuthenticated()) { |
| 342 // Signed-in: get OAuth token --> fetch suggestions. | 319 // Signed-in: get OAuth token --> fetch suggestions. |
| 343 oauth_token_retried_ = false; | 320 oauth_token_retried_ = false; |
| 344 pending_requests_.emplace(std::move(builder), std::move(callback)); | 321 pending_requests_.emplace(std::move(builder), std::move(callback)); |
| 345 StartTokenRequest(); | 322 StartTokenRequest(); |
| 346 } else if (NeedsAuthentication() && signin_manager_->AuthInProgress()) { | 323 } else if (signin_manager_->AuthInProgress()) { |
| 347 // Currently signing in: wait for auth to finish (the refresh token) --> | 324 // Currently signing in: wait for auth to finish (the refresh token) --> |
| 348 // get OAuth token --> fetch suggestions. | 325 // get OAuth token --> fetch suggestions. |
| 349 pending_requests_.emplace(std::move(builder), std::move(callback)); | 326 pending_requests_.emplace(std::move(builder), std::move(callback)); |
| 350 if (!waiting_for_refresh_token_) { | 327 if (!waiting_for_refresh_token_) { |
| 351 // Wait until we get a refresh token. | 328 // Wait until we get a refresh token. |
| 352 waiting_for_refresh_token_ = true; | 329 waiting_for_refresh_token_ = true; |
| 353 token_service_->AddObserver(this); | 330 token_service_->AddObserver(this); |
| 354 } | 331 } |
| 355 } else { | 332 } else { |
| 356 // Not signed in: fetch suggestions (without authentication). | 333 // Not signed in: fetch suggestions (without authentication). |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 600 return request_throttler_active_ntp_user_.DemandQuotaForRequest( | 577 return request_throttler_active_ntp_user_.DemandQuotaForRequest( |
| 601 interactive_request); | 578 interactive_request); |
| 602 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: | 579 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: |
| 603 return request_throttler_active_suggestions_consumer_ | 580 return request_throttler_active_suggestions_consumer_ |
| 604 .DemandQuotaForRequest(interactive_request); | 581 .DemandQuotaForRequest(interactive_request); |
| 605 } | 582 } |
| 606 NOTREACHED(); | 583 NOTREACHED(); |
| 607 return false; | 584 return false; |
| 608 } | 585 } |
| 609 | 586 |
| 610 bool RemoteSuggestionsFetcher::NeedsAuthentication() const { | |
| 611 return (personalization_ == Personalization::kPersonal || | |
| 612 personalization_ == Personalization::kBoth); | |
| 613 } | |
| 614 | |
| 615 std::string RemoteSuggestionsFetcher::PersonalizationModeString() const { | |
| 616 switch (personalization_) { | |
| 617 case Personalization::kPersonal: | |
| 618 return "Only personalized"; | |
| 619 break; | |
| 620 case Personalization::kBoth: | |
| 621 return "Both personalized and non-personalized"; | |
| 622 break; | |
| 623 case Personalization::kNonPersonal: | |
| 624 return "Only non-personalized"; | |
| 625 break; | |
| 626 } | |
| 627 NOTREACHED(); | |
| 628 return std::string(); | |
| 629 } | |
| 630 | |
| 631 } // namespace ntp_snippets | 587 } // namespace ntp_snippets |
| OLD | NEW |