| 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. | 
|  | 66 const char kBackground5xxRetriesName[] = "background_5xx_retries_count"; | 
|  | 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 Get5xxRetryCount(bool interactive_request) { | 
|  | 140   if (interactive_request) { | 
|  | 141     return 2; | 
|  | 142   } | 
|  | 143   return std::max(0, variations::GetVariationParamByFeatureAsInt( | 
|  | 144                          ntp_snippets::kArticleSuggestionsFeature, | 
|  | 145                          kBackground5xxRetriesName, 0)); | 
|  | 146 } | 
|  | 147 | 
| 135 bool UsesChromeContentSuggestionsAPI(const GURL& endpoint) { | 148 bool UsesChromeContentSuggestionsAPI(const GURL& endpoint) { | 
| 136   if (endpoint == kChromeReaderServer) { | 149   if (endpoint == kChromeReaderServer) { | 
| 137     return false; | 150     return false; | 
| 138   } | 151   } | 
| 139 | 152 | 
| 140   if (endpoint != kContentSuggestionsServer && | 153   if (endpoint != kContentSuggestionsServer && | 
| 141       endpoint != kContentSuggestionsStagingServer && | 154       endpoint != kContentSuggestionsStagingServer && | 
| 142       endpoint != kContentSuggestionsAlphaServer) { | 155       endpoint != kContentSuggestionsAlphaServer) { | 
| 143     LOG(WARNING) << "Unknown value for " << kContentSuggestionsBackend << ": " | 156     LOG(WARNING) << "Unknown value for " << kContentSuggestionsBackend << ": " | 
| 144                  << "assuming chromecontentsuggestions-style API"; | 157                  << "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 | | 818   url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 
| 806                             net::LOAD_DO_NOT_SAVE_COOKIES); | 819                             net::LOAD_DO_NOT_SAVE_COOKIES); | 
| 807   data_use_measurement::DataUseUserData::AttachToFetcher( | 820   data_use_measurement::DataUseUserData::AttachToFetcher( | 
| 808       url_fetcher.get(), data_use_measurement::DataUseUserData::NTP_SNIPPETS); | 821       url_fetcher.get(), data_use_measurement::DataUseUserData::NTP_SNIPPETS); | 
| 809 | 822 | 
| 810   url_fetcher->SetExtraRequestHeaders(headers); | 823   url_fetcher->SetExtraRequestHeaders(headers); | 
| 811   url_fetcher->SetUploadData("application/json", body); | 824   url_fetcher->SetUploadData("application/json", body); | 
| 812 | 825 | 
| 813   // Fetchers are sometimes cancelled because a network change was detected. | 826   // Fetchers are sometimes cancelled because a network change was detected. | 
| 814   url_fetcher->SetAutomaticallyRetryOnNetworkChanges(3); | 827   url_fetcher->SetAutomaticallyRetryOnNetworkChanges(3); | 
| 815   // Try to make fetching the files bit more robust even with poor connection. | 828   url_fetcher->SetMaxRetriesOn5xx( | 
| 816   url_fetcher->SetMaxRetriesOn5xx(3); | 829       Get5xxRetryCount(params_.interactive_request)); | 
| 817   return url_fetcher; | 830   return url_fetcher; | 
| 818 } | 831 } | 
| 819 | 832 | 
| 820 void NTPSnippetsFetcher::RequestBuilder::PrepareLanguages( | 833 void NTPSnippetsFetcher::RequestBuilder::PrepareLanguages( | 
| 821     translate::LanguageModel::LanguageInfo* ui_language, | 834     translate::LanguageModel::LanguageInfo* ui_language, | 
| 822     translate::LanguageModel::LanguageInfo* other_top_language) const { | 835     translate::LanguageModel::LanguageInfo* other_top_language) const { | 
| 823   // TODO(jkrcal): Add language model factory for iOS and add fakes to tests so | 836   // 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 | 837   // that |language_model| is never nullptr. Remove this check and add a DCHECK | 
| 825   // into the constructor. | 838   // into the constructor. | 
| 826   if (!language_model_ || !IsSendingTopLanguagesEnabled()) { | 839   if (!language_model_ || !IsSendingTopLanguagesEnabled()) { | 
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1097   NOTREACHED(); | 1110   NOTREACHED(); | 
| 1098   return false; | 1111   return false; | 
| 1099 } | 1112 } | 
| 1100 | 1113 | 
| 1101 bool NTPSnippetsFetcher::NeedsAuthentication() const { | 1114 bool NTPSnippetsFetcher::NeedsAuthentication() const { | 
| 1102   return (personalization_ == Personalization::kPersonal || | 1115   return (personalization_ == Personalization::kPersonal || | 
| 1103           personalization_ == Personalization::kBoth); | 1116           personalization_ == Personalization::kBoth); | 
| 1104 } | 1117 } | 
| 1105 | 1118 | 
| 1106 }  // namespace ntp_snippets | 1119 }  // namespace ntp_snippets | 
| OLD | NEW | 
|---|