Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: components/ntp_snippets/remote/remote_suggestions_fetcher.cc

Issue 2714863004: [Remote suggestions] Move the throttler into the Scheduler (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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: 64 case FetchResult::DEPRECATED_EMPTY_HOSTS:
Marc Treib 2017/02/24 15:50:35 Could you move this into the "invalid" section too
jkrcal 2017/02/27 10:26:23 Done.
65 return "Cannot fetch for empty hosts list."; 65 return "Cannot fetch for empty hosts list.";
66 case FetchResult::URL_REQUEST_STATUS_ERROR: 66 case FetchResult::URL_REQUEST_STATUS_ERROR:
67 return "URLRequestStatus error"; 67 return "URLRequestStatus error";
68 case FetchResult::HTTP_ERROR: 68 case FetchResult::HTTP_ERROR:
69 return "HTTP error"; 69 return "HTTP error";
70 case FetchResult::JSON_PARSE_ERROR: 70 case FetchResult::JSON_PARSE_ERROR:
71 return "Received invalid JSON"; 71 return "Received invalid JSON";
72 case FetchResult::INVALID_SNIPPET_CONTENT_ERROR: 72 case FetchResult::INVALID_SNIPPET_CONTENT_ERROR:
73 return "Invalid / empty list."; 73 return "Invalid / empty list.";
74 case FetchResult::OAUTH_TOKEN_ERROR: 74 case FetchResult::OAUTH_TOKEN_ERROR:
75 return "Error in obtaining an OAuth2 access token."; 75 return "Error in obtaining an OAuth2 access token.";
76 case FetchResult::INTERACTIVE_QUOTA_ERROR: 76 // Enum elements that do not represent any valid fetch result.
77 return "Out of interactive quota."; 77 case FetchResult::INTERACTIVE_QUOTA_ERROR_OBSOLETE:
78 case FetchResult::NON_INTERACTIVE_QUOTA_ERROR: 78 case FetchResult::NON_INTERACTIVE_QUOTA_ERROR_OBSOLETE:
79 return "Out of non-interactive quota.";
80 case FetchResult::RESULT_MAX: 79 case FetchResult::RESULT_MAX:
81 break; 80 break;
82 } 81 }
83 NOTREACHED(); 82 NOTREACHED();
84 return "Unknown error"; 83 return "Unknown error";
85 } 84 }
86 85
87 Status FetchResultToStatus(FetchResult result) { 86 Status FetchResultToStatus(FetchResult result) {
88 switch (result) { 87 switch (result) {
89 case FetchResult::SUCCESS: 88 case FetchResult::SUCCESS:
90 return Status::Success(); 89 return Status::Success();
91 // Permanent errors occur if it is more likely that the error originated 90 // Permanent errors occur if it is more likely that the error originated
92 // from the client. 91 // from the client.
93 case FetchResult::DEPRECATED_EMPTY_HOSTS: 92 case FetchResult::DEPRECATED_EMPTY_HOSTS:
Marc Treib 2017/02/24 15:50:35 Also here
jkrcal 2017/02/27 10:26:23 Done.
94 case FetchResult::OAUTH_TOKEN_ERROR: 93 case FetchResult::OAUTH_TOKEN_ERROR:
95 return Status(StatusCode::PERMANENT_ERROR, FetchResultToString(result)); 94 return Status(StatusCode::PERMANENT_ERROR, FetchResultToString(result));
96 // Temporary errors occur if it's more likely that the client behaved 95 // Temporary errors occur if it's more likely that the client behaved
97 // correctly but the server failed to respond as expected. 96 // correctly but the server failed to respond as expected.
98 // TODO(fhorschig): Revisit HTTP_ERROR once the rescheduling was reworked. 97 // TODO(fhorschig): Revisit HTTP_ERROR once the rescheduling was reworked.
99 case FetchResult::HTTP_ERROR: 98 case FetchResult::HTTP_ERROR:
100 case FetchResult::INTERACTIVE_QUOTA_ERROR:
101 case FetchResult::NON_INTERACTIVE_QUOTA_ERROR:
102 case FetchResult::URL_REQUEST_STATUS_ERROR: 99 case FetchResult::URL_REQUEST_STATUS_ERROR:
103 case FetchResult::INVALID_SNIPPET_CONTENT_ERROR: 100 case FetchResult::INVALID_SNIPPET_CONTENT_ERROR:
104 case FetchResult::JSON_PARSE_ERROR: 101 case FetchResult::JSON_PARSE_ERROR:
105 return Status(StatusCode::TEMPORARY_ERROR, FetchResultToString(result)); 102 return Status(StatusCode::TEMPORARY_ERROR, FetchResultToString(result));
103 // Enum elements that do not represent any valid fetch result.
104 case FetchResult::INTERACTIVE_QUOTA_ERROR_OBSOLETE:
105 case FetchResult::NON_INTERACTIVE_QUOTA_ERROR_OBSOLETE:
106 case FetchResult::RESULT_MAX: 106 case FetchResult::RESULT_MAX:
107 break; 107 break;
108 } 108 }
109 NOTREACHED(); 109 NOTREACHED();
110 return Status(StatusCode::PERMANENT_ERROR, std::string()); 110 return Status(StatusCode::PERMANENT_ERROR, std::string());
111 } 111 }
112 112
113 std::string GetFetchEndpoint() { 113 std::string GetFetchEndpoint() {
114 std::string endpoint = variations::GetVariationParamValueByFeature( 114 std::string endpoint = variations::GetVariationParamValueByFeature(
115 ntp_snippets::kArticleSuggestionsFeature, kContentSuggestionsBackend); 115 ntp_snippets::kArticleSuggestionsFeature, kContentSuggestionsBackend);
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 token_service_(token_service), 253 token_service_(token_service),
254 url_request_context_getter_(std::move(url_request_context_getter)), 254 url_request_context_getter_(std::move(url_request_context_getter)),
255 language_model_(language_model), 255 language_model_(language_model),
256 parse_json_callback_(parse_json_callback), 256 parse_json_callback_(parse_json_callback),
257 fetch_url_(GetFetchEndpoint()), 257 fetch_url_(GetFetchEndpoint()),
258 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_) 258 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_)
259 ? FetchAPI::CHROME_CONTENT_SUGGESTIONS_API 259 ? FetchAPI::CHROME_CONTENT_SUGGESTIONS_API
260 : FetchAPI::CHROME_READER_API), 260 : FetchAPI::CHROME_READER_API),
261 api_key_(api_key), 261 api_key_(api_key),
262 clock_(new base::DefaultClock()), 262 clock_(new base::DefaultClock()),
263 user_classifier_(user_classifier), 263 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 264
277 RemoteSuggestionsFetcher::~RemoteSuggestionsFetcher() { 265 RemoteSuggestionsFetcher::~RemoteSuggestionsFetcher() {
278 if (waiting_for_refresh_token_) { 266 if (waiting_for_refresh_token_) {
279 token_service_->RemoveObserver(this); 267 token_service_->RemoveObserver(this);
280 } 268 }
281 } 269 }
282 270
283 void RemoteSuggestionsFetcher::FetchSnippets( 271 void RemoteSuggestionsFetcher::FetchSnippets(
284 const RequestParams& params, 272 const RequestParams& params,
285 SnippetsAvailableCallback callback) { 273 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) { 274 if (!params.interactive_request) {
296 UMA_HISTOGRAM_SPARSE_SLOWLY( 275 UMA_HISTOGRAM_SPARSE_SLOWLY(
297 "NewTabPage.Snippets.FetchTimeLocal", 276 "NewTabPage.Snippets.FetchTimeLocal",
298 GetMinuteOfTheDay(/*local_time=*/true, 277 GetMinuteOfTheDay(/*local_time=*/true,
299 /*reduced_resolution=*/true, clock_.get())); 278 /*reduced_resolution=*/true, clock_.get()));
300 UMA_HISTOGRAM_SPARSE_SLOWLY( 279 UMA_HISTOGRAM_SPARSE_SLOWLY(
301 "NewTabPage.Snippets.FetchTimeUTC", 280 "NewTabPage.Snippets.FetchTimeUTC",
302 GetMinuteOfTheDay(/*local_time=*/false, 281 GetMinuteOfTheDay(/*local_time=*/false,
303 /*reduced_resolution=*/true, clock_.get())); 282 /*reduced_resolution=*/true, clock_.get()));
304 } 283 }
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
564 } 543 }
565 categories->back().suggestions = std::move(suggestions); 544 categories->back().suggestions = std::move(suggestions);
566 } 545 }
567 return true; 546 return true;
568 } 547 }
569 } 548 }
570 NOTREACHED(); 549 NOTREACHED();
571 return false; 550 return false;
572 } 551 }
573 552
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 553 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698