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

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

Issue 2158843002: Introduce a request throttler for limiting requests in mobile NTP. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Marc's comments #4 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/ntp_snippets/request_counter.h"
6
7 #include <vector>
8
9 #include "base/metrics/histogram.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/time/time.h"
14 #include "components/ntp_snippets/ntp_snippets_constants.h"
15 #include "components/ntp_snippets/pref_names.h"
16 #include "components/prefs/pref_registry_simple.h"
17 #include "components/prefs/pref_service.h"
18 #include "components/variations/variations_associated_data.h"
19
20 namespace {
21
22 const char kQuotaParamPrefix[] = "quota_";
23 const char kUMAHistogramTotalFormat[] =
24 "NewTabPage.RequestCounter.RequestStatus_%s";
25 const char kUMAHistogramPerDayFormat[] = "NewTabPage.RequestCounter.PerDay_%s";
26
27 // When adding a new type here, extend also the "RequestCounterTypes"
28 // <histogram_suffixes> in histograms.xml with the |name| string.
29 using ntp_snippets::RequestCounter;
Marc Treib 2016/07/20 16:15:14 You can also put the anonymous namespace into the
jkrcal 2016/07/21 07:44:28 Done.
30 const RequestCounter::RequestTypeInfo kRequestTypeInfo[] = {
31 // RequestCounter::RequestType::ARTICLE_CONTENT_FETCHER,
32 {"Fetcher", ntp_snippets::prefs::kSnippetFetcherQuotaCount,
33 ntp_snippets::prefs::kSnippetFetcherQuotaDay}};
34
35 } // namespace
36
37 namespace ntp_snippets {
38
39 RequestCounter::RequestCounter(PrefService* pref_service,
40 RequestType type,
41 int default_quota)
42 : pref_service_(pref_service),
43 type_info_(kRequestTypeInfo[static_cast<int>(type)]) {
44 DCHECK(pref_service);
45
46 std::string quota = variations::GetVariationParamValue(
47 ntp_snippets::kStudyName, kQuotaParamPrefix + GetRequestTypeAsString());
48 if (!base::StringToInt(quota, &quota_)) {
49 LOG_IF(WARNING, !quota.empty())
50 << "Invalid variation parameter for quota for "
51 << GetRequestTypeAsString();
52 quota_ = default_quota;
53 }
54
55 // Lookup the histograms.
56 int status_count = static_cast<int>(RequestStatus::REQUEST_STATUS_COUNT);
57 histogram_request_status_ = base::LinearHistogram::FactoryGet(
58 base::StringPrintf(kUMAHistogramTotalFormat,
59 GetRequestTypeAsString().c_str()),
60 1, status_count, status_count + 1,
61 base::HistogramBase::kUmaTargetedHistogramFlag);
62
63 histogram_per_day_ = base::Histogram::FactoryGet(
64 base::StringPrintf(kUMAHistogramPerDayFormat,
65 GetRequestTypeAsString().c_str()),
66 1, 100, 50, base::HistogramBase::kUmaTargetedHistogramFlag);
67 }
68
69 // static
70 void RequestCounter::RegisterProfilePrefs(PrefRegistrySimple* registry) {
71 for (const RequestTypeInfo& info : kRequestTypeInfo) {
72 registry->RegisterIntegerPref(info.count_pref, 0);
73 registry->RegisterIntegerPref(info.day_pref, 0);
74 }
75 }
76
77 bool RequestCounter::DemandQuotaForRequest() {
78 ResetCounterIfDayChanged();
79
80 int new_count = GetCount() + 1;
81 SetCount(new_count);
82
83 bool available = (new_count <= quota_);
84 histogram_request_status_->Add(
85 static_cast<int>(available ? RequestStatus::QUOTA_GRANTED
86 : RequestStatus::QUOTA_EXCEEDED));
87 return available;
88 }
89
90 void RequestCounter::ReportForcedRequest() const {
91 histogram_request_status_->Add(static_cast<int>(RequestStatus::FORCED));
92 }
93
94 void RequestCounter::ResetCounterIfDayChanged() {
95 // The count of days since a fixed reference (the Unix epoch).
96 int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays();
97
98 if (!HasDay()) {
99 // The counter is used for the first time in this profile.
100 SetDay(now_day);
101 } else if (now_day != GetDay()) {
102 // Day has changed - report the number of requests from the previous day.
103 histogram_per_day_->Add(GetCount());
104 // Reset the counter.
105 SetCount(0);
106 SetDay(now_day);
107 }
108 }
109
110 std::string RequestCounter::GetRequestTypeAsString() const {
111 return type_info_.name;
112 }
113
114 int RequestCounter::GetCount() const {
115 return pref_service_->GetInteger(type_info_.count_pref);
116 }
117
118 void RequestCounter::SetCount(int count) {
119 pref_service_->SetInteger(type_info_.count_pref, count);
120 }
121
122 int RequestCounter::GetDay() const {
123 return pref_service_->GetInteger(type_info_.day_pref);
124 }
125
126 void RequestCounter::SetDay(int day) {
127 pref_service_->SetInteger(type_info_.day_pref, day);
128 }
129
130 bool RequestCounter::HasDay() const {
131 return pref_service_->HasPrefPath(type_info_.day_pref);
132 }
133
134 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698