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