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

Side by Side Diff: components/ntp_snippets/ntp_snippets_fetcher.cc

Issue 2162533002: Adding a request counter to the snippets fetcher. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@traffic-monitor
Patch Set: Rebase + rename to throttler Created 4 years, 5 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/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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 69
70 // Constants for possible values of the "fetching_personalization" parameter. 70 // Constants for possible values of the "fetching_personalization" parameter.
71 const char kPersonalizationPersonalString[] = "personal"; 71 const char kPersonalizationPersonalString[] = "personal";
72 const char kPersonalizationNonPersonalString[] = "non_personal"; 72 const char kPersonalizationNonPersonalString[] = "non_personal";
73 const char kPersonalizationBothString[] = "both"; // the default value 73 const char kPersonalizationBothString[] = "both"; // the default value
74 74
75 // Constants for possible values of the "fetching_host_restrict" parameter. 75 // Constants for possible values of the "fetching_host_restrict" parameter.
76 const char kHostRestrictionOnString[] = "on"; // the default value 76 const char kHostRestrictionOnString[] = "on"; // the default value
77 const char kHostRestrictionOffString[] = "off"; 77 const char kHostRestrictionOffString[] = "off";
78 78
79 const int kArticlesQuotaPerDay = 20;
Marc Treib 2016/07/22 13:52:52 This sounds like "number of articles per day", but
jkrcal 2016/07/25 10:05:38 Done. Overriding by variation params is built in
Marc Treib 2016/07/25 10:11:55 I see... this is surprising to me. Since everythin
jkrcal 2016/07/25 10:26:59 Makes sense. Done.
80
79 std::string FetchResultToString(NTPSnippetsFetcher::FetchResult result) { 81 std::string FetchResultToString(NTPSnippetsFetcher::FetchResult result) {
80 switch (result) { 82 switch (result) {
81 case NTPSnippetsFetcher::FetchResult::SUCCESS: 83 case NTPSnippetsFetcher::FetchResult::SUCCESS:
82 return "OK"; 84 return "OK";
83 case NTPSnippetsFetcher::FetchResult::EMPTY_HOSTS: 85 case NTPSnippetsFetcher::FetchResult::EMPTY_HOSTS:
84 return "Cannot fetch for empty hosts list."; 86 return "Cannot fetch for empty hosts list.";
85 case NTPSnippetsFetcher::FetchResult::URL_REQUEST_STATUS_ERROR: 87 case NTPSnippetsFetcher::FetchResult::URL_REQUEST_STATUS_ERROR:
86 return "URLRequestStatus error"; 88 return "URLRequestStatus error";
87 case NTPSnippetsFetcher::FetchResult::HTTP_ERROR: 89 case NTPSnippetsFetcher::FetchResult::HTTP_ERROR:
88 return "HTTP error"; 90 return "HTTP error";
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 << "Error in translating language code to a locale string: " << error; 153 << "Error in translating language code to a locale string: " << error;
152 return locale; 154 return locale;
153 } 155 }
154 156
155 } // namespace 157 } // namespace
156 158
157 NTPSnippetsFetcher::NTPSnippetsFetcher( 159 NTPSnippetsFetcher::NTPSnippetsFetcher(
158 SigninManagerBase* signin_manager, 160 SigninManagerBase* signin_manager,
159 OAuth2TokenService* token_service, 161 OAuth2TokenService* token_service,
160 scoped_refptr<URLRequestContextGetter> url_request_context_getter, 162 scoped_refptr<URLRequestContextGetter> url_request_context_getter,
163 PrefService* pref_service,
161 const ParseJSONCallback& parse_json_callback, 164 const ParseJSONCallback& parse_json_callback,
162 bool is_stable_channel) 165 bool is_stable_channel)
163 : OAuth2TokenService::Consumer("ntp_snippets"), 166 : OAuth2TokenService::Consumer("ntp_snippets"),
164 signin_manager_(signin_manager), 167 signin_manager_(signin_manager),
165 token_service_(token_service), 168 token_service_(token_service),
166 waiting_for_refresh_token_(false), 169 waiting_for_refresh_token_(false),
167 url_request_context_getter_(url_request_context_getter), 170 url_request_context_getter_(url_request_context_getter),
168 parse_json_callback_(parse_json_callback), 171 parse_json_callback_(parse_json_callback),
169 fetch_url_(GetFetchEndpoint()), 172 fetch_url_(GetFetchEndpoint()),
170 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_) 173 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_)
171 ? CHROME_CONTENT_SUGGESTIONS_API 174 ? CHROME_CONTENT_SUGGESTIONS_API
172 : CHROME_READER_API), 175 : CHROME_READER_API),
173 is_stable_channel_(is_stable_channel), 176 is_stable_channel_(is_stable_channel),
174 tick_clock_(new base::DefaultTickClock()), 177 tick_clock_(new base::DefaultTickClock()),
178 request_throttler_(
179 pref_service,
180 RequestThrottler::RequestType::CONTENT_SUGGESTION_FETCHER,
181 kArticlesQuotaPerDay),
175 weak_ptr_factory_(this) { 182 weak_ptr_factory_(this) {
176 // Parse the variation parameters and set the defaults if missing. 183 // Parse the variation parameters and set the defaults if missing.
177 std::string personalization = variations::GetVariationParamValue( 184 std::string personalization = variations::GetVariationParamValue(
178 ntp_snippets::kStudyName, kPersonalizationName); 185 ntp_snippets::kStudyName, kPersonalizationName);
179 if (personalization == kPersonalizationNonPersonalString) { 186 if (personalization == kPersonalizationNonPersonalString) {
180 personalization_ = Personalization::kNonPersonal; 187 personalization_ = Personalization::kNonPersonal;
181 } else if (personalization == kPersonalizationPersonalString) { 188 } else if (personalization == kPersonalizationPersonalString) {
182 personalization_ = Personalization::kPersonal; 189 personalization_ = Personalization::kPersonal;
183 } else { 190 } else {
184 personalization_ = Personalization::kBoth; 191 personalization_ = Personalization::kBoth;
(...skipping 22 matching lines...) Expand all
207 } 214 }
208 215
209 void NTPSnippetsFetcher::SetCallback( 216 void NTPSnippetsFetcher::SetCallback(
210 const SnippetsAvailableCallback& callback) { 217 const SnippetsAvailableCallback& callback) {
211 snippets_available_callback_ = callback; 218 snippets_available_callback_ = callback;
212 } 219 }
213 220
214 void NTPSnippetsFetcher::FetchSnippetsFromHosts( 221 void NTPSnippetsFetcher::FetchSnippetsFromHosts(
215 const std::set<std::string>& hosts, 222 const std::set<std::string>& hosts,
216 const std::string& language_code, 223 const std::string& language_code,
217 int count) { 224 int count,
225 bool force_request) {
226 if (!request_throttler_.DemandQuotaForRequest(force_request))
227 return;
228
218 hosts_ = hosts; 229 hosts_ = hosts;
219 fetch_start_time_ = tick_clock_->NowTicks(); 230 fetch_start_time_ = tick_clock_->NowTicks();
220 231
221 if (UsesHostRestrictions() && hosts_.empty()) { 232 if (UsesHostRestrictions() && hosts_.empty()) {
222 FetchFinished(OptionalSnippets(), FetchResult::EMPTY_HOSTS, 233 FetchFinished(OptionalSnippets(), FetchResult::EMPTY_HOSTS,
223 /*extra_message=*/std::string()); 234 /*extra_message=*/std::string());
224 return; 235 return;
225 } 236 }
226 237
227 locale_ = PosixLocaleFromBCP47Language(language_code); 238 locale_ = PosixLocaleFromBCP47Language(language_code);
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 tick_clock_->NowTicks() - fetch_start_time_); 535 tick_clock_->NowTicks() - fetch_start_time_);
525 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", 536 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult",
526 static_cast<int>(result), 537 static_cast<int>(result),
527 static_cast<int>(FetchResult::RESULT_MAX)); 538 static_cast<int>(FetchResult::RESULT_MAX));
528 539
529 if (!snippets_available_callback_.is_null()) 540 if (!snippets_available_callback_.is_null())
530 snippets_available_callback_.Run(std::move(snippets)); 541 snippets_available_callback_.Run(std::move(snippets));
531 } 542 }
532 543
533 } // namespace ntp_snippets 544 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698