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/ntp_snippets_fetcher.h" | 5 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 7 #include <cstdlib> | 8 #include <cstdlib> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 13 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
| 14 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 15 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/metrics/sparse_histogram.h" | 17 #include "base/metrics/sparse_histogram.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 const char kChromeReaderApiScope[] = | 55 const char kChromeReaderApiScope[] = |
| 55 "https://www.googleapis.com/auth/webhistory"; | 56 "https://www.googleapis.com/auth/webhistory"; |
| 56 const char kContentSuggestionsApiScope[] = | 57 const char kContentSuggestionsApiScope[] = |
| 57 "https://www.googleapis.com/auth/chrome-content-suggestions"; | 58 "https://www.googleapis.com/auth/chrome-content-suggestions"; |
| 58 const char kSnippetsServerNonAuthorizedFormat[] = "%s?key=%s"; | 59 const char kSnippetsServerNonAuthorizedFormat[] = "%s?key=%s"; |
| 59 const char kAuthorizationRequestHeaderFormat[] = "Bearer %s"; | 60 const char kAuthorizationRequestHeaderFormat[] = "Bearer %s"; |
| 60 | 61 |
| 61 // Variation parameter for personalizing fetching of snippets. | 62 // Variation parameter for personalizing fetching of snippets. |
| 62 const char kPersonalizationName[] = "fetching_personalization"; | 63 const char kPersonalizationName[] = "fetching_personalization"; |
| 63 | 64 |
| 65 // Variation parameter for disabling the retry . | |
|
Marc Treib
2016/12/08 10:52:06
nitty nit: no space before "."
fhorschig
2016/12/09 12:37:54
Oh no, a Plenk!
(https://en.wikipedia.org/wiki/Pl
Marc Treib
2016/12/09 12:59:24
Haha, I didn't know that term!
I have to admit tho
fhorschig
2016/12/12 09:54:03
You are not alone ;)
| |
| 66 const char kNumberOf5xxRetries[] = "number_of_non_interactive_5xx_retries"; | |
|
Marc Treib
2016/12/08 10:52:06
nit1: Rename the variable to make clear it's a par
fhorschig
2016/12/09 12:37:54
Done. Done.
| |
| 67 | |
| 64 // Variation parameter for chrome-content-suggestions backend. | 68 // Variation parameter for chrome-content-suggestions backend. |
| 65 const char kContentSuggestionsBackend[] = "content_suggestions_backend"; | 69 const char kContentSuggestionsBackend[] = "content_suggestions_backend"; |
| 66 | 70 |
| 67 // Constants for possible values of the "fetching_personalization" parameter. | 71 // Constants for possible values of the "fetching_personalization" parameter. |
| 68 const char kPersonalizationPersonalString[] = "personal"; | 72 const char kPersonalizationPersonalString[] = "personal"; |
| 69 const char kPersonalizationNonPersonalString[] = "non_personal"; | 73 const char kPersonalizationNonPersonalString[] = "non_personal"; |
| 70 const char kPersonalizationBothString[] = "both"; // the default value | 74 const char kPersonalizationBothString[] = "both"; // the default value |
| 71 | 75 |
| 72 const int kMaxExcludedIds = 100; | 76 const int kMaxExcludedIds = 100; |
| 73 | 77 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 if (param_value == kBooleanParameterDisabled) { | 129 if (param_value == kBooleanParameterDisabled) { |
| 126 return false; | 130 return false; |
| 127 } | 131 } |
| 128 if (!param_value.empty()) { | 132 if (!param_value.empty()) { |
| 129 LOG(WARNING) << "Invalid value \"" << param_value | 133 LOG(WARNING) << "Invalid value \"" << param_value |
| 130 << "\" for variation parameter " << param_name; | 134 << "\" for variation parameter " << param_name; |
| 131 } | 135 } |
| 132 return default_value; | 136 return default_value; |
| 133 } | 137 } |
| 134 | 138 |
| 139 int GetNumberOfNonInteractiveRetries() { | |
| 140 std::string retries = variations::GetVariationParamValue( | |
| 141 ntp_snippets::kStudyName, kNumberOf5xxRetries); | |
| 142 return retries.empty() ? 0 : std::max(0, std::stoi(retries)); | |
|
Marc Treib
2016/12/08 10:52:06
Use GetParamAsInt from ntp_snippets/features.h.
It
fhorschig
2016/12/09 12:37:54
Done.
Marc Treib
2016/12/09 12:59:24
Heads-up: As of https://codereview.chromium.org/25
fhorschig
2016/12/12 09:54:03
Done.
| |
| 143 } | |
| 144 | |
| 135 bool UsesChromeContentSuggestionsAPI(const GURL& endpoint) { | 145 bool UsesChromeContentSuggestionsAPI(const GURL& endpoint) { |
| 136 if (endpoint == kChromeReaderServer) { | 146 if (endpoint == kChromeReaderServer) { |
| 137 return false; | 147 return false; |
| 138 } | 148 } |
| 139 | 149 |
| 140 if (endpoint != kContentSuggestionsServer && | 150 if (endpoint != kContentSuggestionsServer && |
| 141 endpoint != kContentSuggestionsStagingServer && | 151 endpoint != kContentSuggestionsStagingServer && |
| 142 endpoint != kContentSuggestionsAlphaServer) { | 152 endpoint != kContentSuggestionsAlphaServer) { |
| 143 LOG(WARNING) << "Unknown value for " << kContentSuggestionsBackend << ": " | 153 LOG(WARNING) << "Unknown value for " << kContentSuggestionsBackend << ": " |
| 144 << "assuming chromecontentsuggestions-style API"; | 154 << "assuming chromecontentsuggestions-style API"; |
| (...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 805 url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 815 url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 806 net::LOAD_DO_NOT_SAVE_COOKIES); | 816 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 807 data_use_measurement::DataUseUserData::AttachToFetcher( | 817 data_use_measurement::DataUseUserData::AttachToFetcher( |
| 808 url_fetcher.get(), data_use_measurement::DataUseUserData::NTP_SNIPPETS); | 818 url_fetcher.get(), data_use_measurement::DataUseUserData::NTP_SNIPPETS); |
| 809 | 819 |
| 810 url_fetcher->SetExtraRequestHeaders(headers); | 820 url_fetcher->SetExtraRequestHeaders(headers); |
| 811 url_fetcher->SetUploadData("application/json", body); | 821 url_fetcher->SetUploadData("application/json", body); |
| 812 | 822 |
| 813 // Fetchers are sometimes cancelled because a network change was detected. | 823 // Fetchers are sometimes cancelled because a network change was detected. |
| 814 url_fetcher->SetAutomaticallyRetryOnNetworkChanges(3); | 824 url_fetcher->SetAutomaticallyRetryOnNetworkChanges(3); |
| 815 // Try to make fetching the files bit more robust even with poor connection. | 825 if (params_.interactive_request) { |
| 816 url_fetcher->SetMaxRetriesOn5xx(3); | 826 // Try to make fetching the files bit more robust even with poor connection. |
| 827 url_fetcher->SetMaxRetriesOn5xx(2); | |
| 828 } else { | |
| 829 // Don't retry immediately if a scheduled fetch fails. | |
|
Marc Treib
2016/12/08 10:52:06
Comment is now not accurate anymore.
fhorschig
2016/12/09 12:37:54
Done.
| |
| 830 url_fetcher->SetMaxRetriesOn5xx(GetNumberOfNonInteractiveRetries()); | |
|
Marc Treib
2016/12/08 10:52:06
Make a "GetNumberOf5xxRetries(bool interactive)" h
fhorschig
2016/12/09 12:37:54
Done.
| |
| 831 } | |
| 817 return url_fetcher; | 832 return url_fetcher; |
| 818 } | 833 } |
| 819 | 834 |
| 820 void NTPSnippetsFetcher::RequestBuilder::PrepareLanguages( | 835 void NTPSnippetsFetcher::RequestBuilder::PrepareLanguages( |
| 821 translate::LanguageModel::LanguageInfo* ui_language, | 836 translate::LanguageModel::LanguageInfo* ui_language, |
| 822 translate::LanguageModel::LanguageInfo* other_top_language) const { | 837 translate::LanguageModel::LanguageInfo* other_top_language) const { |
| 823 // TODO(jkrcal): Add language model factory for iOS and add fakes to tests so | 838 // TODO(jkrcal): Add language model factory for iOS and add fakes to tests so |
| 824 // that |language_model| is never nullptr. Remove this check and add a DCHECK | 839 // that |language_model| is never nullptr. Remove this check and add a DCHECK |
| 825 // into the constructor. | 840 // into the constructor. |
| 826 if (!language_model_ || !IsSendingTopLanguagesEnabled()) { | 841 if (!language_model_ || !IsSendingTopLanguagesEnabled()) { |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1097 NOTREACHED(); | 1112 NOTREACHED(); |
| 1098 return false; | 1113 return false; |
| 1099 } | 1114 } |
| 1100 | 1115 |
| 1101 bool NTPSnippetsFetcher::NeedsAuthentication() const { | 1116 bool NTPSnippetsFetcher::NeedsAuthentication() const { |
| 1102 return (personalization_ == Personalization::kPersonal || | 1117 return (personalization_ == Personalization::kPersonal || |
| 1103 personalization_ == Personalization::kBoth); | 1118 personalization_ == Personalization::kBoth); |
| 1104 } | 1119 } |
| 1105 | 1120 |
| 1106 } // namespace ntp_snippets | 1121 } // namespace ntp_snippets |
| OLD | NEW |