| 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/json_request.h" | 5 #include "components/ntp_snippets/remote/json_request.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 using net::URLRequestContextGetter; | 42 using net::URLRequestContextGetter; |
| 43 using net::HttpRequestHeaders; | 43 using net::HttpRequestHeaders; |
| 44 using net::URLRequestStatus; | 44 using net::URLRequestStatus; |
| 45 | 45 |
| 46 namespace ntp_snippets { | 46 namespace ntp_snippets { |
| 47 | 47 |
| 48 namespace internal { | 48 namespace internal { |
| 49 | 49 |
| 50 namespace { | 50 namespace { |
| 51 | 51 |
| 52 // Variation parameter for disabling the retry. | 52 constexpr base::FeatureParam<int> kBackground5xxRetries{ |
| 53 const char kBackground5xxRetriesName[] = "background_5xx_retries_count"; | 53 &ntp_snippets::kArticleSuggestionsFeature, "background_5xx_retries_count", |
| 54 0}; |
| 54 | 55 |
| 55 // Variation parameter for sending UrlLanguageHistogram info to the server. | 56 // Variation parameter for sending UrlLanguageHistogram info to the server. |
| 56 const char kSendTopLanguagesName[] = "send_top_languages"; | 57 constexpr base::FeatureParam<bool> kSendTopLanguages{ |
| 58 &ntp_snippets::kArticleSuggestionsFeature, "send_top_languages", false}; |
| 57 | 59 |
| 58 // Variation parameter for sending UserClassifier info to the server. | 60 // Variation parameter for sending UserClassifier info to the server. |
| 59 const char kSendUserClassName[] = "send_user_class"; | 61 constexpr base::FeatureParam<bool> kSendUserClassNameParam{ |
| 62 &kArticleSuggestionsFeature, "send_user_class", false}; |
| 60 | 63 |
| 61 int Get5xxRetryCount(bool interactive_request) { | 64 int Get5xxRetryCount(bool interactive_request) { |
| 62 if (interactive_request) { | 65 if (interactive_request) { |
| 63 return 2; | 66 return 2; |
| 64 } | 67 } |
| 65 return std::max(0, variations::GetVariationParamByFeatureAsInt( | 68 return std::max(0, kBackground5xxRetries.Get()); |
| 66 ntp_snippets::kArticleSuggestionsFeature, | |
| 67 kBackground5xxRetriesName, 0)); | |
| 68 } | |
| 69 | |
| 70 bool IsSendingTopLanguagesEnabled() { | |
| 71 return variations::GetVariationParamByFeatureAsBool( | |
| 72 ntp_snippets::kArticleSuggestionsFeature, kSendTopLanguagesName, | |
| 73 /*default_value=*/true); | |
| 74 } | |
| 75 | |
| 76 bool IsSendingUserClassEnabled() { | |
| 77 return variations::GetVariationParamByFeatureAsBool( | |
| 78 ntp_snippets::kArticleSuggestionsFeature, kSendUserClassName, | |
| 79 /*default_value=*/false); | |
| 80 } | 69 } |
| 81 | 70 |
| 82 // Translate the BCP 47 |language_code| into a posix locale string. | 71 // Translate the BCP 47 |language_code| into a posix locale string. |
| 83 std::string PosixLocaleFromBCP47Language(const std::string& language_code) { | 72 std::string PosixLocaleFromBCP47Language(const std::string& language_code) { |
| 84 char locale[ULOC_FULLNAME_CAPACITY]; | 73 char locale[ULOC_FULLNAME_CAPACITY]; |
| 85 UErrorCode error = U_ZERO_ERROR; | 74 UErrorCode error = U_ZERO_ERROR; |
| 86 // Translate the input to a posix locale. | 75 // Translate the input to a posix locale. |
| 87 uloc_forLanguageTag(language_code.c_str(), locale, ULOC_FULLNAME_CAPACITY, | 76 uloc_forLanguageTag(language_code.c_str(), locale, ULOC_FULLNAME_CAPACITY, |
| 88 nullptr, &error); | 77 nullptr, &error); |
| 89 if (error != U_ZERO_ERROR) { | 78 if (error != U_ZERO_ERROR) { |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 } | 264 } |
| 276 | 265 |
| 277 JsonRequest::Builder& JsonRequest::Builder::SetUrlRequestContextGetter( | 266 JsonRequest::Builder& JsonRequest::Builder::SetUrlRequestContextGetter( |
| 278 const scoped_refptr<net::URLRequestContextGetter>& context_getter) { | 267 const scoped_refptr<net::URLRequestContextGetter>& context_getter) { |
| 279 url_request_context_getter_ = context_getter; | 268 url_request_context_getter_ = context_getter; |
| 280 return *this; | 269 return *this; |
| 281 } | 270 } |
| 282 | 271 |
| 283 JsonRequest::Builder& JsonRequest::Builder::SetUserClassifier( | 272 JsonRequest::Builder& JsonRequest::Builder::SetUserClassifier( |
| 284 const UserClassifier& user_classifier) { | 273 const UserClassifier& user_classifier) { |
| 285 if (IsSendingUserClassEnabled()) { | 274 if (kSendUserClassNameParam.Get()) { |
| 286 user_class_ = GetUserClassString(user_classifier.GetUserClass()); | 275 user_class_ = GetUserClassString(user_classifier.GetUserClass()); |
| 287 } | 276 } |
| 288 return *this; | 277 return *this; |
| 289 } | 278 } |
| 290 | 279 |
| 291 std::string JsonRequest::Builder::BuildHeaders() const { | 280 std::string JsonRequest::Builder::BuildHeaders() const { |
| 292 net::HttpRequestHeaders headers; | 281 net::HttpRequestHeaders headers; |
| 293 headers.SetHeader("Content-Type", "application/json; charset=UTF-8"); | 282 headers.SetHeader("Content-Type", "application/json; charset=UTF-8"); |
| 294 if (!auth_header_.empty()) { | 283 if (!auth_header_.empty()) { |
| 295 headers.SetHeader("Authorization", auth_header_); | 284 headers.SetHeader("Authorization", auth_header_); |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 Get5xxRetryCount(params_.interactive_request)); | 389 Get5xxRetryCount(params_.interactive_request)); |
| 401 return url_fetcher; | 390 return url_fetcher; |
| 402 } | 391 } |
| 403 | 392 |
| 404 void JsonRequest::Builder::PrepareLanguages( | 393 void JsonRequest::Builder::PrepareLanguages( |
| 405 language::UrlLanguageHistogram::LanguageInfo* ui_language, | 394 language::UrlLanguageHistogram::LanguageInfo* ui_language, |
| 406 language::UrlLanguageHistogram::LanguageInfo* other_top_language) const { | 395 language::UrlLanguageHistogram::LanguageInfo* other_top_language) const { |
| 407 // TODO(jkrcal): Add language model factory for iOS and add fakes to tests so | 396 // TODO(jkrcal): Add language model factory for iOS and add fakes to tests so |
| 408 // that |language_histogram| is never nullptr. Remove this check and add a | 397 // that |language_histogram| is never nullptr. Remove this check and add a |
| 409 // DCHECK into the constructor. | 398 // DCHECK into the constructor. |
| 410 if (!language_histogram_ || !IsSendingTopLanguagesEnabled()) { | 399 if (!language_histogram_ || !kSendTopLanguages.Get()) { |
| 411 return; | 400 return; |
| 412 } | 401 } |
| 413 | 402 |
| 414 // TODO(jkrcal): Is this back-and-forth converting necessary? | 403 // TODO(jkrcal): Is this back-and-forth converting necessary? |
| 415 ui_language->language_code = ISO639FromPosixLocale( | 404 ui_language->language_code = ISO639FromPosixLocale( |
| 416 PosixLocaleFromBCP47Language(params_.language_code)); | 405 PosixLocaleFromBCP47Language(params_.language_code)); |
| 417 ui_language->frequency = | 406 ui_language->frequency = |
| 418 language_histogram_->GetLanguageFrequency(ui_language->language_code); | 407 language_histogram_->GetLanguageFrequency(ui_language->language_code); |
| 419 | 408 |
| 420 std::vector<UrlLanguageHistogram::LanguageInfo> top_languages = | 409 std::vector<UrlLanguageHistogram::LanguageInfo> top_languages = |
| (...skipping 12 matching lines...) Expand all Loading... |
| 433 "NewTabPage.Languages.UILanguageRatioInTwoTopLanguages", | 422 "NewTabPage.Languages.UILanguageRatioInTwoTopLanguages", |
| 434 ratio_ui_in_both_languages * 100); | 423 ratio_ui_in_both_languages * 100); |
| 435 break; | 424 break; |
| 436 } | 425 } |
| 437 } | 426 } |
| 438 } | 427 } |
| 439 | 428 |
| 440 } // namespace internal | 429 } // namespace internal |
| 441 | 430 |
| 442 } // namespace ntp_snippets | 431 } // namespace ntp_snippets |
| OLD | NEW |