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

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: Bernhard's comments Created 4 years, 4 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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 << "Error in translating language code to a locale string: " << error; 151 << "Error in translating language code to a locale string: " << error;
152 return locale; 152 return locale;
153 } 153 }
154 154
155 } // namespace 155 } // namespace
156 156
157 NTPSnippetsFetcher::NTPSnippetsFetcher( 157 NTPSnippetsFetcher::NTPSnippetsFetcher(
158 SigninManagerBase* signin_manager, 158 SigninManagerBase* signin_manager,
159 OAuth2TokenService* token_service, 159 OAuth2TokenService* token_service,
160 scoped_refptr<URLRequestContextGetter> url_request_context_getter, 160 scoped_refptr<URLRequestContextGetter> url_request_context_getter,
161 PrefService* pref_service,
161 const ParseJSONCallback& parse_json_callback, 162 const ParseJSONCallback& parse_json_callback,
162 bool is_stable_channel) 163 bool is_stable_channel)
163 : OAuth2TokenService::Consumer("ntp_snippets"), 164 : OAuth2TokenService::Consumer("ntp_snippets"),
164 signin_manager_(signin_manager), 165 signin_manager_(signin_manager),
165 token_service_(token_service), 166 token_service_(token_service),
166 waiting_for_refresh_token_(false), 167 waiting_for_refresh_token_(false),
167 url_request_context_getter_(url_request_context_getter), 168 url_request_context_getter_(url_request_context_getter),
168 parse_json_callback_(parse_json_callback), 169 parse_json_callback_(parse_json_callback),
169 fetch_url_(GetFetchEndpoint()), 170 fetch_url_(GetFetchEndpoint()),
170 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_) 171 fetch_api_(UsesChromeContentSuggestionsAPI(fetch_url_)
171 ? CHROME_CONTENT_SUGGESTIONS_API 172 ? CHROME_CONTENT_SUGGESTIONS_API
172 : CHROME_READER_API), 173 : CHROME_READER_API),
173 is_stable_channel_(is_stable_channel), 174 is_stable_channel_(is_stable_channel),
174 tick_clock_(new base::DefaultTickClock()), 175 tick_clock_(new base::DefaultTickClock()),
176 request_throttler_(
177 pref_service,
178 RequestThrottler::RequestType::CONTENT_SUGGESTION_FETCHER),
175 weak_ptr_factory_(this) { 179 weak_ptr_factory_(this) {
176 // Parse the variation parameters and set the defaults if missing. 180 // Parse the variation parameters and set the defaults if missing.
177 std::string personalization = variations::GetVariationParamValue( 181 std::string personalization = variations::GetVariationParamValue(
178 ntp_snippets::kStudyName, kPersonalizationName); 182 ntp_snippets::kStudyName, kPersonalizationName);
179 if (personalization == kPersonalizationNonPersonalString) { 183 if (personalization == kPersonalizationNonPersonalString) {
180 personalization_ = Personalization::kNonPersonal; 184 personalization_ = Personalization::kNonPersonal;
181 } else if (personalization == kPersonalizationPersonalString) { 185 } else if (personalization == kPersonalizationPersonalString) {
182 personalization_ = Personalization::kPersonal; 186 personalization_ = Personalization::kPersonal;
183 } else { 187 } else {
184 personalization_ = Personalization::kBoth; 188 personalization_ = Personalization::kBoth;
(...skipping 22 matching lines...) Expand all
207 } 211 }
208 212
209 void NTPSnippetsFetcher::SetCallback( 213 void NTPSnippetsFetcher::SetCallback(
210 const SnippetsAvailableCallback& callback) { 214 const SnippetsAvailableCallback& callback) {
211 snippets_available_callback_ = callback; 215 snippets_available_callback_ = callback;
212 } 216 }
213 217
214 void NTPSnippetsFetcher::FetchSnippetsFromHosts( 218 void NTPSnippetsFetcher::FetchSnippetsFromHosts(
215 const std::set<std::string>& hosts, 219 const std::set<std::string>& hosts,
216 const std::string& language_code, 220 const std::string& language_code,
217 int count) { 221 int count,
222 bool force_request) {
223 if (!request_throttler_.DemandQuotaForRequest(force_request))
224 return;
225
218 hosts_ = hosts; 226 hosts_ = hosts;
219 fetch_start_time_ = tick_clock_->NowTicks(); 227 fetch_start_time_ = tick_clock_->NowTicks();
220 228
221 if (UsesHostRestrictions() && hosts_.empty()) { 229 if (UsesHostRestrictions() && hosts_.empty()) {
222 FetchFinished(OptionalSnippets(), FetchResult::EMPTY_HOSTS, 230 FetchFinished(OptionalSnippets(), FetchResult::EMPTY_HOSTS,
223 /*extra_message=*/std::string()); 231 /*extra_message=*/std::string());
224 return; 232 return;
225 } 233 }
226 234
227 locale_ = PosixLocaleFromBCP47Language(language_code); 235 locale_ = PosixLocaleFromBCP47Language(language_code);
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 tick_clock_->NowTicks() - fetch_start_time_); 532 tick_clock_->NowTicks() - fetch_start_time_);
525 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult", 533 UMA_HISTOGRAM_ENUMERATION("NewTabPage.Snippets.FetchResult",
526 static_cast<int>(result), 534 static_cast<int>(result),
527 static_cast<int>(FetchResult::RESULT_MAX)); 535 static_cast<int>(FetchResult::RESULT_MAX));
528 536
529 if (!snippets_available_callback_.is_null()) 537 if (!snippets_available_callback_.is_null())
530 snippets_available_callback_.Run(std::move(snippets)); 538 snippets_available_callback_.Run(std::move(snippets));
531 } 539 }
532 540
533 } // namespace ntp_snippets 541 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_fetcher.h ('k') | components/ntp_snippets/ntp_snippets_fetcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698