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 int exclusion_count = 0; | |
333 for (const auto& id : excluded_ids) { | |
334 if (exclusion_count++ >= kMaxExcludedIds) | |
Marc Treib
2016/08/25 11:41:44
ListValue has a GetSize, no need for a separate co
sfiera
2016/08/25 11:58:07
Done.
| |
335 break; | |
336 excluded->AppendString(id); | |
337 } | |
338 request->Set("excludedSuggestionIds", std::move(excluded)); | |
339 | |
326 // TODO(sfiera): support authentication and personalization | 340 // TODO(sfiera): support authentication and personalization |
327 // TODO(sfiera): support count_to_fetch | 341 // TODO(sfiera): support count_to_fetch |
328 break; | 342 break; |
329 } | 343 } |
330 } | 344 } |
331 | 345 |
332 std::string request_json; | 346 std::string request_json; |
333 bool success = base::JSONWriter::WriteWithOptions( | 347 bool success = base::JSONWriter::WriteWithOptions( |
334 *request, base::JSONWriter::OPTIONS_PRETTY_PRINT, &request_json); | 348 *request, base::JSONWriter::OPTIONS_PRETTY_PRINT, &request_json); |
335 DCHECK(success); | 349 DCHECK(success); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
385 const std::string& key = is_stable_channel_ | 399 const std::string& key = is_stable_channel_ |
386 ? google_apis::GetAPIKey() | 400 ? google_apis::GetAPIKey() |
387 : google_apis::GetNonStableAPIKey(); | 401 : google_apis::GetNonStableAPIKey(); |
388 GURL url(base::StringPrintf(kSnippetsServerNonAuthorizedFormat, | 402 GURL url(base::StringPrintf(kSnippetsServerNonAuthorizedFormat, |
389 fetch_url_.spec().c_str(), key.c_str())); | 403 fetch_url_.spec().c_str(), key.c_str())); |
390 | 404 |
391 RequestParams params; | 405 RequestParams params; |
392 params.fetch_api = fetch_api_; | 406 params.fetch_api = fetch_api_; |
393 params.host_restricts = | 407 params.host_restricts = |
394 UsesHostRestrictions() ? hosts_ : std::set<std::string>(); | 408 UsesHostRestrictions() ? hosts_ : std::set<std::string>(); |
409 params.excluded_ids = excluded_ids_; | |
395 params.count_to_fetch = count_to_fetch_; | 410 params.count_to_fetch = count_to_fetch_; |
396 FetchSnippetsImpl(url, std::string(), params.BuildRequest()); | 411 FetchSnippetsImpl(url, std::string(), params.BuildRequest()); |
397 } | 412 } |
398 | 413 |
399 void NTPSnippetsFetcher::FetchSnippetsAuthenticated( | 414 void NTPSnippetsFetcher::FetchSnippetsAuthenticated( |
400 const std::string& account_id, | 415 const std::string& account_id, |
401 const std::string& oauth_access_token) { | 416 const std::string& oauth_access_token) { |
402 RequestParams params; | 417 RequestParams params; |
403 params.fetch_api = fetch_api_; | 418 params.fetch_api = fetch_api_; |
404 params.obfuscated_gaia_id = account_id; | 419 params.obfuscated_gaia_id = account_id; |
405 params.only_return_personalized_results = | 420 params.only_return_personalized_results = |
406 personalization_ == Personalization::kPersonal; | 421 personalization_ == Personalization::kPersonal; |
407 params.user_locale = locale_; | 422 params.user_locale = locale_; |
408 params.host_restricts = | 423 params.host_restricts = |
409 UsesHostRestrictions() ? hosts_ : std::set<std::string>(); | 424 UsesHostRestrictions() ? hosts_ : std::set<std::string>(); |
425 params.excluded_ids = excluded_ids_; | |
410 params.count_to_fetch = count_to_fetch_; | 426 params.count_to_fetch = count_to_fetch_; |
411 // TODO(jkrcal, treib): Add unit-tests for authenticated fetches. | 427 // TODO(jkrcal, treib): Add unit-tests for authenticated fetches. |
412 FetchSnippetsImpl(fetch_url_, | 428 FetchSnippetsImpl(fetch_url_, |
413 base::StringPrintf(kAuthorizationRequestHeaderFormat, | 429 base::StringPrintf(kAuthorizationRequestHeaderFormat, |
414 oauth_access_token.c_str()), | 430 oauth_access_token.c_str()), |
415 params.BuildRequest()); | 431 params.BuildRequest()); |
416 } | 432 } |
417 | 433 |
418 void NTPSnippetsFetcher::StartTokenRequest() { | 434 void NTPSnippetsFetcher::StartTokenRequest() { |
419 OAuth2TokenService::ScopeSet scopes; | 435 OAuth2TokenService::ScopeSet scopes; |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
594 } | 610 } |
595 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", | 611 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", |
596 static_cast<int>(result), | 612 static_cast<int>(result), |
597 static_cast<int>(FetchResult::RESULT_MAX)); | 613 static_cast<int>(FetchResult::RESULT_MAX)); |
598 | 614 |
599 if (!snippets_available_callback_.is_null()) | 615 if (!snippets_available_callback_.is_null()) |
600 snippets_available_callback_.Run(std::move(snippets)); | 616 snippets_available_callback_.Run(std::move(snippets)); |
601 } | 617 } |
602 | 618 |
603 } // namespace ntp_snippets | 619 } // namespace ntp_snippets |
OLD | NEW |