| 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 <cstdlib> | 7 #include <cstdlib> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 | 247 |
| 248 NTPSnippetsFetcher::FetchedCategory::FetchedCategory(Category c) | 248 NTPSnippetsFetcher::FetchedCategory::FetchedCategory(Category c) |
| 249 : category(c) {} | 249 : category(c) {} |
| 250 | 250 |
| 251 NTPSnippetsFetcher::FetchedCategory::FetchedCategory(FetchedCategory&&) = | 251 NTPSnippetsFetcher::FetchedCategory::FetchedCategory(FetchedCategory&&) = |
| 252 default; | 252 default; |
| 253 NTPSnippetsFetcher::FetchedCategory::~FetchedCategory() = default; | 253 NTPSnippetsFetcher::FetchedCategory::~FetchedCategory() = default; |
| 254 NTPSnippetsFetcher::FetchedCategory& NTPSnippetsFetcher::FetchedCategory:: | 254 NTPSnippetsFetcher::FetchedCategory& NTPSnippetsFetcher::FetchedCategory:: |
| 255 operator=(FetchedCategory&&) = default; | 255 operator=(FetchedCategory&&) = default; |
| 256 | 256 |
| 257 NTPSnippetsFetcher::Params::Params() = default; |
| 258 NTPSnippetsFetcher::Params::Params(const Params&) = default; |
| 259 NTPSnippetsFetcher::Params::~Params() = default; |
| 260 |
| 257 NTPSnippetsFetcher::NTPSnippetsFetcher( | 261 NTPSnippetsFetcher::NTPSnippetsFetcher( |
| 258 SigninManagerBase* signin_manager, | 262 SigninManagerBase* signin_manager, |
| 259 OAuth2TokenService* token_service, | 263 OAuth2TokenService* token_service, |
| 260 scoped_refptr<URLRequestContextGetter> url_request_context_getter, | 264 scoped_refptr<URLRequestContextGetter> url_request_context_getter, |
| 261 PrefService* pref_service, | 265 PrefService* pref_service, |
| 262 CategoryFactory* category_factory, | 266 CategoryFactory* category_factory, |
| 263 LanguageModel* language_model, | 267 LanguageModel* language_model, |
| 264 const ParseJSONCallback& parse_json_callback, | 268 const ParseJSONCallback& parse_json_callback, |
| 265 const std::string& api_key, | 269 const std::string& api_key, |
| 266 const UserClassifier* user_classifier) | 270 const UserClassifier* user_classifier) |
| 267 : OAuth2TokenService::Consumer("ntp_snippets"), | 271 : OAuth2TokenService::Consumer("ntp_snippets"), |
| 268 signin_manager_(signin_manager), | 272 signin_manager_(signin_manager), |
| 269 token_service_(token_service), | 273 token_service_(token_service), |
| 270 waiting_for_refresh_token_(false), | 274 waiting_for_refresh_token_(false), |
| 275 oauth_token_retried_(false), |
| 271 url_request_context_getter_(std::move(url_request_context_getter)), | 276 url_request_context_getter_(std::move(url_request_context_getter)), |
| 272 category_factory_(category_factory), | 277 category_factory_(category_factory), |
| 273 language_model_(language_model), | 278 language_model_(language_model), |
| 274 parse_json_callback_(parse_json_callback), | 279 parse_json_callback_(parse_json_callback), |
| 275 count_to_fetch_(0), | |
| 276 fetch_url_(GetFetchEndpoint()), | 280 fetch_url_(GetFetchEndpoint()), |
| 277 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_) | 281 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_) |
| 278 ? CHROME_CONTENT_SUGGESTIONS_API | 282 ? CHROME_CONTENT_SUGGESTIONS_API |
| 279 : CHROME_READER_API), | 283 : CHROME_READER_API), |
| 280 api_key_(api_key), | 284 api_key_(api_key), |
| 281 interactive_request_(false), | |
| 282 tick_clock_(new base::DefaultTickClock()), | 285 tick_clock_(new base::DefaultTickClock()), |
| 283 user_classifier_(user_classifier), | 286 user_classifier_(user_classifier), |
| 284 request_throttler_rare_ntp_user_( | 287 request_throttler_rare_ntp_user_( |
| 285 pref_service, | 288 pref_service, |
| 286 RequestThrottler::RequestType:: | 289 RequestThrottler::RequestType:: |
| 287 CONTENT_SUGGESTION_FETCHER_RARE_NTP_USER), | 290 CONTENT_SUGGESTION_FETCHER_RARE_NTP_USER), |
| 288 request_throttler_active_ntp_user_( | 291 request_throttler_active_ntp_user_( |
| 289 pref_service, | 292 pref_service, |
| 290 RequestThrottler::RequestType:: | 293 RequestThrottler::RequestType:: |
| 291 CONTENT_SUGGESTION_FETCHER_ACTIVE_NTP_USER), | 294 CONTENT_SUGGESTION_FETCHER_ACTIVE_NTP_USER), |
| 292 request_throttler_active_suggestions_consumer_( | 295 request_throttler_active_suggestions_consumer_( |
| 293 pref_service, | 296 pref_service, |
| 294 RequestThrottler::RequestType:: | 297 RequestThrottler::RequestType:: |
| 295 CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTIONS_CONSUMER), | 298 CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTIONS_CONSUMER), |
| 296 oauth_token_retried_(false), | |
| 297 weak_ptr_factory_(this) { | 299 weak_ptr_factory_(this) { |
| 298 // Parse the variation parameters and set the defaults if missing. | 300 // Parse the variation parameters and set the defaults if missing. |
| 299 std::string personalization = variations::GetVariationParamValue( | 301 std::string personalization = variations::GetVariationParamValue( |
| 300 ntp_snippets::kStudyName, kPersonalizationName); | 302 ntp_snippets::kStudyName, kPersonalizationName); |
| 301 if (personalization == kPersonalizationNonPersonalString) { | 303 if (personalization == kPersonalizationNonPersonalString) { |
| 302 personalization_ = Personalization::kNonPersonal; | 304 personalization_ = Personalization::kNonPersonal; |
| 303 } else if (personalization == kPersonalizationPersonalString) { | 305 } else if (personalization == kPersonalizationPersonalString) { |
| 304 personalization_ = Personalization::kPersonal; | 306 personalization_ = Personalization::kPersonal; |
| 305 } else { | 307 } else { |
| 306 personalization_ = Personalization::kBoth; | 308 personalization_ = Personalization::kBoth; |
| 307 LOG_IF(WARNING, !personalization.empty() && | 309 LOG_IF(WARNING, !personalization.empty() && |
| 308 personalization != kPersonalizationBothString) | 310 personalization != kPersonalizationBothString) |
| 309 << "Unknown value for " << kPersonalizationName << ": " | 311 << "Unknown value for " << kPersonalizationName << ": " |
| 310 << personalization; | 312 << personalization; |
| 311 } | 313 } |
| 312 } | 314 } |
| 313 | 315 |
| 314 NTPSnippetsFetcher::~NTPSnippetsFetcher() { | 316 NTPSnippetsFetcher::~NTPSnippetsFetcher() { |
| 315 if (waiting_for_refresh_token_) | 317 if (waiting_for_refresh_token_) |
| 316 token_service_->RemoveObserver(this); | 318 token_service_->RemoveObserver(this); |
| 317 } | 319 } |
| 318 | 320 |
| 319 void NTPSnippetsFetcher::SetCallback( | 321 void NTPSnippetsFetcher::SetCallback( |
| 320 const SnippetsAvailableCallback& callback) { | 322 const SnippetsAvailableCallback& callback) { |
| 321 snippets_available_callback_ = callback; | 323 snippets_available_callback_ = callback; |
| 322 } | 324 } |
| 323 | 325 |
| 324 void NTPSnippetsFetcher::FetchSnippetsFromHosts( | 326 void NTPSnippetsFetcher::FetchSnippets(const Params& params) { |
| 325 const std::set<std::string>& hosts, | 327 if (!DemandQuotaForRequest(params.interactive_request)) { |
| 326 const std::string& language_code, | |
| 327 const std::set<std::string>& excluded_ids, | |
| 328 int count, | |
| 329 bool interactive_request) { | |
| 330 if (!DemandQuotaForRequest(interactive_request)) { | |
| 331 FetchFinished(OptionalFetchedCategories(), | 328 FetchFinished(OptionalFetchedCategories(), |
| 332 interactive_request | 329 params.interactive_request |
| 333 ? FetchResult::INTERACTIVE_QUOTA_ERROR | 330 ? FetchResult::INTERACTIVE_QUOTA_ERROR |
| 334 : FetchResult::NON_INTERACTIVE_QUOTA_ERROR, | 331 : FetchResult::NON_INTERACTIVE_QUOTA_ERROR, |
| 335 /*extra_message=*/std::string()); | 332 /*extra_message=*/std::string()); |
| 336 return; | 333 return; |
| 337 } | 334 } |
| 338 | 335 |
| 339 hosts_ = hosts; | 336 params_ = params; |
| 340 fetch_start_time_ = tick_clock_->NowTicks(); | 337 fetch_start_time_ = tick_clock_->NowTicks(); |
| 341 excluded_ids_ = excluded_ids; | |
| 342 | |
| 343 locale_ = PosixLocaleFromBCP47Language(language_code); | |
| 344 count_to_fetch_ = count; | |
| 345 | 338 |
| 346 bool use_authentication = UsesAuthentication(); | 339 bool use_authentication = UsesAuthentication(); |
| 347 interactive_request_ = interactive_request; | |
| 348 | |
| 349 if (use_authentication && signin_manager_->IsAuthenticated()) { | 340 if (use_authentication && signin_manager_->IsAuthenticated()) { |
| 350 // Signed-in: get OAuth token --> fetch snippets. | 341 // Signed-in: get OAuth token --> fetch snippets. |
| 351 oauth_token_retried_ = false; | 342 oauth_token_retried_ = false; |
| 352 StartTokenRequest(); | 343 StartTokenRequest(); |
| 353 } else if (use_authentication && signin_manager_->AuthInProgress()) { | 344 } else if (use_authentication && signin_manager_->AuthInProgress()) { |
| 354 // Currently signing in: wait for auth to finish (the refresh token) --> | 345 // Currently signing in: wait for auth to finish (the refresh token) --> |
| 355 // get OAuth token --> fetch snippets. | 346 // get OAuth token --> fetch snippets. |
| 356 if (!waiting_for_refresh_token_) { | 347 if (!waiting_for_refresh_token_) { |
| 357 // Wait until we get a refresh token. | 348 // Wait until we get a refresh token. |
| 358 waiting_for_refresh_token_ = true; | 349 waiting_for_refresh_token_ = true; |
| 359 token_service_->AddObserver(this); | 350 token_service_->AddObserver(this); |
| 360 } | 351 } |
| 361 } else { | 352 } else { |
| 362 // Not signed in: fetch snippets (without authentication). | 353 // Not signed in: fetch snippets (without authentication). |
| 363 FetchSnippetsNonAuthenticated(); | 354 FetchSnippetsNonAuthenticated(); |
| 364 } | 355 } |
| 365 } | 356 } |
| 366 | 357 |
| 367 NTPSnippetsFetcher::RequestParams::RequestParams() | 358 NTPSnippetsFetcher::RequestBuilder::RequestBuilder() = default; |
| 368 : fetch_api(), | |
| 369 obfuscated_gaia_id(), | |
| 370 only_return_personalized_results(), | |
| 371 user_locale(), | |
| 372 host_restricts(), | |
| 373 count_to_fetch(), | |
| 374 interactive_request(), | |
| 375 user_class(), | |
| 376 ui_language{"", 0.0f}, | |
| 377 other_top_language{"", 0.0f} {} | |
| 378 | 359 |
| 379 NTPSnippetsFetcher::RequestParams::~RequestParams() = default; | 360 NTPSnippetsFetcher::RequestBuilder::RequestBuilder(RequestBuilder&&) = default; |
| 380 | 361 |
| 381 std::string NTPSnippetsFetcher::RequestParams::BuildRequest() { | 362 NTPSnippetsFetcher::RequestBuilder::~RequestBuilder() = default; |
| 363 |
| 364 std::string NTPSnippetsFetcher::RequestBuilder::BuildRequest() { |
| 382 auto request = base::MakeUnique<base::DictionaryValue>(); | 365 auto request = base::MakeUnique<base::DictionaryValue>(); |
| 366 std::string user_locale = PosixLocaleFromBCP47Language(params.language_code); |
| 383 switch (fetch_api) { | 367 switch (fetch_api) { |
| 384 case CHROME_READER_API: { | 368 case CHROME_READER_API: { |
| 385 auto content_params = base::MakeUnique<base::DictionaryValue>(); | 369 auto content_params = base::MakeUnique<base::DictionaryValue>(); |
| 386 content_params->SetBoolean("only_return_personalized_results", | 370 content_params->SetBoolean("only_return_personalized_results", |
| 387 only_return_personalized_results); | 371 only_return_personalized_results); |
| 388 | 372 |
| 389 auto content_restricts = base::MakeUnique<base::ListValue>(); | 373 auto content_restricts = base::MakeUnique<base::ListValue>(); |
| 390 for (const auto* metadata : {"TITLE", "SNIPPET", "THUMBNAIL"}) { | 374 for (const auto* metadata : {"TITLE", "SNIPPET", "THUMBNAIL"}) { |
| 391 auto entry = base::MakeUnique<base::DictionaryValue>(); | 375 auto entry = base::MakeUnique<base::DictionaryValue>(); |
| 392 entry->SetString("type", "METADATA"); | 376 entry->SetString("type", "METADATA"); |
| 393 entry->SetString("value", metadata); | 377 entry->SetString("value", metadata); |
| 394 content_restricts->Append(std::move(entry)); | 378 content_restricts->Append(std::move(entry)); |
| 395 } | 379 } |
| 396 | 380 |
| 397 auto content_selectors = base::MakeUnique<base::ListValue>(); | 381 auto content_selectors = base::MakeUnique<base::ListValue>(); |
| 398 for (const auto& host : host_restricts) { | 382 for (const auto& host : params.hosts) { |
| 399 auto entry = base::MakeUnique<base::DictionaryValue>(); | 383 auto entry = base::MakeUnique<base::DictionaryValue>(); |
| 400 entry->SetString("type", "HOST_RESTRICT"); | 384 entry->SetString("type", "HOST_RESTRICT"); |
| 401 entry->SetString("value", host); | 385 entry->SetString("value", host); |
| 402 content_selectors->Append(std::move(entry)); | 386 content_selectors->Append(std::move(entry)); |
| 403 } | 387 } |
| 404 | 388 |
| 405 auto local_scoring_params = base::MakeUnique<base::DictionaryValue>(); | 389 auto local_scoring_params = base::MakeUnique<base::DictionaryValue>(); |
| 406 local_scoring_params->Set("content_params", std::move(content_params)); | 390 local_scoring_params->Set("content_params", std::move(content_params)); |
| 407 local_scoring_params->Set("content_restricts", | 391 local_scoring_params->Set("content_restricts", |
| 408 std::move(content_restricts)); | 392 std::move(content_restricts)); |
| 409 local_scoring_params->Set("content_selectors", | 393 local_scoring_params->Set("content_selectors", |
| 410 std::move(content_selectors)); | 394 std::move(content_selectors)); |
| 411 | 395 |
| 412 auto global_scoring_params = base::MakeUnique<base::DictionaryValue>(); | 396 auto global_scoring_params = base::MakeUnique<base::DictionaryValue>(); |
| 413 global_scoring_params->SetInteger("num_to_return", count_to_fetch); | 397 global_scoring_params->SetInteger("num_to_return", params.count_to_fetch); |
| 414 global_scoring_params->SetInteger("sort_type", 1); | 398 global_scoring_params->SetInteger("sort_type", 1); |
| 415 | 399 |
| 416 auto advanced = base::MakeUnique<base::DictionaryValue>(); | 400 auto advanced = base::MakeUnique<base::DictionaryValue>(); |
| 417 advanced->Set("local_scoring_params", std::move(local_scoring_params)); | 401 advanced->Set("local_scoring_params", std::move(local_scoring_params)); |
| 418 advanced->Set("global_scoring_params", std::move(global_scoring_params)); | 402 advanced->Set("global_scoring_params", std::move(global_scoring_params)); |
| 419 | 403 |
| 420 request->SetString("response_detail_level", "STANDARD"); | 404 request->SetString("response_detail_level", "STANDARD"); |
| 421 request->Set("advanced_options", std::move(advanced)); | 405 request->Set("advanced_options", std::move(advanced)); |
| 422 if (!obfuscated_gaia_id.empty()) { | 406 if (!obfuscated_gaia_id.empty()) { |
| 423 request->SetString("obfuscated_gaia_id", obfuscated_gaia_id); | 407 request->SetString("obfuscated_gaia_id", obfuscated_gaia_id); |
| 424 } | 408 } |
| 425 if (!user_locale.empty()) { | 409 if (!user_locale.empty()) { |
| 426 request->SetString("user_locale", user_locale); | 410 request->SetString("user_locale", user_locale); |
| 427 } | 411 } |
| 428 break; | 412 break; |
| 429 } | 413 } |
| 430 | 414 |
| 431 case CHROME_CONTENT_SUGGESTIONS_API: { | 415 case CHROME_CONTENT_SUGGESTIONS_API: { |
| 432 if (!user_locale.empty()) { | 416 if (!user_locale.empty()) { |
| 433 request->SetString("uiLanguage", user_locale); | 417 request->SetString("uiLanguage", user_locale); |
| 434 } | 418 } |
| 435 | 419 |
| 436 auto regular_hosts = base::MakeUnique<base::ListValue>(); | 420 auto regular_hosts = base::MakeUnique<base::ListValue>(); |
| 437 for (const auto& host : host_restricts) { | 421 for (const auto& host : params.hosts) { |
| 438 regular_hosts->AppendString(host); | 422 regular_hosts->AppendString(host); |
| 439 } | 423 } |
| 440 request->Set("regularlyVisitedHostNames", std::move(regular_hosts)); | 424 request->Set("regularlyVisitedHostNames", std::move(regular_hosts)); |
| 441 request->SetString("priority", interactive_request | 425 request->SetString("priority", params.interactive_request |
| 442 ? "USER_ACTION" | 426 ? "USER_ACTION" |
| 443 : "BACKGROUND_PREFETCH"); | 427 : "BACKGROUND_PREFETCH"); |
| 444 | 428 |
| 445 auto excluded = base::MakeUnique<base::ListValue>(); | 429 auto excluded = base::MakeUnique<base::ListValue>(); |
| 446 for (const auto& id : excluded_ids) { | 430 for (const auto& id : params.excluded_ids) { |
| 447 excluded->AppendString(id); | 431 excluded->AppendString(id); |
| 448 if (excluded->GetSize() >= kMaxExcludedIds) | 432 if (excluded->GetSize() >= kMaxExcludedIds) |
| 449 break; | 433 break; |
| 450 } | 434 } |
| 451 request->Set("excludedSuggestionIds", std::move(excluded)); | 435 request->Set("excludedSuggestionIds", std::move(excluded)); |
| 452 | 436 |
| 453 if (!user_class.empty()) | 437 if (!user_class.empty()) |
| 454 request->SetString("userActivenessClass", user_class); | 438 request->SetString("userActivenessClass", user_class); |
| 455 | 439 |
| 456 if (ui_language.frequency == 0 && other_top_language.frequency == 0) | 440 if (ui_language.frequency == 0 && other_top_language.frequency == 0) |
| 457 break; | 441 break; |
| 458 | 442 |
| 459 auto language_list = base::MakeUnique<base::ListValue>(); | 443 auto language_list = base::MakeUnique<base::ListValue>(); |
| 460 if (ui_language.frequency > 0) | 444 if (ui_language.frequency > 0) |
| 461 AppendLanguageInfoToList(language_list.get(), ui_language); | 445 AppendLanguageInfoToList(language_list.get(), ui_language); |
| 462 if (other_top_language.frequency > 0) | 446 if (other_top_language.frequency > 0) |
| 463 AppendLanguageInfoToList(language_list.get(), other_top_language); | 447 AppendLanguageInfoToList(language_list.get(), other_top_language); |
| 464 request->Set("topLanguages", std::move(language_list)); | 448 request->Set("topLanguages", std::move(language_list)); |
| 465 | 449 |
| 466 // TODO(sfiera): support authentication and personalization | 450 // TODO(sfiera): Support only_return_personalized_results. |
| 467 // TODO(sfiera): support count_to_fetch | 451 // TODO(sfiera): Support count_to_fetch. |
| 468 break; | 452 break; |
| 469 } | 453 } |
| 470 } | 454 } |
| 471 | 455 |
| 472 std::string request_json; | 456 std::string request_json; |
| 473 bool success = base::JSONWriter::WriteWithOptions( | 457 bool success = base::JSONWriter::WriteWithOptions( |
| 474 *request, base::JSONWriter::OPTIONS_PRETTY_PRINT, &request_json); | 458 *request, base::JSONWriter::OPTIONS_PRETTY_PRINT, &request_json); |
| 475 DCHECK(success); | 459 DCHECK(success); |
| 476 return request_json; | 460 return request_json; |
| 477 } | 461 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 508 // Try to make fetching the files bit more robust even with poor connection. | 492 // Try to make fetching the files bit more robust even with poor connection. |
| 509 url_fetcher_->SetMaxRetriesOn5xx(3); | 493 url_fetcher_->SetMaxRetriesOn5xx(3); |
| 510 url_fetcher_->Start(); | 494 url_fetcher_->Start(); |
| 511 } | 495 } |
| 512 | 496 |
| 513 bool NTPSnippetsFetcher::UsesAuthentication() const { | 497 bool NTPSnippetsFetcher::UsesAuthentication() const { |
| 514 return (personalization_ == Personalization::kPersonal || | 498 return (personalization_ == Personalization::kPersonal || |
| 515 personalization_ == Personalization::kBoth); | 499 personalization_ == Personalization::kBoth); |
| 516 } | 500 } |
| 517 | 501 |
| 518 void NTPSnippetsFetcher::SetUpCommonFetchingParameters( | 502 NTPSnippetsFetcher::RequestBuilder NTPSnippetsFetcher::MakeRequestBuilder() |
| 519 RequestParams* params) const { | 503 const { |
| 520 params->fetch_api = fetch_api_; | 504 RequestBuilder result; |
| 521 params->host_restricts = hosts_; | 505 result.params = params_; |
| 522 params->user_locale = locale_; | 506 result.fetch_api = fetch_api_; |
| 523 params->excluded_ids = excluded_ids_; | |
| 524 params->count_to_fetch = count_to_fetch_; | |
| 525 params->interactive_request = interactive_request_; | |
| 526 | 507 |
| 527 if (IsSendingUserClassEnabled()) | 508 if (IsSendingUserClassEnabled()) |
| 528 params->user_class = GetUserClassString(user_classifier_->GetUserClass()); | 509 result.user_class = GetUserClassString(user_classifier_->GetUserClass()); |
| 529 | 510 |
| 530 // TODO(jkrcal): Add language model factory for iOS and add fakes to tests so | 511 // TODO(jkrcal): Add language model factory for iOS and add fakes to tests so |
| 531 // that |language_model_| is never nullptr. Remove this check and add a DCHECK | 512 // that |language_model_| is never nullptr. Remove this check and add a DCHECK |
| 532 // into the constructor. | 513 // into the constructor. |
| 533 if (!language_model_ || !IsSendingTopLanguagesEnabled()) | 514 if (!language_model_ || !IsSendingTopLanguagesEnabled()) |
| 534 return; | 515 return result; |
| 535 | 516 |
| 536 params->ui_language.language_code = ISO639FromPosixLocale(locale_); | 517 // TODO(jkrcal): Is this back-and-forth converting necessary? |
| 537 params->ui_language.frequency = | 518 result.ui_language.language_code = ISO639FromPosixLocale( |
| 538 language_model_->GetLanguageFrequency(params->ui_language.language_code); | 519 PosixLocaleFromBCP47Language(result.params.language_code)); |
| 520 result.ui_language.frequency = |
| 521 language_model_->GetLanguageFrequency(result.ui_language.language_code); |
| 539 | 522 |
| 540 std::vector<LanguageModel::LanguageInfo> top_languages = | 523 std::vector<LanguageModel::LanguageInfo> top_languages = |
| 541 language_model_->GetTopLanguages(); | 524 language_model_->GetTopLanguages(); |
| 542 for (const LanguageModel::LanguageInfo& info : top_languages) { | 525 for (const LanguageModel::LanguageInfo& info : top_languages) { |
| 543 if (info.language_code != params->ui_language.language_code) { | 526 if (info.language_code != result.ui_language.language_code) { |
| 544 params->other_top_language = info; | 527 result.other_top_language = info; |
| 545 | 528 |
| 546 // Report to UMA how important the UI language is. | 529 // Report to UMA how important the UI language is. |
| 547 DCHECK_GT(params->other_top_language.frequency, 0) | 530 DCHECK_GT(result.other_top_language.frequency, 0) |
| 548 << "GetTopLanguages() should not return languages with 0 frequency"; | 531 << "GetTopLanguages() should not return languages with 0 frequency"; |
| 549 float ratio_ui_in_both_languages = params->ui_language.frequency / | 532 float ratio_ui_in_both_languages = |
| 550 (params->ui_language.frequency + | 533 result.ui_language.frequency / |
| 551 params->other_top_language.frequency); | 534 (result.ui_language.frequency + result.other_top_language.frequency); |
| 552 UMA_HISTOGRAM_PERCENTAGE( | 535 UMA_HISTOGRAM_PERCENTAGE( |
| 553 "NewTabPage.Languages.UILanguageRatioInTwoTopLanguages", | 536 "NewTabPage.Languages.UILanguageRatioInTwoTopLanguages", |
| 554 ratio_ui_in_both_languages * 100); | 537 ratio_ui_in_both_languages * 100); |
| 555 break; | 538 break; |
| 556 } | 539 } |
| 557 } | 540 } |
| 541 |
| 542 return result; |
| 558 } | 543 } |
| 559 | 544 |
| 560 void NTPSnippetsFetcher::FetchSnippetsNonAuthenticated() { | 545 void NTPSnippetsFetcher::FetchSnippetsNonAuthenticated() { |
| 561 // When not providing OAuth token, we need to pass the Google API key. | 546 // When not providing OAuth token, we need to pass the Google API key. |
| 562 GURL url(base::StringPrintf(kSnippetsServerNonAuthorizedFormat, | 547 GURL url(base::StringPrintf(kSnippetsServerNonAuthorizedFormat, |
| 563 fetch_url_.spec().c_str(), api_key_.c_str())); | 548 fetch_url_.spec().c_str(), api_key_.c_str())); |
| 564 | 549 FetchSnippetsImpl(url, std::string(), MakeRequestBuilder().BuildRequest()); |
| 565 RequestParams params; | |
| 566 SetUpCommonFetchingParameters(¶ms); | |
| 567 FetchSnippetsImpl(url, std::string(), params.BuildRequest()); | |
| 568 } | 550 } |
| 569 | 551 |
| 570 void NTPSnippetsFetcher::FetchSnippetsAuthenticated( | 552 void NTPSnippetsFetcher::FetchSnippetsAuthenticated( |
| 571 const std::string& account_id, | 553 const std::string& account_id, |
| 572 const std::string& oauth_access_token) { | 554 const std::string& oauth_access_token) { |
| 573 RequestParams params; | 555 RequestBuilder builder = MakeRequestBuilder(); |
| 574 SetUpCommonFetchingParameters(¶ms); | 556 builder.obfuscated_gaia_id = account_id; |
| 575 params.obfuscated_gaia_id = account_id; | 557 builder.only_return_personalized_results = |
| 576 params.only_return_personalized_results = | |
| 577 personalization_ == Personalization::kPersonal; | 558 personalization_ == Personalization::kPersonal; |
| 578 // TODO(jkrcal, treib): Add unit-tests for authenticated fetches. | 559 // TODO(jkrcal, treib): Add unit-tests for authenticated fetches. |
| 579 FetchSnippetsImpl(fetch_url_, | 560 FetchSnippetsImpl(fetch_url_, |
| 580 base::StringPrintf(kAuthorizationRequestHeaderFormat, | 561 base::StringPrintf(kAuthorizationRequestHeaderFormat, |
| 581 oauth_access_token.c_str()), | 562 oauth_access_token.c_str()), |
| 582 params.BuildRequest()); | 563 builder.BuildRequest()); |
| 583 } | 564 } |
| 584 | 565 |
| 585 void NTPSnippetsFetcher::StartTokenRequest() { | 566 void NTPSnippetsFetcher::StartTokenRequest() { |
| 586 OAuth2TokenService::ScopeSet scopes; | 567 OAuth2TokenService::ScopeSet scopes; |
| 587 scopes.insert(fetch_api_ == CHROME_CONTENT_SUGGESTIONS_API | 568 scopes.insert(fetch_api_ == CHROME_CONTENT_SUGGESTIONS_API |
| 588 ? kContentSuggestionsApiScope | 569 ? kContentSuggestionsApiScope |
| 589 : kChromeReaderApiScope); | 570 : kChromeReaderApiScope); |
| 590 oauth_request_ = token_service_->StartRequest( | 571 oauth_request_ = token_service_->StartRequest( |
| 591 signin_manager_->GetAuthenticatedAccountId(), scopes, this); | 572 signin_manager_->GetAuthenticatedAccountId(), scopes, this); |
| 592 } | 573 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 637 token_service_->RemoveObserver(this); | 618 token_service_->RemoveObserver(this); |
| 638 waiting_for_refresh_token_ = false; | 619 waiting_for_refresh_token_ = false; |
| 639 oauth_token_retried_ = false; | 620 oauth_token_retried_ = false; |
| 640 StartTokenRequest(); | 621 StartTokenRequest(); |
| 641 } | 622 } |
| 642 | 623 |
| 643 //////////////////////////////////////////////////////////////////////////////// | 624 //////////////////////////////////////////////////////////////////////////////// |
| 644 // URLFetcherDelegate overrides | 625 // URLFetcherDelegate overrides |
| 645 void NTPSnippetsFetcher::OnURLFetchComplete(const URLFetcher* source) { | 626 void NTPSnippetsFetcher::OnURLFetchComplete(const URLFetcher* source) { |
| 646 DCHECK_EQ(url_fetcher_.get(), source); | 627 DCHECK_EQ(url_fetcher_.get(), source); |
| 628 std::unique_ptr<URLFetcher> deleter = std::move(url_fetcher_); |
| 647 | 629 |
| 648 const URLRequestStatus& status = source->GetStatus(); | 630 const URLRequestStatus& status = source->GetStatus(); |
| 649 | 631 |
| 650 UMA_HISTOGRAM_SPARSE_SLOWLY( | 632 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 651 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode", | 633 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode", |
| 652 status.is_success() ? source->GetResponseCode() : status.error()); | 634 status.is_success() ? source->GetResponseCode() : status.error()); |
| 653 | 635 |
| 654 if (!status.is_success()) { | 636 if (!status.is_success()) { |
| 655 FetchFinished(OptionalFetchedCategories(), | 637 FetchFinished(OptionalFetchedCategories(), |
| 656 FetchResult::URL_REQUEST_STATUS_ERROR, | 638 FetchResult::URL_REQUEST_STATUS_ERROR, |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 790 interactive_request); | 772 interactive_request); |
| 791 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: | 773 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: |
| 792 return request_throttler_active_suggestions_consumer_ | 774 return request_throttler_active_suggestions_consumer_ |
| 793 .DemandQuotaForRequest(interactive_request); | 775 .DemandQuotaForRequest(interactive_request); |
| 794 } | 776 } |
| 795 NOTREACHED(); | 777 NOTREACHED(); |
| 796 return false; | 778 return false; |
| 797 } | 779 } |
| 798 | 780 |
| 799 } // namespace ntp_snippets | 781 } // namespace ntp_snippets |
| OLD | NEW |