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) { | |
Marc Treib
2016/08/25 14:19:05
nit: IsFetchPreconditionFailed (start with a capit
| |
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 default: | |
Marc Treib
2016/08/25 14:19:05
Instead of a default, I'd prefer to list the value
| |
108 return false; | |
109 } | |
110 } | |
111 | |
96 std::string GetFetchEndpoint() { | 112 std::string GetFetchEndpoint() { |
97 std::string endpoint = variations::GetVariationParamValue( | 113 std::string endpoint = variations::GetVariationParamValue( |
98 ntp_snippets::kStudyName, kContentSuggestionsBackend); | 114 ntp_snippets::kStudyName, kContentSuggestionsBackend); |
99 return endpoint.empty() ? kChromeReaderServer : endpoint; | 115 return endpoint.empty() ? kChromeReaderServer : endpoint; |
100 } | 116 } |
101 | 117 |
102 bool UsesChromeContentSuggestionsAPI(const GURL& endpoint) { | 118 bool UsesChromeContentSuggestionsAPI(const GURL& endpoint) { |
103 if (endpoint == GURL(kChromeReaderServer)) { | 119 if (endpoint == GURL(kChromeReaderServer)) { |
104 return false; | 120 return false; |
105 } else if (endpoint != GURL(kContentSuggestionsServer) && | 121 } else if (endpoint != GURL(kContentSuggestionsServer) && |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
211 void NTPSnippetsFetcher::SetCallback( | 227 void NTPSnippetsFetcher::SetCallback( |
212 const SnippetsAvailableCallback& callback) { | 228 const SnippetsAvailableCallback& callback) { |
213 snippets_available_callback_ = callback; | 229 snippets_available_callback_ = callback; |
214 } | 230 } |
215 | 231 |
216 void NTPSnippetsFetcher::FetchSnippetsFromHosts( | 232 void NTPSnippetsFetcher::FetchSnippetsFromHosts( |
217 const std::set<std::string>& hosts, | 233 const std::set<std::string>& hosts, |
218 const std::string& language_code, | 234 const std::string& language_code, |
219 int count, | 235 int count, |
220 bool interactive_request) { | 236 bool interactive_request) { |
221 if (!request_throttler_.DemandQuotaForRequest(interactive_request)) | 237 if (!request_throttler_.DemandQuotaForRequest(interactive_request)) { |
238 FetchFinished( | |
239 OptionalSnippets(), | |
240 interactive_request ? | |
241 FetchResult::INTERACTIVE_QUOTA_ERROR : | |
Marc Treib
2016/08/25 14:19:05
nit: wrong indentation I think? (You could try "gi
| |
242 FetchResult::NON_INTERACTIVE_QUOTA_ERROR, | |
243 /*extra_message=*/std::string()); | |
222 return; | 244 return; |
245 } | |
223 | 246 |
224 hosts_ = hosts; | 247 hosts_ = hosts; |
225 fetch_start_time_ = tick_clock_->NowTicks(); | 248 fetch_start_time_ = tick_clock_->NowTicks(); |
226 | 249 |
227 if (UsesHostRestrictions() && hosts_.empty()) { | 250 if (UsesHostRestrictions() && hosts_.empty()) { |
228 FetchFinished(OptionalSnippets(), FetchResult::EMPTY_HOSTS, | 251 FetchFinished(OptionalSnippets(), FetchResult::EMPTY_HOSTS, |
229 /*extra_message=*/std::string()); | 252 /*extra_message=*/std::string()); |
230 return; | 253 return; |
231 } | 254 } |
232 | 255 |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
578 OptionalSnippets(), FetchResult::JSON_PARSE_ERROR, | 601 OptionalSnippets(), FetchResult::JSON_PARSE_ERROR, |
579 /*extra_message=*/base::StringPrintf(" (error %s)", error.c_str())); | 602 /*extra_message=*/base::StringPrintf(" (error %s)", error.c_str())); |
580 } | 603 } |
581 | 604 |
582 void NTPSnippetsFetcher::FetchFinished(OptionalSnippets snippets, | 605 void NTPSnippetsFetcher::FetchFinished(OptionalSnippets snippets, |
583 FetchResult result, | 606 FetchResult result, |
584 const std::string& extra_message) { | 607 const std::string& extra_message) { |
585 DCHECK(result == FetchResult::SUCCESS || !snippets); | 608 DCHECK(result == FetchResult::SUCCESS || !snippets); |
586 last_status_ = FetchResultToString(result) + extra_message; | 609 last_status_ = FetchResultToString(result) + extra_message; |
587 | 610 |
588 // If the result is EMPTY_HOSTS or OAUTH_TOKEN_ERROR, we didn't actually send | 611 // Don't record FetchTimes if the result indicates that a precondition |
589 // a network request, so don't record FetchTime in those cases. | 612 // failed and we never actually sent a network request |
590 if (result != FetchResult::EMPTY_HOSTS && | 613 if (!isFetchPreconditionFailed(result)) { |
591 result != FetchResult::OAUTH_TOKEN_ERROR) { | |
592 UMA_HISTOGRAM_TIMES("NewTabPage.Snippets.FetchTime", | 614 UMA_HISTOGRAM_TIMES("NewTabPage.Snippets.FetchTime", |
593 tick_clock_->NowTicks() - fetch_start_time_); | 615 tick_clock_->NowTicks() - fetch_start_time_); |
594 } | 616 } |
595 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", | 617 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", |
596 static_cast<int>(result), | 618 static_cast<int>(result), |
597 static_cast<int>(FetchResult::RESULT_MAX)); | 619 static_cast<int>(FetchResult::RESULT_MAX)); |
598 | 620 |
599 if (!snippets_available_callback_.is_null()) | 621 if (!snippets_available_callback_.is_null()) |
600 snippets_available_callback_.Run(std::move(snippets)); | 622 snippets_available_callback_.Run(std::move(snippets)); |
601 } | 623 } |
602 | 624 |
603 } // namespace ntp_snippets | 625 } // namespace ntp_snippets |
OLD | NEW |