fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 5 | #include "components/ntp_snippets/remote/json_request.h" |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 6 | |
| 7 | #include <algorithm> |
| 8 | #include <utility> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "base/command_line.h" |
| 12 | #include "base/json/json_writer.h" |
Ilya Sherman | 1edb6f18 | 2017-12-12 04:00:42 | [diff] [blame] | 13 | #include "base/metrics/histogram_functions.h" |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 14 | #include "base/metrics/histogram_macros.h" |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 15 | #include "base/strings/stringprintf.h" |
markusheintz | 05b1e88 | 2017-02-15 14:38:19 | [diff] [blame] | 16 | #include "base/time/clock.h" |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 17 | #include "base/values.h" |
| 18 | #include "components/data_use_measurement/core/data_use_user_data.h" |
| 19 | #include "components/ntp_snippets/category_info.h" |
| 20 | #include "components/ntp_snippets/features.h" |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 21 | #include "components/ntp_snippets/remote/request_params.h" |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 22 | #include "components/ntp_snippets/user_classifier.h" |
thakis | fe8fa0a | 2017-02-23 19:46:36 | [diff] [blame] | 23 | #include "components/strings/grit/components_strings.h" |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 24 | #include "components/variations/net/variations_http_headers.h" |
| 25 | #include "components/variations/variations_associated_data.h" |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 26 | #include "net/base/load_flags.h" |
| 27 | #include "net/http/http_response_headers.h" |
| 28 | #include "net/http/http_status_code.h" |
rhalavati | c0ea6f04 | 2017-02-27 15:58:06 | [diff] [blame] | 29 | #include "net/traffic_annotation/network_traffic_annotation.h" |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 30 | #include "net/url_request/url_fetcher.h" |
| 31 | #include "net/url_request/url_request_context_getter.h" |
| 32 | #include "third_party/icu/source/common/unicode/uloc.h" |
| 33 | #include "third_party/icu/source/common/unicode/utypes.h" |
| 34 | #include "ui/base/l10n/l10n_util.h" |
| 35 | |
Michael Martis | 98cd873 | 2017-07-14 03:26:19 | [diff] [blame] | 36 | using language::UrlLanguageHistogram; |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 37 | using net::URLFetcher; |
| 38 | using net::URLRequestContextGetter; |
| 39 | using net::HttpRequestHeaders; |
| 40 | using net::URLRequestStatus; |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 41 | |
| 42 | namespace ntp_snippets { |
| 43 | |
| 44 | namespace internal { |
| 45 | |
| 46 | namespace { |
| 47 | |
| 48 | // Variation parameter for disabling the retry. |
| 49 | const char kBackground5xxRetriesName[] = "background_5xx_retries_count"; |
| 50 | |
Michael Martis | 98cd873 | 2017-07-14 03:26:19 | [diff] [blame] | 51 | // Variation parameter for sending UrlLanguageHistogram info to the server. |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 52 | const char kSendTopLanguagesName[] = "send_top_languages"; |
| 53 | |
| 54 | // Variation parameter for sending UserClassifier info to the server. |
| 55 | const char kSendUserClassName[] = "send_user_class"; |
| 56 | |
| 57 | int Get5xxRetryCount(bool interactive_request) { |
| 58 | if (interactive_request) { |
| 59 | return 2; |
| 60 | } |
| 61 | return std::max(0, variations::GetVariationParamByFeatureAsInt( |
| 62 | ntp_snippets::kArticleSuggestionsFeature, |
| 63 | kBackground5xxRetriesName, 0)); |
| 64 | } |
| 65 | |
| 66 | bool IsSendingTopLanguagesEnabled() { |
| 67 | return variations::GetVariationParamByFeatureAsBool( |
| 68 | ntp_snippets::kArticleSuggestionsFeature, kSendTopLanguagesName, |
jkrcal | d1625e2 | 2017-01-18 16:54:38 | [diff] [blame] | 69 | /*default_value=*/true); |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | bool IsSendingUserClassEnabled() { |
| 73 | return variations::GetVariationParamByFeatureAsBool( |
| 74 | ntp_snippets::kArticleSuggestionsFeature, kSendUserClassName, |
Tim Schumann | 7e460298 | 2017-08-28 13:34:53 | [diff] [blame] | 75 | /*default_value=*/true); |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // Translate the BCP 47 |language_code| into a posix locale string. |
| 79 | std::string PosixLocaleFromBCP47Language(const std::string& language_code) { |
| 80 | char locale[ULOC_FULLNAME_CAPACITY]; |
| 81 | UErrorCode error = U_ZERO_ERROR; |
| 82 | // Translate the input to a posix locale. |
| 83 | uloc_forLanguageTag(language_code.c_str(), locale, ULOC_FULLNAME_CAPACITY, |
| 84 | nullptr, &error); |
| 85 | if (error != U_ZERO_ERROR) { |
| 86 | DLOG(WARNING) << "Error in translating language code to a locale string: " |
| 87 | << error; |
| 88 | return std::string(); |
| 89 | } |
| 90 | return locale; |
| 91 | } |
| 92 | |
| 93 | std::string ISO639FromPosixLocale(const std::string& locale) { |
| 94 | char language[ULOC_LANG_CAPACITY]; |
| 95 | UErrorCode error = U_ZERO_ERROR; |
| 96 | uloc_getLanguage(locale.c_str(), language, ULOC_LANG_CAPACITY, &error); |
| 97 | if (error != U_ZERO_ERROR) { |
| 98 | DLOG(WARNING) |
| 99 | << "Error in translating locale string to a ISO639 language code: " |
| 100 | << error; |
| 101 | return std::string(); |
| 102 | } |
| 103 | return language; |
| 104 | } |
| 105 | |
| 106 | void AppendLanguageInfoToList(base::ListValue* list, |
Michael Martis | 98cd873 | 2017-07-14 03:26:19 | [diff] [blame] | 107 | const UrlLanguageHistogram::LanguageInfo& info) { |
Jinho Bang | edffb4ee | 2018-01-02 15:38:30 | [diff] [blame] | 108 | auto lang = std::make_unique<base::DictionaryValue>(); |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 109 | lang->SetString("language", info.language_code); |
| 110 | lang->SetDouble("frequency", info.frequency); |
| 111 | list->Append(std::move(lang)); |
| 112 | } |
| 113 | |
| 114 | std::string GetUserClassString(UserClassifier::UserClass user_class) { |
| 115 | switch (user_class) { |
| 116 | case UserClassifier::UserClass::RARE_NTP_USER: |
| 117 | return "RARE_NTP_USER"; |
| 118 | case UserClassifier::UserClass::ACTIVE_NTP_USER: |
| 119 | return "ACTIVE_NTP_USER"; |
| 120 | case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: |
| 121 | return "ACTIVE_SUGGESTIONS_CONSUMER"; |
| 122 | } |
| 123 | NOTREACHED(); |
| 124 | return std::string(); |
| 125 | } |
| 126 | |
| 127 | } // namespace |
| 128 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 129 | JsonRequest::JsonRequest( |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 130 | base::Optional<Category> exclusive_category, |
markusheintz | 05b1e88 | 2017-02-15 14:38:19 | [diff] [blame] | 131 | base::Clock* clock, // Needed until destruction of the request. |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 132 | const ParseJSONCallback& callback) |
| 133 | : exclusive_category_(exclusive_category), |
markusheintz | 05b1e88 | 2017-02-15 14:38:19 | [diff] [blame] | 134 | clock_(clock), |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 135 | parse_json_callback_(callback), |
| 136 | weak_ptr_factory_(this) { |
markusheintz | 05b1e88 | 2017-02-15 14:38:19 | [diff] [blame] | 137 | creation_time_ = clock_->Now(); |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 138 | } |
| 139 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 140 | JsonRequest::~JsonRequest() { |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 141 | LOG_IF(DFATAL, !request_completed_callback_.is_null()) |
| 142 | << "The CompletionCallback was never called!"; |
| 143 | } |
| 144 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 145 | void JsonRequest::Start(CompletedCallback callback) { |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 146 | request_completed_callback_ = std::move(callback); |
| 147 | url_fetcher_->Start(); |
| 148 | } |
| 149 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 150 | base::TimeDelta JsonRequest::GetFetchDuration() const { |
markusheintz | 05b1e88 | 2017-02-15 14:38:19 | [diff] [blame] | 151 | return clock_->Now() - creation_time_; |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 152 | } |
| 153 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 154 | std::string JsonRequest::GetResponseString() const { |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 155 | std::string response; |
| 156 | url_fetcher_->GetResponseAsString(&response); |
| 157 | return response; |
| 158 | } |
| 159 | |
| 160 | //////////////////////////////////////////////////////////////////////////////// |
| 161 | // URLFetcherDelegate overrides |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 162 | void JsonRequest::OnURLFetchComplete(const net::URLFetcher* source) { |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 163 | DCHECK_EQ(url_fetcher_.get(), source); |
| 164 | const URLRequestStatus& status = url_fetcher_->GetStatus(); |
| 165 | int response = url_fetcher_->GetResponseCode(); |
Ilya Sherman | 1edb6f18 | 2017-12-12 04:00:42 | [diff] [blame] | 166 | base::UmaHistogramSparse("NewTabPage.Snippets.FetchHttpResponseOrErrorCode", |
| 167 | status.is_success() ? response : status.error()); |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 168 | |
| 169 | if (!status.is_success()) { |
| 170 | std::move(request_completed_callback_) |
| 171 | .Run(/*result=*/nullptr, FetchResult::URL_REQUEST_STATUS_ERROR, |
| 172 | /*error_details=*/base::StringPrintf(" %d", status.error())); |
| 173 | } else if (response != net::HTTP_OK) { |
| 174 | // TODO(jkrcal): https://crbug.com/609084 |
| 175 | // We need to deal with the edge case again where the auth |
| 176 | // token expires just before we send the request (in which case we need to |
| 177 | // fetch a new auth token). We should extract that into a common class |
| 178 | // instead of adding it to every single class that uses auth tokens. |
| 179 | std::move(request_completed_callback_) |
| 180 | .Run(/*result=*/nullptr, FetchResult::HTTP_ERROR, |
| 181 | /*error_details=*/base::StringPrintf(" %d", response)); |
| 182 | } else { |
| 183 | ParseJsonResponse(); |
| 184 | } |
| 185 | } |
| 186 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 187 | void JsonRequest::ParseJsonResponse() { |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 188 | std::string json_string; |
| 189 | bool stores_result_to_string = |
| 190 | url_fetcher_->GetResponseAsString(&json_string); |
| 191 | DCHECK(stores_result_to_string); |
| 192 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 193 | parse_json_callback_.Run( |
| 194 | json_string, |
| 195 | base::Bind(&JsonRequest::OnJsonParsed, weak_ptr_factory_.GetWeakPtr()), |
| 196 | base::Bind(&JsonRequest::OnJsonError, weak_ptr_factory_.GetWeakPtr())); |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 197 | } |
| 198 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 199 | void JsonRequest::OnJsonParsed(std::unique_ptr<base::Value> result) { |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 200 | std::move(request_completed_callback_) |
| 201 | .Run(std::move(result), FetchResult::SUCCESS, |
| 202 | /*error_details=*/std::string()); |
| 203 | } |
| 204 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 205 | void JsonRequest::OnJsonError(const std::string& error) { |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 206 | std::string json_string; |
| 207 | url_fetcher_->GetResponseAsString(&json_string); |
| 208 | LOG(WARNING) << "Received invalid JSON (" << error << "): " << json_string; |
| 209 | std::move(request_completed_callback_) |
| 210 | .Run(/*result=*/nullptr, FetchResult::JSON_PARSE_ERROR, |
| 211 | /*error_details=*/base::StringPrintf(" (error %s)", error.c_str())); |
| 212 | } |
| 213 | |
Michael Martis | 98cd873 | 2017-07-14 03:26:19 | [diff] [blame] | 214 | JsonRequest::Builder::Builder() : language_histogram_(nullptr) {} |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 215 | JsonRequest::Builder::Builder(JsonRequest::Builder&&) = default; |
| 216 | JsonRequest::Builder::~Builder() = default; |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 217 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 218 | std::unique_ptr<JsonRequest> JsonRequest::Builder::Build() const { |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 219 | DCHECK(!url_.is_empty()); |
| 220 | DCHECK(url_request_context_getter_); |
markusheintz | 05b1e88 | 2017-02-15 14:38:19 | [diff] [blame] | 221 | DCHECK(clock_); |
Jinho Bang | edffb4ee | 2018-01-02 15:38:30 | [diff] [blame] | 222 | auto request = std::make_unique<JsonRequest>(params_.exclusive_category, |
markusheintz | 05b1e88 | 2017-02-15 14:38:19 | [diff] [blame] | 223 | clock_, parse_json_callback_); |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 224 | std::string body = BuildBody(); |
| 225 | std::string headers = BuildHeaders(); |
| 226 | request->url_fetcher_ = BuildURLFetcher(request.get(), headers, body); |
| 227 | |
| 228 | // Log the request for debugging network issues. |
| 229 | VLOG(1) << "Sending a NTP snippets request to " << url_ << ":\n" |
| 230 | << headers << "\n" |
| 231 | << body; |
| 232 | |
| 233 | return request; |
| 234 | } |
| 235 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 236 | JsonRequest::Builder& JsonRequest::Builder::SetAuthentication( |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 237 | const std::string& account_id, |
| 238 | const std::string& auth_header) { |
| 239 | obfuscated_gaia_id_ = account_id; |
| 240 | auth_header_ = auth_header; |
| 241 | return *this; |
| 242 | } |
| 243 | |
Michael Martis | 98cd873 | 2017-07-14 03:26:19 | [diff] [blame] | 244 | JsonRequest::Builder& JsonRequest::Builder::SetLanguageHistogram( |
| 245 | const language::UrlLanguageHistogram* language_histogram) { |
| 246 | language_histogram_ = language_histogram; |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 247 | return *this; |
| 248 | } |
| 249 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 250 | JsonRequest::Builder& JsonRequest::Builder::SetParams( |
| 251 | const RequestParams& params) { |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 252 | params_ = params; |
| 253 | return *this; |
| 254 | } |
| 255 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 256 | JsonRequest::Builder& JsonRequest::Builder::SetParseJsonCallback( |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 257 | ParseJSONCallback callback) { |
| 258 | parse_json_callback_ = callback; |
| 259 | return *this; |
| 260 | } |
| 261 | |
markusheintz | 05b1e88 | 2017-02-15 14:38:19 | [diff] [blame] | 262 | JsonRequest::Builder& JsonRequest::Builder::SetClock(base::Clock* clock) { |
| 263 | clock_ = clock; |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 264 | return *this; |
| 265 | } |
| 266 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 267 | JsonRequest::Builder& JsonRequest::Builder::SetUrl(const GURL& url) { |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 268 | url_ = url; |
| 269 | return *this; |
| 270 | } |
| 271 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 272 | JsonRequest::Builder& JsonRequest::Builder::SetUrlRequestContextGetter( |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 273 | const scoped_refptr<net::URLRequestContextGetter>& context_getter) { |
| 274 | url_request_context_getter_ = context_getter; |
| 275 | return *this; |
| 276 | } |
| 277 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 278 | JsonRequest::Builder& JsonRequest::Builder::SetUserClassifier( |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 279 | const UserClassifier& user_classifier) { |
| 280 | if (IsSendingUserClassEnabled()) { |
| 281 | user_class_ = GetUserClassString(user_classifier.GetUserClass()); |
| 282 | } |
| 283 | return *this; |
| 284 | } |
| 285 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 286 | std::string JsonRequest::Builder::BuildHeaders() const { |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 287 | net::HttpRequestHeaders headers; |
| 288 | headers.SetHeader("Content-Type", "application/json; charset=UTF-8"); |
| 289 | if (!auth_header_.empty()) { |
| 290 | headers.SetHeader("Authorization", auth_header_); |
| 291 | } |
| 292 | // Add X-Client-Data header with experiment IDs from field trials. |
Mark Pearson | cea91cf | 2017-12-13 20:45:58 | [diff] [blame] | 293 | // Note: It's OK to pass SignedIn::kNo if it's unknown, as it does not affect |
| 294 | // transmission of experiments coming from the variations server. |
| 295 | variations::AppendVariationHeaders(url_, variations::InIncognito::kNo, |
| 296 | variations::SignedIn::kNo, &headers); |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 297 | return headers.ToString(); |
| 298 | } |
| 299 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 300 | std::string JsonRequest::Builder::BuildBody() const { |
Jinho Bang | edffb4ee | 2018-01-02 15:38:30 | [diff] [blame] | 301 | auto request = std::make_unique<base::DictionaryValue>(); |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 302 | std::string user_locale = PosixLocaleFromBCP47Language(params_.language_code); |
treib | a57f51e | 2017-03-23 14:47:52 | [diff] [blame] | 303 | if (!user_locale.empty()) { |
| 304 | request->SetString("uiLanguage", user_locale); |
| 305 | } |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 306 | |
treib | a57f51e | 2017-03-23 14:47:52 | [diff] [blame] | 307 | request->SetString("priority", params_.interactive_request |
| 308 | ? "USER_ACTION" |
| 309 | : "BACKGROUND_PREFETCH"); |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 310 | |
Jinho Bang | edffb4ee | 2018-01-02 15:38:30 | [diff] [blame] | 311 | auto excluded = std::make_unique<base::ListValue>(); |
treib | a57f51e | 2017-03-23 14:47:52 | [diff] [blame] | 312 | for (const auto& id : params_.excluded_ids) { |
| 313 | excluded->AppendString(id); |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 314 | } |
treib | a57f51e | 2017-03-23 14:47:52 | [diff] [blame] | 315 | request->Set("excludedSuggestionIds", std::move(excluded)); |
| 316 | |
| 317 | if (!user_class_.empty()) { |
| 318 | request->SetString("userActivenessClass", user_class_); |
| 319 | } |
| 320 | |
Michael Martis | 98cd873 | 2017-07-14 03:26:19 | [diff] [blame] | 321 | language::UrlLanguageHistogram::LanguageInfo ui_language; |
| 322 | language::UrlLanguageHistogram::LanguageInfo other_top_language; |
treib | a57f51e | 2017-03-23 14:47:52 | [diff] [blame] | 323 | PrepareLanguages(&ui_language, &other_top_language); |
| 324 | if (ui_language.frequency != 0 || other_top_language.frequency != 0) { |
Jinho Bang | edffb4ee | 2018-01-02 15:38:30 | [diff] [blame] | 325 | auto language_list = std::make_unique<base::ListValue>(); |
treib | a57f51e | 2017-03-23 14:47:52 | [diff] [blame] | 326 | if (ui_language.frequency > 0) { |
| 327 | AppendLanguageInfoToList(language_list.get(), ui_language); |
| 328 | } |
| 329 | if (other_top_language.frequency > 0) { |
| 330 | AppendLanguageInfoToList(language_list.get(), other_top_language); |
| 331 | } |
| 332 | request->Set("topLanguages", std::move(language_list)); |
| 333 | } |
| 334 | |
Vitalii Iarko | 3fdb9f91 | 2017-09-21 08:28:25 | [diff] [blame] | 335 | // TODO(vitaliii): Support count_to_fetch without requiring |
| 336 | // |exclusive_category|. |
| 337 | if (params_.exclusive_category.has_value()) { |
| 338 | base::DictionaryValue exclusive_category_parameters; |
| 339 | exclusive_category_parameters.SetInteger( |
| 340 | "id", params_.exclusive_category->remote_id()); |
| 341 | exclusive_category_parameters.SetInteger("numSuggestions", |
| 342 | params_.count_to_fetch); |
| 343 | base::ListValue category_parameters; |
| 344 | category_parameters.GetList().push_back( |
| 345 | std::move(exclusive_category_parameters)); |
| 346 | request->SetKey("categoryParameters", std::move(category_parameters)); |
| 347 | } |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 348 | |
| 349 | std::string request_json; |
| 350 | bool success = base::JSONWriter::WriteWithOptions( |
| 351 | *request, base::JSONWriter::OPTIONS_PRETTY_PRINT, &request_json); |
| 352 | DCHECK(success); |
| 353 | return request_json; |
| 354 | } |
| 355 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 356 | std::unique_ptr<net::URLFetcher> JsonRequest::Builder::BuildURLFetcher( |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 357 | net::URLFetcherDelegate* delegate, |
| 358 | const std::string& headers, |
| 359 | const std::string& body) const { |
rhalavati | c0ea6f04 | 2017-02-27 15:58:06 | [diff] [blame] | 360 | net::NetworkTrafficAnnotationTag traffic_annotation = |
| 361 | net::DefineNetworkTrafficAnnotation("ntp_snippets_fetch", R"( |
| 362 | semantics { |
| 363 | sender: "New Tab Page Content Suggestions Fetch" |
| 364 | description: |
| 365 | "Chromium can show content suggestions (e.g. news articles) on the " |
| 366 | "New Tab page. For signed-in users, these may be personalized " |
| 367 | "based on the user's synced browsing history." |
| 368 | trigger: |
| 369 | "Triggered periodically in the background, or upon explicit user " |
| 370 | "request." |
| 371 | data: |
| 372 | "The Chromium UI language, as well as a second language the user " |
Michael Martis | 98cd873 | 2017-07-14 03:26:19 | [diff] [blame] | 373 | "understands, based on language::UrlLanguageHistogram. For " |
| 374 | "signed-in users, the requests is authenticated." |
rhalavati | c0ea6f04 | 2017-02-27 15:58:06 | [diff] [blame] | 375 | destination: GOOGLE_OWNED_SERVICE |
| 376 | } |
| 377 | policy { |
Ramin Halavati | 3b97978 | 2017-07-21 11:40:26 | [diff] [blame] | 378 | cookies_allowed: NO |
rhalavati | c0ea6f04 | 2017-02-27 15:58:06 | [diff] [blame] | 379 | setting: |
| 380 | "This feature cannot be disabled by settings now (but is requested " |
| 381 | "to be implemented in crbug.com/695129)." |
rhalavati | eaa64e9 | 2017-04-03 09:36:43 | [diff] [blame] | 382 | chrome_policy { |
rhalavati | c0ea6f04 | 2017-02-27 15:58:06 | [diff] [blame] | 383 | NTPContentSuggestionsEnabled { |
| 384 | policy_options {mode: MANDATORY} |
rhalavati | eaa64e9 | 2017-04-03 09:36:43 | [diff] [blame] | 385 | NTPContentSuggestionsEnabled: false |
rhalavati | c0ea6f04 | 2017-02-27 15:58:06 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | })"); |
| 389 | std::unique_ptr<net::URLFetcher> url_fetcher = net::URLFetcher::Create( |
| 390 | url_, net::URLFetcher::POST, delegate, traffic_annotation); |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 391 | url_fetcher->SetRequestContext(url_request_context_getter_.get()); |
| 392 | url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 393 | net::LOAD_DO_NOT_SAVE_COOKIES); |
| 394 | data_use_measurement::DataUseUserData::AttachToFetcher( |
jkrcal | cd18da6 | 2017-02-10 07:53:50 | [diff] [blame] | 395 | url_fetcher.get(), |
| 396 | data_use_measurement::DataUseUserData::NTP_SNIPPETS_SUGGESTIONS); |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 397 | |
| 398 | url_fetcher->SetExtraRequestHeaders(headers); |
| 399 | url_fetcher->SetUploadData("application/json", body); |
| 400 | |
| 401 | // Fetchers are sometimes cancelled because a network change was detected. |
| 402 | url_fetcher->SetAutomaticallyRetryOnNetworkChanges(3); |
| 403 | url_fetcher->SetMaxRetriesOn5xx( |
| 404 | Get5xxRetryCount(params_.interactive_request)); |
| 405 | return url_fetcher; |
| 406 | } |
| 407 | |
treib | 9de525a | 2017-01-19 12:20:37 | [diff] [blame] | 408 | void JsonRequest::Builder::PrepareLanguages( |
Michael Martis | 98cd873 | 2017-07-14 03:26:19 | [diff] [blame] | 409 | language::UrlLanguageHistogram::LanguageInfo* ui_language, |
| 410 | language::UrlLanguageHistogram::LanguageInfo* other_top_language) const { |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 411 | // TODO(jkrcal): Add language model factory for iOS and add fakes to tests so |
Michael Martis | 98cd873 | 2017-07-14 03:26:19 | [diff] [blame] | 412 | // that |language_histogram| is never nullptr. Remove this check and add a |
| 413 | // DCHECK into the constructor. |
| 414 | if (!language_histogram_ || !IsSendingTopLanguagesEnabled()) { |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 415 | return; |
| 416 | } |
| 417 | |
| 418 | // TODO(jkrcal): Is this back-and-forth converting necessary? |
| 419 | ui_language->language_code = ISO639FromPosixLocale( |
| 420 | PosixLocaleFromBCP47Language(params_.language_code)); |
| 421 | ui_language->frequency = |
Michael Martis | 98cd873 | 2017-07-14 03:26:19 | [diff] [blame] | 422 | language_histogram_->GetLanguageFrequency(ui_language->language_code); |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 423 | |
Michael Martis | 98cd873 | 2017-07-14 03:26:19 | [diff] [blame] | 424 | std::vector<UrlLanguageHistogram::LanguageInfo> top_languages = |
| 425 | language_histogram_->GetTopLanguages(); |
| 426 | for (const UrlLanguageHistogram::LanguageInfo& info : top_languages) { |
fhorschig | cb5d7fc0 | 2016-12-20 13:14:55 | [diff] [blame] | 427 | if (info.language_code != ui_language->language_code) { |
| 428 | *other_top_language = info; |
| 429 | |
| 430 | // Report to UMA how important the UI language is. |
| 431 | DCHECK_GT(other_top_language->frequency, 0) |
| 432 | << "GetTopLanguages() should not return languages with 0 frequency"; |
| 433 | float ratio_ui_in_both_languages = |
| 434 | ui_language->frequency / |
| 435 | (ui_language->frequency + other_top_language->frequency); |
| 436 | UMA_HISTOGRAM_PERCENTAGE( |
| 437 | "NewTabPage.Languages.UILanguageRatioInTwoTopLanguages", |
| 438 | ratio_ui_in_both_languages * 100); |
| 439 | break; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | } // namespace internal |
| 445 | |
| 446 | } // namespace ntp_snippets |