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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 | 63 |
64 // Constants for possible values of the "fetching_personalization" parameter. | 64 // Constants for possible values of the "fetching_personalization" parameter. |
65 const char kPersonalizationPersonalString[] = "personal"; | 65 const char kPersonalizationPersonalString[] = "personal"; |
66 const char kPersonalizationNonPersonalString[] = "non_personal"; | 66 const char kPersonalizationNonPersonalString[] = "non_personal"; |
67 const char kPersonalizationBothString[] = "both"; // the default value | 67 const char kPersonalizationBothString[] = "both"; // the default value |
68 | 68 |
69 // Constants for possible values of the "fetching_host_restrict" parameter. | 69 // Constants for possible values of the "fetching_host_restrict" parameter. |
70 const char kHostRestrictionOnString[] = "on"; // the default value | 70 const char kHostRestrictionOnString[] = "on"; // the default value |
71 const char kHostRestrictionOffString[] = "off"; | 71 const char kHostRestrictionOffString[] = "off"; |
72 | 72 |
| 73 const int kMaxExcludedIds = 100; |
| 74 |
73 std::string FetchResultToString(NTPSnippetsFetcher::FetchResult result) { | 75 std::string FetchResultToString(NTPSnippetsFetcher::FetchResult result) { |
74 switch (result) { | 76 switch (result) { |
75 case NTPSnippetsFetcher::FetchResult::SUCCESS: | 77 case NTPSnippetsFetcher::FetchResult::SUCCESS: |
76 return "OK"; | 78 return "OK"; |
77 case NTPSnippetsFetcher::FetchResult::EMPTY_HOSTS: | 79 case NTPSnippetsFetcher::FetchResult::EMPTY_HOSTS: |
78 return "Cannot fetch for empty hosts list."; | 80 return "Cannot fetch for empty hosts list."; |
79 case NTPSnippetsFetcher::FetchResult::URL_REQUEST_STATUS_ERROR: | 81 case NTPSnippetsFetcher::FetchResult::URL_REQUEST_STATUS_ERROR: |
80 return "URLRequestStatus error"; | 82 return "URLRequestStatus error"; |
81 case NTPSnippetsFetcher::FetchResult::HTTP_ERROR: | 83 case NTPSnippetsFetcher::FetchResult::HTTP_ERROR: |
82 return "HTTP error"; | 84 return "HTTP error"; |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 } | 211 } |
210 | 212 |
211 void NTPSnippetsFetcher::SetCallback( | 213 void NTPSnippetsFetcher::SetCallback( |
212 const SnippetsAvailableCallback& callback) { | 214 const SnippetsAvailableCallback& callback) { |
213 snippets_available_callback_ = callback; | 215 snippets_available_callback_ = callback; |
214 } | 216 } |
215 | 217 |
216 void NTPSnippetsFetcher::FetchSnippetsFromHosts( | 218 void NTPSnippetsFetcher::FetchSnippetsFromHosts( |
217 const std::set<std::string>& hosts, | 219 const std::set<std::string>& hosts, |
218 const std::string& language_code, | 220 const std::string& language_code, |
| 221 const std::set<std::string>& excluded_ids, |
219 int count, | 222 int count, |
220 bool interactive_request) { | 223 bool interactive_request) { |
221 if (!request_throttler_.DemandQuotaForRequest(interactive_request)) | 224 if (!request_throttler_.DemandQuotaForRequest(interactive_request)) |
222 return; | 225 return; |
223 | 226 |
224 hosts_ = hosts; | 227 hosts_ = hosts; |
225 fetch_start_time_ = tick_clock_->NowTicks(); | 228 fetch_start_time_ = tick_clock_->NowTicks(); |
| 229 excluded_ids_ = excluded_ids; |
226 | 230 |
227 if (UsesHostRestrictions() && hosts_.empty()) { | 231 if (UsesHostRestrictions() && hosts_.empty()) { |
228 FetchFinished(OptionalSnippets(), FetchResult::EMPTY_HOSTS, | 232 FetchFinished(OptionalSnippets(), FetchResult::EMPTY_HOSTS, |
229 /*extra_message=*/std::string()); | 233 /*extra_message=*/std::string()); |
230 return; | 234 return; |
231 } | 235 } |
232 | 236 |
233 locale_ = PosixLocaleFromBCP47Language(language_code); | 237 locale_ = PosixLocaleFromBCP47Language(language_code); |
234 count_to_fetch_ = count; | 238 count_to_fetch_ = count; |
235 | 239 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 if (!user_locale.empty()) { | 314 if (!user_locale.empty()) { |
311 request->SetString("user_locale", user_locale); | 315 request->SetString("user_locale", user_locale); |
312 } | 316 } |
313 break; | 317 break; |
314 } | 318 } |
315 | 319 |
316 case CHROME_CONTENT_SUGGESTIONS_API: { | 320 case CHROME_CONTENT_SUGGESTIONS_API: { |
317 if (!user_locale.empty()) { | 321 if (!user_locale.empty()) { |
318 request->SetString("uiLanguage", user_locale); | 322 request->SetString("uiLanguage", user_locale); |
319 } | 323 } |
| 324 |
320 auto regular_hosts = base::MakeUnique<base::ListValue>(); | 325 auto regular_hosts = base::MakeUnique<base::ListValue>(); |
321 for (const auto& host : host_restricts) { | 326 for (const auto& host : host_restricts) { |
322 regular_hosts->AppendString(host); | 327 regular_hosts->AppendString(host); |
323 } | 328 } |
324 request->Set("regularlyVisitedHostNames", std::move(regular_hosts)); | 329 request->Set("regularlyVisitedHostNames", std::move(regular_hosts)); |
325 | 330 |
| 331 auto excluded = base::MakeUnique<base::ListValue>(); |
| 332 for (const auto& id : excluded_ids) { |
| 333 excluded->AppendString(id); |
| 334 if (excluded->GetSize() >= kMaxExcludedIds) |
| 335 break; |
| 336 } |
| 337 request->Set("excludedSuggestionIds", std::move(excluded)); |
| 338 |
326 // TODO(sfiera): support authentication and personalization | 339 // TODO(sfiera): support authentication and personalization |
327 // TODO(sfiera): support count_to_fetch | 340 // TODO(sfiera): support count_to_fetch |
328 break; | 341 break; |
329 } | 342 } |
330 } | 343 } |
331 | 344 |
332 std::string request_json; | 345 std::string request_json; |
333 bool success = base::JSONWriter::WriteWithOptions( | 346 bool success = base::JSONWriter::WriteWithOptions( |
334 *request, base::JSONWriter::OPTIONS_PRETTY_PRINT, &request_json); | 347 *request, base::JSONWriter::OPTIONS_PRETTY_PRINT, &request_json); |
335 DCHECK(success); | 348 DCHECK(success); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 const std::string& key = is_stable_channel_ | 398 const std::string& key = is_stable_channel_ |
386 ? google_apis::GetAPIKey() | 399 ? google_apis::GetAPIKey() |
387 : google_apis::GetNonStableAPIKey(); | 400 : google_apis::GetNonStableAPIKey(); |
388 GURL url(base::StringPrintf(kSnippetsServerNonAuthorizedFormat, | 401 GURL url(base::StringPrintf(kSnippetsServerNonAuthorizedFormat, |
389 fetch_url_.spec().c_str(), key.c_str())); | 402 fetch_url_.spec().c_str(), key.c_str())); |
390 | 403 |
391 RequestParams params; | 404 RequestParams params; |
392 params.fetch_api = fetch_api_; | 405 params.fetch_api = fetch_api_; |
393 params.host_restricts = | 406 params.host_restricts = |
394 UsesHostRestrictions() ? hosts_ : std::set<std::string>(); | 407 UsesHostRestrictions() ? hosts_ : std::set<std::string>(); |
| 408 params.excluded_ids = excluded_ids_; |
395 params.count_to_fetch = count_to_fetch_; | 409 params.count_to_fetch = count_to_fetch_; |
396 FetchSnippetsImpl(url, std::string(), params.BuildRequest()); | 410 FetchSnippetsImpl(url, std::string(), params.BuildRequest()); |
397 } | 411 } |
398 | 412 |
399 void NTPSnippetsFetcher::FetchSnippetsAuthenticated( | 413 void NTPSnippetsFetcher::FetchSnippetsAuthenticated( |
400 const std::string& account_id, | 414 const std::string& account_id, |
401 const std::string& oauth_access_token) { | 415 const std::string& oauth_access_token) { |
402 RequestParams params; | 416 RequestParams params; |
403 params.fetch_api = fetch_api_; | 417 params.fetch_api = fetch_api_; |
404 params.obfuscated_gaia_id = account_id; | 418 params.obfuscated_gaia_id = account_id; |
405 params.only_return_personalized_results = | 419 params.only_return_personalized_results = |
406 personalization_ == Personalization::kPersonal; | 420 personalization_ == Personalization::kPersonal; |
407 params.user_locale = locale_; | 421 params.user_locale = locale_; |
408 params.host_restricts = | 422 params.host_restricts = |
409 UsesHostRestrictions() ? hosts_ : std::set<std::string>(); | 423 UsesHostRestrictions() ? hosts_ : std::set<std::string>(); |
| 424 params.excluded_ids = excluded_ids_; |
410 params.count_to_fetch = count_to_fetch_; | 425 params.count_to_fetch = count_to_fetch_; |
411 // TODO(jkrcal, treib): Add unit-tests for authenticated fetches. | 426 // TODO(jkrcal, treib): Add unit-tests for authenticated fetches. |
412 FetchSnippetsImpl(fetch_url_, | 427 FetchSnippetsImpl(fetch_url_, |
413 base::StringPrintf(kAuthorizationRequestHeaderFormat, | 428 base::StringPrintf(kAuthorizationRequestHeaderFormat, |
414 oauth_access_token.c_str()), | 429 oauth_access_token.c_str()), |
415 params.BuildRequest()); | 430 params.BuildRequest()); |
416 } | 431 } |
417 | 432 |
418 void NTPSnippetsFetcher::StartTokenRequest() { | 433 void NTPSnippetsFetcher::StartTokenRequest() { |
419 OAuth2TokenService::ScopeSet scopes; | 434 OAuth2TokenService::ScopeSet scopes; |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 } | 613 } |
599 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", | 614 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", |
600 static_cast<int>(result), | 615 static_cast<int>(result), |
601 static_cast<int>(FetchResult::RESULT_MAX)); | 616 static_cast<int>(FetchResult::RESULT_MAX)); |
602 | 617 |
603 if (!snippets_available_callback_.is_null()) | 618 if (!snippets_available_callback_.is_null()) |
604 snippets_available_callback_.Run(std::move(snippets)); | 619 snippets_available_callback_.Run(std::move(snippets)); |
605 } | 620 } |
606 | 621 |
607 } // namespace ntp_snippets | 622 } // namespace ntp_snippets |
OLD | NEW |