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