| 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/ntp_snippets_fetcher.h" | 5 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 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 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 } | 209 } |
| 210 | 210 |
| 211 void NTPSnippetsFetcher::SetCallback( | 211 void NTPSnippetsFetcher::SetCallback( |
| 212 const SnippetsAvailableCallback& callback) { | 212 const SnippetsAvailableCallback& callback) { |
| 213 snippets_available_callback_ = callback; | 213 snippets_available_callback_ = callback; |
| 214 } | 214 } |
| 215 | 215 |
| 216 void NTPSnippetsFetcher::FetchSnippetsFromHosts( | 216 void NTPSnippetsFetcher::FetchSnippetsFromHosts( |
| 217 const std::set<std::string>& hosts, | 217 const std::set<std::string>& hosts, |
| 218 const std::string& language_code, | 218 const std::string& language_code, |
| 219 const std::set<std::string>& excluded_ids, |
| 219 int count, | 220 int count, |
| 220 bool interactive_request) { | 221 bool interactive_request) { |
| 221 if (!request_throttler_.DemandQuotaForRequest(interactive_request)) | 222 if (!request_throttler_.DemandQuotaForRequest(interactive_request)) |
| 222 return; | 223 return; |
| 223 | 224 |
| 224 hosts_ = hosts; | 225 hosts_ = hosts; |
| 225 fetch_start_time_ = tick_clock_->NowTicks(); | 226 fetch_start_time_ = tick_clock_->NowTicks(); |
| 227 excluded_ids_ = excluded_ids; |
| 226 | 228 |
| 227 if (UsesHostRestrictions() && hosts_.empty()) { | 229 if (UsesHostRestrictions() && hosts_.empty()) { |
| 228 FetchFinished(OptionalSnippets(), FetchResult::EMPTY_HOSTS, | 230 FetchFinished(OptionalSnippets(), FetchResult::EMPTY_HOSTS, |
| 229 /*extra_message=*/std::string()); | 231 /*extra_message=*/std::string()); |
| 230 return; | 232 return; |
| 231 } | 233 } |
| 232 | 234 |
| 233 locale_ = PosixLocaleFromBCP47Language(language_code); | 235 locale_ = PosixLocaleFromBCP47Language(language_code); |
| 234 count_to_fetch_ = count; | 236 count_to_fetch_ = count; |
| 235 | 237 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 if (!user_locale.empty()) { | 312 if (!user_locale.empty()) { |
| 311 request->SetString("user_locale", user_locale); | 313 request->SetString("user_locale", user_locale); |
| 312 } | 314 } |
| 313 break; | 315 break; |
| 314 } | 316 } |
| 315 | 317 |
| 316 case CHROME_CONTENT_SUGGESTIONS_API: { | 318 case CHROME_CONTENT_SUGGESTIONS_API: { |
| 317 if (!user_locale.empty()) { | 319 if (!user_locale.empty()) { |
| 318 request->SetString("uiLanguage", user_locale); | 320 request->SetString("uiLanguage", user_locale); |
| 319 } | 321 } |
| 322 |
| 320 auto regular_hosts = base::MakeUnique<base::ListValue>(); | 323 auto regular_hosts = base::MakeUnique<base::ListValue>(); |
| 321 for (const auto& host : host_restricts) { | 324 for (const auto& host : host_restricts) { |
| 322 regular_hosts->AppendString(host); | 325 regular_hosts->AppendString(host); |
| 323 } | 326 } |
| 324 request->Set("regularlyVisitedHostNames", std::move(regular_hosts)); | 327 request->Set("regularlyVisitedHostNames", std::move(regular_hosts)); |
| 325 | 328 |
| 329 auto excluded = base::MakeUnique<base::ListValue>(); |
| 330 for (const auto& id : excluded_ids) { |
| 331 excluded->AppendString(id); |
| 332 } |
| 333 request->Set("excludedSuggestionIds", std::move(excluded)); |
| 334 |
| 326 // TODO(sfiera): support authentication and personalization | 335 // TODO(sfiera): support authentication and personalization |
| 327 // TODO(sfiera): support count_to_fetch | 336 // TODO(sfiera): support count_to_fetch |
| 328 break; | 337 break; |
| 329 } | 338 } |
| 330 } | 339 } |
| 331 | 340 |
| 332 std::string request_json; | 341 std::string request_json; |
| 333 bool success = base::JSONWriter::WriteWithOptions( | 342 bool success = base::JSONWriter::WriteWithOptions( |
| 334 *request, base::JSONWriter::OPTIONS_PRETTY_PRINT, &request_json); | 343 *request, base::JSONWriter::OPTIONS_PRETTY_PRINT, &request_json); |
| 335 DCHECK(success); | 344 DCHECK(success); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 const std::string& key = is_stable_channel_ | 394 const std::string& key = is_stable_channel_ |
| 386 ? google_apis::GetAPIKey() | 395 ? google_apis::GetAPIKey() |
| 387 : google_apis::GetNonStableAPIKey(); | 396 : google_apis::GetNonStableAPIKey(); |
| 388 GURL url(base::StringPrintf(kSnippetsServerNonAuthorizedFormat, | 397 GURL url(base::StringPrintf(kSnippetsServerNonAuthorizedFormat, |
| 389 fetch_url_.spec().c_str(), key.c_str())); | 398 fetch_url_.spec().c_str(), key.c_str())); |
| 390 | 399 |
| 391 RequestParams params; | 400 RequestParams params; |
| 392 params.fetch_api = fetch_api_; | 401 params.fetch_api = fetch_api_; |
| 393 params.host_restricts = | 402 params.host_restricts = |
| 394 UsesHostRestrictions() ? hosts_ : std::set<std::string>(); | 403 UsesHostRestrictions() ? hosts_ : std::set<std::string>(); |
| 404 params.excluded_ids = excluded_ids_; |
| 395 params.count_to_fetch = count_to_fetch_; | 405 params.count_to_fetch = count_to_fetch_; |
| 396 FetchSnippetsImpl(url, std::string(), params.BuildRequest()); | 406 FetchSnippetsImpl(url, std::string(), params.BuildRequest()); |
| 397 } | 407 } |
| 398 | 408 |
| 399 void NTPSnippetsFetcher::FetchSnippetsAuthenticated( | 409 void NTPSnippetsFetcher::FetchSnippetsAuthenticated( |
| 400 const std::string& account_id, | 410 const std::string& account_id, |
| 401 const std::string& oauth_access_token) { | 411 const std::string& oauth_access_token) { |
| 402 RequestParams params; | 412 RequestParams params; |
| 403 params.fetch_api = fetch_api_; | 413 params.fetch_api = fetch_api_; |
| 404 params.obfuscated_gaia_id = account_id; | 414 params.obfuscated_gaia_id = account_id; |
| 405 params.only_return_personalized_results = | 415 params.only_return_personalized_results = |
| 406 personalization_ == Personalization::kPersonal; | 416 personalization_ == Personalization::kPersonal; |
| 407 params.user_locale = locale_; | 417 params.user_locale = locale_; |
| 408 params.host_restricts = | 418 params.host_restricts = |
| 409 UsesHostRestrictions() ? hosts_ : std::set<std::string>(); | 419 UsesHostRestrictions() ? hosts_ : std::set<std::string>(); |
| 420 params.excluded_ids = excluded_ids_; |
| 410 params.count_to_fetch = count_to_fetch_; | 421 params.count_to_fetch = count_to_fetch_; |
| 411 // TODO(jkrcal, treib): Add unit-tests for authenticated fetches. | 422 // TODO(jkrcal, treib): Add unit-tests for authenticated fetches. |
| 412 FetchSnippetsImpl(fetch_url_, | 423 FetchSnippetsImpl(fetch_url_, |
| 413 base::StringPrintf(kAuthorizationRequestHeaderFormat, | 424 base::StringPrintf(kAuthorizationRequestHeaderFormat, |
| 414 oauth_access_token.c_str()), | 425 oauth_access_token.c_str()), |
| 415 params.BuildRequest()); | 426 params.BuildRequest()); |
| 416 } | 427 } |
| 417 | 428 |
| 418 void NTPSnippetsFetcher::StartTokenRequest() { | 429 void NTPSnippetsFetcher::StartTokenRequest() { |
| 419 OAuth2TokenService::ScopeSet scopes; | 430 OAuth2TokenService::ScopeSet scopes; |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 } | 605 } |
| 595 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", | 606 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", |
| 596 static_cast<int>(result), | 607 static_cast<int>(result), |
| 597 static_cast<int>(FetchResult::RESULT_MAX)); | 608 static_cast<int>(FetchResult::RESULT_MAX)); |
| 598 | 609 |
| 599 if (!snippets_available_callback_.is_null()) | 610 if (!snippets_available_callback_.is_null()) |
| 600 snippets_available_callback_.Run(std::move(snippets)); | 611 snippets_available_callback_.Run(std::move(snippets)); |
| 601 } | 612 } |
| 602 | 613 |
| 603 } // namespace ntp_snippets | 614 } // namespace ntp_snippets |
| OLD | NEW |