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/remote/remote_suggestions_fetcher.h" | 5 #include "components/ntp_snippets/remote/remote_suggestions_fetcher.h" |
6 | 6 |
7 #include <cstdlib> | 7 #include <cstdlib> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 | 54 |
55 // Variation parameter for chrome-content-suggestions backend. | 55 // Variation parameter for chrome-content-suggestions backend. |
56 const char kContentSuggestionsBackend[] = "content_suggestions_backend"; | 56 const char kContentSuggestionsBackend[] = "content_suggestions_backend"; |
57 | 57 |
58 const int kFetchTimeHistogramResolution = 5; | 58 const int kFetchTimeHistogramResolution = 5; |
59 | 59 |
60 std::string FetchResultToString(FetchResult result) { | 60 std::string FetchResultToString(FetchResult result) { |
61 switch (result) { | 61 switch (result) { |
62 case FetchResult::SUCCESS: | 62 case FetchResult::SUCCESS: |
63 return "OK"; | 63 return "OK"; |
64 case FetchResult::DEPRECATED_EMPTY_HOSTS: | |
65 return "Cannot fetch for empty hosts list."; | |
66 case FetchResult::URL_REQUEST_STATUS_ERROR: | 64 case FetchResult::URL_REQUEST_STATUS_ERROR: |
67 return "URLRequestStatus error"; | 65 return "URLRequestStatus error"; |
68 case FetchResult::HTTP_ERROR: | 66 case FetchResult::HTTP_ERROR: |
69 return "HTTP error"; | 67 return "HTTP error"; |
70 case FetchResult::JSON_PARSE_ERROR: | 68 case FetchResult::JSON_PARSE_ERROR: |
71 return "Received invalid JSON"; | 69 return "Received invalid JSON"; |
72 case FetchResult::INVALID_SNIPPET_CONTENT_ERROR: | 70 case FetchResult::INVALID_SNIPPET_CONTENT_ERROR: |
73 return "Invalid / empty list."; | 71 return "Invalid / empty list."; |
74 case FetchResult::OAUTH_TOKEN_ERROR: | 72 case FetchResult::OAUTH_TOKEN_ERROR: |
75 return "Error in obtaining an OAuth2 access token."; | 73 return "Error in obtaining an OAuth2 access token."; |
76 case FetchResult::INTERACTIVE_QUOTA_ERROR: | |
77 return "Out of interactive quota."; | |
78 case FetchResult::NON_INTERACTIVE_QUOTA_ERROR: | |
79 return "Out of non-interactive quota."; | |
80 case FetchResult::RESULT_MAX: | 74 case FetchResult::RESULT_MAX: |
81 break; | 75 break; |
82 } | 76 } |
83 NOTREACHED(); | 77 NOTREACHED(); |
84 return "Unknown error"; | 78 return "Unknown error"; |
85 } | 79 } |
86 | 80 |
87 Status FetchResultToStatus(FetchResult result) { | 81 Status FetchResultToStatus(FetchResult result) { |
88 switch (result) { | 82 switch (result) { |
89 case FetchResult::SUCCESS: | 83 case FetchResult::SUCCESS: |
90 return Status::Success(); | 84 return Status::Success(); |
91 // Permanent errors occur if it is more likely that the error originated | 85 // Permanent errors occur if it is more likely that the error originated |
92 // from the client. | 86 // from the client. |
93 case FetchResult::DEPRECATED_EMPTY_HOSTS: | |
94 case FetchResult::OAUTH_TOKEN_ERROR: | 87 case FetchResult::OAUTH_TOKEN_ERROR: |
95 return Status(StatusCode::PERMANENT_ERROR, FetchResultToString(result)); | 88 return Status(StatusCode::PERMANENT_ERROR, FetchResultToString(result)); |
96 // Temporary errors occur if it's more likely that the client behaved | 89 // Temporary errors occur if it's more likely that the client behaved |
97 // correctly but the server failed to respond as expected. | 90 // correctly but the server failed to respond as expected. |
98 // TODO(fhorschig): Revisit HTTP_ERROR once the rescheduling was reworked. | 91 // TODO(fhorschig): Revisit HTTP_ERROR once the rescheduling was reworked. |
99 case FetchResult::HTTP_ERROR: | 92 case FetchResult::HTTP_ERROR: |
100 case FetchResult::INTERACTIVE_QUOTA_ERROR: | |
101 case FetchResult::NON_INTERACTIVE_QUOTA_ERROR: | |
102 case FetchResult::URL_REQUEST_STATUS_ERROR: | 93 case FetchResult::URL_REQUEST_STATUS_ERROR: |
103 case FetchResult::INVALID_SNIPPET_CONTENT_ERROR: | 94 case FetchResult::INVALID_SNIPPET_CONTENT_ERROR: |
104 case FetchResult::JSON_PARSE_ERROR: | 95 case FetchResult::JSON_PARSE_ERROR: |
105 return Status(StatusCode::TEMPORARY_ERROR, FetchResultToString(result)); | 96 return Status(StatusCode::TEMPORARY_ERROR, FetchResultToString(result)); |
106 case FetchResult::RESULT_MAX: | 97 case FetchResult::RESULT_MAX: |
107 break; | 98 break; |
108 } | 99 } |
109 NOTREACHED(); | 100 NOTREACHED(); |
110 return Status(StatusCode::PERMANENT_ERROR, std::string()); | 101 return Status(StatusCode::PERMANENT_ERROR, std::string()); |
111 } | 102 } |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 token_service_(token_service), | 244 token_service_(token_service), |
254 url_request_context_getter_(std::move(url_request_context_getter)), | 245 url_request_context_getter_(std::move(url_request_context_getter)), |
255 language_model_(language_model), | 246 language_model_(language_model), |
256 parse_json_callback_(parse_json_callback), | 247 parse_json_callback_(parse_json_callback), |
257 fetch_url_(GetFetchEndpoint()), | 248 fetch_url_(GetFetchEndpoint()), |
258 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_) | 249 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_) |
259 ? FetchAPI::CHROME_CONTENT_SUGGESTIONS_API | 250 ? FetchAPI::CHROME_CONTENT_SUGGESTIONS_API |
260 : FetchAPI::CHROME_READER_API), | 251 : FetchAPI::CHROME_READER_API), |
261 api_key_(api_key), | 252 api_key_(api_key), |
262 clock_(new base::DefaultClock()), | 253 clock_(new base::DefaultClock()), |
263 user_classifier_(user_classifier), | 254 user_classifier_(user_classifier) {} |
264 request_throttler_rare_ntp_user_( | |
265 pref_service, | |
266 RequestThrottler::RequestType:: | |
267 CONTENT_SUGGESTION_FETCHER_RARE_NTP_USER), | |
268 request_throttler_active_ntp_user_( | |
269 pref_service, | |
270 RequestThrottler::RequestType:: | |
271 CONTENT_SUGGESTION_FETCHER_ACTIVE_NTP_USER), | |
272 request_throttler_active_suggestions_consumer_( | |
273 pref_service, | |
274 RequestThrottler::RequestType:: | |
275 CONTENT_SUGGESTION_FETCHER_ACTIVE_SUGGESTIONS_CONSUMER) {} | |
276 | 255 |
277 RemoteSuggestionsFetcher::~RemoteSuggestionsFetcher() { | 256 RemoteSuggestionsFetcher::~RemoteSuggestionsFetcher() { |
278 if (waiting_for_refresh_token_) { | 257 if (waiting_for_refresh_token_) { |
279 token_service_->RemoveObserver(this); | 258 token_service_->RemoveObserver(this); |
280 } | 259 } |
281 } | 260 } |
282 | 261 |
283 void RemoteSuggestionsFetcher::FetchSnippets( | 262 void RemoteSuggestionsFetcher::FetchSnippets( |
284 const RequestParams& params, | 263 const RequestParams& params, |
285 SnippetsAvailableCallback callback) { | 264 SnippetsAvailableCallback callback) { |
286 if (!DemandQuotaForRequest(params.interactive_request)) { | |
287 FetchFinished(OptionalFetchedCategories(), std::move(callback), | |
288 params.interactive_request | |
289 ? FetchResult::INTERACTIVE_QUOTA_ERROR | |
290 : FetchResult::NON_INTERACTIVE_QUOTA_ERROR, | |
291 /*error_details=*/std::string()); | |
292 return; | |
293 } | |
294 | |
295 if (!params.interactive_request) { | 265 if (!params.interactive_request) { |
296 UMA_HISTOGRAM_SPARSE_SLOWLY( | 266 UMA_HISTOGRAM_SPARSE_SLOWLY( |
297 "NewTabPage.Snippets.FetchTimeLocal", | 267 "NewTabPage.Snippets.FetchTimeLocal", |
298 GetMinuteOfTheDay(/*local_time=*/true, | 268 GetMinuteOfTheDay(/*local_time=*/true, |
299 /*reduced_resolution=*/true, clock_.get())); | 269 /*reduced_resolution=*/true, clock_.get())); |
300 UMA_HISTOGRAM_SPARSE_SLOWLY( | 270 UMA_HISTOGRAM_SPARSE_SLOWLY( |
301 "NewTabPage.Snippets.FetchTimeUTC", | 271 "NewTabPage.Snippets.FetchTimeUTC", |
302 GetMinuteOfTheDay(/*local_time=*/false, | 272 GetMinuteOfTheDay(/*local_time=*/false, |
303 /*reduced_resolution=*/true, clock_.get())); | 273 /*reduced_resolution=*/true, clock_.get())); |
304 } | 274 } |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 } | 534 } |
565 categories->back().suggestions = std::move(suggestions); | 535 categories->back().suggestions = std::move(suggestions); |
566 } | 536 } |
567 return true; | 537 return true; |
568 } | 538 } |
569 } | 539 } |
570 NOTREACHED(); | 540 NOTREACHED(); |
571 return false; | 541 return false; |
572 } | 542 } |
573 | 543 |
574 bool RemoteSuggestionsFetcher::DemandQuotaForRequest(bool interactive_request) { | |
575 switch (user_classifier_->GetUserClass()) { | |
576 case UserClassifier::UserClass::RARE_NTP_USER: | |
577 return request_throttler_rare_ntp_user_.DemandQuotaForRequest( | |
578 interactive_request); | |
579 case UserClassifier::UserClass::ACTIVE_NTP_USER: | |
580 return request_throttler_active_ntp_user_.DemandQuotaForRequest( | |
581 interactive_request); | |
582 case UserClassifier::UserClass::ACTIVE_SUGGESTIONS_CONSUMER: | |
583 return request_throttler_active_suggestions_consumer_ | |
584 .DemandQuotaForRequest(interactive_request); | |
585 } | |
586 NOTREACHED(); | |
587 return false; | |
588 } | |
589 | |
590 } // namespace ntp_snippets | 544 } // namespace ntp_snippets |
OLD | NEW |