Chromium Code Reviews| 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 79 case NTPSnippetsFetcher::FetchResult::URL_REQUEST_STATUS_ERROR: | 79 case NTPSnippetsFetcher::FetchResult::URL_REQUEST_STATUS_ERROR: |
| 80 return "URLRequestStatus error"; | 80 return "URLRequestStatus error"; |
| 81 case NTPSnippetsFetcher::FetchResult::HTTP_ERROR: | 81 case NTPSnippetsFetcher::FetchResult::HTTP_ERROR: |
| 82 return "HTTP error"; | 82 return "HTTP error"; |
| 83 case NTPSnippetsFetcher::FetchResult::JSON_PARSE_ERROR: | 83 case NTPSnippetsFetcher::FetchResult::JSON_PARSE_ERROR: |
| 84 return "Received invalid JSON"; | 84 return "Received invalid JSON"; |
| 85 case NTPSnippetsFetcher::FetchResult::INVALID_SNIPPET_CONTENT_ERROR: | 85 case NTPSnippetsFetcher::FetchResult::INVALID_SNIPPET_CONTENT_ERROR: |
| 86 return "Invalid / empty list."; | 86 return "Invalid / empty list."; |
| 87 case NTPSnippetsFetcher::FetchResult::OAUTH_TOKEN_ERROR: | 87 case NTPSnippetsFetcher::FetchResult::OAUTH_TOKEN_ERROR: |
| 88 return "Error in obtaining an OAuth2 access token."; | 88 return "Error in obtaining an OAuth2 access token."; |
| 89 case NTPSnippetsFetcher::FetchResult::INTERACTIVE_QUOTA_ERROR: | |
| 90 return "Out of interactive quota."; | |
| 91 case NTPSnippetsFetcher::FetchResult::NON_INTERACTIVE_QUOTA_ERROR: | |
| 92 return "Out of non-interactive quota."; | |
| 89 case NTPSnippetsFetcher::FetchResult::RESULT_MAX: | 93 case NTPSnippetsFetcher::FetchResult::RESULT_MAX: |
| 90 break; | 94 break; |
| 91 } | 95 } |
| 92 NOTREACHED(); | 96 NOTREACHED(); |
| 93 return "Unknown error"; | 97 return "Unknown error"; |
| 94 } | 98 } |
| 95 | 99 |
| 100 bool IsFetchPreconditionFailed(NTPSnippetsFetcher::FetchResult result) { | |
| 101 switch (result) { | |
| 102 case NTPSnippetsFetcher::FetchResult::EMPTY_HOSTS: | |
| 103 case NTPSnippetsFetcher::FetchResult::OAUTH_TOKEN_ERROR: | |
| 104 case NTPSnippetsFetcher::FetchResult::INTERACTIVE_QUOTA_ERROR: | |
| 105 case NTPSnippetsFetcher::FetchResult::NON_INTERACTIVE_QUOTA_ERROR: | |
| 106 return true; | |
| 107 case NTPSnippetsFetcher::FetchResult::SUCCESS: | |
| 108 case NTPSnippetsFetcher::FetchResult::URL_REQUEST_STATUS_ERROR: | |
| 109 case NTPSnippetsFetcher::FetchResult::HTTP_ERROR: | |
| 110 case NTPSnippetsFetcher::FetchResult::JSON_PARSE_ERROR: | |
| 111 case NTPSnippetsFetcher::FetchResult::INVALID_SNIPPET_CONTENT_ERROR: | |
| 112 case NTPSnippetsFetcher::FetchResult::RESULT_MAX: | |
|
Marc Treib
2016/08/25 14:41:06
nit: RESULT_MAX should never happen, so you could
| |
| 113 return false; | |
| 114 } | |
| 115 NOTREACHED(); | |
| 116 return true; | |
| 117 } | |
| 118 | |
| 96 std::string GetFetchEndpoint() { | 119 std::string GetFetchEndpoint() { |
| 97 std::string endpoint = variations::GetVariationParamValue( | 120 std::string endpoint = variations::GetVariationParamValue( |
| 98 ntp_snippets::kStudyName, kContentSuggestionsBackend); | 121 ntp_snippets::kStudyName, kContentSuggestionsBackend); |
| 99 return endpoint.empty() ? kChromeReaderServer : endpoint; | 122 return endpoint.empty() ? kChromeReaderServer : endpoint; |
| 100 } | 123 } |
| 101 | 124 |
| 102 bool UsesChromeContentSuggestionsAPI(const GURL& endpoint) { | 125 bool UsesChromeContentSuggestionsAPI(const GURL& endpoint) { |
| 103 if (endpoint == GURL(kChromeReaderServer)) { | 126 if (endpoint == GURL(kChromeReaderServer)) { |
| 104 return false; | 127 return false; |
| 105 } else if (endpoint != GURL(kContentSuggestionsServer) && | 128 } else if (endpoint != GURL(kContentSuggestionsServer) && |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 211 void NTPSnippetsFetcher::SetCallback( | 234 void NTPSnippetsFetcher::SetCallback( |
| 212 const SnippetsAvailableCallback& callback) { | 235 const SnippetsAvailableCallback& callback) { |
| 213 snippets_available_callback_ = callback; | 236 snippets_available_callback_ = callback; |
| 214 } | 237 } |
| 215 | 238 |
| 216 void NTPSnippetsFetcher::FetchSnippetsFromHosts( | 239 void NTPSnippetsFetcher::FetchSnippetsFromHosts( |
| 217 const std::set<std::string>& hosts, | 240 const std::set<std::string>& hosts, |
| 218 const std::string& language_code, | 241 const std::string& language_code, |
| 219 int count, | 242 int count, |
| 220 bool interactive_request) { | 243 bool interactive_request) { |
| 221 if (!request_throttler_.DemandQuotaForRequest(interactive_request)) | 244 if (!request_throttler_.DemandQuotaForRequest(interactive_request)) { |
| 245 FetchFinished(OptionalSnippets(), | |
| 246 interactive_request | |
| 247 ? FetchResult::INTERACTIVE_QUOTA_ERROR | |
| 248 : FetchResult::NON_INTERACTIVE_QUOTA_ERROR, | |
| 249 /*extra_message=*/std::string()); | |
| 222 return; | 250 return; |
| 251 } | |
| 223 | 252 |
| 224 hosts_ = hosts; | 253 hosts_ = hosts; |
| 225 fetch_start_time_ = tick_clock_->NowTicks(); | 254 fetch_start_time_ = tick_clock_->NowTicks(); |
| 226 | 255 |
| 227 if (UsesHostRestrictions() && hosts_.empty()) { | 256 if (UsesHostRestrictions() && hosts_.empty()) { |
| 228 FetchFinished(OptionalSnippets(), FetchResult::EMPTY_HOSTS, | 257 FetchFinished(OptionalSnippets(), FetchResult::EMPTY_HOSTS, |
| 229 /*extra_message=*/std::string()); | 258 /*extra_message=*/std::string()); |
| 230 return; | 259 return; |
| 231 } | 260 } |
| 232 | 261 |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 578 OptionalSnippets(), FetchResult::JSON_PARSE_ERROR, | 607 OptionalSnippets(), FetchResult::JSON_PARSE_ERROR, |
| 579 /*extra_message=*/base::StringPrintf(" (error %s)", error.c_str())); | 608 /*extra_message=*/base::StringPrintf(" (error %s)", error.c_str())); |
| 580 } | 609 } |
| 581 | 610 |
| 582 void NTPSnippetsFetcher::FetchFinished(OptionalSnippets snippets, | 611 void NTPSnippetsFetcher::FetchFinished(OptionalSnippets snippets, |
| 583 FetchResult result, | 612 FetchResult result, |
| 584 const std::string& extra_message) { | 613 const std::string& extra_message) { |
| 585 DCHECK(result == FetchResult::SUCCESS || !snippets); | 614 DCHECK(result == FetchResult::SUCCESS || !snippets); |
| 586 last_status_ = FetchResultToString(result) + extra_message; | 615 last_status_ = FetchResultToString(result) + extra_message; |
| 587 | 616 |
| 588 // If the result is EMPTY_HOSTS or OAUTH_TOKEN_ERROR, we didn't actually send | 617 // Don't record FetchTimes if the result indicates that a precondition |
| 589 // a network request, so don't record FetchTime in those cases. | 618 // failed and we never actually sent a network request |
| 590 if (result != FetchResult::EMPTY_HOSTS && | 619 if (!IsFetchPreconditionFailed(result)) { |
| 591 result != FetchResult::OAUTH_TOKEN_ERROR) { | |
| 592 UMA_HISTOGRAM_TIMES("NewTabPage.Snippets.FetchTime", | 620 UMA_HISTOGRAM_TIMES("NewTabPage.Snippets.FetchTime", |
| 593 tick_clock_->NowTicks() - fetch_start_time_); | 621 tick_clock_->NowTicks() - fetch_start_time_); |
| 594 } | 622 } |
| 595 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", | 623 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", |
| 596 static_cast<int>(result), | 624 static_cast<int>(result), |
| 597 static_cast<int>(FetchResult::RESULT_MAX)); | 625 static_cast<int>(FetchResult::RESULT_MAX)); |
| 598 | 626 |
| 599 if (!snippets_available_callback_.is_null()) | 627 if (!snippets_available_callback_.is_null()) |
| 600 snippets_available_callback_.Run(std::move(snippets)); | 628 snippets_available_callback_.Run(std::move(snippets)); |
| 601 } | 629 } |
| 602 | 630 |
| 603 } // namespace ntp_snippets | 631 } // namespace ntp_snippets |
| OLD | NEW |